// Function used to swap the images in the header.
// Used by everypage
var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
var isNS4 = (document.layers) ? true : false;
var isIEmac = ((document.all)&&(isMac)) ? true : false;
var isIE4plus = (document.all) ? true : false;
var isIE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
var isIE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
var isIE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
var isIE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? true : false;
var isNS6 = ((!document.layers)&&(navigator.userAgent.indexOf('Netscape')!=-1)) ? true : false;
var isVer6 = (isIE6) || (isNS6);
var isIE = ((document.all)&&(navigator.userAgent.indexOf('MSIE') != -1));
var isNS = (isNS4) || (isNS6);
var isGecko = (navigator.userAgent.indexOf('Gecko') != -1);
var isMozilla = ((isGecko)&&(!isNS)&&(!isIE)&&(navigator.userAgent.indexOf('Mozilla')!=-1));
var isOpera = (navigator.userAgent.indexOf('Opera')!=-1);
var isDOM = (((isIE)&&(!isIE4)) || (isNS6) || (isMozilla) || (isOpera));
var isIE5up = ((isIE4plus)&&(!isIE4));
var isIE6up = ((isIE4plus)&&(!isIE4)&&(!isIE5));

function changeImages(img, newimg) {
	if (document.images) {
		document.images[img].src = eval(newimg + ".src");
	}
}

function openWin(winName, loc, width, height) {					// Opens a new window
	var newWindow;
	newWindow = window.open(loc, winName, "location=no,status=no,scrollbars=no,toolbar=no,menubar=no,directories=no,resizable=no,width="+width+",height="+height);		
	newWindow.focus();	
}

function openWinFull(loc) {					// Opens a new window
	var newWindow;
	newWindow = window.open(loc);		
	newWindow.focus();	
}

function openWinScroll(winName, loc, width, height) {					// Opens a new window
	var newWindow;
	newWindow = window.open(loc, winName, "location=no,status=yes,scrollbars=yes,toolbar=no,menubar=no,directories=no,resizable=yes,width="+width+",height="+height);		
	newWindow.focus();	
}

function openWinPrintable(winName, loc, width, height) {
	var newWindow;
	newWindow = window.open(loc, winName, "location=yes,status=yes,scrollbars=yes,toolbar=yes,menubar=yes,directories=no,resizable=yes,width="+width+",height="+height);		
	newWindow.focus();	
}

// Cross browser object finder
function getElement(id) {
	if (document.all) {
		return document.all(id);
	} else if (document.getElementById) {
		return document.getElementById(id);		//works IE5+ and NS6+
	} else {
		return findLayer(id, document);				
	}
}

function changecursor(obj, icon) {
	switch (icon) {
		case 'hand':	/* Fall through to the next one */
		case 'pointer':
			obj.style.cursor = (isIE && !isIE6up) ? 'hand' : 'pointer';
			break;
		case 'auto':
			obj.style.cursor = 'default';
			break;
		default:
			obj.style.cursor = icon;
			break;
	}
}

function findLayer(name, doc) {
	var i, layer;

	for (i = 0; i < doc.layers.length; i++) {
		layer = doc.layers[i];
		
		if (layer.name == name) {
			return layer;
		
			if (layer.document.layers.length > 0) 
				layer = findLayer(name, layer.document);
		
			if (layer != null)
				return layer;
		}
	}
	return null;
}

// Matches any integer only
function isValidInteger(test) {
	test = test.toString();
	if (isNaN(parseInt(test,10))) {
		return false;
	}
	else {
		// Trim the string
		test=test.replace(/^\s+/g, '');
		test=test.replace(/\s+$/g,'');
					
		var result = test.match(/^[-+]?\d+$/g);
		if (result == null) {
			return false;
		}
		else if (result[0] == test) {
			return true;
		}
		else {
			return false;
		}
	}
}
// Matches any positive integer only
function isValidPositiveInteger(test) {
	test = test.toString();
	if (isNaN(parseInt(test,10))  || parseInt(test, 10) == 0) {
		return false;
	}
	else {
		// Trim the string
		test=test.replace(/^\s+/g, '');
		test=test.replace(/\s+$/g,'');
					
		var result = test.match(/^\d+$/g);
		if (result == null) {
			return false;
		}
		else if (result[0] == test) {
			return true;
		}
		else {
			return false;
		}
	}
}


// Matches positive (> 0) floating point numbers only
function isValidDecimal(test) {
	test = test.toString();
	if (isNaN(parseFloat(test)) || parseFloat(test) == 0) {
		return false;
	}
	else {
		// Trim the string (left and right)
		test=test.replace(/^\s+/g,'');
		test=test.replace(/\s+$/g,'');
					
		var result = test.match(/^(\d+\.?\d*)|(\d*\.?\d+)$/);
		if (result == null) {
			return false;
		}
		else if (result[0] == test) {
			if (parseFloat(test) == 0)
				return false;
			else
				return true;
		}
		else {
			return false;
		}
	}
}
function isValidPositiveDecimal(test) {
	test = test.toString();
	if (isNaN(parseFloat(test)) || parseFloat(test) == 0) {
		return false;
	}
	else {
		// Trim the string
		test=test.replace(/^\s+/g, '');
		test=test.replace(/\s+$/g,'');
					
		var result = test.match(/^[-+]?(\d+\.?\d*)|(\d*\.?\d+)$/);
		if (result == null) {
			return false;
		}
		else if (result[0] == test) {
			if (parseFloat(test) == 0)
				return false;
			else
				return true;
		}
		else {
			return false;
		}
	}
}


function formatdecimal(num) {
	var left, right, str;
	var tmp1 = '';
	var tmp2 = '';
	var commas = 0;
	
	str = Math.round(num * 100).toString();
	left = str.substring(0, str.length - 2);
	right = str.substring(str.length - 2, str.length);
	
	commas = parseInt((left.length / 3).toString(),10);
	tmp1 = left;
	for (var i=0;i<commas;i++) {
		tmp2 = ((tmp1.length!= 3) ? ',' : '') + tmp1.substring(tmp1.length - 3, tmp1.length) + tmp2;
		tmp1 = tmp1.substring(0, tmp1.length - 3);
	}
	
	left = tmp1 + tmp2;	
	
	if (left.length == 0)
		left = '0';		// Make sure we have a leading zero
	if (right.length < 1)
		right = '00';
	else if(right.length < 2)
		right = '0' + right;
		
	return left + '.' + right;
}	

function formatcurrency(num) {
	var left, right, str;
	var tmp1 = '';
	var tmp2 = '';
	var commas = 0;
	
	str = Math.round(num * 100).toString();
	left = str.substring(0, str.length - 2);
	
	// Put commas after every 3rd number
	commas = parseInt((left.length / 3).toString(),10);
	tmp1 = left;
	for (var i=0;i<commas;i++) {
		tmp2 = ((tmp1.length!= 3) ? ',' : '') + tmp1.substring(tmp1.length - 3, tmp1.length) + tmp2;
		tmp1 = tmp1.substring(0, tmp1.length - 3);
	}
	
	left = tmp1 + tmp2;
	right = str.substring(str.length - 2, str.length);

	if (left.length == 0)
		left = '0';		// Make sure we have a leading zero
	if (right.length < 1)
		right = '00';
	else if(right.length < 2)
		right = '0' + right;
		
	return '$' + left + '.' + right;
}

function InchesToMillimeters(value) {
	return value * 25.4;
}

function MillimetersToInches(value) {
	return value / 25.4;
}


function roundup(value, decimals) {
	var tmp;
	
	tmp = parseFloat(value);
	if (isNaN(tmp)) {
		return tmp;
	}
	
	tmp = Math.ceil(tmp * Math.pow(10, decimals)).toString();
	tmp = tmp / Math.pow(10, decimals);
	return tmp;
}

function viewImages(type, item, img) {
	var url = '/public/images.aspx?type=' + type + '&item=' + item + '&img=' + URLencode(img);
	openWinScroll('Images', url, 680, 670, true);
}


function closeWindow() {
	if(window.opener != null) {
		var p = window.opener;
		p.focus();
	}
	window.close();
}	

function closeAndChangeParent(id, url) {
	if(window.opener != null) {
		var p = window.opener;
		window.opener.location = url;
		getElement(id).style.cursor = 'wait';
		setTimeout('closeWindow()', 1000)
	} else {
		window.location = url;
	}
}

function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function OpenerURL() {
	//Returns the RELATIVE URL
	if (window.opener != null) {
		return window.opener.location.pathname + window.opener.location.search;
	} else {
		return window.location.pathname + window.location.search;
	}
}

// USE: <textarea id="instructions" cols="1" rows="2" onkeypress="return maxWidth(this, event, 200)">
function maxWidth(obj, e, maxlength) {
	var code;
	if (window.event) code = window.event.keyCode;
	else if (e)	code = e.which;
	else code = null;
	
	// Certain control keys like delete and backspace should always be allowed
	if ( code==null || code==0 || code==8 || code==9 || code==13 || code==27 ) return true;
	
	if (obj.value.length < maxlength) return true;
	else return false;
}

function trim(str) {
   if(!str) {
      return str;
   } else {
      var test = str.replace(/^\s+/g, '');
		test=test.replace(/\s+$/g,'');
		return test;
   }   
}


/*
// USE: <INPUT onKeyPress="window.status = getkey(event)">
function getkey(e)
{
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}

// USE: <INPUT NAME=INT onKeyPress="return goodchars(event,'0123456789')">
function goodchars(e, goods)
{
var key, keychar;
key = getkey(e);
if (key == null) return true;
// get character
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
goods = goods.toLowerCase();
// check goodkeys
if (goods.indexOf(keychar) != -1)
return true;
// control keys
if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
return true;
// else return false
return false;
}
*/

/* Truncates a specified number of decimal places */
function truncate_decimal(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.floor(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals)
}

/*********************************************************/
/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/


function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString();
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".");

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length;
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}
/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/
/*********************************************************/


function cleartextbox(objname, str) {
   try {     
      var os = getElement(objname);
      if (os.length) {        /* the search pages have more than 1 control with a name of "q", this makes getElement return an array */
         os = os(0);
      }
      var test = trim(os.value.toLowerCase());        
      if(test==str.toLowerCase()) {
         os.value = '';
      }
   } catch(err) {
      /*Ignore it*/
   }
}
function filltextbox(objname, str) {
   try {
      var os = getElement(objname);
      if (os.length) {        /* the search pages have more than 1 control with a name of "q", this makes getElement return an array */
         os = os(0);
      }
      var test = trim(os.value.toLowerCase());
      if(test==''||test==str.toLowerCase()) {
         os.value = str;
      }
   } catch(err) {
      /* Ignore it */
   }
}

function hilighttextbox(objname) {
  try {
    var tb = getElement(objname);
    tb.select();
  } catch(err) {
    /* Ignore it */
  }
}

/*
** This is a useful function that will show you all properties of an object 
** USE: alert(show_props(obj, 'any name')); 
*/
function show_props(obj, obj_name) { 
    var result = "" 
    for (var i in obj) 
        result += obj_name + "." + i + " = " + obj[i] + "\n" 
    return result; 
} 

/****************************************************/
//This code is for the my Account popup menu in the header

var timer = null;
var gobj = null;
        
function popup(show) {
	var obj = document.getElementById('mouseOverMenu');
	if(show) {
		clearTimeout(timer);
		obj.style.display = 'block';
	} else {
		gobj = obj;
		timer = setTimeout('hideobj(\'mouseOverMenu\')',500);
	}
}
	    	
function hideobj(objname) {
	var obj = document.getElementById(objname);
	obj.style.display = 'none';
	clearTimeout(timer);
} 

var yft = {
   _shade: {1:'ff', 2:'ee', 3:'dd', 4:'cc', 5:'bb', 6:'aa', 7:'99'},
   fadeIn: function(e,i,basecolor) { 
      /* basecolor is the color to reset to after the fade finishes */
      if (i >= 1) {
         var elem = $get(e);
         if (elem) elem.style.backgroundColor = '#ffff' + this._shade[i];
         else return;

         if (i > 1) {
            i -= 1;
            if(basecolor!=null) 
               setTimeout("yft.fadeIn('"+elem.id+"',"+i+",'"+basecolor+"')", 200);
            else
               setTimeout("yft.fadeIn('"+elem.id+"',"+i+")", 200);
         } else {
            i -= 1;
            if(basecolor!=null) {
               setTimeout("yft.fadeIn('"+elem.id+"',"+i+",'"+basecolor+"')", 200);
               elem.style.backgroundColor = basecolor;
            } else {
               setTimeout("yft.fadeIn('"+elem.id+"',"+i+")", 200);            
               elem.style.backgroundColor = "transparent";
            }
         }
      }
   },
   customFadeIn: function(e,i,colors,delay, initialdelay) { 
      if (i >= 0) {
         var elem = $get(e);
         var ca = eval(colors); // Convert to an actual array
         if (elem) elem.style.backgroundColor = ca[i];
         else return;

         if (i > 0) {
            i -= 1;
            if (initialdelay>0) {
              setTimeout("yft.customFadeIn('"+elem.id+"',"+i+",'"+colors+"',"+delay+",0)", initialdelay);
            } else {
              setTimeout("yft.customFadeIn('"+elem.id+"',"+i+",'"+colors+"',"+delay+",0)", delay);
            }
         } 
      }      
   }   
}

/* Used to change the cursor to an hourglass and back again */
function changeCursor(cursor_style) {
   if(cursor_style.toLowerCase() == 'hand' || cursor_style.toLowerCase() == 'pointer') {
      document.body.style.cursor = (isIE) ? 'hand' : 'pointer';   // 'hand' works with IE 5+
   } else {
      document.body.style.cursor = cursor_style;
   }
}

/* Embed the proper Google Analytics code to track recommendation add to carts */
function CallPageTracker(itemid)
{
   try{
      pageTracker._trackEvent('Recommendations', 'AddToCart', itemid);
   }
   catch(err){
   }
}

/*
This is a sample routine to attach a focus and blur event to all input boxes on a page
*/

/*
  function fnTXTFocus(varname) {
    var objTXT = document.getElementById(varname)
    objTXT.style.borderColor= "#FFFFCC";
  }

  function fnTXTLostFocus(varname) {
    var objTXT = document.getElementById(varname)
    objTXT.style.borderColor = "#000000";
  }

  function fnOnLoad() {
    var t = document.getElementsByTagName('input');
    var i;
    for(i=0;i<t.length;i++) {
        if(t[i].type == "text") {
            t[i].attachEvent('onfocus', new Function("fnTXTFocus('"+t[i].id+ "')"));
            t[i].attachEvent('onblur', new Function("fnTXTLostFocus('"+t[i].id+ "')"));
        }
    }
  }
*/    