﻿var g_ajax_endpoint_url = '/controls/api-qs.aspx';

function AjaxObject(method, params, returntype)
{
    this.method = method;
    if(params == undefined) this.params = new Array();
    else this.params = params;

    if(returntype == undefined) this.returntype = 'html';
    else this.returntype = returntype;

    this.sessionid = '';
    this.debugmode = false;
    this.xmldoc = null;
    this.responsetext = '';
    this.customdata = '';
    this.resulttype = 'ajax';
    
    // Methods
    this.Send = Send;
}

function Send(resultFunction, preFunction)
{
    if(preFunction != undefined) preFunction();
    
	var xmlHttpReq = false;
	var strURL = g_ajax_endpoint_url + "?result-type=" + escape(this.resulttype) + "&session-id=" + escape(this.sessionid) + "&method=" + this.method + "";
    var i = 0;
    
    while(i < this.params.length)
    {
        if(this.params[i].toString().indexOf('=') > 0) strURL += '&' + escape(this.params[i].toString()).replace('%3D', '=');
        i++;
    }
    
	if (window.XMLHttpRequest) xmlHttpReq = new XMLHttpRequest();
	else if (window.ActiveXObject) xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	else
	{
	    if(errFunction != undefined) errFunction('Couldn\'t create ajax core object.');
	    return;
    }
    
	xmlHttpReq.open('GET', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	xmlHttpReq.onreadystatechange = function()
	{
		if (xmlHttpReq.readyState == 4)
		{
		    try
		    {
			    this.responsetext = new String(xmlHttpReq.responseText + '');
			    if(this.returntype == 'xml') this.xmldoc = xmlHttpReq.responseXml;
			    resultFunction(this.responsetext);
			}
			catch(ex)
			{
		        alert(ex.toString());
			}
		}
	}
	
	xmlHttpReq.send(strURL);
}

function LoadXMLDoc(xmlstring)
{
    var xmlDoc = null;
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlstring);
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            parser = new DOMParser();
            xmlDoc=parser.parseFromString(xmlstring, "text/xml");
        }
        catch(e) {alert(e.message)}
    }
    return xmlDoc;
}

function GetNodeValue(xmlDoc, nodename, index)
{
    try
    {
        if(typeof(index) == 'undefined') index = 0;
        return xmlDoc.getElementsByTagName(nodename)[index].firstChild.nodeValue.toString();
    }
    catch(ex)
    {
    
    }
    
    return null;
}

function GetX(obj)
{
    var x = 0;
    do { x += obj.offsetLeft; }
    while (obj = obj.offsetParent);
    
    return x;
}

function GetY(obj)
{
    var y = 0;
    do { y += obj.offsetTop; }
    while (obj = obj.offsetParent);

    return y;
}
