//function to validate fields
function validateField(oEvent) {
//debugger;
    oEvent = oEvent || window.event;
    var txtField = oEvent.target || oEvent.srcElement;
    var oXmlHttp = zXmlHttp.createRequest();

    // add this bit in to cater for the password comparison.  Need to get the first password.
    if (txtField.name == "g_passwd2") {
      oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value)
                               +"&g_passwd=" + document.getElementById("g_passwd").value, true);
    }
    else if (txtField.name == "g_passwd") {
      if (document.getElementById("g_userid").value != "") { 
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value)
                               +"&g_userid=" + document.getElementById("g_userid").value, true);
      }
      else if (document.getElementById("g_passwd2").value == "") {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value), true);
      }
      else if (document.getElementById("g_passwd2").value != "") { 
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value)
                               +"&g_passwd2=" + document.getElementById("g_passwd2").value, true);
      }
    }
    // cater for if userid/email combination
    else if (txtField.name == "g_userid") {
      if (document.getElementById("g_email").value == "") {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value), true);
      }
      else {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value)
                               +"&g_email=" + document.getElementById("g_email").value, true);
      }
    }
    // cater for if email/userid combination
    else if (txtField.name == "g_email") {
      if (document.getElementById("g_userid").value == "") {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value), true);
      }
      else {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value)
                               +"&g_userid=" + document.getElementById("g_userid").value, true);
      }
    }


    // cater for userid/password combination
    else if (txtField.name == "g_userid") {
      if (document.getElementById("g_passwd").value == "") {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value), true);
      }
      else {
        oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value)
                               +"&g_passwd=" + document.getElementById("g_passwd").value, true);
      }
    }

    else {
     //just the field being validated
      oXmlHttp.open("get", "ValidateRegoForm.php?" + txtField.name + "=" + encodeURIComponent(txtField.value), true);
    }


    oXmlHttp.onreadystatechange = function () {
        if (oXmlHttp.readyState == 4) {
            if (oXmlHttp.status == 200) {
                var arrInfo = oXmlHttp.responseText.split("||");
                var imgError = document.getElementById("g_" + txtField.id.substring(2) + "Error");
                var btnSignUp = document.getElementById("btnSignUp");
                
                if (!eval(arrInfo[0])) {
                    imgError.title = arrInfo[1];
                    imgError.style.display = "";
                    document.getElementById("errorBox").innerHTML = "<h3><span class=\"accent_error\">" + arrInfo[1] + "</span></h3>";
                    txtField.valid = false;                    
                } else {
                    /* cater for the password errors */   
                    if (document.getElementById("g_passwd").value != "") {
                       imgError = document.getElementById("g_passwd"  + "Error");
                       imgError.style.display = "none";
                       imgError.valid = true;
                    }
                    if (document.getElementById("g_passwd2").value != "") {
                       imgError = document.getElementById("g_passwd2"  + "Error");
                       imgError.style.display = "none";
                       imgError.valid = true;
                    }

                    if (((document.getElementById("g_passwd2").value != "") &&
                                             (document.getElementById("g_passwd").value != ""))
                                             &&
                         (document.getElementById("g_passwd2").value ==
                                                         document.getElementById("g_passwd").value)) {
                         document.getElementById("g_passwd2").valid = true;
                         document.getElementById("g_passwd").valid = true;
                    }

                    // passwords keep reseting this
                    var imgError = document.getElementById("g_" + txtField.id.substring(2) + "Error");
 
                    imgError.style.display = "none";
                    txtField.valid = true;
                    document.getElementById("errorBox").innerHTML = "";
                }
                
                btnSignUp.disabled = !isFormValid();
            } else {
                alert("An error occurred while trying to contact the server.");
            }
        }
    };
    oXmlHttp.send(null);
};

function isFormValid() {
    var frmMain = document.forms[0];
    var blnValid = true;

    //  this is for all the elements in the form: for (var i=0; i < frmMain.elements.length; i++) {        

    for (var i=0; i < 4; i++) {       
/*
      if (frmMain.elements[i].valid) {
        alert ("VALID field: " + frmMain.elements[i].value);
      } 
      else {
        alert ("INVALID field: " + frmMain.elements[i].value);
      }
*/

        if (typeof frmMain.elements[i].valid == "boolean") {
            blnValid = blnValid && frmMain.elements[i].valid;            
        }
    }
    
    return blnValid;
}

//if Ajax is enabled, disable the submit button and assign event handlers
window.onload = function () {
    if (zXmlHttp.isSupported()) {
        var btnSignUp = document.getElementById("btnSignUp");        
        var g_userid = document.getElementById("g_userid");
        var g_passwd = document.getElementById("g_passwd");
        var g_passwd2 = document.getElementById("g_passwd2");
        var g_email = document.getElementById("g_email");

        btnSignUp.disabled = true;
        g_userid.onchange = validateField;
        g_passwd2.onchange = validateField;
        g_passwd.onchange = validateField;
        g_email.onchange = validateField;
        g_userid.valid = false;
        g_passwd2.valid = false;
        g_passwd.valid = false;
        g_email.valid = false;
        
    }
};
