window.onload = function() 
{
	if (document.getElementById)
	{
		/* Associate logo's onclick event with home page */
		var logo = document.getElementById("logo");
		if (logo != null)
		{
				logo.onclick = function() 
				{
					window.location = 'index.php';
					return false;
				}
		}
		
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aQuoteLimitsForm = document.getElementById('quotelimtsform');
		if (aQuoteLimitsForm != null) aQuoteLimitsForm.onsubmit = JSFnValidateQuoteLimitsForm;

		var aQuoteForm = document.getElementById('quoteform');
		if (aQuoteForm != null) aQuoteForm.onsubmit = JSFnValidateQuoteForm;

		var aFinalQuoteForm = document.getElementById('finalquoteform');
		if (aFinalQuoteForm != null) aFinalQuoteForm.onsubmit = JSFnValidateFinalQuoteForm;
		
		var aConfirmForm = document.getElementById('confirmform');
		if (aConfirmForm != null) 
		{
			JSFnOnChangeConfirmForm();
			aConfirmForm.onsubmit = JSFnValidateCheckout;
		}

		var aPrint = document.getElementById("printbutton");
		if (aPrint != null)
		{
			aPrint.innerHTML = '<input class="button" type="button" value="Print" onclick="PrintPage();"></input>';
		}
	}	
}


function PrintPage()
{
	if(document.getElementById('termsbox').checked)
	{
		window.print();
	}
	else
	{
		alert("You must tick to say that you agree to PCB Value terms and conditions.");
	}
}

function JSFnOnChangeConfirmForm()
{
	var aQuoteForm = document.getElementById('quoteform');
	
	if (aQuoteForm != null)
	{
		aInputs = aQuoteForm.getElementsByTagName("input");
		for (aIndex = 0; aIndex < aInputs.length; aIndex++)
		{
			aInputs[aIndex].onchange = JSFnHideConfirmForm;
		}

		aSelects = aQuoteForm.getElementsByTagName("select");
		for (aIndex = 0; aIndex < aSelects.length; aIndex++)
		{
			aSelects[aIndex].onchange = JSFnHideConfirmForm;
		}	
	}	
}

function JSFnHideConfirmForm()
{
	var aConfirmForm = document.getElementById('confirmform');

	if (aConfirmForm != null)
	{
		aConfirmForm.style.display = 'none';

		var aRecalc = document.getElementById("recalculate");
		if (aRecalc != null)
		{
			aRecalc.innerHTML = '<p class="warning">As you have changed the requirements, please recalculate the quote.</p>';
		}
	}
}

//Validate the contact form
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 aQuoteLimitsRequiredFields = new Array ("CompanyName","Please enter a company name to continue",
											"ContactName","Please enter a contact name to continue",
	 									 	 "ContactPhoneNo","Please enter a phone no to continue",
									 		 "EmailAddress","Please enter your email address to continue");

function JSFnValidateQuoteLimitsForm()
{
	return JSFnValidateForm(aQuoteLimitsRequiredFields);
}


//Validate quote limits form
/*var aQuoteLimitsRequiredFields = new Array ("CompanyName","Please enter a company name",
										"ContactName","Please enter your name",
										"ContactPhoneNo","Please enter your contact phone no",
										"EmailAddress","Please enter an email address",
										"IssueNo","Please enter the issue no.",
										"QuantityRequired","Please enter the quantity required",
										"BoardName","Please enter the board name",
										"MadeBeforeYes","Please select whether we have made this board before",
										"MadeBeforeNo","Please select whether we have made this board before",
										"DimensionsX","Please enter x Dimension",
										"DimensionsY","Please enter y dimension",
										"ApproxNoOfHoles","Please enter the approx no of holes"
										);

function JSFnValidateQuoteLimitsForm()
{
	aOK =  JSFnValidateForm(aQuoteLimitsRequiredFields);
	
	if (aOK == true)
	{
		aOK =  JSFnQuoteStageSpecificChecks();
	}
	
	return aOK;
}
*/
//************************************************

//Validate quote form
var aQuoteRequiredFields = new Array ("QuantityRequired","Please enter the quantity required",
									  "BoardName","Please enter the board name",
									  "CompanyName","Please enter a company name to continue",
										"ContactName","Please enter a contact name to continue",
									 	"EmailAddress","Please enter your email address to continue",
	 									"ContactPhoneNo","Please enter a phone no to continue",
										"MadeBeforeYes","Please select whether we have made this board before",
										"MadeBeforeNo","Please select whether we have made this board before",
										"DimensionsX","Please enter x Dimension",
										"DimensionsY","Please enter y dimension",
										"ApproxNoOfHoles","Please enter the approx no of holes"
										);

function JSFnValidateQuoteForm()
{
	aOK =  JSFnValidateForm(aQuoteRequiredFields);
	
	if (aOK == true)
	{
		aOK =  JSFnQuoteStageSpecificChecks();
	}
	
	return aOK;
}
//************************************************

// Validate final stage of quote
var aConfirmRequiredFields = new Array ("CompanyName","Please enter a company name",
										"EmailAddress","Please enter your email address",
										"PurchaseOrderNo","Please enter a purchase order no.",
										"BoardName","Please enter the board name",
										"IssueNo","Please enter the issue no.",
										"InvoiceAddress","Please enter an invoice address",
										"ContactPhoneNo","Please enter your contact phone no",
										"ContactName","Please enter your name");

function JSFnValidateFinalQuoteForm()
{
	aOK =  JSFnValidateForm(aConfirmRequiredFields);
	
	if (aOK == true)
	{
		aOK =  JSFnFinalQuoteStageSpecificChecks();
	}

	return aOK;
}
//************************************************


CheckoutRequiredFields = new Array ("termsbox","You must tick to say that you agree to PCB Value terms and conditions.");
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;
}

function JSFnQuoteStageSpecificChecks()
{
	Result = true;
	qtyRequired = document.getElementById('QuantityRequired');
	dimmX = document.getElementById('DimensionsX');
	dimmY = document.getElementById('DimensionsY');
	noHoles = document.getElementById('ApproxNoOfHoles');

	if (isNaN(qtyRequired.value))
	{
		alert('The quantity required field must only contain numbers');
		Result = false;
	}
	else if (qtyRequired.value < 1) 
	{
		alert('The quantity required field must be positive');
		Result = false;	
	}
	else if (isNaN(dimmX.value))
	{
		alert('The dimmension in mm(x) field must only contain numbers');
		Result = false;
	}
	else if (dimmX.value < 1) 
	{
		alert('The dimmension in mm(x) field must be positive');
		Result = false;	
	}
	else if (isNaN(dimmY.value))
	{
		alert('The dimmension in mm(y) field must only contain numbers');
		Result = false;
	}
	else if (dimmY.value < 1) 
	{
		alert('The dimmension in mm(y) field must be positive');
		Result = false;	
	}
	else if (isNaN(noHoles.value))
	{
		alert('The approx no. of holes field must only contain numbers');
		Result = false;
	}
	else if (noHoles.value < 1) 
	{
		alert('The approx no. of holes field must be positive');
		Result = false;	
	}
	
	return Result;
}

function JSFnFinalQuoteStageSpecificChecks()
{
	Result = true;
	madeBefore = document.getElementById('MadeBefore');
	
	if (madeBefore.value == 'No')
	{
		if ((document.getElementById('attachment1').value == '') && (document.getElementById('attachment2').value == '') && 
		    (document.getElementById('attachment3').value == '') && (document.getElementById('attachment4').value == ''))
		{
			alert('You have stated that we have not made this board before.  Please attach any relevant documents/designs for this board.');
			Result = false;
		}
		else
		{
			Result = true;
		}
	}
	
	return Result;
}