/***
This script produces an a to z search listing based on a PAM keyword for the
letter.
E.g. 
http://www.perfumexyz.com/store/advsearch.asp?=eee

***/ 
// print a holder for the search list
/**
document.write ('<!-- BEGIN  ALPHA SEARCH BANNER -->    ');
document.write ('<DIV id="alphaSearchWrapper"   ALIGN="center"  >  ');
document.write ('<DIV id="alphaSearch" > &nbsp; </DIV> ');
document.write (' </DIV> ');
document.write ('<!-- END    ALPHA SEARCH BANNER -->    ');

*/
aryLetters = new Array('#','A','B','C','D','E','F','G','H','I','J','K','L','M',
                     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
aryLetterSearchKeys = [
  'NUMBERNUMBERNUMBER','AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III', 'JJJ', 'KKK',
  'LLL', 'MMM', 'NNN', 'OOO', 'PPP', 'QQQ', 'RRR', 'SSS', 'TTT', 'UUU', 'VVV',
  'WWW', 'XXX', 'YYY', 'ZZZ'
  ];

/**
  Get the selected brand and display it 
 **/
// clear the search result
if(document.getElementById('searchresult')) { 
  document.getElementById('searchresult').innerHTML='';
}

sBrand = "";                       
sCategoryId =""; // filled if it's the category page
sDepartmentId  =""; // if it's the department 
sDisplay = ""; 

// on the brand search page
// chop off the / and then get the brand based on the last
sUrl = document.location.href;

// fill in the new Disply to search result
// get the categoryid 
if (sUrl.lastIndexOf('category/') > 0) {
  nIndex =  sUrl.lastIndexOf('category');
  
  // the cat id is between nIndex and the next slash
  sCategoryId = sUrl.substring(nIndex + 11); // category/1

  // remove the /womens or whatever
  sCategoryId = sCategoryId.substring(0, 2) ;  
}

if (sUrl.lastIndexOf('keyword') > 0){
  nLastSlash = sUrl.lastIndexOf('=', sUrl.lastIndexOf('keyword') + 8);

  if (sUrl.lastIndexOf('+', sUrl.lastIndexOf('keyword') + 24) > 0) {
    nNextField = sUrl.lastIndexOf('+', sUrl.lastIndexOf('keyword') + 15);
    sSubmittedKeyword = sUrl.substring(nLastSlash + 1, nNextField);
  } else {
    if (sUrl.lastIndexOf('&', sUrl.lastIndexOf('keyword') + 21) > 0) {
      nNextField = sUrl.lastIndexOf('&', sUrl.lastIndexOf('keyword') + 21);
    } else {
      nNextField = sUrl.length;
    }
    sSubmittedKeyword = sUrl.substring(nLastSlash + 1, nNextField);
  }

  if (sSubmittedKeyword.length < 4) {
    sSubmittedKeyword = '';
  }

  if (sSubmittedKeyword.length == 18) {
    sSubmittedKeyword = '';
  }
}

if (sUrl.lastIndexOf('brand')  > 0) { 
  sUrl = sUrl.substring(0, sUrl.length - 1);
  pathArray = sUrl.split( '/' );

  for (i=0; i<pathArray.length; i++){
    if (pathArray[i] == 'brand'){
      sBrand = pathArray[i + 1];
      break;
    }
  }

  // write the brand out 
  sBrandWrite=sBrand.replace('-',' ');
  sDisplay = " Search Results : \"" + sBrandWrite.replace('%97','-')  + "\"";
  
  // need to see if it's been submitted and reset the global vars for the search criteria
  if (sUrl.search('advsear')> 0) { //then we need to hide it
    sDisplay = ''; 

    sBrand = getParameter('product_brandname');

    sCategoryId = getParameter('product_categorylink');
  }

  // clear the search result
  if (document.getElementById('searchresult')) { 
    document.getElementById('searchresult').innerHTML = sDisplay;
  }
}

sSelectedLetter ="";

// get the  selected selected Letter 
sLocation = document.location.href;
sSelectedLetter = '';

sKeywords = getParameter('keyword');
arrKeywords = sKeywords.split('+');

for (i = 0; i < arrKeywords.length; i++) {
  for (var j in aryLetterSearchKeys) {
    if (aryLetterSearchKeys[j] == arrKeywords[i]) {
      sSelectedLetter = arrKeywords[i];
      break;
    }
  }
}

nSelectedIndex=0;

// generate the list based on the alphabet above
sOutput =  "";
sPrevious = ''; 
sNext = ''; 
sNumberLink = '';

//set empty keywords for use below
if (sUrl.lastIndexOf('keyword') > 0 && sSubmittedKeyword.length > 0) {
  sKeywords = 'submitted=1' + "&keyword=" + sSubmittedKeyword + '+';
} else {
  sKeywords = 'submitted=1' + "&keyword=";
}

sBrandParameter = '&product_brandname='+ escape(sBrand.replace(/-/g, '+')) + '&product_categorylink=' + sCategoryId;
sStyle = "alphaSearchLink";
//sNumberLink = '<A    title="Items starting a number   class="' + sStyle +'" href="/store/advsearch.asp?' +  sKeywords + 'NUMBERNUMBERNUMBER' + sBrandParameter     +'"  >  ';
//sNumberLink += '#</A> ';

for (x = 0; x < aryLetterSearchKeys.length; x++) {
  sLetters = aryLetterSearchKeys[x];
  sKeywordsParameter = sKeywords + sLetters;
  sParameters = sKeywordsParameter + sBrandParameter;

  // if a letter is selected, assign the "alphaSearchLinkSelected" class to its link
  if ((sSelectedLetter != "") && (aryLetterSearchKeys[x] == sSelectedLetter)) { 
    nSelectedIndex = x;
    sStyle = "alphaSearchLinkSelected";

    // print the selected letter
    sOutput += '<a title="Items starting with '+
      (x == 0 ? 'a number' : sSelectedLetter)+
      '" class="'+sStyle+'"href="/store/advsearch.asp?'+sParameters+'">';
    sOutput += (x == 0 ? '#' : aryLetters[x])+'</a> ';
    sLetters = (x == 0 ? aryLetterSearchKeys[aryLetterSearchKeys.length - 1] : aryLetterSearchKeys[x - 1]);
    sKeywordsParameter = sKeywords + sLetters;
    sParameters = sKeywordsParameter + sBrandParameter;

    sPrevious = '<a class="alphaNavigation" title="Previous Letter - '+
      (x == 0 ? aryLetters[aryLetters.length - 1] : aryLetters[x - 1])+
      '" href="/store/advsearch.asp?'+sParameters+'">&lt;&lt; previous </a> ';
    sLetters = (x == aryLetterSearchKeys.length - 1 ? aryLetterSearchKeys[0] : aryLetterSearchKeys[x + 1]);
    sKeywordsParameter = sKeywords  + sLetters;
    sParameters = sKeywordsParameter + sBrandParameter;

    sNext = '<a class="alphaNavigation" title="Next Letter - '
      +(x == aryLetters.length - 1 ? aryLetters[0] : aryLetters[x + 1])+
      '" href="/store/advsearch.asp?'+sParameters+'"> next &gt;&gt;</a>';

    //reset the style
    sStyle = "alphaSearchLink";
  } else {
    sOutput += '<a title="Items starting with '+(x == 0 ? 'a number' : aryLetters[x])+'" class="'+sStyle+'" href="/store/advsearch.asp?'+sParameters+'">';
    sOutput += (x == 0 ? '#' : aryLetters[x]) + '</a> ';
  }

  //reset the style
  sStyle = "alphaSearchLink";
}

function displayAlphaBanner(objId) {
  // copy it into the holder
  if (document.getElementById(objId)) {
    document.getElementById(objId).innerHTML = sPrevious + sOutput  + sNext;
  }
}

function getParameter(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec(window.location.href);
  if( results == null )
    return "";
  else
    return results[1];
}

if (sUrl.search('store/search.asp')>0 && sUrl.lastIndexOf('?') == -1){
  sBrand='regular search';
}

if (sBrand.length == 0) { 
  // top
  displayAlphaBanner('alphaSearch');

  // bottom
  displayAlphaBanner('alphaSearchBottom');
}
