
//function to validate fields
function validateField(oEvent) {
    oEvent = oEvent || window.event;
    var txtField = oEvent.target || oEvent.srcElement;
    var oXmlHttp = zXmlHttp.createRequest();

    oXmlHttp.open("get", "/js/validator/ralph_url.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("img" + txtField.id.substring(3) + "Error");
                var divError = document.getElementById("div" + txtField.id.substring(3));
                
                if (!eval(arrInfo[0])) {
                    imgError.title = arrInfo[1];
                    imgError.style.display = "inline";
                    divError.setAttribute('class','required_error');
					divError.setAttribute('className','required_error');
                    txtField.valid = false;                    
                } else {
                    imgError.style.display = "none";
                    divError.setAttribute('class','required');
					divError.setAttribute('className','required');
                    txtField.valid = true;
                }
            }// else {
             //   alert("An error occurred while trying to contact the server.");
            //}
        }
    };
    oXmlHttp.send(null);
};

function isFormValid() {
    var frmMain = document.forms[0];
    var blnValid = true;

    for (var i=0; i < frmMain.elements.length; i++) {        
        if (typeof frmMain.elements[i].valid == "boolean") {
            blnValid = blnValid && frmMain.elements[i].valid;            
        }
    }
    
    return blnValid;
}

window.onload = function () {
    if (zXmlHttp.isSupported()) {
        var txtVisitorEmail = document.getElementById("txtVisitorEmail");
        var txtSubject = document.getElementById("txtSubject");
        var txtMessage = document.getElementById("txtMessage");
        var txtValidator = document.getElementById("txtValidator");
        txtVisitorEmail.onblur = validateField;
        txtVisitorEmail.valid = false;
        txtSubject.onblur = validateField;
        txtSubject.valid = false;
        txtMessage.onblur = validateField;
        txtMessage.valid = false;
        txtValidator.onblur = validateField;
        txtValidator.valid = false;
        
    }
};

