var errMessage = ""

// Set an array for each field type containg the regular expression to match and the error
// message to be returned if incorrect

streetName = new Array(/^[a-zA-Z0-9]([a-zA-Z0-9]|\s)*$/, 'Street name should be only letters and spaces\r\n');
// postcode = new Array(/^[A-Z]{2}\d{1,2}\s\d{1,2}[A-Z]{2}$/,'Postal code should be in the format: XY12 12AB or XY1 2AB\r\n');
postcode = new Array(/^GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2}$/,'Postal code should be in the format: XY12 12AB or XY1 2AB\r\n');
phoneNumber = new Array(/^\d{11}$/,'Phone number should contain eleven numbers\r\n');
emailAddress = new Array(/^\w+((\.|-)\w+)*@\w+\.(co(m|\.uk)|org|net)$/,'E-mail address should be in the format: xyzabc@bleah.co.uk or xyzabc@bleah.com\r\n');

// Create an array of all the field type arrays indexed by the identifier of the field type

validFieldArray = new Array();

validFieldArray['streetName'] = streetName;
validFieldArray['postcode'] = postcode;
validFieldArray['phoneNumber'] = phoneNumber;
validFieldArray['emailAddress'] = emailAddress;

// validateField takes arguments of the field type array and the field to be validated
// Its purpose is to validate the field and fill the errMessage variable if necessary
// if the correct version of JavaScript is in use then it will also colour incorrect
// fields red.
function validateField(field2validate,aField){
	fieldSetting = validFieldArray[field2validate];
	// check the field is correctly formatted
	if (field2validate == 'phoneNumber') {
		aField.value = aField.value.replace(/\s/,"");
		} 
	if (!fieldSetting[0].exec(aField.value)) {
		errMessage = errMessage + fieldSetting[1];
		// check for correct version of JavaScript
		if (document.body) {
			// set the backgound colour of the field to red
			aField.style.backgroundColor = '#FF0000';
			}
		// show tool tip for field
		//showTip('visible',aField.name + 'Tip');
		showTip('2em',aField.name + 'Tip');
		return false;
		}
	// hide tool tip for field
	//showTip('hidden',aField.name + 'Tip');
	showTip('0px',aField.name + 'Tip');
	}

// validField checks an individual field and returns an error message if it is invalid
function validField(validateType, aField) {

	// set the errMessage variable to an empty string
	errMessage = "";
	// call the validateField function to check the field
	validateField(validateType,aField);
	// check the errMessage string, if it is empty the field is valid if the the field
	// fails validation the string will have content.
	if (errMessage != "") {
		// Output the error message and exit the function
		alert(errMessage);
		return false;
		
		}
	// This section will only be carried out is the field is valid

	// check for correct version of JavaScript
	if (document.body) {
		// set the backgound colour of the field to white
		aField.style.backgroundColor = '#FFFFFF';
		}
	}

// validForm is called when the user attempts to submit the form it directly calls the 
// validateForm function in order to compile a comlete list of all error messages before
// returning this (if necessary) to the user. It will return false if any errors are found
function validForm(aForm) {

	// set the errMessage variable to an empty string
	errMessage = "";
	
	// check each field for validity
	validateField('phoneNumber',aForm.billPhoneNumber);
	validateField('streetName',aForm.billAddress1);
	validateField('postcode',aForm.billPostalCode);
	validateField('emailAddress',aForm.billEmailAddress);
	if (aForm.billSame=="No") {
		validateField('phoneNumber',aForm.shipPhoneNumber);
		validateField('streetName',aForm.shipAddress1);
		validateField('postcode',aForm.shipPostalCode);
		}
	
	// if the errMessage string is not empty then errors need to be reported
	if (errMessage != "") {
		alert(errMessage);
		return false
		}
	return true
	}
	
// showTip sets shows or hides the specified tool tip
function showTip(tipStatus,tipId){
	// Check for recent JavaScript version
	if(document.getElementById){
		toolTip=document.getElementById(tipId).style;
//		toolTip.visibility=tipStatus;
		toolTip.height=tipStatus;
		}
	}
	
function sameAddress(listValue) {
	if (listValue == 'Yes') {
		showTip('0px','shipping');
		}
	else {
		showTip('30em','shipping');
		}
	}
