// JavaScript Document

//DNH - 09/26/08 - Always get the browser type in case we need it later
// Set a variable for use in ALL pages for browser type
var bType = getBrowserType();
//alert("Browser Type: " + bType);



/*************************************************************************************************************************/
function getBrowserType(show) {//Created by Dave Hopkins, ImageSetters 09/26/08
	//Function for retrieving the user's browser type
	var bT;
	if(show) alert("This browser is: " + navigator.appName);
	if(show) alert("This browser is: " + navigator.appCodeName);
	if (navigator.appName.indexOf('Microsoft') != -1)
		bT = 'IE';
	else if (navigator.appName.indexOf('Netscape') != -1)
		bT = 'FF';
	return bT;
}

/*************************************************************************************************************************/
function openWin(theURL,theTarget,theParms) { //Created by Dave Hopkins, ImageSetters 11/14/07
	//Function to open and populate a window
	newWindow = window.open(theURL,theTarget,theParms);
	newWindow.focus();
}

/*************************************************************************************************************************/
function togglePtr(me,theOpenImage,theCloseImage) {//Created by Dave Hopkins, ImageSetters 09/20/07
	//Function for toggling the standard menu pointer when used in an image
	var theSrc, theImg;
	theImg = document.getElementById(me);
	if(theImg.src.indexOf(theOpenImage) != -1){
		theSrc = theImg.src.replace(theOpenImage,theCloseImage);
		theImg.src = theSrc;
	} else 
	if(theImg.src.indexOf(theCloseImage) != -1){
		theSrc = theImg.src.replace(theCloseImage,theOpenImage);
		theImg.src = theSrc;
	}
}
	
/*************************************************************************************************************************/
function toggleRow()
{ // Created by ImageSetters - DNH - 9/19/07 - toggles a DIV on or off
  for (var i=0;i<arguments.length;i++)
  {
    var elm = document.getElementById(arguments[i]);
	//alert(elm);
    var disp = elm.style.display;
    elm.style.display = (!disp || disp == "")? "none" : "block";
  }
}

/*************************************************************************************************************************/
function cursorHand(){//Created by Dave Hopkins, ImageSetters 09/20/07
	//Function for toggling the standard menu pointer when used in an image
	
}

/*************************************************************************************************************************/
function getParmValue(name){//Adapted by Dave Hopkins, ImageSetters 10/11/07
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else 
		return results[1];	
}

/*************************************************************************************************************************/
// Created by ImageSetters - DNH - 9/19/07 - expands sections of a multi-sectional page of lists
// This function has different sub-functions, based upon the calling page. It has been placed in the main
//  scripts file for ease of updating when new pages are create that need the same function
// Used in: forms.shtml
function expandSections(pageRef) { 
	var openParm = getParmValue("open");
	var formParm = getParmValue("form");
	var anchorRef = window.location.href.split("#");
	//alert (openParm);
	if (openParm != "") {
		var items = openParm.split(",");
		if(items.length > 0) {
			for(i=0;i<items.length;i++) {
			//alert(items[i]);
			toggleRow(items[i]+'Table');
			togglePtr(items[i]+'Ptr');
			}
		} else {
			toggleRow(openParm+'Table');
			togglePtr(openParm+'Ptr');
		}
	}	
	// If the calling page referenced a particular form, then highlight its row
	if(formParm != "") highlightRow(formParm);

// Check if page was referenced by an anchor (#) and open that section only - we expect reference to only one section
	if(anchorRef.length > 1) {
		toggleRow(anchorRef[1]+'Table');
		togglePtr(anchorRef[1]+'Ptr');
	}
}
/*************************************************************************************************************************/
	// We are currently setup to show only one form. If multiples are references, this routine will not display ANY forms.
function highlightRow(formParm) {
	if (formParm != "") {
		formParm = formParm.replace("%20"," ");
		//alert(formParm);
		document.getElementById(formParm).bgColor ="#FFFFCC";	
//This is not currently working... Probs with IE vs. Firefox handling elements...		
//		var theElements = document.getElementsByName(formParm);
//		alert(theElements.length);
//		for (cntr=0;cntr<theElements.length;cntr++) {
//			theElements(cntr).bgColor="#FFFFDD";
//			alert(theElements(cntr).innerText);
//		}
		
		location.href = "#" + formParm;
	}	
}


/*************************************************************************************************************************/
function setSubMenuByPageName() { //Created by Dave Hopkins, ImageSetters 09/06/07
	/* This function will strip the document URL to get the page name without the extension
		If there is a DIV element with the same name in one of the menus, it will recursively
		call the openSubMenu() function until the menu item is shown. 
		
	  This function calls openSubMenu(). */
	
	var theRef = document.referrer;
		
	var pageName = document.URL.substring(document.URL.lastIndexOf("/")+1,document.URL.length).split(".");
	//feed the page name (which should also be the submenu ID) to the function along with a "1" to force it open
		//alert("Page: " + pageName[0] + "<<<");
	if(document.getElementById(pageName[0])) {
		//alert("Page: " + pageName[0] + "<<<");
		openSubMenu(pageName[0],1);
		//alert("Past Submenu Open");
		var pNode = "";
		pNode = document.getElementById(pageName[0]).parentNode.attributes.getNamedItem("Id").value;
		//alert(pNode);
		while(pNode != "p7TMnav"){
			//alert("Opening: " + pNode);
			openSubMenu(pNode,1);
			//Get this node's parent and repeat...
			pNode = document.getElementById(pNode).parentNode.attributes.getNamedItem("Id").value;
		}
	}
		//alert("All Menu Items should be the way we want them...");
	
	var ha, hr, d;
	d=document.getElementById('p7TMnav');
	hr=window.location.href;
	//alert(hr);
}

/*************************************************************************************************************************/
function openSubMenu(a,og) {//Created by Dave Hopkins, ImageSetters 09/06/07
	/* This function will open a submenu based on the structure of Project 7 tree menus. */ 
 if(document.getElementById){
 // get the proposed item's parent ID
 var b,i,s,c,k,j,m,tN,hh;
 b=document.getElementById(a);
			//alert("Looking for: " + b.attributes.getNamedItem("Id").value);
 
 if(b.parentNode && b.parentNode.childNodes){
	 tN=b.parentNode.childNodes;
	//alert("it has " + tN.length + " child nodes");
	 }else{
	 //alert("NO parent nodes");
	 return;
	 }
 for(i=0;i<tN.length;i++){
	 if(tN[i].tagName=="DIV"){
	 s=tN[i].style.display;
	 //this shows or hides the DIV group
		hh=(s=="block")?"none":"block";
		if(og==1){hh="block";}
		tN[i].style.display=hh;
			if(tN[i].attributes.getNamedItem("Id").value == a) {
			 if(tN[i].firstChild.className=='p7plusmark'){
				tN[i].firstChild.className='p7minusmark';
					//alert("switch");
			 } else 
			 if(tN[i].firstChild.className=='p7minusmark'){
				 if(og!=1) {
					tN[i].firstChild.className='p7plusmark';
				 }
			 }
			}
		}
	 }
	 
	 c=b.firstChild;
	 //alert(b.innerText);
	 if(b.innerHTML){
		 //alert("inner HTML: " + b.innerHTML);
		 k=b.innerHTML;
		 
		 for(m=0;m<k.length;m++) {
			 j=k.charAt(m);
			 if(j=='+'){
				 //alert(k);
				 k= k.substring(0,m) + '-' + k.substring(m+1,k.length);
				 m = k.length
				 alert(k);
			 } 
		 }
		 b.innerHTML=k;
	 }
 }
}

/**********************************************************************************************/
// Signon Scripts
// These scripts are used as a very rudimentary way of prohibiting or permitting users to access
// "special" pages on the site. 
// !!!!!!!!!!!!!!!!!!!!THEY ARE NOT MEANT AS ANY FORM OF SECURITY!!!!!!!!!!!!!!!!!!!!!!! 
// They are only to keep out casual viewers from certain maintenance pages


// Variables used by these routines
var usersArray = new Array();
var thisUser = "";
var thisPwd = "";
var thisLevel = 0;
var thisTitle = "";
var verified = -1;

function makeUsers() {
	//Current users as of 2/15/08 
	//Any level above 9 is admin, while 1-9 are users
	//A user can have multiple security levels, based upon password
	usersArray[0] = ["Name","Password","Sec Lvl","Security Level Description"];
	usersArray[1] = ["Hopkins","dave","10","Site Administrator/Programmer"];
	usersArray[2] = ["Soto","erin","10","Site Administrator/Programmer"];
	usersArray[3] = ["Buele","jane","10","Site Administrator/Programmer"];
	usersArray[4] = ["Black","Griffin","1","Security Level 1 Description"];	
	usersArray[5] = ["User","user","1","Security Level 1 Description"];	
}

//makeUsers();

function verifyUser(userName, password) {
	verified = -1;
	for (a=1;a<usersArray.length;a++) {
		if(userName.toLowerCase() == usersArray[a][0].toLowerCase()) {
			//We found a user. Let's check the password
			if(password == usersArray[a][1]) {			
				thisUser = usersArray[a][0];
				thisPwd = usersArray[a][1];
				thisLevel = usersArray[a][2];
				thisTitle = usersArray[a][3];				
				verified = 1;
			} else {
				if(verified !=1) {
					verified = 0;
				}
			}
		}
//		alert(verified);
	}
}

function findUser(userName, theDIV) {
	if(theDIV === undefined) theDIV = "";
	var theTable = '<table width="440" border="1" align="center" cellpadding="0" cellspacing="0">\n';
	theTable += '<tr><th width="80">' + usersArray[0][0] + '</th><th width="80">' + usersArray[0][1]	+ '</th><th width="60">' + 
		usersArray[0][2] + '</th><th width="220">' + usersArray[0][3] + '</th></tr>';

	for (a=0;a<usersArray.length;a++) {
		if(userName.toLowerCase() == usersArray[a][0].toLowerCase()) {
			//We found a user. Let's insert the info into the vars...
			thisUser = usersArray[a][0];
			thisPwd = usersArray[a][1];
			thisLevel = usersArray[a][2];
			thisTitle = usersArray[a][3];			
			if(theDIV != "") {
				theTable += '<tr><td width="80">' + thisUser + '</td><td width="80">' + thisPwd	+ '</td><td width="60">' + 
					thisLevel + '</td><td width="220">' + thisTitle + '</td></tr>';
			} else {
				alert("***** Found User *****\n\nName: " + thisUser + ".\nPassword: " + thisPwd + ".\nSecurity Level: " + thisLevel + ".\nTitle: " + thisTitle);
			}			
		}
	}
	if(theDIV != "") {
		theTable += "</table>";
		//alert(theTable);
		document.getElementById(theDIV).innerHTML = theTable;	
	}				
}

function makeUserTable(theDIV) {
	var theTable = '<table width="440" border="1" align="center" cellpadding="0" cellspacing="0">\n';
	for (a=0;a<usersArray.length;a++) {
		//Get every user name and build a table to insert into a DIV
		thisUser = usersArray[a][0];
		thisPwd = usersArray[a][1];
		thisLevel = usersArray[a][2];
		thisTitle = usersArray[a][3];
		if(a==0) {
			theTable += '<tr><th width="80">' + thisUser + '</th><th width="80">' + 
				thisPwd	+ '</td><th width="60">' + thisLevel + '</th><th width="220">' + thisTitle + '</th></tr>';
		} else {
			theTable += '<tr><td width="80">' + thisUser + '</td><td width="80">' + 
				thisPwd	+ '</td><td width="60">' + thisLevel + '</td><td width="220">' + thisTitle + '</td></tr>';
		}
	}
	theTable += "</table>";
	//alert(theTable);
	document.getElementById(theDIV).innerHTML = theTable;
}

function findU() {
	findUser(document.getElementById("userName").value,"userTable");
}
function verifyU() {
	verifyUser(document.getElementById("userName").value,document.getElementById("password").value);
}

function setCookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
//alert(expires);
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = "";
document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


/**********************************************************************************************/



