// Закрытие окна заказа
$(document).ready(function () {
	$('#order-window-close').click(function () {
		$('#order-window-overlay, #order-window-buy-box').hide();
		return true;
	});
	$(window).resize(function () {
		if (!$('#order-window-buy-box').is(':hidden')) popup();
	});
});

// Вывод окна заказа
function open_cart_window(id,order,site) {
	var bodyHeight = $(window).height();
	var bodyWidth = $(window).width();
	var winHeight = $('#order-window-buy-box').height();
	var winWidth = $('#order-window-buy-box').width();
	var dialogTop =  (bodyHeight/2) - (winHeight/2);
	var dialogLeft = (bodyWidth/2) - (winWidth/2);
	$('#order-window-overlay').css({height:bodyHeight, width:bodyWidth}).show();
	$('#order-window-buy-box').css({top:dialogTop, left:dialogLeft}).show();

	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     document.getElementById("order-window-cont").innerHTML=xmlhttp.responseText;
		 var winHeight = $('#order-window-buy-box').height();
		 var winWidth = $('#order-window-buy-box').width();
		 var dialogTop =  (bodyHeight/2) - (winHeight/2);
		 var dialogLeft = (bodyWidth/2) - (winWidth/2);
		 $('#order-window-buy-box').css({top:dialogTop, left:dialogLeft}).popup();
	     }
	    }
	 xmlhttp.open("GET",site+"/lama-shop-window/cart/handlers/handler-win-order.php?id="+id+"&order="+order+"&r="+Math.random(),true);
	 xmlhttp.send();
}

// Подсчет цены в корзине
function count_price(addcart)
	{
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     if(xmlhttp.responseText == 0){document.getElementById("lama-order-sum").innerHTML='Значние поля "Количество" указывается цифрами и превышать значение "0"';}
         else{document.getElementById("lama-order-sum").innerHTML=xmlhttp.responseText}
	     }
	    }
	xmlhttp.open("GET",addcart.site.value+"/lama-shop-window/cart/handlers/handler-count-price.php?id="+addcart.id.value+"&order="+addcart.order.value+"&count="+addcart.count.value+"&r="+Math.random(),true);
	xmlhttp.send();
    return true;
	}

// Добавление в корзину
function add_to_cart(addcart)
	{
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     if(xmlhttp.responseText == 0){document.getElementById("lama-order-sum").innerHTML='Значние поля "Количество" указывается цифрами и превышать значение "0"';}
         else
         	{
         	document.getElementById("bay-box").innerHTML=xmlhttp.responseText;
            $('#order-window-overlay, #order-window-buy-box').hide();
         	}
	     }
	    }
	xmlhttp.open("GET",addcart.site.value+"/lama-shop-window/cart/handlers/handler-add-cart.php?id="+addcart.id.value+"&order="+addcart.order.value+"&count="+addcart.count.value+"&r="+Math.random(),true);
	xmlhttp.send();
    return true;
	}

// Перезапись значений в корзине
function update_cart(addcart)
	{
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     var arr = xmlhttp.responseText.split(/[;]/);
	     document.getElementById("price"+addcart.id.value).innerHTML=arr['0']+" руб.";
	     document.getElementById("lama-order-sum-full").innerHTML="всего сумма: <strong>"+arr['1']+" руб.</strong>";
	     document.getElementById("bay-box").innerHTML=arr['2'];
	     }
	    }
	xmlhttp.open("GET",addcart.site.value+"/lama-shop-window/cart/handlers/handler-update-cart.php?id="+addcart.id.value+"&order="+addcart.order.value+"&count="+addcart.count.value+"&r="+Math.random(),true);
	xmlhttp.send();
    return true;
	}

//
// Страница корзины
//

// Вид платильщика
function payer_type(type,order,site)
	{
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     document.getElementById("payer").innerHTML=xmlhttp.responseText;
	     }
	    }
	xmlhttp.open("GET",site+"/lama-shop-window/cart/handlers/handler-payer-type.php?type="+type+"&order="+order+"&r="+Math.random(),true);
	xmlhttp.send();
    return true;
	}

// Оформление заказа Юр.лицо
function order(order,site)
	{
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     document.getElementById("bay-box").innerHTML="Заказ оформлен";
	     document.getElementById("cart-page").innerHTML="<p>Ваш заказ отправлен и в ближайшее время будет обработан.</p>";
	     }
	    }
	xmlhttp.open("GET",site+"/lama-shop-window/cart/handlers/handler-send-order.php?order="+order+"&r="+Math.random(),true);
	xmlhttp.send();
    return true;
	}

// Оформление заказа Физ.лицо
function order_individual(ind,order,site)
	{
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
	else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	xmlhttp.onreadystatechange=function()
		{
	   	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	     {
	     if(xmlhttp.responseText == 0){document.getElementById("lama-individual-msg").innerHTML='<span>Пожалуйста заполните все обязательные поля, обозначенные звездочкой.</span>';}
         else
         	{
	     	document.getElementById("bay-box").innerHTML="Заказ оформлен";
	     	document.getElementById("cart-page").innerHTML="<p>Ваш заказ отправлен и в ближайшее время будет обработан.</p>";
	     	}
	     }
	    }
	xmlhttp.open("GET",site+"/lama-shop-window/cart/handlers/handler-send-order.php?f1="+ind.f1.value+"&f2="+ind.f2.value+"&f3="+ind.f3.value+"&f4="+ind.f4.value+"&f5="+ind.f5.value+"&f6="+ind.f6.value+"&f7="+ind.f7.value+"&order="+order+"&r="+Math.random(),true);
	xmlhttp.send();
    return true;
	}
