﻿
function AjaxCall(method, params)
{
    this.method = method;
    if(params == undefined || params == null) this.params = new Array();
    else this.params = params;

    this.sessionid = '';
    this.debugmode = false;
    this.xmldoc = null;
    this.responsetext = '';
    this.customdata = '';
    this.resulttype = 'ajax';
    this.posturl = '/__controls/api-form.aspx';
    this.appendurl = true;
    
    // Methods
    this.Send = Send;
}

function Send(resultFunction, preFunction)
{
    if(typeof(preFunction) != 'undefined' && preFunction != null) preFunction();
    
	var xmlHttpReq = false;
	var strURL = this.posturl;
	
	if(this.appendurl)
	{
	    if(strURL.indexOf('?', 0) < 0) strURL += '?';
	    strURL += "session-id=" + escape(this.sessionid) + "&method=" + escape(this.method) + "&result-type=" + escape(this.resulttype) + "";
	}
    var i = 0;
    
    while(i < this.params.length)
    {
        if(this.params[i].toString().indexOf('=') > 0) strURL += '&' + escape(this.params[i].toString()).replace('%3D', '=');
        i++;
    }
    
    //alert(strURL);
    
	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.resulttype == 'xml') this.xmldoc = xmlHttpReq.responseXml;
			    if(resultFunction != null) resultFunction(this.responsetext);
			}
			catch(ex)
			{
		        alert(ex.toString());
			}
		}
	}
	
	xmlHttpReq.send(strURL);
}

function ScrollX()
{
    if(document.all) return document.documentElement.scrollLeft;
    else return offsety = window.pageXOffset;
}

function ScrollY()
{
    if(document.all) return document.documentElement.scrollTop;
    else return offsety = window.pageYOffset;
}

function LoadXMLDoc(xmlstring)
{
    var xmlDoc = null;
    try //Internet Explorer
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(xmlstring, "text/xml");
        return xmlDoc;
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(xmlstring);
            return xmlDoc;
        }
        catch(e) {alert(e.message)}
    }
    
    return null;
}

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;
}

function ScreenWidth()
{
    if(parseInt(navigator.appVersion) > 3)
    {
        if(navigator.appName=="Netscape") return window.innerWidth;
        if(navigator.appName.indexOf("Microsoft")!=-1) return document.body.offsetWidth;
    }
    
    return 0;
}

function ScreenHeight()
{
    if(parseInt(navigator.appVersion) > 3)
    {
        if(navigator.appName=="Netscape") return window.innerHeight;
        if(navigator.appName.indexOf("Microsoft")!=-1) return document.body.offsetHeight;
    }
    
    return 0;
}


function MessageBox()
{

}

MessageBox.Show = ShowMessageBox;
MessageBox.Hide = HideMessageBox;

function ShowMessageBox(text)
{
    var n = document.getElementById('ajax_box_holder');
    if(n == null)
    {
        n = document.createElement('div', 'ajax_box_holder');
        n.id = 'ajax_box_holder';
        
        var holder = document.createElement('div');
        var txt = document.createElement('div');
        var buttons = document.createElement('div');
        var b_ok = document.createElement('input');
        
        holder.id = 'msg-box';
        
        b_ok.type = 'button';
        b_ok.className = '';
        b_ok.value = 'OK';
        
        b_ok.onclick = function(ev)
        {
            MessageBox.Hide();
        };
        
        txt.innerHTML = text;
        txt.id = 'msg-box-text';
        
        buttons.id = 'msg-box-buttons';
        buttons.appendChild(b_ok);
        
        holder.appendChild(txt);
        holder.appendChild(buttons);
        n.appendChild(holder);
        

        document.body.appendChild(n);
        
        n.style.left = ((ScreenWidth() / 2) - 200).toString() + 'px';
        n.style.top = (ScrollY() + ((ScreenHeight() / 2) - 120)).toString() + 'px';
        
        $('#' + n.id).fadeIn(200);
    }
}

function HideMessageBox()
{
    var n = document.getElementById('ajax_box_holder');
    if(n != null)
    {
        $('#' + n.id).fadeOut(200, function()
        {
            document.body.removeChild(n);
        });
    }
    
}
