﻿var isIE5 = false;
var agt=navigator.userAgent.toLowerCase();
if(agt.indexOf("msie 5")!=-1)
	isIE5 = true;
	

function PollingHTTPRequest( name , url, proc , delay_ms)
{
	this.url = url;
	this.proc = proc;
	this.name = name;
	this.delay_ms = delay_ms;
	this.requestProcessing = false;
	this.delay_offset = 0;
	
	this.req = null;
	
    try {
		if (window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			if(isIE5)
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			else
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
		}
	} catch (ex) {
		this.req = null;
	}
}

PollingHTTPRequest.prototype.GetDelay = function( )
{
	return(this.delay_ms);
}

PollingHTTPRequest.prototype.SetDelay = function( new_delay )
{
	this.delay_ms = new_delay;
}


PollingHTTPRequest.prototype.IsValid = function()
{
	return( this.req != null );
}

var pollingIdx = 0;

PollingHTTPRequest.prototype.Click = function()
{
	pollingIdx++;
	if( this.requestProcessing ) {
		this.req.abort();
		this.delay_offset += 1000;
	}
	if (this.req.readyState == 4 || this.req.readyState == 0) {
		
		this.requestProcessing = true;
		var sReobj = this;
		this.req.open("GET", this.url + '&OFFSET=' + this.delay_offset, true);
		this.req.onreadystatechange = function() { 
			if (sReobj.req.readyState == 4) {
				if (sReobj.req.status == 200) {
					sReobj.requestProcessing = false;
					sReobj.proc(sReobj.req);
					sReobj.delay_offset = Math.max( sReobj.delay_offset - 250 , 0 );
				} else {
					sReobj.requestProcessing = false;
				}
			}
		};
		this.req.send(null);
	}

}


PollingHTTPRequest.prototype.Poll = function()
{
	this.Click();
	//if( pollingIdx < 10 ) 
	setTimeout ( this.name + ".Poll()" , this.delay_ms + this.delay_offset );
}


PollingHTTPRequest.prototype.Start = function( first_delay_ms )
{
	if( first_delay_ms == 0 ) {
		this.Poll();
	} else {
		setTimeout ( this.name + ".Poll()" , first_delay_ms );
	}
}




function ClickOnceHTTPRequest( )
{
	this.req = null;
	this.requestProcessing = false;
	
	this.clickTries = 0;
	
    try {
		if (window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			if(isIE5)
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			else
				this.req = new ActiveXObject("Msxml2.XMLHTTP");
		}
	} catch (ex) {
		this.req = null;
	}
	
}
ClickOnceHTTPRequest.prototype.IsProcessing = function()
{
	return this.requestProcessing;
}

ClickOnceHTTPRequest.prototype.IsValid = function()
{
	return( this.req != null );
}

ClickOnceHTTPRequest.prototype.Click = function( url , proc )
{
	this.clickTries += 1;
	if(this.requestProcessing && this.clickTries >= 20) {
		this.clickTries = 0;
		this.req.abort();
		this.requestProcessing = false;
	}
	
	if(!this.requestProcessing) {
		this.clickTries = 0;
		if (this.req.readyState == 4 || this.req.readyState == 0) {
			var sReobj = this;
			sReobj.requestProcessing = true;
			this.req.open("GET", url, true);
			this.req.onreadystatechange = function() { 
				if (sReobj.req.readyState == 4) {
					if (sReobj.req.status == 200) {
						proc(sReobj.req);
						sReobj.requestProcessing = false;
					} else {
						sReobj.requestProcessing = false;				
					}
				}
			};
			this.req.send(null);
			return 1;
		} else {
			// failure
			return -1;
		}
	} else {
		return 0;
	}
}


ClickOnceHTTPRequest.prototype.ClickPost = function( url , proc , param)
{
	this.clickTries += 1;
	if(this.requestProcessing && this.clickTries >= 3) {
		this.clickTries = 0;
		this.req.abort();
		this.requestProcessing = false;
	}
	
	if(!this.requestProcessing) {
		this.clickTries = 0;
		if (this.req.readyState == 4 || this.req.readyState == 0) {
			var sReobj = this;
			sReobj.requestProcessing = true;
			this.req.open("POST", url, true);
			this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.req.onreadystatechange = function() { 
				if (sReobj.req.readyState == 4) {
					if (sReobj.req.status == 200) {
						proc(sReobj.req);
						sReobj.requestProcessing = false;
					} else {
						sReobj.requestProcessing = false;				
					}
				}
			};
			this.req.send(param);
		} else {
			// failure
		}
	}
}



// Queue implementation
function QueueNode( elem ){
	this.next = null;
	this.value = elem;
}

function Queue() {
	this.first = null;
	this.last = null;
}

Queue.prototype.dequeue = function() {
	if( this.first == null ) {
		return null;
	} else {
		var val = this.first.value;
		if( this.first == this.last ) {
			this.first = null; this.last = null;
		} else {
			this.first = this.first.next;
		}
		return val;
	}
}

Queue.prototype.enqueue = function(elem) {
	if( this.last == null ) {
		this.last = new QueueNode( elem );
		this.first = this.last;
	} else {
		this.last.next = new QueueNode( elem );
		this.last = this.last.next;
	}
}

var JA_Ajax = {};

Function.prototype.bindThis = function() {
  var __method = this, args = $.makeArray(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($.makeArray(arguments)));
  }
}

JA_Ajax.PeriodicUpdater = function(container, url, options) {
    this.url = url;
    this.container = $("#"+container);
    this.containerID = container;
    this.delay = 1000 * ((options.frequency == null) ? 1 : options.frequency);
    this.before = (options.before == null) ? function() {} : options.before;
    this.after = (options.after == null) ? function() { } : options.after;
    this.options = options;
    this.start();
};

JA_Ajax.PeriodicUpdater.prototype.start = function() {
    this.repeater = setInterval(this.update.bindThis(this), this.delay);
};

JA_Ajax.PeriodicUpdater.prototype.stop = function() {
    clearInterval(this.repeater);
};

JA_Ajax.PeriodicUpdater.prototype.update = function() {
    this.before.bindThis(this).call();
    $.ajax({
        type: 'GET',
        url: this.url,
        dataType: "html",
        contentType: "text/html; charset=utf-8",
        success: function(response) {
            var html = $(response).children("#htmlContent").html();
            if (html == null) {
                html = response;
            }
            var e = document.getElementById(this.containerID);
            e.innerHTML = html;
            this.after.bindThis(this).call();
        }.bindThis(this)
    });
};