// Version 3.5
// 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) {
    var href=facet_url+extra_href;
    href=href.replace(/%20/g,' ');
    href=href.replace(/%3a/g,':');
    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);
        }
        // change cat1 and cat2 to whatever you have called you categories and sub categories
        if(facet == 'cat1') {
            // also need to remove subcategory
            newurl = newurl.replace(/cat2:[^ ^\+^&]* ?/i,'');
        }


    } 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;
}
// 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=[^&]+[&]*/),'');
}

function expandFacets(type, current)
{
	var h = document.getElementById('reviewTagGroupList'+type);
	h.style.height = 'auto';
	current.innerHTML = 'less';
	current.onclick = function () {collapseFacets(type ,this);return false;};
}

function collapseFacets(type, current)
{
	var h = document.getElementById('reviewTagGroupList'+type);
	h.style.height = '137px';
	current.innerHTML = 'more';
	current.onclick = function () {expandFacets(type ,this);return false;};
}
// 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;
}