﻿// requires mootools.js

var baseUrl = '';

function callWebService(wsUrl, wsFunction, responseFunction, paramHash){
    var wsNamespace = "http://tempuri.org/";

    new Ajax(baseUrl + wsUrl, {
        method: 'post', 
        urlEncoded: false, 
        headers: {'Content-Type': 'text/xml; charset="utf-8"', 'SOAPAction': wsNamespace + wsFunction},
        onComplete: function(text) {
                // code for IE
                if (window.ActiveXObject){
                  var doc=new ActiveXObject("Microsoft.XMLDOM");
                  doc.async="false";
                  doc.loadXML(text);
                  }
                // code for Mozilla, Firefox, Opera, etc.
                else {
                  var parser=new DOMParser();
                  var doc=parser.parseFromString(text,"text/xml");
                  }

                var x=doc.documentElement;
                var functionResponse = x.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue
                var myObject = Json.evaluate(functionResponse);
                               
                if (responseFunction != null)
                    eval(responseFunction + '(myObject);');
		    },
		onFailure: function(msg) {
    		    alert("ajax failure");
		    }    
	    }).request(getSoapEnvelope(wsFunction, wsNamespace, paramHash));    
	    
}

function getSoapEnvelope(wsFunction, wsNamespace, paramHash){
    var envelope = '';
    
    envelope += '<?xml version="1.0" encoding="utf-8"?>' + "\n"
    envelope += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + "\n"
    envelope += '  <soap:Body>' + "\n"
    envelope += '    <' + wsFunction + ' xmlns="' + wsNamespace + '">' + "\n"

    if (paramHash != null){
        if (paramHash.length > 0){
            var keys = paramHash.keys();
            var values = paramHash.values();
            
            for (var i = 0; i < paramHash.length; i++){
                envelope += "      <" + keys[i] + ">";
                envelope += values[i];
                envelope += "</" + keys[i] + ">\n";       
            }
        }
    }
          
    envelope += '    </' + wsFunction + '>' + "\n"
    envelope += '  </soap:Body>' + "\n"
    envelope += '</soap:Envelope>'
    
    return envelope
}

