
//Generic alert to be used with links to delete things.  Simply a confirmation of intent to delete.
function deleteCheck(objName) {
	return confirm("This will permanently delete this " + objName + ".  Are you sure you want to do this?");
}

//Pop-up Window
function gimmeAWin(filename) {
	helpWindow = window.open(filename,"PopUpWindow","width=400,height=400,scrollbars=yes");
	if (window.focus) {helpWindow.focus()}

}

//Variable pop-up window
function newWindow(filename,widthVal,heightVal) {

	argVar = "width=" + widthVal + ",height=" + heightVal + ",scrollbars=yes,resizable=yes";
	reportWindow = window.open(filename,"PopUpWindow",argVar);
	if (window.focus) {reportWindow.focus()}
}


//warning message to the user if they un-chcek the "sub-budget' box on a subcontractor line item in the budget that all of the sub-budget information will be deleted!!!
function subBudgetWarningMessage() {
	warningMsg = "Warning: Unchecking this box will delete any budget information that has been entered by the selected subcontractor!!\n\n Are you sure you want to do this?";

	return confirm(warningMsg);
}


//  These two functions support the timeout pop-up warning.
// Timeout warning message...
function timeOutWarningMessage() {
	var nowDate = new Date();
	
	warningMsg = "Warning: Your login session on OTIS should expire in approximately 5-10 minutes.\n\nTo prevent this, you must interact with the system in an active fashion.  For example, clicking on a link or saving data in a form.\n\n (Message Displayed:" + nowDate + ")";

	alert(warningMsg);
}

//after X minutes show logout alert
function sessionLogOutWarning() {
	warningTime =  13800000; //13800000;  // 230 Minutes (3Hrs, 50 minutes)
		
	//give warning
	window.setTimeout("timeOutWarningMessage()", warningTime);

}
// end timeout pop-up window

// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// ugly workaround for missing support for selectorText in Netscape6/Mozilla
// call onLoad() or before you need to do anything you would have otherwise used
// selectorText for.
var ugly_selectorText_workaround_flag = false;
var allStyleRules;
// code developed using the following workaround (CVS v1.15) as an example.
// http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
function ugly_selectorText_workaround() {
	if((navigator.userAgent.indexOf("Gecko") == -1) ||
	   (ugly_selectorText_workaround_flag)) {
		return; // we've already been here or shouldn't be here
	}
	var styleElements = document.getElementsByTagName("style");
	
	for(var i = 0; i < styleElements.length; i++) {
		var styleText = styleElements[i].firstChild.data;
		// this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
		// error in IE5, so we include the open brace and then strip it
		allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
	}

	for(var i = 0; i < allStyleRules.length; i++) {
		// probably insufficient for people who like random gobs of 
		// whitespace in their styles
		allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
	}
	ugly_selectorText_workaround_flag = true;
}


// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

// getStyleById: given an element ID and style property
// return the current setting for that property, or null.
// args:
//  i - element id
//  p - property
function getStyleById(i, p) {
	var n = document.getElementById(i);
	var s = eval("n.style." + p);

	// try inline
	if((s != "") && (s != null)) {
		return s;
	}

	// try currentStyle
	if(n.currentStyle) {
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null)) {
			return s;
		}
	}
	
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[y] == i) {
							return z[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) ||
						   (rules[y].selectorText == i)) {
							return z[p];
						}
					}
				}
			}
		}
	}
	return null;
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;

function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}

// getStyleByClass: given an element type, a class selector and a property,
// return the value of the property for that element type.
// args:
//  t - element type
//  c - class identifier
//  p - CSS property
function getStyleByClass(t, c, p) {
	// first loop over elements, because if they've been modified they
	// will contain style data more recent than that in the stylesheet
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					var theStyle = eval('node.style.' + p);
					if((theStyle != "") && (theStyle != null)) {
						return theStyle;
					}
				}
			}
		}		
	}
	// if we got here it's because we didn't find anything
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if((allStyleRules[y] == c) ||
						   (allStyleRules[y] == (t + "." + c))) {
							return z[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) &&
						   ((rules[y].selectorText == c) ||
						    (rules[y].selectorText == (t + "." + c)))) {
							return z[p];
						}
					}
				}
			}
		}
	}

	return null;
}

// setStyleByTag: given an element type, style property and 
// value, and whether the property should override inline styles or
// just global stylesheet preferences, apply the style.
// args:
//  e - element type or id
//  p - property
//  v - value
//  g - boolean 0: modify global only; 1: modify all elements in document
function setStyleByTag(e, p, v, g) {
	if(g) {
		var elements = document.getElementsByTagName(e);
		for(var i = 0; i < elements.length; i++) {
			elements.item(i).style[p] = v;
		}
	} else {
		var sheets = document.styleSheets;
		if(sheets.length > 0) {
			for(var i = 0; i < sheets.length; i++) {
				var rules = sheets[i].cssRules;
				if(rules.length > 0) {
					for(var j = 0; j < rules.length; j++) {
						var s = rules[j].style;
						// selectorText broken in NS 6/Mozilla: see
						// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
						ugly_selectorText_workaround();
						if(allStyleRules) {
							if(allStyleRules[j] == e) {
								s[p] = v;
							}			
						} else {
							// use the native selectorText and style stuff
							if(((s[p] != "") && (s[p] != null)) &&
							   (rules[j].selectorText == e)) {
								s[p] = v;
							}
						}

					}
				}
			}
		}
	}
}

// getStyleByTag: given an element type and style property, return
// the property's value
// args:
//  e - element type
//  p - property
function getStyleByTag(e, p) {
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		for(var i = 0; i < sheets.length; i++) {
			var rules = sheets[i].cssRules;
			if(rules.length > 0) {
				for(var j = 0; j < rules.length; j++) {
					var s = rules[j].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[j] == e) {
							return s[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((s[p] != "") && (s[p] != null)) &&
						   (rules[j].selectorText == e)) {
							return s[p];
						}
					}

				}
			}
		}
	}

	// if we don't find any style sheets, return the value for the first
	// element of this type we encounter without a CLASS or STYLE attribute
	var elements = document.getElementsByTagName(e);
	var sawClassOrStyleAttribute = false;
	for(var i = 0; i < elements.length; i++) {
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if((node.attributes.item(j).nodeName == 'class') ||
			   (node.attributes.item(j).nodeName == 'style')){
			   sawClassOrStyleAttribute = true;
			}
		}
		if(! sawClassOrStyleAttribute) {
			return elements.item(i).style[p];
		}
	}
}

// toggleVisibility:
//		given an id, expand or collapse a result item on a page
//		if no id is passed, expand or collapse all results on a page based on what the image in the heading bar is set to
function toggleVisibility(itemID) {
	if (itemID) {	// set an individual item
		var infoBlock = document.getElementById('itemInfo' + itemID).style;
		var actionItem = document.getElementById('visToggleAction' + itemID);
		if (infoBlock.display == "block") { 
			infoBlock.display = "none"; 
			actionItem.innerHTML = "<img src=\"images/icons/expand.gif\" border=\"0\" alt=\"Expand\">";
		} else { 
			infoBlock.display = "block";
			actionItem.innerHTML = "<img src=\"images/icons/collapse.gif\" border=\"0\" alt=\"Collapse\">";
		}
	} else {	// set all items on page
		var i = 1;
		var toggleAll = document.getElementById('visToggleActionAll');
		var expandOrCollapse = toggleAll.innerHTML.match("collapse");
		// run through all of the expand/collapse icons and information blocks and set them accordingly
		while (document.getElementById('visToggleAction' + i)) {
			var actionItem = document.getElementById('visToggleAction' + i)
			var infoBlock = document.getElementById('itemInfo' + i).style;
			if (expandOrCollapse) { 
				infoBlock.display = "none";
				actionItem.innerHTML = "<img src=\"images/icons/expand.gif\" border=\"0\" alt=\"Expand\">";
			} else { 
				infoBlock.display = "block";
				actionItem.innerHTML = "<img src=\"images/icons/collapse.gif\" border=\"0\" alt=\"Collapse\">";
			}
			i++;
		}
		// change icons for expand/collapse all
		if (expandOrCollapse) { 
			toggleAll.innerHTML = "<img src=\"images/icons/expandAll.gif\" border=\"0\" alt=\"Expand All\">";
		} else { 
			toggleAll.innerHTML = "<img src=\"images/icons/collapseAll.gif\" border=\"0\" alt=\"Collapse All\">";
		}
	}
}

// toggleCheckboxStatus: check or uncheck all results on a page
//		formObj = the form object that the checkboxes reside in
//		if checked = 1, then all checkboxes will be checked
//		if checked = 2, then all checkboxes will be unchecked
//		if no parameters are passed, then all checkboxes will be checked/unchecked based on what the checkbox in the heading bar is set to
function toggleCheckboxStatus(formObj, checked) {
	if (!checked) {
		var checked = formObj.selectionAll.checked;
	}
    if (typeof formObj.selection.length == "undefined") {
		if (checked == 1) {
			formObj.selection.checked = true;
		} else {
			formObj.selection.checked = false;
		}
	}
	for (i = 0; i < formObj.selection.length; i++) {
		if (checked == 1) {
			formObj.selection[i].checked=true;
		} else {
			formObj.selection[i].checked=false;
		}
	}
	if (checked == 1) {
		toggleAll.innerHTML = "<a href=\"javascript:toggleCheckboxStatus(document.resultsList,2);\"><img src=\"images/buttons/uncheck_all.gif\" width=\"147\" height=\"19\" alt=\"\" border=\"0\"></a>";
		toggleAll2.innerHTML = "<a href=\"javascript:toggleCheckboxStatus(document.resultsList,2);\"><img src=\"images/buttons/uncheck_all.gif\" width=\"147\" height=\"19\" alt=\"\" border=\"0\"></a>";
	} else {
		toggleAll.innerHTML = "<a href=\"javascript:toggleCheckboxStatus(document.resultsList,1);\"><img src=\"images/buttons/check_all.gif\" width=\"147\" height=\"19\" alt=\"\" border=\"0\"></a>";
		toggleAll2.innerHTML = "<a href=\"javascript:toggleCheckboxStatus(document.resultsList,1);\"><img src=\"images/buttons/check_all.gif\" width=\"147\" height=\"19\" alt=\"\" border=\"0\"></a>";
	}
}




var IE = document.all ? true : false;
function ShowTooltip(toolTip,e) {
	var x,y;
	var ttObj = document.getElementById("tooltip");
	if (!e) var e = window.event;
	if (IE) {
		x = event.clientX + document.body.scrollLeft;
		y = event.clientY + document.body.scrollTop;
	} else {
		x = e.pageX;
		y = e.pageY;
	}
	ttObj.innerHTML = toolTip; 
    ttObj.style.display = "block";
    ttObj.style.left = (x + 15) + 'px';
    ttObj.style.top = (y - 15) + 'px'; 
}

function HideTooltip() {document.getElementById("tooltip").style.display = "none";}


// swapOptions(select_object,option1,option2)
// Swap positions of two options in a select list
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

// hasOptions(obj)
// Utility function to determine if a select object has an options array
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}

// moveOptionUp(select_object)
// Move selected option in a select list up one if possible
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i < obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}

// moveOptionDown(select_object)
// Move selected option in a select list down one
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}

// selectAllOptions(select_object)
// This function takes a select box and selects all options (in a multiple select object).
function selectAllOptions(obj) {
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
	}
}

// checkAllOptions(select_object)
// This function takes a check box and checks all options (in a multiple select object).
function checkAllOptions(obj) {
	for (var i=0; i<obj.length; i++) {
		obj[i].checked = true;
	}
}
// uncheckAllOptions(select_object)
// This function takes a check box and unchecks all options (in a multiple select object).
function uncheckAllOptions(obj) {
	for (var i=0; i<obj.length; i++) {
		obj[i].checked = false;
	}
}

// moveSelectedRows(select_object, select_object)
// Move selected option in a select list to another select list
function moveSelectedRows(srcList,destList) {
	// Move rows from srcList to destList from bottom to top
	for (i=srcList.options.length - 1; i>=0; i--) {
		if (srcList.options[i].selected == true) {
			var newRow = new Option(srcList.options[i].text,srcList.options[i].value);
			destList.options[destList.length]=newRow;
			srcList.options[i]=null;
		}
	}
}

// sortSelect(select_object)
// Sort a list down by its option's text property
function sortSelect(selectObj) {
	var ID='';
	var Text='';
	for (x=0; x < selectObj.length - 1; x++) {
		for (y=x + 1; y < selectObj.length; y++) {
			if (selectObj[x].text > selectObj[y].text) {
				// Swap rows
				ID=selectObj[x].value;
				Text=selectObj[x].text;
				selectObj[x].value=selectObj[y].value;
				selectObj[x].text=selectObj[y].text;
				selectObj[y].value=ID;
				selectObj[y].text=Text;
			}
		}
	}
}

// skipToSelectOption(text_input_object, select_object)
// Move highlight to the row in a select list where the find parameter matched at least the initial text in the select list row
function skipToSelectOption(find, selectList) {
	if (find != "") {
		var skipToIndex = -1;
		for (var i=0; i < selectList.options.length; i++) {
			if (selectList.options[i].text.toLowerCase().indexOf(find.toLowerCase()) == 0) {
					skipToIndex = i;
					break;
			}
		}
		if (skipToIndex != -1) {
			selectList.selectedIndex = skipToIndex;
		}
	}
}

// isSelectionMade(form_object)
// This function determines if a selection has been made on a specified form
function isSelectionMade(formObj) {
	var isSelectionMadeDecision = false;
	if (formObj.selection.length) {
		for (var i=0; i<formObj.selection.length; i++) {
			if (formObj.selection[i].checked == true) {isSelectionMadeDecision = true;}
		}
	} else {
		if (formObj.selection.checked == true) {isSelectionMadeDecision = true;}
	}
//	for (var i=0; i<formObj.selection.length; i++) {
//		if (formObj.selection[i].checked == true) {isSelectionMadeDecision = true;}
//	}
	if (!isSelectionMadeDecision) {alert("A selection must be made before you can submit this form");}
	return isSelectionMadeDecision;
}


//Generic alert to be used with links to cancel things.  Simply a confirmation of intent to delete.
function cancelCheck(objName) {
	return confirm("This will cancel this " + objName + ".  Are you sure you want to do this?");
}

//Alert to verify intent to logout..
function logoutCheck() {
	return confirm("Are you sure you want to log out of ROVER?");
}

//Remove from Check
function removeCheck(objName) {
	return confirm("This will remove the selected/current record from this " + objName + ".  Are you sure you want to do this?");
}