// Version 4.4
// REQUIRES THAT facet_url be set before loading.
if ( typeof(facet_url)=='undefined' ) {
 alert("SLI Critical Javascript Error, facet_url is not defined.\nFacet functions will not work properly\nPlease check the top of your header template");
}
// For HREF status bar rewrites
function st(t) { 
 window.status=t.getAttribute('title'); 
 return true; 
}
function nost() {
 window.status=""; 
}
// sets the focus of the cursor to our search box
function sfocus() {
 if( typeof(document.searchform.w)!='undefined'){
  document.searchform.w.focus();
 }
}
// code to help process text link style facets
function getQueryVariable(variable) {
 var query=facet_url;
 var vars=query.split("&");
 for (var i=0;i<vars.length;i++) {
  var pair=vars[i].split("=");
  if(pair[0]==variable){return pair[1];}
 }
 return '';
}
// code to process text link style facets
function processfacets2(extra_href,facet,value,depth,maxDepth,removeMakeModel) { 
 var href=facet_url+extra_href;
 href=href.replace(/%20/g,' ');
 href=href.replace(/%3a/g,':'); 
 if (removeMakeModel==1) {
 	href=href.replace(/&model_id=(.*?)&/g,''); 
 	document.location=href;
 	return false;
 }
 var newurl='';
 var reg=new RegExp(facet+':');
 if (reg.test(href)) {
  // replace
  if(value==''){newurl=href.replace(new RegExp(facet+":[^ ^\+^&]* ?"),'');}
  else{newurl=href.replace(new RegExp(facet+":[^ ^\+^&]*"), facet+":"+value);}
  if(newurl==''){newurl = href;}
  if(depth){
   if(!maxDepth){maxDepth=10;}
   var facetPrefix=facet;
   facetPrefix=facetPrefix.replace(/[\d]*$/,'');
   var startRemoveDepth=depth+1;
   for(var i=startRemoveDepth;i<maxDepth;i++){
    var reg=facetPrefix+i+':[^ ^\+^&]* ?';
    var re=new RegExp(reg,'g');
    newurl=newurl.replace(re,'');
   }
  }
 }else{
  // prepend
  if (getQueryVariable('af')==''){newurl=href.replace(/af=/i,'af='+facet+":"+value);}
  else{newurl=href.replace(/af=/i,'af='+facet+":"+value+' ');}
 }
 document.location=newurl;
}

function RemoveProductFacetSelections(facetTopic) {
	var href = facet_url;
	if (facetTopic != 'products') {
		var reg = new RegExp('&af=');
		if (reg.test(href)) {
			var reg = new RegExp(/af=([^&]+)/i);
			var m = reg.exec(href);
			if(m!=null) {
				facetArray = m[1].split(/\s/);
				for(var i = 0; i < facetArray.length; i++){
					href = href.replace(facetArray[i],'');
				}
				facet_url = href;
			}
		}
	}
}

// Code needed for Dynamic Facet Breadcrumb Trial
// Remove all facets selected *after* a certain facet.
function removefacets2(extra_href,facet) { 
 var href=facet_url+extra_href;
 href=href.replace(/%20/g,' ');
 href=href.replace(/%3a/g,':'); 
 var newurl=href;
 var reg=new RegExp(/af=([^&]+)/i);
 var m=reg.exec(href);
 if(m!=null) {
  var r2=new RegExp(facet+':[^&]+');
  var m2=r2.exec(m[1]);
  if(m2!=null){newurl = href.replace(reg,'af='+m2[0]);}
 }
 document.location=newurl;
}

// Remove all facets from a URL.
function resetfacets2(extra_href) {
 var href=facet_url+extra_href;
 document.location=href.replace(new RegExp(/af=[^&]+[&]*/),'');
}
// Click Capture
function clk(o,u,r,s,a) {
 var e=encodeURIComponent||escape,t=u?u:o.href,xmlhttp=0,x="";
 if(!c_url){return true;}
 var cu=[c_url,t?"&url="+e(t):"",r?"&rk="+e(r):"",a?"&"+a:"",s?"&"+s:""].join("");
 o.href=cu+'&p=R';o.mousedown="";
 if(typeof(XMLHttpRequest)!="undefined"){try{xmlhttp=new XMLHttpRequest();x="XHR";}catch(e){xmlhttp=0;}}
 if(!xmlhttp&&typeof(window.ActiveXObject)!="undefined"){try{xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");x="XO2";}catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");x="XO1";}catch(E){xmlhttp=0;}}}
 if(xmlhttp){xmlhttp.open("GET",cu+"&p=LG&rt=xclick&lot=xml&ourl="+x,true);xmlhttp.send(null);}
 return true;
}
