var app;

var cssStyle="";


CostCalculator = function (field1, field2, price, field1Name, field2Name, selectedProduct) {

	if(!field1 || !field2 || !price || !field1Name || !field2Name || !selectedProduct) return;
	
	this.field1 = field1;
	this.field2 = field2;	
	this.price = price;
	this.field1Name = field1Name;
	this.field2Name = field2Name;
	this.selectedProduct = selectedProduct;
	
	this.field1.onchange = this.scope(this.getPrice);
	this.field2.onchange = this.scope(this.getPrice);
	
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	this.getPrice();
}

CostCalculator.prototype.readstateChange = function () {
	if (this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) {
		this.fillPrice(this.xmlhttp.responseXML);
	}
	
}

CostCalculator.prototype.init = function () {
	var cc1 = new CostCalculator(document.getElementById("command.houseType"),document.getElementById("command.persons"),
		document.getElementById("price1"),"houseType", "persons", document.getElementById("selectedProduct"));

	var cc2 = new CostCalculator(document.getElementById("electricityConsumption"),document.getElementById("gasConsumption"),
		document.getElementById("price2"),"electricityConsumption", "gasConsumption", document.getElementById("selectedProduct"));
}

CostCalculator.prototype.getPrice = function () {

	/* The type for field1 and field2 is int, so passing empty strings will
	 * not work, therefore we convert these values to default integers if
	 * necessary
	 */
	var val1 = (this.field1.value=="") ? 0 : parseInt(this.field1.value);
	var val2 = (this.field2.value=="") ? 0 : parseInt(this.field2.value);
	
	/* This is a bit dirty, but during testing it turned out that 
	 * you can request a price for values over 100.000 KWh and 
	 * so to make sure we cross the limit we double the value if
	 * it is to big.
	 */
	var year=Boolean("true"); 
	var max_electricity_usage=parseInt("100000");
	if(!year)
	  max_electricity_usage=max_electricity_usage/12;
	var max_gas_usage=parseInt("170000"); 
	if(!year)
	  max_gas_usage=max_gas_usage/12;
	   
	if(val1 > max_electricity_usage) { val1 *= 2; }
	if(val2	> max_gas_usage) { val2 *= 2; }
	
	
	try {
		this.xmlhttp.open("GET", window.location.protocol + "//" + window.location.host + "/zakelijk/system/productresponder.jsp?" + this.field1Name + "=" + val1 + "&" + this.field2Name + "=" + val2 + "&selectedProducts=" + this.selectedProduct.value,  true);
		this.resetPrice();
		this.xmlhttp.onreadystatechange = this.scope(this.readstateChange);
		this.xmlhttp.send(null);
	} catch (e) {
		alert(e);
	}
}

CostCalculator.prototype.fillPrice = function (xml) {
	if (xml != null) {
		var element1 = xml.getElementsByTagName("label");
		var element2 = xml.getElementsByTagName("price");
		if ((element1 != null) && (element2 != null) && (element1.length > 0) && (element2.length > 0)) {
			var node1 = element1[0];
			var node2 = element2[0];
			if ((node1 != null) && (node2 != null)) {
				this.setPrice(this.getValue(node1), this.getValue(node2)); 
			} else {
				this.setMessage("Uw maandlasten kunnen niet berekend worden.");
			}
		} else {
			this.setMessage("Uw maandlasten kunnen niet berekend worden.");
		}
	} else {
		this.setMessage("Algemene foutmelding: geen geldige XML gevonden. Geen response");
	}
}

CostCalculator.prototype.resetPrice = function () {
	this.price.innerHTML = "<p class=\"price\">Uw maandlasten worden bepaald.</p>";
}

CostCalculator.prototype.setMessage = function (message) {
	this.price.innerHTML = "<p class=\"price\"><span class=\"errormessage\" style=\"color: red;\">" + message + "</span></p>";
}

CostCalculator.prototype.setPrice = function (label, price) {
	this.price.innerHTML = "<p class=\"price\">" + label + "&nbsp;<strong>" + price + "</strong></p>";
}



CostCalculator.prototype.getValue = function (element) {//some DOM's add a text Node, some don't
	var s = (element.firstChild) ? element.firstChild.nodeValue : element.nodeValue;
	s = s.replace( /^\s+/g, "" );
	s = s.replace( /\s+$/g, "" );
	return s;
}

CostCalculator.prototype.scope = function(method) {
	var scope = this;
	return function() {
		return method.apply(scope, arguments);
	}
}