/**
 * THIS FILE REQUIRES:
 * ../../js/validation.js
 * ../../js/global.js
 */

function validate()
{
	$('PrimaryPhone').value = $F('PrimaryPhone').gsub(/[^0-9]/, '');
	$('SecondaryPhone').value = $F('SecondaryPhone').gsub(/[^0-9]/, '');
	$('Email').value = fixEmail($F('Email'));

	// NOTE: formValidationError is located in ../../js/validation.js
	if(!validateName($F('FirstName')))
	{
		return formValidationError($('FirstName'), "Please enter a valid first name");
	}
	else if(!validateName($F('LastName')))
	{
		return formValidationError($('LastName'), "Please enter a valid last name");
	}
	else if(!validateEmail($F('Email')))
	{
		return formValidationError($('Email'), "Please enter a valid email address");
	}
	else if($F('Address') == "")
	{
		return formValidationError($('Address'), "Please enter your address");
	}
	else if($F('City') == "")
	{
		return formValidationError($('City'), "Please enter a city");
	}
	else if($F('State') == "")
	{
		return formValidationError($('State'), "Please enter a state");
	}
	else if(!validatePhone($F('PrimaryPhone')))
	{
		return formValidationError($('PrimaryPhone'), "Please enter a valid daytime phone number including area code");
	}
	else if($F('SecondaryPhone') != "" && !validatePhone($F('SecondaryPhone')))
	{
		return formValidationError($('SecondaryPhone'), "Please enter a valid evening phone number including area code");
	}
	else if($F('TimeToContactID') == "")
	{
		return formValidationError($('TimeToContactID'), "Please select the best time to be contacted.");
	}
	else if($F('EducationID') == "")
	{
		return formValidationError($('EducationID'), "Please select your highest level of education.");
	}
	else if($F('HSGradYear') == "")
	{
		return formValidationError($('HSGradYear'), "Please select your year of High School graduation or GED completion.");
	}
	
	if($('program_id').options.length == 0 || $F('program_id') == "")
	{
		return formValidationError($('program_id'), "Please select a Program of Interest");
	}

	return true;
}


function enter_zip()
{
	if($F('Zip') == "")
		return false;
	else if(!validateZip($F('Zip')))
		return false;
	
	// ajax_path can be set as a global variable
	if(typeof ajax_path == 'undefined')
		var url = 'ajax.php';
	else
		var url = ajax_path+'ajax.php';
		
	var param = 'action=zip&zip='+$F('Zip');
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: param,
			onSuccess: function(transport)
			{
				eval(transport.responseText);
			}
		}
	);
	
}

function program_select()
{
	var progSelIndex = $('program_id').selectedIndex;
	var programId = $('program_id').options[progSelIndex].value;

	get_program_description(programId);
}

function get_program_description(programId)
{
	// ajax_path can be set as a global variable
	if(typeof ajax_path == 'undefined')
		var url = 'ajax.php';
	else
		var url = ajax_path+'ajax.php';
	
	var param = 'action=description&program_id='+escape(programId);

	var myAjax = new Ajax.Updater(
		'program_description',
		url,
		{
			method: 'post',
			parameters: param,
			evalScripts: true
		}
	);
}

// focus on the form field that has an error
function formValidationError(id, msg)
{
	$(id).style.background = 'yellow';
	alert(msg);
	$(id).focus();
	
	return false;
}

// prompt the user asking if they want to leave the form page if they try to navigate away
function prompt_form_alert(e)
{
	msg  = "Completing the form below is free, fast, and easy. It's your education, ";
	msg += "let us help you make an informed choice!";
	e.returnValue = msg;
	return msg;
}

// remove the form prompt when necessary, like submitting the form
function end_form_alert()
{
	window.onbeforeunload = null;
}


Event.observe(window, 'load', enter_zip);

// prompt a user exiting a form without submitting it, located in globals.js
Event.observe(window, 'load', function(){ new FormExit(document.forms[0]); });
