	// this script is for the text area character limit
	var maxKeys = 500;
	var keysSoFar = 0;
		
	function change(what) {
		what.value = what.value.substring(0,maxKeys-1);
	}

	function keyup(what) {
		keysSoFar++;
		if (keysSoFar > maxKeys) {
			what.value = what.value.substring(0,maxKeys-1); // chop the last typed char
		}
		//else document.contactform.charsleft.value=maxKeys-keysSoFar;
	}
	
	// form validation
	function validateForm(objForm) {
		var strErrorMessage;
		var boolError = false;
			
		strErrorMessage = "Porsche Centre <%=strCentreName%> - Contact Us\n\n";
		strErrorMessage += "You have not completed the form correctly.\n";
		strErrorMessage += "Please fix the following fields and submit\n";
		strErrorMessage += "the form again:\n\n";

		if (objForm.title.selectedIndex == 0) {
			boolError = true;
			strErrorMessage += "\t- Title\n";
		}
		
		if (objForm.firstname.value == "") {
			boolError = true;
			strErrorMessage += "\t- First name\n";
		}
		
		if (objForm.surname.value == "") {
			boolError = true;
			strErrorMessage += "\t- Surname\n";
		}
		
		if (objForm.email.value == "") {
			boolError = true;
			strErrorMessage += "\t- Email address\n";
		}
		else if (!checkEmail(objForm.email.value)) {
				boolError = true;
				strErrorMessage += "\t- Email address\n";
		}
			
		if (objForm.message.value == "") {
			boolError = true;
			strErrorMessage += "\t- Message\n";
		}
		else {
			if (objForm.message.value.length >= 500) {
				boolError = true;
				strErrorMessage += "\t- Message exceeds 500 character limit\n";
			}
		}
		
		strErrorMessage += "\nPress OK to go back to the form.";
						
		if (boolError) {
			alert(strErrorMessage);
			return false;
		}
		else {
			return true;
		}	
	}
	
	function checkEmail(email) {
		var invalidChars = " /:,;*#^%&$!~`+=()'\"\\|{[]};:<>?";
		if (email == "") {
			return false;
		}

		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i);
			if (email.indexOf(badChar,0) != -1) {
				return false;
			}
		} 
		var atPos = email.indexOf("@",1);

		if (atPos == -1) {
			return false;
		}

		if (email.indexOf("@", atPos+1) != -1) {
			return false;
		}
		
		var periodPos = email.indexOf(".", atPos);

		if (periodPos == -1) {
			return false;
		}

		if (periodPos+3 > email.length) {
			return false;
		}
       
		if ("." == email.charAt(email.length-1)) {
			return false;
		}

		return true;
	}
