window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	  
		var aCheckoutForm = document.getElementById('checkoutform');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;
		
		var aDeliveryForm = document.getElementById('deliveryinformation');
		if (aDeliveryForm != null) aDeliveryForm.onsubmit = JSFnValidateDelivery;	
		
		var aProductZoom = document.getElementById('productimagezoom');
		if (aProductZoom != null) aProductZoom.onclick = JSFnProductZoom;
		
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aCustomerRegisterForm = document.getElementById('CustomerRegisterForm');
		if (aCustomerRegisterForm != null) aCustomerRegisterForm.onsubmit = JSFnValidateCustomerRegisterForm;

		var aCustomerChgPassForm = document.getElementById('CustomerChgPassForm');
		if (aCustomerChgPassForm != null) aCustomerChgPassForm.onsubmit = JSFnValidateNewPassForm;
	}
}
/******************************************************************************************************************************************/
function JSFnProductZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'menubar=no, resizeable=no, toolbar=no, width=600px, height=600px')
	aWindow.focus();
	return false;
}
/******************************************************************************************************************************************/
var aContactRequiredFields = new Array ("name","Please enter your name to continue");
function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('email');
	var aTelephone = document.getElementById('telephone');
	if ((aEmail != null) && (aTelephone != null))
	{
		if ((aEmail.value == '') && (aTelephone.value == ''))
		{
			alert('You must provide either your telephone number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactRequiredFields);
}
/******************************************************************************************************************************************/

CheckoutRequiredFields = new Array ("conditions","You must tick to say that you agree to Jaydeelivings' terms and conditions in order to checkout.",
									"privacy","You must tick to say that you have read our privacy policy in order to continue.");
function JSFnValidateCheckout()
{
	var aDelivery1 = document.getElementById('Delivery1');
	var aDelviery2 = document.getElementById('Delivery2');
	if ((aDelivery1 != null) && (aDelviery2 != null))
	{
		if ((!aDelivery1.checked) && (!aDelviery2.checked))
		{
			alert('You must select a delivery option to continue.');
			return false;
		}
	}	
	
	return JSFnValidateForm(CheckoutRequiredFields);
}
/******************************************************************************************************************************************/

DeliveryRequiredFields = new Array ("CustomerName","You must enter your name to continue.",
									"CustomerEmail","You must enter your email address to continue",
									"DeliveryAddressLine1","You must enter the first line of your address to continue.",
									"DeliveryAddressLine4","You must enter the town to continue.",
									"DeliveryPostCode","You must enter your postcode to continue.",
									"SpecialDelivery","You must select a delivery location to continue.");

DeliveryBillingRequiredFields = new Array ("BillingFirstName","You must enter your billing first name to continue.",
										   "BillingSurname","You must enter your billing surname to continue",
										   "BillingAddress1","You must enter the first line of your billing address to continue.",
										   "BillingCity","You must enter your billing city/town to continue.",
										   "BillingPostcode","You must enter your billing postcode to continue.");

function JSFnValidateDelivery()
{
	aResult = JSFnValidateForm(DeliveryRequiredFields);

	if (aResult != false)
	{
		var aLoginBillingAddress = document.getElementById('BillingAddress')
		
		if(aLoginBillingAddress == false)
		{
			var aBillingFirstName = document.getElementById('BillingFirstName');
			var aBillingSurname = document.getElementById('BillingSurname');
			var aBillingAddress = document.getElementById('BillingAddress1');
			var aBillingCity = document.getElementById('BillingCity');
			var aBillingPostcode = document.getElementById('BillingPostcode');
			if (((aBillingFirstName.value != '') || (aBillingSurname.value != '') || (aBillingAddress.value != '') || (aBillingCity.value != '') || (aBillingPostcode.value != '')) || (aBillingAddress.value != ''))
			{
				aResult = JSFnValidateForm(DeliveryBillingRequiredFields);	
			}
		}
	}
	
	return aResult;
}
/******************************************************************************************************************************************/

/* Validate the Customer Register Form: Fields */
var aCustomerRegisterRequiredFields = new Array ("FirstName","Please enter your first name to continue",
			 									 "Surname","Please enter your surname to continue",
			 									 "Email","Please enter your email address to continue",	
             									 "Password","Please enter a password to continue",
             									 "PasswordCheck","Please repeat password to continue",
             									 "Description","Please enter contact details name to continue",
             									 "AddressLine1","Please enter address line 1 to continue",
             									 "Postcode","Please enter postcode to continue",
             									 "TelephoneNo","Please enter telephone number to continue");

/* Validate the Customer Register Form: Function */
function JSFnValidateCustomerRegisterForm()
{
	if (this.Password.value.length < 6)
	{
		alert("Please enter at least 6 characters in the \"Password\" field.");
		this.Password.focus();
		return (false);
	}
	 
	// check if both password fields are the same
	if (this.Password.value != this.PasswordCheck.value)
	{
		alert("The two passwords are not the same.");
		this.PasswordCheck.focus();
		return (false);
	}
	 
	return JSFnValidateForm(aCustomerRegisterRequiredFields);
}
/******************************************************************************************************************************************/
var aNewPassRequiredFields = new Array ("OldPassword","Please enter your old password to continue",	
             "Password","Please enter a password to continue",
             "PasswordCheck","Please repeat password to continue");

function JSFnValidateNewPassForm()
{
	if (this.Password.value.length < 6)
	{
		alert("Please enter at least 6 characters in the \"Password\" field.");
		this.Password.focus();
		return (false);
	}
	 
	// check if both password fields are the same
	if (this.Password.value != this.PasswordCheck.value)
	{
		alert("The two passwords are not the same.");
		this.PasswordCheck.focus();
		return (false);
	}
	
	return JSFnValidateForm(aNewPassRequiredFields);
}
/******************************************************************************************************************************************/
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;
}

function chgImg(imgField,newImg,linkField,newlink)
{
	if(document.images)
	{
		document.images[imgField].src = newImg;
		document.getElementById(linkField).setAttribute("href", newlink);
	}
}

