﻿
function CallajaxWebService(wsFunctionName, ParameterName, ParameterValue, wsURL, wsCallback) {
    var CallingFunctionURL = "http://tempuri.org/" + wsFunctionName;
    var xml = "<?xml version='1.0' encoding='utf-8'?>" +
                "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
                " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
                " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
                "<soap:Body>" +
                "<" + wsFunctionName + " xmlns=\'http://tempuri.org/\'>";

    xml = xml + "<" + ParameterName + ">" + ParameterValue + "</" + ParameterName + ">";

    xml = xml + "</" + wsFunctionName + ">";
    xml = xml + "</soap:Body></soap:Envelope>";

    xmlHttp = createRequestObject();

    xmlHttp.open("POST", wsURL, true);
    xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttp.setRequestHeader("Content-Length", xml.length);
    xmlHttp.setRequestHeader("SOAPAction", CallingFunctionURL);
    xmlHttp.onreadystatechange = ajaxcallback
    xmlHttp.tmp_functionName = wsCallback
    //xmlHttp.tmp_ResponseName = wsFunctionName + "Result"
    xmlHttp.send(xml);
    var resultXml = xmlHttp.responseXML;

    return resultXml  
}

function createRequestObject() {

    if (window.XMLHttpRequest) 
        return xmlhttprequest = new XMLHttpRequest();
    else if (window.ActiveXObject)
        return xmlhttprequest = new ActiveXObject("Microsoft.XMLHTTP");
}

function ajaxcallback() {

    if (xmlHttp.readyState == 4) 
    {
        var myXml = xmlHttp.responseXML;

        var fnccallBack = xmlHttp.tmp_functionName;
        
        if (xmlhttprequest.status == 500)
            callbackstr = "<E>" + myXml.childNodes[1].text + "<E>"
        else {
            var ResponseelmName = fnccallBack + "Result";

            /*if (window.XMLHttpRequest) {
                callbackstr = myXml.text;
            }
            else*/

            if (myXml.text)
                callbackstr = myXml.text;
            else if (myXml.firstChild.childNodes[0].textContent)
                callbackstr = myXml.firstChild.childNodes[0].textContent;
            else            
                callbackstr = myXml.getElementsByTagName(ResponseelmName)[0].childNodes[0].nodeValue;
        }

        var fnccallBack = xmlHttp.tmp_functionName;

        //callbackstr = callbackstr.replace(String.fromCharCode(10), "");
        //callbackstr = callbackstr.replace(String.fromCharCode(13), "");
        callbackstr = callbackstr.replace(/\n/g, "");
        callbackstr = callbackstr.replace(/\'/g, "\\" + "\'" + "\\" + "\'");
        
        var xstr = fnccallBack + "('" + callbackstr + "')"
        eval(xstr)
    }
}