// random shop order
function random_db_order() {
   var x, y, z;
   for (x = (shopsdb.length - 1); x > 0; x--) {
      y = Math.floor(Math.random() * x);
      if (y != x) {   
         z          = shopsdb[y];
         shopsdb[y] = shopsdb[x];
         shopsdb[x] = z;     
      }
   }
}

//Flippers
floc = 'united kingdom';

function init_shops() {
   //Query String (Form data (only use GET requests))
   var sbuf = String(self.location.search).substring(1,999).split('&');
   var nvpair, n, v;

   for (x=0; x < sbuf.length; x++) {
      nvpair = sbuf[x].split("=");
      n = String(unescape("" + nvpair[0]));
      n = n.toLowerCase();
   
      if (nvpair.length > 1) {
         v = String(unescape(nvpair[1]));
         v = v.replace(/\+/, " ");
      } else {
         v = String('');	
      }
   
      if ((n == 'loc') && (v != '')) {
         floc = v.toLowerCase();
		 break;
	  }
   }
   
   random_db_order();
}


function flipers() {
   var x;
   if (document.form1.loc) {
      for(x=0; x < document.form1.loc.length; x++) {
         if (String(document.form1.loc.options[x].value).toLowerCase() == floc) {
            document.form1.loc.selectedIndex = x;
            break;
         }
      }
   }
   if (document.form2.loc) {
      for(x=0; x < document.form2.loc.length; x++) {
         if (String(document.form2.loc.options[x].value).toLowerCase() == floc) {
            document.form2.loc.selectedIndex = x;
            break;
         }
      }
   }
   if (document.form3.loc) {
      for(x=0; x < document.form3.loc.length; x++) {
         if (String(document.form3.loc.options[x].value).toLowerCase() == floc) {
            document.form3.loc.selectedIndex = x;
            break;
         }
      }
   }
}



/*
   0=id, 1=company_title, 2=country, 3=description, 
   4=filter_on, 5=filter_ad, 6=filter_ap,
   7=tn_img, 8=real_website
*/
function showshop(idx) {
  
	var id          = shopsdb[idx][0];
	var title       = shopsdb[idx][1];
	var description = shopsdb[idx][3];
	var tn_img      = shopsdb[idx][7];
	var url         = shopsdb[idx][8];
	var website     = (url=='') ? '' : String(url).replace(/^https?:[\/]{2}([^\/]+).*$/i, '$1');

	if (url != '') {
		document.writeln(   
		 '<table width="655" height="90" border="0" cellpadding="10" cellspacing="0" background="./images/backgroundnc.jpg">\n' +
		 '  <tr> \n' +
		 '    <td width="223"><div align="center"><a href="' + url + '" onclick="gotoWebsite(' + id + ');return false;"><img src="' + tn_img + '" alt="' + 
				title + '" border="0"></a></div></td>\n' +
		 '    <td><div class="RowDescBoxDiv"><b>' + title + '</b><br>' + description + '</div>\n' +
		 '      <div class="RowLinkDiv"><a href="' + url + '" onclick="gotoWebsite(' + id + ');return false;">' + 
				website + '</a></div></td>\n' +
		 '  </tr>\n' +
		 '</table>\n<br>\n'
		);
	} else {
		document.writeln(   
		 '<table width="655" height="90" border="0" cellpadding="10" cellspacing="0" background="./images/backgroundnc.jpg">\n' +
		 '  <tr> \n' +
		 '    <td width="223"><div align="center"><a href="#" onclick="openInfoWin(' + id + ');return false;"><img src="' + tn_img + '" alt="' + 
				title + '" border="0"></a></div></td>\n' +
		 '    <td><div class="RowDescBoxDiv"><b>' + title + '</b><br>' + description + '</div>\n' +
		 '      <div class="RowLinkDiv"><a href="#" onclick="openInfoWin(' + id + ');return false;">' + 
				'More Information ...</a></div></td>\n' +
		 '  </tr>\n' +
		 '</table>\n<br>\n'
		);
	}
}

function show_shops(mode) {
   var x, z;
   z=0;
   for (x=0; x < shopsdb.length; x++) {
      if (mode == 1) {
	     if (shopsdb[x][4] && shopsdb[x][5] && shopsdb[x][6]) {
/*          if ((String(shopsdb[x][2]).toLowerCase() == floc) || (floc=='')) { */
            if (String(shopsdb[x][2]).toLowerCase() == floc) {
               showshop(x);
               z++;
            }
         }
      } else {
	     if (! (shopsdb[x][4] && shopsdb[x][5] && shopsdb[x][6])) { 
/*          if ((String(shopsdb[x][2]).toLowerCase() == floc) || (floc=='')) { */
            if (String(shopsdb[x][2]).toLowerCase() == floc) {
               showshop(x);
               z++;
            }
         }
      }
   }
   if (z==0) {
      document.writeln('<div align=center>');
      document.writeln('<font face=arial><b>Sorry,</b> no ' + ((mode == 1) ? 'premier stores' : 'matches') + ' found for your country...');
      document.writeln('</font></div>');
   }
}

function openInfoWin(sn) {
   window.open('http://www.oldcuriosityshop.net/shop/se_res_info.php?id=' + sn, '_blank', 'width=590,height=440');
}

function gotoWebsite(id) {
   //location = 'http://www.oldcuriosityshop.net/shop/se_res_jump.php?id=' + sn;
   window.open('./se_res_jump.php?id=' + id, '_blank');
}