/* This is the validation function that will switch the required
	input box to red via CSS.
	
	Again, time factor
*/
function validate(theform){
	var i = 0;
	// First Name
	if(theform.first_name.value.length == 0){
		alert("Please enter your first name");
		theform.first_name.focus();
		return false;
	} else {
	/*	if(!validateChars(theform.first_name.value, 'alpha')){
			alert("Please enter a valid first name");
			i++;
			return;
		}*/
	}
	// Last Year
	if(theform.last_name.value.length == 0){
		alert("Please enter your last name");
		theform.last_name.focus();
		return false;
	} else {
		/*if(!validateChars(theform.last_name.value, 'alpha')){
			alert("Please enter a valid last name");
			i++;
			return;
		}*/
	}
	// Email Address
	// Check for *@*.* regular expression
	if(theform.email.value.length == 0){
		alert("Please enter your email address");
		theform.email.focus();
		return false;
	} else {
		if(!validateChars(theform.email.value, 'emailaddress')){
			alert("Please enter a valid email address");
			return false;
		}
	}
	// Address
	if(theform.address1.value.length == 0){
		alert("Please enter your address");
		theform.address1.focus();
		return false;
	}
	// Suburb
	if(theform.address2.value.length == 0){
		alert("Please enter a valid suburb");
		theform.address2.focus();
		return false;
	}
	// City
	if(theform.city.value.length == 0){
		alert("Please enter a valid city");
		theform.city.focus();
		return false;
	}
	// Postcode
	if(theform.zip.value.length == 0){
		alert("Please enter your postcode");
		theform.zip.focus();
		return false;
	}
	// State
	if(theform.state.value.length == 0){
		alert("Please enter your state");
		theform.state.focus();
		return false;
	}
}

function validateChars(v, condition){
	//console.log(v, condition);
	switch (condition){
		case "numeric":
			return !/[^\d]/.test(v);
			break;
		case "alpha":
			//console.log(/[^\d]/.test(v));
			return /^[a-zA-Z]+$/.test(v);
			break;
		case "alphanumeric":
			return /^[a-zA-Z0-9_-]+$/.test(v);
			break;
		case "emailaddress":
			return /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
			break;
		default:
			return false;
	}
}
