/*  energycodes.gov Javascript
    Author: Geoffrey Elliott
    Updated: September 2003
*/

//  navSelect :: highlights the current page in the navigation bar

function navSelect(currentPage) {
    if(document.getElementById) {
        var navElement = document.getElementById(currentPage);
        if(navElement.style) {
            navElement.style.backgroundColor = "#EEE";
            navElement.style.border = "1px solid #CCC";
            navElement.style.color = "#006";
        }
    }
}

//  isEmail :: returns true if a given string is a valid email address

function isEmail(str) {
    // are regular expressions supported?
    var supported = 0;

    if (window.RegExp) {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr))
            supported = 1;
    }

    if (!supported)
        return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");

    return (!r1.test(str) && r2.test(str));
}

//  getSearchAsArray ::

function getSearchAsArray() {
   var results = new Array()

   if (navigator.version >= 3) {
      // Unescape and strip away leading question mark.
      var input = unescape(location.search.substring(1))
      if (input) {
         // Divide long string into array of name/value pairs.
         var srchArray = input.split("&")
         var tempArray = new Array()
         for (i = 0; i < srchArray.length; i++) {
            // Divide each name/value pair temporarily into a two-entry array.
            tempArray = srchArray[i].split("=")
            // Use temp array values as index identifier and value.
            results[tempArray[0]] = tempArray[1]
         }
      }
   }
   return results
}

//  newWindow :: creates a new window and loads a given URL

function newWindow(url) {
    //alert(url);
    var nwLeft = (screen.availWidth - 780) / 2;
    var nwTop = (screen.availHeight - 450) / 2;
    nw=window.open(url, "emslWindow", "scrollbars,toolbar,menubar,location,status,resizable,width=780,height=450");

    // Bug workaround for Netscape 2 on Unix and Mac
    if(parseInt(navigator.appVersion) == 2 && navigator.appName == 'Netscape') {
        nw=window.open(url, "emslWindow", "scrollbars,toolbar,menubar,location,status,resizable,width=750,height=450");
    }

    nw.moveTo(nwLeft,nwTop);
    nw.focus();
}

//  captureClick :: passes a URL to newWindow(), then returns false to keep the target="_new" from working

function captureClick(url) {
    newWindow(url);
    return false;
}


//  MM_reloadPage :: reloads the page for Netscape 4.  Called when page is loaded and onresize

function MM_reloadPage(init) {
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);





//<!-- Copyright 2005 Bontrager Connection, LLC
//
// Two places need to be customized.
//
//
// Place 1:
// Between the quotation marks, specify the name of 
//    your form.

var FormName = "contactYou";


// Place 2:
// Between the quotation marks, specify the field names 
//    that are required. List the field name separated 
//    with a comma.

var RequiredFields = "a02_FirstName, a03_LastName, whofrom, a07_phone";


//
// No other customization of this JavaScript is required.
//
/////////////////////////////////////////////////////////

function ValidateRequiredFields()
{
var FieldList = RequiredFields.split(",");
var BadList = new Array();
for(var i = 0; i < FieldList.length; i++) {
	var s = eval('document.' + FormName + '.' + FieldList[i] + '.value');
	s = StripSpacesFromEnds(s);
	if(s.length < 1) { BadList.push(FieldList[i]); }
	}
if(BadList.length < 1) { return true; }
var ess = new String();
if(BadList.length > 1) { ess = 's'; }
var message = new String('\n\nThe following field' + ess + ' are required:\n');
for(var i = 0; i < BadList.length; i++) { message += '\n' + BadList[i]; }
alert(message);
return false;
}

function StripSpacesFromEnds(s)
{
while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
	s = s.substring(1,s.length);
	}
while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
	s = s.substring(0,(s.length - 1));
	}
if((s.indexOf(' ',0) == 0) && (s.length == 1)) { s = ''; }
return s;
}
// -->

