function getXMLHTTPObject() {
    try {
    req = new XMLHttpRequest();
    } catch(err1) {
      try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (err2) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (err3) {
          req = false;
        }
      }
    }
    return req;
}

function fn_HumanTest(FormName){
	// Checks that the form entry was done by a human
	var testthis = FormName.wordtest.value;
	
	if(testthis.toUpperCase() == 'NEXT'){
		FormName.action = 'http://wwmd.chairacademy.com/index.php/Formhandler/safe_submit2';
		FormName.submit();
	}else{
		alert("Incorrect human word entered.  Please try again.");
	}		
}

function process_validation(theform,theformtype){
       switch(theformtype){
        case 'reg':
            fn_Validate_Reg_Form(theform);
            break;
        case 'call':
            fn_Validate_Call_Form(theform);
            break;
        case 'exe':
            fn_Validate_Exe_Form(theform);
            break;
		case 'generic':
			fn_HumanTest(theform);
			break;			
	   }
}
		   
function checkform(theform,theformtype) {
    // First the normal form validation
    if(theform.vt_VerifyCaptcha.value==''){
          alert('Please enter the string from the displayed image');
          theform.vt_VerifyCaptcha.value='';
          theform.vt_VerifyCaptcha.focus();
          return false;
      }else{
        // Now the Ajax CAPTCHA validation
        checkcode(theform,theformtype);
        return false;
      }
}

function checkcode(thecodeform,theformtype) {
//	alert(window.location.href);
    var url = 'http://www.chairacademy.com/ajax_proxy/pass_thru.php?ws_path=';
    http.open("GET", url + escape(thecodeform.vt_VerifyCaptcha.value), true);
    http.onreadystatechange = function(){
        handleHttpResponse(thecodeform,theformtype);
    };
    http.send(null);
}

function handleHttpResponse(theform,theformtype) {
    if (http.readyState == 4) {
	var the_response = http.responseText;
       if(the_response != '"1"') {
          alert('The entered code was not correct. Please try again');
          theform.vt_VerifyCaptcha.value='';
          theform.vt_VerifyCaptcha.focus();
          return false;
      }
       // Now pass control to the functions specific to the form type
       switch(theformtype){
        case 'reg':
            fn_Validate_Reg_Form(theform);
            break;
        case 'call':
            fn_Validate_Call_Form(theform);
            break;
        case 'exe':
            fn_Validate_Exe_Form(theform);
            break;
		case 'generic':
			fn_HumanTest(theform);
			break;
        default:
            alert('Invalid form type passed to handleHttpResponse');
        }
   }
}

function fn_Validate_Form(the_form_name,the_form_type){
    // Master function for form validation including Captcha
    checkform(the_form_name,the_form_type);
}

function fn_Validate_Reg_Form(the_form_name){
    // Validate the form
    if(the_form_name.a_first_name.value != ''){
	if(the_form_name.aa_last_name.value != ''){
        if(the_form_name.b_title.value != ''){
            if(the_form_name.c_college.value != ''){
                if(the_form_name.e_address.value != ''){
                    if(the_form_name.f_city.value !=''){
                        if(the_form_name.g_state.value !=''){
                            if(the_form_name.h_zipcode.value !=''){
                                if(the_form_name.i_country.value !=''){
                                    if(the_form_name.j_email.value !=''){
                                            if(the_form_name.k_phone.value !=''){
												if(the_form_name.l_fax.value !=''){
                                          			if(the_form_name.n_supervisor_name.value !=''){
                                                   		 if(the_form_name.o_supervisor_title.value !=''){
                                                       		 if(the_form_name.p_supervisor_phone.value !=''){
																 if(the_form_name.q_supervisor_email.value !=''){
																// Form validated ok
																fn_HumanTest(the_form_name);
															}else{
                                                            alert("Supervisor email please");
                                                        }
                                                    	}else{
                                                            alert("Supervisor Phone Number please");
                                                        }
                                                    }else{
                                                        alert("Supervisor Title please");
                                                    }
                                                }else{
                                                    alert("Supervisor Name please");
                                                }
												}else{
                                                alert("Fax Number please");
                                            }
                                            }else{
                                                alert("Phone Number please");
                                            }
                                    }else{
                                        alert("Email please");
                                    }
                                }else{
                                    alert("Country please");
                                }
                            }else{
                                alert("Zipcode please");
                            }
                        }else{
                            alert("State/Province please");
                        }
                    }else{
                        alert("City please");
                    }
                }else{
                    alert("Address please");
                }
            }else{
                alert("College please");
            }
        }else{
            alert("Title please");
        }
    }else{
        alert("Last Name please");
    }
    }else{
	alert("First name please");
   }
}

// Instantiate AJAX object
var http = getXMLHTTPObject();

