////////////////////////////////////////
//FORMS JAVASCRIPT!!!!!!!!!!!!!!!!!!!!!!

function checkForm(objForm){

    if( objForm.firstNameC.value == "" ){
        alert( "First Name is a required field." );
        objForm.firstNameC.focus();
        return false;
    }
	if( objForm.firstNameL.value == "" ){
        alert( "First Name is a required field." );
        objForm.firstNameL.focus();
        return false;
    }
    if( objForm.lastNameC.value == "" ){
        alert( "Last Name is a required field." );
        objForm.lastNameC.focus();
        return false;
    }
	if( objForm.lastNameL.value == "" ){
        alert( "Last Name is a required field." );
        objForm.lastNameL.focus();
        return false;
    }
    if( objForm.emailC.value == "" ){
        alert( "Email is a required field." );
        objForm.emailC.focus();
        return false;
    }
	if( !isValidEmail( objForm.emailC.value ) ){
        alert( "Please enter a valid email address for yourself. (johndoe@somewhere.com)" );
        objForm.emailC.focus();
        return false;
	}
	if( objForm.emailL.value == "" ){
        alert( "Email is a required field." );
        objForm.emailL.focus();
        return false;
    }
	if( !isValidEmail( objForm.emailL.value ) ){
        alert( "Please enter a valid email address for your friend. (johndoe@somewhere.com)" );
        objForm.emailL.focus();
        return false;
	}
}

// check if character is a digit
function isDigit( theChar ){
	var re = new RegExp("[0-9]");
	return theChar.match(re);
}
// check if character is a letter
function isLetter( theChar ){
	var re = new RegExp("[a-zA-Z]");
	return theChar.match(re);	
}
// removes chars that are not numeric
function stripNonNumerics( inString ){
	var retString = "";
	for( i = 0; i < inString.length; i++ ){		
		var theChar = inString.substr(i,1);
		if( isDigit( theChar ) ){
			retString += theChar;
		}	
	}
	return retString;
}
// remove non numerics
function stripNonAlphaNumerics( inString ){
	var retString = "";
	for( i = 0; i < inString.length; i++ ){		
		var theChar = inString.substr(i,1);
		if( isDigit( theChar ) || isLetter( theChar ) ){
			retString += theChar;
		}	
	}
	return retString;
}
// checks for a valid Canadian postal code
// "^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]$"
function isValidCdnPostalCode( inString ){
	var stripped = stripNonAlphaNumerics( inString );
	if( stripped.length != 6 )
		return false;
	var re = new RegExp("^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]$");
	var matched = stripped.match(re);
	if( matched )
		return true;
	else
		return false;
}
// formats a valid canadian postalcode
// Uppercase: "L#L #L#"
function formatCdnPostalCode( inString ){
	var stripped = stripNonAlphaNumerics( inString );
	var retString = stripped.substr(0,3) + " " + stripped.substr(3,3);
	return retString.toUpperCase();
}
// checks for valid US zip code
// 5-digits or 5-digits-hyphen-4-digits
function isValidUSZip( inString ){
	var stripped = stripNonNumerics( inString );
	if( stripped.length == 5 || stripped.length == 9 )
		return true;
	else 
		return false;
}
// formats a valid US Zip Code
// 5-digits or 5-digits-hyphen-4-digits
function formatUSZip( inString ){	
	var stripped = stripNonNumerics( inString );
	if( stripped.length == 5 )
		return stripped;
	else 
		return stripped.substr(0,5) + "-" + stripped.substr(5,4);
}
// checks for valid north american phone number
// 10 digits for 11 digits starting with 1
// can optionally have an extension if of an x followed by some digits
function isValidNAPhoneNo( inString ){
	var lcString = inString.toLowerCase();
	if( lcString.indexOf("x") > -1 ){
		var phoneNo = stripNonNumerics( lcString.substring(0,lcString.indexOf("x")) );
	} else {
		var phoneNo = stripNonNumerics( inString );
	}
	if( phoneNo.length == 10 || (phoneNo.length == 11 && phoneNo.substr(0,1) == "1") )
		return true;
	else 
		return false;
}
// formats a valid north american phone number
// 10 digits for 11 digits starting with 1
// can optionally have an extension if of an x followed by some digits
function formatNAPhoneNo( inString ){
	var lcString = inString.toLowerCase();
	if( lcString.indexOf("x") > -1 ){
		// has extension
		var phoneNo = stripNonNumerics( lcString.substring(0,lcString.indexOf("x")) );
		var ext = " x" + stripNonNumerics( lcString.substring(lcString.indexOf("x"),lcString.length) );
	} else {
		// no extension
		var phoneNo = stripNonNumerics( inString );
		ext = "";
	}
	if( phoneNo.length == 11 && phoneNo.substr(0,1) == "1") {
		phoneNo = phoneNo.substr(1,10);
	}
	return phoneNo.substr(0,3) + "-" + phoneNo.substr(3,3) + "-" + phoneNo.substr(6,4) + ext; ;
}
function isValidEmail( inString ){
	var re = new RegExp("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
	return inString.match(re);
}

