﻿ErrorMessages=new Object();
ErrorMessages.Initialisation = "Unable to initialize AJAX object.";
ErrorMessages.InvalidParameter = "Invalid parameter (PARAMETER).";

function Ajax (v_method, v_url, v_parameters, v_responsehandler, v_errorhandler) {
    this.Method;
    if (v_method.match(/get/i)) {
        this.Method="GET";
    }
    else {
        this.Method="POST";
    }
    this.Url=v_url;
    this.Parameters=v_parameters;
    this.ResponseHandler=v_responsehandler;
    this.ErrorHandler=v_errorhandler;
    this.HttpRequest=false;
    this.Request=AjaxRequest;
    vObject=this;
    this.Response=function () {
        AjaxResponse(vObject);
    }
    this.Error=AjaxError;
    
    if (v_url==null || typeof(v_url)!="string" || v_url.length < 5) {
        this.Error(ErrorMessages.InvalidParameter.replace(/PARAMETER/, "url"));
    }
    if (v_responsehandler==null || typeof(v_responsehandler)!="function") {
        this.Error(ErrorMessages.InvalidParameter.replace(/PARAMETER/, "response handler"));
    }
    if (v_errorhandler==null || typeof(v_errorhandler)!="function") {
        this.Error(ErrorMessages.InvalidParameter.replace(/PARAMETER/, "error handler"));
    }
    
    if (window.XMLHttpRequest) {
        this.HttpRequest=new XMLHttpRequest();
        if (this.HttpRequest.overrideMimeType) {
            this.HttpRequest.overrideMimeType("text/xml");
        }
    }
    else if (window.ActiveXObject) {
        try {
            this.HttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                this.HttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                this.Error(ErrorMessages.Initialisation);
            }
        }
    }
    else {
        this.Error(ErrorMessages.Initialisation);
    }
    
    if (!this.HttpRequest) {
        this.Error(ErrorMessages.Initialisation);
    }
    
    this.HttpRequest.onreadystatechange = this.Response;
}

function AjaxRequest() {
    if (this.Method=="GET") {
        if (this.Parameters && this.Parameters.length > 0) {
            FinalUrl = this.Url + "?" + f_encode_parameters(this.Parameters);
        }
        else {
            FinalUrl = this.Url;
        }
        if (!this.UserName || !this.Password || typeof this.UserName!="string" || typeof this.Password!="string" || this.UserName.length==0 || this.Password.length==0) {
            this.HttpRequest.open(this.Method, FinalUrl, true);
        }
        else {
            this.HttpRequest.open(this.Method, FinalUrl, true, this.UserName, this.Password);
        }
        this.HttpRequest.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, text/plain, */*");
        this.HttpRequest.send(null);
    }
    else {
        if (!this.UserName || !this.Password || typeof this.UserName!="string" || typeof this.Password!="string" || this.UserName.length==0 || this.Password.length==0) {
            this.HttpRequest.open(this.Method, this.Url, true);
        }
        else {
            this.HttpRequest.open(this.Method, this.Url, true, this.UserName, this.Password);
        }
        this.HttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.HttpRequest.setRequestHeader("Accept", "text/javascript, text/html, application/xml, text/xml, text/plain, */*");
        
        // old Mozilla workaround
        if (this.HttpRequest.overrideMimeType && (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) {
            this.HttpRequest.setRequestHeader("Connection", "close");
        }
        
        // Mozilla workaround: always encode parameter variable, even if it is null
        this.HttpRequest.send(f_encode_parameters(this.Parameters));
    }
}

function AjaxResponse(vAjax) {
    if (vAjax.HttpRequest.readyState==4) {
        if (vAjax.HttpRequest.status==200) {
            vAjax.ResponseHandler(vAjax.HttpRequest.responseText);
        }
        else {
            vAjax.Error("HTTP error " + vAjax.HttpRequest.status + " (" + vAjax.HttpRequest.statusText + ") occured.");
        }
    }
}

function AjaxError(v_message) {
    eval(this.ErrorHandler("Error: " + v_message));
}

function f_encode_parameters(v_parameters) {
    if (v_parameters && v_parameters.length>0) {
        a_parameters=v_parameters.split("&");
        a_parameters_pairs=new Array();
        for (v_i=0;v_i<a_parameters.length;v_i++) {
            a_parameters_pairs[v_i]=a_parameters[v_i].split("=");
        }
        for (v_i=0;v_i<a_parameters.length;v_i++) {
            a_parameters_pairs[v_i][1]=encodeURIComponent(a_parameters_pairs[v_i][1]);
        }
        for (v_i=0;v_i<a_parameters.length;v_i++) {
            a_parameters[v_i]=a_parameters_pairs[v_i].join("=");
        }
        v_parameters=a_parameters.join("&");
    }
    return encodeURI(v_parameters);
}

// update shopping cart panel
function f_get_cartvalue() {
    // $("spanCartValue").innerHTML = "-";
    UpdateCartValue = new Ajax("post", "/shop/CartStatus.ashx", null, f_update_cartvalue, f_update_error);
    UpdateCartValue.Request();
}

function f_update_cartvalue(v_message) {
    $("spanCartValue").innerHTML = v_message;
}

function f_update_error(v_message) {
    // $("spanCartValue").innerHTML = "e";
}

