// Code in the Index page

   var HeaderLoaded = false;
      
   function restore () {
      self.header.location.reload();
      self.main.location.reload();
   }
   
   var uniqueID = 0;
   var c1 = "#E0E0E0";
   var c2 = "#F0F0F0";
   var c3 = "#D0F0F0"
   
   var s = new String(self.location);
   var SitePathLen = s.lastIndexOf("/");
   var SitePath = s.substring(0,SitePathLen);
   var LastFrameRef = new String("");
   
   function FormatField(text,len,align) {
     var s = new String(text);
     var n = s.length;
     if (n > len) {
     	s = s.substring(0,len);     
     } else {
       if (align == "left") {
          for (var i = 1; i <= (len-n); ++i) {
             s = s + " ";
          }
       } else if (align == "center") {
          var k = (len-n) / 2;
          for (var i = 1; i <= k ; ++i) {
             s = " " + s;
          }     
          for (var i = 1; i <= (len - n - k); ++i) {
             s = " " + s;
          }            
       } else if (align == "right") {
          for (var i = 1; i <= (len-n); ++i) {
             s = " " + s;
          }     
       }    
     }
     return (s);   
   }
   
   function FormatNum(num) {
     var i = new Number(num*100);
     i = Math.round(i) / 100;
     var s = new String(i);
     var p = s.lastIndexOf(".");
     var len = s.length;
     if (p<0) {
     	s = s + ".00";
     } else {
        if (p == len-2) {
           s = s + "0";
        }
     }
     return (s);
   }
   
   function ShopItem(productID,imgSrc,name,price,qty) {
      var pos = -1;
      uniqueID++;
      this.tag = uniqueID;
      this.id = productID;
      this.imgSrc = imgSrc;
      pos = this.imgSrc.indexOf("../../");
      if (pos == 0) {
         this.imgSrc = this.imgSrc.substring(6,this.imgSrc.length);
      }
      
      this.name = name;
      this.price = parseFloat(price);
      this.unique = qty; 
      this.qty = 1;
      this.prev = null;
      this.next = null;
      this.Print = PrintShopItem;
      this.ItemString = ItemToString;
   }

   function ItemToString () {
     var s = new String("");
     s = s + FormatField(this.id,8,"left");
     s = s + FormatField(this.name,20,"left");
     s = s + FormatField(this.qty,4,"right");
     s = s + FormatField(FormatNum(this.price),12,"right");
     s = s + FormatField(FormatNum(this.price*this.qty),12,"right");
     return (s);
   }
   //bgColor="+c+"
   function PrintShopItem(target,c) {
      target.writeln("<TR VALIGN=TOP>");
      target.writeln("<TD class=\"blacktext\" AlIGN=CENTER><IMG SRC="+this.imgSrc+">");
      target.writeln("<TD class=\"blacktext\"><p class=\"blacktext\">"+this.name+"</p>");
      var s = new String(FormatNum(this.price));
      target.writeln("<TD class=\"blacktext\"  ALIGN=RIGHT><p class=\"blacktext\">"+s+"</p>");
      target.writeln("<TD class=\"blacktext\"  AlIGN=CENTER><form><input type=text size=2 maxlength=2 value="+this.qty+" onChange='SetQty("+this.tag+",this.value)'></form>");
      if (this.unique == 'true') {
         target.writeln("<p class=\"blacktext\">unique</p>");
      }
      s = FormatNum(this.price*this.qty);
      target.writeln("<TD class=\"blacktext\"  ALIGN=RIGHT><p class=\"blacktext\">"+s+"</p>");
      target.writeln("</TR>");
   }
   
   function ShoppingList() {
      this.first = null;
      this.last = null;
      this.Add = AddToList;
      this.Find = FindItem;
      this.Delete = DeleteFromList;
      this.Print = PrintList;
      this.SetQty = SetItemQty;
   }

	function PrintList(target) {
		var total = parseFloat("0.0");
		var Item = this.first;
		var c = c1;
		target.writeln("<center><p><table width=80% border=0>");
		target.writeln("<caption align=center><B>Shopping List<br><br></B></caption>");
		target.writeln("<B><THEAD><TR>");
		target.writeln("<TH>Image</TH><TH>Name</TH><TH>Price</TH><TH>Qty</TH><TH>Total</TH>");
		target.writeln("</TR></THEAD></B><TBODY>");

		while (Item != null) {
			Item.Print(target,c);
			total = total+Item.price*Item.qty;
			Item = Item.next;
			if (c==c1) { c = c2 } else {c = c1};
		}
		var s = new String(FormatNum(total));

		target.writeln("</TBODY>");
		target.writeln("<TFOOT><TR VALIGN=TOP>");
		target.writeln("<TD class=\"blacktext\"><TD class=\"blacktext\">");
       target.writeln("<TD class=\"blacktext\"  AlIGN=CENTER><form><input type=button value='Update' onClick='ReloadPage()'></form>");
		target.writeln("<TD class=\"blacktext\"><p class=\"blacktext\">Total:</p><TD class=\"blacktext\"  ALIGN=RIGHT><p class=\"blacktext\">"+s+"</p></TR></TFOOT>");
		target.writeln("</table></p></center>");
		target.writeln("<center><form><input type=button value='Create Order' onClick='parent.CreateOrder()'>    <input type=button value='Continue Shopping' onClick='parent.history.back()'></form></center>");
			
	}



   var List = new ShoppingList();
   
   function CountItems() {
   		var Item = List.first;
   		var count = 0;
   		while (Item != null) {
   		   count++;
   			Item = Item.next;
   		
   		}
   		return(count);
   }
   
   function ListItems() {
   		var Item = List.first;
   		var text = new String("");
   		var count = 0;
   		var total = 0;
		
   		text = text+FormatField("ID",8,"left")+FormatField("Name",20,"left")+FormatField("Qty",4,"right");
   		text = text+FormatField("Price",12,"right")+FormatField("Total",12,"right")+"\n";
   		while (Item != null) {
   		   count++;
   		   text = text +"prod_" + count + "=" + Item.ItemString() + "\n";
   		   total = total + Item.qty*Item.price;
   			Item = Item.next;
   		
   		}
   		if (count>0) {
   			text = text + FormatField("Total : " + FormatNum(total),56,"right") + "\n";
   		}
   		return(text);		
   }
   
	function FindItem(thisId) {
	   var Item = this.first;
	   var result = false;
	   while (Item != null) {
	      //alert ("comparing:"+Item.id+" and:"+thisId);
   	      if (Item.id == thisId) {
   	         result = true;
   	        // alert("already in shopping list");
   	         break;
		   } else {
		    // alert("found to be not equal!");
			  Item = Item.next;
		   }
		}
		return(result);	 
   }
   
   function AddToList(Item) {	      
	  if (this.first == null) {
		   this.first = Item;
	  } else {
		   this.last.next = Item;
		   Item.prev = this.last;		
	  }
	  this.last = Item;
	}
   
   function DeleteFromList(tag) {
      var Item = this.first;
      while (Item != null) {
         if (Item.tag == tag) {
         	  if (Item.prev != null) {
         	     Item.prev.next = Item.next;
         	  }
         	  if (Item.next != null) {
         	     Item.next.prev = Item.prev;
         	  }
         	  if (List.first == Item) {
         	     List.first = Item.next;
         	  }
         	  if (List.last == Item) {
         	  	  List.last = Item.prev;
         	  }
          	  Item = null;
         } else {
           Item = Item.next;
         }
      }
   }

	function SetItemQty(tag,qty) {
		var Item = this.first;
		while (Item != null) {
			if (Item.tag == tag) {
			   if ((Item.unique == 'true') && (parseInt(qty) > 1)) {
			     alert("\""+Item.name + "\" is a uniqe Item. The quantity can not be more than one");
			   } else {
			     Item.qty = parseInt(qty); 
			   }
			   Item = null;
			} else {
			  Item = Item.next;
			}
		}	
	}
   
   function ShopAdd(productID,imgSrc,name ,price,qty) {
      //alert ("Index.htm shop add"+productID);
      var Item = new ShopItem(productID,imgSrc,name ,price,qty);
      if (!List.Find(productID)) {
         List.Add(Item);
      }
      self.header.BlinkShoplistBtn();
   } 
   
	function ShopRemove(tag) {
	   List.Delete(tag);
	   ShowShoppingList();
	}
	
	function ShopUpdate() {
	   ShowShoppingList();
	}
	


   function ShowShoppingList (doc) {
      //alert("showing shopping list");
      //self.main.main.document.open();
      //self.main.main.document.clear();
      //self.main.main.document.write();
      List.Print(doc);
      //self.main.main.document.close();
	}

	function SetQty(tag,qty) {
	   if (parseInt(qty) == 0) {
	      List.Delete(tag);
	   } else {
	      List.SetQty(tag,qty);
	   }
	}

   function ContinueShopping() {
   	self.main.location.href=LastFrameRef;
   }

   function RememberLastFrame(Ref) {
   	LastFrameRef = Ref;
   }

   function SelectedProduct() {
      this.id = null;
      this.ImgSrc = null;
      this.SmallImgSrc = null;
      this.PrdName = null;
      this.PrdText = null;
      this.PrdArtist = null;
      this.PrdPrice = null;
      this.ExternURL = null; 
      this.PrdCats = null;
      this.qty = null;
      this.imgcnt = null;  
   }
   
   var SelProduct = new SelectedProduct();
   
   function ShowImage(productID,ImgSrc,SmallImgSrc,PrdName,PrdText,PrdArtist,PrdPrice,ExternURL,qty,imgcnt,catref) {
      SelProduct.ImgSrc = ImgSrc;
      SelProduct.SmallImgSrc = SmallImgSrc;
      SelProduct.PrdName = PrdName;
      SelProduct.PrdText = PrdText;
      SelProduct.PrdArtist = PrdArtist;
      SelProduct.PrdPrice = PrdPrice;
      SelProduct.id = productID;
      SelProduct.ExternURL = ExternURL;
      SelProduct.qty = qty;
      SelProduct.imgcnt = imgcnt;
      SelProduct.PrdCats = catref;
      self.main.submain.location.href = SitePath +"/prddis.htm"; // show the document
   }
   
   function AddSelProduct() {
     if (SelProduct != null) {
         ShopAdd(SelProduct.id,SelProduct.SmallImgSrc,SelProduct.PrdName,SelProduct.PrdPrice,SelProduct.qty);
     }
   }

   function HighlightNavButton(name) {
      //alert('Index: highlight:'+name);
      if (HeaderLoaded) 
        { 
           self.header.HilightButton(name); 
        }
      else 
        {
           var timer = setTimeout("HighlightNavButton(name)",500);
        }

   }


