/*
 * @file
 * @description Contains the ajax class.
 *
 * @author Keve Kutner <keve.kutner@4digits.hu>
 * @owner  4DIGITS Kft. <http://4digits.hu>
 */

/*
 * @class
 * @description Constructor.
 */
function fourDigitAjax() {
}//end fourDigitAjax()

/*
 * @function
 * @description Creates the xmlHttpObj. Browser independent.
 *
 * @param
 * @return xmlHttp The xmlHttp object.
 */
fourDigitAjax.prototype.createXmlHttpObj = function() {
    var xmlHttp = undefined;

    try {
        //Firefox, Opera 8.0+, Safari, IE7 / 8
        xmlHttp = new XMLHttpRequest();
    } catch (e){
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (e){
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    return xmlHttp;
}//end createXmlHttpObj()

/*
 * @function
 * @description Sets the success, failure and progress function based on the readystate
 * of the object.
 *
 * @param xmlHttp The xmlHttp obj.
 * @param successFunc The function to be executed on success.
 * @param failureFunc The function to be executed on failure.
 * @param progressFunc The function to be executed on progress.
 *
 * @return void
 */
fourDigitAjax.prototype.setFunc = function(xmlHttp, successFunc, failureFunc, progressFunc) {
    xmlHttp.onreadystatechange = function() {
        switch (xmlHttp.readyState) {
            case 1:
                //Request has been set up.
                break;
            case 2:
                //Request sent.
                break;
            case 3:
                //Request is in process.
                //Get the data downloaded.
                //var total      = xmlHttp.getResponseHeader("Content-Length");
                //var downloaded = xmlHttp.responseText.length; //Simple...
                //progressFunc(total, downloaded);
                break;
            case 4:
                //Request complete
                //There is no status in IE6
                if (this.status == 200) {
                    successFunc(this);
                } else {
                    if (xmlHttp.status == 200) {
                        successFunc(xmlHttp);
                    } else {
                        //If we get status other then 200, we have an error.
                        failureFunc(this);
                    }
                }
                break;
        }
    };
}//end setFunc()

/*
 * @function
 * @description Wrapper, this is used to execute ajax calls
 *
 * @param method POST or GET
 * @param url The URL of the server side script.
 * @param isAsync Tells the function if the request is synchronous or asynchronous.
 * @param successFunc The functionthat will be executed when the request sucessfully completes.
 * @param failureFunc The function that will be executed when the request unecspectedly ends.
 * @param progressFunc The function that willbe executed on progress.
 *
 * @return void
 */
fourDigitAjax.prototype.request = function(method, url, isAsync, successFunc, failureFunc, progressFunc, params) {
    if (method == 'GET') {
        params = null;
    }

    var xmlHttp = this.createXmlHttpObj();
    this.setFunc(xmlHttp, successFunc, failureFunc, progressFunc);
    xmlHttp.open(method, url, isAsync);

    if (method == 'POST') {
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    }

    xmlHttp.send(params);
}//end request()