/** uop_single_step_form_validation.js
 * this collection of functions is used to validate the single step form.  It should probably also be used
 * for form validation for all UOP forms, but for some reason unknown to me at this time, it is not.
 *
 * REQUIREMENT: this file requires that uop.js is also included when used
 * REQUIREMENT: scriptaculous
 * REQUIREMENT: prototype
 * REQUIREMENT: uop.js
 */


/* validate()
 * validates form values for the UOP single step form
 */
function validate() {
	$('pref_phone').value = $F('pref_phone').gsub(/[^0-9]/, '');
	$('alt_phone').value = $F('alt_phone').gsub(/[^0-9]/, '');
	sel_prog = $('program_id').options[$('program_id').selectedIndex].getAttribute('code');
	n = getCheckedValue($('form').is_registerednurse);
	
	if($('program_id').options.length == 0 || $F('program_id') == "") {
		alert("Please select a Program of Interest");
		$('program_id').focus();
		return false;
	}
	else if(UOPRequiresNursingValidation(sel_prog) && n != 'Y') {
		alert("Thank you for your interest. This program requires an RN license. Please select another program.");
		return false;					
	}
	else if($F('fname') == "") {
		alert("First name must be completed");
		$('fname').focus();
		return false;
	}
	else if($F('lname') == "") {
		alert("Last name must be completed");
		$('lname').focus();
		return false;
	}
	else if(!validateEmail($F('email'))) {
		alert("Please enter a valid email address");
		$('email').focus();
		return false;
	}
	else if($F('address1') == "") {
		alert("Please enter your address");
		$('address1').focus();
		return false;
	}
	else if($F('city') == "") {
		alert("Please enter a city");
		$('city').focus();
		return false;
	}
	else if($F('state') == "") {
		alert("Please enter a state");
		$('state').focus();
		return false;
	}
	else if(!validatePhone($F('pref_phone'))) {
		alert("Please enter a valid preferred phone number including area code");
		$('pref_phone').focus();
		return false;
	}
	else if($F('alt_phone')!= "" && !validatePhone($F('alt_phone'))) {
		// keep in mind that at this point, alt_phone could have had
		// the non numeric characters stripped out, thus causing the value
		// to be "" even though something was entered, but that is fine since
		// in that situation no alt phone number will be submitted
		alert("Please enter a valid alternate phone number including area code");
		$('alt_phone').focus();
		return false;
	}
	else if(!$('contact_confirmation').checked) {
		alert("Please indicate that you understand a representitive of the University Of Phoenix will contact you.");
		$('contact_confirmation').focus();
		return false;
	}

	return true;
}

/** zipEnter()
 * used to validate zipcode
 *
 * NOTE - this isn't behaving as intended.  If no zip is entered, the radio buttons are still active and available.
 */
function zip_enter() {
	if($F('zip').length < 5) { 
		$('program_id').disabled == true;
		return;
	}
	
	if(validateZip($F('zip'))) {
		$('highesteducation').disabled = false;
		var url = 'quick-form-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);
				}
			}
		);
	}
	else {
		$('online').disabled = true;
		$('campus').disabled = true;
		$('highesteducation').disabled = true;
		$('deg_lvl').disabled = true;
		$('program_id').disabled = true;
		alert("Please enter a valid 5 digit U.S. zip code.");
	}
}

/** category_select()
 * used whenever factors that can affect the categories available to a person change
 */
function category_select() {
	$('category_id').disabled = false;

    radval = $F('school_type');
	if(radval == 1)
		update_program_list(1, "school");
	else if(radval == 2)
		update_program_list($F('closest_campus_id'), "campus");

	return true;
}

/** interests_select()
 * used by the area of interest to unlock the programs select
 */
 function interest_select() {
 	$('program_id').disabled = false;
 	category_select();
 	return true;
 }

/** Update the program select list
 */
function update_program_list(id, id_type) {
	var param = '';
	var prog_id = $F('program_id');
	if(prog_id == null) prog_id = "";
	var url = 'quick-form-ajax.php';
	
	if(id_type == "campus")
		param = 'campus_id='+escape(id)+'&';
	else
		param = 'school_id='+escape(id)+'&';
	
	param += 'action=program_list&program_id=' + prog_id + '&';
	param += 'zip='+escape($F('zip'))+'&category_id='+escape($F('category_id'))+'&';
	param += 'highesteducation='+escape($F('highesteducation'));

	var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: param,
			onSuccess: function(transport)
			{
            	eval(transport.responseText);
			}
		}
	);
}


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

	// be sure to tell which school program was selected and save it so we can pass it into step 3
	var sel_prog = $('program_id').options[progSelIndex].getAttribute('code');
	
	// hide or show the nursing question depending on the program they picked
	if(UOPRequiresNursingValidation(sel_prog))
	{
		if($('nursing_question').style.display == 'none')
			new Effect.SlideDown('nursing_question');
	}	
	else if($('nursing_question').style.display != 'none')
		new Effect.SlideUp('nursing_question');				

	/** do we want a description div?
	var url = 'quick-form-ajax.php';
	var param = 'action=description&program_id=' + escape(programId);

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


 