
/*************************************************************************************
*
*		Filename:		headerCode.js
*		Author:			Diarmid Sloan 
*		Date:			March 2001
*		Version:		0.1
*		Description:	JavaScript header code for the Home Heating Oil site
*
**************************************************************************************
*
*		CHANGE LOG
*		Date:			Details:
*
*************************************************************************************/

//////////////////////////////////////////////////////////////////////////////////////
//
//		Function:		hilite
//		Parameters:		ImgButton
//		Returns:	
//		Description:	Called by onMouseOver event on graphic in HTML. Changes the
//						graphic to a highlighted version
//		
//////////////////////////////////////////////////////////////////////////////////////

function hilite(ImgButton)
{
	if (document.images)
	{
		ImgButtonOn=eval(ImgButton + "on.src");
		document [ImgButton].src = ImgButtonOn;
	}
} 


//////////////////////////////////////////////////////////////////////////////////////
//
//		Function:		lolite
//		Parameters:		ImgButton
//		Returns:	
//		Description:	Called by onMouseOut event on graphic in HTML. Changes the
//						graphic back to an unselected version
//		
//////////////////////////////////////////////////////////////////////////////////////

function lolite(ImgButton)
{
	if (document.images)
	{
		ImgButtonOff=eval(ImgButton + "off.src");
		document [ImgButton].src = ImgButtonOff;
	}
} 


//////////////////////////////////////////////////////////////////////////////////////
//
//		Function:		showProducts
//		Parameters:		item
//		Returns:	
//		Description:	Called when a fuel type is selected in the order form.
//						Changes the product types to match the current fuel type.
//						Also changes the format of the quantity field.
//		
//////////////////////////////////////////////////////////////////////////////////////

function showProducts(item)
{
	var this_fuel = "document.order_form.fuel"+item;
	var this_product = "document.order_form.product"+item;
	var this_qty = "document.order_form.qty"+item;
	
	var fuel_option = eval(this_fuel).selectedIndex;
	var fuel_selection = eval(this_fuel).options[fuel_option].value;
	
	var oil_text = new Array("Kerosene - 28sec  ", "Gasoil - 35sec  ", "Derv  ");
	var oil_values = new Array("kerosene", "gasoil", "derv");
	//var oil_qty = "Litres or value";
	var coal_text = new Array("Five Star Coal  ", "Doubles  ", "Slack  ", "Beans  ", "Phurnacite  ", "Glovoids  ", "Coalite  ", "Redflame  ", "Emberglo  ", "Wonderco  ", "Petcoke  ", "Economy Coal  ", "Economy Doubles  ", "Peat Briquettes  ");
	var coal_values = new Array("5star", "doubles", "slack", "beans", "phurnacite", "glovoids", "coalite", "redflame", "emberglo", "wonderco", "petcoke", "economycoal", "economydoubles", "briquettes");
	//var coal_qty = "No. of 50kg bags";
	var gas_text = new Array("Propane - 100lb  ", "Propane - 75lb  ", "Propane - 40lb  ", "Propane - 24lb  ", "Propane - 9.9lb  ", "Butane - 25lb  ", "Butane - 11lb  ");
	var gas_values = new Array("propane100", "propane75", "propane40", "propane24", "propane9", "butane25", "butane11");		
	//var gas_qty = "No. of bottles";

	switch (fuel_selection)
	{							
		case 'coal':
			var coal_count = coal_text.length;
			eval(this_product).options.length = coal_count;
			
			for (var i=0; i<coal_count; i++)
			{
				eval(this_product).options[i].value = coal_values[i];
				eval(this_product).options[i].text = coal_text[i];
			}
			
			//Pre-fill the quantity field in appropriate format
			//eval(this_qty).value = coal_qty;

			break;
		
		case 'gas':
			var gas_count = gas_text.length;
			eval(this_product).options.length = gas_count;
			
			for (var i=0; i<gas_count; i++)
			{
				eval(this_product).options[i].value = gas_values[i];
				eval(this_product).options[i].text = gas_text[i];
			}
			
			//Pre-fill the quantity field in appropriate format
			//eval(this_qty).value = gas_qty;
			
			break;
		
		case 'oil':
			var oil_count = oil_text.length;			
			eval(this_product).options.length = oil_count;
			
			for (var i=0; i<oil_count; i++)
			{
				eval(this_product).options[i].value = oil_values[i];
				eval(this_product).options[i].text = oil_text[i];
			}
			
			//Pre-fill the quantity field in appropriate format
			//eval(this_qty).value = oil_qty;

			break;
			
		case 'none':
		default:
			eval(this_product).options.length = 1;
			eval(this_product).options[0].value = "none";
			eval(this_product).options[0].text = "Select ->  ";
			
			//Clear the quantity field
			//eval(this_qty).value = "";
	}
}


//////////////////////////////////////////////////////////////////////////////////////
//
//		Function:		validateForm()
//		Parameters:		
//		Returns:		boolean
//		Description:	Checks that all mandatory fields are completed and that at 
//						least one item has been ordered to prevent void emails.
//		
//////////////////////////////////////////////////////////////////////////////////////

function clearOrder()
{	
	var item_count = 5;
	
	for (var i=1; i<item_count+1; i++)
	{
		var this_fuel = "document.order_form.fuel"+i;
		var this_product = "document.order_form.product"+i;
		var this_qty = "document.order_form.qty"+i;
		
		eval(this_fuel).options[0].selected = true;
		eval(this_product).options.length = 1;
		eval(this_product).options[0].value = "none";
		eval(this_product).options[0].text = "Select ->  ";
		eval(this_product).options[0].selected = true;
		eval(this_qty).value = "";
	}
}


//////////////////////////////////////////////////////////////////////////////////////
//
//		Function:		validateForm()
//		Parameters:		
//		Returns:		boolean
//		Description:	Checks that all mandatory fields are completed and that at 
//						least one item has been ordered to prevent void emails.
//		
//////////////////////////////////////////////////////////////////////////////////////

function validateForm()
{
	var name = document.order_form.fullname.value;
	var address = document.order_form.address.value;
	var town = document.order_form.town.value;
	var county = document.order_form.county.value;
	var postcode = document.order_form.postcode.value;	
	var phone = document.order_form.phone.value;	
	var email = document.order_form.email.value;	
	var item_count = 5;
	
	if (name == "") {
		alert ("Please fill in your name.");
		document.order_form.fullname.focus();
		return false;
	}
	
	if (address == "") {
		alert ("Please fill in your address.");
		document.order_form.address.focus();
		return false;
	}
	
	if (town == "") {
		alert ("Please fill in your town.");
		document.order_form.town.focus();
		return false;
	}
	
	if (county == "") {
		alert ("Please fill in your county.");
		document.order_form.county.focus();
		return false;
	}
	
	if (postcode == "") {
		alert ("Please fill in your postcode.");
		document.order_form.postcode.focus();
		return false;
	}
	
	if (phone == "") {
		alert ("Please fill in your phone number.");
		document.order_form.phone.focus();
		return false;
	}
	
	if (phone != "" && !isNumeric(phone)) {
		alert ("The phone number must only contain numeric characters.");
		document.order_form.phone.focus();
		return false;
	}
	
	// E-mail Validation by Henrik Petersen / NetKontoret
	// Explained at www.echoecho.com/jsforms.htm
	// Please do not remove this line and the two lines above.
	if (email != "") {
		apos=email.indexOf("@"); 
		dotpos=email.lastIndexOf(".");
		lastpos=email.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			alert("You must enter a valid email address.");
			document.order_form.email.focus();
			return false;
		}
	}
	
	if (document.order_form.fuel1.selectedIndex == 0) {
		alert ("Please choose at least one order item, starting with Item 1.");
		document.order_form.fuel1.focus();
		return false;
	}	
	
	return true;
} 


//////////////////////////////////////////////////////////////////////////////////////
//
//		Function:		checkPrice
//		Parameters:		
//		Returns:	
//		Description:	Checks the price of the order.
//						Currently only deals with coal prices.
//		
//////////////////////////////////////////////////////////////////////////////////////

function checkPrice()
{
	var items_count = 5;
	var total_cost = 0;
	var this_cost;
	var this_product;
	var this_qty;
	
	var coal_products = new Array("5star", "doubles", "slack", "beans", "phurnacite", "glovoids", "coalite", "redflame", "emberglo", "wonderco", "petcoke", "economycoal", "economydoubles", "briquettes");	
	var coal_prices = new Array(8.30, 8.00, 7.20, 10.50, 11.80, 10.00, 10.90, 8.90, 8.40, 8.80, 8.50, 7.60, 7.40, 2.00);
	var coal_length = coal_products.length;
	
	for (i=1; i<items_count+1; i++)
	{
		var this_fuel = "document.order_form.fuel"+i;
		var fuel_option = eval(this_fuel).selectedIndex;
		var fuel_selection = eval(this_fuel).options[fuel_option].value;
	
		if (fuel_selection == "coal")
		{
			this_product = "document.order_form.product"+i;
			product_option = eval(this_product).selectedIndex;
			product_selection = eval(this_product).options[product_option].value;
			this_qty = "document.order_form.qty"+i;
				
			for (j=0; j<coal_length; j++)
			{
				if (coal_length[j] == product_selection)
				this_cost = coal_prices[j];
			}
			
			total_cost += this_cost*this_qty;
		}
	}

	alert ("The cost of this order, excluding oil and gas products, is &pound;" + total_cost);
}

function isNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
