// $Id: common.js 13 2011-11-01 19:07:06Z erik $

// ======================
// most common functions
// ======================

// redirect
function go(url) { document.location.href=url; }

// pop-up window
function pop(url, wname, w, h, s)
{
	var mb = (url.indexOf('XLS=1') != -1) ? 1 : 0;
	var sb = (s == 'yes' || s == '1') ? 'yes' : 'no';
	var https = (url.substring(0,5) == 'https') ? 'yes' : 'no';
	var features = '"toolbar=no,location=no,directories=no,menubar='+mb+',scrollbars='+sb+',resizable=yes,top=20,left=20,status='+https;
	if (h != '') { features += ',height='+h; }
	if (w != '') { features += ',width='+w; }
	features += '"';
	window.open(url, (wname != '') ? wname : 'pop'+window.name, features);
}
// pop-up window { previous name }
function jsPopUp(url, w, h, s) { pop(url, '', w, h, s); }


// ======================
// old functions
// ======================

//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);
}

// check for date
function isDate(value) {
	var d = Date.parse(value);
	return (d > 0);
} 

function jsTextAreaInsert(sFieldId, sValue) {
	// get element
	sField = document.getElementById(sFieldId);
	// ie
	if (document.selection) {
		sField.focus();
		sSelection = document.selection.createRange();
		sSelection.text = sValue;
	}
	// moz
	else if (sField.selectionStart || sField.selectionStart == '0') {
		// init variables
		var iStart = sField.selectionStart;
		var iEnd = sField.selectionEnd;
		var iScroll = sField.scrollTop;
		// insert text
		sField.value = sField.value.substring(0, iStart) + sValue + sField.value.substring(iEnd, sField.value.length);
		// reset indexes
		sField.selectionStart = sField.selectionEnd = iStart + sValue.length;
		sField.scrollTop = iScroll;
	}
	// default
	else { sField.value += sValue; }
	// select element
	sField.focus();
}

function jsCheckCookie()
{
	var tmpcookie = new Date();
	chkcookie = (tmpcookie.getTime() + '');
	document.cookie = "chkcookie=" + chkcookie + ";path=/";
	if (document.cookie.indexOf(chkcookie,0) < 0) { alert('You must have cookies enabled to perform this action.');return false; }
	else { 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; }
}

// hide form element
function jsHideElement(obj)
{
	element = document.getElementById(obj).style;
	element.display = 'none';
}

// show form element
function jsShowElement(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 = ''; }
}

// toggle form element
function jsToggleElement(obj)
{
	element = document.getElementById(obj).style;
	element.display == 'none' ? element.display = '' : element.display='none';
}

// clear element
function jsResetElement(obj)
{
	element = document.getElementById(obj);
	switch (element.type)
	{
		case "text":
		case "textarea":
		case "password":
			element.value = '';
			break;
			
		case "select-one":
			//element.selectedIndex = -1;
			element.selectedIndex = 0;
			break;
			
		case "checkbox":
		case "radio":
			// need to iterate thru all radio buttons/checkboxes
			break;
			
		default:
			break;
	}
}

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--;
				}
			}
	 }
}
if (navigator.appName == 'Microsoft Internet Explorer'){ window.attachEvent("onload", jsPNG); }

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; }
		}
	}
}

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);
}

function jsTrim(s) {
 s = s.replace(/(^\s*)|(\s*$)/gi,"");
 s = s.replace(/[ ]{2,}/gi," ");
 s = s.replace(/\n /,"\n");
 return s;
}
