var xmlHttp = createXmlHttpRequestObject (); 
setInterval ('showCart()', 10000);
var img = new Image();
img.src="/img/icon_basket_roll.gif";

function changeInput (item, string)
{
	if (item.value == "")
	{
		item.value = string;
		item.style.color = "#999999";
	}
	else if (item.value == string)
	{
		item.value = "";
		item.style.color = "#000000";
	}
}

function rollCart (type_img, unit_id, unit_src, roll)
{
	document.getElementById (type_img + 'img_' + unit_id).src = unit_src;
}

function showCart ()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/inc/cart.php", true);
		xmlHttp.onreadystatechange = updateCart;
		xmlHttp.send (null);
	}
}

function addUnit (unit_id)
{
	rollCart ("unit", unit_id, "/img/icon_basket_roll.gif");
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/inc/cart.php?unit_type=unit&unit_id=" + unit_id + "&unit_num=1", true);  
		xmlHttp.onreadystatechange = updateCart;
		xmlHttp.send (null);
		setTimeout ('rollCart("unit", "' + unit_id + '", "/img/icon_basket.gif")', 1000);
	}
	else
		setTimeout ('addUnit("' + unit_id + '")', 500);
}

function addSet (unit_id)
{
	rollCart ("set", unit_id, "/img/icon_basket_roll.gif");
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/inc/cart.php?unit_type=set&unit_id=" + unit_id + "&unit_num=1", true);  
		xmlHttp.onreadystatechange = updateCart;
		xmlHttp.send (null);
		setTimeout ('rollCart("set", "' + unit_id + '", "/img/icon_basket.gif")', 1000);
	}
	else
		setTimeout ('addSet("' + unit_id + '")', 500);
}

function updateCart () 
{
	if (xmlHttp.readyState == 4) 
	{
		if (xmlHttp.status == 200) 
		{
			var xmlResponse, num, summ, discount, total, newHTML, textgoods;
			xmlResponse = xmlHttp.responseXML.documentElement;
			num = xmlResponse.getElementsByTagName ("num").item(0).firstChild.data;
			summ = xmlResponse.getElementsByTagName ("summ").item(0).firstChild.data;
			discount = xmlResponse.getElementsByTagName ("discount").item(0).firstChild.data;
			total = xmlResponse.getElementsByTagName ("total").item(0).firstChild.data;
			if (num == 0)
				newHTML =	"\t\t\t\tКорзина пуста<br />\n" +
							"\t\t\t\t<a href=\"/ru/catalogue/\">ЗАКАЖИ МЕБЕЛЬ ПРЯМО СЕЙЧАС</a>\n";
			else
			{
				if (num % 10 == 1 && num % 100 != 11)
					textgoods = "товар";
				else if (num % 10 > 1 && num % 10 < 5 && (num < 11 || num > 21))
					textgoods = "товара";
				else
					textgoods = "товаров";

				newHTML = "\t\t\t\t" + num + " <span>" + textgoods + "</span> (<b>" + total + " грн.</b>)<br />\n";
				if (typeof (discount) != "undefined" && discount > 0)
					newHTML += "\t\t\t\tскидка: <b>" + discount / 100 + " %</b><br />\n";
				newHTML += "\t\t\t\t<a href=\"/ru/buy/\">оформить заказ</a><br />\n";
			}
			if (document.getElementById ("cart"))
				document.getElementById ("cart").innerHTML = newHTML;
		} 
	}
}

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 ("Ваш браузер не поддерживает AJAX. Установите Internet Explorer, Opera или другой совместимый браузер.");
	else 
		return xmlHttp;
}

function openWindow (openURL, openWidth, openHeight)
{
	add = ", left=" + Math.max ((screen.width - openWidth) / 2, 0) + ", top=" + Math.max ((screen.height - 64 - openHeight) / 2, 0);
	newWindow = window.open ("", "window1", "width=" + openWidth + ", height=" + openHeight + ", toolbar=no, location=no, directories=no, status=0, menubar=no, scrollbars=no, resizable=no" + add);
	newWindow.document.write ("<html>\n");
	newWindow.document.write ("<head>\n");
	newWindow.document.write ("\t<link rel=\"stylesheet\" href=\"/list.css\" type=\"text/css\" />\n");
	newWindow.document.write ("</head>\n");
	newWindow.document.write ("<body>\n");
	newWindow.document.write ("\t<img src=\"" + openURL + "\" alt=\"\" />\n");
	newWindow.document.write ("</body>\n");
	newWindow.document.write ("</html>");
}

function addAnswer (inputObject)
{
	if (inputObject.value == "+")
	{
		ua = navigator.userAgent.toLowerCase ();
		inputObject.value = "-";

		newInput1Text = document.createElement ("input");
		newInput1Text.type = "text";
		newInput1Text.className = "input_center";
		newInput1Text.name = "number[]";
		newInput1Text.value = parseInt (document.getElementsByName ('number[]')[document.getElementsByName ('number[]').length - 1].value) + 1;
		newInput1Text.size = "5";
		newInput1Text.readOnly = true;

		newInput2Text = document.createElement ("input");
		newInput2Text.type = "text";
		newInput2Text.name = "name[]";
		newInput2Text.size = "49";

		newInput3Text = document.createElement ("input");
		newInput3Text.type = "text";
		newInput3Text.name = "color[]";
		newInput3Text.size = "39";

		newInput4Text = document.createElement ("input");
		newInput4Text.type = "text";
		newInput4Text.name = "quantity[]";
		newInput4Text.size = "9";

		newInput5Text = document.createElement ("input");
		newInput5Text.type = "text";
		newInput5Text.name = "size[]";
		newInput5Text.size = "10";

		newInputPlus = document.createElement ("input");
		newInputPlus.type = "button";
		newInputPlus.value = "+";
		if (ua.indexOf ("msie") != -1 && ua.indexOf ("opera") == -1 && ua.indexOf ("webtv") == -1)
			newInputPlus.setAttribute ("onclick", function() {addAnswer (this)});
		else
			newInputPlus.setAttribute ("onclick", "Javascript: addAnswer (this)");

		document.getElementById ('vote_answers').appendChild (newInput1Text);
		document.getElementById ('vote_answers').appendChild (document.createTextNode (" "));
		document.getElementById ('vote_answers').appendChild (newInput2Text);
		document.getElementById ('vote_answers').appendChild (document.createTextNode (" "));
		document.getElementById ('vote_answers').appendChild (newInput3Text);
		document.getElementById ('vote_answers').appendChild (document.createTextNode (" "));
		document.getElementById ('vote_answers').appendChild (newInput4Text);
		document.getElementById ('vote_answers').appendChild (document.createTextNode (" "));
		document.getElementById ('vote_answers').appendChild (newInput5Text);
		document.getElementById ('vote_answers').appendChild (document.createTextNode (" "));
		document.getElementById ('vote_answers').appendChild (newInputPlus);
		document.getElementById ('vote_answers').appendChild (document.createElement ("br"));
	}
	else
	{
		delInput1 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling;
		delSpace1 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling;
		delInput2 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling;
		delSpace2 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling;
		delInput3 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling;
		delSpace3 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling;
		delInput4 = inputObject.previousSibling.previousSibling.previousSibling.previousSibling;
		delSpace4 = inputObject.previousSibling.previousSibling.previousSibling;
		delInput5 = inputObject.previousSibling.previousSibling;
		delSpace5 = inputObject.previousSibling;
		delBR = inputObject.nextSibling;

		delInput1.parentNode.removeChild (delInput1);
		delSpace1.parentNode.removeChild (delSpace1);
		delInput2.parentNode.removeChild (delInput2);
		delSpace2.parentNode.removeChild (delSpace2);
		delInput3.parentNode.removeChild (delInput3);
		delSpace3.parentNode.removeChild (delSpace3);
		delInput4.parentNode.removeChild (delInput4);
		delSpace4.parentNode.removeChild (delSpace4);
		delInput5.parentNode.removeChild (delInput5);
		delSpace5.parentNode.removeChild (delSpace5);
		inputObject.parentNode.removeChild (inputObject);
		delBR.parentNode.removeChild (delBR);

		for (i = 1; i < document.getElementsByName ('number[]').length; i ++)
		{
			document.getElementsByName ('number[]')[i].value = i;
		}
	}
}

