// JavaScript Document

function doCheck() {

    var errorString = "";
    var form = document.contactForm;

    var country_val = form.field_required_country.options[form.field_required_country.selectedIndex].value;

    if (country_val == '') {
        errorString += "Please select option for \"Country\".\n";
    }		

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

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

    if (country_val == 'United States' || country_val == 'Canada') {
        var state = form.field_state;
        if (state.options[state.selectedIndex].value == '') errorString += "Please enter value for \"State\".\n";
        if (!form.field_zip.value) errorString += "Please enter value for \"Zip Code\".\n";
    }						

    var msg;
    if (msg = invalid_phone(form.field_required_primary_phone_area.value, form.field_required_primary_phone_body.value, country_val) ) {
        errorString += "Please enter a valid phone number - " + msg;
    }

    if (form.field_required_birth_month.selectedIndex < 1) errorString += "Please select an option for \"Date of Birth (Month):\".\n";		
    if (form.field_required_birth_day.selectedIndex < 1) errorString += "Please select an option for \"Date of Birth (Day):\".\n";		
    if (form.field_required_birth_year.selectedIndex < 1) errorString += "Please select an option for \"Date of Birth (Year):\".\n";		

    if (form.field_required_gradyear.selectedIndex < 1) errorString += "Please select option for \"High School Graduation or GED Completion Date\".\n";		
    
    if (form.field_required_level_of_education.selectedIndex < 1) errorString += "Please select an option for \"Highest level of education\".\n";		

    return errorString;		
		
}

function handleSubmit () {

   var errorGlobal = doGlobalCheck();
   if (errorGlobal) {
      alert(errorGlobal);
      return false;
   }
    var errorText = doCheck();
    if (errorText) {
        alert(errorText);
        return false;
    }
    else {
    	translatebirthdate();
        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">';		

    
    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 (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';
    }
}

function translatebirthdate()
{
	var birthday_val = document.contactForm.field_required_birth_day.options[document.contactForm.field_required_birth_day.selectedIndex].value;
	var birthmonth_val = document.contactForm.field_required_birth_month.options[document.contactForm.field_required_birth_month.selectedIndex].value;
	var birthyear_val = document.contactForm.field_required_birth_year.options[document.contactForm.field_required_birth_year.selectedIndex].value;
	
	if (birthday_val && birthmonth_val && birthyear_val){
		document.contactForm.field_birthdate.value = birthmonth_val + '/' + birthday_val + "/" + birthyear_val;
	}

}
