// JavaScript Document
// Author: Tonia M. White of Affluent Concepts
// www.affluentconcepts.com
// Date: December 30, 2008


//Short Form Validation Script.
function validateContactForm() {
    var msgString = "";
	document.getElementById("ChildInfo_FullName");
	document.getElementById("ChildInfo_Grade");
	document.getElementById("ChildInfo_BirthDate");
	document.getElementById("ChildInfo_Age");
	document.getElementById("Contact_FullName");
	document.getElementById("Contact_StreetAddress");
	document.getElementById("Contact_City");
	document.getElementById("Contact_State");
	document.getElementById("Contact_ZipCode");
	document.getElementById("Contact_HomePhone");
	document.getElementById("Contact_Email");
	document.getElementById("Allergies");
	if( !isTwoLetters( trim(document.IDContactForm.ChildInfo_FullName.value) ) ) { 
		msgString += "* Your child's full name must contain at least 2 letters.<br>"; 
		document.getElementById("ChildInfo_FullName").className='errorStyle';
	}
	if( document.getElementById("ChildInfo_Grade").value == ""){
			msgString += "* Enter your child's grade.<br>";
			document.getElementById("ChildInfo_Grade").className='errorStyle';
	}
	if( document.getElementById("ChildInfo_BirthDate").value == ""){
			msgString += "* Enter your child's birth date.<br>";
			document.getElementById("ChildInfo_BirthDate").className='errorStyle';
	}
	if( document.getElementById("ChildInfo_Age").value == ""){
			msgString += "* Enter your child's age.<br>";
			document.getElementById("ChildInfo_Age").className='errorStyle';
	}
	if( document.getElementById("Contact_FullName").value == ""){
			msgString += "* Enter your full name.<br>";
			document.getElementById("Contact_FullName").className='errorStyle';
	}
	if( document.getElementById("Contact_StreetAddress").value == ""){
			msgString += "* Enter your address.<br>";
			document.getElementById("Contact_StreetAddress").className='errorStyle';
	}
	if( document.getElementById("Contact_City").value == ""){
			msgString += "* Enter your city.<br>";
			document.getElementById("Contact_City").className='errorStyle';
	}
	if( document.getElementById("Contact_State").value == ""){
			msgString += "* Enter your state.<br>";
			document.getElementById("Contact_State").className='errorStyle';
	}
	if(!isPostalCode( document.IDContactForm.Contact_ZipCode.value ) && !isZip( document.IDContactForm.Contact_ZipCode.value ) ) {
		msgString += "* Enter your zip code.<br>"; 
		document.getElementById("Contact_ZipCode").className='errorStyle';
	}
	if( document.IDContactForm.Contact_HomePhone.value == "" || !isPhoneNumber( document.IDContactForm.Contact_HomePhone.value ) ) {
			msgString += "* Enter a valid phone number.<br>"; 
			document.getElementById("Contact_HomePhone").className='errorStyle';
    }
	if( document.IDContactForm.Contact_Email.value == "" || !isEmail( document.IDContactForm.Contact_Email.value ) ) {
    		msgString += "* Enter your valid email.<br>";
			document.getElementById("Contact_Email").className='errorStyle';
    }
	if( msgString.length > 0 ) { 
		document.getElementById('error').innerHTML = msgString;  
		return false; 
	} else {
		document.IDContactForm.Contact_HomePhone.value = correctPhoneNumber(document.IDContactForm.Contact_HomePhone.value);
	}
    return true;
}


//Validation Functions//
function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}
function isPhone(string) {
    if (string.search(/^\d{10}/) != -1)
        return true;
    else
        return false;
}
function isPhoneNumber(string) {
    if (string.search(/^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/) != -1)
        return true;
    else
        return false;
}
function isZip(string) {
    if (string.search(/^\d{5}/) != -1)
        return true;
    else
        return false;
}
function isTwoLetters(string) {
    if (string.length > 1)
        return true;
    else
        return false;
}
function isPhoneSeven(string) {
    if (string.search(/^(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/) != -1)
        return true;
    else
        return false;
}
function isPostalCode(string) {
    if (string.search(/^\s*[a-ceghj-npr-tvxy]\d[a-ceghj-npr-tv-z](\s)?\d[a-ceghj-npr-tv-z]\d\s*$/i) != -1) {
        return true;
	} else {
        return false;
	}
}
function correctPhoneNumber (string) {
	 phoneNumber = string.replace(/ /g,"");
	 phoneNumber = phoneNumber.replace(/\(/g,"");
	 phoneNumber = phoneNumber.replace(/\)/g,"");
	 phoneNumber = phoneNumber.replace(/-/g,"");
	 phoneNumber = phoneNumber.replace(/\./g,"");
	 return phoneNumber;
}
function trim(str){
	while(''+str.charAt(0)==' ')
	str=str.substring(1,str.length);
	while(''+str.charAt(str.length-1)==' ')
	str=str.substring(0,str.length-1);
	return str;
}
