
function nav( itemnum )
{
	nav_with_cc(itemnum, "");
}

function nav_with_cc( itemnum, cc )
{
	prepYAH();
	document.fNav.item.value=itemnum;
	document.fNav.sc.value="";
	document.fNav.cc.value=cc;
	document.fNav.submit();
}

function groupnav( itemnum, tpl, navlist, showtsvbutton )
{
	if (top.topFrame) {
		top.topFrame.open_pd('/scripts/detail.dll?item=' + itemnum + '&tpl=detailensembles');
	}
	else {
		copyYAHToNavGroup();
		document.fNavGroup.item.value=itemnum;
		document.fNavGroup.navlist.value=navlist;
		document.fNavGroup.tpl.value=tpl;
		document.fNavGroup.showtsvbutton.value=showtsvbutton;
		document.fNavGroup.sc.value="";
		document.fNavGroup.cc.value="";
		document.fNavGroup.submit();
	}
}

function detailnav( itemnum )
{
	document.fNav.item.value=itemnum;
	document.fNav.sc.value="";
	document.fNav.cc.value="";
	document.fNav.submit();
}

function cm_detailnav(itemnum, prevnext)
{
    document.fNav.item.value=itemnum;
	document.fNav.sc.value="";
	document.fNav.cc.value="";
	// need to build the form's action so that PREV or NEXT is set for coremetrics
	document.fNav.action += prevnext
	document.fNav.submit();
}

function searchnav( itemnum, tmp, cont, cc )
{
	var objForm = document.fNav;
	addDynamic( objForm, "yahLabels", getContentsByID("spnYahLabels"));
	addYahLinks( objForm, "yahLinks", "spnYahLinks" );
	addFastNavVars( objForm );
	objForm.item.value=itemnum;
	objForm.sc.value="";
	objForm.cc.value=cc;
	objForm.tmp.value=tmp;
	objForm.cont.value=cont;
	
	objForm.submit();
}
function searchnav_ns( itemnum, tmp, cont, cc,sdesc )
{
	var objForm = document.fNav;
	addDynamic( objForm, "yahLabels", getContentsByID("spnYahLabels"));
	addYahLinks( objForm, "yahLinks", "spnYahLinks" );
	addFastNavVars( objForm );
	objForm.item.value=itemnum;
	objForm.sc.value="";
	objForm.cc.value=cc;
	objForm.tmp.value=tmp;
	objForm.cont.value=cont;
	//--------------Natural Search Start
	var formaction = objForm.action;
 
	if (formaction.indexOf("item")==-1)
	{
	    if (formaction.indexOf("/params.")>0)
	    {
	        objForm.action = objForm.action + ".item." + itemnum + ".desc." + sdesc ;
	    }
	    else
	    {
	        objForm.action = objForm.action + "/params.item." + itemnum + ".desc." + sdesc ;
	    }
	}
    //-----------Natural Search End
 
	objForm.submit();
}
function plnav_ns(itemnum, tmp, cont, cc, sdesc )
{
//New variation for Natural search --  friendly URL 
	prepYAH();
	document.fNav.item.value=itemnum;
	document.fNav.sc.value="";
	document.fNav.cc.value=cc;
	document.fNav.tmp.value=tmp;
	document.fNav.cont.value=cont;

	//TA - DSI - 08/02/2005 - For Coremetric tagging
	if (typeof CM_ShopCatID != 'undefined')
	{
		if (document.fNav) {
			if (document.fNav.cm_scid)  {
				document.fNav.cm_scid.value = CM_ShopCatID;
			}
		}
	}
	
	
	//--------------Natural Search Start
     document.fNav.action = "http://www.qvc.com/qic/qvcapp.aspx/view.2/app.detail/params.item." + itemnum +  ".desc." + sdesc ;
    //-----------Natural Search End
	document.fNav.submit();
}

function plnav( itemnum, tmp, cont )
{
	plnav_with_cc( itemnum, tmp, cont, "" )
}

function plnav_with_cc( itemnum, tmp, cont, cc )
{
	prepYAH();
	document.fNav.item.value=itemnum;
	document.fNav.sc.value="";
	document.fNav.cc.value=cc;
	document.fNav.tmp.value=tmp;
	document.fNav.cont.value=cont;

	//TA - DSI - 08/02/2005 - For Coremetric tagging
	if (typeof CM_ShopCatID != 'undefined')
	{
		if (document.fNav) {
			if (document.fNav.cm_scid)  {
				document.fNav.cm_scid.value = CM_ShopCatID;
			}
		}
	}
	document.fNav.submit();
}

function ensnav( ensid, t )
{
	var l = "/qic/qvcapp.aspx/app.nav/params";
	var f = document.EnsNav;
	l += ".class." + f.Qclass.value;
	l += ".level." + f.Qlevel.value;
	l += "/walk.yah." + f.Qparents.value;
	
	var qYah = new QYahBuilder("yahDiv");
	qYah.lastYahLink = l;
	qYah.newLastYah = t;

	f.Qyah.value=qYah.getNewYah();
	f.ensid.value=ensid;

	//if (typeof CM_ShopCatID != 'undefined')
	//{
	//	if (document.fNav) {
	//		if (document.fNav.cm_scid)  {
	//			document.fNav.cm_scid.value = CM_ShopCatID;
	//		}
	//	}
	//}
	
	f.submit();
}

function enslst( ensid, t )
{
	var l = "/navigationapp/qvcnavigate.aspx?type=ensemblelisting";
	var f = document.EnsLst;
	l += "&comensids=" + f.comensids.value;

	var qYah = new QYahBuilder("yahDiv");
	qYah.lastYahLink = l;
	qYah.newLastYah = t;

	f.Qyah.value=qYah.getNewYah();
	f.ensid.value=ensid;

	//if (typeof CM_ShopCatID != 'undefined')
	//{
	//	if (document.fNav) {
	//		if (document.fNav.cm_scid)  {
	//			document.fNav.cm_scid.value = CM_ShopCatID;
	//		}
	//	}
	//}
	
	f.submit();
}

function buy( itemnum )
{
	document.fNav.elements[0].value=itemnum;
	document.fNav.elements[1].value="gsbuynow";
	document.fNav.elements[2].value="";
	document.fNav.elements[3].value="";
	document.fNav.submit();
}

function nav1( itemnum, cc, sc )
{
	prepYAH();
	document.fNav.elements[0].value=itemnum;
	document.fNav.elements[1].value="detail";
	document.fNav.elements[2].value=sc;
	document.fNav.elements[3].value=cc;
	document.fNav.submit();
}

function detailnav1( itemnum,cc,sc )
{
	document.fNav.elements[0].value=itemnum;
	document.fNav.elements[1].value="detail";
	document.fNav.elements[2].value=sc;
	document.fNav.elements[3].value=cc;
	document.fNav.submit();
}

function plnav1( itemnum, cc, sc, tmp, cont )
{
	prepYAH();
	document.fNav.elements[0].value=itemnum;
	document.fNav.elements[1].value="detail";
	document.fNav.elements[2].value=sc;
	document.fNav.elements[3].value=cc;
	document.fNav.elements[4].value=tmp;
	document.fNav.elements[5].value=cont;
	document.fNav.submit();
}

function YAHNav( itemnum, cp, tmp, cont )
{
	document.fYAHNav.item.value=itemnum;
	document.fYAHNav.cp.value="";
	document.fYAHNav.tmp.value="";
	document.fYAHNav.cont.value="";
	document.fYAHNav.submit();
}

function addFastNavVars( objForm ) {
	objForm.navlist.value = getContentsByID("spnNavList");
	objForm.CURRENT_PAGE.value = getContentsByID("spnCurrPage");
	objForm.TOTAL_PAGES.value = getContentsByID("spnTotalPages");
	objForm.PAGE_URL.value=getCurrURLRelative();
}

function getContentsByID( strID ) {
	objElement = document.getElementById(strID);
	return (objElement == null) ? "" : objElement.innerHTML;
}

function getCurrURLArgs_removeme( ) {
	var strRootToStripOut = "http://www.qvc.com/qsearch/search.aspx";
	var intRootLen = strRootToStripOut.length;
	var currURL = location.href;
	
	if (currURL == strRootToStripOut) {
		return currURL + "?" + getContentsByID("spnEndecaQueryString");
	}
	else if (currURL.substr(0, intRootLen) == strRootToStripOut) {
		return currURL.substr(intRootLen+1);
	}
	else {
		return getCurrURLRelative();
	}	
}

function getCurrURLRelative() {
	var strRootToStripOut = "http://www.qvc.com";
	var intRootLen = strRootToStripOut.length;

	var currURL = location.href;
	if (currURL.substr(0, intRootLen) == strRootToStripOut) {
		return currURL.substr(intRootLen);
	}
	else {
		return currURL;
	}
}

function addYahLinks( objForm, strField, strBlockID ) {
	var divContents = getContentsByID(strBlockID);
	var strYahLinks = divContents + getContentsByID("spnEndecaQueryString");
	addDynamic( objForm, strField, strYahLinks)
}

function addDynamic( objForm, strName, strValue ) {
	if (strValue.length>0) {
		var newElem = document.createElement('input');
		newElem.setAttribute('type', 'hidden');
		newElem.setAttribute('name', strName);
		newElem.setAttribute('value', strValue);
		objForm.appendChild(newElem);
	}
}

function prepForm ( itemnum, frmObj ) {
	var strAction = new String(frmObj.action);
	if (strAction.indexOf("?") == -1) {
		frmObj.action = strAction + "?cont=fastnav&item=" + itemnum;
	}
	else {
		frmObj.action = strAction + "&cont=fastnav&item=" + itemnum;
	}
}

function copyYAHToNavGroup() {
	if (document.fNav) {
		document.fNavGroup.yahlevel.value = document.fNav.yahlevel.value;
		document.fNavGroup.yahclass.value = document.fNav.yahclass.value;
		document.fNavGroup.yahnames.value = document.fNav.yahnames.value;
		document.fNavGroup.yahparent.value = document.fNav.yahparent.value;
		document.fNavGroup.yahsub.value = document.fNav.yahsub.value;
		document.fNavGroup.yahsort.value = document.fNav.yahsort.value;
		document.fNavGroup.yahsearchterm.value = document.fNav.yahsearchterm.value;
	}
}

// The purpose of this function is to gather the YAH bar info from the yahinfo form, massage it, and
// include it in the fNav form to enable the YAH bar to be reconstructed on the product detail page.
function prepYAH() {
  if (!(document.fNav.yahlevel)) {
    return;
  }

  if (document.fNav.yahparent.value == "search") {
    return;  // This function not needed for search-based YAH bar
  }
  else {
    document.fNav.yahlevel.value = 0;
  }

// Drilldown-based YAH bar
  if ( (document.yahinfo) && (document.yahinfo.ccode1) ) {
    var yahlevel = 1;
    var yahsub = "";
    var yahparent = "";
    var yahsortkey = "";
    var yahstind = "";
    var yahclass = document.yahinfo.ccode1.value+'~';
    var yahnames = document.yahinfo.cname1.value+'~';

    if (document.yahinfo.ccode2) {
      yahlevel = 2;
      yahclass += document.yahinfo.ccode2.value+'~';
      yahnames += document.yahinfo.cname2.value+'~';
      if (document.yahinfo.cparent2) {
        yahparent = document.yahinfo.cparent2.value;
      }

      if (document.yahinfo.ccode3) {
        yahlevel = 3;
        if (document.yahinfo.yah3) {
          yahsub = document.yahinfo.yah3.value+',';
        }
        else {
          yahsub = yahclass+',';
        }
        yahclass += document.yahinfo.ccode3.value+'~';
        yahnames += document.yahinfo.cname3.value+'~';

        if (document.yahinfo.ccode4) {
          yahlevel = 4;
          if (document.yahinfo.yah4) {
            yahsub += document.yahinfo.yah4.value+',';
          }
          else {
            yahsub += yahclass+',';
          }
          yahclass += document.yahinfo.ccode4.value+'~';
          yahnames += document.yahinfo.cname4.value+'~';

          if (document.yahinfo.ccode5) {
            yahlevel = 5;
            if (document.yahinfo.yah5) {
              yahsub += document.yahinfo.yah5.value+',';
            }
            else {
              yahsub += yahclass+',';
            }
            yahclass += document.yahinfo.ccode5.value+'~';
            yahnames += document.yahinfo.cname5.value+'~';
          } //level 5
        } //level 4
      } //level 3
    } //level 2

  // Grab current class code and label
    if (document.yahinfo.lname) {
	  var currClass = getVarFromURL("class");
      if (currClass != "error") {
        yahnames += document.yahinfo.lname.value+'~';
        yahclass += currClass+'~';
        yahlevel++;
      }
    }
	else {
	// Grab current sort type
	  if (document.yahinfo.sorttype) {
	    var currClass = getVarFromURL("class");
	    if (currClass != "error") {
	      yahnames += document.yahinfo.sorttype.value+'~';
	      yahclass += currClass+'~';
	      yahstind = "1";
	      yahlevel++;
	    }
	  }
	}

  // Grab current sub YAH
	if (yahlevel >= 3) {
	  var addYahSub = getVarFromURL("yah");
	  if (addYahSub != "error") {
	    yahsub += addYahSub+',';
	  }
	}
	else {
	  yahsub = "0";
	}

  // Grab current sortkey
    if (document.frmPagingHead.sortkey) {
      yahsortkey = getSortKey(document.frmPagingHead.sortkey);
    }

  // Copy YAH info to fNav to use in the Product Detail page
    document.fNav.yahlevel.value = yahlevel;
    document.fNav.yahclass.value = yahclass;
    document.fNav.yahnames.value = yahnames;
    document.fNav.yahparent.value = yahparent;
    document.fNav.yahsub.value = yahsub;
    document.fNav.yahsort.value = yahsortkey;
    document.fNav.yahsorttypeind.value = yahstind;
  } //level 1 - of Drilldown-based YAH

  else {
    // 24 Hour Product Review based YAH bar
    if (document.prodreview) {
      if (document.prodreview.showname) {
        document.fNav.yahlevel.value = 24;
        document.fNav.yahnames.value = document.prodreview.showname.value;
      }
      if (document.prodreview.datetime) {
        document.fNav.yahsub.value = document.prodreview.datetime.value;
      }
      if (document.prodreview.tz) {
        document.fNav.yahclass.value = document.prodreview.tz.value;
      }
    }
  } // 24-Hour
} // prepYAH

function getVarFromStandardURL(varName_in) {
	// Retrieves the first instance of the requested var from the current page URL
	
	// http://www.qvc.com/navigationapp/QVCNavigate.aspx?type=rightnav&class=F377&level=3&yah=C500~F396&tmp=ddsn&cont=F377&cm_re=PAGE-_-DEPARTMENTS-_-RINGS

	var currURL = new String(document.URL);
	var varName = new String(varName_in);
	var startIdx = currURL.indexOf(varName+"=");
	if (startIdx == -1) { return "error"; }  // Var Name was not found in the URL
	startIdx += varName.length + 1;  // Advance index to beginning of value
	var varValue = "";
	var currChar = currURL.charAt(startIdx++);
	while ( (currChar != '&') && (currChar != '') ) {
		varValue += currChar;
		currChar = currURL.charAt(startIdx++);
	}
	return unescape(varValue);
}

function getVarFromQICURL(varName_in) {
	// Retrieves the first instance of the requested var from the current page URL
	
	// http://www.qvc.com/qic/qvcapp.aspx/app.nav/params.class.G930.level.3/walk.yah.0011-2403?tmp=ddsn&cont=G930&cm_re=PAGE-_-DEPARTMENTS-_-CARPETFLOORCLEANERS

	var currURL = new String(document.URL);
	var varName = new String(varName_in);
	var startIdx = currURL.indexOf("."+varName+".");
	if (startIdx == -1) { return "error"; }  // Var Name was not found in the URL
	startIdx += varName.length + 2;  // Advance index to beginning of value
	var varValue = "";
	var currChar = currURL.charAt(startIdx++);
	while ( (currChar != '.') && (currChar != '') && (currChar != '?') && (currChar != '/') && (currChar != '&') ) {
		varValue += currChar;
		currChar = currURL.charAt(startIdx++);
	}
	return unescape(varValue);
}

function getVarFromURL(varName_in) {
	// Retrieves the first instance of the requested var from the current page URL
	var currURL = new String(document.URL);
	
	if( currURL.indexOf( "/qvcapp.aspx/" ) != -1 )
	{
		return getVarFromQICURL( varName_in );
	}
	else
	{
		return getVarFromStandardURL( varName_in );
	}
	return "error";
}


function getSortKey(frmSelect) {
	var sortkey = frmSelect.options[frmSelect.selectedIndex].value;
	return sortkey;
}

function fixSelect() {
	// Changes the label of the default sort option if necessary
/*
	var currURL = new String(document.URL);
	var is24Hour = currURL.indexOf("?show");
	if (is24Hour > 0) {
		if (document.frmPagingHead.sortkey) {
			var selectObj = document.frmPagingHead.sortkey;
			if (selectObj.options[0].text == "Default Listing") {
				selectObj.options[0].text = "Most Recently Aired";
				selectObj = document.frmPagingTail.sortkey;
				selectObj.options[0].text = "Most Recently Aired";
			}
		}
	}
*/
	// Defaults the select boxes to the current sortkey
	var currSortKey = getVarFromURL("sortkey");
	if (document.frmPagingHead.sortkey) {
		setSelect(document.frmPagingHead.sortkey, currSortKey);
		setSelect(document.frmPagingTail.sortkey, currSortKey);
	}
}

function setSelect(selectObj, currSortKey) {
	if (currSortKey == "error") {
		selectObj.selectedIndex = 0;
	}
	else {
		var i;
		for (i=0; i < selectObj.length; i++) {
			if (selectObj.options[i].value == currSortKey) {
				selectObj.selectedIndex = i;
				break;
			}
		}
	}
}

function pageNav(urlFieldName) {
	var destURL = eval("document.urlstore." + urlFieldName + ".value");

// Carry over sortkey to the next page
	if (document.frmPagingHead.sortkey) {
		var sortKey = getSortKey(document.frmPagingHead.sortkey);
		destURL += "&sortkey=" + escape(sortKey);
	}

// Carry over subyah to the next page
	var addYahSub = getVarFromURL("yah");
	if (addYahSub != "error") {
		destURL += "&yah=" + escape(addYahSub);
	}
	self.location.href = destURL;
}

//executes before a page change is forced
function addYAHvars(frmObj) 
{	
	formAction = frmObj.action;
	formAction = formAction.replace(/\.ProductListing,/i,".|scripts|ProductListing,");
	
	//alert("formAction = " + formAction);
	
	// check to see if the url ends with &cm_sortind=
	if(formAction.indexOf("&cm_sortind=") >= 0)
	{
		formAction = formAction.replace(/&cm_sortind=/i,".cm_sortind.nullempty");
	}
	
	//alert("formAction = " + formAction);
	
	walkParam = "";
	walkLocation = formAction.indexOf("/walk");
	
	// if the walk parameter is present, save it, and remove it from the url.	
	if(walkLocation >=0)
	{
		walkParam = formAction.substring(walkLocation);
		formAction = formAction.substring(0,walkLocation);
	}	
	

	if (frmObj.sortkey) {
		var sortKey = getSortKey(frmObj.sortkey);
		formAction = formAction + ".sortkey." + escape(sortKey);
	}
	
	var currClass = getVarFromURL("class");
	if (currClass != "error") {
		formAction = formAction + ".class." + currClass;
	}

	var addYahSub = getVarFromURL("yah");
	if (addYahSub != "error") {
		formAction = formAction + ".yah." + escape(addYahSub);
		if (frmObj.yah) {
			frmObj.yah.value = addYahSub;
		}
	}
	
	// add the walk parameter back on the url, since it needs to be at the end
	frmObj.action = formAction + walkParam;
}

//executes before a sort is performed
function doSortSubmit(frmObj) {
	if (frmObj.page) {
		frmObj.page.value = 1;  // To always start at page 1 when sorting results
	}
	
	//TA - DSI - 02/06/2006 - Set Sort indicator if sorting required for coremetrics
	if (frmObj.cm_sortind)  {
		frmObj.cm_sortind.value = "true";
	}
	
	addYAHvars(frmObj);
	frmObj.submit();
}

function doThumbChange() {
	var thumbURL = document.urlstore.thumburl.value;
	
	//alert("document.urlstore.thumburl.value=" + document.urlstore.thumburl.value);

	PRDateTime = document.frmPagingHead.datetime.value;
	PRTimeZone = document.frmPagingHead.tz.value;
	PRCoreMetr = document.frmPagingHead.cm_sortind.value;
	PRThmbnail = getUrlParam(thumbURL,"THUMBNAILS");
	
	thumbURL = "/qic/qvcapp.aspx?app=pr&params=datetime^" + PRDateTime + ",tz^" + PRTimeZone + ",cm_sortind^" + PRCoreMetr +",THUMBNAILS^" + PRThmbnail;
	
	
	if (document.frmPagingHead.sortkey) {
		var sortKey = getSortKey(document.frmPagingHead.sortkey);
		thumbURL += ",sortkey^" + escape(sortKey);
	}
	var addYahSub = getVarFromURL("yah");
	if (addYahSub != "error") {
		thumbURL += ",yah^" + escape(addYahSub);
	}
	
	//alert("thumbURL = " + thumbURL);
	self.location.href = thumbURL;
	
}

//given a url and and name, return the value
//that is associated with the give name. cr - 9.27.2006
function getUrlParam(url, name )
{
  	var regexS = "[\\?&]"+name+"=([^&#]*)";
  	var regex = new RegExp( regexS );
  	var tmpURL = url;
  	var results = regex.exec( tmpURL );
  	if( results == null )
  	{
    		return "";
  	}
  	else
  	{
  		return results[1];
    	}
}


