var globalLetter = 'A';
img = new Image();
img.src="/img/basket_mini_roll.gif";

function changeLetter (letter)
{
	document.getElementById ("letter").innerText = letter;
	document.getElementById ('letter' + globalLetter).style.display = 'none';
	document.getElementById ('letter' + letter).style.display = 'block';
	globalLetter = letter;
	return false;
}

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=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0" + 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 + "\" />\n");
	newWindow.document.write ("</body>\n");
	newWindow.document.write ("</html>");
}

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";
	}
}

var xmlHttp = createXmlHttpRequestObject (); 
setInterval ('showCart()', 10000);
function showCart ()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/cart.php", true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.send (null);
	}
}

function addUnit (unit_id)
{
	rollCart (unit_id, "/img/basket_mini_roll.gif");
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/cart.php?unit_id=" + unit_id + "&unit_num=1", true);  
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.send (null);
		setTimeout ('rollCart("' + unit_id + '", "/img/basket_mini.gif")', 1000);
	}
	else
		setTimeout ('addUnit("' + unit_id + '")', 500);
}

function rollCart (unit_id, unit_src, roll)
{
	document.getElementById ('img_' + unit_id).src = unit_src;
}

function handleServerResponse () 
{
	if (xmlHttp.readyState == 4) 
	{
		if (xmlHttp.status == 200) 
		{
			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<p>в пакете пусто</p>\n";
				if (discount.substr (0, discount.indexOf (",")) > 0)
					newHTML += "<p>скидка: <b>" + discount + " %</b></p>";
			}
			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<p>" + num + " " + textgoods + "</p>\n" +
							"\t\t\t<p>на сумму " + total + "</p>\n";
				if (discount.substr (0, discount.indexOf (",")) > 0)
					newHTML += "<p>скидка: <b>" + discount + " %</b></p>";
			}
			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;
}

