/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Ajax(doneHandler, failHandler, params) {
	this.onDone = doneHandler;
	this.onFail = failHandler;
	this.onCallParams = params;
	this.transport = this.getTransport();
	this.showLoad = false;
	this.showingLoad = false;
	this.isXML = false;
	this.transport.onreadystatechange = toggleAjax(this);
}

Ajax.prototype.get = function (uri, query) {
	var rand = Math.floor(Math.random()*9000)+1000;
	uri += "&arandno="+rand;
	var sep = "?";
	if (uri.indexOf('?') != -1) {
		sep = '&';
	}
	var fullURI = uri;//+(query ? (sep+query) : '');
	if (this.isXML && this.overrideMimeType) {
		this.overrideMimeType('text/xml');
	}
	this.transport.open('GET', fullURI, true);
	this.transport.send('');
};

Ajax.prototype.post = function (uri, data) {
	var rand = Math.floor(Math.random()*9000)+1000;
	data += "&arandno="+rand;

	if (this.isXML && this.overrideMimeType) {
		this.overrideMimeType('text/xml');
	}
	this.transport.open('POST', uri, true);
	this.transport.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.transport.send(data);
};

Ajax.prototype.stateDispatch = function () {
	if( this.transport.readyState == 1 && this.showLoad )
		ajaxShowLoadIndicator(this);

	if( this.transport.readyState == 4 ) {
		if( this.showingLoad )
			ajaxHideLoadIndicator(this);
		if( this.transport.status >= 200 && this.transport.status < 300 && this.transport.responseText.length > 0 ) {
			if( this.onDone ) {
				var response;
				if (this.isXML) {
					response = this.transport.responseXML;
				} else {
					response = this.transport.responseText;
					/*if(response.trim()=="logout") {
						window.parent.location="/login.jsp";
						return;
					}*/
				}
				if (this.onCallParams)
					this.onDone(this, response, this.onCallParams);
				else
					this.onDone(this, response);
			}
		} else {
			if( this.onFail ) {
				if (this.onCallParams)
					this.onFail(this, this.onCallParams);
				else
					this.onFail(this);
			}
		}
	}
};

var ajaxTransport = null;

Ajax.prototype.getTransport = function () {
	if (ajaxTransport != null) {
		return ajaxTransport;
	}
	var ajax = null;

	try { ajax = new XMLHttpRequest(); }
	catch(e) { ajax = null; }

	try { if(!ajax) ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) { ajax = null; }

	try { if(!ajax) ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch(e) { ajax = null; }



	return ajax;
};

function toggleAjax(ajaxObject) {
	return function () { ajaxObject.stateDispatch(); };
}


function progressElem(txt){
  var fullPage = document.getElementById("fullpage");
	var cont = document.getElementById("indicator");
	var bw = 0, bh = 0;
	if (window.innerHeight) bh = window.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight != 0) bh = document.documentElement.clientHeight;
  else if (document.body) bh = document.body.clientHeight;

	if(window.innerWidth) bw = window.innerWidth;
  else if(document.documentElement &&  document.documentElement.clientWidth) bw = document.documentElement.clientWidth
  else if(document.body) bw = document.body.clientWidth;

	 var xScroll, yScroll, pageHeight =0 ,pageWidth=0;

  if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < bh){
		pageHeight = bh;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < bw){
		pageWidth = bw;
	} else {
		pageWidth = xScroll;
	}
	var currentOffsetTop = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari
  var currentOffsetLeft = document.documentElement.scrollLeft || document.body.scrollLeft; // body for Safari

	var desiredOffsetTop = (bh- cont.offsetHeight)/2+currentOffsetTop;
  var desiredOffsetLeft = (bw - cont.offsetWidth)/2+currentOffsetLeft;

	if (desiredOffsetTop != parseInt(cont.style.top, 10)) cont.style.top = desiredOffsetTop + 'px';
  if (desiredOffsetLeft != parseInt(cont.style.left, 10)) cont.style.left= desiredOffsetLeft + 'px';

	fullPage.style.height = (pageHeight) + 'px';
  fullPage.style.width = (pageWidth) + 'px';
	fullPage.className="";
	cont.className = "";
}

function resetProgress() {
	var fullPage = document.getElementById("fullpage");
	var cont = document.getElementById("indicator");
	fullPage.className="displayNone";
	cont.className = "displayNone";
}

function progressElem2(txt){
  var bw = 0;
  var elm=document.getElementById("progress");
  elm.innerHTML = txt+"...";

  if(window.innerWidth) bw=window.innerWidth;
  else if(document.documentElement &&  document.documentElement.clientWidth) bw=document.documentElement.clientWidth
  else if(document.body) bw=document.body.clientWidth;
  var currentOffsetLeft = document.documentElement.scrollLeft || document.body.scrollLeft; // body for Safari
  var desiredOffsetLeft = (bw - elm.offsetWidth)/2+currentOffsetLeft;
  if (desiredOffsetLeft != parseInt(elm.style.left, 10)) elm.style.left= desiredOffsetLeft + 'px';
  if (elm) elm.className='';
}

function checkLogin(response){
  if(typeof(response)=="object") return true;
  var htmlExp = new RegExp(/<[a-zA-Z][^>]*\son\w+=(\w+|'[^']*'|"[^"]*")[^>]*>/);
  if(htmlExp.test(response)) return true;
  if(typeof(response) == "string" && response.search(/loginform/)!=-1){
    window.location = "/login.jsp";
    return false;
  }

  var idx = response.indexOf("login") ;
  if(idx != -1) {
    var obj = eval ('('+response+')');
    if(typeof(obj)!="undefined" && typeof(obj.login)!="undefined"){
      //alert("Session Expired");
      window.location = "/login.jsp";
      return false;
    }
   }
  return true;
}

/*
 * Trim function = Removes all occurrences of white space characters from the beginning and end of this instance.
 */
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,"");
};




