/* Hide javascript errors from the end user */
function noalert(){
	return true;
}
//window.onerror=noalert;

/* Browser Detection */
var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op;

d = document;
n = navigator;
na = n.appVersion;
nua = n.userAgent;
win = ( na.indexOf( 'Win' ) != -1 );
mac = ( na.indexOf( 'Mac' ) != -1 );
lin = ( nua.indexOf( 'Linux' ) != -1 );

if ( !d.layers ){
	dom = ( d.getElementById );
	op = ( nua.indexOf( 'Opera' ) != -1 );
	konq = ( nua.indexOf( 'Konqueror' ) != -1 );
	saf = ( nua.indexOf( 'Safari' ) != -1 );
	moz = ( nua.indexOf( 'Gecko' ) != -1 && !saf && !konq);
	ie = ( d.all && !op );
	ie4 = ( ie && !dom );

	/*
	ie5x tests only for functionality. ( dom||ie5x ) would be default settings.
	Opera will register true in this test if set to identify as IE 5
	*/

	ie5x = ( d.all && dom );
	ie5mac = ( mac && ie5x );
	ie5xwin = ( win && ie5x );
}

/* Trim whitespace from right and left */
function trimAll(sString) {
	while (sString.substring(0,1) == ' ') { sString = sString.substring(1, sString.length); }
	while (sString.substring(sString.length-1, sString.length) == ' ') { sString = sString.substring(0,sString.length-1); }
	return sString;
}

/* Calculate page size including scrollable width and height */
function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX - 16;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
		}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

/* Calculate page size of viewable page */
function getPageSize() {
	var xWidth = 0, yHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		xWidth = window.innerWidth - 20;
		yHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		xWidth = document.documentElement.clientWidth;
		yHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		xWidth = document.body.clientWidth;
		yHeight = document.body.clientHeight;
	}
	arrayPageSize = new Array(xWidth,yHeight);
//  window.alert( 'Width = ' + myWidth );
//  window.alert( 'Height = ' + myHeight );
	return arrayPageSize;
}

/* Calculate scrolled amount */
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

/* Find absolute position of an element */
function findPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != 'undefined' ) {
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
		return [ posX, posY ];
	} else {
		return [ oElement.x, oElement.y ];
	}
}

/* Image mouseovers for top navigation */
var imgprefix = 'v3_topnav_';
function navimgon(imgid) {
	if (typeof imgon != 'undefined') { document.getElementById('ni_' + imgon).src = "/gifs/new/" + imgprefix + imgon + "_on.gif"; }
	if ((typeof imgon == 'undefined' ? '' : imgon) != imgid) {
		document.getElementById('ni_' + imgid).src = "/gifs/new/" + imgprefix + imgid + "_over.gif";
	}
}
function navimgoff(imgid) {
	document.getElementById('ni_' + imgid).src = "/gifs/new/" + imgprefix + imgid + "_off.gif";
	if (typeof imgon != 'undefined') { document.getElementById('ni_' + imgon).src = "/gifs/new/" + imgprefix + imgon + "_on.gif"; }
}

/* Form input validation functions */
function IsBlank(s) {
	if (s.length==0) return true;
	for(var i=0;i<s.length;i++) {
		var c=s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}
function IsDollarAmount(s) {
	var validstr='^[\$]?[0-9]{1,7}[.]?[0-9]{0,2}$';
	return s.match(validstr);
}

function ValidateCardAmount(v) {
	var validstr='^[0-9]{1,3}$';
	if (v.match(validstr)) {
		if (!((parseInt(v)>=5) && (parseInt(v)<=500))) { alert('Card amount must be between 5 and 500.'); return false; }
	} else { alert('Card amount must be a whole dollar amount between 5 and 500. Enter digits only.'); return false; }
	return true;
}

/* Add standard URL parameters to ajax query strings */
function AddOsUrlParms(q) {
	if (typeof OsUrlParms == 'undefined') { return q; }
	if (OsUrlParms != '') {
		var separator = (q.indexOf( '?' ) == -1) ? '?' : '&';
		return q + separator + OsUrlParms; //OsUrlParms found in footer.xhtml
	}
	return q;
}

/* Popup a new window */
function popWin(url,w,h,attrstring) {
	if (attrstring=='all') {attrstring='toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,'}
	attr_wh = 'width=' + w + ',height=' + h + ',outerwidth=' + w + ',outerheight=' + h;
	ospw=window.open(url,"",attrstring + attr_wh);
	return false;
}
function popUp(url) {
	ospw=window.open(url,"",'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=800,height=600,outerwidth=800,outerheight=600');
	return false;
}

/* Slide down Guarantee info for the SKU and Category Pages */
function guaranteeOn() {
	$('#guarantee_off').hide();
	if (ie) {
		$('#guarantee_on').show();
	} else {
		$('#guarantee_on').slideToggle('slow');
	}
	return false;
}
function guaranteeOff() {
	if (ie) {
		$('#guarantee_on').hide();
		$('#guarantee_off').show();
	} else {
		$('#guarantee_on').slideToggle('slow',function(){
			$('#guarantee_off').show();
		});
	}
	return false;
}

/* General use modal popup functions */
function showModalPopup() {
	if (ie) { $('select').hide(); }
	$('#Blanket').css('width', getPageSizeWithScroll()[0] + 'px');
	$('#Blanket').css('height', getPageSizeWithScroll()[1] + 'px');
	$('#Blanket').css('backgroundPosition',Math.max(0,getScrollXY()[0] + (getPageSize()[0]-50)/2) + 'px ' + Math.max(0,getScrollXY()[1] + (getPageSize()[1]-50)/2) + 'px');
	$('#Blanket').show();
	$('#modalPopup').css('visibility','hidden');
	$('#modalPopup').show("fast",function(){
		$('#modalPopup').css('left', Math.max(getScrollXY()[0],getScrollXY()[0] + (getPageSize()[0] - $('#modalPopup')[0].offsetWidth)/2) + 'px');
		$('#modalPopup').css('top', Math.max(getScrollXY()[1],getScrollXY()[1] + (getPageSize()[1] - $('#modalPopup')[0].offsetHeight)/2) + 'px');
		$('#modalPopup').css('visibility','visible');
	});
	return false;
}
function hideModalPopup() {
	$('#Blanket').hide();
	$('#modalPopup').hide();
	if (ie) { $('select').show(); }
	return false;
}
function showTipPopup(tipID,anchor) {
  $("#modalPopup").html($("#" + tipID).html()); 
  if (ie) { $('select').hide(); }
	$('#modalPopup').css('visibility','hidden');
  $('#modalPopup').show("fast",function(){
	  $('#modalPopup').css('left', Math.max(getScrollXY()[0], (findPosition(anchor)[0] - 2 - $('#modalPopup')[0].offsetWidth)) + 'px');
	  $('#modalPopup').css('top', Math.max(getScrollXY()[1], (findPosition(anchor)[1] + 5 - $('#modalPopup')[0].offsetHeight/2)) + 'px');
	  $('#modalPopup').css('visibility','visible');
  });
  return false;    
}


/* Calculate Delivery ajax form in left navigation */
function OSCalculateDelivery(theform) {
	theform.zip.value = trimAll(theform.zip.value);
	if (theform.zip.value == 'Enter Zip Code') {return false;}
	if (theform.zip.value == '') {return false;}
	var queryString = "/servlet/OnlineShopping?" + $(theform).serialize();
	$('#CalcDeliveryDiv').load(AddOsUrlParms(queryString), { force: 'post' }, function(data){
	});
	return false;
}

/* Find the parent form of the element sent */
function FindParentForm(element) {
	var parent_form = element;
	while (parent_form.tagName.toLowerCase() != "form") {
		parent_form = parent_form.parentNode;
		if (!parent_form.tagName) { return null; }
	}
	return parent_form;
}
