
































var M_IN_STOCK 			= "IN_STOCK";
var M_IN_STOCK_RETAIL	= "IN_STOCK_RETAIL";
var M_BACKORDERED		= "BACKORDERED";
var M_RE_ORDERED		= "RE_ORDERED";
var M_OUT_OF_STOCK	= "OUT_OF_STOCK";
var M_iMouseX				= 0;
var M_iMouseY				= 0;
var M_strGFileName	= "Global.js";
var M_bIsDropdownHidden			= false;
var M_bMenuItemShow 				= false;
var M_bIsPFWOpen 						= false;
var BrowserInfo 						= new BrowserDetect2();
var M_bIsPToP								= false;
var M_oPToPWin							= null;

// Track mouse position
SetTimeout( "document.onmousemove = GetMousePosition;", 1 );

/* disallow the page to be framed */
if (window.self != window.top) {
	try {
		top.location.replace( window.location.pathname + window.location.search);
	}
	catch( e ) {
		HandleError( "unframe window", e, M_strGFileName );
	}
}

/* javascript regex for illegal characters (anything not number, letter, underscore) - returns bool if it finds them*/
function HasIllegalChars( strVal ) {
	try {
			var illegalChars = /\W/;
			
			// allow only letters, numbers, and underscores
			return illegalChars.test(strVal);
	}
	catch( e ) {
		HandleError( "HasIllegalChars", e, M_strGFileName );
	}
}


/* Changes the visibility on our pop up boxes. */
function DisplayPopUp( strLayer, bShow ) {
	try {
		Element.setStyle(strLayer, {visibility: ( bShow ? "visible" : "hidden" )});
	}
	catch( e ) {
		HandleError( "DisplayPopUp", e, M_strGFileName );
	}
}

function IsEmpty( strVal ) {
	try {
		return ( strVal == null || strVal == "" || strVal == '' || TrimWhitespace(strVal) == "" );
	}
	catch( e ) {
		HandleError( "IsEmpty", e, M_strGFileName );
	}
}

function RemoveError() {
	try {
		if( $("errorGlobal") ) {
			document.getElementsByTagName("div")[0].removeChild($("errorGlobal"));
		}
	}
	catch( e ) {
		HandleError( "RemoveError", e, M_strGFileName );
	}
}

function HandleError( strFunction, err, strFile ) {
  try {
		alert( "Something is erroring: \n\nFile Name: " + strFile + "\nFunction Name: " + strFunction + "\nMessage: " + err + ", " + err.description + "\n\nRefreshing your browser window may fix things." );
	}
	catch( e ) {
		alert( "Something is erroring: \n\nFile Name: " + strFile + "\nFunction Name: " + strFunction + "\nMessage: " + err + ", " + err.description + "\n\nRefreshing your browser window may fix things." );
	}
}

/* Launches pop up window, does not return a reference */
function popUp( strURL, strWindowName, strFeatures ){
	try {
		popUpRef( strURL, strWindowName, strFeatures );
	} // End try
	catch( e ) {
	 HandleError( "popUp", e, M_strGFileName );
	}
}

/* Launches pop up window. Returns a reference */
function popUpRef( strURL, strWindowName, strFeatures ){
	try {
		new_window = window.open( strURL, strWindowName, strFeatures );
		new_window.focus();
		return new_window;
	} // End try
	catch( e ) {
	 HandleError( "popUp", e, M_strGFileName );
	}
}


// jump to next field, used on a number of pages for the phone field.
function autoNext( objStartTxtBox, objNextTxtBox ){
  try {
		if( objStartTxtBox != null ) {
			if ( objStartTxtBox.value.length == objStartTxtBox.getAttribute("maxlength") ) {
				objNextTxtBox.focus();
			} // End length check
		} // End if not null
	} // End try
	catch( e ) {
	 HandleError( "autoNext", e, M_strGFileName );
	}
}

/* Gets the coordinates of the 'off' image and positions the 'on' image, on top of it. */
function PositionPopUpBox( strImgOff, strImgOn, strDiv ) {
	var objCoordinates;
	
	try {
		objCoordinates = GetLayerAdjustmentCoordinates( strImgOff, strImgOn, strDiv );
		
		SetObjectPosition( strDiv, objCoordinates.x, objCoordinates.y );
	}
	catch( e ) {
		HandleError( "PositionPopUpBox", e, M_strPageName );
	}
}
			


/* Generic method of deciding where we need to position the 'on' image. */
function GetLayerAdjustmentCoordinates( strImgOff, strImgOn, strDiv ) {
	var objImgOff, objImgOn, objDivLayer, objReturn;
	
	try {
		objImgOff 		= GetObjectPosition( strImgOff );
		objImgOn 			= GetObjectPosition( strImgOn );
		objDivLayer 	= GetObjectPosition( strDiv );
		objReturn = new Object();
		objReturn.x = 0;
		objReturn.y = 0;
		
		// Find the spread between where the off image is and where we want it to be
		if( objImgOff.x > objImgOn.x ) {
			objReturn.x = ( objDivLayer.x + ( objImgOff.x - objImgOn.x ) );
		}
		else {
			objReturn.x = ( objDivLayer.x - ( objImgOn.x - objImgOff.x ) );
		}
		
		if( objImgOff.y > objImgOn.y ) {
			objReturn.y = ( objDivLayer.y + ( objImgOff.y - objImgOn.y ) );
		}
		else {
			objReturn.y = ( objDivLayer.y - ( objImgOn.y - objImgOff.y ) );
		}
		
	}
	catch( e ) {
		HandleError( "GetLayerAdjustmentCoordinates", e, M_strPageName );
	}
	
	return objReturn;
}

/* This function gets all of the dropdown menus on the page and hides them.  On pages where
we may have layers that need to sit on top of dropdowns, we show and hide the dropdowns before we
show and hide the layer by overriding a function pointer. */
function HideDropdowns() {
	try {
		
		var objSelects = document.getElementsByTagName( "select" );
		var nodes = $A(objSelects);
		
		nodes.each( function(node) { ShowObject( node, false );} );
		
		M_bIsDropdownHidden = true;
	}
	catch( e ) {
		HandleError( "HideDropdowns", e, M_strPageName );
	}
}


/* This function gets all of the dropdown menus on the page and shows them.  On pages where
we may have layers that need to sit on top of dropdowns, we show and hide the dropdowns before we
show and hide the layer by overriding a function pointer. */
function ShowDropdowns() {
	try {
		
		var objSelects = document.getElementsByTagName( "select" );
		var nodes = $A(objSelects);
		
		nodes.each( function(node) { ShowObject( node, true );} );
		
		M_bIsDropdownHidden = false;
	}
	catch( e ) {
		HandleError( "ShowDropdowns", e, M_strPageName );
	}
}

/* Adds a bookmark.  Grabs the present URL/Title*/
function BookmarkPage( strProduct ) {
	try {
		var strTitle = "";
		
		if( strProduct != null && strProduct.length > 0 ) {
			var strNewProduct = strProduct.replace(/\*/g,"'");
			strTitle = document.title + ": " + strNewProduct;
		}
		else {
			strTitle = document.title;
		}
		
		if( window.external ) {
			window.external.AddFavorite( document.location.href, strTitle  );
		}
		else if( window.sidebar ) {
			window.sidebar.addPanel( strTitle, document.location.href, "" );
		}
		else {
			alert( "Sorry.  Your browser does not support this functionality." );
		}
	}
	catch( e ) {
		HandleError( "BookmarkPage", e, M_strGFileName );
	}
}

/* Gets the Y scroll position */
function GetScrollPositionY() {
	var intReturn = 0;
	try {
		 if ( document.all )
      {
         if ( !document.documentElement.scrollTop ) {
         	intReturn = document.body.scrollTop;
				 }
         else {
         	intReturn = document.documentElement.scrollTop;
				 }
      }   
      else
      {
		 		intReturn = window.pageYOffset;
			}
	}
	catch( e ) {
		HandleError( "GetScrollPositionY", e, M_strGFileName );
	}
	return intReturn;
}

/* Gets the X scroll position */
function GetScrollPositionX() {
	var intReturn = 0;
	try {
		 if ( document.all )
      {
         if ( !document.documentElement.scrollLeft ) {
         	intReturn = document.body.scrollLeft;
				 }
         else {
         	intReturn = document.documentElement.scrollLeft;
				 }
      }   
      else
      {
		 		intReturn = window.pageXOffset;
			}
	}
	catch( e ) {
		HandleError( "GetScrollPositionY", e, M_strGFileName );
	}
	return intReturn;
}

/* Takes a currency string and checks it for '$'.  If it finds them, it removes them. */
function ConvertCurrencyToNumber( strInput ) {
	var strReturn;
	
	try {
		strReturn =  strInput.toString().replace( /$|\,/g, '' );
		
		// Its not a number, return 0
		if ( isNaN( strReturn ) ) {
			strReturn = 0.00;
		}
		
	}
	catch( e ) {
		HandleError( "ConvertCurrencyToNumber", e, M_strGFileName );
	}
	
	return parseFloat( strReturn );
}


/* Takes a 'number' and formats it US dollar style */
function FormatCurrency( objAmount )
{
	var flAmount, strMinus, strAmount;
	try {
		var flAmount = parseFloat( objAmount );
		
		if( isNaN( flAmount ) ) {
			flAmount = 0.00;
		}
		
		strMinus = "";
		if( flAmount < 0 ) {
			strMinus = "-";
		}
		
		flAmount = Math.abs( flAmount );
		flAmount = parseInt( (flAmount + .005) * 100 );
		flAmount = flAmount / 100;
		
		strAmount = new String( flAmount );
		if( strAmount.indexOf( "." ) < 0 ) {
			strAmount += ".00";
		}
		
		if( strAmount.indexOf( "." ) == ( strAmount.length - 2 ) ) {
			strAmount += "0";
		}
		
		strAmount = strMinus + strAmount;
	}
	catch( e ) {
		HandleError( "FormatCurrency", e, M_strGFileName );
	}
	return strAmount;
}

function OpenSlideshowL(id) {
	try {
		popUp('/web/us' + '/popup/media_gallery_photo_b.jsp?OPTION=SAR&assetid=' + id,'win','width=760,height=830,scrollbars=yes,resizable=yes');
	}
	catch(e) {
		HandleError( "OpenSlideshowL", e, M_strGFileName );
	}
}

/* Uses regex to trim whitespace */
function TrimWhitespace( strInput )
{
  var strReturn;
  try {
		if( strInput != null && strInput != ""){	
			strReturn =  strInput.toString().replace(/^\s+/g, '').replace(/\s+$/g, '');
		}
		else {
			strReturn = strInput;
		}
	}
	catch( e ) {
		HandleError( "TrimWhitespace", e, M_strGFileName );
	}
  return strReturn;
}

// Function validates email in the footer
function ValidateEmailSignUpFooter() {
	var bIsValid = false;
	try {
		var oFrm = $("emailForm");
		if(oFrm != null) {
			var oEmail = oFrm.getElementsByTagName("input")[0];
			if(oEmail != null) {
				bIsValid = ValidateEmail($F(oEmail));
				if( !bIsValid ) {
					alert("Invalid email address. Must be of the form: 'name@something.xyz'");
				} // end if not valid
			} // oemail != null
		} // end if ofrm != null
	}
	catch( e ) {
		HandleError( "ValidateSignUp", e, M_strPageName );
	}
	
	return bIsValid;
}

// Function validates email in the footer
function ValidateEmailSignUpSidebar() {
	var bIsValid = false;
	try {
		var oFrm = $("emailFormSide");
		if(oFrm != null) {
			var oEmail = oFrm.getElementsByTagName("input")[0];
			if(oEmail != null) {
				bIsValid = ValidateEmail($F(oEmail));
				if( !bIsValid ) {
					alert("Invalid email address. Must be of the form: 'name@something.xyz'");
				} // end if not valid
			} // oemail != null
		} // end if ofrm != null
	}
	catch( e ) {
		HandleError( "ValidateSignUp", e, M_strPageName );
	}
	
	return bIsValid;
}

// Validates Emails
function ValidateEmail( strVal )	
{
		var bIsValid = false;
		
		try {
			var emailRegex = new RegExp(/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/);
			bIsValid = emailRegex.test(TrimWhitespace(strVal));
		}
		catch( e ) {
			HandleError( "ValidateEmail", e, M_strGFileName );
		}
		
		return bIsValid;
}	

/* Positions an object. */
function SetObjectPosition( layer, intX, intY ) {
	try {
		var objLayer = GetObject( layer );
		
		if( objLayer != null ) {
			strX = intX + ( intX.toString().indexOf("px") == -1 ? "px" : "" );
			strY = intY + ( intY.toString().indexOf("px") == -1 ? "px" : "" );
			
			Element.setStyle(objLayer,{top: strY, left: strX });
		}
		
	}
	catch( e ) {
		HandleError( "SetObjectPosition" + e, M_strGFileName );
	}
}


/* Helper function to return baseline x coordinate parsed as int. */
function GetBasePositionX() {
	var objPos;
	try {
		objBase = GetBasePosition();
	}
	catch( e ) {
		HandleError( "GetBasePositionX" + e, M_strGFileName );
	}
	return parseInt( objBase.x );
}


/* Helper function to return baseline y coordinate parsed as int. */
function GetBasePositionY() {
	var objPos;
	try {
		objBase = GetBasePosition();
	}
	catch( e ) {
		HandleError( "GetBasePositionY" + e, M_strGFileName );
	}
	return parseInt( objBase.y );
}


/* For elements that we position absolutely, this function provides us the X and Y coordinate of the item
	that we use as our baseline for the topmost, leftmost item.  Presently its <div id="wrapper">*/
function GetBasePosition() {
	var objPos;
	
	try {
		objBase = $("wrapper");
		objPos = GetObjectPosition( objBase );
	}
	catch( e ) {
		HandleError( "GetBasePosition" + e, M_strGFileName );
	}
	return objPos;
}

/* Checks to see if we have an object or an object name.  If its a name, we get the object. */
function GetObject( layer ) {
		try {
			return ( typeof layer == "object" ? layer : $(layer) );
		}
		catch( e ) {
				HandleError( "GetObject", e, M_strGFileName );
		}
}

/* Changes an objects visibility to visible or hidden based on boolean. */
function ShowObject( layer, bIsVisible ) {
	try {
		var objLayer = GetObject( layer );
		
		if( objLayer != null ) {
			Element.setStyle(objLayer, {visibility: ( bIsVisible ? "visible" : "hidden" )});
		}
	}
	catch( e ) {
		HandleError( "ShowLayer", e, M_strGFileName );
	}
}

/* This function runs all the time - as a user mouses across the window.  We use it for lining up the options box with the holster
and potentially other usese in the future.  It sets global variables that let us access the x and y coordicates from our scripts. */
function GetMousePosition( e ) {
		
		try {
		if( window == null) alert('null');
		
		if (!e) var e = window.event;
		
		M_iMouseX = Event.pointerX(e);
		M_iMouseY = Event.pointerY(e);
		
	}
	catch( er ) {
		// This thing tweaks in IE when the page loads - swallow the error
		//HandleError( "GetMousePosition", er, M_strGFileName );
	}
}

/* Wrapper for javascript time out method. */
function SetTimeout( strCodeToRun, flTimeInSeconds ) {
	try {
		var flLimit = parseFloat( flTimeInSeconds ) * 1000;
		setTimeout( strCodeToRun, flLimit ); 
	}
	catch( e ) {
		HandleError( "SetTimeout", e, M_strGFileName );
	}
}

/* Gets the position(x,y) of an object in the window.  Makes adjustments for browsers.  Returns values as obj.x and obj.y */
function GetObjectPosition( target ) {
	
	var x = 0;
	var y = 0;
	var tempObj = new Object();
	try {
		var obj = GetObject( target );
	
		if( obj != null ) {
			while ( obj.offsetParent != null ) {
				x += obj.offsetLeft;
				y += obj.offsetTop;
				obj = obj.offsetParent;
			}
			
			x += obj.offsetLeft;
			y += obj.offsetTop;
		}
		
		tempObj.x = parseInt( x );
		tempObj.y = parseInt( y );
	}
	catch( e ) {
		HandleError( "GetObjectPosition", e, M_strGFileName );
	}
	return tempObj;
}

/* Gets broswer size.*/
function GetBrowserSize() {
  var intWidth, intHeight;
	var objReturn = new Object();
	
	try {
		intWidth = 0;
		intHeight = 0;
		
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			intWidth = window.innerWidth;
			intHeight = window.innerHeight;
		}
		else if( document.documentElement &&
				( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			intWidth = document.documentElement.clientWidth;
			intHeight = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			intWidth = document.body.clientWidth;
			intHeight = document.body.clientHeight;
		}
		else {}
		
		objReturn.x = intWidth;
		objReturn.y = intHeight;
	}
	catch( e ) {
		HandleError( "GetBrowserSize", e, M_strGFileName );
	}
	return objReturn;	
}

function GetQuerystringParam( strParam ) {
	var arrQueryParams;
	var strReturn = "";
	
	try {
		// Get first param from query string
		arrQueryParams = window.location.search.substring(1).toQueryParams();
		strReturn =  arrQueryParams[strParam];
		
		return ( strReturn == null ? "" : strReturn );
	}
	catch ( e ) {
		ShowError( "GetQuerystringParam", e, M_strGFileName );
	}
}


/* Helper class for traversing nodes.  Originally written for parsing xml but can be used for html as well.*/
function XmlHelper( xml ) {
		var objXml = xml;
		
		// Gets attribute
		this.getAttribute = function( element, strAttName ) {
			var objElement;
			objElement = GetElementObj( element );
			
			return objElement.getAttribute( strAttName );
		}
		
		// Gets the text value of an element node
		this.getElementValue = function( element ) {
			var strValue, objElement;
			objElement = GetElementObj( element );
			
			strValue = "";
		
			if( objElement != null ) {
				strValue = objElement.firstChild.nodeValue;
			}
			
			return strValue;
		}
		
		// Gets a pointer to an element object
		this.getElement = GetElement;
		function GetElement( strElementName ) {
			return objXml.getElementsByTagName( strElementName )[0];
		}
		
		// Gets a collection of xml elements
		this.getElementSet = function( strElementName ) {
			return objXml.getElementsByTagName( strElementName );
		}
		
		/* Gets element type
			Element Node: 					1
			Attribute Node: 				2
			Text Node: 							3
			Comment Node: 					8
			Document Node: 					9
			Document Fragment Node: 11
			*/
		this.getElementType = function( element ) {
			var objElement = GetElementObj( element );
			
			return objElement.nodeType;
		}
		
		// Gets pointer to element object
		function GetElementObj( element ) {
			return ( typeof element == "object" ? element : GetElement( element ) );
		}
		
		// Is it an element node?
		function IsElementNode( element ) {
			return ( this.getElementType( element )  == 1 );
		}
		
		// Gets the true number of child nodes of type element
		function GetElementChildCount( xmlElement ) {
			var iCount, i;
			iCount = 0;
			
			if( xmlElement != null ) {
				for(i=0; i < xmlElement.childNodes.length; i++) {
					
					// Mozilla counts blank space as a child node.  Check for non-text nodes, only
					if( IsElementNode( xmlElement.childNodes[i]) ) {
						iCount++;
					}
				} // End Loop
			} // End if not null

			return iCount;
		}
		
		// Gets the name of the element
		function GetElementName( element ) {
			return ( typeof element == "object" ? element.nodeName : element );
		}
		
		// Gets element child count
		this.getChildCount = function( element ) {
			var objElement = GetElementObj( element );
			
			return GetElementChildCount( objElement );
		}
	} // End XmlHelper Object
	

/**********************************************************************
To use this in code, simply type: BrowserInfo.isIE() or BrowserInfo.hdnShowPFW() etc...
Code is optimized to only fetch the value one time.
************************************************************************/
function BrowserDetect2() {
	var _strFrameName 				= "ifrHolsterInfo";
	var _objIE 								= new BrowserProperty( "hdnIsIE", _strFrameName );
	var _objFF 								= new BrowserProperty( "hdnIsFF", _strFrameName );
	var _objSafari 						= new BrowserProperty( "hdnIsSafari", _strFrameName );
	var _objShowPFW 					= new BrowserProperty( "hdnShowPFW", _strFrameName );
	var _bOverrideShowPFW			= false;
	var _bShowPfwOverride			= false;
	
	
	this.isIE = function() {
		return _objIE.isOfType();
	}
	
	this.isFF = function() {
		return _objFF.isOfType();
	}
	
	this.isSafari = function() {
		return _objSafari.isOfType();
	}
	
	this.showPFW = function() {
		if( _bOverrideShowPFW ) {
			return _bShowPfwOverride;
		}
		else {
			return _objShowPFW.isOfType();
		}
	}
	
	this.setOverridePFW = function( bVal ) { _bOverrideShowPFW = bVal; }
	this.setShowPFWOverride = function( bVal ) { _bShowPfwOverride = bVal; }
	
}


/* BrowserProperty class*/
function BrowserProperty( strField, strFrame ) {
	var _bIsSet 				= false;
	var _bIsType 				= false;
	var _strFieldName 	= strField;
	var _strFrameName 	= strFrame;
	
	this.isOfType = function() {
		
		_bIsType = this.getHiddenBrowserVal();
		return _bIsType;

	}
	
	this.getHiddenBrowserVal = function() {
		var bReturnVal 			= false;
		var ifrHolsterInfo 	= window.frames[_strFrameName];

		if ( ifrHolsterInfo != null ) {
			if( ifrHolsterInfo.document.getElementById( _strFieldName ) ) {
				bReturnVal = Boolean( parseInt( ifrHolsterInfo.document.getElementById( _strFieldName ).value ) );
				_bIsSet = true;
			}
			else {
				bReturnVal = false;
			}
		}
		return bReturnVal;
	}
}

//Macromedia common functions
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
