/////////////////////////////////////////////////
// JavaScript Functions: trim, LTrim and RTrim //
//   Written by:                               //
//     Scott Mitchell                          //
//     mitchell@4guysfromrolla.com             //
//     http://www.4GuysFromRolla.com           //
/////////////////////////////////////////////////

function RTrim(str)
{
	var whitespace = new String(" \t\n\r");

	var s = new String(str);

	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		// We have a string with trailing blank(s)...

		var i = s.length - 1;       // Get length of string

		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;


		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}

	return s;
}

function LTrim(str)
{
	var whitespace = new String(" \t\n\r");

	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1) {
		// We have a string with leading blank(s)...

		var j=0, i = s.length;

		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;


		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}

	return s;
}

function trim(str)
{
	return RTrim(LTrim(str));
}

function is_valid_email(str)
{
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

////////////////////////////
// careers form functions //
////////////////////////////

function send_application(formname)
{
	var jobform = eval("document.forms." + formname);
	
	var errorcode = get_errors(jobform);
	show_errors_onform( errorcode );
	
	if ( errorcode > 0 )
		alert( get_errorstr( errorcode ) );
	else
	{
		jobform.action = "send_application.asp";
		jobform.method = "post";
		jobform.submit();
	}

	return false;
}

function get_errors(jform)
{
	// error codes
	//
	//	required fields
	//	---------------
	//	001	0000 0000 0000 0001	first name is missing
	//	002	0000 0000 0000 0010	last name is missing
	//	004	0000 0000 0000 0100	address is missing
	//	008	0000 0000 0000 1000	city is missing
	//	016	0000 0000 0001 0000	province is missing
	//	032	0000 0000 0010 0000	postal code is missing		
	//	064	0000 0000 0100 0000	email is missing
	//	128	0000 0000 1000 0000	home phone number is missing
	//	256	0000 0001 0000 0000	education is missing
	//
	//	format check
	//	------------
	//	512	0000 0010 0000 0000	email is in bad format

	var errorcode = 0;
	var errorstr;


	// check required fields
	errorcode = ( trim(jform.firstname.value) == "" ? errorcode | 1 : errorcode );
	errorcode = ( trim(jform.lastname.value) == "" ? errorcode | 2 : errorcode );
	errorcode = ( trim(jform.address.value) == "" ? errorcode | 4 : errorcode );
	errorcode = ( trim(jform.city.value) == "" ? errorcode | 8 : errorcode );
	errorcode = ( trim(jform.province.value) == "" ? errorcode | 16 : errorcode );
	errorcode = ( trim(jform.postalcode.value) == "" ? errorcode | 32 : errorcode );
	errorcode = ( trim(jform.email.value) == "" ? errorcode | 64 : errorcode );
	errorcode = ( trim(jform.homephone.value) == "" ? errorcode | 128 : errorcode );
	errorcode = ( trim(jform.education.value) == "" ? errorcode | 256 : errorcode );

	if ( ! (errorcode & 64) )
		errorcode = ( !is_valid_email(jform.email.value) ? errorcode | 512 : errorcode );
		
	return errorcode;
}

function get_errorstr(errcode)
{
	var errstr = "";
	
	errstr = ( errcode & 1 ? errstr + "\n\"First Name\" must be typed in" : errstr );
	errstr = ( errcode & 2 ? errstr + "\n\"Last Name\" must be typed in" : errstr );
	errstr = ( errcode & 4 ? errstr + "\n\"Address\" must be typed in" : errstr );
	errstr = ( errcode & 8 ? errstr + "\n\"City\" must be typed in" : errstr );
	errstr = ( errcode & 16 ? errstr + "\n\"Province\" must be typed in" : errstr );
	errstr = ( errcode & 32 ? errstr + "\n\"Postal Code\" must be typed in" : errstr );
	errstr = ( errcode & 64 ? errstr + "\n\"Email\" must be typed in" : errstr );
	errstr = ( errcode & 512 ? errstr + "\n\"Email\" must be in valid email format (user@domain.com)" : errstr );
	errstr = ( errcode & 128 ? errstr + "\n\"Home Phone Number\" must be typed in" : errstr );
	errstr = ( errcode & 256 ? errstr + "\n\"Educational & Employment Information\" must be typed in" : errstr );
	
	if ( errstr != "" )
		errstr = "Before you can send your application,\nyou must correct the following error(s):\n" + errstr

	return errstr;
}

function show_errors_onform(errcode)
{
	set_error_star('firstname', errcode & 1);
	set_error_star('lastname', errcode & 2);
	set_error_star('address', errcode & 4);
	set_error_star('city', errcode & 8);
	set_error_star('province', errcode & 16);
	set_error_star('postalcode', errcode & 32);
	set_error_star('email', errcode & 64 || errcode & 512);
	set_error_star('homephone', errcode & 128);
	set_error_star('education', errcode & 256);
}


//////////////////////////
// contact us functions //
//////////////////////////

function send_contact(formname)
{
	var contact_form = eval("document.forms." + formname);
	
	var errorcode = get_errors_contact(contact_form);
	show_errors_onform_contact( errorcode );
	
	if ( errorcode > 0 )
		alert( get_errorstr_contact( errorcode ) );
	else
	{
		contact_form.action = "contact_email.asp";
		contact_form.method = "post";
		contact_form.submit();
	}

	return false;
}

function get_errors_contact(c_form)
{
	// error codes
	//
	//	required fields
	//	---------------
	//	001	0000 0000 0000 0001	name is missing
	//	002	0000 0000 0000 0010	title is missing
	//	004	0000 0000 0000 0100	company is missing
	//	008	0000 0000 0000 1000	email is missing
	//	016	0000 0000 0001 0000	phone is missing
	//	032	0000 0000 0010 0000	city is missing		
	//	064	0000 0000 0100 0000	province is missing
	//	128	0000 0000 1000 0000	postal code is missing
	//
	//	format check
	//	------------
	//	256	0000 0010 0000 0000	email is in bad format

	var errorcode = 0;

	// check required fields
	errorcode = ( trim(c_form.thename.value) == "" ? errorcode | 1 : errorcode );
	errorcode = ( trim(c_form.title.value) == "" ? errorcode | 2 : errorcode );
	errorcode = ( trim(c_form.company.value) == "" ? errorcode | 4 : errorcode );
	errorcode = ( trim(c_form.email.value) == "" ? errorcode | 8 : errorcode );
	errorcode = ( trim(c_form.phone.value) == "" ? errorcode | 16 : errorcode );
	errorcode = ( trim(c_form.city.value) == "" ? errorcode | 32 : errorcode );
	errorcode = ( trim(c_form.province.value) == "" ? errorcode | 64 : errorcode );
	errorcode = ( trim(c_form.postal.value) == "" ? errorcode | 128 : errorcode );

	if ( ! (errorcode & 8) )
		errorcode = ( !is_valid_email(c_form.email.value) ? errorcode | 256 : errorcode );
		
	return errorcode;
}

function get_errorstr_contact(errcode)
{
	var errstr = "";
	
	errstr = ( errcode & 1 ? errstr + "\n\"Name\" must be typed in" : errstr );
	errstr = ( errcode & 2 ? errstr + "\n\"Title\" must be typed in" : errstr );
	errstr = ( errcode & 4 ? errstr + "\n\"Company\" must be typed in" : errstr );
	errstr = ( errcode & 8 ? errstr + "\n\"Email\" must be typed in" : errstr );
	errstr = ( errcode & 256 ? errstr + "\n\"Email\" must be in valid email format (user@domain.com)" : errstr );
	errstr = ( errcode & 16 ? errstr + "\n\"Phone\" must be typed in" : errstr );
	errstr = ( errcode & 32 ? errstr + "\n\"City\" must be typed in" : errstr );
	errstr = ( errcode & 64 ? errstr + "\n\"Province / State\" must be typed in" : errstr );
	errstr = ( errcode & 128 ? errstr + "\n\"Postal / Zip Code\" must be typed in" : errstr );
	
	if ( errstr != "" )
		errstr = "Before you can send your contact request,\nyou must correct the following error(s):\n" + errstr

	return errstr;
}

function show_errors_onform_contact(errorcode)
{
	set_error_star('thename', errorcode & 1 );
	set_error_star('title', errorcode & 2 );
	set_error_star('company', errorcode & 4 );
	set_error_star('email', errorcode & 8 || errorcode & 256 );
	set_error_star('phone', errorcode & 16 );
	set_error_star('city', errorcode & 32 );
	set_error_star('province', errorcode & 64 );
	set_error_star('postal', errorcode & 128 );
}

///////////////////////////////////
// Miscellaneous Error Functions //
///////////////////////////////////


function set_error_star(inputname, has_error)
{
	if (has_error)
		document.getElementById("err_" + inputname).style.visibility = "visible";
	else
		document.getElementById("err_" + inputname).style.visibility = "hidden";
}