﻿Adapt = function() { }

Adapt.PublicationCart = function() { }

Adapt.PublicationCart.physicalRadioId = null;
Adapt.PublicationCart.juristicalRadioId = null;
Adapt.PublicationCart.juristicalFields = null;

Adapt.PublicationCart.addToCart = function(item) {
	var frame = document.createElement('iframe');
	frame.style.display = 'none';
	
	var id = Math.floor(Math.random() * 1000000000000);
	frame.id = Adapt.PublicationCart.getFrameId(id);
	
	var addUrl = '/webpages/Добавление%20публикации%20в%20корзину.aspx?item={0}&frame={1}';
	addUrl = addUrl.replace('{0}', item).replace('{1}', id);
	
	document.body.appendChild(frame);
	frame.src = addUrl;
}

Adapt.PublicationCart.getFrameId = function(id) {
	return 'frame-' + id.toString()
}

Adapt.PublicationCart.finishFrame = function(id) {
	var frameId = Adapt.PublicationCart.getFrameId(id);
	var frame = document.getElementById(frameId);
	
	if(frame != null)
		document.body.removeChild(frame);
}

Adapt.PublicationCart.frameOnLoad = function(id) {
	if(window.parent != null)
		window.parent.Adapt.PublicationCart.finishFrame(id);
}

Adapt.PublicationCart.updateQuickCart = function(count, price) {
	var quickCartCountElement = document.getElementById('quick-publication-cart-count');
	var quickCartPriceElement = document.getElementById('quick-publication-cart-price');
	
	if(quickCartCountElement != null && quickCartPriceElement != null) {
		var countValue = parseInt(quickCartCountElement.innerText);
		
		if(countValue < count) {
			quickCartCountElement.innerText = count.toString();
			quickCartPriceElement.innerText = price;
		}
	}
}

Adapt.PublicationCart.radioClick = function(radio) {
	var able = true;
	
	if (radio.id == Adapt.PublicationCart.physicalRadioId)
		able = false;
		
		Adapt.PublicationCart.ableJuristicalFields(able);
}

Adapt.PublicationCart.ableJuristicalFields = function(able) {
	var fields = Adapt.PublicationCart.juristicalFields.split(',');
	
	for(var i = 0; i < fields.length; i++)
		Adapt.PublicationCart.ableWithChilds(document.getElementById(fields[i]), able);
}

Adapt.PublicationCart.ableWithChilds = function(element, able) {
	if(element.nodeType != 3) {
		element.disabled = !able;
		
		if (element.className == 'name disabled')
			if(able)
				element.className = element.className.replace(' disabled', '');
				
		if (element.className == 'name')
			if(!able)
				element.className += ' disabled';
				
		if(element.className == 'not-empty')
			if(able)
				element.style.visibility = 'visible';
			else
				element.style.visibility = 'hidden';
				
		if(element.tagName.toLowerCase() == 'input' || element.tagName.toLowerCase() == 'textarea' || element.tagName.toLowerCase() == 'select')
			element.disabled = !able;
		
		for(var i = 0; i < element.childNodes.length; i++)
			Adapt.PublicationCart.ableWithChilds(element.childNodes[i], able);
	}
}

Adapt.PublicationCart.validateJuristicalField = function(sender, args) {	
	args.IsValid = false;
	var radioJuristical = document.getElementById(Adapt.PublicationCart.juristicalRadioId);
	
	if (radioJuristical.checked) {
		if (args.Value != null) {
			var value = args.Value;
			
			while (value.indexOf(' ') > -1)
				value = value.replace(' ', '');

			if(value.length > 0)
				args.IsValid = true;
		}
	}
	else
		args.IsValid = true;
}

Adapt.PublicationCart.validateEMailField = function(sender, args) {
		args.IsValid = false;
		var regex = /[0-9a-zA-Z_\-\.]+@([0-9a-zA-Z_\-]+\.[0-9a-zA-Z_\-]+(\.[0-9a-zZ-Z_\-]+)*)+/;

		if(args.Value.match(regex) != null)
			args.IsValid = true;
}

Adapt.PublicationCart.orderOnChange = function(select) {
	document.location.href = '?order=' + select.options[select.selectedIndex].value.toString();
}

Adapt.PublicationCart.changeOrder = function(input) {
	document.location.href = '?order=' + document.getElementById(input).value;
}

Adapt.PublicationCart.confirmDelete = function() {
	return window.confirm('Вы уверены что хотите удалить выбраные публикации из корзины?');
}