/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Steve Chipman | http://slayerofficeLicensed under: Creative Commons License
 */

var links = new Array();
var categories = new Array();
var searchFilter = "Any";

function init() {
  initLinks();
  buildCategoryArray();
  selectHeight = categories.length + 1;
  links.sort();
  drawSelect();
  buildCategoryList();
}

function initLinks() {
  // name, links, category
  links[0] = new Array("Special Orders","../../php/specialordering/index.php","Services");
  links[1] = new Array("Subscribe NOW","../../php/foodmarket/optingin/newsletter.php","FOODM@RKET");
  links[2] = new Array("Manx Produce","../../php/manxproduce.php","Produce");
  links[3] = new Array("Weddings","../../php/terrace/weddings/index.php","Services");
  links[4] = new Array("Filipino","../../php/filipino/index.php","Produce");
  links[5] = new Array("South African","../../php/safrican.php","Produce");
  links[6] = new Array("Polish","../../php/polish/index.php","Produce");
  links[7] = new Array("Ballapaddag","../../php/freshprod.php","Produce");
  links[8] = new Array("Fresh Fish","../../php/fish.php","Produce");
  links[9] = new Array("Opportunities","../../php/emplyment.php","Employment");
  links[10] = new Array("Flower Ordering","https://www.robinsons-fruit.com/shoplink/floral.shtml","Produce");
  links[11] = new Array("Chinese Recipes","../../php/recipes.php","Recipes");
  links[12] = new Array("Italian Recipes","../../php/recipes.php","Recipes");
  links[13] = new Array("Vegetarian Recipes","../../php/recipes.php","Recipes");
  links[14] = new Array("5 A DAY UK","http://www.5aday.nhs.uk","Healthy Eating");
  links[15] = new Array("Eat Your Colours","../../php/5aday.php","Healthy Eating");
  links[16] = new Array("Why 5 A DAY? Links >>>","../../php/5adayinfo.php","Healthy Eating");
  links[17] = new Array("Healthy School (visits)","../../php/schoolvisits/index.php","Healthy Eating");
  links[18] = new Array("Manx Produce Week","../../php/manxfoodshow/index.php","Healthy Eating");
  links[19] = new Array("About FOODM@RKET","../../php/foodmarket/index.php","FOODM@RKET");
  links[20] = new Array("Latest Offers","../../php/foodmarket/fmoffers.php","FOODM@RKET");
  links[21] = new Array("Organics","../../php/organic.php","Produce");
  links[22] = new Array("Johnsons Coffee","../../php/johnsons.php","Services");
  links[23] = new Array("Organic Produce","../../php/organics.php","Services");
  links[24] = new Array("Island of Plenty","../../iop/main.php","News and Features");
  links[25] = new Array("The Fresh Approach","../../feature/main.php","News and Features");
  links[26] = new Array("Capespan","../../capespan/main.php","News and Features");
  links[27] = new Array("Manx Telecom Case Study","http://www.manx-telecom.co.uk/default.asp?id=802","News and Features");
  links[28] = new Array("About Robinson's","../../php/company.php","Company Details");
  links[29] = new Array("Where are we?","../../php/wherearewe.php","Company Details");
  links[30] = new Array("Privacy and Security","../../php/confidentiality.php","Company Details");
  links[31] = new Array("SHELL Step","../../php/step/index.html","Awards / Success");
  links[32] = new Array("E Commerce","../../php/ecom.php","Awards / Success");
  links[33] = new Array("Award for Excellence","../../php/excellence/index.php","Awards / Success");
  links[34] = new Array("Management Insight","../../php/insight/index.php","Awards / Success");
  links[35] = new Array("Charity Contributions","../../php/charities.php","Company Details");
  links[36] = new Array("Trading References","../../references/index.html","Company Details");
  links[37] = new Array("Hygiene Certificates","../../php/certification.php","Awards / Success");
  links[38] = new Array("Order Online","https://www.robinsons-fruit.com/shoplink/floral.shtml","Floral Services");
  links[39] = new Array("Weddings","../../php/terrace/weddings/index.php","Floral Services");
  links[40] = new Array("Oriental","../../php/oriental.php","Produce");
  links[41] = new Array("Wedding Flowers","../../php/terrace/weddings/index.php","Floral Services");
  links[42] = new Array("ProJuice","../../php/projuice/index.php","Services");
  links[43] = new Array("Food and Drink Festival","../../php/mfs2009/index.php","News and Features");
  links[44] = new Array("Opening Hours","../../php/hours.php","Opening Hours");
  links[45] = new Array("Corporate Video","../../information/video/index.php","Company Details");
  links[46] = new Array("Taste Isle of Man","../../php/harvest/index.php","News and Features");
  links[47] = new Array("Bon Appetit","../../php/bonappetit/index.php","News and Features");
 
}

function drawSelect() {
  mHTML = "<select name=\"lists\" class=\"sel\" size=" + selectHeight + ">";
  for(i=0;i<links.length;i++) {
    if(searchFilter == "Any") {
      mHTML+="<option title=\"test\" value=\"" + links[i][1] + "\">" + links[i][0] + "</option>";
    } else {
      if(links[i][2] == searchFilter)mHTML+="<option value=\"" + links[i][1] + "\">" + links[i][0] + "</option>";
    }
  }
  mHTML+="</select>";
  document.getElementById("mContainer").innerHTML = mHTML;
}

function narrowList(searchString) {
  resultsFound = 0;
  searchString = searchString.toLowerCase();
  mHTML = "<select size=" + selectHeight + " class=\"sel\" name=\"lists\">";
  found=false;
  partialStringMatching = document.forms[0].psm.checked;
  for(i=0;i<links.length;i++) {
    zLink = links[i][0].toLowerCase();
    if((partialStringMatching && zLink.indexOf(searchString)>-1) || (zLink.indexOf(searchString) == 0 && (links[i][2] == searchFilter || searchFilter == "Any"))) {
      found=true;
      resultsFound++;
      mHTML+="<option value=\"" + links[i][1] + "\">" + links[i][0] + "</option>";
    }
  }
  mHTML+="</select>";
  if(found)document.getElementById("mContainer").innerHTML = mHTML
  showResults(resultsFound,searchString);
}

function showResults(zResultsFound,zSearchString) {
  zHTML = "";
  zHTML += "<b>" + zResultsFound + "</b> results found";
  if(zSearchString != "") zHTML += " for search term <b>" + zSearchString + "</b>";
  zHTML += " in category <b>" + searchFilter + "</b>";
  if(zResultsFound==0) zHTML+=" so listing what was found.";
  document.getElementById("mResults").innerHTML = zHTML;
}

function buildCategoryArray() {
  for(i=0;i<links.length;i++)if(!existsInArray(categories,links[i][2])) categories[categories.length] = links[i][2];
  categories.sort();
}

function buildCategoryList() {
  zHTML = "<select size=" + selectHeight + " name=\"cats\" class=\"sel\" onClick=\"handleCategoryClick(this.value);\">";
  zHTML += "<option value=\"Any\">Any</option>";
  for(i=0;i<categories.length;i++)  zHTML+="<option value=\"" +categories[i] + "\">" + categories[i] + "</option>";
  zHTML+= "</select>";
  document.getElementById("nContainer").innerHTML = zHTML;
}

function existsInArray(arrayToCheck,valueToCheck) {
  for(z=0;z<arrayToCheck.length;z++)if(arrayToCheck[z] == valueToCheck) { return true; break; }
  return false;
}

function handleCategoryClick(sValue) {
  searchFilter = sValue;
  drawSelect();
  narrowList(document.forms[0].searchText.value);
}

function handleClear() {
  document.forms[0].searchText.value="";
  narrowList("");
}

function handleGoClick() {
  if(document.forms[0].lists.value)location.href = document.forms[0].lists.value;
}

// Multiple onload function created by: Simon Willison
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  init();
});

