// Author: Alex Glass
// Date: 6/14/06

if(window.location.href.search("Login\.aspx$") == -1) // if not the login page
	setTimeout("session_expire()", 15 * 60000); // keep session alive by reloading the page every 15 mins

function session_expire() {
	window.location.href=window.location.href;
}

// -------------------------------------------- MISC FUNCTIONS --------------------------------------------

function trim(value) {	
	var rgx1 = /^\s+/;
	var rgx2 = /\s+$/;
	var temp = value;
	temp = temp.replace(rgx1, ""); 
	temp = temp.replace(rgx2, "");
	return temp;
}

String.prototype.trim = function trimString() {	
	var rgx1 = /^\s+/;
	var rgx2 = /\s+$/;
	var temp = this;
	temp = temp.replace(rgx1, ""); 
	temp = temp.replace(rgx2, "");
	return temp;
}

function EndsWith(subject, search) {
	if(subject.length < search.length) return false;
	var pos = subject.length - search.length;
	return subject.substring(pos) == search
}

function isEmpty(s) {
	if(s == null)
		return true;
	else
		return trim(s).length == 0;
}

function isDate(val) {
	var rgx = /^(\d{1,2})\/(\d{1,2})\/(\d{2}|\d{4})$/i;
	
	matches = rgx.exec(val);
	if(matches) {
		if( (matches[1] >= 0 && matches[1] <= 12) && (matches[2] >= 1 && matches[2] <= 31)) return true;		
	} 
	
	return false;
}

function isTime(val) {
	var rgx = /^\d{1,2}(:\d{2})? ?(AM|PM)$/i;
	return rgx.test(val);
}

function isPhoneNumber(val) {	
	var rgx = /^[-0-9 .+]*$/;
	return rgx.test(val);
}

function isEmail(email) {	
	var regex = /^[a-z0-9]\w*([-.]\w+)*@[a-z0-9]\w*([-.]\w+)*\.([a-z]{2}|com|net|org|edu|gov|mil)$/i;
	return regex.test(email);
}

function isNumeric(val) {
	var regex = /^[0-9 ()-.]*$/;
	return regex.test(val);
}

function contains(haystack, needle) {
	if(isEmpty(haystack)) return false;
	return haystack.indexOf(needle) != -1;
}

function dump(itm) {
	obj = eval(itm);
	style = "<style type='text/css'>th, td { font-size: 10px; text-align: left; vertical-align: top; font-family: tahoma; } pre { font-family: \"Courier New\", Courier, mono; font-size: 10px; }</style>";
	msg = "<html><head>"+style+"<title>vardump("+itm+")</title></head>";
	msg += "<body bgcolor='#EEEEEE'><h1>*** vardump ***</h1><table border='0' bgcolor='white' cellpadding='0' cellspacing='2'>";	
	for (var key in obj)
	{
		msg += "<tr><th align='left'>"+key+"</th><td><pre>"+obj[key]+"<pre></td></tr>";
	}	
	msg += "</table></body></html>";
	var win = window.open('about:blank');
	win.document.write(msg);
}

function format_seconds(seconds) {
	// return time in hh:mm:ss format
	
	var hours = Math.floor(seconds / 3600);
	var minutes = Math.floor((seconds % 3600) / 60);
	var seconds = seconds % 60;

	if(seconds < 10) seconds = '0'+seconds;
	if(minutes < 10) minutes = '0'+minutes;
	if(hours<10) hours = '0'+hours;

	// only display hours if they are specified	
	if(hours>0)	
		return hours + ":" + minutes + ":" + seconds;
	else
		return minutes+":"+seconds;	
}

function today() {
	var dt=new Date();
	var d,m,y;	
	d=dt.getDate();
	m=dt.getMonth()+1;
	y=dt.getFullYear();
	if(d<=9) d="0"+d;
	if(m<=9) m="0"+m;
	y = y % 100;
	if(y<=9) y="0"+y;
	return m + "-" + d + "-" + y;	
}

// ---------------------------------------------- FORM PROCESSING -----------------------------------------------

// load form objects using their name property
function loadForm(name, value) {
	var f = document.forms[0];
	f[name].value = value;
}

// requires all elements to have the same name followed by their respective value 
// (ie. rating0 rating1 rating2)
function loadFormRadio(name, value) {
	var f = document.forms[0];

	var elem = f[name];
	for(i=0; i<elem.length; i++)
	{
		if(elem[i].value == value) {
			elem[i].checked = true;
			return;
		}
	}	
}

function loadFormCheck(name, value) {
	var f = document.forms[0];
	var elem = f[name];
	elem.checked = value;
}
		
function submitForm(action) {
	var f = document.forms[0];
	f._action.value = action;
	try {
		f.submit();
	} catch(ex) {		
		msgboxError("Unable to post form: " + ex.message);
	}	
}

// clear all items from a select control
function listboxClear(id) {
	var elem = document.getElementById(id);
	
	while(elem.firstChild)
		elem.removeChild(elem.firstChild);
}

function listboxSelection(list) {
	var select = document.getElementById(list);
	
	for(var i=0; i<select.childNodes.length; i++)
		if(select.childNodes[i].selected)
			return select.childNodes[i];
}

function isChecked(elem) {
	return document.getElementById(elem).checked;
}

// ----------------------------------------------- SHARED CONSTANTS --------------------------------------------------
var WARNING_IMG = "ui/images/warning.gif";
var ERROR_IMG = "ui/images/error.gif";
var QUESTION_IMG = "ui/images/question.gif";
var INFO_IMG = "ui/images/info.gif";

// ------------------------------------------------- MESSSAGE BOX ----------------------------------------------------
function msgboxError(message, onOK) {
	hideRow("trText");
	loadElement("btnPopup1", "OK");	
	showElement("btnPopup1");
	hideElement("btnPopup2");
	msgboxShow(message, ERROR_IMG, onOK);
}

function msgboxWarning(message, onOK, onCancel) {
	loadElement("btnPopup1", "OK");
	loadElement("btnPopup2", "Cancel");
	hideRow("trText");
	showElement("btnPopup1");
	showElement("btnPopup2");
	msgboxShow(message, WARNING_IMG, onOK, onCancel);
}

function msgboxInfo(message, onOK) {
	loadElement("btnPopup1", "OK");
	hideRow("trText");
	showElement("btnPopup1");
	hideElement("btnPopup2");
	msgboxShow(message, INFO_IMG, onOK);
}

function msgboxQuestion(question, onYes, onNo) {
	loadElement("btnPopup1", "Yes");
	loadElement("btnPopup2", "No");
	hideRow("trText");
	showElement("btnPopup1");
	showElement("btnPopup2");
	msgboxShow(question, QUESTION_IMG, onYes, onNo);
}

function msgboxPrompt(message, onOK, onCancel) {
	loadElement("btnPopup1", "OK");
	loadElement("btnPopup2", "Cancel");
	showRow("trText");	
	showElement("btnPopup1");
	showElement("btnPopup2");
	msgboxShow(message, INFO_IMG, onOK, onCancel);
	document.getElementById("txtMsgbox").focus();
}

function msgboxShow(message, image, btn1click, btn2click) {
	if(btn1click == null) btn1click = msgboxHide;
	if(btn2click == null) btn2click = msgboxHide;
	showElement("msgbox");
	setHTML("msgbox_text", message);
	setImage("msgbox_img", image);
	var button1 = document.getElementById("btnPopup1");
	var button2 = document.getElementById("btnPopup2");
	button1.onclick = btn1click;
	button2.onclick = btn2click;
}

function msgboxHide() {
	hideElement("msgbox");
	hideElement("btnPopup1");
	hideElement("btnPopup2");
}

// ------------------------------------------------------- FORM MESSAGE -------------------------------------------------------
function fmsgError(message, source) {
	setVisible("fmsg", true);
	setImage("fmsg_img", ERROR_IMG);
	setHTML("fmsg_text", message);	
}

// show a warning if an error is not visible
function fmsgWarning(message, source) {
	setVisible("fmsg", true);
	setImage("fmsg_img", WARNING_IMG);
	setHTML("fmsg_text", message);
}

// if an error message is passed, only hide the message if they match
function fmsgHide() {
	setVisible("fmsg", false);
}

// ------------------------------------------------------- DHTML STYLES -------------------------------------------------------

function hideElement(id) {
	var element = document.getElementById(id);
	element.style.visibility = "hidden";
}

function showElement(id) {
	var element = document.getElementById(id);
	element.style.visibility = "visible";
}

function loadElement(id, value) {
	var element = document.getElementById(id);
	element.value = value;
}

function setHTML(id, val) {
	var element = document.getElementById(id);
	element.innerHTML = val;
}

function setClass(id, cssclass) {
	var element = document.getElementById(id);
	element.className = cssclass;
}

function setImage(id, src) {
	var img = document.getElementById(id);
	img.src = src;
}

function setDisplay(id, val) {
	document.getElementById(id).style.display = val;
}

function setVisible(id, val) {
	document.getElementById(id).style.visibility = (val ? "visible" : "hidden");
}

function showRow(id) {
	var element = document.getElementById(id);
	if(Browser.isIE) {		
		element.style.display = "inline"; // for IE
	} else {
		element.style.display = "table-row";				
	}
}

function hideRow(id) {
	var element = document.getElementById(id);
	if(element == null) 
		alert("ERROR: element " + id + " was not found");
	else
		element.style.display = "none";
}

function hide(elem) {
	var e = document.getElementById(elem);
	if(e != null) e.style.display = "none";
}

function show(elem) {
	var e = document.getElementById(elem);
	if(e != null) e.style.display = "";
}

function visible(elem) {
	var e = document.getElementById(elem);
	if(e != null) return e.style.display == "";
}

// ---------------------------------------------------- TOOLBAR ----------------------------------------------------
var _lastClass;	

function tbMouseOver(o) {
	if(o.className == "selected") return;
	_lastClass = o.className;
	o.className="highlight";
}

function tbMouseOut(o) {	
	if(o.className == "selected") return;
	o.className=_lastClass; 
}	


// ---------------------------------------------------- FORM CHANGES ----------------------------------------------------
/// Prevent user from switching page without saving/cancelling changes
// requires hidden form element continuePage and events set on all controls
var _changes = false; // flag to mark changes 
var _cbNavigate = null; // callback function
var _formValue = "";

function navigate(url) {
	if(_cbNavigate == null) alert("ERROR: Navigate callback function not set");
	if(document.forms[0].continuePage == null) alert("ERROR: form element continuePage not found");
	
	if(_changes) {
		document.forms[0].continuePage.value = url;
		msgboxWarning("Save changes and continue?", _cbNavigate)
	} 
	else
		window.location.href = url;
}

function txtFocus(ctl) {
	_formValue = ctl.value;
}

function txtKeyup(ctl) {
	if(_changes) return;
	
	if(_formValue != ctl.value) setDirty();
}

function setDirty(val) {
	if(val != null && val == false)
		_changes = false;
	else
		_changes = true;
		
	if(_changes) {
		document.body.style.background = "#ffffee";
		showRow("trSave");
		showRow("trCancel");
	} else {
		document.body.style.background = "";
		hideRow("trSave");
		hideRow("trCancel");
	}
}

// --------------------------------------- MOUSE CAPTURE ---------------------------------------

var _IE;
function initMouseCapture() {
	_IE = document.all?true:false

	// If NS -- that is, !IE -- then set up for mouse capture
	if (!_IE) document.captureEvents(Event.MOUSEMOVE)

	// Set-up to use getMouseXY function onMouseMove
	document.onmousemove = getMouseXY;
}

// Temporary variables to hold mouse x-y pos.s
var _mouseX = 0
var _mouseY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (_IE) { // grab the x-y pos.s if browser is IE
    _mouseX = event.clientX + document.body.scrollLeft
    _mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    _mouseX = e.pageX
    _mouseY = e.pageY
  }  
  // catch possible negative values in NS4
  if (_mouseX < 0){_mouseX = 0}
  if (_mouseY < 0){_mouseY = 0}  
  return true
}

// Define and initialize Browser object singleton
var Browser = new function() {
  this.name = navigator.userAgent;
  this.isWinIE = this.isMacIE = false;
  this.isGecko = this.name.match(/Gecko\//);
  this.isSafari = this.name.match(/AppleWebKit/);
  this.isKHTML = this.isSafari || 
    navigator.appVersion.match(/Konqueror|KHTML/);
  this.isOpera = window.opera;
  this.hasNS = (document.documentElement) ? 
    document.documentElement.namespaceURI : null;
  if (document.all && !this.isGecko && !this.isSafari && !this.isOpera) {
    this.isWinIE = this.name.match(/Win/);
    this.isMacIE = this.name.match(/Mac/);
  }
  this.isIE = this.isWinIE || this.isMacIE;
}(); // end Browser