var color = ""
var pageName=null;
var isVar = null;
var jsColor = "";

function preload_image(file) {         
	var img = file.replace(/amp;/g,"");        	
	(new Image()).src = img; 
}

function changeView(imgURL,imgID)
{		
document.getElementById(imgID).src = imgURL;

}
		
function setUrl(link1,linkStyle,link2,imgId) {		
	
	var query = document.getElementById(imgId).src;			
	var colorLink = query.substring(query.lastIndexOf("_") + 1, query.length - 4);	
	
	if (color == "") 
		{ 
			document.location.href=link1+link2;
		} 
			else 
		{
			document.location.href=link1+linkStyle+'_'+colorLink+link2;
			} 
	}		
	
    
   function toggleVis(id) {
       var e = document.getElementById(id);	       
       if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}

function toggleVisOn(id) {
   var e = document.getElementById(id);
   if(e != null){	      
      e.style.display = 'block';
      }
}

function toggleVisOff(id) {
   var e = document.getElementById(id);
      if(e != null){
      e.style.display = 'none';
      }
}

function toggleAddress() {
   if(document.getElementById('UseShippingAsBillingFlag').value == 'true')
   {
   		document.getElementById('divCass').style.display = 'none';
   		document.getElementById('divDass').style.display = 'block';	
   		//document.getElementById('buttons').style.marginTop = '-30px';       		
   		document.getElementById('UseShippingAsBillingFlag').value = 'false'
   }
   else 
   {
   		document.getElementById('divCass').style.display = 'block';
   		document.getElementById('divDass').style.display = 'none';	       		
   		document.getElementById('UseShippingAsBillingFlag').value = 'true'
   }
}

function setElementValue(id, value) {
   var e = document.getElementById(id);
      e.value = value;
}


function getNextElement(iElement) { 
var parentForm = iElement.form; 

for (i=0; i<parentForm.length; i++) { 
if (parentForm.elements[i]==iElement) 
if (parentForm.elements[i+1]) 
	return parentForm.elements[i+1]; 
} 
return null; 
} 

function autotab(iCtl, iMax) { 
var nextElement, textValue = iCtl.value; 
	if (textValue.length==iMax) { 
		nextElement = getNextElement(iCtl); 	
		if (nextElement) { 
			nextElement.select(); 
			nextElement.focus(); 
		} 
	} 
} 
		

function empty(object)
{
	if (object == null)
	{
		return true;
	}
	if (object.toString().length == 0)
	{
		return true;
	}
	
	return false;
}		
		
		
// class hashtable
function Hashtable(){
	this.hash = new Array();
	this.keys = new Array();

	this.location = 0;
}


Hashtable.prototype.get = function (key)
{
	return this.hash[key];
}

Hashtable.prototype.contains = function (key)
{
	if (this.hash[key] != null)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Hashtable.prototype.put = function (key, value)
{
	if (value == null)
		return null;

	if (this.hash[key] == null)
		this.keys[this.keys.length] = key;

	this.hash[key] = value;
}


function addToCartFromQuickView(FormID, continueImage)
{		
	MiniCart.cartAdd( FormID, continueImage);
	return false;
}



function preLoadImagesMain()
{				
	preload_image(svrPath+'images/backgrounds/bg_quickview.gif');	
	preload_image(svrPath+'images/backgrounds/bg_quickview.png');	
	preload_image(svrPath+'images/backgrounds/bg_drop_sm.png');		
	preload_image(svrPath+'images/backgrounds/bg_drop_lg.png');		
	preload_image(svrPath+'images/backgrounds/bg_minicart.jpg');		
	preload_image(svrPath+'images/backgrounds/bg_searchform.png');			
	
	
}

function preLoadImagesPD()
{
	//preload_image(svrPath+'images/backgrounds/loading_qv.gif');
	preload_image(svrPath+'images/backgrounds/bg_quickview.gif');	
	preload_image(svrPath+'images/backgrounds/bg_quickview.png');
	preload_image(svrPath+'images/backgrounds/bg_flyout_shad.png');
	preload_image(svrPath+'images/backgrounds/bg_prodImageTh.gif');	
	
}

function widgetShow(id)
{
	//new Effect.SlideDown(id);
	jQuery('#' + id).slideDown();
}

function mcDivShow(id)
{

	//alert(svrPath);
	var d1 = 'mcImg'+id;
	var d2 = 'mcDesc'+id;	
	var d3 = 'arrDiv'+id;	
	
	//alert(document.getElementById(d1).style.display);
	if (document.getElementById(d1).style.display == 'none')
	{
		document.getElementById(d1).style.display = 'block';
		document.getElementById(d2).style.display = 'block';
		document.getElementById(d3).src = svrPath+'images/buttons/arrw_mc_dwn.gif';		
	}
	else
	{
		document.getElementById(d1).style.display = 'none';
		document.getElementById(d2).style.display = 'none';
		document.getElementById(d3).src = svrPath+'images/buttons/arrw_mc.gif';
		//alert(document.getElementById(d3).src);
	}
}

document.onclick = function (e) {
	e = e || event
	var target = e.target || e.srcElement
	var box = document.getElementById("divSearchWidget")
	var sbox = document.getElementById("srchBoxMain")
	do {
		if (box == target || sbox ==target ) {
			// Click occured inside the box, do nothing.
			return true
		}
		target = target.parentNode
	} while (target)
	// Click was outside the box, hide it.
	
	if (box != null && box.style.display != 'none')
	{
	Effect.SlideUp('divSearchWidget');
	//box.style.display = "none"
	}
}

function queryString( key )
{
    var re = new RegExp( "[?&]" + key + "=([^&$]*)", "i" );
    var offset = location.search.search( re );
    if ( offset == -1 ) return null;
    return RegExp.$1;
}

function searchWidg() {
	if(document.getElementById("divSearchWidget").style.display == 'none')
	 {	 
	 	Effect.SlideDown('divSearchWidget');
	 }
	 else
	 {
	 return false
	 }
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

//jQuery extSlide plugin, facilitates some functions that were using scriptaculous
(function($){
	$.extend($.fn, {
		slideLeftShow: function(speed,callback){
			return this.animate({width: "show"}, speed, callback);
		},
		slideLeftHide: function(speed,callback){
			return this.animate({width: "hide"}, speed, callback);
		},
		slideLeftToggle: function(speed,callback){
			return this.animate({width: "toggle"}, speed, callback);
		},
		slideRightShow: function(speed,callback){
			return this.animate({width:"show"},{
				step: function(now, data){
					var w = Math.ceil(now);
					if(typeof(data.origLeft) == 'undefined'){
						var position = $(this).css('position');
						if(position=='static')
						{
							$(this).css('position','relative');
						}
						data.origPos = position;
						data.origLeft = data.end+parseInt($(this).css('left'))||0;
					}
					$(this).css({left:data.origLeft-(data.start+w)});
					if(w==data.end)$(this).css('position',data.origPos);
				},
				duration: speed,
				complete: callback
			});
		},
		slideRightHide: function(speed,callback){
			return this.animate({width:"hide"},{
				step: function(now, data){
					var w = Math.ceil(now);
					if(typeof(data.origLeft) == 'undefined')
					{
						var position = $(this).css('position');
						if(position=='static')
						{
							$(this).css('position','relative');
						}
						data.origPos = position;
						data.origLeft = parseInt($(this).css('left'))||0;
					}
					$(this).css({left:data.origLeft+(data.start-w)});
					if(w==0)$(this).css({left:data.origLeft, position:data.origPos});
				},
				duration: speed,
				complete: callback
			});
		},
		slideRightToggle: function(speed,callback){
			return this.animate({width:"toggle"},{
				step: function(now, data){
					var w = Math.ceil(now);
					if(typeof(data.origLeft) == 'undefined'){
						var position = $(this).css('position');
						if(position=='static')
						{
							$(this).css('position','relative');
						}
						data.origPos = position;
						data.origLeft = data.end+parseInt($(this).css('left'))||0;
					}
					if(data.start == 0)
					{
						if(w==data.end)$(this).css('position',data.origPos);
						$(this).css({left:data.origLeft-(data.start+w)});
					}else{
						$(this).css({left:data.origLeft+(data.start-w)});
						if(w==0)$(this).css({left:data.origLeft, position:data.origPos});
					}
				},
				duration: speed,
				complete: callback
			});
		}
	});
})(jQuery);

/*common footer functions*/
function validateEmail(){
	  var reg = /^[\w_\-.]+\@[^.]+(\.[^\.]+){1,}$/; // regular expression for email
	  var footerRecipientEmailValid = jQuery("#signupEmail").val().match(reg)   ? true : false;
	  if (footerRecipientEmailValid ) {
	        jQuery("#footerRecipientEmailErrorId").css(display, 'none');
	        return true;
	      } else {	    	        		 
	        jQuery("#footerRecipientEmailErrorId").css("display",((footerRecipientEmailValid)  ? "none":"block"));  	           
	        return false;
	      }
  
    }

function onFocusEmail(emailField) {
	var defaultFieldValue = emailFieldDefaultValue; //uses js value defined in-page, server side;
	if (emailField.value == defaultFieldValue) {
		emailField.value = '';
	}
}

function onBlurEmail(emailField) {
    var defaultFieldValue = emailFieldDefaultValue; //uses js value defined in-page, server side;
	if (emailField.value == '') {
		emailField.value = defaultFieldValue;
	}
}

//Function used to set session cookie, used in Channel Advisor support
function setCookie(c_name, value) {
	var sitedomain = jsSiteDomain; //uses js value defined in-page, server side;
	document.cookie = c_name+ "=" + escape(value)  + ";path=/" + ";domain=" + sitedomain;
}

function qvShow(pid,cgid,navid) {	
	var url = jsQuickViewUrl; //set in page
	if(cgid != null){var vcgid = '&cgid='+cgid;}
	else {var vcgid ='';}	
	if(navid != null){var vnavid = '&navid='+navid;}
	else {var vnavid ='';}		
	url = url+'?pid='+pid+vcgid+vnavid;
	qvPopUp(url);
}

function qvPopUp(url) {	
	
	$('<div id=\'popup_container\'></div>').appendTo(document.body);

	var popupDiv = $('#popup_container');
	
	var height = jQuery(window).height()*.90;

	var popupHandle = {
		modal: true,
		position: 'center',
		width: 575,
		height: height,
		autoOpen: false,
		dialogClass: 'qvPopUp_container',
		close: function(){
			popupDiv.remove();
		}		
	}
	
	popupDiv.dialog(popupHandle);
	$(".ui-dialog-titlebar").remove();
	
	popupDiv.load(url).dialog('open');
	
}		

function pdShow(pid,cgid,navid) {
	var url = jsPdpUrl; //set in page	
	if(cgid != null){var vcgid = '&cgid='+cgid;}
	else {var vcgid ='';}	
	if(navid != null){var vnavid = '&navid='+navid;}
	else {var vnavid ='';}			
	url = url+'?pid='+pid+vcgid+vnavid;	
	parent.location = url;
}
function catShow(cgid,navid) {
	var url = jsPlpUrl; //set in page
	if(navid != null){var vnavid = '&navid='+navid;}
	else {var vnavid ='';} 
	url = url+'?cgid='+cgid+vnavid;   
	parent.location = url;
}
function linkShow(cid) {
	var url = jsContentLinkUrl; //set in page		
	if(cid == null){return false;}		
	url = url+'?cid='+cid;	
	parent.location = url;
}
function qvShowfb(pid,cgid,navid) {
	var url = jsFBQuickViewUrl; //set in page			
	if(cgid != null){var vcgid = '&cgid='+cgid;}
	else {var vcgid ='';}	
	if(navid != null){var vnavid = '&navid='+navid;}
	else {var vnavid ='';}		
	url = url+'?pid='+pid+vcgid+vnavid;	
	PopupDiv.showByUrl(url,'quickview');
}

function cartPopUp(url,jobj) {	// REMOVE FROM CART POP UP

	var form = jobj.parents('form').attr('id');
	var row = jobj.parents('tr');
	var product_name = row.find('div.name a').text();
	var dialog_title = 'Important';
	
	$('<div id="popup_container"><p>Are you sure you want to remove '+product_name+' from Shopping Cart?</p></div>').appendTo(document.body);

	var popupDiv = $('#popup_container');
	
	var popupHandle = {
		modal: true,
		position: 'center',
		title: dialog_title,
		width: 375,
		height: 400,
		autoOpen: false,
		dialogClass: 'qvPopUp_container',
		close: function(){
			popupDiv.remove();
		},
		buttons: {
			'No': function(){popupDiv.dialog('close');},
			'Yes': function(){
				window.location = url;
				return false;
			}
		}			
	}
	
	popupDiv.dialog(popupHandle).dialog('open');

	return;		
}

//initialize document functions/set bindings
jQuery(document).ready( function() {
	
	//cookie test code
	if (navigator.cookieEnabled == 0 && jQuery('#cookietest').length > 0) 
		{
		jQuery('#cookietest').css('display', 'block');
	}
	
	//jQuery('.imgFailed').attr('src', noImg); 
	
});
