// requires mootools.js

var webServiceLog = '';

	

function callWebService(wsUrl, wsFunction, responseFunction, paramHash, responseAsString){
    var wsNamespace = "http://tempuri.org/";
    var newUrl = wsUrl.indexOf("http") != -1 ? wsUrl : baseURL + wsUrl;

    new mooflXHR({
		XDomain: {
							noCacheHeader:false, 
							loadPolicyURL:"http://search.yahooapis.com/crossdomain.xml"
		},
        url: newUrl,
        method: 'post', 
        urlEncoded: false, 
        headers: {'Content-Type': 'text/xml; charset="utf-8"', 'SOAPAction': wsNamespace + wsFunction},
        onSuccess: 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.textContent;
                if (functionResponse == null)
                    functionResponse = x.text;
                // appendDebugLog('WS Response', functionResponse);
                var myObject;
                //if (responseAsString) {
                    myObject = functionResponse;
                //}
                //else
                //{
                //    myObject = JSON.decode(functionResponse)
                //}
                
                               
                if (responseFunction != null)
                    eval(responseFunction + '(myObject);');
		    }    
	    }).send(getSoapEnvelope(wsFunction, wsNamespace, paramHash, responseAsString));    
	    
}

function getSoapEnvelope(wsFunction, wsNamespace, paramHash, responseAsString){
    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.getLength() > 0){
            var keys = paramHash.getKeys();
            var values = paramHash.getValues();
            
            for (var i = 0; i < paramHash.getLength(); i++){
                envelope += "      <" + keys[i] + ">";
                if (typeof(values[i]) == 'string')
                    envelope += encode(values[i].trim());
                else
                    envelope += values[i];
                envelope += "</" + keys[i] + ">\n";       
            }
        }
    }

    envelope += '    </' + wsFunction + '>' + "\n";
    envelope += '  </soap:Body>' + "\n";
    envelope += '</soap:Envelope>';
    
    //appendDebugLog('WS Request', envelope);

    return envelope
}

function encode(str) {
 	    return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\\'/g,'&apos;').replace(/"/g,'&quot;');
}




/*
Script: mooflXHR adapter
License:
	GPLv3  (http://www.gnu.org/licenses/gpl-3.0.txt)
Copyright:
	MaXPert [Zohaib Sibt-e-Hassan] (http://www.gnu.org/licenses/gpl-3.0.txt)
*/
var mooflXHR = new Class(
{
	Extends: Request,

	initialize: function(options){
		var flXOpts = $extend(options.XDomain || {}, {
								instancePooling: true,
								autoUpdatePlayer: true,
							});
		this.xhr = new flensed.flXHR(flXOpts);
		this.options.headers = {};
		this.setOptions(options);
		this.options.isSuccess = this.options.isSuccess || this.isSuccess;
		this.headers = new Hash(this.options.headers);
	}
	
	
}
);

mooflXHR.JSON = new Class({

	Extends: mooflXHR,

	options: {
		secure: true
	},

	initialize: function(options){
		this.parent(options);
	},

	success: function(text){
		this.response.json = JSON.decode(text, this.options.secure);
		this.onSuccess(this.response.json, text);
	}

});
