var price_initialized=false;

var pricecontrols = {
	onoff:		"<th class=\"price-auto\"><input type=\"checkbox\"></th>",
	multiple:	"<input type=text class=\"pricenum\" onKeyPress=\"return onlyNumbers(event)\" value='0'>",
	summary: 	"<tfoot><tr class=\"price-end\"><th colspan=\"2\"><strong>Итого:</strong></th><td><input type=\"text\" value=\"0\" readonly></td></tr></tfoot>"
}
/* OLD	summary: 	"<tfoot><tr class=\"price-end\"><th colspan=\"2\"><strong>Итого:</strong></th><td><input type=\"text\" value=\"0\" readonly></td></tr><tr class=\"price-add\"><th colspan=\"2\"><input type=\"button\" class=\"price-button logincontrol\" value=\"Заказать в количестве:\" /></th><td class=\"order-number\"><input type=\"text\" onKeyPress=\"return onlyNumbers(event)\" > шт. </td></tfoot>" */

function onlyNumbers(e){
	var keynum;
	var keychar;
	var numcheck;
	if(window.event) keynum = e.keyCode;
	else if(e.which) keynum = e.which;
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return numcheck.test(keychar);
}

function price_init(){
	if(price_initialized) return;
	price_initialized=true;
	$(pricecontrols["onoff"]).prependTo("table.price thead tr");
	$(pricecontrols["onoff"]).prependTo("table.price tbody tr");
	$("tr.price-multiple th input").addClass("invisible");
	$(pricecontrols["summary"]).appendTo("table.price"); 

	$(pricecontrols["multiple"]).prependTo("tr.price-multiple th");

	$(".price th input:checkbox").click(function(){
		$(this.parentNode.parentNode).toggleClass("selected");
		var price=$(this.parentNode.parentNode).children("td.priceval").text();
		price=price.replace("&nbsp;","");
		price=price.replace(" ","");
		price=price.replace(",",".");
		price=parseFloat(price);
		if(isNaN(price)) return false;
		if(!this.checked)price=-price;
		var obj=$(this.parentNode.parentNode.parentNode.parentNode).children("tfoot").children("tr.price-end").children("td").children("input");
		var oldprice=parseFloat(obj.attr("value"));
		obj.attr("value",oldprice+price);
		return true;
	});

	$("tr.price-multiple input:text").change(function(){
		if(isNaN($(this).attr("value")) || !$(this).attr("value")) $(this).attr("value",0);
		if(!this.xvalue) this.xvalue=0;

		if($(this).attr("value") && $(this).attr("value")!="0") $(this.parentNode.parentNode).addClass("selected");
		else $(this.parentNode.parentNode).removeClass("selected");

		var price=$(this.parentNode.parentNode).children("td.priceval").text();
		price=price.replace(" ","");
		price=price.replace("&nbsp;","");
		price=price.replace(",",".");
		price=parseFloat(price);
		if(isNaN(price)) return false;

		var obj=$(this.parentNode.parentNode.parentNode.parentNode).children("tfoot").children("tr.price-end").children("td").children("input");
		var oldprice=parseFloat(obj.attr("value"));
		
		obj.attr("value",oldprice+price*(parseFloat(this.value)-parseFloat(this.xvalue)));
		this.xvalue=this.value;
	});

	$(".price-button").click(function(){
		var number=parseFloat($(this.parentNode.parentNode).children("td.order-number").children("input").attr("value"));

		if(isNaN(number) || !number) {
			jAlert("Выберите количество единиц указанной комплектации!","Предупреждение");
			return false;
		}
		
		var ids=new Array();
		
		$(this.parentNode.parentNode.parentNode.parentNode).children("tbody").children("tr.priceval").each(function(){
			$(this).children("th.price-auto").children("input:checked").each(function(){
				$(this.parentNode.parentNode).children("td:nth-child(2)").each(function(){
					ids[this.className]=number;
				});
			});
		});

		var curnum=0;
		$(this.parentNode.parentNode.parentNode.parentNode).children("tbody").children("tr.price-multiple").each(function(){
			$(this).children("th.price-auto").children("input.pricenum").each(function(){
				curnum = parseFloat($(this).attr("value"));
				if(!isNaN(curnum) && curnum) $(this.parentNode.parentNode).children("td:nth-child(2)").each(function(){
					ids[this.className]=curnum*number;
				});
			});
		});
		
		order(ids);	
	});

	$("a.price_rolldown").click(function(){
		$("#maintext .dynamic").removeClass("closed");
		$("#maintext .dynamic").addClass("opened");
		return false;
	});
	$("a.price_rollup").click(function(){
		$("#maintext .dynamic").removeClass("opened");
		$("#maintext .dynamic").addClass("closed");
		return false;
	});
}

function order(myorder){
	if(document.getElementById("account-enter")){
//		jAlertFile("/private/?actio n=form","","Осуществите, пожалуйста, вход!",popupinit);
		jAlert("Осуществите, пожалуйста, вход!","Добавление товара");
		return false;
	}
	var senddata="";
	for(i in myorder){
		senddata+=i+"="+myorder[i]+"&";
	}
	$.post("/private/?action=add&popup"+"&random="+Math.floor(100000*Math.random()), {"data":senddata, "uri":""+window.location}, function(data){jMessage(data,"Заказ");updateprivatemessages();},"html");

}

