/***********************************************************
This is the global JS file for the GE Consumer & Industrial
Environmental Information Center.
***********************************************************/


// global vars
var popupWin;
var scrnwidth=screen.width;
var scrnheight=screen.height;

// page URL after 'environmentalinfo' directory (for URL matching purposes)
var pageSubURL = document.location.href.split("environmentalinfo")[1];
var pageSubURL = pageSubURL.split("?")[0];
var pageSubURL = pageSubURL.split("#")[0];



/**********************************************************************************************
 "only 1 open at a time" popup function.

The fifth argument, windowname, is optional.  If specified, it is passed as the second argument 
to window.open().  This is required in order to leave the popup opener page (without closing 
the popup), come back to it, and NOT have a duplicate popup window.

**********************************************************************************************/
function openPopUp (url, width, height, addloptions, windowname) {
	if (windowname == null) {
		windowname = "";
	}

	var xspot = Math.round((scrnwidth/2)-(width/2));
	var yspot = Math.round((scrnheight/2)-(height/2)-30);

	features = "height="+height+",width="+width+",top="+yspot+",screenY="+yspot+",left="+xspot+",screenX="+xspot;

	if (addloptions && addloptions!="") { features += ","+addloptions; }

	if (!popupWin || popupWin.closed) {
		popupWin = window.open(url, windowname, features);
	} else {
		window.popupWin.close();	
		popupWin = window.open(url, windowname, features);
	}
}




//get url parameter value by name
function getParamFromURL(paramName)
{
  var regexS = "[\\?&'&amp;']"+ paramName +"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}





/***********************************************************
The "topNavSelector" function sets a selected item in the
top nav for the current page (based on the page URL and the
left nav link's HREF).

For pages that exist outside the major categories defined in
the top nav, the default behavior will be to not show any top
nav item as selected. To force an item in the top nav to be 
set as selected, the containing TD must be assigned a unique
ID in the top nav include file. Then you must pass that ID 
into the "topNavSelector" function.

This function should be run in the page immediately 
following the top nav client side include call.
***********************************************************/
function topNavSelector(optionalID) {
try {
	var navArray = document.getElementById("topNav").getElementsByTagName("a");
	// cycle through nav items looking for one with HREF that matches the beginnging of the page URL
	for(i=0;i<navArray.length;i++) {
		var testHref = navArray[i].href.split("environmentalinfo")[1];
		if(pageSubURL.indexOf(testHref) == 0 || navArray[i].parentNode.parentNode.id == optionalID) {
			navArray[i].parentNode.parentNode.className += " selected";
		}
	} // for
} catch(err) {} // try-catch
} // function



/***********************************************************
The "leftNavSelector" function sets a selected item in the
left nav for the current page (based on the page URL and the
left nav link's HREF) and builds the left nav accordingly, 
based on the current page's place in the overall site
hierarchy.

In pages not included in the left nav, the default behavior
will cause only the "Home" item to show. To force another
item in the left nav to be set as selected, that item must
be assigned a unique ID in the left nav include file. Then
you must pass that ID in to the "leftNavSelector" function.

This function should be run in the page immediately 
following the left nav client side include call.
***********************************************************/

function leftNavSelector(optionalID) {
try {
	var navArray = document.getElementById("leftNav").getElementsByTagName("div");
	
	// cycle through nav items looking for one with HREF that matches the document HREF
	for(i=0;i<navArray.length;i++) {

		var testHref = navArray[i].childNodes[0].href.split("environmentalinfo")[1];
		
		if(pageSubURL == testHref || navArray[i].id == optionalID) {
			// set as selected
			navArray[i].className += " selected";
			navArray[i].childNodes[0].className += " selected";
			
			// is this a top level category?
			if(navArray[i].parentNode.id == "leftNav") {
			
				navArray[i].style.display = "block";
				childArray = navArray[i].childNodes;
				for(j=0;j<childArray.length;j++) {
					if(childArray[j].tagName == "DIV") {
						childArray[j].className += " navSubSet";
						childArray[j].style.display = "block";
					} //if
				}//for
				
			} else { // if not a top level category...
				
				// enable siblings
				siblingsArray = navArray[i].parentNode.childNodes;
				for(j=0;j<siblingsArray.length;j++) {
					if(siblingsArray[j].tagName == "DIV") {
						siblingsArray[j].className += " navSubSet";
						siblingsArray[j].style.display = "block";
					}
				}
				
				// bubble up and enable parent generations
				nodeObj = navArray[i].parentNode;
				while(nodeObj.id != "leftNav")	{
					nodeObj.style.display = "block";
					nodeObj = nodeObj.parentNode;
				}// while
				
			} // else
			
			break;
		} // if(document.location.href...
	} // for
} catch(err) {} // try-catch
} // function






/*******************************************************************
VALIDATION VARs AND SUB-FUNCTIONS
*******************************************************************/

// validation vars
var validationErrorTitle = "Please complete the following information to continue:\n\n";
var postValidateFocus = "";



//Sets the first field with an error to be focused after validation.
function setFirstInvalid(inputObj) {
	if(!validationErrorMessage)	{postValidateFocus = document.getElementById(inputObj);}
}

// This function checks for a valid email address
function validateEmail(fieldID)	{
	var fldObj = document.getElementById(fieldID);
	if(fldObj.value.search(/^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/) == -1) {
		return false;
	} else {
		return true;
	}
}

// This function checks for a valid zip code
function validateZip(fieldID)	{
	var fldObj = document.getElementById(fieldID);
	if(fldObj.value.search(/^\d{5}$/) == -1) {
		return false;
	} else {
		return true;
	}
}
