var xmlHttp = createXmlHttpRequestObject ();

function createXmlHttpRequestObject () {
	var xmlHttp;
	
	if (window.ActiveXObject) {
		try {	xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); }
		catch (e) { xmlHttp = false; }
	} else {
		try { xmlHttp = new XMLHttpRequest (); }
		catch (e) { xmlHttp = false; }
	}
	
	if (!xmlHttp) alert ("Ошибка создания XMLHttpRequest");
	else return xmlHttp;
}

function process (id) {   

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		document.getElementById("ajax-indicator").style.display="inline";
		xmlHttp.open ("GET","../../myax.php?qw="+Math.random()+"&id="+id, true);
		xmlHttp.onreadystatechange = handleServerResponse;
 		xmlHttp.send (null);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}

function handleServerResponse ()
{
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200) {
			
			var p = xmlHttp.responseText.split("|");
			
			document.getElementById('ajax-indicator').style.display='none';
			
			if (p[0]==1 || p[0]==21) tovar_mes='товар';
			else {
				if (p[0]==2 || p[0]==3 || p[0]==4) tovar_mes='товара'; 
				else tovar_mes='товаров'; 
			}
			
			document.getElementById ("divSum").innerHTML = '('+p[0]+' '+tovar_mes+')';
			document.getElementById ("divImg").innerHTML = p[1];
			document.getElementById ("divOfform").innerHTML = '<a href="/shoppingcard.php">Оформить заказ &raquo;</a>';
			
			$(document).ready(function() {
				$("#img_kor a").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'zoomSpeedChange': 300, 'overlayShow': false});
			});

		}
		else {
			alert (xmlHttp.statusText);
		}
	}
}

function process_card (id, action) {   

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		document.getElementById("ajax-indicator").style.display="inline";
		xmlHttp.open ("GET","../../myax_card.php?qw="+Math.random()+"&action="+action+"&id="+id, true);
		xmlHttp.onreadystatechange = handleServerResponse_card;
 		xmlHttp.send (null);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}

function handleServerResponse_card ()
{
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			
			var p = xmlHttp.responseText.split("|");
			
			document.getElementById('ajax-indicator').style.display='none';
			
			if (p[0]==1 || p[0]==21) tovar_mes='товар';
			else {
				if (p[0]==2 || p[0]==3 || p[0]==4) tovar_mes='товара'; 
				else tovar_mes='товаров'; 
			}
			if (p[0]!=0) {
				document.getElementById ("divSum").innerHTML = '('+p[0]+' '+tovar_mes+')';
				document.getElementById ("divOfform").innerHTML = '<a href="">Оформить заказ &raquo;</a>';
			}
			else {
				document.getElementById ("divSum").innerHTML = '(пусто)';
				document.getElementById ("divOfform").innerHTML = '';
				if (p[1]=='') document.getElementById ("form_id").innerHTML = 'Ваша корзина пуста!';
			}
			
			document.getElementById ("divImg").innerHTML = p[1];
			
			$(document).ready(function() {
				$("#img_kor a").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'zoomSpeedChange': 300, 'overlayShow': false});
			});
			
		}
		else
		{
			alert (xmlHttp.statusText);
		}
	}
}

function process_del () {   

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		document.getElementById("ajax-indicator").style.display="inline";
		xmlHttp.open ("GET","../../myax_del.php?qw="+Math.random()+"&action=del_all", true);
		xmlHttp.onreadystatechange = handleServerResponse_del;
 		xmlHttp.send (null);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}

function handleServerResponse_del ()
{
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			
			var p = xmlHttp.responseText.split("|");
			
			document.getElementById('ajax-indicator').style.display='none';
			
			document.getElementById ("divSum").innerHTML = '(пусто)';
			document.getElementById ("divOfform").innerHTML = '';
			document.getElementById ("divImg").innerHTML = '';
			document.getElementById ("form_id").innerHTML = 'Ваша корзина пуста!';
			
		}
		else
		{
			alert (xmlHttp.statusText);
		}
	}
}

function process_lay (id) {   

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		document.getElementById("ajax-indicator").style.display="inline";
		xmlHttp.open ("GET","../../myax_lay.php?qw="+Math.random()+"&id="+id, true);
		xmlHttp.onreadystatechange = handleServerResponse_lay;
 		xmlHttp.send (null);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}

function handleServerResponse_lay ()
{
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			
			var p = xmlHttp.responseText.split("|");
			
			document.getElementById('ajax-indicator').style.display='none';
			document.getElementById('lay').innerHTML='<a title="Отложенное" href="/lay.php">Отложенное</a>';
			
		}
		else
		{
			alert (xmlHttp.statusText);
		}
	}
}

function process_lay_del (id) {   

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		document.getElementById("ajax-indicator").style.display="inline";
		xmlHttp.open ("GET","../../myax_del_lay.php?qw="+Math.random()+"&id="+id, true);
		xmlHttp.onreadystatechange = handleServerResponse_lay_del;
 		xmlHttp.send (null);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}

function handleServerResponse_lay_del ()
{
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			
			var p = xmlHttp.responseText.split("|");
			
			document.getElementById('ajax-indicator').style.display='none';

			if (p[0]=='clear') {
				document.getElementById('form_lay').innerHTML='Отложенного нет!';
				document.getElementById('lay').innerHTML='';
			}
		}
		else
		{
			alert (xmlHttp.statusText);
		}
	}
}

function process_stat_view (id) {   

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		xmlHttp.open ("GET","../../myax_stat_view.php?qw="+Math.random()+"&id="+id, true);
		xmlHttp.onreadystatechange = handleServerResponse_stat_view;
 		xmlHttp.send (null);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}


function handleServerResponse_stat_view ()
{
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200) {}
		else {
			alert (xmlHttp.statusText);
		}
	}
}


function process_card_check (elem) {   
	
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)	{	
		//document.getElementById("ajax-indicator").style.display="inline";
		
		params=$("#reg_form").serialize();
		
		xmlHttp.open ("POST", "../../myax_card_check.php?qw="+Math.random()+"&elem="+elem, true);
		
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = handleServerResponse_card_check;
 		xmlHttp.send (params);
	}
	else {
		//	setTimeout ('process ()', 1000);
	}
}


function handleServerResponse_card_check ()
{	
	if(xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200) {
			
			var p = xmlHttp.responseText.split("|");
			
			//document.getElementById('ajax-indicator').style.display='none';
			
			//alert(p);
			
			if (p.length>1) {
				if (document.getElementById ("err1_2_3")) document.getElementById ("err1_2_3").innerHTML='';
				if (document.getElementById ("err4_6")) document.getElementById ("err4_6").innerHTML='';
				if (document.getElementById ("err5_6")) document.getElementById ("err5_6").innerHTML='';
				document.getElementById ("err7").innerHTML='';
				document.getElementById ("err8").innerHTML='';
				document.getElementById ("err9").innerHTML='';
				document.getElementById ("err10").innerHTML='';
				document.getElementById ("err11").innerHTML='';
				document.getElementById ("err12").innerHTML='';
				document.getElementById ("err13").innerHTML='';
				if (document.getElementById ("err14_15")) document.getElementById ("err14_15").innerHTML='';
				if (document.getElementById ("err16")) document.getElementById ("err16").innerHTML='';
				if (document.getElementById ("err17")) document.getElementById ("err17").innerHTML='';
				
				for (i=0; i<p.length; i++) {
					if (p[i]==1 && document.getElementById ("err1_2_3")) document.getElementById ("err1_2_3").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==2 && document.getElementById ("err1_2_3")) document.getElementById ("err1_2_3").innerHTML='<b>Введен не корректный email</b>';
					if (p[i]==3 && document.getElementById ("err1_2_3")) document.getElementById ("err1_2_3").innerHTML='<b>Данный e-mail уже используется</b>';
					if (p[i]==4 && document.getElementById ("err4_6")) document.getElementById ("err4_6").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==5 && document.getElementById ("err5_6")) document.getElementById ("err5_6").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==6 && document.getElementById ("err4_6") && document.getElementById ("err5_6")) {
						document.getElementById ("err4_6").innerHTML='<b>Пароли должны совпадать</b>';
						document.getElementById ("err5_6").innerHTML='<b>Пароли должны совпадать</b>';
					}
					if (p[i]==7) document.getElementById ("err7").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==8) document.getElementById ("err8").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==9) document.getElementById ("err9").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==10) document.getElementById ("err10").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==11) document.getElementById ("err11").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==12) document.getElementById ("err12").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==13) document.getElementById ("err13").innerHTML='<b>Это поле необходимо заполнить</b>';
					if (p[i]==14 && document.getElementById ("err14_15")) document.getElementById ("err14_15").innerHTML='<b>Дата не может быть в прошлом</b>';
					if (p[i]==15 && document.getElementById ("err14_15")) document.getElementById ("err14_15").innerHTML='<b>Дата доставки не корректна</b>';
					if (p[i]==16 && document.getElementById ("err16")) document.getElementById ("err16").innerHTML='<b>Время доставки не корректно</b>';
					if (p[i]==17 && document.getElementById ("err17")) document.getElementById ("err17").innerHTML='<b>Пожалуйста, выберите метро</b>';
					
				}
				
				
			}
			else {
				//alert('sdsd');
				document.getElementById('reg_form').submit();
			}

		}
		else {
			alert (xmlHttp.statusText);
		}
	}
}