//Create array to hold items found by searching stock database
stockfound = new Array();
defaultKeyword = "Enter keyword here";

//create a 'last modified' and copyright notice for the page that calls //this script
var lastmoddate = document.lastModified;
if(lastmoddate == 0)
   {var pagedata = "Last modified: Unknown<BR>&copy; 2003 Copyright Kevin Sands"}
else
   {var pagedata = "Last modified:" + lastmoddate + "<BR>&copy; 2003 Copyright Kevin Sands"};

//function to search a database by keyword

function getStockList(key){
list = new Array();
list[0] = stock[0][key];
var resultIndex = 1;
var found =false;
for (var a=0; a<stock.length; a++) {
	for (var i=0; i <list.length ; i++) {
		if (stock[a][key] == list[i]){found = true};
		}
	if (found==false){list[resultIndex] = stock[a][key];
	resultIndex++};
	
	found = false;
};
return(sortArray(list));
};

function fillMenus(menuToPopulate, ItemArray)
{
  var myEle ;
  var x ;
  // Empty the drop down box of any choices
  for (var q=menuToPopulate.options.length;q>=0;q--) menuToPopulate.options[q]=null;
 
  // ADD Default Choice
  myEle = document.createElement("option") ;
  myEle.value = 0 ;
  myEle.text = "All" ;
  menuToPopulate.add(myEle) ;

  // Now loop through the array of individual items

  for ( x = 0 ; x < ItemArray.length  ; x++ )
    {
               myEle = document.createElement("option") ;
          myEle.value = x+1 ;
          myEle.text = ItemArray[x] ;
          menuToPopulate.add(myEle) ;
        
    }
menuToPopulate.options[0].selected = true;
}


function getItems(itemKey, itemType, dataArray)
{tempArray = new Array();
tempArray[0] = "Not in Database";
var index = 0;
for (var a=0; a<dataArray.length; a++) 
	{if (dataArray[a][itemType] == itemKey)
		{tempArray[index] = dataArray[a]; index++;}
	}
return(tempArray);
};

function sortArray(listToSort){
var temp;

for (var a=0; a<listToSort.length; a++) {
	for (var i=0; i <listToSort.length ; i++) {
		if (listToSort[i] > listToSort[i+1]){
			temp = listToSort[i+1];
			listToSort[i+1] = listToSort[i];
			listToSort[i] = temp};
                   };
       };
return(listToSort);
};

function excluded(item){
// items to be excluded from mail order - given by stock number
found = false;
exclusionList = new Array("65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","82","83","84","98","99","100","101","102","103","104","105","106","107","141","142","143","144","145","160","162","164","214","276","277","362","380","514","515","516","517","518","519","520","521","522","523","524","525","526","527","528","529","530","531");
for(var a=0; a<exclusionList.length; a++){
	if(item == exclusionList[a]){found = true; break;};
    };
return found;
};

// --------------------------------------------------------------------
// Function search and display - main search engine


function search_and_display(chosenManufacturer, chosenType, chosenScale, chosenKeyword)
{
// --------------------------- ITEM SEARCH -------------------------
// copy stock list into a temporary array to allow a search
itemArray = new Array();
itemArray = stock;
// search itemArray by manufacturer - extract result back into itemArray
if (chosenManufacturer != "All") {itemArray = getItems(chosenManufacturer, manufacturer, itemArray);}
// search itemArray by type - extract result back into itemArray
if (chosenType != "All") {
	if (chosenType == "Military Aircraft"){chosenType = "A" };
     	if (chosenType == "Military Vehicle"){chosenType = "B"};
      	if (chosenType == "Civilian Vehicle"){chosenType = "C"};
	if (chosenType == "Civilian Aircraft"){chosenType = "D"};
	if (chosenType == "Ship"){chosenType = "E"};
	if (chosenType == "Tools; paints; glues etc."){chosenType = "F"};
	if (chosenType == "Space"){chosenType = "G"};
	if (chosenType == "Catalogues; Books"){chosenType = "H"};
	if (chosenType == "TV/SciFi related"){chosenType = "I"};
	if (chosenType == "Figures"){chosenType = "J"};	
	if (chosenType == "Scenic/detailing"){chosenType = "K"};
	if (chosenType == "Flying Models"){chosenType = "L"};
	if (chosenType == "Wargaming"){chosenType = "M"};
      if (chosenType == "Model Railways"){chosenType = "N"};
	if (chosenType == "Diecast"){chosenType = "O"};
      if (chosenType == "Second Hand"){chosenType = "P"};

itemArray = getItems(chosenType, type, itemArray);}
// search itemArray by scale - extract result back into itemArray
if (chosenScale != "All") {itemArray = getItems(chosenScale, scale, itemArray);}
// search itemArray by keyword - extract result back into itemArray
if (chosenKeyword != "Enter keyword here")
	{var index = 0; 
	tempArray = new Array();
	tempArray[0] = "Not in Database";
	for (var a=0; a<itemArray.length; a++) 
		{if (itemArray[a][title].toLowerCase().search(chosenKeyword.toLowerCase()) >= 0)
			{tempArray[index] = itemArray[a]; index++;}
		}
	itemArray = tempArray;
	}
// ---------------------------- DISPLAY ITEMS ---------------------------
// create new html page
document.write("<HTML>\n<HEAD>\n");
<!-- include the nebucart settings file -->
document.writeln("<script language=javascript src=\"js/NC_settings.js\"><\/script>");
<!-- include the nebucart cart engine -->
document.writeln("<script language=javascript src=\"js/NebuCart.js\"><\/script>");
<!-- include the nebucart formatting script -->
document.writeln("<script language=javascript src=\"js/NC_formatting.js\"><\/script>");

document.write("</HEAD>\n");
document.write("<BODY background=\"images/paper.gif\" vlink=olive>\n");
<!-- Always include the NebuCart form tag in catalog pages! -->
document.writeln("<FORM NAME=\"NC_form\">\n");
document.writeln("<CENTER>");

document.writeln("<TABLE BORDER=0>");
document.writeln("<TR>\n<TD ALIGN=CENTER>");
document.writeln("<IMG SRC=\"images/title0-2.gif\"  ALT=\"Shop logo\" BORDER=\"0\">");
document.writeln ("</TD>");
document.writeln("<TD ALIGN=CENTER><a href=\"cart.html\"><img src=\"images/cart.gif\" width=\"162\" height=\"12\" border=\"0\"></a></TD>");
document.writeln ("</TR>\n");
document.write("</TABLE>\n");
document.writeln("<B>Availability</B>");
document.writeln("<BR>Please note, at present stock control is separate from the internet site so the quantity in stock represents the quantity we have at the last update of the database.  Should we have any problem filling your order we will contact you.<BR><BR>");
document.writeln("<B>Shipping</B>");
document.writeln("<BR>At present we can ship items only within the UK and the Post Office will not allow items containing solvents to be sent through the post - these items are consequently marked as not available, but we're working on it!  We charge £3 per parcel.<BR><BR>");
document.writeln("<B>Credit/Debit Cards</B>");
document.writeln("<BR>As we are not yet on a secure server we regret that although we accept credit cards by phone, for your peace of mind, we cannot accept card payment over the net - so if you email your order we will contact you for payment details.<BR>");
document.writeln("<BR><B>If an item price is set at zero, it isn't free - phone or email and make me an offer!</B><BR><BR>");
// format items that have been found onto page by cycling through itemArray
document.write("<TABLE BORDER=1>\n");
document.write("<TR>\n<TH>Item</TH>\n<TH>Manufacturer</TH>\n<TH>Description</TH>\n<TH>Scale</TH>\n<TH>Price</TH>\n<TH>No. in stock</TH>\n<TH>Quantity required</TH>\n</TR>\n");
//nebucart requires an alphanumeric product id - so prefix N to stock code
for (var a=0; a<itemArray.length; a++) {

if(itemArray[a][quantity]!=0){
	var productID = "N" +  itemArray[a][stockCode];
	document.writeln ("<TR>");
	document.writeln ("   <TD>" + itemArray[a][title] + "</TD>");
	document.writeln ("   <TD>" + itemArray[a][manufacturer] + "</TD>");
	document.writeln ("   <TD>" + itemArray[a][description] + "</TD>");
	document.writeln ("   <TD>" + itemArray[a][scale] + "</TD>");
	document.writeln ("   <TD> "+currency + formatDecimal(itemArray[a][price]) + "</TD>");
	document.writeln ("   <TD>" + itemArray[a][quantity] + "</TD>");

     if (!excluded( itemArray[a][stockCode])){
	document.writeln ("   <TD><input type=\"text\" name=" + productID + " size =\"2\">");
	document.writeln ("   <a href=\"javascript:AddItem('" +  productID + "')\">Add to Cart</a>");
	document.writeln ("   <input type=\"hidden\" name=\"" + productID + "_price\" value=\""+ itemArray[a][price] +"\">");
	document.writeln ("   <input type=\"hidden\" name=\"" +  productID + "_desc\" value=\""+ itemArray[a][title] +"\">");
	document.writeln ("   <input type=\"hidden\" name=\"" +  productID + "_limit\" value=\""+ itemArray[a][quantity] +"\">");
	document.writeln ("   </TD>");};
     else {document.writeln ("   <TD>Not available by mail order");};
	document.writeln ("</TR>\n");

     };
};
document.write("</TABLE>\n");
document.writeln("</CENTER>");
document.write("</FORM>\n");
document.write("</BODY>\n");
document.write("</HTML>");
}

//---------------------------------------------------------------------
