// Greenshop Solar domestic hot water systems calculations
var PlanoSize = 1;
var TuboSize = 2;

function calcQuote() {
var people = document.getElementById('NumPeople').value;
var over = 0, TmaxSize, CollectorSize, Cylinder, Vented, ExistingCylinder = 0, ThermalStore = 0;
var orientation = document.getElementById('Orientation').value;
var rooftype = document.getElementById('RoofType').value;
var collector = document.getElementById('CollectorType').value;
var mainspressure = document.getElementById('MainsPressure').value;
var collectorPrice, installationPrice, totalPrice; 
	 
	if (people == "over 6") {
		people = 6;
		over = 1;
		document.getElementById('lblOverSix').style.visibility = 'visible';		
	} else {
		document.getElementById('lblOverSix').style.visibility = 'hidden';
	}
	
	// Calc number/size of collectors
	if (people != "select..." && orientation != "select...") {
		if (orientation == "South West" || orientation == "South East") orientation = "South";
		if (orientation == "East") orientation = "West";
		TmaxSize = Tmax[orientation + people];
		TuboSize = Tubo[orientation + people];
		PlanoSize = Plano[orientation + people];
		setRoofType();
		//document.getElementById('Tmax').innerHTML = TmaxSize + " tube collector";
		document.getElementById('Tubo').innerHTML = TuboSize + " collectors";
		document.getElementById('Plano').innerHTML = PlanoSize + ((PlanoSize == "1") ? " collector" : " collectors");
	} else { 
		//document.getElementById('Tmax').innerHTML = "";
		document.getElementById('Tubo').innerHTML = "";
		document.getElementById('Plano').innerHTML = "";
	}
	
	// Calc cost of collectors
	if (people != "select..." && orientation != "select..." && rooftype != "select...") {
		//document.getElementById('Tmax').innerHTML = document.getElementById('Tmax').innerHTML + " &pound;" + CMPrice("Tmax", TmaxSize, rooftype);
		document.getElementById('Tubo').innerHTML = document.getElementById('Tubo').innerHTML + " &pound;" + CMPrice("Tubo", TuboSize, rooftype);
		document.getElementById('Plano').innerHTML = document.getElementById('Plano').innerHTML + " &pound;" + CMPrice("Plano", PlanoSize, rooftype);
	}
	
	// check for existing dual coil Cylinder
	if (document.getElementById('DualCoil').value == "Yes") {
			ExistingCylinder = 1;
			mainspressure = "No";	// ??? - is this the right thing to do?
	}
	
	// check for existing dual coil Cylinder
	if (document.getElementById('ThermalStore').value == "Yes") {
			ThermalStore = 1;
			mainspressure = "Yes";	// ??? - is this the right thing to do?
	}
	
	// set up the cylinder type
	if (people != "select..." && orientation != "select..." && rooftype != "select..." && collector != "select..." && mainspressure != "select...") {
		if (collector == "Tmax") {
			CollectorSize = TmaxSize;
			document.getElementById('CollectorInfo').value = "Thermomax: " + TmaxSize + " tube collector";
		}
		if (collector == "Tubo") {
			CollectorSize = TuboSize;
			document.getElementById('CollectorInfo').value = "Consolar Tubo: " + TuboSize + " collectors";
		}
		if (collector == "Plano") {
			CollectorSize = PlanoSize;
			document.getElementById('CollectorInfo').value = "Consolar Plano: " + PlanoSize + " collectors";
		}
		if (mainspressure == "Yes") {
			Vented = "U";
		} else {
			Vented = "V";
		}
		if (ExistingCylinder != 0) {
			Cylinder = "EXISTING";
		} else if (ThermalStore != 0) {
			Cylinder = "CONUS";			
		} else {
			Cylinder = CylinderType[collector + CollectorSize + Vented];
		}
		document.getElementById('CylinderSizeStr').innerHTML = CylinderSize[Cylinder];
		document.getElementById('CylinderSize').value = CylinderSize[Cylinder];
	}
	
	// calc total price
	if (people != "select..." && orientation != "select..." && rooftype != "select..." && collector != "select..." && mainspressure != "select...") {

		// Get price for different elements
		collectorPrice = CMPrice(collector, CollectorSize, rooftype);
		installationPrice = INSTALLPrice(document.getElementById('Installed').value, document.getElementById('MainsPressure').value, collector, document.getElementById('InRoof').value);
		// only turn on debug for testing
		// document.getElementById('debugmsg').innerHTML = "Debug: " + "INST " + installationPrice + " CYL " + Cylinder + " " + CylinderPrice[Cylinder] + " BITS " + (parseFloat(PumpPrice) + parseFloat(ControllerPrice) + parseFloat(PipeworkPrice));

		// Display the total price
		//totalPrice = parseFloat(collectorPrice) + parseFloat(installationPrice);
		totalPrice = parseFloat(collectorPrice) + parseFloat(installationPrice) + parseFloat(CylinderPrice[Cylinder]) + parseFloat(PumpPrice) + parseFloat(ControllerPrice) + parseFloat(PipeworkPrice);
		displayPrice(totalPrice);
		document.getElementById('EstimateDone').value = "YES";
	} else {
		displayPrice("NA");
		document.getElementById('EstimateDone').value = "NO";
	}
	setSpecial();
}

function CMPrice(collectorType, collectorSize, roofType) {
var extraInfo = "";
	if ((document.getElementById('InRoof').value == "Yes") && (collectorType == "Plano") && (roofType == "Pitched")) extraInfo = "In";
	
	// document.getElementById('debugmsg').innerHTML = "CMP " + collectorType + collectorSize + roofType + extraInfo;
	
	//return  parseFloat(CollectorPrice[collectorType + collectorSize]) + parseFloat(MountingPrice[collectorType + collectorSize + roofType + extraInfo]);
	return  parseFloat(CollectorPrice[collectorType + collectorSize]) + parseFloat(MountingPrice[collectorType + collectorSize + roofType + extraInfo]);
}

function INSTALLPrice(installed, unvented, collector, inroof) {
var priceInst = 0;
	if (installed == "Yes") {
		priceInst = InstallationPrice;
		if (unvented == "Yes") priceInst = parseFloat(priceInst) + parseFloat(UnVentedExtra);
		if (collector == "Plano" && inroof == "Yes") priceInst = parseFloat(priceInst) + parseFloat(InRoofExtra);
	} else {
		priceInst = parseFloat(ShippingCharge) + parseFloat(DIYKit) - parseFloat(PipeworkPrice);
	}
	return (priceInst);
}

function displayPrice(price) {
var vatAmount, vatRate, priceStr, priceTotal;

	if (price != "NA") {
		price = parseFloat(price * 1.25);
		priceStr = "&pound;" + price;
		if (document.getElementById('Installed').value == "Yes") {
			vatAmount = parseInt (parseFloat(price) * parseFloat(InstalledVAT)) / 100;
			vatRate = InstalledVAT;
		} else {
			vatAmount =  parseInt (parseFloat(price) * parseFloat(SelfInstallVAT)) / 100;
			vatRate = SelfInstallVAT;
		}
		priceTotal = parseFloat(price) + parseFloat(vatAmount);
		priceStr = priceStr + " plus &pound;" + vatAmount + " VAT at " + vatRate + "% = &pound;" + priceTotal;
		document.getElementById('priceStr').innerHTML = priceStr;
		document.getElementById('Price').value = priceStr;
	} else {
		document.getElementById('priceStr').innerHTML = "Not enough information";
		document.getElementById('Price').value = "Not enough information";
	}
}

function setSpecial() {
var special = 0, specialMsg;
	
	if (document.getElementById('NumPeople').value == "over 6") special = 1;
	if (document.getElementById('CombiBoiler').value == "Yes") special = 1;	
	if (document.getElementById('RoofShaded').value == "Yes") special = 1;		
	if (document.getElementById('RoofAreaRestricted').value == "Yes") special = 1;	
	if (document.getElementById('RoofAccess').value == "No") special = 1;	
	if (document.getElementById('ThermalStore').value == "Yes") special = 1;
	if (document.getElementById('FitCylinder').value == "No") special = 1;	
	if (document.getElementById('OneHour').value == "No") special = 1;
	if (document.getElementById('CollectorType').value == "Tubo" && TuboSize > 4) special = 1;	// Larger tubo or plano systems are special cylinder selection may be a problem
	if (document.getElementById('CollectorType').value == "Plano" && PlanoSize > 2) special = 1;	// Larger tubo or plano systems are special cylinder selection may be a problem
	
	if (special != 0) {
		specialMsg = SpecialQuoteMessage;
	} else {
		specialMsg = StandardQuoteMessage;
	}
	// document.getElementById('qtype2').innerHTML = specialMsg;
	document.getElementById('qtypeStr').innerHTML = specialMsg;	
	document.getElementById('QuoteType').value = specialMsg;	
}

function setRoofType() {
var pitch = document.getElementById('RoofType').value;
var ctype = document.getElementById('CollectorType').value;

	if (pitch == "Flat") {
		document.getElementById('Orientation').selectedIndex = 1;
	}
	if (PlanoSize != 1 && ctype == "Plano") {
		setVisible('Pitched', 'RoofType', 'lblInRoof');
	} else {
		document.getElementById('lblInRoof').style.visibility = 'hidden';
	}
}

function setVisible(testVal, selectVal, spanId) {
	if (document.getElementById(selectVal).value == testVal) {
		document.getElementById(spanId).style.visibility = 'visible';		
	} else {
		document.getElementById(spanId).style.visibility = 'hidden';
	}
	setSpecial();
}

function setPeople() {
	if (document.getElementById('NumPeople').value == "over 6") {
		alert ("System sizing will be done for 6 people - further discussion will be needed to complete the quotation");
	}
	setSpecial();
}

