﻿// JScript File
var global_ajax_endpoint_url = "/controls/ajax.aspx";

function AjaxCall(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 = '';
    
    // Methods
    this.Send = Send;
}

function Send(resultFunction, preFunction)
{
    if(preFunction != undefined) preFunction();
    
	var xmlHttpReq = false;
	var strURL = global_ajax_endpoint_url + "?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 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 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 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 LoadXMLDoc(xmlstring)
{
    var xmlDoc = null;
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlstring);
        return xmlDoc;
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(xmlstring, "text/xml");
            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 ViBox()
{
    this.text = '';
    this.iconurl = '';
    this.buttontype = 0;    //0=OK, 1=Yes/No, 2=OK/Cancel
}

function ShowViNetBox(text, iconurl)
{
    var outer = document.getElementById('outer-vi-box');
    
    if(outer == null)
    {
        outer = document.createElement('div');
        outer.id = 'outer-vi-box';
        
        var inner = document.createElement('div');
        inner.id = 'inner-vi-box';
        
        var div = document.createElement('div')
        div.id = 'vi-box-image';

        var img = document.createElement('img');
        img.id = 'vi-box-img'
        img.src = iconurl;

        div.appendChild(img);
        inner.appendChild(div);
        
        div = document.createElement('div');
        div.id = 'vi-box-text';
        div.innerHTML = text;
        inner.appendChild(div);

        div = document.createElement('div');
        div.className = 'clear';
        inner.appendChild(div);
        
        div = document.createElement('div');
        div.id = 'vi-box-buttons';
        
        var b_ok = document.createElement('input')
        b_ok.type = 'button';
        b_ok.className = 'vi-box-button';
        b_ok.id = 'vi-box-ok-button';
        b_ok.value = 'OK';
        
        b_ok.onclick = function(ev)
        {
            ViBox.Hide();
        };
        
        div.appendChild(b_ok);
        inner.appendChild(div);
        
        outer.appendChild(inner);
        document.body.appendChild(outer);

        outer.style.left = ((ScreenWidth() / 2) - 175).toString() + 'px';
        outer.style.top = (ScrollY() + ((ScreenHeight() / 2) - 120)).toString() + 'px';
        
        $('#' + outer.id.toString()).fadeIn(200, function()
        {
            
        });
        
    }
}

function HideViNetBox()
{
    var outer = document.getElementById('outer-vi-box');
    if(outer != null)
    {
        $('#' + outer.id.toString()).fadeOut(200, function()
        {
            document.body.removeChild(outer);
        });
    }
}

ViBox.Show = ShowViNetBox;
ViBox.Hide = HideViNetBox;