﻿// JScript File

function xmlrequest (command,data)
{
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
		    xmlhttp = new XMLHttpRequest();
	    } catch (e) {
		    xmlhttp=false;
	    }
    }
    if (!xmlhttp && window.createRequest) {
	    try {
		    xmlhttp = window.createRequest();
	    } catch (e) {
		    xmlhttp=false;
	    }
    }

    this.request = xmlhttp;
    this.command = command;
    this.data = data;
  
}

xmlrequest.prototype.GetArray = function(target)
{
    return parseResult(this.GetXML(target).documentElement,new Object());
}

xmlrequest.prototype.GetXML = function(target)
{
    var url;
    if (target.indexOf("http://") == -1)
        url = document.location.href + "/" + target;
    else 
        url = target;
        
    this.request.open("PUT", url,false);
    
    var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
    xmlDoc.loadXML("<Request/>");
    var root = xmlDoc.documentElement;
    root.setAttribute("Command",this.command);
    for (key in this.data)
    {
        var elem = xmlDoc.createElement("Param");
        elem.setAttribute("Name",key);
        elem.setAttribute("Value",this.data[key]);
        root.appendChild(elem);
    }
    this.request.send(xmlDoc);
    return this.request.responseXML;
}
var gRequest = null;
function HandleStateChange()
{
  if (gRequest != null && gRequest.request.readyState == 4)
  {
		gRequest.callback(gRequest.request.responseText);	
  }
}

xmlrequest.prototype.Send = function(target, funcCallback)
{
	var url;
    if (target.indexOf("http://") == -1)
        url = document.location.href + "/" + target;
    else 
        url = target;
    gRequest = this;
	this.callback = funcCallback;
	this.request.onreadystatechange = HandleStateChange;
	this.request.open("POST", url,true);
    this.request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
    var dataToSend = "";
    dataToSend += "command=" + this.command;
    for (key in this.data)
    {
        dataToSend += "&" + key + "=" + this.data[key];
    }
    this.request.send(dataToSend);	
}

xmlrequest.prototype.GetHTML = function(target)
{
    var url;
    if (target.indexOf("http://") == -1)
        url = document.location.href + "/" + target;
    else 
        url = target;
            
    this.request.open("POST", url,false);
    this.request.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
    var dataToSend = "";
    dataToSend += "command=" + this.command;
    for (key in this.data)
    {
        dataToSend += "&" + key + "=" + this.data[key];
    }
    this.request.send(dataToSend);	
    return this.request.responseText;
}


function parseResult(root,obj)
{
    for (var index = 0; index < root.childNodes.length; index++)
    {
        var node=root.childNodes[index];
        
        if (node.nodeTypeString =="element")
        {
            if (obj[node.nodeName] != undefined)
            {
                var temp = obj[node.nodeName];
                if (temp.length == undefined)
                    obj[node.nodeName] = new Array(temp);
                obj[node.nodeName][obj[node.nodeName].length] = parseResult(node,new Object());
            }
            else
                obj[node.nodeName] = parseResult(node,new Object());
        }
        else
            obj = node.nodeValue;
    }
    return obj;
}
