/* *********************************************************
** JSDATA.JS - JS Client-Side Database Library
** ===========================================
** This library contains JS code and data to power the
** JS Client-Side Database demo presented in the 4/2000 
** JS ScriptHead column. It is loaded into the demo HTML
** document, jsdata.html, with the line:
**
** <script src="jsdata.js" language="javascript"></script>
**
** Author      Ver  Date      Comments
** ======      ===  ====      ========
** Rick Scott  1.0  4/1/2000  First release
**
** Copyright 2000, Rick Scott, all rights reserved.
********************************************************* */

/* This is the GARDEN IDEA test Data */

/* ********************************************************* 
** Script() - Script object constructor function
** ========
** Script() creates a Script object (database record) for 
** each ScriptHead script in the demo. Here's its syntax:
**
** var scriptObj = new Script(name, url, type, level, region, rating, date)
**   name - the script name (string)
**   url - the absolute or relative script URL (string)
**   type -  the script type (string)
**     this demo supports types: "cookies", "database", "date/time",
**       "detection", "forms", "images", "instruction", "navigation",
**       "personalization", "programming", "popups", "sound", "text"
**     type can contain 2+ types using a comma (,) to delimit: 
**       "cookies, database, forms"
**   level - the script level (string)
**     this demo supports the level values: "elementary",
**       "intermediate", "advanced"
**   region - the script region (string)
**     this demo supports the region values: "Northland", "Auckland", "Waikato",
**	 "Wellington", "Bay of Plenty"
**   rating - the script rating (string)
**     this demo supports ratings: "*****", "****", "***"
**   date - the script publish date (Date obj)
**     specify date this way: new Date(yyyy, mm)
**       where yyyy is a 4-digit year, mm is an int from 0-11
**
** Examples of Script() in action: See below.
**
** When creating your own client-side database, rename the
** Script() constructor function appropriately -- e.g.,
** Widget() or Photo() -- and modify its properties as needed.
********************************************************* */

function Script(name, url, type, level, region, rating, date)
  {
  this.name = name;      // script name property (string)
  this.url = url;        // script URL property (string)
  this.type = type;      // script type property (string)
  this.level = level;    // script level property (string)
  this.region = region;  // script region property (string)
  this.rating = rating;  // script rating (string)
  this.date = date;      // script publish date property (Date obj)
  }


/* *********************************************************
** scripts - array of Script objects for this demo
** =======
** count automates indexing of scripts[], enabling
** you to safely insert new scripts between existing ones
** ****************************************************** */




var scripts = new Array();
var count = -1;

// ScriptHead Script objects

/* VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV  DATA BELOW HERE VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV */

scripts[++count] = new Script(
  "Busines 1",
  "http://www.business1.co.nz",
  "Arches, Bridges, Fences Decks, Gates, Pergolas",
  "Grey Lynn",
  "Auckland",
  "***",
  new Date(1998, 9)
  );

scripts[++count] = new Script(
  "Business 2",
  "http://www.business2.co.nz",
  "Driveways, Paths, Ponds, Steps, Walls",
  "Hamilton",
  "Waikato",
  "****",
  new Date(1998, 10)
  );

scripts[++count] = new Script(
  "Business 3",
  "http://www.business3.co.nz",
  "Furniture",
  "Hamilton",
  "Waikato",
  "*****",
  new Date(1998, 11)
  );

scripts[++count] = new Script(
  "Business 4",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2181021,00.html",
  "Sculptures, Garden Art",
  "Birkenhead",
  "Auckland",
  "****",
  new Date(1999, 0)
  );

scripts[++count] = new Script(
  "Business 5",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2207065,00.html",
  "Gates, Fences, Pergolas",
  "Cambridge",
  "Waikato",
  "***",
  new Date(1999, 1)
  );

scripts[++count] = new Script(
  "Company A",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2207065,00.html",
  "Stone Work, Concrete, Paths, Driveways",
  "Hamilton",
  "Waikato",
  "****",
  new Date(1999, 1)
  );

scripts[++count] = new Script(
  "Company B",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2218756,00.html",
  "Ponds, Pools, Bridges",
  "Hamilton",
  "Waikato",
  "****",
  new Date(1999, 2)
  );

scripts[++count] = new Script(
  "Company C",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2218756,00.html",
  "Landscaping, Garden Furniture",
  "Morrinsville",
  "Waikato",
  "****",
  new Date(1999, 2)
  );

scripts[++count] = new Script(
  "Company D",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2218756,00.html",
  "Cobblestones, Bricks, Pavers, Paths, Driveways",
  "North Shore",
  "Auckland",
  "****",
  new Date(1999, 2)
  );

scripts[++count] = new Script(
  "Company E",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2236411,00.html",
  "Garden Sheds, Pergolas, Decks",
  "Raglan",
  "Waikato",
  "***",
  new Date(1999, 3)
  );

scripts[++count] = new Script(
  "Services 1",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2257425,00.html",
  "Pebbles, Bark, Builders  Mix, Sand",
  "Hamilton",
  "Waikato",
  "****",
  new Date(1999, 4)
  );

scripts[++count] = new Script(
  "Services 2",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2257425,00.html",
  "Retaining Walls, Driveways, Paths",
  "Porirua",
  "Wellington",
  "****",
  new Date(1999, 4)
  );

scripts[++count] = new Script(
  "Services 3",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2268987,00.html",
  "Swimming Pools",
  "Takapuna",
  "Auckland",
  "***",
  new Date(1999, 5)
  );

scripts[++count] = new Script(
  "Services 4",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2288937,00.html",
  "Gates",
  "Tauranga",
  "Bay of Plenty",
  "****",
  new Date(1999, 6)
  );

scripts[++count] = new Script(
  "Services 5",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2310175,00.html",
  "Posts, Timber, Fencing, Decking",
  "Waitetuna",
  "Waikato",
  "*****",
  new Date(1999, 7)
  );

scripts[++count] = new Script(
  "Products A",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2330064,00.html",
  "Arches and Pergolas",
  "Waingaro",
  "Waikato",
  "***",
  new Date(1999, 8)
  );

scripts[++count] = new Script(
  "Products B",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2330064,00.html",
  "Sun Dials, Wind Chimes, Garden Ornaments",
  "Wellington Central",
  "Wellington",
  "***",
  new Date(1999, 8)
  );

scripts[++count] = new Script(
  "Products C",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2330064,00.html",
  "Section Maintenance, Lawn Mowing, Tree Pruning, Gardening",
  "Karori",
  "Wellington",
  "***",
  new Date(1999, 8)
  );

scripts[++count] = new Script(
  "Products D",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2348787,00.html",
  "Garden Pots and Planters",
  "Henderson",
  "Auckland",
  "*****",
  new Date(1999, 9)
  );

scripts[++count] = new Script(
  "Products E",
  "http://www.zdnet.com/devhead/stories/articles/0,4413,2348787,00.html",
  "Sculptures",
  "Hamilton",
  "Waikato",
  "***",
  new Date(1999, 9)
  );


/* XXXXXXXXXXXXXXXXXXXXXXXXXXX  DATA ABOVE HERE  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */

/* *********************************************************
** process query string of URL that loaded this page to get:
**   sortVal, typeVal, levelVal, regionVal, ratingVal
** 
** when creating your own client-side database, modify this
** code to process the query strings that you are using
** ****************************************************** */

var i, j;  // all-purpose vars
var query   = location.search;  // URL query string
var queries = new Array();      // array to hold parsed query data
var sortVal   = "nameAsc";  // default sort setting
var typeVal   = "any";      // default type setting
var levelVal  = "any";      // default level setting
var regionVal = "any";      // default region setting
var ratingVal = "any";      // default rating setting

if (query.length > 1)  // query string there, parse it!
  queries = query.substring(1,query.length).split("&");

for (i=0; i<queries.length; i++)  // set sort/type/level/region/rating vals
  {
  if (queries[i].indexOf("sort") > -1)
    sortVal = queries[i].substring(5, queries[i].length);
  else if (queries[i].indexOf("type") > -1)
    typeVal = queries[i].substring(5, queries[i].length);
  else if (queries[i].indexOf("level") > -1)
    levelVal = queries[i].substring(6, queries[i].length);
  else if (queries[i].indexOf("region") > -1)
    regionVal = queries[i].substring(7, queries[i].length);
   else if (queries[i].indexOf("rating") > -1)
    ratingVal = queries[i].substring(7, queries[i].length);
  }


/* *********************************************************
** filter table - i.e., build scriptsInList array per
** Type/Level/Region/Rating settings (typeVal, levelVal, regionVal, ratingVal)
**
** when creating your own database script, modify this code
** to build an array based on your data-filter settings
** ****************************************************** */

var scriptsInList = new Array();

function doesRatingPass(testRating, currRating)
  {
  if (testRating.indexOf("+") != -1)
    {
    currRating += "+";
    if (currRating.indexOf(testRating) != -1)
      return true;
    }
  else
    {
    if (currRating == testRating)
      return true;
    }
  return false;
  }

for (i=0,j=0; i<scripts.length; i++)
  {
  if ((typeVal == "any" || scripts[i].type.indexOf(typeVal) != -1) && 
      (levelVal == "any" || scripts[i].level == levelVal) && 
      (regionVal == "any" || scripts[i].region == regionVal) && 
      (ratingVal == "any" || doesRatingPass(ratingVal, scripts[i].rating))) 
    scriptsInList[j++] = scripts[i];
  }

function applyFilters(form)
  {
  // get current Type, Level, Region, Rating settings
  typeVal = form.typeSel.options[form.typeSel.selectedIndex].text;
  levelVal = form.levelSel.options[form.levelSel.selectedIndex].text;
  regionVal = form.regionSel.options[form.regionSel.selectedIndex].text;
  ratingVal = form.ratingSel.options[form.ratingSel.selectedIndex].text;

  var queryStr = "";
  queryStr += "sort=" + sortVal;
  queryStr += ((typeVal == "any") ? "" : "&type="+typeVal);
  queryStr += ((levelVal == "any") ? "" : "&level="+levelVal);
  queryStr += ((regionVal == "any") ? "" : "&region="+regionVal);
  queryStr += ((ratingVal == "any") ? "" : "&rating="+ratingVal);
  if (queryStr != "")
    queryStr = "?" + queryStr;
  location = "jsdata.html" + queryStr;
  }

function clearFilters()
  {
  var queryStr = "";
  queryStr += "sort=" + sortVal;
  if (queryStr != "")
    queryStr = "?" + queryStr;
  location = "jsdata.html" + queryStr;
  }


/* *********************************************************
** here are the various and sundry sort functions that are
** sent (as arguments) to scriptsInList.sort() as in:
**   scriptsInList.sort(sortByNameAscending)
**   scriptsInList.sort(sortByTypeDescending)
**   etc.
**
** when creating your own database, modify these to use
** appropriate sorting algorithms to sort by your headers
** ****************************************************** */

function sortByNameAscending(a, b)
  {
  if (a.name < b.name)
    return -1;
  else if (a.name > b.name)
    return 1;
  return 0;
  }

function sortByNameDescending(a, b)
  {
  if (a.name < b.name)
    return 1;
  else if (a.name > b.name)
    return -1;
  return 0;
  }

function sortByTypeAscending(a, b)
  {
  if (a.type < b.type)
    return -1;
  else if (a.type > b.type)
    return 1;
  return 0;
  }

function sortByTypeDescending(a, b)
  {
  if (a.type < b.type)
    return 1;
  else if (a.type > b.type)
    return -1;
  return 0;
  }

function sortByLevelAscending(a, b)
  {
  if (a.level == "elementary")
    {
    if (b.level != "elementary")
      return -1;
    }
  else if (a.level == "intermediate")
    {
    if (b.level == "elementary")
      return 1;
    else if (b.level == "advanced")
      return -1;
    }
  else if (a.level == "advanced")
    {
    if (b.level != "advanced")
      return 1;
    }
  return 0;
  }

function sortByLevelDescending(a, b)
  {
  if (a.level == "elementary")
    {
    if (b.level != "elementary")
      return 1;
    }
  else if (a.level == "intermediate")
    {
    if (b.level == "elementary")
      return -1;
    else if (b.level == "advanced")
      return 1;
    }
  else if (a.level == "advanced")
    {
    if (b.level != "advanced")
      return -1;
    }
  return 0;
  }

function sortByRegionAscending(a, b)
  {
  if (a.region < b.region)
    return -1;
  else if (a.region > b.region)
    return 1;
  return 0;
  }

function sortByRegionDescending(a, b)
  {
  if (a.region < b.region)
    return 1;
  else if (a.region > b.region)
    return -1;
  return 0;
  }

function sortByRatingAscending(a, b)
  {
  if (a.rating == "***")
    {
    if (b.rating != "***")
      return -1;
    }
  else if (a.rating == "****")
    {
    if (b.rating == "***")
      return 1;
    else if (b.rating == "*****")
      return -1;
    }
  else if (a.rating == "*****")
    {
    if (b.rating != "*****")
      return 1;
    }
  return 0;
  }

function sortByRatingDescending(a, b)
  {
  if (a.rating == "***")
    {
    if (b.rating != "***")
      return 1;
    }
  else if (a.rating == "****")
    {
    if (b.rating == "***")
      return -1;
    else if (b.rating == "*****")
      return 1;
    }
  else if (a.rating == "*****")
    {
    if (b.rating != "*****")
      return -1;
    }
  return 0;
  }

function sortByDateAscending(a, b)
  {
  if (a.date < b.date)
    return -1;
  else if (a.date > b.date)
    return 1;
  return 0;
  }

function sortByDateDescending(a, b)
  {
  if (a.date < b.date)
    return 1;
  else if (a.date > b.date)
    return -1;
  return 0;
  }


/* *********************************************************
** sort scripts array by name, type, level, region, rating, or date
** and assign values to name/type/level/region/rating/dateSortArrow
**
** when creating your own database, modify this code to
** sort according to your sort-order criteria
** ****************************************************** */

var nameSortArrow = "";
var typeSortArrow = "";
var levelSortArrow = "";
var regionSortArrow = "";
var ratingSortArrow = "";
var dateSortArrow = "";

if (sortVal == "nameAsc") {
  scriptsInList.sort(sortByNameAscending)
  nameSortArrow = "&nbsp;<img src=sort_up.gif>";
  }
else if (sortVal == "nameDesc") { 
  scriptsInList.sort(sortByNameDescending)
  nameSortArrow = "&nbsp;<img src=sort_down.gif>";
  }
else if (sortVal == "typeAsc") { 
  scriptsInList.sort(sortByTypeAscending)
  typeSortArrow = "&nbsp;<img src=sort_up.gif>";
  }
else if (sortVal == "typeDesc") { 
  scriptsInList.sort(sortByTypeDescending)
  typeSortArrow = "&nbsp;<img src=sort_down.gif>";
  }
else if (sortVal == "levelAsc") { 
  scriptsInList.sort(sortByLevelAscending)
  levelSortArrow = "&nbsp;<img src=sort_up.gif>";
  }
else if (sortVal == "levelDesc") { 
  scriptsInList.sort(sortByLevelDescending)
  levelSortArrow = "&nbsp;<img src=sort_down.gif>";
  }
else if (sortVal == "regionAsc") { 
  scriptsInList.sort(sortByRegionAscending)
  regionSortArrow = "&nbsp;<img src=sort_up.gif>";
  }
else if (sortVal == "regionDesc") { 
  scriptsInList.sort(sortByRegionDescending)
  regionSortArrow = "&nbsp;<img src=sort_down.gif>";
  }
else if (sortVal == "ratingAsc") { 
  scriptsInList.sort(sortByRatingAscending)
  ratingSortArrow = "&nbsp;<img src=sort_up.gif>";
  }
else if (sortVal == "ratingDesc") { 
  scriptsInList.sort(sortByRatingDescending)
  ratingSortArrow = "&nbsp;<img src=sort_down.gif>";
  }
else if (sortVal == "dateAsc") { 
  scriptsInList.sort(sortByDateAscending)
  dateSortArrow = "&nbsp;<img src=sort_up.gif>";
  }
else if (sortVal == "dateDesc") { 
  scriptsInList.sort(sortByDateDescending)
  dateSortArrow = "&nbsp;<img src=sort_down.gif>";
  }


/* *********************************************************
** updateTable(sortBy)
** ===================
** update table by reloading jsdata.html with query string
**
** when creating your own database, replace these variables
** with your custom query-string variables
** ****************************************************** */

function updateTable(sortBy)
  {
  var queryStr = "";
  var sortType;

  if ((sortBy == "name" && sortVal == "nameAsc") ||
      (sortBy == "type" && sortVal == "typeAsc") ||
      (sortBy == "level" && sortVal == "levelAsc") ||
      (sortBy == "region" && sortVal == "regionAsc") ||
      (sortBy == "rating" && sortVal == "ratingAsc") ||
      (sortBy == "date" && sortVal == "dateAsc"))
    sortType = "Desc";
  else
    sortType = "Asc";

  queryStr += "sort=" + sortBy + sortType;
  queryStr += ((typeVal == "any") ? "" : "&type="+typeVal);
  queryStr += ((levelVal == "any") ? "" : "&level="+levelVal);
  queryStr += ((regionVal == "any") ? "" : "&region="+regionVal);
  queryStr += ((ratingVal == "any") ? "" : "&rating="+ratingVal);
  if (queryStr != "")
    queryStr = "?" + queryStr;
  location = "jsdata.html" + queryStr;
  }


/* *********************************************************
** create HTML table in which specified scripts are listed
**
** when creating your own database, modify the following
** JS/HTML code to reflect your header/list needs
** ****************************************************** */

var tableBegin = '<table border="4" cellspacing="2" cellpadding="4" align="center" width="98%" bgcolor="#d0f0e1" bordercolorlight="#006000" bordercolordark="#006500">';
var tableEnd   = '</table>';

var rowHeaders = '<tr><td width="35%"><a href="javascript:updateTable(\'Name\')" onMouseOver="return showStatusMsg(\'Click once to sort by NAME ascending, click again to sort descending.\')"><B><BIG>Business Name</BIG></B></a>' + nameSortArrow + '</td><td width="25%"><a href="javascript:updateTable(\'type\')" onMouseOver="return showStatusMsg(\'Click once to sort by TYPE ascending, click again to sort descending.\')"><B><BIG>Product Type</BIG></B></a>' + typeSortArrow + '</td><td><a href="javascript:updateTable(\'level\')" onMouseOver="return showStatusMsg(\'Click once to sort by LEVEL ascending, click again to sort descending.\')"><B><BIG>Location</BIG></B></a>' + levelSortArrow + '</td><td><a href="javascript:updateTable(\'region\')" onMouseOver="return showStatusMsg(\'Click once to sort by REGION ascending, click again to sort descending.\')"><B><BIG>Region</BIG></B></a>' + regionSortArrow + '</td><td><a href="javascript:updateTable(\'rating\')" onMouseOver="return showStatusMsg(\'Click once to sort by RATING ascending, click again to sort descending.\')"><B><BIG>Phone Number</BIG></B></a>' + ratingSortArrow + '</td><td><a href="javascript:updateTable(\'date\')" onMouseOver="return showStatusMsg(\'Click once to sort by DATE ascending, click again to sort descending.\')"><B><BIG>Date</BIG></B></a>' + dateSortArrow + '</td></tr>';

var rowBegin  = '<tr valign="top">';
var rowEnd    = '</tr>';

var tableBody = "";  // this will hold all the currently listed script data

function getyyyyYear(date)  // return 4-digit year value
  {
  var yyyyYear = date.getYear();
  if (yyyyYear < 1000)
    yyyyYear += 1900;
  return yyyyYear;
  }

for (i=0; i<scriptsInList.length; i++)
  {
  tableBody += rowBegin;
  tableBody += "<td width='35%'><a href='" + scriptsInList[i].url + "' target=newWin>";
  tableBody += scriptsInList[i].name + "</a></td>";
  tableBody += "<td width='25%'>" + scriptsInList[i].type + "</td>";
  tableBody += "<td>" + scriptsInList[i].level + "</td>";
  tableBody += "<td>" + scriptsInList[i].region + "</td>";
  tableBody += "<td>" + scriptsInList[i].rating + "</td>";
  var date = scriptsInList[i].date.getMonth() + 1;
  date += "/" + getyyyyYear(scriptsInList[i].date);
  tableBody += "<td>" + date + "</td>";
  tableBody += rowEnd;  
  }


/* *********************************************************
** init()
** ======
** set initial values of Type/Level/Region/Rating select boxes
**
** when creating your own database, modify this code to
** set the initial values for your filter boxes
** ****************************************************** */

function init()
  {
  if (typeVal == "any")
    document.dbForm.typeSel.selectedIndex = 0; 
  else if (typeVal == "Cookies")
    document.dbForm.typeSel.selectedIndex = 1;
  else if (typeVal == "database")
    document.dbForm.typeSel.selectedIndex = 2; 
  else if (typeVal == "date/time")
    document.dbForm.typeSel.selectedIndex = 3; 
  else if (typeVal == "detection")
    document.dbForm.typeSel.selectedIndex = 4; 
  else if (typeVal == "forms")
    document.dbForm.typeSel.selectedIndex = 5; 
  else if (typeVal == "images")
    document.dbForm.typeSel.selectedIndex = 6; 
  else if (typeVal == "instruction")
    document.dbForm.typeSel.selectedIndex = 7; 
  else if (typeVal == "navigation")
    document.dbForm.typeSel.selectedIndex = 8; 
  else if (typeVal == "personalization")
    document.dbForm.typeSel.selectedIndex = 9; 
  else if (typeVal == "programming")
    document.dbForm.typeSel.selectedIndex = 10; 
  else if (typeVal == "popups")
    document.dbForm.typeSel.selectedIndex = 11; 
  else if (typeVal == "sound")
    document.dbForm.typeSel.selectedIndex = 12; 
  else if (typeVal == "text")
    document.dbForm.typeSel.selectedIndex = 13;

  if (levelVal == "any")
    document.dbForm.levelSel.selectedIndex = 0; 
  else if (levelVal == "elementary")
    document.dbForm.levelSel.selectedIndex = 1;
  else if (levelVal == "intermediate")
    document.dbForm.levelSel.selectedIndex = 2; 
  else if (levelVal == "advanced")
    document.dbForm.levelSel.selectedIndex = 3; 

  if (ratingVal == "any")
    document.dbForm.ratingSel.selectedIndex = 0; 
  else if (ratingVal == "*****")
    document.dbForm.ratingSel.selectedIndex = 1;
  else if (ratingVal == "****+")
    document.dbForm.ratingSel.selectedIndex = 2; 
  else if (ratingVal == "****")
    document.dbForm.ratingSel.selectedIndex = 3; 
  else if (ratingVal == "***+")
    document.dbForm.ratingSel.selectedIndex = 4; 
  else if (ratingVal == "***")
    document.dbForm.ratingSel.selectedIndex = 5; 
  }


/* *********************************************************
** showStatusMsg()
** ===============
** zee humble little status-bar message changer ... 
**   usage: onMouseOver="return showStatusMsg('message')"
** ****************************************************** */

function showStatusMsg(msg)  // display status-bar message
  {
  status = msg;
  return true;
  }


