// JavaScript Document

function doCheck() {
var errorString = "";
	if (!document.contactForm.field_required_firstname.value) errorString += "Please enter value for \"First Name\".\n";
	if (!document.contactForm.field_required_lastname.value) errorString += "Please enter value for \"Last Name\".\n";
	if (!document.contactForm.field_required_email.value) errorString += "Please enter value for \"Email\".\n";
	if (!good_phone()) errorString += "Please enter only valid Phone Numbers (digits only -- no letters).\n";		
var country_val = document.contactForm.field_required_country.options[document.contactForm.field_required_country.selectedIndex].value;
	if (country_val == '') {
			errorString += "Please select option for \"Country\".\n";
		}		

		if (!document.contactForm.field_required_address.value) errorString += "Please enter value for \"Address\".\n";
		

		if (!document.contactForm.field_required_city.value) errorString += "Please enter value for \"City\".\n";				

		if (country_val == 'United States' || country_val == 'Canada') {

			var state = document.contactForm.field_state;

			if (state.options[state.selectedIndex].value == '') errorString += "Please enter value for \"State\".\n";

			if (!document.contactForm.field_zip.value) errorString += "Please enter value for \"Zip Code\".\n";	
		}			
			

		if (document.contactForm.field_required_program.value == "") errorString += "Please select option for \"Program of Interest\".\n";		


		
	if (! document.contactForm.field_required_gradyear.value) errorString += "Please select option for \"High School Graduation or GED Completion Date\".\n";

		if ( !(document.contactForm.field_required_usa_resident[0].checked || document.contactForm.field_required_usa_resident[1].checked) )
		errorString += "Please select option for \"Are you a U.S. citizen or a permanent resident with a green card?\".\n";				

		return errorString;

	}
	
	
	function good_phone () {
		var phone = document.contactForm.field_required_primary_phone_area.value + document.contactForm.field_required_primary_phone_body.value;		
			return (validate_phone(phone));
	}
	
	function validate_phone (phone_number) {
		if (!phone_number) return false;
	
		phone_number = phone_number.replace(/[^\d]/g, "");
		
		var country_val = document.contactForm.field_required_country.options[document.contactForm.field_required_country.selectedIndex].value;
	
		if (country_val == 'United States' || country_val == 'Canada') {
			if (phone_number.length != 10) return false;		
		}
		else {
			if (phone_number.length < 10) return false;
		}
		
			var bad_strings = /0{10}|1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10}|(\d{3})1234567/g;
//|\d{3}1234567|0{3}(\d+)|1{3}(\d+)|2{3}(\d+)|3{3}(\d+)|4{3}(\d+)|5{3}(\d+)|6{3}(\d+)|7{3}(\d+)|8{3}(\d+)|9{3}(\d+)|123(\d+)|911(\d+)|(\d{3})555(\d+)	
			if (bad_strings.test(phone_number)) return false;	
		
		return true;
	}				



	function handleSubmit () {

   var errorGlobal = doGlobalCheck();
   if (errorGlobal) {
      alert(errorGlobal);
      return false;
   }

		var errorText = doCheck();

		if (errorText) {

			alert(errorText);

			return false;

		}

		else {

			return true;

		}

	}

	
	function preFill () {	
		var country_value = '<TMPL_VAR NAME="country_value_js">';
		var state_value = '<TMPL_VAR NAME="state_value_js">';
		var gradyear_value = '<TMPL_VAR NAME="gradyear_value_js">';	
		var program_value = '<TMPL_VAR NAME="program_value_js">';			
		

		if (country_value) {
			for (i = 0; i < document.contactForm.field_required_country.options.length; i++) {
				if (document.contactForm.field_required_country.options[i].value == country_value) {
					document.contactForm.field_required_country.options[i].selected = true;
					break;
				}
			}
		}

		if (state_value) {
			for (i = 0; i < document.contactForm.field_state.options.length; i++) {
				if (document.contactForm.field_state.options[i].value == state_value) {
					document.contactForm.field_state.options[i].selected = true;
					break;
				}
			}
		}
		
		if (program_value) {
			for (i = 0; i < document.contactForm.field_required_program.options.length; i++) {
				if (document.contactForm.field_required_program.options[i].value == program_value) {
					document.contactForm.field_required_program.options[i].selected = true;
					break;
				}
			}
		}				
	
		if (gradyear_value) {
			for (i = 0; i < document.contactForm.field_required_gradyear.options.length; i++) {
				if (document.contactForm.field_required_gradyear.options[i].value == gradyear_value) {
					document.contactForm.field_required_gradyear.options[i].selected = true;
					break;
				}
			}
		}	
		
		change_country(); 
}


	function change_country () {

		var country = document.contactForm.field_required_country;

		var zip = document.getElementById('zip');

		var state = document.getElementById('state');		

		

		if (country.options[country.selectedIndex].value == 'United States' ||

			country.options[country.selectedIndex].value == 'Canada') {		    

			zip.style.display = 'inline';

			state.style.display = 'inline';

		}

		else {

			zip.style.display = 'none';

			state.style.display = 'none';

		}

	}
