// this page should never load inside of another frame
if (top.location != self.location) {
	top.location = self.location;
}

//is this in Prototype already?
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

common_class = function() {

}

//cursor focus on a from element
common_class.prototype.focus = function(fld) {
	$(fld).focus();
}

//logout
common_class.prototype.logout = function(logoutURL) {
	if(confirm('Are you sure you want to logout?')) {
		window.location.href = logoutURL;
	}
}

//common popup window
common_class.prototype.openWin = function(url,w,h) {
	if(!w) { w = "800"; }
	if(!h) { h = "600"; }
	var newWin  = window.open(url,"commonWin","width="+w+",height="+h+",top=10,left=10,resizable=1,scrollbars=yes");
}

//primary nav hovers to kickstart IE
common_class.prototype.startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("top");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace("over", "");
				}
			}
		}
	}
}



//generic "Are you sure you want to do that?"
common_class.prototype.confirm = function(doWhat,showPermAlert) {
	
	var msg = '';
	msg += 'Are you sure you want to '+doWhat+ '?';
	if(showPermAlert) {
		msg += '\nThere is no undo.';
	}

	if(confirm(msg)) {
		return true;
	} else {
		return false;
	}
}

//for sample program tables
common_class.prototype.zebraStripe = function() {

}

common_class.prototype._zebraStripe = function() {
	var rows = $('main').getElementsBySelector('table.sample tr');
	var len = rows.length;
	for (var r=0; r<len; r++) {
		if (r % 2 == 0) {
			Element.addClassName(rows[r], 'even'); //grey bg
		} else {
			Element.addClassName(rows[r], 'odd'); //currently no style
		}
	}
}

common_class.prototype.zebraStripeAddress = function(col) {
	var rows = $('main').getElementsBySelector('table.col1 tbody tr');
	var len = rows.length;
	for (var r=0; r<len; r++) {
		if (r % 2 == 0) {
			Element.addClassName(rows[r], 'even'); //grey bg
		} else {
			Element.addClassName(rows[r], 'odd'); //currently no style
		}
	}
	var rows = $('main').getElementsBySelector('table.col2 tbody tr');
	var len = rows.length;
	for (var r=0; r<len; r++) {
		if (r % 2 == 0) {
			Element.addClassName(rows[r], 'even'); //grey bg
		} else {
			Element.addClassName(rows[r], 'odd'); //currently no style
		}
	}
}

// init 
addLoadEvent(common = new common_class());
addLoadEvent(common.startList);


function toggleOtherField(spanName,theSelectField,inputName,theValue) {
	// alert(spanName + " " + fieldName + " " + theValue);
	
	var inputSpan = $(spanName);
	var inputField = $(inputName);
	
	if (isSelectFieldValueThis(theSelectField,theValue)) {		
		inputSpan.show();		
		inputField.focus();
	} else {
		inputField.value = '';
		inputSpan.hide();
	}
}

function isSelectFieldValueThis(theSelectField, theValue) {	
	//var selectVal = theSelectField.options[theSelectField.selectedIndex].value;
	
	return getSelectFieldValue(theSelectField) == theValue; 
}

function getSelectFieldValue (theSelectField) {
	return theSelectField.options[theSelectField.selectedIndex].value;
}

function timeoutSession() {
	var origTimeout = 1;
	var timeoutInMilliseconds = origTimeout*60*1000;
	return setTimeout("needToConfirm=0;timeoutLogout();window.location = timeoutUrl;",timeoutInMilliseconds);
}

function trackPageOpenTime() {
	var origTimeout = 29;
	var warningInMilliseconds = (origTimeout*60*1000);
	return setTimeout("$('timeoutWarningBanner').show();trackTimeoutEvents();timeoutSessionId=timeoutSession();",warningInMilliseconds);
}

function trackTimeoutEvents() {
	Event.observe(document,'click',function(event){
		
		clearTimeout(timeoutSessionId);
		
		clearTimeout(initialTimeoutId);
		
		initialTimeoutId = trackPageOpenTime();
		
		clearTimeoutViaAjax();
		$('timeoutWarningBanner').hide();
		Event.stopObserving(document,'click');
	});
	
}

function clearTimeoutViaAjax() {
	new Ajax.Request(timeoutUrl, {
		  onSuccess: function(response) {

		  }
		  
		});

}
function timeoutLogout() {
	new Ajax.Request(logoutUrl, {
		  onSuccess: function(response) {
		    
		  }
		  
		});

}
