// Sort Values
// Featured

var featuredsort = Array();
featuredsort["Baby"] = "psrank";
featuredsort["Beauty"] = "pmrank";
featuredsort["Classical"] = "psrank";
featuredsort["Electronics"] = "pmrank";
featuredsort["HealthPersonalCare"] = "pmrank";
featuredsort["Jewelry"] = "pmrank";
featuredsort["Kitchen"] = "pmrank";
featuredsort["Miscellaneous"] = "pmrank";
featuredsort["Music"] = "psrank";
featuredsort["MusicalInstruments"] = "pmrank";
featuredsort["OfficeProducts"] = "pmrank";
featuredsort["OutdoorLiving"] = "psrank";
featuredsort["PCHardware"] = "psrank";
featuredsort["PetSupplies"] = "+pmrank";
featuredsort["Photo"] = "pmrank";
featuredsort["Software"] = "pmrank";
featuredsort["Tools"] = "pmrank";
featuredsort["Toys"] = "pmrank";
featuredsort["VideoGames"] = "pmrank";
featuredsort["WirelessAccessories"] = "psrank";

// Price

var priceup = Array();
priceup['Apparel'] = 'inverseprice';
priceup['Automotive'] = '-price';
priceup['Baby'] = '-price';
priceup['Beauty'] = '-price';
priceup['Books'] = 'inverse-pricerank';
priceup['Classical'] = '-price';
priceup['DVD'] = '-price';
priceup['Electronics'] = '-price';
priceup['GourmetFood'] = 'inverseprice';
priceup['Grocery'] = 'inverseprice';
priceup['HealthPersonalCare'] = 'inverseprice';
priceup['HomeGarden'] = '-price';
priceup['Jewelry'] = 'inverseprice';
priceup['Kitchen'] = '-price';
priceup['Magazines'] = '-price';
priceup['Merchants'] = 'inverseprice';
priceup['Miscellaneous'] = '-price';
priceup['Music'] = '-price';
priceup['MusicalInstruments'] = '-price';
priceup['OfficeProducts'] = '-price';
priceup['OutdoorLiving'] = '-price';
priceup['PCHardware'] = '-price';
priceup['PetSupplies'] = '-price';
priceup['Photo'] = '-price';
priceup['Software'] = '-price';
priceup['SportingGoods'] = 'inverseprice';
priceup['Tools'] = '-price';
priceup['Toys'] = '-price';
priceup['VHS'] = '-price';
priceup['Video'] = '-price';
priceup['VideoGames'] = '-price';
priceup['Wireless'] = 'inverse-pricerank';

var pricedown = Array();
pricedown['Apparel'] = 'pricerank';
pricedown['Automotive'] = 'price';
pricedown['Baby'] = 'price';
pricedown['Beauty'] = 'price';
pricedown['Books'] = 'pricerank';
pricedown['Classical'] = 'price';
pricedown['DVD'] = 'price';
pricedown['Electronics'] = 'price';
pricedown['GourmetFood'] = 'pricerank';
pricedown['Grocery'] = 'pricerank';
pricedown['HealthPersonalCare'] = 'pricerank';
pricedown['HomeGarden'] = 'price';
pricedown['Jewelry'] = 'pricerank';
pricedown['Kitchen'] = 'price';
pricedown['Magazines'] = 'price';
pricedown['Merchants'] = 'pricerank';
pricedown['Miscellaneous'] = 'price';
pricedown['Music'] = 'price';
pricedown['MusicalInstruments'] = 'price';
pricedown['OfficeProducts'] = 'price';
pricedown['OutdoorLiving'] = 'price';
pricedown['PCHardware'] = 'price';
pricedown['PetSupplies'] = 'price';
pricedown['Photo'] = 'price';
pricedown['Software'] = 'price';
pricedown['SportingGoods'] = 'pricerank';
pricedown['Tools'] = 'price';
pricedown['Toys'] = 'price';
pricedown['VHS'] = 'price';
pricedown['Video'] = 'price';
pricedown['VideoGames'] = 'price';
pricedown['Wireless'] = 'pricerank';

// BestSelling

var bssort = Array();
bssort['Apparel'] = 'salesrank';
bssort['Automotive'] = 'salesrank';
bssort['Baby'] = 'salesrank';
bssort['Beauty'] = 'salesrank';
bssort['Books'] = 'salesrank';
bssort['Classical'] = 'salesrank';
bssort['DigitalMusic'] = 'songtitlerank';
bssort['DVD'] = 'salesrank';
bssort['Electronics'] = 'salesrank';
bssort['GourmetFood'] = 'salesrank';
bssort['Grocery'] = 'salesrank';
bssort['HealthPersonalCare'] = 'salesrank';
bssort['HomeGarden'] = 'salesrank';
bssort['Jewelry'] = 'salesrank';
bssort['Kitchen'] = 'salesrank';
bssort['Magazines'] = 'subslot-salesrank';
bssort['Merchants'] = 'salesrank';
bssort['Miscellaneous'] = 'salesrank';
bssort['Music'] = 'salesrank';
bssort['MusicalInstruments'] = 'salesrank';
bssort['OfficeProducts'] = 'salesrank';
bssort['OutdoorLiving'] = 'salesrank';
bssort['PCHardware'] = 'salesrank';
bssort['PetSupplies'] = 'salesrank';
bssort['Photo'] = 'salesrank';
bssort['Software'] = 'salesrank';
bssort['SportingGoods'] = 'salesrank';
bssort['Tools'] = 'salesrank';
bssort['Toys'] = 'salesrank';
bssort['VHS'] = 'salesrank';
bssort['Video'] = 'salesrank';
bssort['VideoGames'] = 'salesrank';
bssort['Wireless'] = 'salesrank';
bssort['WirelessAccessories'] = 'salesrank';


// Get Rating Image Source

function rateimage(score)
{
   var score2img = Array();
   var imgsrc = "";
   score2img['0'] = 'images/rating_0_0_newr.gif';
   score2img['0.5'] = 'images/rating_0_5_newr.gif';
   score2img['1'] = 'images/rating_1_newr.gif';
   score2img['1.5'] = 'images/rating_1_5_newr.gif';
   score2img['2'] = 'images/rating_2_newr.gif';
   score2img['2.5'] = 'images/rating_2_5_newr.gif';
   score2img['3'] = 'images/rating_3_newr.gif';
   score2img['3.5'] = 'images/rating_3_5_newr.gif';
   score2img['4'] = 'images/rating_4_newr.gif';
   score2img['4.5'] = 'images/rating_4_5_newr.gif';
   score2img['5'] = 'images/rating_5_newr.gif';

   score = ((Math.round(score * 2)) / 2);
   var imgsrc = score2img[score];

   return imgsrc;
}


// Array Search Function

function in_array(needle, haystack, strict)
{
   // http : // kevin.vanzonneveld.net
   // +   original by : Kevin van Zonneveld (http : // kevin.vanzonneveld.net)
   // *     example 1 : in_array('van', ['Kevin', 'van', 'Zonneveld']);
   // *     returns 1 : true

   var found = false, key, strict = ! ! strict;

   for (key in haystack)
   {
      if ((strict && haystack[key] === needle) || ( ! strict && haystack[key] == needle))
      {
         found = true;
         break;
      }
   }

   return found;
}

// Pagination function

function paginate (cur, tres, cat, srt, key)
{
   cur = Number(cur);
   tres = Number(tres);
   var buffer = "";
   var tot = Math.ceil(tres / 10);
   var low = Math.max(1, (cur - 2));
   if (cur <= 3)
   {
      var high = Math.min((cur + (5 - cur)), tot);
   }
   else if (cur > 3)
   {
      var high = Math.min(tot, (cur + 2));
   }
   var pagination = cel('div');
   var paginationul = cel('ul');
   var pageli1 = cel('li');
   var pageli2 = cel('li');
   var pageli3 = [];
   var pageli4 = cel('li');
   var pageli5 = cel('li');
   var pageanchor1 = cel('a');
   var pageanchor2 = cel('a');
   var pageanchor3 = [];
   var pageanchor4 = cel('a');
   var pageanchor5 = cel('a');
   var clearing = cel('div');
   var pageright = cel('div');

   pagination.setAttribute("id", "pagination");

   if (cur == 1)
   {
      pageanchor1.setAttribute('href', 'javascript:void(0);');
      pageanchor1.setAttribute('className', 'off');
      pageanchor1.appendChild(ctn('First'));
      pageli1.appendChild(pageanchor1);
      paginationul.appendChild(pageli1);

      pageanchor2.setAttribute('href', 'javascript:void(0);');
      pageanchor2.setAttribute('className', 'off');
      pageanchor2.appendChild(ctn('Prev'));
      pageli2.appendChild(pageanchor2);
      paginationul.appendChild(pageli2);
   }
   else if (cur > 1)
   {
      pageanchor1.setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + "1');");
      pageanchor1.appendChild(ctn('First'));
      pageli1.appendChild(pageanchor1);
      paginationul.appendChild(pageli1);

      pageanchor2.setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + (cur-1) + "');");
      pageanchor2.appendChild(ctn('Prev'));
      pageli2.appendChild(pageanchor2);
      paginationul.appendChild(pageli2);
   }

   for (i = low; i <= high; i ++ )
   {
      pageli3[i] = cel('li');
      pageanchor3[i] = cel('a');
      if (i == cur)
      {
         pageli3[i].setAttribute('className', 'selected');
         pageanchor3[i].appendChild(ctn(i));
         pageanchor3[i].setAttribute('href', 'javascript:void(0);');
         pageanchor3[i].setAttribute('className', 'off');
         pageli3[i].appendChild(pageanchor3[i]);
         paginationul.appendChild(pageli3[i]);
      }
      else
      {
         pageanchor3[i].appendChild(ctn(i));
         pageanchor3[i].setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + i + "');");
         pageli3[i].appendChild(pageanchor3[i]);
         paginationul.appendChild(pageli3[i]);
      }
   }

   if ((tot > 1) && (cur < tot))
   {
      pageanchor4.setAttribute("href", "javascript:amazonSearch('" + key + "', '" + srt + "', '" + cat + "', '" + (cur + 1) + "');");
      pageanchor4.appendChild(ctn('Next'));
      pageli4.appendChild(pageanchor4);
      paginationul.appendChild(pageli4);
   }
   else
   {
      pageanchor5.setAttribute('href', 'javascript:void(0);');
      pageanchor5.setAttribute('className', 'off');
      pageanchor5.appendChild(ctn('Next'));
      pageli5.appendChild(pageanchor5);
      paginationul.appendChild(pageli5);
   }

   pagination.appendChild(paginationul);

   clearing.setAttribute('className', 'clearing');
   clearing.appendChild(ctn(' '));
   pageright.setAttribute('className', 'paginationRight');

   pagination.appendChild(clearing);
   pagination.appendChild(pageright);

   return pagination;
}

// Basic DOM shortcut functions
function getEl(x)
{
   return document.getElementById(x)
}
function ctn(x)
{
   return document.createTextNode(x)
}
function cel(x)
{
   return document.createElement(x)
}

// JSON ScriptBuilder Function and Prototype, from Jason Levitt of Yahoo !
// code found here : http : // devx.com / webdev / Article / 30860 / 1954
function JSONscriptRequest(fullUrl)
{
   this.fullUrl = fullUrl;
   this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
   this.headLoc = document.getElementsByTagName("head").item(0);
   this.scriptId = 'azScriptId' + JSONscriptRequest.scriptCounter ++ ;
}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function ()
{
   this.scriptObj = document.createElement("script");
   this.scriptObj.setAttribute("type", "text/javascript");
   this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
   this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function ()
{
   this.headLoc.removeChild(this.scriptObj);
}
JSONscriptRequest.prototype.addScriptTag = function ()
{
   this.headLoc.appendChild(this.scriptObj);
}

// Callback function referenced by the JSON script, This teases the
// JSON object out into DOM Nodes, and appends them to 'searchResults' DIV)
var amzJSONCallback = function(tmpData)
{
   // Set Result Count Variable
   var count = 0;

   // Set Container Div Elements for Results
   var gDiv = getEl('searchResults');

   // Clear any previous results
   gDiv.innerHTML = "";

   var sa_listTop = cel('div');
   sa_listTop.setAttribute("id", "sa_listTop");
   var scoller = cel('div');
   scoller.setAttribute("id", "scoller");
   var iDiv = cel('div');
   iDiv.setAttribute("id", "products");

   // Amazon Logo Display
   var viewOptions = cel('div');
   viewOptions.setAttribute("id", "viewOptions");
   var amzlogo = cel('img');
   amzlogo.setAttribute('src', './images/amazon_sm2.png');
   amzlogo.setAttribute('alt', 'amazon.com tutorial');
   amzlogo.cssText = "border:0px;height:20px;width:104px";
   viewOptions.appendChild(amzlogo);

   // Result Sorting
   var sortBy = cel('div');
   sortBy.setAttribute("id", "sortBy");
   var sortByem = cel('em');
   sortByem.appendChild(ctn("Sort By"));
   sortBy.appendChild(sortByem);
   var sortByul = cel('ul');
   var sortByli1 = cel('li');
   var sortByli2 = cel('li');
   var sortByli3 = cel('li');
   var popularity = cel('a');
   if (in_array(tmpData.ItemSet.sort, bssort))
   {
      popularity.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #999999; font-weight:bold;";
   }
   else
   {
      popularity.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', 'salesrank', '" + tmpData.ItemSet.category + "');");
      popularity.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
   }
   popularity.appendChild(ctn("BestSelling"));
   var featured = cel('a');
   if (in_array(tmpData.ItemSet.sort, featuredsort))
   {
      featured.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #999999; font-weight:bold;";
   }
   else
   {
      featured.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', 'psrank', '" + tmpData.ItemSet.category + "');");
      featured.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
   }
   featured.appendChild(ctn("Featured"));
   var pricesort = cel('a');
   if (in_array(tmpData.ItemSet.sort, priceup))
   {
      pricesort.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', 'price', '" + tmpData.ItemSet.category + "');");
      pricesort.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
      pricesort.appendChild(ctn("Price" + "\u25bc"));
   }
   else
   {
      pricesort.setAttribute("href", "javascript:amazonSearch('" + tmpData.ItemSet.keywords + "', '-price', '" + tmpData.ItemSet.category + "');");
      pricesort.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; color: #669966; font-weight:bold; text-decoration:none;";
      pricesort.appendChild(ctn("Price" + "\u25b2"));
   }
   sortByli1.appendChild(popularity);
   sortByli2.appendChild(featured);
   sortByli3.appendChild(pricesort);
   sortByul.appendChild(sortByli1);
   sortByul.appendChild(sortByli2);
   sortByul.appendChild(sortByli3);
   sortBy.appendChild(sortByul);

   // Loop for each Result Item
   for(i = 0; i < tmpData.ItemSet.Item.length - 1;
   i ++ )
   {
      count ++ ;
      // Update Result Count

      // Set Item Variable
      var tmpItem = tmpData.ItemSet.Item[i];

      // Define Item Container Variables
      var container = cel('div');
      var content = cel('div');
      var maintable = cel('table');
      var tbody = cel('tbody');
      var tr1 = cel('tr');
      var tr2 = cel('tr');
      var td1 = cel('td');
      var td2 = cel('td');
      var td3 = cel('td');
      var subtable = cel('table');
      var subtbody = cel('tbody');
      var subtr = cel('tr');
      var subtd1 = cel('td');
      var subtd2 = cel('td');

      // Define Item Container Attributes
      container.setAttribute("id", "productsContainer");
      content.setAttribute("id", "productsContent");
      maintable.setAttribute("width", "390px");
      subtable.setAttribute("width", "100%");
      td1.setAttribute("width", "100px");
      td1.setAttribute("rowSpan", "2");
      td2.setAttribute("width", "368px");
      subtd1.setAttribute("width", "50%");
      subtd2.setAttribute("width", "50%");

      // Thumbnail Image
      var ig = cel('img');
      var a_ig = cel('a');
      if (tmpItem.thumburl)
      {
         ig.setAttribute('src', tmpItem.thumburl);
         ig.setAttribute('alt', tmpItem.title);
         ig.style.cssText = "border:0px;height:" + tmpItem.thumbdims[0] + "px;width:" + tmpItem.thumbdims[1] + "px";
      }
      else
      {
         ig.setAttribute('src', "./images/cogs.jpg");
         ig.setAttribute('alt', "No Product Image");
         ig.style.cssText = "border:0px;height:52px;width:75px";
      }


      a_ig.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; color: #0068B3; font-weight: bold; text-decoration:none;";
      a_ig.setAttribute('href', tmpItem.url);
      a_ig.setAttribute('target', '_blank');


      // Item Title
      var li1 = cel('p');
      li1.style.cssText = "margin-top:0px; margin-bottom:2px;";
      var a1 = cel('a');
      a1.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; color: #0068B3; font-weight: bold; text-decoration:none;";
      a1.setAttribute('href', tmpItem.url);
      a1.setAttribute('target', '_blank');
      if(tmpItem.title.length > 30)
      {
         tmpItem.title = tmpItem.title.substr(0, 29) + "..."
      }
      a1.appendChild(ctn(tmpItem.title));
      li1.appendChild(a1);

      // Item Vendor
      var li2 = cel('p');
      li2.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000; font-weight:normal; text-decoration:none;";
      if(tmpItem.vendor)
      {
         li2.appendChild(ctn("Vendor: " + tmpItem.vendor));
      }

      // Item Price
      var li3 = cel('p');
      li3.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #000000; font-weight:normal; text-decoration:none;";
      var a3 = cel('a');
      a3.style.cssText = "cursor:hand; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; color: #990000; font-weight: bold; text-decoration:none;";
      if(tmpItem.lowestnewprice)
      {
         a3.setAttribute("href", "javascript:amazonSearch('" + tmpItem.asin + "', 'price&Condition=New', '" + tmpData.ItemSet.category + "');");
         a3.appendChild(ctn(tmpItem.lowestnewprice + " (" + tmpItem.totalnew + ")"));
         li3.appendChild(ctn("From: "));
         li3.appendChild(a3);
      }
      else if(tmpItem.price)
      {
         li3.appendChild(ctn("Price: " + tmpItem.price));
      }
      else if(tmpItem.lowestusedprice)
      {
         li3.appendChild(ctn("From: " + tmpItem.lowestusedprice + " (" + tmpItem.totalused + ") used"));
      }

      // Item Description
      var li4 = cel('p');
      li4.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #333333; font-weight:normal; margin-top:0px; margin-bottom:2px;";
      if(tmpItem.desc)
      {
         // RegEx used to strip out extraneous HTML and Entities in Description text
         tmpItem.desc = tmpItem.desc.replace(/<.*?>/gi, '');
         tmpItem.desc = tmpItem.desc.replace(/&.*?;/gi, ' ');
         if(tmpItem.desc.length > 121)
         {
            tmpItem.desc = tmpItem.desc.substr(0, 120) + "..."
         }
         li4.appendChild(ctn(tmpItem.desc));
      }

      // Ratings, Sellers & Reviews
      // Define Containers
      var subInfo = cel('div');
      subInfo.setAttribute("id", "subInfo");
      var subInfoUl = cel('ul');
      var subInfoLi1 = cel('li');
      var subInfoLi2 = cel('li');
      var subInfoLi3 = cel('li');
      var subInfoA1 = cel('a');
      var subInfoA2 = cel('a');
      var subInfoA3 = cel('a');
      var subInfoIg = cel('img');
      var selliconIg = cel('img');

      // Ratings
      subInfoIg.setAttribute('src', rateimage(tmpItem.avgrating));
      subInfoIg.setAttribute('alt', tmpItem.avgrating + ' Star Average');
      subInfoIg.setAttribute('align', 'center');
      subInfoIg.style.cssText = "border:0px;height:11px;width:60px;clear:none;margin-right: 5px;padding-right: 5px;";
      subInfoA1.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #0068B3; font-weight: bold;";
      subInfoA1.setAttribute('href', tmpItem.url);
      subInfoA1.setAttribute('target', '_blank');
      subInfoA1.appendChild(subInfoIg);

      // Reviews
      subInfoA2.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #0068B3; font-weight: bold;";
      subInfoA2.setAttribute('href', tmpItem.url);
      subInfoA2.setAttribute('target', '_blank');
      subInfoA2.appendChild(ctn(tmpItem.totalreviews + ' Reviews'));
      if (Number(tmpItem.totalreviews) > 0)
      {
         subInfoLi1.appendChild(subInfoA1);
         subInfoLi2.appendChild(subInfoA2);
      }
      else
      {
         subInfoLi1.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #666666; font-weight: bold; ";
         subInfoLi1.appendChild(ctn('No Votes'));
         subInfoLi2.appendChild(subInfoA2);
      }

      // Sellers
      selliconIg.setAttribute('src', 'images/sa_seller_ico.gif');
      selliconIg.setAttribute('alt', tmpItem.totalsellers + ' Sellers');
      selliconIg.style.cssText = "border:0px;height:10px;width:11px;clear:none;margin-right: 5px;";
      subInfoA3.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #666666; font-weight: bold;";
      subInfoA3.setAttribute('href', tmpItem.url);
      subInfoA3.setAttribute('target', '_blank');
      subInfoA3.appendChild(selliconIg);
      subInfoA3.appendChild(ctn(tmpItem.totalsellers + ' Sellers'));
      if (Number(tmpItem.totalsellers) > 0)
      {
         subInfoLi3.appendChild(subInfoA3);
      }
      else
      {
         subInfoLi3.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8px; color: #666666; font-weight: bold; ";
         subInfoLi3.appendChild(ctn('No Sellers'));
      }

      // Build Ratings, Reviews & Sellers Display
      subInfoUl.appendChild(subInfoLi1);
      subInfoUl.appendChild(subInfoLi2);
      subInfoUl.appendChild(subInfoLi3);
      subInfo.appendChild(subInfoUl);

      // Build Item Content
      a_ig.appendChild(ig);
      td1.appendChild(a_ig); // Thumbnail
      td2.appendChild(li1); // Title
      td2.appendChild(li4); // Description
      subtd1.appendChild(li2); // Vendor
      subtd2.appendChild(li3); // Price
      subtr.appendChild(subtd1);
      subtr.appendChild(subtd2);
      subtbody.appendChild(subtr);
      subtable.appendChild(subtbody);
      td3.appendChild(subtable);
      tr1.appendChild(td1);
      tr1.appendChild(td2);
      tr2.appendChild(td3);
      tbody.appendChild(tr1);
      tbody.appendChild(tr2);
      maintable.appendChild(tbody);
      content.appendChild(maintable);
      container.appendChild(content);
      container.appendChild(subInfo); // Ratings, Reviews & Sellers
      iDiv.appendChild(container);
      scoller.appendChild(iDiv);
   }
   if(count != 0)
   {
      // Build Result List
      sa_listTop.appendChild(viewOptions);
      sa_listTop.appendChild(sortBy);
      sa_listTop.appendChild(scoller);

      // Build Pagination
      var pagination = paginate (tmpData.ItemSet.pagenum, tmpData.ItemSet.TotalResults, tmpData.ItemSet.category, tmpData.ItemSet.sort, tmpData.ItemSet.keywords);
      sa_listTop.appendChild(pagination);
      gDiv.appendChild(sa_listTop);
      gDiv.appendChild(cel('p'));
   }
   else // If there are no results
   {
      gDiv.appendChild(ctn("sorry, no results found for '" + getEl('asch').value + "'"));
      gDiv.appendChild(cel('p'));
   }
}

function stristr( haystack, needle, bool )
{
   // http : // kevin.vanzonneveld.net
   // +   original by : Kevin van Zonneveld (http : // kevin.vanzonneveld.net)
   // *     example 1 : stristr('Kevin van Zonneveld', 'Van');
   // *     returns 1 : 'van Zonneveld'
   // *     example 2 : stristr('Kevin van Zonneveld', 'VAN', true);
   // *     returns 2 : 'Kevin '

   var pos = 0;

   pos = haystack.toLowerCase().indexOf( needle.toLowerCase() );
   if( pos == - 1 )
   {
      return false;
   }
   else
   {
      if( bool )
      {
         return haystack.substr( 0, pos );
      }
      else
      {
         return haystack.slice( pos );
      }
   }
}

// Search function - creates, builds, and adds remote JSON script
// to the page, which in turn calls amzJSONCallback
var amazonSearch = function(x, s, c, p)
{
   // Determine Sort Variable
   if (s && c)
   {
      if ((s == 'salesrank') && (bssort[c]))
      {
         s = bssort[c];
      }
      else if ((s == 'psrank') && (featuredsort[c]))
      {
         s = featuredsort[c];
      }
      else if ((s == 'price') && (pricedown[c]))
      {
         s = pricedown[c];
      }
      else if ((s == '-price') && (priceup[c]))
      {
         s = priceup[c];
      }
      else
      {
         s = '';
      }
   }

   // Assign default page number
   p = (typeof p == 'undefined') ? '1' : p;

   // Build Request String
   var request = 'http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService';
   request += '&SubscriptionId=0AQDYVHJNF45Z9AXKG82';
   request += '&AssociateTag=tropidesig-20';
   request += '&Operation=ItemSearch';
   request += '&Keywords=' + x;
   request += '&SearchIndex=' + c;
   request += '&ItemPage=' + p;
   request += '&Sort=' + s;
   request += '&MerchantId=All';
   request += '&ResponseGroup=OfferFull,Medium,Reviews';
   request += '&Condition=All';
   request += '&Style=http://tropicaltalent.com/dev/amazon/example10/ajsonCategorySearch.xsl';
   request += '&ContentType=text/javascript';
   request += '&CallBack=amzJSONCallback';

   getEl('searchResults').innerHTML = "Loading...";

   // Build Script Object
   aObj = new JSONscriptRequest(request);

   // Build Script Tag
   aObj.buildScriptTag();

   // Append Script Tag to Document
   aObj.addScriptTag();
}

