var BASE_HREF = '';
if(document.getElementsByTagName('base')[0] !== undefined) {
	BASE_HREF = document.getElementsByTagName('base')[0].href;
}

function darkEn(){
	document.getElementById('q').style.color = '#000000';
	if (document.formH.q.value=="Product ID or Keyword"){document.formH.q.value='';}
}
function lightEn(){
if (document.formH.q.value === "")	{
	document.formH.q.value="Product ID or Keyword";
	document.getElementById('q').style.color = '#666666';
	}
}
	
(function($){
	
		$.fn.alphanumeric = function(p) { 

		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- _",
			nchars: "",
			allow: "",
			callback: function(){return false;}
		  }, p);	

		return this.each
			(
				function() 
				{

					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');

					$(this).keypress
						(
							function (e)
								{
								
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
									
									if (ch.indexOf(k) == -1) p.callback(this);
								}
								
						);
						
					$(this).bind('contextmenu',function () {return false;});
									
				}
			);

	};

	$.fn.numeric = function(p) {
	
		var az = "abcdefghijklmnopqrstuvwxyz";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);	
		  	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};
	
	$.fn.alpha = function(p) {

		var nm = "1234567890";

		p = $.extend({
			nchars: nm
		  }, p);	

		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};	

	$.fn.clearSelect = function() {
		return this.each(function() {
			if (this.tagName == 'SELECT') {
				this.options.length = 0;
			}
		});
	};
	
	$.fn.fillSelect = function(data) {
		return this.clearSelect().each(function() {
			if (this.tagName == 'SELECT') {
				var dropdownList = this;
				$.each(data, function(index, optionData) {
					var option = new Option(optionData.text, optionData.value);
										
					if ($.browser.msie) {
						dropdownList.add(option);
					} else {
						dropdownList.add(option, null);
					}
				});
			}
		});
	};

	$.fn.luxe_menu = function(off,delay){
		var time = 250;
		var hideDelay = 100;
		var beingShown = false;
		
		var showDelayTimer = null;		
		var hideDelayTimer = null;

		// tracker
		var shown = false;
		var originaltop = null;
		
		if (delay < 1) {
			delay = 0;
		}
			
		$(off).mouseover(function() {
			if (hideDelayTimer) {clearTimeout(hideDelayTimer);}
		}).mouseout(function(){			
			if (hideDelayTimer) {clearTimeout(hideDelayTimer);}
			
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				shown = false;
				//$(off).hide("slow");
				$(off).slideUp("slow");
			}, hideDelay);			
		}).hide();

		return this.each(function() {
			$(this).mouseover(function(){
				// stops the hide event if we move from the trigger to the popup element
				if (hideDelayTimer) {clearTimeout(hideDelayTimer);}
				if (showDelayTimer) {clearTimeout(showDelayTimer);}

				if($(off).is(":animated")) return;

				// don't trigger the animation again if we're being shown, or already visible
				if (beingShown || shown) {
					return;
				} else {
					showDelayTimer = setTimeout(function() {
						beingShown = true;
						$(off).css("top",originaltop).show();
		
						var h = $(off).offset(),
							aa = $(off).height(),
							oh = $(window).height();
						if(h) {
							b = _getPageScrollTop();							
							if ((h.top+aa) > (oh+b[1])) {					
								var diff = (oh - (h.top+aa)), top = $(off).css('top');
								originaltop = top;
								move = (parseInt(top,10)+diff-5+b[1])+"px";
								$(off).css("top",move);
							}
						}
						//$(off).hide().show("slow");
						$(off).hide().slideDown("slow");
						//$(off).show("slow");
						beingShown = false;
						shown = true;
					}, delay);
				}
			}).mouseout(function(){
				// reset the timer if we get fired again - avoids double animations
				if(beingShown) {return;}
				if (hideDelayTimer) {clearTimeout(hideDelayTimer);}
				if (showDelayTimer) {clearTimeout(showDelayTimer);}

				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					shown = false;					
					//$(off).hide("slow");		
					$(off).slideUp("slow");
				}, hideDelay);
			});
		});
		
	};

	$.fn.luxe_cascadingmenu = function(){	
		return this.each(function() {
			$('ul:has(li)',this).addClass('submenu');
			$('li:has(ul)',this).each(function() {
				$(this).addClass('li-with-ul').luxe_menu($('ul:first',this)[0],300);
			});
		});
	};

	$.fn.rollover = function(over){		
		return this.each(function() {
			var active = [];
			var orig = [];
			var roll = [];

			if(typeof over == 'string') {
				orig = this.src;
				$("<img>").attr('src',over);
				$(this).hover(function(){this.src =over;},function(){this.src=orig;});
			} else if (typeof over[0] == 'string') {
				active = $(over[0])[0];
				orig = active.src;
				roll = over[1];
				$("<img>").attr("src",over[1]);
				
				$(this).hover(function(){
					active.src=roll;
				},function(){
					active.src=orig;
				});					
			} else if (typeof over == 'object') {
				for (var i=0; i<over.length; i++) {
					active[i] = $(over[i][0])[0];
					orig[i] = active[i].src;
					roll[i] = over[i][1];
					$("<img>").attr("src",over[i][1]);
				}
				$(this).hover(function(){
					for(var i=0;i<active.length;i++) {
						active[i].src=roll[i];
					}
				},function(){
					for(var i=0;i<active.length;i++) {
						active[i].src=orig[i];
					}
				});					
			}
		});
	};
	

	$.fn.center = function(params) {
	
			var options = {
				vertical: true,
				horizontal: true
			},
			op = jQuery.extend(options, params);
	
	   return this.each(function(){
	
			//initializing variables
			var $self = jQuery(this);
			//get the dimensions using dimensions plugin
			var width = $self.width();
			var height = $self.height();
			//get the paddings
			var paddingTop = parseInt($self.css("padding-top"));
			var paddingBottom = parseInt($self.css("padding-bottom"));
			//get the borders
			var borderTop = parseInt($self.css("border-top-width"));
			var borderBottom = parseInt($self.css("border-bottom-width"));
			//get the media of padding and borders
			var mediaBorder = (borderTop+borderBottom)/2;
			var mediaPadding = (paddingTop+paddingBottom)/2;
			//get the type of positioning
			var positionType = $self.parent().css("position");
			// get the half minus of width and height
			var halfWidth = (width/2)*(-1);
			var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
			// initializing the css properties
			var cssProp = {
				position: 'absolute'
			};
	
			if(op.vertical) {
				cssProp.height = height;
				cssProp.top = '50%';
				cssProp.marginTop = halfHeight;
			}
			if(op.horizontal) {
				cssProp.width = width;
				cssProp.left = '50%';
				cssProp.marginLeft = halfWidth;
			}
			//check the current position
			if(positionType == 'static') {
				$self.parent().css("position","relative");
			}
			//aplying the css
			$self.css(cssProp);
	
	
	   });
	};
	
	$.extend($, {
		preloadImages: function (src, options) {
			if (typeof src === 'object') {
				$.each(src, function () {
					$.preloadImages(String(this), options);
				});
			}
		
			var image = new Image();

			options = options || {};
			
			$.each(['load', 'error', 'abort'], function () { // Callbacks
				var e = String(this);
				if (typeof options[e] === 'function') { $(image)[e](options[e]); }
			});
			
			image.src = src;
			
			return image;
		}
	});
 
   $.extend($.fn, {
     preloadImages: function (options) {
       return this.each(function () {
         $.preloadImages(this.src, options);
       });
     }
   });
	
})(jQuery);

_getPageScrollTop = function(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
	  yScrolltop = self.pageYOffset;
	  xScrollleft = self.pageXOffset;
	} else if(document.documentElement && document.documentElement.scrollTop || 
			  document.documentElement.scrollLeft ) {   // Explorer 6 Strict
	  yScrolltop = document.documentElement.scrollTop;
	  xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
	  yScrolltop = document.body.scrollTop;
	  xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = [xScrollleft,yScrolltop];
	return arrayPageScroll;
};

function underC_freeFreight(){
	window.open("/phase/freeShipping.html","pv","height=415,width=830,scrollbars,resizable");
}

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY);

}

function Rcertify() {
	popupWin = window.open('https://www.bbbonline.org/cks.asp?id=10303251215530818','Participant','location=yes,scrollbars=yes,width=450,height=300'); 
	window.name = 'opener';
}
	
$(function() {
	$("#formH").submit(function() {
		if(this.q.value=="Product ID or Keyword") {
			return false;
		}
	});
	
	$("button.goto").click(function() {
		var href = $(this).attr('href');
		if(href.startsWith('/')) {
			document.location = href;
		} else {
			document.location = BASE_HREF+href;
		}
	});
});

function wOpen(){
	window.open("/mailboxes/bacova/priceGty.html" ,"pv","width=420,height=380,resizable");
}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str);};

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str);};

String.prototype.trim = function()
{return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));};

function writeCookie(name, value, hours) {

  var expire = "";
  if(hours !== null) {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

function readCookie(name) {
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) {end = document.cookie.length;}
      cookieValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return cookieValue;
}

function giftCard(siDe,cartID){
	var currentTime = new Date(),
		month = currentTime.getMonth() + 1,
		day = currentTime.getDate(),
		holiDay="";

	switch(month) {
		case 1:
			if (day<2)holiDay="NEWYEARS";
			if (day>20)holiDay="VALENTINES";
			break;
		case 2:
			if (day<15)holiDay="VALENTINES";
			if (day>14 && day<19)holiDay="PRESIDENTS";
			break;
		case 3:
			if (day<18)holiDay="STPATRICK";
			break;
		case 10:
			if (day<9)holiDay="COLUMBUS";
			if (day>8)holiDay="HALLOWEEN";
			break;
		case 11:
			if (day<23)holiDay="THANKSGIVING";
			if (day>22)holiDay="CHRISTMAS";
			break;
    	case 12:
			if (day<26)holiDay="CHRISTMAS";
			if (day>24)holiDay="NEWYEARS";
			break;
		default:
			holiDay="";
	}

	if (holiDay>"" && holiDay!="undefined")	{
		holiDay=holiDay+"gift-card"+siDe;
	}else{
		holiDay="gift-card"+siDe;
	}

	document.writeln('<a href="/info/gift_cert.php"><img src="/images_seasonal/'+holiDay+'.jpg" border="0"></a>');
	
} // end of function