xmlHttp = new createXMLHttpRequest();
host="http://"+document.domain+"/";
function createXMLHttpRequest()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			var XmlHttpVersions  = new Array ('MSXML2.XMLHTTP.6.0',
											'MSXML2.XMLHTTP.5.0',
											'MSXML2.XMLHTTP.4.0',
											'MSXML2.XMLHTTP.3.0',
											'MSXML2.XMLHTTP'
											,'Microsoft.XMLHTTP');
			for (var i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
			{
				try 
				{
					xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
				}
				catch(e) 
				{}
			
			}
			//xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			//xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch (e)
		{
		}
		
	}
	if (!xmlHttp)
	{
		alert ("ошибка создания объекта XMLHttpRequest.");
		return false;
	}
	else return xmlHttp;
}

function buyed()
{
	if (xmlHttp.readyState ==4)
	{
		if (xmlHttp.status==200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			num = xmlDocumentElement.getElementsByTagName('num')[0].firstChild.data;
			cos = xmlDocumentElement.getElementsByTagName('cost')[0].firstChild.data;
			if (num!=11 && num%10==1) 
				str=' товар';
			else
				if ((num!=12 && num%10==2) ||(num!=13 &&  num%10==3) ||(num!=14 &&  num%10==4)) 
					str =' товара';
				else 
					str= ' товаров';
			if (num>0) 
				document.getElementById("num1").style.color='#009933';
			else
				document.getElementById("num1").style.color='#2786a6';
			document.getElementById("num1").innerHTML='у вас в корзине <br />'+num+str;
			if (cos>0)
			{
				st='Цена в зависимости от суммы заказа   (общая сумма заказа:  <font color="red">'+cos+'</font>)';
			}	
			else
				st='Цена в зависимости от суммы заказа  ';
			document.getElementById("cos0").innerHTML=st;
			document.getElementById("cos1").innerHTML=st;
			document.getElementById("cos2").innerHTML=st;
			document.getElementById("cos3").innerHTML=st;
			document.getElementById("cos4").innerHTML=st;
			document.getElementById("cos5").innerHTML=st;
			document.getElementById("cos6").innerHTML=st;
			document.getElementById("cos7").innerHTML=st;
			if(rastr = xmlDocumentElement.firstChild.nextSibling.nextSibling )
			{		
				id = rastr.getElementsByTagName('id')[0].firstChild.data;
				cost_prod = rastr.getElementsByTagName('cost')[0].firstChild.data;
				if (cost_prod!=0)
					document.getElementById("cost"+id).innerHTML=cost_prod;
				else
					document.getElementById("cost"+id).innerHTML=' &nbsp;';
			}
			while(rastr = rastr.nextSibling )
			{		
				id = rastr.getElementsByTagName('id')[0].firstChild.data;
				cost_prod = rastr.getElementsByTagName('cost')[0].firstChild.data;
				if (cost_prod!=0)
					document.getElementById("cost"+id).innerHTML=cost_prod;
				else
					document.getElementById("cost"+id).innerHTML=' &nbsp;';
			}

		}
		else
		{
			alert ("При обращении к серверу возникли проблемы.");
		}
	}
}


function send_get(script,str,func_response)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState ==0)
	{
		xmlHttp.open('GET',script+'?'+str,true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=windows-1251');
		xmlHttp.onreadystatechange = func_response;
		xmlHttp.send(null);
	}
	else
		setTimeout('send_get(script,str,func_response)',500);
}

function send_post(script,str,func_response)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState ==0)
	{
		xmlHttp.open('POST',script,true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=windows-1251');
		xmlHttp.onreadystatechange = func_response;
		xmlHttp.send(str);
	}
	else
		setTimeout('send_post(script,str,func_response)',500);
}


function buy(id,prod)
{ 
	n=document.getElementById("num_rastr_"+id).value;
	if ((!n.is_NaN) && (n>=0))
	{
		str='prod='+prod+'&id='+id+'&n='+n;
		send_get(host+'buy.php',str,buyed);
	}
}

function buy1(id,prod){	buy(id,prod); }
function buy2(){}

function buy_probe(id,prod)
{ 
	n=document.getElementById("num_rastr_probe_"+id).value;
	if ((!n.is_NaN) && (n>=0))
	{
		str='prod='+prod+'&id='+id+'&n='+n;
		send_get(host+'buy_probe.php',str,buyed);
	}
}

function buy1_probe(id,prod){	buy_probe(id,prod); }
function buy2_probe(){}

function keyPress(id,prod) 
{ 
	if(document.all) //it's IE 
	var whichKey = window.event.keyCode; 
	else if(document.layers) //it's netscape 
	var whichKey = evt.which 
	else 
	var whichKey = evt.keyCode 
	if (whichKey==13)
		buy(id,prod);
}
