String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

Date.prototype.getMonthName = function() {
   return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][this.getMonth()]; 
}

function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
   	window.onload = function() { 
      	if (oldonload) { 
        		oldonload(); 
      	} 
      	func(); 
    	} 
  	} 
}

function addUnloadEvent(func) {
	var blnFuncAdded = false;
	if (navigator.userAgent.toLowerCase().indexOf('safari')!=-1) {
		var bodyElt;
		bodyElt = document.getElementsByTagName('body')[0];
		if (bodyElt) {
			bodyElt.addEventListener("unload", func, false);
			blnFuncAdded = true;
		}
	} 
	
	if (!blnFuncAdded) {
   	var oldUnload = window.onunload;
   	if (typeof window.onunload != 'function') {
   		window.onunload = func;
   	} else {
   		window.onunload = function() {
   			if (oldUnload) {
   				oldUnload();
   			}
   			func();
   		}
   	}
	}
}

function addBlurEvent(func) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		var oldFocusOut = document.onfocusout;
		if (typeof document.onfocusout != 'function') {
			document.onfocusout = func;
		} else {
			document.onfocusout = function() {
				if (oldFocusOut) {
					oldFocusOut();
				}
				func();
			}
		}
	} else {
		var oldBlur = window.onblur;
		if (typeof window.onblur != 'function') {
			window.onblur = func;
		} else {
			window.onblur = function() {
				if (oldBlur) {
					oldBlur();
				}
				func();
			}
		}
	}
}

function addFocusEvent(func) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		var oldFocusIn = document.onfocusin;
		if (typeof document.onfocusin != 'function') {
			document.onfocusin = func;
		} else {
			document.onfocusin = function() {
				if (oldFocusIn) {
					oldFocusIn();
				}
				func();
			}
		}
	} else {
   	var oldFocus = window.onfocus;
   	if (typeof window.onfocus != 'function') {
   		window.onfocus = func;
   	} else {
   		window.onfocus = function() {
   			if (oldFocus) {
   				oldFocus();
   			}
   			func();
   		}
   	}
	}
}

function AddLeadingZero(val) {
	var intVal = parseInt(val);
	if (intVal < 10) return "0" + intVal;
	return intVal;
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function GetWindowSize() {
	if(typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
    	myWidth = window.innerWidth;
    	myHeight = window.innerHeight;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    	//IE 4 compatible
    	myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
  	}
	
	return [myWidth, myHeight];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getAbsoluteTop(obj) {
	var top = 0;
	var o = obj;
	while (o!=null) {
		top += o.offsetTop;
		o = o.offsetParent;
	}
	return top;
}

function getAbsoluteLeft(obj) {
	var left = 0;
	var o = obj;
	while (o!=null) {
		left += o.offsetLeft;
		o = o.offsetParent;
	}
	return left;
}

function CreateModalLayer(layerID) {
	var modalLayer = document.createElement("div");
   modalLayer.id = layerID;
   modalLayer.className = "ModalLayer";
   modalLayer.style.zIndex = 50;
   var arrPageSize = getPageSizeWithScroll();
   var layerHeight = arrPageSize[1];
   if ((window.innerHeight) && (layerHeight < window.innerHeight)) layerHeight = window.innerHeight;
   if ((document.documentElement) && (document.documentElement.clientHeight) && (layerHeight < document.documentElement.clientHeight)) layerHeight = document.documentElement.clientHeight;
   modalLayer.style.height = layerHeight + "px";
   
   document.body.appendChild(modalLayer);
	return modalLayer;
}

function PopupMessage(strMsg, popupID) {
	HidePdfFrame();
	var popupLayer = document.createElement("div");
	popupLayer.id = popupID;
	popupLayer.className = "PopupMessage";
	popupLayer.style.zIndex = 99;
	popupLayer.innerHTML = strMsg;
	
	document.body.appendChild(popupLayer);
	
	var puHeight = popupLayer.offsetHeight;
	var puWidth = popupLayer.offsetWidth;
	var winSize = GetWindowSize();
   var winScroll = getScrollXY();
   var newTop = ((winSize[1]-parseInt(puHeight))/2 + winScroll[1]) + "px";
   var newLeft = ((winSize[0]-parseInt(puWidth))/2 + winScroll[0]) + "px";
   popupLayer.style.top = newTop;
   popupLayer.style.left = newLeft;
	
	return popupLayer;
}

function ShowPdfFrame() {
	if ((document.getElementById('pdf_frame')) && (document.getElementById('pdf_frame').style.display=="none") && (document.getElementById('timeoutFrame'))) {
		document.getElementById('pdf_frame').style.height = document.getElementById('timeoutFrame').style.height;
		document.getElementById('timeoutFrame').style.display = "none";
		document.getElementById('pdf_frame').style.display = "";
		// Firefox loses the pdf for some reason
   	if (navigator.userAgent.indexOf("Firefox") >= 0) {
			var blnFFReset = true;
			if (arguments.length > 0) blnFFReset = arguments[0];
   		if (blnFFReset) {
				if ((typeof progGen == "boolean") && (progGen)) {
					window.location.replace(unescape(window.location) + "&pdfRefresh=true");
				} else {
					window.location.replace(unescape(window.location.pathname) + "?pdfRefresh=true");
				}
			}
   	}
	}
}

function HidePdfFrame() {
	if ((document.getElementById('pdf_frame')) && (document.getElementById('timeoutFrame'))) {
		document.getElementById('timeoutFrame').style.height = document.getElementById('pdf_frame').style.height;
		document.getElementById('pdf_frame').style.display = "none";
		document.getElementById('timeoutFrame').style.display = "";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function CalculateTime(timeValue) {
	var tmVal = parseInt(timeValue);
	var intTemp = tmVal % 3600;
	var Hours = (tmVal-intTemp)/3600;
	tmVal = tmVal - (Hours*3600);
	intTemp = tmVal % 60;
	var Minutes = (tmVal-intTemp)/60;
	var Seconds = tmVal - (Minutes*60);
	//if (Hours > 0)
	return Hours + ":" + AddLeadingZero(Minutes) + ":" + AddLeadingZero(Seconds);
	//return Minutes + ":" + AddLeadingZero(Seconds);
}

function ResizePDFFrame() {
	if ((document.getElementById('pdf_header')) && (document.getElementById('pdf_footer')) && (document.getElementById('pdf_frame'))) {
   	var windowSize = GetWindowSize();
   	var windowHeight = parseInt(windowSize[1]);
   	iframeHeight = windowHeight - document.getElementById('pdf_header').offsetHeight - document.getElementById('pdf_footer').offsetHeight - 20;
   	if (iframeHeight < 150) iframeHeight = 150;
   	document.getElementById('pdf_frame').style.height = iframeHeight + "px";
	}
}

function ResizeActionFrame() {
	if ((document.getElementById('pdf_header')) && (document.getElementById('pdf_footer')) &&
	    (document.getElementById('CodeFrame')) && (document.getElementById('ActionFrame'))) {
   	var windowSize = GetWindowSize();
   	var windowHeight = parseInt(windowSize[1]);
   	iframeHeight = windowHeight - document.getElementById('pdf_header').offsetHeight - document.getElementById('CodeFrame').offsetHeight - document.getElementById('pdf_footer').offsetHeight - 20;
   	if (iframeHeight < 150) iframeHeight = 150;
   	document.getElementById('ActionFrame').style.height = iframeHeight + "px";
	}
}

function GetGeneratedPageName(pageURL) {
	var PageName = "";
	if (pageURL.match(/.*?[\&|\?]page=[^\&]*?#.*/i))
		PageName = pageURL.replace(/.*?[\&|\?]page=([^\&]*?)#.*/i, "$1");	
	else if (pageURL.match(/.*?[\&|\?]page=[^\&]*?#$/i))
		PageName = pageURL.replace(/.*?[\&|\?]page=([^\&]*?)#$/i, "$1");
	else if (pageURL.match(/.*?[\&|\?]page=.*?\&.*/i)) 
		PageName = pageURL.replace(/.*?[\&|\?]page=(.*?)\&.*/i, "$1");
	else if (pageURL.match(/.*?[\&|\?]page=.*?$/i)) 
		PageName = pageURL.replace(/.*?[\&|\?]page=(.*?)$/i, "$1");
	else
		PageName="index.php";
	
	return PageName;
}