var popupTimeout;
var popupItemId;

function addToCart(id) {

	try{window.clearInterval(popupTimeout);}
	catch(e) {};

	popupItemId = id;

	var t = document.getElementById("item"+id).getElementsByTagName("td");
	var price =  t[1].innerHTML.replace(",",".");
	price = isNaN(parseFloat(price)) ? 0 : parseFloat(price);
	var mess = "<b>"+t[0].innerHTML+"</b><br />Цена за "+t[2].innerHTML+".: <b>"+price+" руб.</b>";
	mess += "<br /><br />Количество: <input type='text' value='1' id='cartValue' maxlength='4' size='4' onkeyup='calcTotal("+price+")'/>";
	mess += "<br /><br />Общая стоимость: <span style='font-weight:bold' id='cartTotal'>"+price+"</span>&nbsp;руб.<span id='cartTotalFake'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
	mess += "<div align='right' style='margin-top:20px;width:100%'><button id='popupWin__but_ok' style='width:80px'>В корзину</button>&nbsp;<button id='popupWin__but_cancel' style='width:60px'>Отмена</button></div>";
	popupWin.create({header:"Добавление в корзину", content:mess});

	popupWin.whichDog.className = "popupWin__confirm";
	document.getElementById("popupWin__header").className = "popupWin__header_confirm";
	document.getElementById("popupWin__header_content").className = "popupWin__header_content_confirm";
	document.getElementById("popupWin__but_ok").onclick = ajaxAddToCart;
	document.getElementById("popupWin__but_cancel").onclick = typeof cancel == "function" ? cancel : popupWin.close;
		
	if(typeof cancel == "function") {
		document.getElementById("popupWin__header_close").onCustomClose = onClose;
	}


	try {document.getElementById("cartValue").focus();}
	catch(e) {}

	return false;
}

function calcTotal(price) {
	var v = document.getElementById("cartValue");
	var value = isNaN(parseInt(v.value)) ? 0 : parseInt(v.value);
	if(value <= 0) value = 1;
	if(v.value != "" && v.value != "" + value) v.value = value;
	
	document.getElementById("popupWin__but_ok").disabled = v.value == "";
	document.getElementById("cartTotalFake").style.display = "none";
	document.getElementById("cartTotal").innerHTML = v.value == "" ? "" : utils.round(price * value);
}


function ajaxAddToCart() {
	popupWin.close();

	ajaxRequest.processRequest("systemlogin", "tryLogin", {id:popupItemId,qty:document.getElementById("cartValue").value},
		function(r) {

			popupWin.create({header:"Сообщение", content:"Позиция добавлена в корзину"});
			popupTimeout = setTimeout("hidePopup()", 3000);

		});
}

function hidePopup() {
	window.clearInterval(popupTimeout);
	try {popupWin.close();}
	catch(e) {}
}


function verOrder(f) {

	if(f.reqty.value == 1) return true;

	if(f.name.value == "") {
		alert("Укажите Ф.И.О.");
		f.name.focus();
		return false;
	}
	else if(f.firm.value == "") {
		alert("Укажите Организацию");
		f.firm.focus();
		return false;
	}
	else if(f.phone.value == "") {
		alert("Укажите Телефон");
		f.phone.focus();
		return false;
	}
	else if(f.email.value == "") {
		alert("Укажите E-Mail");
		f.email.focus();
		return false;
	}

	return true;
}