var as = null
var atype=null
var asid=null
var s2hid = true
var Descs = new Array();
var cprice = 0
var tprice=0
var totprice=0
var reduce=0;

function formatDouble(d, dz)
{
 	dd=Math.pow(10,dz);
 return Math.round(d*dd)/dd;
}

function doStage1()
{
	document.getElementById("Complect").disabled = 1;
	document.getElementById("Thermostat").disabled = 1;
	document.getElementById("Addon").disabled = 1;
	document.getElementById("Square").disabled = 1;

	//stage = 1
	var s = document.getElementById('s').value;
	var type = document.getElementById('type1').checked?1:0;
	if (!s || s <0.6 || s > 20 ) { alert ("Для расчета S<0.6 или S>20 кв.м., пожалуйста свяжитесь с менеджером!"); return; }
	jQuery("#spinner").show();
	mydiv=jQuery("#calc_stage1");
	mydiv.fadeOut("slow",function() { showStage2(s,type)});
	return;

}

function showStage2(s,type)
{
			jQuery.post ("/px.php",
				 {'stage':"1", 's':s, "type":type},
				 function(data)
				 {
				 	var row = null
				 	var tdd= null
				 	if ( !data  ){  mydiv.fadeOut("slow");return;}
			 		jQuery("#ntable").empty();
			 		jQuery("<table></table>").attr("id","tid").attr("class","table wrap").attr("cellspacing","0").attr("cellpadding","0").appendTo("#ntable");
					jQuery("<caption></caption>").html("Выбор комплекта").appendTo("#tid");
			 		row=jQuery("<tr></tr>").attr("class","head").appendTo("#tid");
			 		jQuery("<th></th>").appendTo(row);
					jQuery("<th></th>").html("Комплект без терморегулятора:").appendTo(row);
					jQuery("<th></th>").html("Марка").appendTo(row);
					jQuery("<th></th>").html("Розничная цена, руб:").appendTo(row);

					var t=null,c=null,p=0;
				 	for ( i=0; i< data.length; i++)
				 	{
				 		p=0;
				 		m="";
				 		if ( !data[i].ok )
				 		{
							row = jQuery("<tr></tr>").attr("id","row-" + data[i].square_id).attr("class","srow").attr("title","Кликните, чтбы выбрать").appendTo("#tid");
							tdd= jQuery("<td></td>").appendTo(row);
							jQuery("<img>").attr("src","http://www.sst.ru/images/JpgImg.php?name=typePicture"+data[i].type_id+".gif&width=50&height=50&rand=0").attr("alt",data[i].type_name).attr("title",data[i].type_name).appendTo(tdd);
							t = jQuery("<td></td>").attr("class","ctype").html(data[i].type_name).appendTo(row);
							c = jQuery("<td></td>").attr("class","cmark").html(unescape(data[i].mark)).appendTo(row);

							data[i].ok = 1
							p+=formatDouble(data[i].price,2)
							m = data[i].mark
					 		//alert (data[i].id);
							for ( z=0; z< data.length; z++)
							{
								if ( !data[z].ok && z!=i && data[z].square_id == data[i].square_id )
								{
									data[z].ok = 1
									p+=formatDouble(data[z].price,2)
									jQuery("<span></span>").attr("class","adb").html(unescape(data[i].mark)).appendTo(c);
									//jQuery("<span></span>").html(data[i].price).appendTo(p);
									m+= ", " + data[z].mark;

								}
							}
							jQuery("<td></td>").attr("class","cprice").html(formatDouble(p,2)).appendTo(row);
							row.bind("click",new Array(data[i].square_id,data[i].id,formatDouble(p,2),m),doStage3 )

				 		}
				 	}
				 	as = s
					jQuery("#spinner").hide();
					mydiv=jQuery("#calc_stage2");
					jQuery("#calc_stage2").fadeIn("slow");


				 }
				 , "json"
	);

}

function s2back ()
{
	cprice=0
	tprice=0
	document.getElementById("Complect").value = "";
	document.getElementById("Thermostat").value = "";
	document.getElementById("Square").value = "";
	jQuery("#calc_stage4").fadeOut("fast");
	jQuery("#calc_stage3").fadeOut("fast");
	jQuery("#calc_stage2").fadeOut("fast");
	jQuery("#calc_stage1").fadeOut("slow",function() { showStage1();});
	if ( !s2hid ) jQuery("#calc_stage3").fadeOut("fast");
	jQuery("#final").hide();
}

function showStage1()
{
	jQuery("#spinner").hide();
	jQuery("#calc_stage1").fadeIn("slow");

}

function doStage3 (event)
{
	var id    =event.data[0]
	var rid   =event.data[1]
	var price =event.data[2]
	var name  =event.data[3]

	document.getElementById("Square").value = as;
	cprice=price
	rectotal();
	jQuery("table#tid tr").attr("class","srow")
	jQuery("#row-"+id).attr("class","srow-active")
    document.getElementById("Complect").value=name + " за " + price + " руб. ( с учетом скидки "+Reduce +"% - "+formatDouble(price-(price/100*Reduce),2) + " руб. )";
	if ( !s2hid ) jQuery("#calc_stage3").fadeOut("fast",function () { doS3Q(id); });
	else
		doS3Q(id);

}

function doS3Q (id)
{
	jQuery("#spinner2").show();

			jQuery.post ("/px.php",
				 {'stage':"2", 's':as, "type":atype,"square":id},
				 function(data)
				 {
				 	var row = null
				 	var tdd= null
				 	if ( !data  ){ return;}

			 		jQuery("#ntable2").empty();
			 		jQuery("<table></table>").attr("id","tid2").attr("class","table wrap").attr("cellspacing","0").attr("cellpadding","0").appendTo("#ntable2");
					jQuery("<caption></caption>").html("Выбор терморегулятора").appendTo("#tid2");

					row = jQuery("<tr></tr>").attr("class","zrow").appendTo("#tid2");

				 	for (var i=0; i< data.length; i++)
				 	{
						tdd= jQuery("<td></td>").attr("class","ther").attr("id","zz-"+data[i].id).bind("mouseover",data[i].id,Dshowhide).bind("mouseout",function(){jQuery("#about").html()}).bind("click",new Array(data[i].id,data[i].price,data[i].name),doStage4).appendTo(row);
						jQuery("<span></span>").html(data[i].name).appendTo(tdd);
						jQuery("<img>").attr("src","http://www.sst.ru/images/JpgImg.php?name=thermostatPicture"+data[i].id+".gif&width=80&height=80&rand=0").attr("alt",data[i].name).attr("title",data[i].name).appendTo(tdd);
						jQuery("<span></span>").html("<b>"+data[i].price+" руб.</b>").appendTo(tdd);
						Descs[data[i].id] = data[i].description;

				 	}
					jQuery("#spinner2").hide();
					mydiv=jQuery("#calc_stage3");
					jQuery("#calc_stage3").fadeIn("slow");
					jQuery("#final").hide();
					s2hid=false;
					asid=id;

				 }
				 , "json"
	);
}

function Dshowhide(event)
{
	var id = event.data
	var sh = true
	if (sh)
		jQuery("#about").html(Descs[id]);
	else
		jQuery("#about").html();
}

function doStage4(event)
{
	var id 	  = event.data[0];
	var price = event.data[1];
	var name  = event.data[2];
	tprice= price
    document.getElementById("Thermostat").value=name + " за " + tprice + " руб. (с учетом скидки "+Reduce +"% - "+formatDouble(tprice-(tprice/100*Reduce),2) + " руб.)";
	rectotal()
	jQuery("table#tid2 td").attr("class","ther")
	jQuery("#zz-"+id).attr("class","ther-active")
	var sum = formatDouble((formatDouble(cprice,2) + formatDouble(tprice,2)),2)
	jQuery("#final").html("<h2>Цена комплекта с термостатом:"+ sum+" руб. (с учетом скидки "+Reduce +"% - "+formatDouble(sum-(sum/100*Reduce),2) + " руб.)</h2>");
	jQuery("#final").show();

	jQuery("#spinner3").show();
			jQuery.post ("/px.php",
				 {'stage':"3", 's':as, "type":atype,"square":asid, "thermostat":id},
				 function(data)
				 {
				 	if ( !data  ){ return;}
				 		var row = null
					    jQuery("#spinner3").hide();
			 			jQuery("#ntable3").empty();

			 			jQuery("<table></table>").attr("id","tid3").attr("class","table wrap").attr("cellspacing","0").attr("cellpadding","0").attr("title","Кликните, чтбы выбрать").appendTo("#ntable3");
						jQuery("<caption></caption>").html("Доп. оборудование").appendTo("#tid3");
			 			row=jQuery("<tr></tr>").appendTo("#tid3");
			 			//jQuery("<th></th>").appendTo(row);
						jQuery("<th></th>").attr("colspan","2").html("Наименование:").appendTo(row);
						jQuery("<th></th>").html("Описание").appendTo(row);
						jQuery("<th></th>").html("Розничная цена, руб:").appendTo(row);

						for (var i=0; i< data.length; i++)
					 	{

							var row = jQuery("<tr></tr>").attr("id","xrow-" + data[i].id).attr("class","srow").bind("click","xrow-" + data[i].id,markAdd).attr("title","Кликните, чтобы добавить в заказ/убрать из заказа").appendTo("#tid3");
						    var tdd= jQuery("<td></td>").appendTo(row);
							jQuery("<img>").attr("src","http://www.sst.ru/images/JpgImg.php?name=additional"+data[i].id+".gif&width=100&height=100&rand=0").attr("alt",data[i].name).attr("title",data[i].name).appendTo(tdd);

							jQuery("<td></td>").attr("class","ctype").html(data[i].name).appendTo(row);
							jQuery("<td></td>").attr("class","cmark").html(unescape(data[i].text)).appendTo(row);
							jQuery("<td></td>").attr("class","cprice").html(data[i].price).appendTo(row);

					 	}

						jQuery("#calc_stage4").fadeIn("slow");

				 },"json");
}

function markAdd ( event )
{
	var obj =  jQuery("#"+event.data);
	obj.attr("class",(obj.attr("class")=="srow-active")?"srow":"srow-active");
	document.getElementById("Addon").value="";
	jQuery("table#tid3 tr.srow-active").each (
	 function(i)  { 	document.getElementById("Addon").value= document.getElementById("Addon").value +  jQuery(this).find("td.ctype").text() + " за " + jQuery(this).find("td.cprice").text() + " руб.; "   } );
	 rectotal()
	 if (document.getElementById("Addon").value)
	 	document.getElementById("Addon").value+=" | Всего дополнительного оборудования на сумму " + totprice + " руб. ( с учетом скидки "+Reduce +"% - "+formatDouble(totprice-(totprice/100*Reduce),2) + " руб. )";
}

function rectotal()
{
	totprice = 0;
	jQuery("table#tid3 tr.srow-active").each (
	 function(i)  { 	totprice+=  formatDouble(jQuery(this).find("td.cprice").text(),2) } );
	var tpr = formatDouble(formatDouble(tprice,2)+formatDouble(cprice,2)+formatDouble(totprice,2),2)
	 jQuery("#total").html("Стоимость доп. оборудования: "+totprice+" (с учетом скидки "+Reduce +"% - "+formatDouble(totprice-(totprice/100*Reduce),2) + " руб. )<br/><h1>Сумма с учетом доп. оборудования: " + tpr + " руб.<br />( с учетом скидки "+Reduce +"% - "+formatDouble(tpr-(tpr/100*Reduce),2) + " руб.)</h1>").show();

}

function sodo()
{
	document.getElementById("Complect").disabled = 0;
	document.getElementById("Thermostat").disabled = 0;
	document.getElementById("Addon").disabled = 0;
	document.getElementById("Square").disabled = 0;

}
