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

function checkForm(objForm){

    if(!objForm.title[0].checked && !objForm.title[1].checked){
        alert( "Title is a required field." );
        return false;
    }
    if( objForm.firstName.value == "" ){
        alert( "First Name is a required field." );
        objForm.firstName.focus();
        return false;
    }
    if( objForm.lastName.value == "" ){
        alert( "Last Name is a required field." );
        objForm.lastName.focus();
        return false;
    }
    if( objForm.address.value == "" ){
        alert( "Address is a required field." );
        objForm.address.focus();
        return false;
    }
    if( objForm.city.value == "" ){
        alert( "City is a required field." );
        objForm.city.focus();
        return false;
    }
    if( objForm.state.value == "please select" ){
        alert( "State is a required field." );
        objForm.state.focus();
        return false;
    }
    if( objForm.zip.value == "" ){
        alert( "Zip/Postal Code is a required field." );
        objForm.zip.focus();
        return false;
    }
    if( objForm.country.value == "please select" ){
        alert( "Country is a required field." );
        objForm.country.focus();
        return false;
    }
	// check for valid zip or postal code for US and canada
	if( objForm.country.value == "USA" ){
		// US zip code format: 5 digits or 5 digits - hyphen - 4 digits
		if( isValidUSZip( objForm.zip.value ) ){
			objForm.zip.value = formatUSZip( objForm.zip.value );
		} else {
			alert( "Please enter a valid US ZIP Code.(5 digits or 5 digits - hyphen - 4 digits)" );
			objForm.zip.focus();
			return false;
		}	
	} else if( objForm.country.value == "Canada" ){	
		// Canadian postal code format: Letter-Digit-Letter-Space-Digit-Letter-Digit	
		if( isValidCdnPostalCode( objForm.zip.value ) ){
			objForm.zip.value = formatCdnPostalCode( objForm.zip.value );
		} else {
			alert( "Please enter a valid Canadian Postal Code.  (A1B 7H8)." );
			objForm.zip.focus();
			return false;
		}	
	}
    if( objForm.email.value == "" ){
        alert( "Email is a required field." );
        objForm.email.focus();
        return false;
    }
	if( !isValidEmail( objForm.email.value ) ){
        alert( "Please enter a valid email address. (johndoe@somewhere.com)" );
        objForm.email.focus();
        return false;
	}
    if( objForm.homePhone.value == "" ){
        alert( "Home Phone is a required field." );
        objForm.homePhone.focus();
        return false;
    }	
	// check for valid NA phone number
	if( objForm.country.value == "USA" || objForm.country.value == "Canada" ){
		// Canadian postal code format: Letter-Digit-Letter-Space-Digit-Letter-Digit	
		if( isValidNAPhoneNo( objForm.homePhone.value ) ){
			objForm.homePhone.value = formatNAPhoneNo( objForm.homePhone.value );
		} else {
			alert( "Home Phone: Please enter a valid North America phone number, ###-###-####." );
			objForm.homePhone.focus();
			return false;
		}
	}
  

    if( objForm.source.value == "please select" ){
        alert( "Source is a required field." );
        objForm.source.focus();
        return false;
    }
    if( objForm.source.value == "other" && objForm.otherSource.value == "" ){
	alert( "Please specify other source." );
        objForm.otherSource.focus();
        return false;
    }
    if( objForm.password.value.length < 4 || objForm.password.value != objForm.confirmPassword.value ){
        alert( "Passwords must be at least 4 characters long and Password and Confirm Password must be the same." );
        objForm.password.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);
}
