// JavaScript Document

var xmlhttp;
var url = "calc_ajax.asp";
var kvadratura;
var vrata;
var prozori;

function fillDropDown(catID) {
	
	xmlhttp = GetXmlHttpObject();
	
	if (xmlhttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}

	req_url = url + "?cmd=dd&catID=" + catID;
	req_url = req_url + "&sid=" + Math.random();

	xmlhttp.onreadystatechange = stateChanged;
	xmlhttp.open("GET",req_url,true);
	xmlhttp.send(null);
}

function stateChanged() {
	if (xmlhttp.readyState==4) {
		document.getElementById("loadDD").innerHTML = xmlhttp.responseText;
	}
}




function calcPaint(prodID) {
	
	xmlhttp = GetXmlHttpObject();
	
	if (xmlhttp==null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	req_url = ""
	req_url = url + "?cmd=calc&pID=" + prodID + "&kvadratura=" + kvadratura;
	req_url = req_url + "&sid=" + Math.random();
	
	xmlhttp.onreadystatechange = stateChangedCalc;
	xmlhttp.open("GET",req_url,true);
	xmlhttp.send(null);
}

function stateChangedCalc() {
	if (xmlhttp.readyState==4) {
		document.getElementById("calc_results").innerHTML = xmlhttp.responseText;
	}
}



function calcArea() {
	
	kvadratura = 0;
	vrata = 0;
	prozori = 0;
	
	w1_w = document.getElementById("w1_w").value; w1_w = w1_w.replace(/,/, ".");
	w1_h = document.getElementById("w1_h").value; w1_h = w1_h.replace(/,/, ".");
	
	w2_w = document.getElementById("w2_w").value; w2_w = w2_w.replace(/,/, ".");
	w2_h = document.getElementById("w2_h").value; w2_h = w2_h.replace(/,/, ".");
	
	w3_w = document.getElementById("w3_w").value; w3_w = w3_w.replace(/,/, ".");
	w3_h = document.getElementById("w3_h").value; w3_h = w3_h.replace(/,/, ".");
	
	w4_w = document.getElementById("w4_w").value; w4_w = w4_w.replace(/,/, ".");
	w4_h = document.getElementById("w4_h").value; w4_h = w4_h.replace(/,/, ".");
	
	d1_w = document.getElementById("d1_w").value; d1_w = d1_w.replace(/,/, ".");
	d1_h = document.getElementById("d1_h").value; d1_h = d1_h.replace(/,/, ".");
	
	d2_w = document.getElementById("d2_w").value; d2_w = d2_w.replace(/,/, ".");
	d2_h = document.getElementById("d2_h").value; d2_h = d2_h.replace(/,/, ".");
	
	win1_w = document.getElementById("win1_w").value; win1_w = win1_w.replace(/,/, ".");
	win1_h = document.getElementById("win1_h").value; win1_h = win1_h.replace(/,/, ".");
	
	win2_w = document.getElementById("win2_w").value; win2_w = win2_w.replace(/,/, ".");
	win2_h = document.getElementById("win2_h").value; win2_h = win2_h.replace(/,/, ".");
	
	win3_w = document.getElementById("win3_w").value; win3_w = win3_w.replace(/,/, ".");
	win3_h = document.getElementById("win3_h").value; win3_h = win3_h.replace(/,/, ".");
	
	win4_w = document.getElementById("win4_w").value; win4_w = win4_w.replace(/,/, ".");
	win4_h = document.getElementById("win4_h").value; win4_h = win4_h.replace(/,/, ".");
	
	for (i=1;i<=4;i++) {
		if (eval("w"+i+"_w") > "" && eval("w"+i+"_h") > "") {
			kvadratura = kvadratura + (eval("w"+i+"_w") * eval("w"+i+"_h"));
		}
		
		if (eval("win"+i+"_w") > "" && eval("win"+i+"_h") > "") {
			prozori = prozori + (eval("win"+i+"_w") * eval("win"+i+"_h"));
		}
	}
	
	for (i=1;i<=2;i++) {
		if (eval("d"+i+"_w") > "" && eval("d"+i+"_h") > "") {
			vrata = vrata + (eval("d"+i+"_w") * eval("d"+i+"_h"));
		}
	}
	
	kvadratura = kvadratura - (prozori + vrata);
	
	var formatKvadratura = new Number(kvadratura);
	kvadratura = formatKvadratura.toFixed(2);
	
	document.getElementById("prikaziKvadraturu").innerHTML = "<strong>" + kvadratura + " m2</strong>";
}



function GetXmlHttpObject() {
	
	if (window.XMLHttpRequest) {
	// code for IE7+, Firefox, Chrome, Opera, Safari
	return new XMLHttpRequest();
	}
	
	if (window.ActiveXObject) {
	// code for IE6, IE5
	return new ActiveXObject("Microsoft.XMLHTTP");
  	}

	return null;
}