﻿// Preload the waiting image
var imgWaiting;
if (document.images) {
    imgWaiting = new Image(50, 50);
    imgWaiting.src = "/images/wait_anim.gif";    
}
// -- Preload the waiting image

function AJAX(config) {
    this.config = config;
    
    this.loading = null;
    this.dimmer = null;
    this.iframeBkg = null;
    
    this.xmlRequest = null;
    
    this.init();
};

AJAX.Config = function() {
    this.url = "";
    this.mode = AJAX.GET;
    this.processResponse = null;
    this.displayPopup = false;
};

AJAX.isLoading = false;

AJAX.GET = "GET";
AJAX.POST = "POST";

AJAX.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
AJAX.isMSIE5 = AJAX.isMSIE && (navigator.userAgent.indexOf('MSIE 5') != -1);
AJAX.isMSIE5_0 = AJAX.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
AJAX.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
AJAX.isOpera = navigator.userAgent.indexOf('Opera') != -1;

AJAX.prototype.init = function () {
    var obj = this;
    
    if (obj.config.displayPopup) 
        obj.CreateLoadingPopup();
    
    AJAX.addEvent(window, 'scroll',function (event) {
				 return obj.OnScroll(AJAX.isMSIE ? window.event : event);
			 });
}

AJAX.prototype.CreateLoadingPopup = function () {
    var obj = this;
    
    if (!obj.loading) {
        var loadingDiv = document.getElementById('AJAXLoading');
        
        if (!loadingDiv) {
            var div = document.createElement('DIV');
            div.id = 'AJAXLoading';
            
            div.innerHTML = iLoading + "<br /><br />";
            div.innerHTML += "<img width=\"50\" height=\"50\" border=\"0\">";
            if (document.images) div.lastChild.src = imgWaiting.src;
            
            var css = "border: 1px solid black;"
            css += "width: 250px;";
            css += "height : 120px;";
            css += "border: 1px solid #333;";
            css += "background-color: #ccc;";
            css += "color: #333;";
            css += "font-family: Tahoma;";
            css += "font-size: 12px;";
            css += "font-weight: bold;";
            css += "padding: 15px 0 0 0;";
            css += "text-align: center;";

            div.style.cssText = css;

            document.body.appendChild(div);

            obj.loading = div;
        }else {
            obj.loading = loadingDiv;
        }
    }
    
    if (!obj.iframeBkg) {
        var iframeBkg = document.getElementById('AJAXIFrame');
        
        if (!iframeBkg) {
            //Initail IFrame to Fix Overlap with select
	        obj.iframeBkg = document.createElement("IFRAME");
	        obj.iframeBkg.id = 'AJAXIFrame';
	        obj.iframeBkg.src = 'javascript:false';
	        obj.iframeBkg.style.position = "absolute";
	        obj.iframeBkg.style.top = '0px';
	        obj.iframeBkg.style.left = '0px';
        	obj.iframeBkg.style.display = 'none';
	        
	        obj.iframeBkg.style.width = obj.loading.style.width;

        	document.body.appendChild(this.iframeBkg);
	    } else {
	        obj.iframeBkg = iframeBkg
	    }
	}
}

AJAX.clientWidth = function () {
    return AJAX.filterResults (
        window.innerWidth ? window.innerWidth : 0,
        document.documentElement ? document.documentElement.clientWidth : 0,
        document.body ? document.body.clientWidth : 0
        );
}

AJAX.clientHeight = function () {
    return AJAX.filterResults (
        window.innerHeight ? window.innerHeight : 0,
        document.documentElement ? document.documentElement.clientHeight : 0,
        document.body ? document.body.clientHeight : 0
        );
}

AJAX.scrollLeft = function () {
    return AJAX.filterResults (
        window.pageXOffset ? window.pageXOffset : 0,
        document.documentElement ? document.documentElement.scrollLeft : 0,
        document.body ? document.body.scrollLeft : 0
    );
}

AJAX.scrollTop = function () {
    return AJAX.filterResults (
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0
    );
}

AJAX.filterResults = function (n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

AJAX.prototype.OnScroll = function (e) {
    var obj = this;
    
    obj.setPosition();
}

AJAX.prototype.setPosition = function() {
    var obj = this;
    
    if (obj.loading) {
        var scrollTop = AJAX.scrollTop();
        var scrollLeft = AJAX.scrollLeft();
        var clientWidth = AJAX.clientWidth();
        var clientHeight = AJAX.clientHeight();

        // Calculate left position
        //var left = (clientWidth - obj.loading.offsetWidth)/2;
        var left = (clientWidth - parseInt(obj.loading.style.width))/2;
        //var top = (clientHeight - obj.loading.offsetHeight)/2;
        //left = scrollLeft + left;

        // Set position
        obj.loading.style.position = 'absolute';
        obj.loading.style.left = left + 'px';
        obj.loading.style.top = (scrollTop + 250) + 'px';
        
        obj.iframeBkg.style.left = obj.loading.style.left;
        obj.iframeBkg.style.top = obj.loading.style.top;
    }
}

AJAX.prototype.OnReadyStateChange = function(response) {
    var obj = this;
    
    if(response.readyState==4 && response.responseText){
		obj.config.processResponse(response.responseText);
	};
}

AJAX.prototype.displayLoading = function () {
    var obj = this;
    
    if (obj.loading) {
        obj.setPosition();
        //obj.iframeBkg.style.display = 'block';
        obj.loading.style.display = 'block';
    }
}

AJAX.prototype.hideLoading = function () {
    var obj = this;
    
    if (obj.loading) {
        //obj.iframeBkg.style.display = 'none';
        obj.loading.style.display = 'none';
    }
}

AJAX.moveDot = function () {
    var div = document.getElementById('AJAXLoading');
    if (div == null || div == '' || typeof(div) != 'object') { return false; }

    var html = div.innerHTML;

    // Add dot or start over?
    var num_dots = html.split('.').length-1;
    if (num_dots > 8) {
        div.innerHTML = iLoading + '<br/><br/>.';
    } else {
        div.innerHTML = html + '.';
    }
}

AJAX.prototype.request = function(reqBody) {
    var obj = this;
    
    if(obj.xmlRequest && obj.xmlRequest.readyState != 0){
			obj.xmlRequest.abort();
    };

    obj.xmlRequest = AJAX.CreateXMLHttp();
    
    if (obj.xmlRequest && !AJAX.isLoading) {
        AJAX.isLoading = true;
        
        if (obj.config.displayPopup) 
            obj.displayLoading();
            
        obj.xmlRequest.open(obj.config.mode,obj.config.url,true);
        obj.xmlRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        
        obj.xmlRequest.onreadystatechange = function() {obj.OnReadyStateChange(obj.xmlRequest) };
        
        obj.xmlRequest.send(reqBody);
    };
}

AJAX.prototype.OnReadyStateChange = function(response){
	var obj = this;

	if(response.readyState==4 && response.responseText){
	    AJAX.isLoading = false;
	    
	    if (obj.config.displayPopup) 
	        obj.hideLoading();
	        
		obj.config.processResponse(response.responseText);
	};
};

AJAX.CreateXMLHttp = function(){
	var C=null;
	try{
		C=new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			C=new ActiveXObject("Microsoft.XMLHTTP");
		}catch(sc){
			C=null;
		};
	};
	
	if(!C&&typeof XMLHttpRequest!="undefined"){
		C=new XMLHttpRequest();
	};
	
	return C;
};

AJAX.addEvent = function (obj, name, handler) {
	// Browser check
	if (AJAX.isMSIE)
		obj.attachEvent("on" + name, handler);
	else
		obj.addEventListener(name, handler, false);
};