
arrSQServicesValue = new Array();
arrSQServices = new Array();

/*Squash court services...*/

arrSQServices.push("--  Please Select --");
arrSQServicesValue.push("--  Please Select --");

arrSQServices.push("Lighting");
arrSQServicesValue.push("LIGHTING");

arrSQServices.push("Court Lines");
arrSQServicesValue.push("COURT-LINES");

arrSQServices.push("The playing surface - Painting");
arrSQServicesValue.push("THE-PLAYING-SURFACE-PAINTING");

arrSQServices.push("Out of court redecoration (including ceiling)");
arrSQServicesValue.push("OUT-OF-COURT-REDECORATION-INC-CEILING");

arrSQServices.push("Out of court redecoration (not including ceiling)");
arrSQServicesValue.push("OUT-OF-COURT-REDECORATION-INC-CEILING");

arrSQServices.push("Major Plastering - Front Impact Wall");
arrSQServicesValue.push("MAJOR-PLASTERING-FRONT-PLAYWALL");

arrSQServices.push("Minor Plastering");
arrSQServicesValue.push("MINOR-PLASTERING");

arrSQServices.push("Plaster Patch");
arrSQServicesValue.push("PLASTER-PATCH");

arrSQServices.push("Court furniture");
arrSQServicesValue.push("COURT-FURNITURE");

arrSQServices.push("Glass back overhaul");
arrSQServicesValue.push("GLASS-BACK-OVERHAUL");

arrSQServices.push("Sanding");
arrSQServicesValue.push("SANDING");

arrSQServices.push("Lining");
arrSQServicesValue.push("LINING");

arrSQServices.push("Flooring repairs");
arrSQServicesValue.push("FLOORING-REPAIRS");

arrSQServices.push("Floor Releasing");
arrSQServicesValue.push("FLOOR-RELEASING");

arrSQServices.push("New tin, playboard and quadrant");
arrSQServicesValue.push("NEW-TIN-PLAYBOARD-AND-QUADRANT");

arrSQServices.push("Cleaning");
arrSQServicesValue.push("CLEANING");



//Create Javascript implementation of hashmap
//hashmap.put(key, value) - key and value can be of any type 
//hasmap.get(key) will return corresponding value/description
//hasmap.size() returns number of elements in hashmap

function CreateHashtable()
{
	this.hash = new Array();
	this.keys = new Array();
	this.location = 0;
}

CreateHashtable.prototype.hash = null;
CreateHashtable.prototype.keys = null;
CreateHashtable.prototype.location = null;

CreateHashtable.prototype.get = function (key)
{
	return this.hash[key];
}

CreateHashtable.prototype.put = function (key, value)
{
	if (value == null)
	
	return null;
		
	if (this.hash[key] == null)
		
		this.keys[this.keys.length] = key;
		this.hash[key] = value;
}

CreateHashtable.prototype.size = function ()
{
	return this.keys.length;
}


function fGenerateServices(Material){

	arrServices = new Array();
	//ensure it does not contain any previous values
	arrServices.length=0;
	
	arrServicesDescription = new Array();
	//ensure it does not contain any previous values
	arrServicesDescription.length=0;

	//add the default please select which is used in validation
	arrServices.push("--  Please Select  --");
	arrServicesDescription.push("--  Please Select  --");

	switch (Material){
	
	case "SYNTHETIC":
		
		//services for SYNTHETIC material only
		arrServices.push("LINING")
		arrServicesDescription.push("Lining")
		arrServices.push("CLEANING-ABRADING-SEALING")
		arrServicesDescription.push("Cleaning, abrading & sealing")

		
	break;
	
	case "ENGINEERED-TIMBER-BOARD":
	
		//services for ENGINEERED-TIMBER-BOARD material only
		arrServices.push("LINING")
		arrServicesDescription.push("Lining")
		arrServices.push("CLEANING-ABRADING-SEALING")
		arrServicesDescription.push("Cleaning, abrading & sealing")

		
	break;
	
	
	default : 
		//TIMBER or GRANWOOD-BLOCK have all service options
		arrServices.push("SANDING-SEALING-LINING");
		arrServicesDescription.push("Sanding, sealing & lining");
		arrServices.push("SANDING-SEALING");
		arrServicesDescription.push("Sanding & sealing");
		arrServices.push("LINING");
		arrServicesDescription.push("Lining")
		arrServices.push("CLEANING-ABRADING-SEALING");
		arrServicesDescription.push("Cleaning, abrading & sealing")

	}

	//new floor system and repair work available for all materials
	arrServices.push("NEW-FLOOR-SYSTEM");
	arrServicesDescription.push("New floor system")
	arrServices.push("REPAIR-WORK");
	arrServicesDescription.push("Repair work")
	
	fResetListbox("selService");
	
	for(i=0; i<arrServices.length; i++){
		//create options for each element in the array
		fCreateOption("selService",arrServices[i],arrServicesDescription[i]);
	}
}

function fGetSQServiceDescription(sValue){

	sDescription = hMapSQDescription.get(sValue);

}

function fEnableDisableNonSQElements(sEnableDisable){

	document.getElementById("txtMSquared").disabled = sEnableDisable;
	document.getElementById("txtWidth").disabled = sEnableDisable;
	document.getElementById("txtLength").disabled = sEnableDisable; 
	document.getElementById("selFloorMaterial").disabled = sEnableDisable;

}



function fGenerateSQServices(sType){

	if(sType=="SQUASH-COURT-MAINTENANCE"){
	
		fEnableDisableNonSQElements(true);
		
		
		fResetListbox("selService");
		for(i=0; i<arrSQServices.length; i++){
			//create options for each element in the array
			fCreateOption("selService",arrSQServicesValue[i],arrSQServices[i]);
		}
		//change the buttons onclick to run fGetSQServiceDescription
		document.getElementById("selService").onchange = function() {fGetSQServiceDescription(this.value)}
	}
	else{
		fEnableDisableNonSQElements(false);
		document.getElementById("selService").onchange = "";
		fGenerateServices("all");
	}
}


//Creates an option element for the desired listbox taking value and text parameters
function fCreateOption(whichBox,newValue,newText){

		var objSelect=document.getElementById(whichBox)
		var objOption = document.createElement("option");
		objOption.text = newText
		objOption.value = newValue
		
		objSelect.options.add(objOption);
		
		return;		
}



function fResetListbox(whichBox){

	var objSelect=document.getElementById(whichBox);
	
	lboxlen = document.getElementById(whichBox).length
	
	for(i=0; i<lboxlen; i++){
		objSelect.remove(0);
	}

}

function fCalculateSize(width, length){

	width = width.replace(/\s+/g,'');
	length = length.replace(/\s+/g,'');
	document.getElementById("txtMSquared").value = document.getElementById("txtMSquared").value.replace(/\s+/g,'');

	//check for "" as this is interpreted as false by the isNaN
	if((width !="" && length !="") || document.getElementById("txtMSquared").value != ""){
		if(!isNaN(document.getElementById("txtMSquared").value) && document.getElementById("txtMSquared").value != ""){
			document.getElementById("txtLength").value = "";
			document.getElementById("txtWidth").value = "";
			return document.getElementById("txtMSquared").value;
		}
		else if(isNaN(width) || isNaN(length)){
			alert("Please enter an approximate size");
			return -1;
		}
		else if(!isNaN(width) && width>0 && !isNaN(length) && length>0){
			total = width * length;
			document.getElementById("txtMSquared").value = total;
			return total;
		}
		else{
			alert("Please enter an approximate size");
			document.getElementById("txtMSquared").focus();
			return -1;		
		}
	
	}
	else{
			alert("Please enter an approximate size");
			document.getElementById("txtMSquared").focus();
			return -1;
	}

}


function fSetPriceAndDescription(iPrice, sDescription){

	if(!isNaN(iPrice)){
	//if it is numerical
		document.getElementById("divPrice").innerHTML = "<p>Your Price is: £" + iPrice +"</p>";
	}
	else{
	//else set to the description
		document.getElementById("divPrice").innerHTML = "<p>"+ iPrice +"</p>";
	}

	try{
	
		document.getElementById("divServiceDescription").innerHTML = "<p>Service Description: " + sDescription + "</p>";
	
	}
	catch(xxx){
		//no description passed so set it to ""
		document.getElementById("divServiceDescription").innerHTML = "";
	}

}

function fResetPriceAndDescription(iPrice, sDescription){

	//reset any previous quote values
	document.getElementById("divPrice").innerHTML = ""
	document.getElementById("divServiceDescription").innerHTML = "";
}

function fGetQuote(sType){
	
	//reset Price and Description values from previous quotes
	fResetPriceAndDescription();
	
	if(sType=="SQUASH-COURT-MAINTENANCE"){

		//check location
		if(document.getElementById("selLocation").value!="-- Please Select --"){
		
		}
		else{
			alert("Please select a location.");
			document.getElementById("selLocation").focus();
			return;
		}
	
		theString = "SQUASH-COURT-MAINTENANCE" + "_" + document.getElementById("selService").value;
		theString = theString.toString();
		if(!isNaN(hMapPrices.get(theString))){
			iPrice = formatCurrency(hMapPrices.get(theString));
		}
		else{
			//set iPrice to the description which is returned
			iPrice = hMapPrices.get(theString);
		}
		theVal = document.getElementById("selService").value
		desc = hMapSQDescription.get(theVal);
		fSetPriceAndDescription(iPrice, desc)
		return;
	}
	
	//check floor type
	if(document.getElementById("selFloorType").selectedIndex==0){

		alert("Please select a floor type.");
		document.getElementById("selFloorType").focus();
		return;
	}
	
	//check material
	if(document.getElementById("selFloorMaterial").selectedIndex==0){
	
		alert("Please select a material.");
		document.getElementById("selFloorMaterial").focus();
		return;
	}	 

	//check service
	if(document.getElementById("selService").selectedIndex==0){

		alert("Please select a service.");
		document.getElementById("selService").focus();
		return;
	}
	

	//check location
	if(document.getElementById("selLocation").selectedIndex==0){

		alert("Please select a location.");
		document.getElementById("selLocation").focus();
		return;
	}
	
	floorSize = fCalculateSize(document.getElementById("txtWidth").value, document.getElementById("txtLength").value);
	
	if(floorSize!=-1){
		
	
		theString = document.getElementById("selFloorType").value + "_" + document.getElementById("selFloorMaterial").value + "_" + document.getElementById("selService").value;
		
		//discount if over 200 M2
		if(theString=="SPORTS-HALL_TIMBER_SANDING-SEALING-LINING" && floorSize>200){
			theString += "_200";
		}
		else if(theString=="SPORTS-HALL_GRANWOOD-BLOCK_SANDING-SEALING-LINING" && floorSize>200){
			theString += "_200";			
		}
		else if(theString=="SPORTS-HALL_TIMBER-BLOCK_SANDING-SEALING" && floorSize>200){
			theString += "_200";			
		}
		else if(theString=="SPORTS-HALL_GRANWOOD-BLOCK_SANDING-SEALING" && floorSize>200){
			theString += "_200";			
		}
		else if(theString=="GYMNASIUM_TIMBER_SANDING-SEALING-LINING" && floorSize>200){
			theString += "_200";
		}
		else if(theString=="GYMNASIUM_GRANWOOD-BLOCK_SANDING-SEALING-LINING" && floorSize>200){
			theString += "_200";			
		}
		else if(theString=="GYMNASIUM_TIMBER-BLOCK_SANDING-SEALING" && floorSize>200){
			theString += "_200";			
		}
		else if(theString=="GYMNASIUM_GRANWOOD-BLOCK_SANDING-SEALING" && floorSize>200){
			theString += "_200";			
		}
		else if(document.getElementById("selService").value=="CLEANING-ABRADING-SEALING" && floorSize>499){
			theString = "CLEANING-ABRADING-SEALING_500"
		}

		//alert("the string = " + theString)

		pricePerSM = hMapPrices.get(theString);
		if(!isNaN(pricePerSM)){
			total = pricePerSM * floorSize;
			iPrice = formatCurrency(total);
		}
		else{
			//set iPrice to the description which is returned
			iPrice = hMapPrices.get(theString);
		}
		theVal = document.getElementById("selService").value
		desc = hMapDescription.get(theVal);
		
		//if we dont have a price in the hmap - get them to phone the office
		if(isNaN(pricePerSM)){
			iPrice = sToBeAgreed
		}
		
		fSetPriceAndDescription(iPrice, desc)
		return;
	}

}




//function returns formatted currency
function formatCurrency(num){
	
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		pence = num%100;
		num = Math.floor(num/100).toString();

	if(pence<10){
		pence = "0" + pence;
	

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+
		num.substring(num.length-(4*i+3));
	}

	return (((sign)?'':'-') + num + '.' + pence);

}

//create hashmap to hold all prices
var hMapPrices = new CreateHashtable();
var hMapDescription = new CreateHashtable();
var hMapSQDescription = new CreateHashtable();

