// Set equal column heights, animation available
function P7_colH2(){ 
 var i,oh,h=0,tg,el,np,dA=document.p7eqc,an=document.p7eqa;if(dA&&dA.length){
 for(i=1;i<dA.length;i+=2){dA[i+1].style.paddingBottom='';}for(i=1;i<dA.length;i+=2){
 oh=dA[i].offsetHeight;h=(oh>h)?oh:h;}for(i=1;i<dA.length;i+=2){oh=dA[i].offsetHeight;
 if(oh<h){np=h-oh;if(!an&&dA[0]==1){P7_eqA2(dA[i+1].id,0,np);}else{
 dA[i+1].style.paddingBottom=np+"px";}}}document.p7eqa=1;
 document.p7eqth=document.body.offsetHeight;
 document.p7eqtw=document.body.offsetWidth;}
}
function P7_eqT2(){ 
 if(document.p7eqth!=document.body.offsetHeight||document.p7eqtw!=document.body.offsetWidth){P7_colH2();}
}
function P7_equalCols2(){ 
 var c,e,el;if(document.getElementById){document.p7eqc=new Array();
 document.p7eqc[0]=arguments[0];for(i=1;i<arguments.length;i+=2){el=null;
 c=document.getElementById(arguments[i]);if(c){e=c.getElementsByTagName(arguments[i+1]);
 if(e){el=e[e.length-1];if(!el.id){el.id="p7eq"+i;}}}if(c&&el){
 document.p7eqc[document.p7eqc.length]=c;document.p7eqc[document.p7eqc.length]=el}}
 setInterval("P7_eqT2()",10);}
}
function P7_eqA2(el,p,pt){ 
 var sp=10,inc=20,g=document.getElementById(el);np=(p>=pt)?pt:p;
 g.style.paddingBottom=np+"px";if(np<pt){np+=inc;
 setTimeout("P7_eqA2('"+el+"',"+np+","+pt+")",sp);}
}
//end column height set


//Begin Tab control scripts//
//To use multiple menus, create several arrays, example: new Array("tablink", "anotherTablink", "etc")
//Set the id tab a link (without a number at the end)
browserName=navigator.appName; 
var tablink_idname = new Array("tablink","tab2link","tab3link")
//the id name of tab contentarea (without a number at the end)
var tabcontent_idname = new Array("tabcontent","tab2content","tab3content") 
//number of tabs
var tabcount = new Array("2","2","2")
//the Tab that will be active when page loads 
var loadtabs = new Array("1","1","1")  
function tabs(menunr, active){
	menunr = menunr-1;
	for (i=1; i <= tabcount[menunr]; i++){
	document.getElementById(tablink_idname[menunr]+i).className='tab'+i;
	document.getElementById(tabcontent_idname[menunr]+i).style.display = 'none';
		if (browserName != "Microsoft Internet Explorer") {
	document.getElementById(tablink_idname[menunr]+i).childNodes[2].firstChild.className='iconOff';
	var cicon = document.getElementById(tablink_idname[menunr]+i).childNodes[2].firstChild.getAttribute("src");
	var ccicon = cicon.replace(/_active.gif/,"_inactive.gif");
	document.getElementById(tablink_idname[menunr]+i).childNodes[2].firstChild.setAttribute("src", ccicon);
	} else {
	document.getElementById(tablink_idname[menunr]+i).firstChild.nextSibling.firstChild.className='iconOff'; 
	var cicon = document.getElementById(tablink_idname[menunr]+i).firstChild.nextSibling.firstChild.getAttribute("src");
	var ccicon = cicon.replace(/_active.gif/,"_inactive.gif");
	var cicon = document.getElementById(tablink_idname[menunr]+i).firstChild.nextSibling.firstChild.setAttribute("src", ccicon);	
	}
}
	document.getElementById(tablink_idname[menunr]+active).className='tab'+active+' tabactive';
 	document.getElementById(tabcontent_idname[menunr]+active).style.display = 'block';
			if (browserName != "Microsoft Internet Explorer") {
	document.getElementById(tablink_idname[menunr]+active).childNodes[2].firstChild.className='iconOn';
	var iicon = document.getElementById(tablink_idname[menunr]+active).childNodes[2].firstChild.getAttribute("src");
	var iiicon = iicon.replace(/_inactive.gif/,"_active.gif");
	document.getElementById(tablink_idname[menunr]+active).childNodes[2].firstChild.setAttribute("src", iiicon);
	} else {
	document.getElementById(tablink_idname[menunr]+active).firstChild.nextSibling.firstChild.className='iconOn';
	var iicon = document.getElementById(tablink_idname[menunr]+active).firstChild.nextSibling.firstChild.getAttribute("src");
	var iiicon = iicon.replace(/_inactive.gif/,"_active.gif");
	document.getElementById(tablink_idname[menunr]+active).firstChild.nextSibling.firstChild.setAttribute("src",iiicon);	
} 
	}
window.onload=function(){
var menucount=loadtabs.length; var a = 0; var b = 1; do {tabs(b, loadtabs[a]);  a++; b++;}while (b<=menucount);

}
//End Tab control scripts//

//Forum scripts//
var iframeids=["forum","forumList"]
var iframehide="yes"
var getFFVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
var FFextraHeight=parseFloat(getFFVersion)>=0.1? 16 : 0 //extra height in px to add to iframe in FireFox 1.0+ browsers

function resizeCaller() {
var dyniframe=new Array()
for (i=0; i<iframeids.length; i++){
if (document.getElementById)
resizeIframe(iframeids[i])
//reveal iframe for lower end browsers? (see var above):
if ((document.all || document.getElementById) && iframehide=="no"){
var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
tempobj.style.display="block"
}
}
}

function resizeIframe(frameid){
var currentfr=document.getElementById(frameid)
if (currentfr && !window.opera){
currentfr.style.display="block"
if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
currentfr.height = currentfr.Document.body.scrollHeight;
if (currentfr.addEventListener)
currentfr.addEventListener("load", readjustIframe, false)
else if (currentfr.attachEvent){
currentfr.detachEvent("onload", readjustIframe) // Bug fix line
currentfr.attachEvent("onload", readjustIframe)
}
}
}

function readjustIframe(loadevt) {
var crossevt=(window.event)? event : loadevt
var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
if (iframeroot)
resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url){
if (document.getElementById)
document.getElementById(iframeid).src=url
}

if (window.addEventListener)
window.addEventListener("load", resizeCaller, false)
else if (window.attachEvent)
window.attachEvent("onload", resizeCaller)
else
window.onload=resizeCaller

//end forum scripts//

function loadAsync(url, elementid)
{
	new AsyncRequest().execute(url, elementid);
}

/** This method sends an asynchronous request to the server for a list of counties
	in the state selected.
	It creates the following global variables: _countyRequest, _countyField
	The global variables are used by actuallyUpdateCountyOptions() when the asynchronous
	request is finished and the data is ready to be parsed.
*/
function updateCountyOptions(stateField, countyField, context)
{
	var state = stateField.value;
	_countyRequest = getXMLHttpRequest();
	if (_countyRequest)
	{
		_countyField = countyField;
		_countyRequest.onreadystatechange=actuallyUpdateCountyOptions;
		_countyRequest.open("GET", context+"/county.do?state="+state);
		_countyRequest.send(null);
	}
}

// Method definition for AsyncRequest.updateElement(), which is invoked by the XMLHttpRequest 
// when the state of the request has changed.
// This method replaces the innerHTML of the element specified in the AsyncRequest constructor.
function asyncRequestUpdateElement()
{
	if(this.asyncRequest.readyState==4) // when the responseText is ready
	{
		this.elementId.innerHTML=this.asyncRequest.responseText;
	}
}

/**
	Object constructor for an asynchronous AJAX call to the url provided.
	Use this to load content into an element asynchronously.
*/
function AsyncRequest(url, elementid)
{
	this.url = url;
	this.elementid = document.getElementById(elementid);
	this.asyncRequest = getXMLHttpRequest();
	this.updateElement = asyncRequestUpdateElement; // method declaration
	if (this.asyncRequest)
	{
		this.asyncRequest.onreadystatechange=function()
		{
			if(this.readyState==4) // when the responseText is ready
			{
				alert(this.responseText);
				this.elementId.innerHTML=this.asyncRequest.responseText;
			}
		}
		this.asyncRequest.open("GET", url);
		this.asyncRequest.send(null);
	}
}

/** This method is invoked by XMLHttpRequest when the response is ready to be parsed.<b> 
	This method requires the following global variables: _countyRequest, _countyField
*/
function actuallyUpdateCountyOptions()
{
	if(_countyRequest.readyState==4)
	{
		// Get the list of counties for the state.
		var counties = _countyRequest.responseText.split(",");
		
		// remove all current options from the County field.
		while(_countyField.options.length>0)
			_countyField.options[0]= null;
		
		// add each county as an option to the County field.
		for(i=0; i<counties.length; i++)
		{
			var county = counties[i];
			var countyId = county.split(":")[0];
			var countyKey = county.split(":")[1];
			if(county.length==0)
				continue;
			var option = document.createElement("OPTION");
			option.value = countyId;
			option.text = countyKey;
			_countyField.options.add(option);
		} 
	}
}

/** Get the AJAX XMLHttpRequest in a platform independent manner. */
function getXMLHttpRequest()
{
	if (window.XMLHttpRequest)
	   return new XMLHttpRequest();
	if(window.ActiveXObject)
		return new ActiveXObject("MSXML2.XMLHTTP.3.0");
	return;
}

/*
	This function removes selected options from availableCountyElement (a select object), and adds new options
	to the selectedCountyElement (a select object).  The new options are combined with the currently selected state
	(from stateElement) so that the user knows which state each selected county resides in.
*/
function addSelectedCounties(availableCountyElement, selectedCountyElement, stateElement)
{
	var i;
	for(i=0; i<availableCountyElement.options.length; i++)
	{
		var option = availableCountyElement.options[i];
		if(option.selected)
		{
			var newoption = document.createElement("OPTION");
			newoption.value = option.value;
			newoption.text = option.text+" - "+stateElement.value;
			selectedCountyElement.options.add(newoption);
			availableCountyElement.options[i--] = null; // remove the option and decrement the index because we reduced the number of options.
		}
	}
}

function removeSelectedCounties(availableCountyElement, selectedCountyElement, stateElement)
{
	var i;
	var availableOptionAdded = false;
	for(i=0; i<selectedCountyElement.options.length; i++)
	{
		var option = selectedCountyElement.options[i];
		if(option.selected)
		{
			var optionState = option.text.substring(option.text.length - 2);
			if(optionState == stateElement.value)
			{
				var newoption = document.createElement("OPTION");
				newoption.value = option.value;
				newoption.text = option.text.substring(0, option.text.indexOf(" - ")); // remove the state from the option
				availableCountyElement.options.add(newoption);
				availableOptionAdded = true;
			}
			selectedCountyElement.options[i--] = null; // remove the option and decrement the index because we reduced the number of options.
		}
	}
	if(availableOptionAdded)
		sortOptions(availableCountyElement);
}

/**
	Used by sortOptions() to compare two OptionInfo objects by the 'text' property in a case-insensitive manner.
*/
function compareOptionInfo(opt1, opt2)
{
	return opt1.text.toLowerCase() < opt2.text.toLowerCase() ? -1 : opt1.text.toLowerCase() > opt2.text.toLowerCase() ? 1 : 0;
}

/**
	Called by removeSelectedCounties() to sort the options in the availableCounties select box.
*/
function sortOptions(selectElement)
{
	var optionInfo = new Array(selectElement.options.length);
	var i;
	for(i=0; i<selectElement.options.length; i++)
	{
		optionInfo[i] = new Object();
		optionInfo[i].text = selectElement.options[i].text;
		optionInfo[i].value = selectElement.options[i].value
	}
	optionInfo.sort(compareOptionInfo);
	for(i=0; i<optionInfo.length; i++)
	{
		selectElement.options[i].text = optionInfo[i].text;
		selectElement.options[i].value = optionInfo[i].value;
	}
}

function removeAllCounties(selectedCountyElement, availableCountyElement, stateElement)
{
	var i;
	
	// Select all selectedCounty options
	for(i=0; i<selectedCountyElement.options.length; i++)
		selectedCountyElement.options[i].selected = true;

	// Remove selected options
	removeSelectedCounties(availableCountyElement, selectedCountyElement, stateElement);
}

function addAllCounties(selectedCountyElement, availableCountyElement, stateElement)
{
	var i;
	
	// Select all available options
	for(i=0; i<availableCountyElement.options.length; i++)
		availableCountyElement.options[i].selected = true;

	// Add selected options
	addSelectedCounties(availableCountyElement, selectedCountyElement, stateElement);
}

/** This method sends an asynchronous request to the server for a list of counties
	in the state selected.
	It creates the following global variables: _countyRequest, _availableCountyElement, _selectedCountyElement
	The global variables are used by actuallyUpdateCountyOptions() when the asynchronous
	request is finished and the data is ready to be parsed.
*/
function updateAvailableCounties(stateElement, availableCountyElement, selectedCountyElement, context)
{
	var state = stateElement.value;
	_countyRequest = getXMLHttpRequest();
	if (_countyRequest)
	{
		_availableCountyElement = availableCountyElement; // declare & define global variable
		_selectedCountyElement = selectedCountyElement; // declare & define global variable
		_stateElement = stateElement; // declare & define global variable
		_countyRequest.onreadystatechange=actuallyUpdateAvailableCounties;
		var url = context+"/county.do?state="+state;
		_countyRequest.open("GET", url);
		_countyRequest.send(null);
	}
}

/** This method is invoked by XMLHttpRequest when the response is ready to be parsed.<b> 
	This method requires the following global variables: _countyRequest, _availableCountyElement
*/
function actuallyUpdateAvailableCounties()
{
	if(_countyRequest.readyState==4)
	{
		// Get the list of counties for the state.
		var counties = _countyRequest.responseText.split(",");
		var i;
		
		// remove all current options from the County field.
		while(_availableCountyElement.options.length>0)
			_availableCountyElement.options[0] = null;
		
		// add each county as an option to the County field.
		for(i=0; i<counties.length; i++)
		{
			var county = counties[i];
			var countyId = county.split(":")[0];
			var countyKey = county.split(":")[1];
			if(county.length==0)
				continue;
			var selectedCountyText = countyKey + " - " + _stateElement.value;
			if(optionExists(_selectedCountyElement, selectedCountyText)==false)
			{
				var option = document.createElement("OPTION");
				option.value = countyId;
				option.text = countyKey;
				_availableCountyElement.options.add(option);
			}
		}
		_availableCountyElement = undefined; // delete global variable
		_selectedCountyElement = undefined;  // delete global variable
		_stateElement = undefined;  // delete global variable
	}
}

/**
	This method selects all options within the select box provided, and fires the select box's
	onchange event handler(s).
	@author Stephen Wick
	@since August 24, 2009 (FMCCONX-171)
*/
function selectAllOptions(selectbox)
{
	for(var i=0; i<selectbox.options.length; i++)
		selectbox.options[i].selected=1;
	selectbox.fireEvent("onchange");	
}

/**
	This method deselects all options within the select box provided, and fires the select box's
	onchange event handler(s).
	@author Stephen Wick
	@since August 24, 2009 (FMCCONX-171)
*/
function deselectAllOptions(selectbox)
{
	for(var i=0; i<selectbox.options.length; i++)
		selectbox.options[i].selected=0;
	selectbox.fireEvent("onchange");
}

/**
	This method returns true if the selectbox has an 'option' with the 'text' property set to the optionText provided.<b> 
	This method is used by actuallyUpdateAvailableCounties().
*/
function optionExists(selectbox, optionText)
{
	var i;
	for(i=0; i<selectbox.options.length; i++)
		if(selectbox.options[i].text == optionText)
			return true;
	return false;
}

/**
	This method returns the index of the 'option' with the 'value' property set to the optionValue specified.
	Returns the index of the option (0-infinity), or -1 if the option does not exist.
	@author Stephen Wick
	@since August 24, 2009 (FMCCONX-171)
*/
function getOptionIndexByValue(selectbox, optionValue)
{
	var i;
	for(i=0; i<selectbox.options.length; i++)
		if(selectbox.options[i].value == optionValue)
			return i;
	return -1;
}

/**
	This method returns the 'option' with the 'value' property set to the optionValue specified.
	Returns the option, or null if the option does not exist.
	@author Stephen Wick
	@since August 24, 2009 (FMCCONX-171)
*/
function getOptionByValue(selectbox, optionValue)
{
	var i;
	for(i=0; i<selectbox.options.length; i++)
		if(selectbox.options[i].value == optionValue)
			return selectbox.options[i];
	return null;
}

/** Custom Object: User
	Example: var user = new User("John", "Doe", "NK_USER", new Array("ADMIN","SWITCH_USER"));
	Since: August 19, 2009 (FMCCONX-171)
	Author: Stephen M. Wick
*/
function User(firstName, lastName, userType, roles)
{
	this.firstName = firstName;
	this.lastName = lastName;
	this.userType = userType;
	this.roles = roles;
}
/** Determine whether or not the User has the role specified. */
User.prototype.hasRole=function(roleKey)
{
	if(this.roles)
	{
		for(var i=0; i<this.roles.length; i++)
		{
			if(this.roles[i]==roleKey)
				return true;
		}
	}
	return false;
}
/** Get the User's full name (ie: "John Doe"). */
User.prototype.getFullName=function()
{
	var fullName = this.firstName;
	if(this.lastName)
	{
		fullName+=" "+this.lastName;
	}
	return fullName;
}