// The first thing we need to do is add the Array.push() method for those browsers
// that do not support it (IE5.0 on Windows, and IE 5.1 on Mac -- THANKS Microsoft!)
function isUndefined(property) {
    return (typeof property == 'undefined');
}

// Array.push() - Add an element to the end of an array
if (isUndefined(Array.push) == true) {
    Array.prototype.push = function() {
        var currentLength = this.length;
        for (var i = 0; i < arguments.length; i++) {
            this[currentLength + i] = arguments[i];
        }
        return this.length;
    };
}

// Make sure the "FM" namespace object exists
if (typeof FM != 'object') {
    FM = new Object();
}

/**
 * Displays the specified URL in a new window with all menus, tools, and scrollbars
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   url             URL to display in the popup window
 * @param   width           (Optional) The width of the new popup window
 * @param   height          (Optional) The height of the new popup window
 * @param   windowName      (Optional) The name of the window
 */
FM.popup = function(url, width, height, windowName) {
    // Default to a 700 x 500 window
    width = (!isNaN(width) ? width + '' : '700');
    height = (!isNaN(height) ? height + '' : '500');
    windowName = (windowName == undefined || windowName == '' ? 'popup' : windowName);

    // Create it
    var popUp = window.open(url, windowName, 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');

    // Bring it to the front
    if (typeof popUp == 'object') { popUp.focus(); }

}  // popup()

/**
 * Displays the specified URL in a new window with NO menus or tools, but with scrollbars
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   url             URL to display in the popup window
 * @param   width           (Optional) The width of the new popup window
 * @param   height          (Optional) The height of the new popup window
 * @param   windowName      (Optional) The name of the window
 */
FM.popupNoTools = function(url, width, height, windowName) {
    // Default to a 700 x 500 window
    width = (!isNaN(width) ? width + '' : '700');
    height = (!isNaN(height) ? height + '' : '500');
    windowName = (windowName == undefined || windowName == '' ? 'popup' : windowName);

    // Create it
    var popUp = window.open(url, windowName, 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=no,toolbar=no,location=no');

    // Bring it to the front
    if (typeof popUp == 'object') { popUp.focus(); }

}  // popupNoTools()

/**
 * Displays the specified URL in a new window with NO menus, tools OR scrollbars
 *
 * @author  Dan Delaney     http://fluidmind.org/
 * @param   url             URL to display in the popup window
 * @param   width           (Optional) The width of the new popup window
 * @param   height          (Optional) The height of the new popup window
 * @param   windowName      (Optional) The name of the window
 */
FM.popupNoScroll = function(url, width, height, windowName) {
    // Default to a 700 x 500 window
    width = (!isNaN(width) ? width + '' : '700');
    height = (!isNaN(height) ? height + '' : '500');
    windowName = (windowName == undefined || windowName == '' ? 'popup' : windowName);

    // Create it
    var popUp = window.open(url, windowName, 'width=' + width + ',height=' + height + ',scrollbars=no,resizable=no,menubar=no,toolbar=no,location=no');

    // Bring it to the front
    if (typeof popUp == 'object') { popUp.focus(); }

}  // popupNoScroll()

function windowOnLoad(f) { var prev=window.onload; window.onload=function(){ if(prev)prev(); f(); }}

function popupVideo(path) {
  window.open("/popups/video-popup.asp?path="+path,'Multimedia_Center','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=no,scrollbars=no,width=320,height=285,screenX=90,screenY=90,top=90,left=90');
}

function popSurvey() { 
	FM.popupNoTools('/sitesurvey/',650,500,'sitesurvey');
}

function RotateBanner() {
    //alert("Banner" + currentBannerNumber);
    var currentBanner = document.getElementById("Banner" + currentBannerNumber).style;
    currentBannerNumber++;
    if(currentBannerNumber > numberOfBanners) { currentBannerNumber = 1; }
    var nextBanner = document.getElementById("Banner" + currentBannerNumber).style;

    currentBanner.display = 'none';
    nextBanner.display = 'inline';
}

function OpenFootnotes(footnoteNumber) {
    var footnoteWindow = window.open("/footnotes.asp#footnote" + footnoteNumber,"footnotes","toolbar=no,location=no,directories=no,status=no,menubars=no,scrollbars=yes,resizable=no,width=450,height=350");
    footnoteWindow.focus();
}

function BYBPopup(term) {
    var popUp = window.open('/popups/byb-popup.asp?term=' + term.toLowerCase(), 'bybpopup', 'width=500,height=250,scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    popUp.focus();
}

function AccessoriesPopup(pid,aid) {
    var popUp = window.open('/popups/accessory-popup.asp?pid='+pid+'&aid='+aid, 'accessorypopup', 'width=530,height=430,scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    popUp.focus();
}

function ProdInfoPopup(sessionid) {
    var popUp = window.open('prod_info_popup.asp?session='+sessionid, 'prodinfo', 'width=450,height=600,scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    //popUp.focus();
}

function ProdPagePopup(id) {
    var popUp = window.open('/products/overview.asp?pid='+id, 'prodinfo', 'width=600,height=500,scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    //popUp.focus();
}

function ResourcePopup(path) {
    var popUp = window.open(path, 'resource', 'width=400,height=500,scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    popUp.focus();
}

function SerialLocatePopup(product) {
    var popUp = window.open('/popups/seriallocate-' + product + '.asp', 'seriallocate', 'width=640,height=500,scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    popUp.focus();
}

function ViewAnimation(filename, width, height) {
    var popUp = window.open('/popups/animation-popup.asp?filename=' + filename, 'animation', 'width=' + width + ',height=' + height + ',scrollbars=no,resizable=yes,menubar=no,toolbar=no');
    popUp.focus();
}

function popupWindow(uri, width, height) {
    var popUp = window.open(uri, 'popup', 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=no,toolbar=no');
    popUp.focus();
}

function popupScrollWindow(uri, width, height) {
    var popUp = window.open(uri, 'popup', 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes');
    if (typeof popUp == 'object') { popUp.focus(); }
}

function popPromo() {
	popupWindow("/popups/2006promo.asp",600,400);
}

function PopUpload(formName,UploadPath, fileName) {
  var n = window.open("upload.asp?field=" + formName + "&path=" + UploadPath ,'uploader','toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,copyhistory=no,scrollbars=no,width=320,height=285');
}

// This function is used to make sure that required form fields have values.
//    Field_Name: name of a form field in the form of document.form_name.form_field_name that will be validated.
//    Error_Message: this is the alert error message that will be displayed to the user.
function Has_Value(Field_Name, Error_Message) {
	var text;

	text = Field_Name.value;

	while(text.search(/\s/) > -1) {
        text = text.replace(/\s/, "");
    }

    if(text == 0) {
        alert(Error_Message);
        Field_Name.focus();
        return false;
    }
    return true;
}

/**
 * Checks a string containing an email address and makes sure that it's valid
 *
 * @param string value  An email address to be checked for validity
 * @return boolean
 */
function checkEmail(value) {
    var result = false;

    // First, check for normal (non-quoted) email addresses
    // The username part of the RegEx is from Breaking Par (www.breakingpar.com)
    // For the domain part, I put an arbitrary but absurdly reasonable
    // limit of 6 sub-domains after the top level domain
    var check = value.match(/^([^<>()[\]:;@\\,"\s.]+(\.[^<>()[\]:;@\\,"\s.]+)*)@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]\.){1,6}[a-zA-Z]{2,64}$/);

    if (check != null) {
        // This is an arbitrary but reasonable limit of 100 characters for the local-part (username)
        if (check[1].length <= 100) {
            result = true;
        }
    } else {
        // If it didn't match that, see if the local-part is a quoted-string
        // (again, with a limit of 100 characters)
        check = value.match(/^"(.{1,100})"@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]\.){1,6}[a-zA-Z]{2,64}$/);
        if (check != null) {
            if (check[1].search(/[^\\]"/) == -1) {
                result = true;
            }
        }
    }

    return(result);
} // checkEmail()

// IS_VALID_EMAIL( emailaddy )
// Returns TRUE if Email is valid and FALSE if NOT VALID
function Is_Valid_Email( emailaddy ){
    var tempchar;       // temp holder
    var atPos;          // string position holder
    var periodPos;      // position of the period in string
    var invalchar = "/:,;"; // not valid email characters
    // if the email string is empty return false
    //if( Is_Empty( emailaddy ) ){ return false; }
    // Loop through each invalid character
    for( count = 0; count < invalchar.length; count++ ){
        // Pull one character out of the string
        tempchar = invalchar.charAt( count );
        // If an invalid character is found return false
        if( emailaddy.indexOf( tempchar, 0 ) > -1 ){
            return false;
        }
    }
    // find and store the position of the at sign
    atPos = emailaddy.indexOf( "@", 1 );
    // If no at sign is in the string return false
    if( atPos == -1 ){ return false; }
    // If at sign is the last character in the string return false
    if( emailaddy.indexOf( "@", atPos + 1 ) > -1 ){ return false; }
    // Find and store the position of the period in the email string
    periodPos = emailaddy.indexOf(".", atPos);
    // If there is no period in the string return false
    if( periodPos == -1 ){ return false; }
    // If the period is less than 3 characters from
    // the end of the string return false
    if( periodPos + 3 > emailaddy.length ){ return false; }
    // all is well this is a valid email address so return true
    return true;
}
// END OF FUNCTION

function Is_Valid_Date(Field_Name, Error_Message)
	//This function will validate a form date field to make sure that its in the proper
	//format.  It will accept dates in the following format: mm/dd/yyyy || m/dd/yyyy || m/d/yy ||
	// 	m/dd/yy || mm/d/yy || any valid date that is delemitted by the '/' symbol.
	//Field_Name: name of a form field in the form of document.form_name.form_field_name that will be validated.
	//Error_Message: this is the alert error message that will be displayed to the user.
	{
		if (!Is_Date(Field_Name, Error_Message))
			{
			 alert(Error_Message);
			 Field_Name.focus();
			 return false;
			}
		//everything passed so...
		return true;
	}

	function Is_Date(Field_Name)
	    {
	    //Returns true if value is a date format or is NULL
	    //otherwise returns false

	    if (Field_Name.value.length == 0)
	        return true;

	    //Returns true if value is a date in the mm/dd/yyyy format
	        isplit = Field_Name.value.indexOf('/');

	        if (isplit == -1 || isplit == Field_Name.value.length)
	                {
					 return false;
					}

	    sMonth = Field_Name.value.substring(0, isplit);

	        if (sMonth.length == 0)
	        {
			 return false;
			}

	        isplit = Field_Name.value.indexOf('/', isplit + 1);

	        if (isplit == -1 || (isplit + 1 ) == Field_Name.value.length)
	                {
					 return false;
					}

			sDay = Field_Name.value.substring((sMonth.length + 1), isplit);

	        if (sDay.length == 0)
	        {
			 return false;
			}

	        sYear = Field_Name.value.substring(isplit + 1);

	        if (!_CF_checkinteger(sMonth)) //check month
	                {
					 return false;
					}
	        else
	        if (!_CF_checkrange(sMonth, 1, 12)) //check month
	                {
					 return false;
					}
	        else
	        if (!_CF_checkinteger(sYear)) //check year
	                {
					 return false;
					}
	        else
	        if (!_CF_checkrange(sYear, 0, 9999)) //check year
	                {
					 return false;
					}
	        else
	        if (sYear.length != 4)
				{
				return false;
				}
	        if (!_CF_checkinteger(sDay)) //check day
	                {
					 return false;
					}
	        else


	        if (!_CF_checkday(sYear, sMonth, sDay)) // check day
	                {
					 return false;
					}
	        else
	                return true;
	    }

function _CF_checkday(checkYear, checkMonth, checkDay)
    {

        maxDay = 31;

        if (checkMonth == 4 || checkMonth == 6 ||
                        checkMonth == 9 || checkMonth == 11)
                maxDay = 30;
        else
        if (checkMonth == 2)
        {
                if (checkYear % 4 > 0)
                        maxDay =28;
                else
                if (checkYear % 100 == 0 && checkYear % 400 > 0)
                        maxDay = 28;
                else
                        maxDay = 29;
        }

        return _CF_checkrange(checkDay, 1, maxDay); //check day
    }

function _CF_checkinteger(Field_Name)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false

    if (Field_Name.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
        var decimal_format = ".";
        var check_char;

    //The first character can be + -  blank or a digit.
        check_char = Field_Name.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
        return _CF_checknumber(Field_Name);
    else
        {
		 return false;
		}
    }

function _CF_numberrange(Field_Name, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
        {
        if (Field_Name < min_value)
                {
				 return false;
				}
        }

    // check maximum
    if (max_value != null)
        {
        if (Field_Name > max_value)
                {
				 return false;
				}
        }

    //All tests passed, so...
    return true;
    }

function _CF_checknumber(Field_Name)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false

    if (Field_Name.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
        var start_format = " .+-0123456789";
        var number_format = " .0123456789";
        var check_char;
        var decimal = false;
        var trailing_blank = false;
        var digits = false;

    //The first character can be + - .  blank or a digit.
        check_char = start_format.indexOf(Field_Name.charAt(0))
    //Was it a decimal?
        if (check_char == 1)
            decimal = true;
        else if (check_char < 1)
                {
				 return false;
				}

        //Remaining characters can be only . or a digit, but only one decimal.
        for (var i = 1; i < Field_Name.length; i++)
        {
                check_char = number_format.indexOf(Field_Name.charAt(i))
                if (check_char < 0)
                        {
						 return false;
						}
                else if (check_char == 1)
                {
                        if (decimal)            // Second decimal.
                                {
								 return false;
								}
                        else
                                decimal = true;
                }
                else if (check_char == 0)
                {
                        if (decimal || digits)
                                trailing_blank = true;
        // ignore leading blanks

                }
                else if (trailing_blank)
                        {
						 return false;
						}
                else
                        digits = true;
        }
    //All tests passed, so...
    return true
    }

function _CF_checkrange(Field_Name, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (Field_Name.length == 0)
        return true;


    if (!_CF_checknumber(Field_Name))
        {
         return false;
        }
    else
        {
        return (_CF_numberrange((eval(Field_Name)), min_value, max_value));
        }

    //All tests passed, so...
    return true;
    }

function ValidatePostal() {
	if( (document.LocateForm.reqzip.value.length < 5) || (document.LocateForm.reqzip.value.length > 7) || (document.LocateForm.reqzip.value == 'enter postal code') ) {
		alert('Please enter your 5-digit US ZIP code.');
		document.LocateForm.reqzip.focus();
		return false;

	}
	else return true;
}

function getRef(obj){
  if(typeof obj == "string") {obj= document.getElementById(obj);}
  else if (typeof obj == "object") {obj = obj;}

  return obj;
}

function toggleMenu(elementId) {
    if(getStyle(elementId, 'display') != 'block') {
        show(elementId);
    } else {
        hide(elementId);
    }
    return false;
}

function setStyle(obj, style, value){
  getRef(obj).style[style]= value;
}

function getStyle2(obj, style){
  return getRef(obj).style[style];
}

function getStyle(obj, style){
  obj = getRef(obj);
  var y;
  if (window.getComputedStyle)
    var y = window.getComputedStyle(obj,null).getPropertyValue(style);
  else if (obj.currentStyle)
    var y = eval('obj.currentStyle.' + style);
  return y;
}

function move(elem,x,y) {
  getRef(elem).style.left = x + "px";
  getRef(elem).style.top = y + "px";
}

function hide(elem)
{
  setStyle(elem,'display','none');
  setStyle(elem,'visibility','hidden');
}

function show(elem)
{
    setStyle(elem,'display','block');
    setStyle(elem,'visibility','visible');
}

function showhide(elem)
{
  if (getStyle(elem,'visibility') == "visible") {
  	hide(elem);
  } else { show(elem); }

}

function getCheckBoxValue(obj) {
	var message = '';
	for (i = 0; i < obj.length; i++) {
	    if (obj[i].checked) {
		message += obj[i].value + ', ';
	    }
	}

	return message;
}

function getRadioButtonValue(obj) {
	for (i = 0; i < obj.length; i++) {
		if (obj[i].checked) {
			return obj[i].value;
		}
	}
}

retVal = true;

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
  }
}

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;
}

sfHover = function() {
	if (document.getElementById("nav")) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(/sfhover/, "");
			}
		}
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnloadEvent(func) {
  var oldunload = window.onbeforeunload;

  if (typeof window.onbeforeunload != 'function') {
    window.onbeforeunload = func;
  } else {
    window.onbeforeunload = function() {
    oldonunload();
    func();
    }
  }

  var oldunload = window.onunload;

  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
    oldonunload();
    func();
    }
  }

}

if (window.attachEvent) window.attachEvent("onload", sfHover);
//addLoadEvent(sfHover);
