var highlightBGColor = "#ffffbb";

var resetBGColor; // used to reset the backgroundcolor of the form fields
var resetElement; // element to reset the backgroundColor of 

/*----------------------------------------------------------
"checkForm"

Description:
Validates standard address fields for addresses in the US. 
On the first non-compliant field, function highlights and 
focuses the element, pops up an alert box stating the reason,
and returns false. 
------------------------------------------------------------*/
function checkForm()
{
	
	// if we have a highlighted field, reset it
	if ( resetElement != null )
		resetHighlightedElement( );

	if ( !checkContactInfo( "contact", "Contact" ) )
		return false;

		
	
	// we made it through the gauntlet, return true
	return true;
}

/*----------------------------------------------------------
"checkContactInfo"

Args: 
	String contact - the contact this info belongs to
	String desc - a label (i.e "emergency contact", etc )
	
Description:
Validates standard contact fields like phones, fax, email. 
On the first non-compliant field, function highlights and 
focuses the element, pops up an alert box stating the reason,
and returns false. 
------------------------------------------------------------*/
function checkContactInfo( contact, desc )
{
	var firstName = document.getElementById( contact+"_first_name" );
	var lastName = document.getElementById( contact+"_last_name" );
	var phone = document.getElementById( contact+"_phone" );
	var email = document.getElementById( contact+"_email" );
	var organization = document.getElementById( contact+"_company" );

	var msg;
	if ( firstName.value.trim() == "" )
	{		
		alert("Please enter the your first name.");
		highlightElement( firstName );
		return false;	
	}
	if ( lastName.value.trim() == "" )
	{		
		alert("Please enter your last name.");
		highlightElement( lastName );
		return false;	
	}
	
	if ( organization.value.trim() == "" )
	{		
		alert("Please enter the organization name.");
		highlightElement( organization );
		return false;	
	}
	
	if ( !checkContactAddress( "contact" ) )
		return false;
	
	phone.value = phone.value.replace(/[^0-9]/g, "" );
	msg = checkPhoneNumber( phone.value, "phone", desc, true )
	if ( msg )
	{
		alert( msg );
		highlightElement( phone );
		return false;
	}
	phone.value = formatPhoneNumber( phone.value );
	
	// check the email address for an @ and a dot at the very least
	if ( email.value == "" || !checkEmailAddress( email.value ) )
	{
		alert("Please enter a valid email address.");
		highlightElement( email );
		return false;
	}
	
	return true;	
}

/*----------------------------------------------------------
"checkContactAddress"

Args: 
	String contact - the contact this address belongs to
	String desc - a label (i.e "emergency contact", etc )
	
Description:
Validates standard address fields for addresses in the US. 
On the first non-compliant field, function highlights and 
focuses the element, pops up an alert box stating the reason,
and returns false. 
------------------------------------------------------------*/
function checkContactAddress( contact, desc )
{
	var street = document.getElementById( contact+"_address" );
	var city = document.getElementById( contact+"_city" );
	var state = document.getElementById( contact+"_state" );
	var zip = document.getElementById( contact+"_zip" );	
	
	if ( street.value == "" || street.value.indexOf( " " ) == -1 )
	{		
		alert("Please enter your organization's street address.");
		highlightElement( street );
		return false;	
	}
	if ( city.value == "" || city.value.length < 3 )
	{		
		alert("Please enter your organization's city.");
		highlightElement( city );
		return false;	
	}
	if ( state.value == "" || state.value.length < 2 )
	{		
		alert("Please enter your organization's state.");
		highlightElement( state );
		return false;	
	}
	zip.value = zip.value.replace(/[^0-9]/g, "" );
	if ( !zip.value.match( /[0-9]{5}/ ) )
	{
		alert("Please enter the 5 digit zip code of your organization.");
		highlightElement( zip );
		return false;		
	}
	return true;
}


/*----------------------------------------------------------
"checkEmailAddress"
------------------------------------------------------------*/
function checkEmailAddress( address )
{	
	//check the lengths before and after the '@'
	if ( address.match( /^[^@]{1,64}@[^@]{1,255}$/ ) )
	{
		var email_array = address.split("@");
		var local_array = email_array[0].split(".");
		for ( var i = 0; i < local_array.length; i++ ) 
		{
			if ( !local_array[ i ].match(/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|("[^(\\|")]{0,62}"))$/ ) )
				return false;
		}
		if ( !email_array[1].match(/^\[?[0-9\.]+\]?$/) ) 
		{
			var domain_array = email_array[1].split(".");
			if ( domain_array.length < 2 )
				return false;

			for ( var i = 0; i < domain_array.length; i++)
			{
				if ( !domain_array[i].match(/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/ ) )
					return false;
			}
		}
		return true;
	}
	return false;

}

function formatPhoneNumber( number )
{
	if ( number.length != 10 )
		return number;
	return "("+number.substring(0,3)+")"+" "+number.substring(3,6)+"-"+number.substring(6,10); 
}

/*----------------------------------------------------------
"checkPhoneNumber"

Args: 
	String number - the number to check
	String desc - a label (i.e "mobile phone", "fax", etc )
	String contact - the contact this number belongs to
	bool required - whether or not this field is required
	
Description:
Validates the given number and returns an error message string
on failure and false on success.	
------------------------------------------------------------*/
function checkPhoneNumber( number, desc, contact, required )
{	
	var empty = false;
	var reqTag = " (field not required)";
	if ( required )
		reqTag = "";
	
	
	if ( number == "" )
		empty = true;
	
	if ( !empty )
		number = number.replace( /[^0-9]/g , "");
	
	if ( number.length != 10 )		
	{
		if ( required || !empty )		
			return "Please enter a valid phone number (inc. area code).\n "+reqTag;
	}
	return false;
}

// checkCheckbox
// Determines whether a check box needs to be checked based on the presence of extra field data
function checkCheckbox( fieldName )
{
	el = document.getElementById( fieldName );
	if ( document.getElementById( fieldName+'_extra' ).value.trim() != "" )
		el.checked = true;
	else
		el.checked = false;
}	


// highlightElement
// Takes subject element as argument
// Set's the background color 
function highlightElement( el )
{
	resetBGColor = el.style.backgroundColor;
	resetElement = el;
	el.style.backgroundColor = highlightBGColor;
	el.focus();
	return;
}

// resetHighlightedElement
// Restores the previous bgcolor of the element which highlightElement was called on.
function resetHighlightedElement( )
{
	if ( resetElement == null ) 
		return;
	resetElement.style.backgroundColor = resetBGColor;
	resetElement = null;
	return;
}

// Returns true if the passed value is found in the
// array.  Returns false if it is not.
Array.prototype.inArray = function (value)
{
	var i;
	for (i=0; i < this.length; i++) {
		// Matches identical (===), not just similar (==).
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
// Strip leading and trailing white-space
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "");
}