// $Id: common.js 4480 2010-05-14 02:43:18Z erik $

//pop up window
function doPop(filename,w,h,s,r){
	var opts = "width=" + w + ",height=" + h + ",status=no,scrollbars=" + s + ",resizable=" + r + ",directories=no,location=no,menubar=no,toolbar=no,screenX=50,screenY=50,top=50,left=50";
	window.open(filename,"popWin",opts);
}

//pop up window
function doPop2(filename,winname,w,h,s,r){
	var opts = "width=" + w + ",height=" + h + ",status=no,scrollbars=" + s + ",resizable=" + r + ",directories=no,location=no,menubar=no,toolbar=no,screenX=65,screenY=65,top=65,left=65";
	window.open(filename,winname,opts);
}

//pop up window secure
function doPopSecure(filename,w,h,s,r){
	var opts = "width=" + w + ",height=" + h + ",status=yes,scrollbars=" + s + ",resizable=" + r + ",directories=no,location=no,menubar=no,toolbar=no,screenX=50,screenY=50,top=50,left=50";
	window.open(filename,"popWin",opts);
}

//round any remainders to X number of decimal places
function doRound(num,X) {
	return Math.round(num*Math.pow(10,X))/Math.pow(10,X);
}

//round numeric to next integer
function doShowInt(x) {
	return Math.ceil(x);
}

//format values to #.00 or #.#0, accordingly
function doShowMoney(num){
	var num = doRound(jsCleanNum(num),2);
	var amount = num.toString();
	var newdollars = '';
	if (amount.indexOf('.') != -1) { var dollars = amount.substring(0,amount.indexOf('.')); }
	else { var dollars = amount; }
	//places , for thousands & millions
	if (dollars.length > 3)
	{
		last = dollars.substring(dollars.length-3,dollars.length)
		rest = dollars.substring(0,dollars.length-3);
		if (rest.length > 3)
		{
			middle = rest.substring(rest.length-3,rest.length)
			if (rest.length > 3)
			{
				rest = rest.substring(0,rest.length-3)
				output = rest+','+middle+','+last;
			}
			else { output = middle+','+last; }
		}
		else
		{
			output = rest+','+last;
		}
		newdollars = output;
	}
	else { newdollars = dollars; }
	if (amount.indexOf('.') != -1){ var cents = amount.substring(amount.indexOf('.'),amount.length); }
	else { var cents='.00' }
	if (cents.length < 3)
	{
		if (cents.length == 2){cents=cents+'0';}
		else {cents=cents+'00';}
	}
	return newdollars+cents+'';
}

//format values to #.00, #.#0, or #.##0 accordingly
function doShowMoney3(x){
	var x = jsCleanNum(x);
	x = doRound(x,3);
	var str1 = x.toString();
	var str1length = str1.length;
	var str1index = str1.indexOf('.') + 1;
	if (str1index == 0){
		return(str1 + '.00');
	}
	else{
		if (str1index == str1length - 1){
			return(str1 + '00');
		}
		else{
			if (str1index == str1length - 2){
				return(str1 + '0');
			}
			else{
				if (str1index == str1length - 3){
					return(str1);
				}
			}
		}
	}
}

function doStrReverse(string){
	var newstring = "";
	for (i=0; i<string.length; i++){
		newstring = string.charAt(i) + newstring;
	}
	return (newstring);
}

function jsCheckCookie()
{
	var tmpcookie = new Date();
	chkcookie = (tmpcookie.getTime() + '');
	document.cookie = "chkcookie=" + chkcookie + "; path=/";
	if (document.cookie.indexOf(chkcookie,0) < 0) {
		//cookies disabled
		alert('You must have cookies enabled to perform this action.');
		return false;
    	}
	else {
		//cookies enabled
		return true;
  	}
}
function jsCheckCookie()
{
	var tmpcookie = new Date();
	chkcookie = (tmpcookie.getTime() + '');
	document.cookie = "chkcookie=" + chkcookie + "; path=/";
	if (document.cookie.indexOf(chkcookie,0) < 0)
	{
		//cookies disabled
		alert('You must have cookies enabled to perform this action.');
		return false;
	}
	else
	{
		//cookies enabled
		return true;
	}
}

function jsCheckVisible(obj)
{
	element = document.getElementById(obj).style;
	if (element.display == 'none') return false;
	else return true;
}
function jsCleanNum(num)
{
	var newstring = "";
	string = num+'';
	for (i=0; i<string.length; i++)
	{
		if (!isNaN(string.charAt(i)) || string.charAt(i)=='.') newstring += string.charAt(i);
	}
	var result = parseFloat(newstring);
	if (!isNaN(result)) return result;
	else return 0;
}
function jsHideElement(obj)
{
	//alert('hide' + obj);
	element = document.getElementById(obj).style;
	element.display = 'none';
}
function jsPopUp(url,w,h,scroll)
{
	var menubar=0;
	if (url.indexOf('XLS=1') != -1) { menubar = 1; }
	if (scroll != 'yes') { scroll = 'no'; }
	var features = 'toolbar=no,location=no,directories=no,menubar='+menubar+',scrollbars='+scroll+',resizable=yes,top=20,left=20';
	if (url.substring(0,5) == 'https') { features += ',status=yes'; }
	else { features += ',status=no'; }
	if (h != '') { features += ',height='+h; }
	if (w != '') { features += ',width='+w; }
	window.open(url,'pop'+window.name,'"'+features+'"');
}
function jsShowElement(obj)
{
	//alert('show' + obj);
	element = document.getElementById(obj).style;
	if (document.getElementById(obj).tagName == 'TR')
	{
		if (navigator.appName != "Microsoft Internet Explorer")element.display = 'table-row';
		else element.display = '';
	}
	else element.display = '';
}
function jsToggleElement(obj)
{
	element = document.getElementById(obj).style;
	element.display == 'none' ? element.display = '' : element.display='none';
}

function jsPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters))
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }
}
function jsChangeCSS(theClass,element,value) {
	var selector = '';
	var cssRules = document.all?'rules':'cssRules';
	for (var i=0; i < document.styleSheets.length; i++){
		for (var r = 0; r < document.styleSheets[i][cssRules].length; r++) {
			selector = document.styleSheets[i][cssRules][r].selectorText+'';
			selector = selector.toLowerCase();
			if (selector == theClass) { document.styleSheets[i][cssRules][r].style[element] = value; }
		}
	}
}

if (navigator.appName == 'Microsoft Internet Explorer'){
	window.attachEvent("onload", jsPNG);
}

function jsGetQuerystring(key) {
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(parent.document.location.href);
  if(qs == null)
    return '';
  else
    return qs[1];
}

//sales gui floater
function jsFloat(id, sx, sy)
{
  var ns = (navigator.appName.indexOf("Netscape") != -1);
  var px = document.layers ? "" : "px";

//used only for center-aligned screens
/*
	var winW = 0;
	var winH = 0;
	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  winW = window.innerWidth;
	  winH = window.innerHeight;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winW = document.body.offsetWidth;
	  winH = document.body.offsetHeight;
	 }
	}

	sx = winW/2+sx;
*/

	var obj=document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
	window[id + "_obj"] = obj;
	if (document.layers) obj.style = obj;
	obj.cx = obj.sx = sx;
	obj.cy = obj.sy = sy;
	obj.sP = function(x,y){
		this.style.left = x+px;
		this.style.top = y+px;
	}
	obj.flt = function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth :
		document.documentElement && document.documentElement.clientWidth ?
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ?
		document.documentElement.scrollTop : document.body.scrollTop;
		if ( this.sy <0 ) pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/4;
		this.cy += (pY + this.sy - this.cy)/4;
		this.sP(this.cx, this.cy);
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.flt()", 40);
	}
	return obj;
}

function jsPopImage(pline,alias,pid){
	var url = "/catalog/popProductImage.asp?ProductLineID=" + pline + "&IsAlias=" + alias + "&ProductID=" + pid + "&BlurClose=1";
	jsPopUp(url);
}