window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	
		var aReceipt = document.getElementById('shoppingcart');
		var aShowReceiptBtn = document.getElementById('showcart');
		var aHideReceiptBtn = document.getElementById('closecart');
		var aCloseReceiptImg = document.getElementById('closereceipt');
		if ((aReceipt != null) && (aShowReceiptBtn != null) && (aHideReceiptBtn != null) && (aCloseReceiptImg != null))
		{
			aShowReceiptBtn.onclick = jsFnShowReceipt;
			aHideReceiptBtn.onclick = jsFnHideReceipt;
			aCloseReceiptImg.onclick = jsFnHideReceipt;
			aCloseReceiptImg.style.cursor = 'pointer';
		}
		
		if ((typeof aShowCart != "undefined") && (aShowCart == true)) aReceipt.style.display = 'block';

		var aSampleReceipt = document.getElementById('sampleshoppingcart');
		var aSampleShowReceiptBtn = document.getElementById('sampleshowcart');
		var aSampleHideReceiptBtn = document.getElementById('sampleclosecart');
		var aSampleCloseReceiptImg = document.getElementById('sampleclosereceipt');
		if ((aSampleReceipt != null) && (aSampleShowReceiptBtn != null) && (aSampleHideReceiptBtn != null) && (aSampleCloseReceiptImg != null))
		{
			aSampleShowReceiptBtn.onclick = jsFnSampleShowReceipt;
			aSampleHideReceiptBtn.onclick = jsFnSampleHideReceipt;
			aSampleCloseReceiptImg.onclick = jsFnSampleHideReceipt;
			aSampleCloseReceiptImg.style.cursor = 'pointer';
		}
		
		if ((typeof aSampleShowCart != "undefined") && (aSampleShowCart == true)) aSampleReceipt.style.display = 'block';

		var paypalcards = document.getElementById("paypalcards");
		if (paypalcards != null)
		{
				paypalcards.onclick = JSFnPaypalNewWindow;
		}

		var productimage = document.getElementById("productimage");
		if (productimage != null)
		{
				productimage.onclick = JSFnImageNewWindow;
		}

		var impconversion = document.getElementById("impconversion");
		if (impconversion != null)
		{
				impconversion.onclick = JSFnImperialConversionTool;
		}

		// Put Ken White in where applicable
		var aKWSpans = document.getElementsByTagName("span");
		for (aKWIndex = 0; aKWIndex < aKWSpans.length; aKWIndex++)
		{
			if (aKWSpans[aKWIndex].className == 'kw') aKWSpans[aKWIndex].innerHTML = 'Natural Blinds ';
		}

		var width = document.getElementById("ict_width");
		var drop = document.getElementById("ict_drop");
		var UseValues = document.getElementById("UseValues");
		
		if ((width != null) && (drop != null) && (UseValues != null))
		{
			width.onchange = JSFnImperialConvert;
			drop.onchange = JSFnImperialConvert;
			UseValues.onclick = JSFnImperialConvertUseValues;
		}

		var aCheckoutForm = document.getElementById('viewcheckout');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;

		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aParasolForm = document.getElementById('ParasolForm');
		if (aParasolForm != null) aParasolForm.onsubmit = JSFnValidateParasolForm;

		var aSamplesForm = document.getElementById('viewsamplescheckout');
		if (aSamplesForm != null) aSamplesForm.onsubmit = JSFnValidateSampleForm;
	}
}

function jsFnShowReceipt()
{
	var aReceipt = document.getElementById('shoppingcart');
	aReceipt.style.display = 'block';
	return false;
}

function jsFnHideReceipt()
{
	var aReceipt = document.getElementById('shoppingcart');
	aReceipt.style.display = 'none';
	return false;
}

function jsFnSampleShowReceipt()
{
	var aSampleReceipt = document.getElementById('sampleshoppingcart');
	aSampleReceipt.style.display = 'block';
	return false;
}

function jsFnSampleHideReceipt()
{
	var aSampleReceipt = document.getElementById('sampleshoppingcart');
	aSampleReceipt.style.display = 'none';
	return false;
}


function JSFnImperialConversionTool()
{
	window.open('imperialconversion.php', 'ImperialConversion','width=400px,height=320px,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	return false;
}

function JSFnPaypalNewWindow()
{
	window.open(this.href, 'paypal','width=400px,height=450px,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	return false;
}

function JSFnImageNewWindow()
{
	window.open(this.href, 'paypal','width=620px,height=450px,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no');
	return false;
}

function JSFnImperialConvert()
{
	var width = document.getElementById("ict_width");
	var drop = document.getElementById("ict_drop");
	if ((width != null) && (drop != null))
	{
		var widthcms = document.getElementById("widthcms");
		var dropcms = document.getElementById("dropcms");
		widthcms.innerHTML = Math.round(width.value*2.54*Math.pow(10,2))/Math.pow(10,2);
		dropcms.innerHTML = Math.round(drop.value*2.54*Math.pow(10,2))/Math.pow(10,2);
	}
}

function JSFnImperialConvertUseValues()
{
	window.opener.document.getElementById("MeasuredWidth").value = document.getElementById("widthcms").innerHTML;
	window.opener.document.getElementById("MeasuredDrop").value = document.getElementById("dropcms").innerHTML;
	window.close();
}

function JSFnIndexProductLink()
{
	var aProductLinks = this.getElementsByTagName("a");
	for (aProductIndex = 0; aProductIndex < aProductLinks.length; aProductIndex++)
	{
		window.location = aProductLinks[aProductIndex].href; 
	}
	return false;
}



var aContactRequiredFields = new Array ("name","Please enter your name to continue");
function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('email');
	var aAddress = document.getElementById('address');
	var aTelephone = document.getElementById('telephone');
	var aFax = document.getElementById('fax');
	if ((aEmail != null) && (aAddress != null) && (aTelephone != null) && (aFax != null))
	{
		if ((aEmail.value == '') && (aAddress.value == '') && (aTelephone.value == '') && (aFax.value == ''))
		{
			alert('You must provide either your address, telephone/fax number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactRequiredFields);
}

var aSamplesRequiredFields = new Array ("name","Please enter your name to continue");
function JSFnValidateSampleForm()
{
	var aEmail = document.getElementById('email');
	var aAddress = document.getElementById('address');
	var aTelephone = document.getElementById('telephone');
	if ((aEmail != null) && (aAddress != null) && (aTelephone != null))
	{
		if ((aEmail.value == '') && (aAddress.value == '') && (aTelephone.value == ''))
		{
			alert('You must provide either your address, telephone number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aSamplesRequiredFields);
}

/* Validate the Parasol Form: Fields */
var aParasolRequiredFields = new Array ("FullName","Please enter your name to continue",
										"TelephoneNo","Please enter a telephone number to continue",
										"ColourChoice","Please choose a colour to continue");
/* Validate the Parasol Form: Function */
function JSFnValidateParasolForm()
{
	return JSFnValidateForm(aParasolRequiredFields);
}


CheckoutRequiredFields = new Array ("terms","You must tick to say that you agree to Natural Wood Blinds' terms and conditions in order to checkout.");
function JSFnValidateCheckout()
{
	return JSFnValidateForm(CheckoutRequiredFields);
}

function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}
