function globalOnLoad() {
    // Assign link events
    for (var i = 0; i < document.links.length; i++) {
        if (document.links[i].className.match('External')) {
            document.links[i].onclick = function() { offsitePopup(this.href); return false; }
        }
        if (document.links[i].className.match('Enlargement')) {
            document.links[i].onclick = function() { enlargementPopup(this.href, this.title); return false; }
        }
    }

    // Run page onLoad function, if it exists
    if (typeof onLoad != 'undefined') {
        onLoad();
    }

}

function offsitePopup(url) {
    var popUp = window.open(url, 'offsiteWindow', 'width=750,height=500,scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');
    if (typeof popUp == 'object') {
        popUp.focus();
    }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(globalOnLoad);

function validPostal(field) { //validates Zip Code
    popUpMsg = ((arguments[1]>"")&&(arguments[1]!="undefined"))?arguments[1]:"";
    reFull =/^[0-9]{5}-[0-9]{4}/;                                                         //regular expression matching US Zip+4
    reZip =/^[0-9]{5}$/;                                                                  //regular expression matching US Zip
    reCan =/^[abceghj-np-tvxyABCEGHJ-NPRSTVXY]\d[abceghj-nprstvwxyzABCEGHJ-NPRSTVWXYZ]( )?\d[abceghj-nprstvwxyzABCEGHJ-NPRSTVWXYZ]\d$/    //regular expression matching Canadian Postal Code
    if ((field.value.search(reFull) == -1)&& (field.value.search(reZip) == -1) && (field.value.search(reCan) == -1)) cleaned = false;
    else cleaned = true;
    if(!cleaned) {
        field.onchange = function () { validPostal(field,popUpMsg) };                            //set field to recall this fn onChange
        showMsg("Please enter a valid US or Canadian postal code.",field,popUpMsg);              //show user the Error
        retVal = retVal && cleaned;
    } else {
        noError(field,popUpMsg);                                                                 //unset Error indicators
        retVal = true;
    }
    return retVal;
}

function validEmail(field) {                                                                //validates Email
    popUpMsg = ((arguments[1]>"")&&(arguments[1]!="undefined"))?arguments[1]:"";
    //re =/^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/;
    re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(field.value.search(re) == -1) {                                                      //if not valid
        field.onchange = function () { validEmail(field,popUpMsg) };                        //set field to recall this fn onChange
        showMsg("Please enter a valid email address.",field,popUpMsg);                      //show user the Error
        retVal = false;
    } else {
        retVal = true;
        noError(field,popUpMsg);                                                            //unset Error indicators
    }
    return retVal;
}

function validSearch() {
	field = document.getElementById("SearchBox")
    if ((field.value == "Search")||(field.value<="")) {                                     //if not valid
        field.onchange = function () { validSearch() };                        				//set field to recall this fn onChange
        showMsg("Please enter a search term.",field,1);			                      		//show user the Error
        retVal = false;
    } else {
        retVal = true;
        noError(field,1);                                                            //unset Error indicators
    }
    return retVal;
}

function hasValue(field,fDesc) {
    popUpMsg = ((arguments[2]>"")&&(arguments[2]!="undefined"))?arguments[2]:"";
    if (field.value>"") {
        noError(field,popUpMsg);                                                                 //unset Error indicators
    } else {
        field.onchange = function () { hasValue(field,fDesc,popUpMsg) };                         //set field to recall this fn onChange
        showMsg("Please enter a "+fDesc+".",field,popUpMsg);                                     //show user the Error
        retVal = false;
    }
    return retVal;
}

function showMsg(msg,field) {                                                             //shows user form Error
  popUpMsg = ((arguments[2]>"")&&(arguments[2]!="undefined"))?arguments[2]:"";
  if ((!field.oBGC)&&(field.style.backgroundColor!="pink")) field.oBGC = field.style.backgroundColor;  //save field bG color
  field.style.backgroundColor = "pink";                                                   //set field Error bG color
  if (popUpMsg) {
    alert(msg);                                                                           //alert message text
  } else {
    errSpan = eval("document.getElementById('"+field.name+"_error')");
    errSpan.innerHTML = "<br/>"+msg;
  }
  field.focus();                                                                          //put focus on offending field
  return;
}

function noError(field,popUpMsg) {                                                        //unset Error indicators
  if (field.style.backgroundColor == "pink") field.style.backgroundColor = field.oBGC;    //reset field bG color
  if (!popUpMsg) {
    errSpan = eval("document.getElementById('"+field.name+"_error')");                    //get error Span
    errSpan.innerHTML = ""                                                                //empty it
  }
}

