/** Custom Search for iapetus Magic gift finder
<script src="search.js" language="Javascript" ></script>
<script   language="Javascript"> buildSearchForm()</script> 
http://www.iapetus.co.uk.com/

-------------------------------------------------------- N B ---------------------------
---- 	For this script to work
----	Enable the advanced search bar
----	WSM > Configuration > Advanced Search Bar > Enable the advanced search bar
----
----------------------------------------------------------------------------------------
**/

function englishIfy( dValue ) {
  if( dValue.length<50 ){
    firstChar = dValue.substring(0,1);
    //if its a range
    signPos = dValue.indexOf( '<>' )   ;
    if( signPos > 0 ) {
      part1 = dValue.substring( 0 , signPos );
      part2 = dValue.substring(  signPos + 2 , dValue.length );
      return "between  $"  + part1 + ' and $' +  part2;
      // if it's a less than
    }else {
      if( firstChar =='<') {
        return 'less than $' + dValue.substring( 1, dValue.length);
      } else {
        return 'greater than $' + dValue.substring( 1, dValue.length);
      }
    }
  }
}

function populatePriceRange( ranges ) {
  priceRangeArray = new Array();
  priceRangeArray = ranges.split(',');
  priceSelect = document.getElementById( 'product_pricerange' ) ;
  priceSelect.options[0]=  new Option( 'All Prices ', '' );
  for( var i = 0; i < priceRangeArray.length ; i++) {
    // make english
    englishOption  = englishIfy ( priceRangeArray[i]  );
    priceSelect.options[i + 1] = new Option(  englishOption  , priceRangeArray[i] );
    priceSelect.options[i+1].text= priceSelect.options[i+1].text.replace(/\$/g, '\u00a3'); //http://www.unicode.org/charts/PDF/U0080.pdf
    priceSelect.options[i+1].title= priceRangeArray[i];
  }
}
// validate search form
//  sets the value of
function doCustomSearch(){
  var who  = document.getElementById('who').value;
  var what   = document.getElementById('whatList').value;
  // add the following values from adv search
  // product_departmentid  - copy value from what
  // product_theme - > copy value from who
  // product_pricerange  - manually generated form
  document.getElementById('product_departmentid').value = what;
  document.getElementById('product_theme').value = who;
  document.customSearch.submit();
}
////////////////////////////////////////////////////////
// this populates the who is it for  list
// from the theme dropdown
///////////////////////////////////////////////////////
var t = '';
function populateWhatDoTheyLikeList() {
  	getListingData('department', '');
 	dept = document.getElementById('department');
	var obj2 = document.getElementById('who');
 }
function tidyUp (theList) {
  var obj = document.getElementById(theList);
  for( var i = 1; i < obj.options.length ; i++) {
    optValue = obj.options[i].text;
    trimmedOptValue = optValue.substring(0,24);
    obj.options[i].text =  trimmedOptValue ;
    obj.options[i].title= optValue;
  }
}
////////////////////////////////////////////////////////
// this populates the who is it for  list
// from the theme dropdown
///////////////////////////////////////////////////////
function populateWhosItForList() {
  var obj = document.getElementById('theme_dropdown');
  var obj2 = document.getElementById('who');
  if (obj) {
    obj2.options.length = 1;
    for( var i = 1; i < obj.options.length ; i++) {
      optValue = obj.options[i].value;
      trimmedOptValue = optValue.substring(0,25);
      obj2.options[i] = new Option( trimmedOptValue, optValue  );
      obj2.options[i].title= optValue;
    }
  }
}

///////////////////////////////////////////
// functions to handle the changes in
// who what and big spender lists
function whoChanged(){
  // copy the who's it for value into the theme_Field
  who =  document.getElementById('who');
  document.getElementById('themefield').value = 'product_theme';
} function whatChanged(){
  // alert(' What ( dept ) has changed ' +    document.getElementById('department').value );
  //getListingData('category',document.getElementById('department').value);
  //<input id="category" name="category" type="hidden" value="">
}
function bigSpenderChanged(){
}
function gotoResultPage(pageNumber) {
  document.getElementById('frmSearchResults').start.value = pageNumber;
  document.getElementById('frmSearchResults').submit();
}
function ignoreSpaces(string) {
  var temp = "";
  string = '' + string;
  splitstring = string.split(" ");
  for(i = 0; i < splitstring.length; i++)
    temp += splitstring[i];
  return temp;
}
function populateBrandList() {
  var obj = document.getElementById('field_product_brand');
  var obj2 = document.getElementById('brandsearch');
  if (obj) {
   obj2.options.length = 1;
   for( var i = 1; i < obj.options.length ; i++) {
     obj2.options[i] = new Option(obj.options[i].value,obj.options[i].value);
   }
  }
}
//  does ajax call to store script that dumps the results into fuinctions
function getListingData(dumpName,dumpId) {
  ajaxRequest = getXMLHttpRequest ();
  if (dumpName == 'category') {
    ajaxRequest.open('GET', '/store/store.asp?action=do&doaction=dump&dump=category&dumpid='+dumpId);
    ajaxRequest.onreadystatechange = gotCategoryList;
  } else {
    ajaxRequest.open('GET', '/store/store.asp?action=do&doaction=dump&dump=department');
    ajaxRequest.onreadystatechange = gotDepartmentList;
  }
  ajaxRequest.send(null);
}

//  handles ajax info from get listing data and adds it to deptartment select
//  list
function gotDepartmentList () {
  //objList.options[i] = new Option(departmentDetails[1],departmentDetails[0]);
  if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) 
    populateList('whatList',ajaxRequest.responseText);
}

function populateList(listName,listData) {
  if(document.getElementById(listName)){
    var objList = document.getElementById(listName);
    objList.options.length = 1;
    var lines = listData.split("\n");
    t=listData;
    for(i=0;i<lines.length-1;i++) {
      var elementDetails = lines[i].split(",");
      objList.options[i+1] = new Option(elementDetails[1],elementDetails[0]);
    }
  }
}

function gotCategoryList () {
  if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200)
    populateList('category',ajaxRequest.responseText);
}
function getXMLHttpRequest () {
  ajaxRequest = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    ajaxRequest = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) { // IE
    try {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}
    }
  }
  return ajaxRequest;
}

/***
Onload we map
Brand search { department} is mapped tot he What do they like ? select list id="department"
PAM_theme is mapped to Who's it for ?  select list    id="who"
PAM_keywords is manually set in the form tags Big Spender select list  id="bigSpender"
//  fill in the forms
*/
populateWhosItForList();
populateWhatDoTheyLikeList() ;
tidyUp('product_pricerange') ;
tidyUp('who') ;
// retreive the prices from '/store/advsearchprices.asp';
// <option value="200<>299.99" >Between £200.00 and £299.99</option>
//product_pricerange
//dRanges = "20<>50,<20,50<>100,100<>200,200<>300,>300";
//populatePriceRange(dRanges);
populatePriceRange("<20,<50,<100,<200,<300,<500,<1000");


