
//VALIDATION FOR CONTACT FORM
function validateForm(){
	var this_form = document.contact;
	var err_count = 0;
		
		//VALIDATE SUBJECT
		if(this_form.subject.value == -1){
		err_count++;
		hasErrors('subject',true);
		}else{
		hasErrors('subject',false);
		}
		
		//VALIDATE NAME
		if(isEmpty(this_form.yourname.value) || !isName(this_form.yourname.value) || this_form.yourname.value == this_form.yourname.title){
		err_count++;
		hasErrors('yourname',true);
		}else{
		hasErrors('yourname',false);
		}
		
		//VALIDATE EMAIL
		if(isEmpty(this_form.youremail.value) || !isEmail(this_form.youremail.value)){
		err_count++;
		hasErrors('youremail',true);
		}else{
		hasErrors('youremail',false);
		}
		
		//VALIDATE COMPANY
		if(isEmpty(this_form.yourcompany.value) && this_form.yourcompany.value != this_form.yourcompany.title){
		err_count++;
		hasErrors('yourcompany',true);
		}else{
		hasErrors('yourcompany',false);
		}
		
		//VALIDATE PHONE
		if(!isPhoneNumber(this_form.yourphone.value) && (this_form.yourphone.value != this_form.yourphone.title)){
		err_count++;
		hasErrors('yourphone',true);
		}else{
		hasErrors('yourphone',false);
		}
		
		//VALIDATE MESSAGE
		if(isEmpty(this_form.yourmessage.value)){
		err_count++;
		hasErrors('yourmessage',true);
		}else{
		hasErrors('yourmessage',false);
		}
		
	//check for errors and alert the user, or submit the form
	if(err_count == 0){
		$("#formerrors").html('');
		return true;
	}else{
		$("#formerrors").html('<p>Please fill in all the required fields.</p>');
		return false;
	}

}

//VALIDATION FOR FREE SIGNUP FORM
function validateSignupForm(){
	var this_form = document.freesignup;
	var err_count = 0;
		
		
		//VALIDATE AGREE TO ANTI-SPAM
		if(!this_form.agree.checked){ err_count++; alert('You must agree to the anti-spam policy'); return false;}
		
		//VALIDATE NAME
		if(isEmpty(this_form.yourname.value) || !isName(this_form.yourname.value) || this_form.yourname.value == this_form.yourname.title){
		err_count++;
		hasErrors('yourname',true);
		}else{
		hasErrors('yourname',false);
		}
		
		//VALIDATE EMAIL
		if(isEmpty(this_form.youremail.value) || !isEmail(this_form.youremail.value)){
		err_count++;
		hasErrors('youremail',true);
		}else{
		hasErrors('youremail',false);
		}
		
		//VALIDATE PHONE
		if(!isPhoneNumber(this_form.yourphone.value) && (this_form.yourphone.value != this_form.yourphone.title)){
		err_count++;
		hasErrors('yourphone',true);
		}else{
		hasErrors('yourphone',false);
		}
		
		//VALIDATE COMPANY
		if(isEmpty(this_form.yourcompany.value) && this_form.yourcompany.value != this_form.yourcompany.title){
		err_count++;
		hasErrors('yourcompany',true);
		}else{
		hasErrors('yourcompany',false);
		}
		
		//VALIDATE MESSAGE
		if(isEmpty(this_form.yourplans.value)){
		err_count++;
		hasErrors('yourplans',true);
		}else{
		hasErrors('yourplans',false);
		}
		
	//check for errors and alert the user, or submit the form
	if(err_count == 0){
		$("#formerrors").html('');
		return true;
	}else{
		$("#formerrors").html('<p>Please fill in all the required fields.</p>');
		return false;
	}

}




			
	// returns true if the string is a US phone number formatted as...
	// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
	function isPhoneNumber(str){
		var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
		return re.test(str);
	}
	
	// returns true if the string only contains characters A-Z, a-z or 0-9 or . or #
	function isAddress(str){
		var re = /[^a-zA-Z0-9\#\.]/g
		if (re.test(str)) return true;
		return false;
	}
	
	// returns true if the string is 5 digits
	function isZip(str){
		var re = /\d{5,}/;
		if(re.test(str)) return true;
		return false;
	}
	
	// returns true if the string only contains characters A-Z or a-z
	function isAlpha(str){
		var re = /[^a-zA-Z]/g
		if (re.test(str)) return true;
		return false;
	}
	
	// returns true if the string only contains characters A-Z or a-z or 0-9
	function isAlphaNumeric(str){
		var re = /[^a-zA-Z0-9]/g
		if (re.test(str)) return true;
		return false;
	}

	function isEmpty(str){
		if(str.length == 0 || str == null){
			return true;
		}else{
			return false;
		}
	}
	
	// returns true if string follows proper name conventions A-Z or - or ' or space 
function isName(str){
	var re = /^[a-zA-Z-'\s]*$/
	if (re.test(str)) return true;
	return false;
}
	
	function isEmail(str){
	if(str == '') return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
	}
	
function stripWhitespace(str, replacement){
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}

//function for colouring the fields with errors
function hasErrors(fieldID, err){
	if(err){
		document.getElementById(fieldID).style.backgroundColor = '#f9d1d1';
	}else{
		document.getElementById(fieldID).style.backgroundColor = 'white';
	}
}
