
var essayMaximumLength = 450;
var rateBoxError="#FFFF99";
var rateBoxNoError = "";

var mo;

function haf(x) { mo = x; }

function validate(f)
	{

	var error=false;

	// student_fname
	if (f.student_fname.value=="") { changeStyle(f.student_fname, "student_fname", true);  error=true; }		
	else { changeStyle(f.student_fname, "student_fname", false);   }  //SetCookie ("student_fname", f.student_fname.value);

	// student_lname
	if (f.student_lname.value=="") { changeStyle(f.student_lname, "student_lname", true);  error=true; }		
	else { changeStyle(f.student_lname, "student_lname", false);  }

	// birth_month
	if (pulldown(f.birth_month)) { changeStyle(f.birth_month, "birth_month", true);  error=true; }		
	else  {  changeStyle(f.birth_month, "birth_month", false);  }
	
	// birth_day
	if (pulldown(f.birth_day)) { changeStyle(f.birth_day, "birth_day", true);  error=true; }		
	else  {  changeStyle(f.birth_day, "birth_day", false);  }

	// birth_year
	if (pulldown(f.birth_year)) { changeStyle(f.birth_year, "birth_year", true);  error=true; }		
	else  {  changeStyle(f.birth_year, "birth_year", false);  }

	// student_phone
	if (phonenumber(f.student_phone)) { changeStyle(f.student_phone, "student_phone", true);  error=true; }		
	else { changeStyle(f.student_phone, "student_phone", false);   }

	// student_email
	if (email(f.student_email)) { changeStyle(f.student_email, "student_email", true);  error=true; }		
	else { changeStyle(f.student_email, "student_email", false);   }

	// student_city
	if (f.student_city.value=="") { changeStyle(f.student_city, "student_city", true);  error=true; }		
	else { changeStyle(f.student_city, "student_city", false);   }

	// student_state
	if (pulldown(f.student_state)) { changeStyle(f.student_state, "student_state", true);  error=true; }		
	else  {  changeStyle(f.student_state, "student_state", false);  }

	// institution_name
	if (f.institution_name.value=="") { changeStyle(f.institution_name, "institution_name", true);  error=true; }		
	else { changeStyle(f.institution_name, "institution_name", false);   }

	// campus_address1
	if (f.campus_address1.value=="") { changeStyle(f.campus_address1, "campus_address1", true);  error=true; }		
	else { changeStyle(f.campus_address1, "campus_address1", false);   }

	// campus_city
	if (f.campus_city.value=="") { changeStyle(f.campus_city, "campus_city", true);  error=true; }		
	else { changeStyle(f.campus_city, "campus_city", false);   }

	// campus_state
	if (pulldown(f.campus_state)) { changeStyle(f.campus_state, "campus_state", true);  error=true; }		
	else  {  changeStyle(f.campus_state, "campus_state", false);  }

	// campus_zip
	if (zipCode(f.campus_zip)) { changeStyle(f.campus_zip, "campus_zip", true);  error=true; }		
	else { changeStyle(f.campus_zip, "campus_zip", false);     }

	// rep_fname
	if (f.rep_fname.value=="") { changeStyle(f.rep_fname, "rep_fname", true);  error=true; }		
	else { changeStyle(f.rep_fname, "rep_fname", false);  }

	// rep_lname
	if (f.rep_lname.value=="") { changeStyle(f.rep_lname, "rep_lname", true);  error=true; }		
	else { changeStyle(f.rep_lname, "rep_lname", false);  }

	// rep_title
	if (f.rep_title.value=="") { changeStyle(f.rep_title, "rep_title", true);  error=true; }		
	else { changeStyle(f.rep_title, "rep_title", false);  }

	// phone
	if (phonenumber(f.rep_phone)) { changeStyle(f.rep_phone, "rep_phone", true);  error=true; }		
	else { changeStyle(f.rep_phone, "rep_phone", false);   }

	// program_concept
	if (f.program_concept.value==""||f.program_concept.value.length>4000) { changeStyle(f.program_concept, "program_concept", true);  error=true; }		
	else { changeStyle(f.program_concept, "program_concept", false);  }

	// program_budget
	if (f.program_budget.value==""||f.program_budget.value.length>3000) { changeStyle(f.program_budget, "program_budget", true);  error=true; }		
	else { changeStyle(f.program_budget, "program_budget", false);  }

	// program_timeline
	if (f.program_timeline.value==""||f.program_timeline.value.length>3000) { changeStyle(f.program_timeline, "program_timeline", true);  error=true; }		
	else { changeStyle(f.program_timeline, "program_timeline", false);  }


	// accept_terms
	if (onecheckbox(f.accept_terms)) { changeStyle(f.accept_terms, "accept_terms2", true);  error=true; }		
	else { changeStyle(f.accept_terms, "accept_terms2", false);   }

	// hear_about
	if (radioButtons(f.hear_about)) { changeStyle(f.hear_about[0], "hear_about", true);  error=true; }		
	else { changeStyle(f.hear_about[0], "hear_about", false);   }

	// if user chose "Other" from "hear_about"
	// then double checks that user actually wrote something in the other box
	if (mo)
		{
		if (f.explain_about.value=="") { changeStyle(f.explain_about, "explain_about", true);  error=true; }		
		else { changeStyle(f.explain_about, "explain_about", false);  }	
		}
	else { changeStyle(f.explain_about, "explain_about", false);  }

	// security_code
	if (f.security_code.value=="") { changeStyle(f.security_code, "security_code", true);  error=true; }		
	else { changeStyle(f.security_code, "security_code", false);  }

	// check to see if all form fields were correctly fill in
	if (error) { return false; }
	else { return true; }
	}

function changeStyle(form, formName, changeError)
	{
	// bc = border color
	// bgc = background color
	// tc = text color
	// wt = font weight
	// if a required form field is not filled in correctly,
	// the form field and the label are highlighted to show user improperly 
	// filled out form fields
	if (changeError) { bc="#CC0000"; bgc="#FFFF99"; tc="#CC0000"; wt="bold"; }
	else { bc="#5ED5FD"; bgc="#FFFFFF"; tc="#6A6A6A"; wt="normal"; }
	form.style.borderColor=bc;
	form.style.backgroundColor=bgc;
	
	// bug in ie because the checkbox is before the text
	// so the text would not change styles
	//if (formName=="accept_terms") { formName="accept_terms2"; }
	
	document.getElementById(formName).style.color = tc;
	document.getElementById(formName).style.fontWeight = wt;
	}

// text box with maximum length
function textMax(form, max, message)
	{
	if ( form.value.length > max ) {  return true; }
	return false;
	}

// straight text box
function textBox(form, message)
	{
	if ( form.value.length == 0 ) {  return true; }
	return false;
	}

// read the rules confirmation
function onecheckbox(form)
	{
	if (!form.checked) {  return true; }
	return false;
	}

// radio buttons
function radioButtons(form, message)
	{
	madeChoice=false;
	for (s = 0; s < form.length; s++) { if (form[s].checked == 1) madeChoice=true; }
	if (!madeChoice) {  return true;  }
	return false;
	}

// compare two text boxes
function compare(field1, field2, caseSensitive)
	{
	if (!caseSensitive) { field1.value = field1.value.toLowerCase(); field2.value = field2.value.toLowerCase();  }
	if (field1.value!=field2.value || (field1.value=="" && field2.value=="")) {  return true; }
	return false;
	}

// email
function email(form)
	{
	em = form.value;
	var fff = / /g;
	if ( fff.test(em) ) { em = form.value; em = em.replace(fff, "");  form.value = em; }
	var filter = /^[A-Za-z0-9][A-Za-z0-9\_\-\.]*\@[A-Za-z0-9][A-Za-z0-9\_\-\.]*\.[A-Za-z]{2,5}$/;
	var filter2 = /(\.\.+)|(\@\@+)|(\_\_+)|(\-\-+)/;
	if( (!filter.test( form.value ))  || ( filter2.test( form.value )) ) { return true; }
	return false;
	}

// textarea exceeds max length
function essayTooLong(form, e)
	{
	var word_count=form.value.length;
	if ( word_count > essayMaximumLength ||  word_count==0) { return true; }
	return false;
	}

// zip
function zipCode(form)
	{	
	// usa zip code format: ##### or #####-####
	var filterUS = /^[0-9]{5}(\-[0-9]{4})?$/;
	// canadian zip code format: X#X #X# or X#X#X# or X#X-#X#
	var filterCANADA = /^[A-z][0-9][A-z][ -]?[0-9][A-z][0-9]$/;       
	if( !filterUS.test( form.value )  && !filterCANADA.test( form.value ) )
		{  return true; }
	return false;
	}

// phone
// format ###-###-####
function phonenumber(form)
	{
	var phne = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}?$/;
	if( !phne.test( form.value ) ) {  return true; }
	return false;
	}

// pulldown menu
function pulldown(form)
	{
	if ( form.options[form.options.selectedIndex].value == "--" || form.options[form.options.selectedIndex].value == "" )
		{ return true; }
	return false;
	}

var essayAlertPrefix = "Please write your message using ";
var essayAlertSuffix = " words or less.";
var letterS = (essayMaximumLength > 1) ? "s" : "";
var m1 = "Character";
var m2 = " remaining: "
var m3 = "Limit";
var m4 = " character"
var initialMessage = "(" + m3  + m4 + letterS + " " + essayMaximumLength + ")" ;

var displayErrorMessage = false;

/*
if(document.layers)
	{ document.captureEvents(Event.KEYPRESS); }

document.onkeyup=kpress;
*/

// live time word count tracking of text area
function kpress(e)
	{ checkEssayLength( document.register, 0 ); }

function checkEssayLength(form, submitAlert)
	{
	form = form.whyDoYouLoveIt.value;
	var word_count=form.length;
	var display_count = essayMaximumLength - word_count;
	
	letterS = ( display_count == 1 )  ? "" : "s";

	if ( display_count < 0 ) { display_count = 0; }
	document.getElementById("wordsLeft2").innerHTML = "(" + m3  + m4 + letterS + " " + display_count + ")";
	return true;
	}
