// JavaScript Document

function validateLoginForm(){
	// SET UP ERROR MESSAGE
	errorMsg = "The following error(s) exist, please rectify and try again.\r\n\r\n";
	// INITIALISE ERROR COUNTER
	hasErrors = 0;
	// CREATE REG EX EXPRESSION
	var alphaNumeric = /[^\sa-zA-Z0-9!@$%&()'",.?_\-]/;
	
	// FUNCTION TO VALIDATE USERNAME ENTERED
	if(document.loginForm.username.value == "") {
			hasErrors ++;
			errorMsg += "Username - Please enter a Username\r\n";
			document.loginForm.username.focus();
			document.loginForm.username.select();
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.loginForm.username.value)){
					hasErrors ++;
					errorMsg += "Username - Invalid characters entered, letters and numbers only.\r\n";		
					document.loginForm.username.value = "";
					document.loginForm.username.focus();
					document.loginForm.username.select();
			}
	}
	
	// FUNCTION TO VALIDATE PASSWORD ENTERED
	if (document.loginForm.pwd.value == "") {
		hasErrors ++;
		errorMsg += "Password - Please enter a password.\r\n";
		document.loginForm.pwd.focus();
		document.loginForm.pwd.select();
	} else if (document.loginForm.pwd.value.length < 8) {
		hasErrors ++;
		errorMsg += "Password - Please enter a minimum of 8 characters.\r\n";
		document.loginForm.pwd.focus();
		document.loginForm.pwd.select();	
	} else {
		if (alphaNumeric.test(document.loginForm.pwd.value)){
				hasErrors ++;
				errorMsg += "Password - Invalid characters entered, letters and numbers only.\r\n";
				document.loginForm.pwd.focus();
				document.loginForm.pwd.select();
		}
	}
	
		// ALL ENTRIES VALIDATED SO SUBMIT FORM
	if(hasErrors < 1) {
			//alert("Submit Form");
			document.loginForm.submit();
	} else {
		alert(errorMsg);
		return false;
	}
}

//
function validateContactUsForm(){
	// SET UP ERROR MESSAGE
	errorMsg = "The following error(s) exist, please rectify and try again.\r\n\r\n";
	var required = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_required.png' width='112' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var invalidChar = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_invalid_char.png' width='230' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var invalidNum = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_invalid_num.png' width='181' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var invalidEmail = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_invalid_email.png' width='118' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var shortChar = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_short_char.png' width='203' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";	
	// INITIALISE ERROR COUNTER
	hasErrors = 0;
	// CREATE REG EX EXPRESSION
	var alphaNumeric = /[^\sa-zA-Z0-9!@$%&()'",.?_\-]/;
	var number = /^\d+$/;
	var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
	// FUNCTION TO VALIDATE FIRST NAME ENTERED
	if(document.contactUsForm.firstName.value == "") {
			hasErrors ++;
			document.getElementById('firstNameError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contactUsForm.firstName.value)){
					hasErrors ++;
					document.getElementById('firstNameError').innerHTML = invalidChar;
			}else{
				document.getElementById('firstNameError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE LAST NAME ENTERED
	if(document.contactUsForm.lastName.value == "") {
			hasErrors ++;
			document.getElementById('lastNameError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contactUsForm.lastName.value)){
					hasErrors ++;
					document.getElementById('lastNameError').innerHTML = invalidChar;
			}else{
				document.getElementById('lastNameError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE E-MAIL ENTERED
	if (document.contactUsForm.email.value == "") { 
			hasErrors ++;
			document.getElementById('emailError').innerHTML = required;
	} else {
			if (!email.test(document.contactUsForm.email.value)){
					hasErrors ++;
					document.getElementById('emailError').innerHTML = invalidEmail;
			}else{
				document.getElementById('emailError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE LOCALE ENTERED
	if(document.contactUsForm.locale.value == "") {
			hasErrors ++;
			document.getElementById('localeError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contactUsForm.locale.value)){
					hasErrors ++;
					document.getElementById('localeError').innerHTML = invalidChar;
			}else{
				document.getElementById('localeError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE PHONE ENTERED
	if (document.contactUsForm.phoneNumber.value != "") {
			if (!number.test(document.contactUsForm.phoneNumber.value)){
					hasErrors ++;
					document.getElementById('phoneNumberError').innerHTML = invalidNum;
			}else{
				document.getElementById('phoneNumberError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE MESSAGE ENTERED
	if(document.contactUsForm.message.value == "") {
			hasErrors ++;
			document.getElementById('messageError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.contactUsForm.message.value)){
					hasErrors ++;
					document.getElementById('messageError').innerHTML = invalidChar;
			}else{
				document.getElementById('messageError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE CAPTCHA ENTERED
	if(document.contactUsForm.code.value == "") {
			hasErrors ++;
			document.getElementById('codeError').innerHTML = required;
	} else {
			if(document.contactUsForm.code.value.length < 5){
				document.getElementById('codeError').innerHTML = shortChar;
			}else{
				if (alphaNumeric.test(document.contactUsForm.code.value)){
						hasErrors ++;
						document.getElementById('codeError').innerHTML = invalidChar;
				}else{
					document.getElementById('codeError').innerHTML = "";
				}
			}
	}
	
		// ALL ENTRIES VALIDATED SO SUBMIT FORM
	if(hasErrors < 1) {
		//alert("Submit Form");
		document.contactUsForm.submit();
	} else {
		//alert(errorMsg);
		return false;
	}
}

//
function validateInfoPackForm(){
	// SET UP ERROR MESSAGE
	errorMsg = "The following error(s) exist, please rectify and try again.\r\n\r\n";
	var required = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_required.png' width='112' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var invalidChar = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_invalid_char.png' width='230' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var invalidNum = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_invalid_num.png' width='181' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var invalidEmail = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_invalid_email.png' width='118' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";
	var shortChar = "<img src='images/error_msg_left.png' width='20' height='23' border='0' /><img src='images/error_msg_short_char.png' width='203' height='23' border='0' /><img src='images/error_msg_right.png' width='8' height='23' border='0' />";	
	// INITIALISE ERROR COUNTER
	hasErrors = 0;
	// CREATE REG EX EXPRESSION
	var alphaNumeric = /[^\sa-zA-Z0-9!@$%&()'",.?_\-]/;
	var number = /^\d+$/;
	var email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	
	// FUNCTION TO VALIDATE FIRST NAME ENTERED
	if(document.infoPackForm.firstName.value == "") {
			hasErrors ++;
			document.getElementById('firstNameError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.firstName.value)){
					hasErrors ++;
					document.getElementById('firstNameError').innerHTML = invalidChar;
			}else{
				document.getElementById('firstNameError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE LAST NAME ENTERED
	if(document.infoPackForm.lastName.value == "") {
			hasErrors ++;
			document.getElementById('lastNameError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.lastName.value)){
					hasErrors ++;
					document.getElementById('lastNameError').innerHTML = invalidChar;
			}else{
				document.getElementById('lastNameError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE E-MAIL ENTERED
	if (document.infoPackForm.email.value == "") { 
			hasErrors ++;
			document.getElementById('emailError').innerHTML = required;
	} else {
			if (!email.test(document.infoPackForm.email.value)){
					hasErrors ++;
					document.getElementById('emailError').innerHTML = invalidEmail;
			}else{
				document.getElementById('emailError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE ADDRESS ENTERED
	if(document.infoPackForm.address.value == "") {
			hasErrors ++;
			document.getElementById('addressError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.address.value)){
					hasErrors ++;
					document.getElementById('addressError').innerHTML = invalidChar;
			}else{
				document.getElementById('addressError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE SUBURB ENTERED
	if(document.infoPackForm.suburb.value == "") {
			hasErrors ++;
			document.getElementById('suburbError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.suburb.value)){
					hasErrors ++;
					document.getElementById('suburbError').innerHTML = invalidChar;
			}else{
				document.getElementById('suburbError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE CITY ENTERED
	if(document.infoPackForm.city.value == "") {
			hasErrors ++;
			document.getElementById('cityError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.city.value)){
					hasErrors ++;
					document.getElementById('cityError').innerHTML = invalidChar;
			}else{
				document.getElementById('cityError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE POSTCODE ENTERED
	if(document.infoPackForm.postcode.value == "") {
			hasErrors ++;
			document.getElementById('postcodeError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.postcode.value)){
					hasErrors ++;
					document.getElementById('postcodeError').innerHTML = invalidChar;
			}else{
				document.getElementById('postcodeError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE PHONE ENTERED
	if (document.infoPackForm.phoneNumber.value != "") {
			if (!number.test(document.infoPackForm.phoneNumber.value)){
					hasErrors ++;
					document.getElementById('phoneNumberError').innerHTML = invalidNum;
			}else{
				document.getElementById('phoneNumberError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE VEHICLE MAKE ENTERED
	var selMake = document.getElementById('make').selectedIndex;
	if(selMake == '0') {
			hasErrors ++;
			document.getElementById('makeError').innerHTML = required;
	} else {
			document.getElementById('makeError').innerHTML = "";
	}
	
	// FUNCTION TO VALIDATE VEHICLE MODEL ENTERED
	if(document.infoPackForm.model.value == "") {
			hasErrors ++;
			document.getElementById('modelError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.model.value)){
					hasErrors ++;
					document.getElementById('modelError').innerHTML = invalidChar;
			}else{
				document.getElementById('modelError').innerHTML = "";
			}
	}	
	
	// FUNCTION TO VALIDATE TYRE SIZE ENTERED
	if(document.infoPackForm.size.value == "") {
			hasErrors ++;
			document.getElementById('sizeError').innerHTML = required;
	} else {
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.size.value)){
					hasErrors ++;
					document.getElementById('sizeError').innerHTML = invalidChar;
			}else{
				document.getElementById('sizeError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE DRIVING CATEGORY ENTERED
	var selCategory = document.getElementById('category').selectedIndex;
	if(selCategory == '0') {
			hasErrors ++;
			document.getElementById('categoryError').innerHTML = required;
	} else {
			document.getElementById('categoryError').innerHTML = "";
	}
	
	// FUNCTION TO VALIDATE HOW OFTEN ENTERED
	var selFrequency = document.getElementById('frequency').selectedIndex;
	if(selFrequency == '0') {
			hasErrors ++;
			document.getElementById('frequencyError').innerHTML = required;
	} else {
			document.getElementById('frequencyError').innerHTML = "";
	}
	
	// FUNCTION TO VALIDATE TYPE OF DRIVING ENTERED
	if(document.infoPackForm.driving_type.value != "") {
			/*hasErrors ++;
			document.getElementById('driving_typeError').innerHTML = required;
	} else {*/
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.driving_type.value)){
					hasErrors ++;
					document.getElementById('driving_typeError').innerHTML = invalidChar;
			}else{
				document.getElementById('driving_typeError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE REFFERAL ENTERED
	var selReferral = document.getElementById('referral').selectedIndex;
	if(selReferral == '0') {
			hasErrors ++;
			document.getElementById('referralError').innerHTML = required;
	} else {
			document.getElementById('referralError').innerHTML = "";
	}
	
	// FUNCTION TO VALIDATE COMMENTS ENTERED
	if(document.infoPackForm.message.value != "") {
			/*hasErrors ++;
			document.getElementById('messageError').innerHTML = required;
	} else {*/
			// Check for number and type of characters entered
			if (alphaNumeric.test(document.infoPackForm.message.value)){
					hasErrors ++;
					document.getElementById('messageError').innerHTML = invalidChar;
			}else{
				document.getElementById('messageError').innerHTML = "";
			}
	}
	
	// FUNCTION TO VALIDATE CAPTCHA ENTERED
	if(document.infoPackForm.code.value == "") {
			hasErrors ++;
			document.getElementById('codeError').innerHTML = required;
	} else {
			if(document.infoPackForm.code.value.length < 5){
				document.getElementById('codeError').innerHTML = shortChar;
			}else{
				if (alphaNumeric.test(document.infoPackForm.code.value)){
						hasErrors ++;
						document.getElementById('codeError').innerHTML = invalidChar;
				}else{
					document.getElementById('codeError').innerHTML = "";
				}
			}
	}
	
		// ALL ENTRIES VALIDATED SO SUBMIT FORM
	if(hasErrors < 1) {
		//alert("Submit Form");
		document.infoPackForm.submit();
	} else {
		//alert(errorMsg);
		return false;
	}
}
