strNavActiveSection = "";
oPopupTimer = null;
intPopupMenuGlobalShiftX = -15;
intPopupMenuGlobalShiftY = 30;

strUserAgent = navigator.userAgent.toLowerCase();
isMac = (strUserAgent.indexOf("mac") != -1);
isGaleon = (strUserAgent.indexOf("galeon") != -1);
isIE = ((strUserAgent.indexOf("msie") != -1) && (strUserAgent.indexOf("gecko") == -1) && (strUserAgent.indexOf("opera") == -1) && (strUserAgent.indexOf("netscape") == -1)); 


if (!(isMac && isIE) && !isGaleon) {
  if (window.addEventListener)
    window.addEventListener("load", fncPageLoadAll, false);
  else if (window.attachEvent)
    window.attachEvent("onload", fncPageLoadAll);
  else if (document.getElementById)
    window.onload=fncPageLoadAll;
}
  

function fncPageLoadAll() {
  if (fncReadCookie("fontsize")) {
    fncSetFontSize(fncReadCookie("fontsize"));
  }
  fncCreateShadowBox('idFooter', 'NavPopup');
}

function fncSetBackgroundColor(oElement, strColor) {
  oElement.style.backgroundColor = strColor;
}


//---------------------------------------------------------------------------
// FORM FUNCTIONS
function fncFormInputFocus(oField) {
  oField.style.color = "#000000";
  oField.style.backgroundColor = "#E6F3FF";
  fncRemoveClass(oField, "clsFormInputInvalid");
}

function fncFormInputBlur(oField) {
  oField.style.color = "#666666";
  oField.style.backgroundColor = "#FFFFFF";
  fncValidFormField(oField, true);
}

function fncValidateForm(oForm) {
  blnDoFancyFields = false;
  var strErrors = "";
  var oElements = oForm.elements;
  if (arguments.length >= 2) {
    if (arguments[1] == true) {
      blnDoFancyFields = true;
    }
  }
  
  for (var i=0;i<oElements.length;i++) {
    // Validate each element using the fncValidFormField function which will
    //   return any errors found.  
    strErrors = strErrors + fncValidFormField(oElements[i]);
  }
  
  if (strErrors.length > 0) {
    // Report any errors and return false
    alert("Please correct these errors before continuing: \n" + strErrors);
    return false;
  }
  else {
    // All elements have been validated, return true
    return true;
  }
}

function fncValidFormField(oElement) {
  // Returns error message if invalid
  // Form Elements can have the following additional attributes (which may lead to invalid XHTML)
  //   required - makes sure there is a value provided or selected
  //   validphone - ensures valid US/Canadian phone number
  //   validemail - ensures valid email address
  // Does not validate checkbox lists or radio buttons
  var strElementError = "";
  var strNiceName = oElement.name;
  var blnAllRequired = false;
  
  if (oElement.form) {
    if (oElement.form.getAttribute("required")) {
      blnAllRequired = true;
    }
  }
  
  if (typeof(blnDoFancyFields) == "undefined") {
    blnDoFancyFields = false;
  }
  if (arguments.length >= 2) {
    if (arguments[1] == true) {
      blnDoFancyFields = true;
    }
  }
  
  if (oElement.getAttribute("nicename")) {
    strNiceName = oElement.getAttribute("nicename");
  }
  
  if (!oElement.disabled && oElement.getAttribute("type") != "hidden") {
    
    // Validate Required Elements
    if (blnAllRequired || oElement.getAttribute("required")) {
      
      // Check Required Text Elements (Text, Textbox, Textarea)
      if ((oElement.type.indexOf("text") >= 0 || oElement.type.indexOf("password") >= 0) && oElement.value.length <= 0) {
        strElementError = "Please provide a response for " + strNiceName;
      }
      
      // Check Required Drop-Down Boxes
      if (oElement.type.indexOf("select") >= 0) {
        var intNumSelected = 0;
        for (var j=0;j<oElement.length;j++) {
          if (oElement[j].selected && oElement[j].value.length > 0) {
            intNumSelected++;
          }
        }
          
        if (intNumSelected == 0) {
          strElementError = "Please select a response for " + strNiceName;
        }
      }
    }
    
    // Validate Phone Number
    if (oElement.getAttribute("validphone") && oElement.value.length > 0) {
      if (!fncValidUSCanadaPhone(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
    
    // Validate Email Addresses
    if (oElement.getAttribute("validemail") && oElement.value.length > 0) {
      if (!fncValidEmail(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
    if (oElement.getAttribute("validintlphone") && oElement.value.length > 0) {
      if (!fncValidIntlPhone(oElement.value)) {
        strElementError = "Please provide a valid " + strNiceName;
      }
    }
  }
  
  if (strElementError.length > 0) {
    strElementError = "\n - " + strElementError;
    if (blnDoFancyFields) { fncAddClass(oElement, "clsFormInputInvalid"); }
  }
  else {
    if (blnDoFancyFields) { fncRemoveClass(oElement, "clsFormInputInvalid"); }
  }
  return strElementError;
}

function fncValidEmail(strEmail) {
	strInvalidChars = " /:,;'";
	if (strEmail == "" ) {
		return false;
	}
	for ( i = 0; i<strInvalidChars.length;  i++) {
		chrBadChar=strInvalidChars.charAt(i);
		if (strEmail.indexOf(chrBadChar,0) > -1) {
			return false;
		}
	}
	intPos=strEmail.indexOf("@",1)
	if (intPos == -1 ) {
		return false;
	}
	if (strEmail.indexOf("@",intPos+1) > -1 ) {
		return false;
	}
	intPeriodPos=strEmail.indexOf(".",intPos)
	if (intPeriodPos == -1 ) {
		return false;
	}
	if (intPeriodPos+3 > strEmail.length ) {
		return false;
	}
	return true;
}

function fncValidUSCanadaPhone(strPhone) {
	if (strPhone.match(/\s*\(?\s*[2-9]\d{2}\s*\)?\s*([-.]?)\s*\d{3}\s*([-.]?)\s*\d{4}\s*/)) {
	  if (fncValidIntlPhone(strPhone)) {
		  return true;
	  }
	}
	return false;
}

function fncValidIntlPhone(strPhone) {
	strPhone = strPhone.replace(/[\s\(\)\-\.]/g,"");
	if (strPhone.length >= 10) {
	  return true;
	}
}



//---------------------------------------------------------------------------
// FONT RESIZE FUNCTIONS
function fncSetFontSize(intSize) {
  oElement = document.getElementById("idMainContent");
  if (oElement) {
    fncSetElementFontSize(document.getElementById("idMainContent"), intSize)
  }
}

function fncSetElementFontSize(oElement, intSize) {
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  oElement.style.fontSize = intSize + "pt";
  fncSetFontButtonBorder(intSize);
  
  if (typeof(fncSetCookie) == "function")
  fncSetCookie('fontsize', intSize, 'never', '/')
  window.status = "Font Size now " + intSize + "px";
  setTimeout("window.status = '';", 4000);
}

function fncSetFontButtonBorder(intSize) {
  for (var i=0;i<=20;i++) {
    var strID = "idFontSizeButton" + i;
    if (document.getElementById(strID)) {
      if (i != intSize) {
        document.getElementById(strID).style.borderColor = "#CCCCCC";
      }
      else {
        document.getElementById(strID).style.borderColor = "#000000";
      }
    }
  }
}

function fncSetFontSizeDefault() {
  fncSetElementFontSize('idPrimaryContent', 12);
  if (typeof(fncSetCookie) == "function")
    fncSetCookie('fontsize', null, 'now', '/')
}
    

//---------------------------------------------------------------------------
// POPUP NAV FUNCTIONS

function fncNavPopupDelayHide() {
  oPopupTimer = setTimeout("fncNavPopupHide()",500);
}
function fncNavPopupCancelHide() {
  clearTimeout(oPopupTimer);
}

function fncNavPopupOver(oItem) {
  oItem.style.color = "#FFB800";
}
function fncNavPopupOut(oItem) {
  oItem.style.color = "#FFFFFF";
}

function fncNavPopupShow(strSection) {
  fncNavPopupCancelHide();
  if (!(isMac && isIE)) {
    // first argument is any horizontal offset for popup menu
    // second argument is any vertical offset for popup menu
    // third argument is specified width for popup menu
    if (arguments) {
      intMenuShiftX = 0;
      intMenuShiftY = 0;
      intMenuWidth = -1;
      if (arguments.length >= 2) {
        if (typeof(arguments[1]) == "number") {
          intMenuShiftX = arguments[1];
        }
      }
      if (arguments.length >= 3) {
        if (typeof(arguments[2]) == "number") {
          intMenuShiftY = arguments[2];
        }
      }
      if (arguments.length >= 4) {
        if (typeof(arguments[3]) == "number") {
          intMenuWidth = arguments[3];
        }
      }
      
      fncNavPopupHide();
      strNavActiveSection = strSection;
      
      if (typeof(document.getElementById('idNavButton_' + strSection)) != "undefined") {
        oPopupNavButton = document.getElementById('idNavButton_' + strSection);
        // disabled due to IE "flashing" if (!isIE) { oPopupNavButton.style.color = "#FFB800 !important"; }
      }
      
      if (typeof(document.getElementById('idNavPopup_' + strSection)) != "undefined" && document.getElementById('idNavPopup_' + strSection)) {
        oPopupNavMenu = document.getElementById('idNavPopup_' + strSection);
        oPopupNavMenu.style.display = "block";
        if (intMenuWidth > 0) {
          oPopupNavMenu.style.width = intMenuWidth + "px";
        }
        intLocationX = (fncGetAbsoluteX(oPopupNavButton)) + (intPopupMenuGlobalShiftX) + (intMenuShiftX);
        intLocationY = (fncGetAbsoluteY(oPopupNavButton)) + (intPopupMenuGlobalShiftY) + (intMenuShiftY);
        
        if (isIE) { intLocationY = intLocationY-11; intLocationX = intLocationX + 20; }
        
        oPopupNavMenu.style.left = intLocationX + "px";
        oPopupNavMenu.style.top = intLocationY + "px";
        fncSetNavPopupShadow('idNavPopup_' + strSection);
      }
    }
  }
}

function fncNavDelayHide() {
  fncNavPopupDelayHide();
}

function fncNavPopupHide() {
  if (strNavActiveSection.length > 0) {
    if (typeof(document.getElementById('idNavButton_' + strNavActiveSection)) != "undefined") {
      oPopupNavButton = document.getElementById('idNavButton_' + strNavActiveSection);
      // disabled due to IE "flashing" if (!isIE) { oPopupNavButton.style.color = "#FFFFFF"; }
    }
    if (typeof(document.getElementById('idShadowNavPopup')) != "undefined" && document.getElementById('idShadowNavPopup')) {
      document.getElementById('idShadowNavPopup').style.display = "none";
    }
    if (typeof(document.getElementById('idNavPopup_' + strNavActiveSection)) != "undefined" && document.getElementById('idNavPopup_' + strNavActiveSection)) {
      oPopupNavMenu = document.getElementById('idNavPopup_' + strNavActiveSection);
      oPopupNavMenu.style.display = "none";
    }
  }
}


//---------------------------------------------------------------------------
// UTILITY FUNCTIONS

function fncGetAbsoluteX(oObjectToGetPosition) {
  // Utility function to get the absolute X-coordinate of an object on the page
  var intCoords = {x: 0};
  while (oObjectToGetPosition) {
    intCoords.x += oObjectToGetPosition.offsetLeft;
    oObjectToGetPosition = oObjectToGetPosition.offsetParent;
  }
  return intCoords.x;
}
function fncGetAbsoluteY(oObjectToGetPosition) {
  // Utility function to get the absolute Y-coordinate of an object on the page
  var intCoords = {y: 0};
  while (oObjectToGetPosition) {
    intCoords.y += oObjectToGetPosition.offsetTop;
    oObjectToGetPosition = oObjectToGetPosition.offsetParent;
  }
  return intCoords.y;
}

function fncGetWidth(oObjectToGetWidth) {
  // Utility function to get the width of an object on the page
	if (oObjectToGetWidth.style.pixelWidth) {
		return oObjectToGetWidth.style.pixelWidth;
	}
  if (oObjectToGetWidth.style.width) {
      return oObjectToGetWidth.style.width;
  } 
  return oObjectToGetWidth.offsetWidth;
}

function fncGetHeight(oObjectToGetHeight) {
  // Utility function to get the height of an object on the page
	if (oObjectToGetHeight.style.pixelHeight) {
		return oObjectToGetHeight.style.pixelHeight;
	}
  if (oObjectToGetHeight.style.height) {
      return oObjectToGetHeight.style.height;
  }
  return oObjectToGetHeight.offsetHeight;
}


function fncAddClass(oElement, strClassName) {
  if (typeof(oElement) == "object" && strClassName.length > 0) {
    if (oElement.className) {  
      var strCurrentClassName = oElement.className;
      if (strCurrentClassName.indexOf(strClassName) < 0) {
        if (strCurrentClassName.length > 1) {
          strClassName = " " + strClassName;
        }        
        oElement.className = strCurrentClassName + strClassName;
      }
    }
  }
}

function fncRemoveClass(oElement, strClassName) {
  if (typeof(oElement) == "object" && strClassName.length > 0) {
    if (oElement.className) {
      var strCurrentClassName = oElement.className;
      if (strCurrentClassName.indexOf(strClassName) >= 0) {
        strNewClassName = fncRemoveClassRecursive(" " + strCurrentClassName + " ", " " + strClassName + " ");
        if (strNewClassName.indexOf(" ") == 0) { strNewClassName = strNewClassName.substring(1); }
        if (strNewClassName.lastIndexOf(" ") == strNewClassName.length-1) { strNewClassName = strNewClassName.substring(0,strNewClassName.length-1); }
        oElement.className = strNewClassName;
      }
    }
  }
}

function fncRemoveClassRecursive(strSource, strSubstrToRemove) {
  var intIndex = strSource.indexOf(strSubstrToRemove);
  var strReturn = "";
  if (intIndex == -1) return strSource;
  strReturn += strSource.substring(0,intIndex) + " " + fncRemoveClassRecursive(strSource.substring(intIndex + strSubstrToRemove.length), strSubstrToRemove);
  return strReturn;
}



//---------------------------------------------------------------------------
// COOKIE FUNCTIONS

function fncSetCookie (name, value, exp, path) {
 /*
    INPUT
      name (string) - name of the cookie
      value (string) - value of the cookie
      exp (string) - default is "never"
                     if "never" it sets exp to "Thu, 7 Dec 2113 01:00:00 UTC"
                     if "exp" it sets exp to "Fri, 13 Apr 1970 01:00:00 UTC"
                     if int (ie "5", "20", "60000") it translates to int hours from now
                     if valid GMT date then it uses that as exp
                     else it defaults to 840 hours (5 weeks)
      path (string)
 */
 if(exp == "") {
   exp = "never";
 }
 if (typeof(exp) == 'string') {
   if (exp == 'never') { 
     var strExp = "Thu, 7 Dec 2113 01:00:00 UTC";
   }
   else if (exp == 'exp' || exp == 'now') { 
     var strExp = "Fri, 13 Apr 1970 01:00:00 UTC";
   }
   else {
     if (Date.parse(exp)) {
      var strExp = exp;
     }
     else {
       exp = exp*1;
       if (isNaN(exp)) {
        var strExp = (new Date((new Date()).getTime() + 840*3600000)).toGMTString();
       }
       else {
        var strExp = (new Date((new Date()).getTime() + exp*3600000)).toGMTString();
       }
     }
   }
 }
 document.cookie = name + '=' + escape(value) + ((strExp)?(';expires=' + strExp):'') + ((path)?';path=' + path:'');
}



function fncReadCookie(name) {
 var strCookies = document.cookie;
 
 // find beginning of cookie value in document.cookie
 var prefix = name + "=";
 var begin = strCookies.indexOf("; " + prefix);
 if (begin == -1) {
   begin = strCookies.indexOf(prefix);
   if (begin != 0) return null;
 }
 else begin += 2;
 
 // find end of cookie value
 var end = document.cookie.indexOf(";", begin);
 if (end == -1) end = strCookies.length;
 
 // return cookie value
 return unescape(strCookies.substring(begin + prefix.length, end));
}



function fncKillCookie(name, path) {
 /*
    REQUIRED MODULES
      "fncReadCookie" function
      "fncSetCookie" function
 */
 fncSetCookie (name, null, 'now', path);
}


function fncKillAllCookies(path) {
 var strCookies = document.cookie;
 
 var astrCookies = document.cookie.split(";");
 
 for(var i=0;i<=astrCookies.length-1;i++) {
   var astrCookie = astrCookies[i].split("=");
   var strCookieName = astrCookie[0];
   if (i>0) {
     if (strCookieName.indexOf(" ") == 0) {
       // remove leading space
       strCookieName = strCookieName.substring(1,strCookieName.length)
     }
   }
   fncKillCookie(strCookieName, path);
 }
 alert('All cookies deleted for path ' + path);
}


//---------------------------------------------------------------------------
// PROTOTYPE FUNCTIONS

/* Non-IE browsers do not have the insertAdjacentElement function so we create it here */
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
