// color button text changes to when you mouse over it
var highlightColor = "660000";
var highlightBg = "#DDD9D2";

// captures current color of button text then sets text color to above specified highlight color
function buttonRollOn(which) {
	if (which.style) {
		colorBuffer = which.style.color;
		bgBuffer = which.style.background;
		which.style.backgroundColor = highlightBg;
		which.style.color = '#' + highlightColor;
	}
}

// sets color of button text back to original value
function buttonRollOff(which) {
	if (which.style) {
		which.style.color = colorBuffer;
		which.style.backgroundColor = bgBuffer;
	}
}

//javascript for check all/none
var checkState = false;
function checkAll(myCheckboxes){
	if(checkState == false){
		checkState = true;
	} else {
		checkState = false;
	}
	for(i = 0; i < document.forms[0].elements.length; i++) {
		elm = document.forms[0].elements[i]
		if (elm.type == 'checkbox') {
			if((elm.name == myCheckboxes)||(!myCheckboxes)){
				elm.checked = checkState;
			}
		}
	}
}




// confirmation utility
function confirmSubmit(msg) {
	var agree=confirm(msg);
	if (agree)
		return true;
	else
		return false;
}

//ROLL OVER FUNCTIONS FOR LIST TABLES
// holder for init state
var inited = 0;
rowColors = new Array();
tables = new Array();
var rowHighlightColor = "#FFFFAA";
var columnHighlightColor = "#EFEAE7";
var cellHighlightColor = "#EAEA9C";
var rowSelectedColor = "#FFFF66";
var columnSelectedColor = "#EDE0C2";

// sets the roll off and click off colors to the current background colors of the table, allows you to update style of table without having to worry about roll off color not matching
function initColors( originalCellOneColor, originalRowColor ){
	if (inited == 0){
		inited = 1;
		cellonedefColor = originalCellOneColor;
		rowdefaultColor = originalRowColor;
	}
}

// assigns titles to each cell of a table based on the inner text of column header
function init_titles(thisTable){
	this_row = thisTable.rows;
	titles = this_row[0].cells;
	var i = 1;
	while (this_row[i]){
		current_cells = this_row[i].cells;
		j = 0;
		while (current_cells[j]){
			if(titles[j].innerText != ' '){
				current_cells[j].title = titles[j].innerHTML;
			}
			j++;
		}
		i++;
	}
}

function listBoxClickToggle(whichElement, e) {
	//var x=thisTable.rows;
	var myRows = whichElement.parentNode.parentNode.rows;
	e = e || window.event;
	var tgt = e.target || e.srcElement;
	var clickedElement;
	if (tgt) {
		clickedElement = tgt.nodeName;
	}
	if ((clickedElement != 'A') && (clickedElement != 'INPUT') && (clickedElement != 'SELECT')) {
		if(whichElement.parentNode.rowIndex != 0){
			if(whichElement.parentNode.name != 'active') {  
				whichElement.parentNode.name='active';
				myCells = myRows[whichElement.parentNode.rowIndex].cells;
				i=0;
				while(myCells[i]) {
					myCells[i].style.borderRightColor = rowSelectedColor;
					myCells[i].style.borderLeftColor = rowSelectedColor;
					myCells[i].style.backgroundColor = rowSelectedColor;
					i++;
				}
			} else {
				whichElement.parentNode.name = '';  
				myCells = myRows[whichElement.parentNode.rowIndex].cells;
				i=0;
				while(myCells[i]) {
					if(myRows[0].cells[i].name != 'active'){
						myCells[i].style.borderRightColor = rowHighlightColor;
						myCells[i].style.borderLeftColor = rowHighlightColor;
						myCells[i].style.backgroundColor = rowHighlightColor;
					} else {
						//do column highlight for that cell
						myCells[i].style.borderRightColor = columnSelectedColor;
						myCells[i].style.borderLeftColor = columnSelectedColor;
						myCells[i].style.backgroundColor = columnSelectedColor;
					}
					i++;
				}
			}
		} else {
			if(whichElement.parentNode.cells[1].cellIndex == 1){
				i=1;
				if(whichElement.name != 'active') {
					whichElement.name = 'active';
					while(myRows[i]){
						myRow = myRows[i];
						if(myRow.name != 'active') {
							myRow.cells[whichElement.cellIndex].style.borderRightColor = columnSelectedColor;
							myRow.cells[whichElement.cellIndex].style.borderLeftColor = columnSelectedColor;
							myRow.cells[whichElement.cellIndex].style.backgroundColor = columnSelectedColor;
						}
						i++;
					}
				} else {
					whichElement.name = '';
					while(myRows[i]){
						myRow = myRows[i];
						if(myRow.name != 'active') {
							myRow.cells[whichElement.cellIndex].style.borderRightColor = columnHighlightColor;
							myRow.cells[whichElement.cellIndex].style.borderLeftColor = columnHighlightColor;
							myRow.cells[whichElement.cellIndex].style.backgroundColor = columnHighlightColor;
						}
						i++;
					}
				}
			}
		}
	}
}  
	
//add these to cells by doing a javascript add listener event thing
function listBoxRollOn(whichElement){
	i=1;
	if(whichElement.parentNode.rowIndex == 0){
		if(whichElement.parentNode.cells[1].cellIndex == 1){
			if(whichElement.parentNode.parentNode.rows[0].cells[whichElement.cellIndex].name != 'active'){
				while(whichElement.parentNode.parentNode.rows[i]){
					myRow = whichElement.parentNode.parentNode.rows[i];
					if(myRow.name != 'active') {
						myRow.cells[whichElement.cellIndex].style.borderRightColor = columnHighlightColor;
						myRow.cells[whichElement.cellIndex].style.borderLeftColor = columnHighlightColor;
						myRow.cells[whichElement.cellIndex].style.backgroundColor = columnHighlightColor;
					}
					i++;
				}
			}
		}
	}
	if(whichElement.parentNode.rowIndex != 0){
		if(whichElement.parentNode.name != 'active') {  
			myRow = whichElement.parentNode.parentNode.rows[whichElement.parentNode.rowIndex];
			myRowCells = myRow.cells;
			i=0;
			while(myRowCells[i]){
				if(whichElement.parentNode.parentNode.rows[0].cells[i].name != 'active'){
					myRowCells[i].style.borderRightColor = rowHighlightColor;
					myRowCells[i].style.borderLeftColor = rowHighlightColor;
					myRowCells[i].style.backgroundColor = rowHighlightColor;
				}
				i++;
			}
			/*
			if(whichElement.parentNode.parentNode.rows[0].cells[whichElement.cellIndex].name != 'active'){
				whichElement.style.borderRightColor = cellHighlightColor;
				whichElement.style.borderLeftColor = cellHighlightColor;
				whichElement.style.backgroundColor = cellHighlightColor;
			}
			*/
		}
	}
}
function listBoxRollOff(whichElement){
	i=1;
	if(whichElement.parentNode.rowIndex == 0){
		if(whichElement.parentNode.cells[1].cellIndex == 1){
			if(whichElement.parentNode.parentNode.rows[0].cells[whichElement.cellIndex].name != 'active'){
				while(whichElement.parentNode.parentNode.rows[i]){
					myRow = whichElement.parentNode.parentNode.rows[i];
					if(myRow.name != 'active') { 
						myRow.cells[whichElement.cellIndex].style.borderRightColor = whichElement.parentNode.parentNode.style.backgroundColor;
						myRow.cells[whichElement.cellIndex].style.borderLeftColor = whichElement.parentNode.parentNode.style.backgroundColor;
						myRow.cells[whichElement.cellIndex].style.backgroundColor = whichElement.parentNode.parentNode.style.backgroundColor;
					}
					i++;
				}
			}
		}
	}
	if(whichElement.parentNode.rowIndex != 0){
		if(whichElement.parentNode.name != 'active') { 
			myRow = whichElement.parentNode.parentNode.rows[whichElement.parentNode.rowIndex].cells;
			i=0;
			while(myRow[i]){
				if(whichElement.parentNode.parentNode.rows[0].cells[i].name != 'active'){
					myRow[i].style.borderRightColor = whichElement.parentNode.parentNode.style.backgroundColor;
					myRow[i].style.borderLeftColor = whichElement.parentNode.parentNode.style.backgroundColor;
					myRow[i].style.backgroundColor = whichElement.parentNode.parentNode.style.backgroundColor;
				}
				i++;
			}
		}
	}
}

function init_table(thisTable){
	init_titles(thisTable);
	var tableRows = thisTable.rows;
	i=0;
	while(tableRows[i]){
		rowCells = tableRows[i].cells;
		j=0;
		while(rowCells[j]){
			if (navigator.appName == "Microsoft Internet Explorer"){
				rowCells[j].onmouseover = function(){listBoxRollOn(this)};
				rowCells[j].onmouseout = function(){listBoxRollOff(this)};
				rowCells[j].onclick = function(){listBoxClickToggle(this, event)};
			} else {
				rowCells[j].addEventListener('mouseover',function(){listBoxRollOn(this)},false);
				rowCells[j].addEventListener('mouseout',function(){listBoxRollOff(this)},false);
				rowCells[j].addEventListener('click',function(event){listBoxClickToggle(this, event)},false);
			}
			j++;
		}
		i++;
	}
}

//HIDE SHOW FOR NOTES
//find the y position of an html dom object
function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
//find the x position of an html dom object
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
//show note
function show_note(myNote, alignment){
	var objectNote = document.getElementById(myNote);
	var myMarker = document.getElementById(myNote + "_marker");
	objectNote.style.visibility='visible';
	
	var myY = findPosY(myMarker);
	var myX = findPosX(myMarker);
	if(myY>18){
		objectNote.style.top=(myY - 5) + "px";
	}else{
		objectNote.style.top=0 + "px";
	}
	if(alignment == "left"){
		objectNote.style.left=(myX - 220) + "px";
	} else {
		objectNote.style.left=(myX + 15) + "px";
	}
}
//hide note on roll off
function hide_note(myNote){
	var objectNote = document.getElementById(myNote);
	objectNote.style.visibility='hidden';
}

//COOKIE FUNCTIONS FOR COLLAPSABLE BOXES
//tries to get a cookie with a particular name
function getCookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}
//creates a cookie with hidden/visible value
function setCookie(name,value,expires,path,domain,secure) {
   document.cookie = name + "=" +escape(value) +
   ((expires) ? ";expires=" + expires.toGMTString() : "") +
   ((path) ? ";path=" + path : "") + 
   ((domain) ? ";domain=" + domain : "") +
   ((secure) ? ";secure" : "");
}

//creates the expire date and sets it to 1 year from creation
var expireDate = new Date();
expireDate.setFullYear(expireDate.getFullYear() + 1);

//VISIBILITY FUNCTIONS
//sets the element to hidden and changed it's toggle image
function setHidden(whichID){
	elementID = document.getElementById(whichID);
	elementImage = document.getElementById(whichID + 'Image');
	//elementIndicator = document.getElementById(whichID + 'Indicator');
	functionFooter = document.getElementById(whichID + 'Footer');

	elementID.style.display = 'none';
	if(functionFooter){
		if (navigator.appName == "Microsoft Internet Explorer"){
			functionFooter.style.display = '';
		} else {
			functionFooter.style.display = 'table-row';
		}
		
	}
	//elementIndicator.innerText = '+';
	elementImage.src = 'images/expand.gif';
	setCookie(whichID, "hidden", expireDate);
}
//sets the element to visible and changed it's toggle image
function setVisible(whichID){
	elementID = document.getElementById(whichID);
	elementImage = document.getElementById(whichID + 'Image');
	//elementIndicator = document.getElementById(whichID + 'Indicator');
	functionFooter = document.getElementById(whichID + 'Footer');
	
	if(functionFooter){
		functionFooter.style.display = 'none';
	}
	if (navigator.appName == "Microsoft Internet Explorer"){
		elementID.style.display = '';
	} else {
		elementID.style.display = 'table-row';
	}
	//elementIndicator.innerText = '-';
	elementImage.src = 'images/collapse.gif';
	setCookie(whichID, "visible", expireDate);
}
//toggles visibility based on current state
function toggleVisible(whichID){
	elementID = document.getElementById(whichID);
	if((elementID.style.display == '')||(elementID.style.display == 'table-row')){
		setHidden(whichID);
	} else {
		setVisible(whichID);
	}
}
//tests to see if there is an existing value on the element, as default is to alway be visible this only takes action when it's set to hidden
function checkCookie(whichID){
	tempCookie = getCookie(whichID);
	if(tempCookie=="hidden"){
		setHidden(whichID);
	} else if (tempCookie=="visible") {
		setVisible(whichID);
	}
}

//code for scrolling to first row beggining with pressed key on iframe boxes
var lastSelected;
var myLetterIndex = new Array();

function initLookAhead(thisTable){
	this_row = thisTable.rows;
	i = 1;
	while (this_row[i]){
		current_cells = this_row[i].cells;
		myLetterIndex[this_row[i].rowIndex] = current_cells[0].innerText.substring(0,1).toLowerCase();
		i++;
	}
}

function scrollToKey(which,thisTable) {
	if(!which.modifiers){
		myLetter = String.fromCharCode(which.keyCode);
		if(lastSelected){
			currRow = lastSelected + 1;
		} else {
			currRow = 1;
		}
		while((myLetterIndex[currRow]) && (myLetterIndex[currRow] != myLetter)){
			currRow++;
		}
		if(myLetterIndex[currRow] != myLetter){
			if(lastSelected){
				currRow = 1;
				while((myLetterIndex[currRow]) && (myLetterIndex[currRow] != myLetter)){
					currRow++;
				}
			}
		}
		if(myLetterIndex[currRow] == myLetter){
			yPosition = findPosY(thisTable.rows[currRow].cells[0]);
			scrollMe(yPosition);
			if(lastSelected){
				myRow = thisTable.rows[lastSelected];
				//rolloff(lastSelected, thisTable, myRow);
				listBoxRollOff(thisTable.rows[lastSelected].cells[1]);
			}
			lastSelected = currRow;
			//rollon(currRow, thisTable, thisTable.rows[currRow]);
			listBoxRollOn(thisTable.rows[currRow].cells[1]);
		}
	}
}
 
function scrollMe(myY){
	window.scrollTo(0,myY);
}
 
//hide show for credit history
var myHistoryIndex = new Array();
function hideShowHistory(myHistoryID){
	elementID = document.getElementById("myhistory" + myHistoryID);
	linkElementID = document.getElementById("hideShow" + myHistoryID);
	if(myHistoryIndex[myHistoryID] != "show"){
		elementID.style.display = 'block';
		linkElementID.innerHTML="Hide Comments";
		myHistoryIndex[myHistoryID] = "show";
	} else {
		elementID.style.display = 'none';
		myHistoryIndex[myHistoryID] = "hide";
		linkElementID.innerHTML="Show Comments";
	}
}