// file: support.js
// author: u4n
// created: 21.09.2007 18:36
// updated: 21.09.2007 18:36


var xmlHttpValidate = createXmlHttpRequestObject();
var cacheValidate   = new Array();

function createXmlHttpRequestObject() {
    var xmlHttp;

    try {
        xmlHttp = new XMLHttpRequest();
    } catch(e) {
        var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                        "MSXML2.XMLHTTP.5.0",
                                        "MSXML2.XMLHTTP.4.0",
                                        "MSXML2.XMLHTTP.3.0",
                                        "MSXML2.XMLHTTP",
                                        "Microsoft.XMLHTTP"
                                        );
        for(var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++) {
            try {
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            } catch(e) {}
        }
    }

    if(!xmlHttp)
        alert("Error occurs while creating XMLHttpRequestObject");
    else
        return xmlHttp;
}

function validate(fieldID, fieldValue) {
    if(xmlHttpValidate) {
        if(fieldID) {
            fieldID = encodeURIComponent(fieldID);
            fieldValue = encodeURIComponent(fieldValue);
            cacheValidate.push("field_id="+fieldID+"&field_value="+fieldValue);
        }
        try {
            if((xmlHttpValidate.readyState == 4 || xmlHttpValidate.readyState == 0) && cacheValidate.length > 0) {
                var cacheEntry = cacheValidate.shift();
                xmlHttpValidate.open("POST", "support.php?action=validate_ajax", true);
                xmlHttpValidate.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttpValidate.onreadystatechange = handleRequestStateChange;
                xmlHttpValidate.send(cacheEntry);
            }
        } catch(e) {
            alert('Can\'t connect to server');
            setTimeout("validate();", 10000);
        }
    }
}

function handleRequestStateChange() {
    if(xmlHttpValidate.readyState == 4) {
        if(xmlHttpValidate.status == 200) {
            try {
                readValidateResponse();
            } catch(e) {
                alert(e.toString());
            }
        } else {
            alert('Server returns: '+xmlHttpValidate.statusText);
        }
    }
}

function readValidateResponse() {
    var responseXML = xmlHttpValidate.responseXML;

    xmlDoc = responseXML.documentElement;
    field = xmlDoc.getElementsByTagName("field")[0].firstChild.data;
    result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;

    if(result != "OK") {
        errorElement = document.getElementById('error_'+field);
        if(errorElement != null) {
            errorElement.innerHTML = result;
        }
        document.getElementById('submitButton').disabled = "true";
    } else {
        document.getElementById('error_'+field).innerHTML = "";
        document.getElementById('submitButton').disabled = false;
    }

    setTimeout("validate();", 1000);
}
