
var PicWin = null;
var ShipWin = null;
var ExInfoWin = null;
var LinkWin = null;
// GetCookie - Returns the value of the specified cookie or null
//             if the cookie doesn't exist
// 
function GetCookie(name) {
   var result = null;
   var myCookie = " " + document.cookie + ";";
   var searchName = " " + name + "=";
   var startOfCookie = myCookie.indexOf(searchName)
   var endOfCookie;
   if (startOfCookie != -1) {
      startOfCookie += searchName.length; // skip past cookie name
      endOfCookie = myCookie.indexOf(";",startOfCookie);
      result =
         myCookie.substring(startOfCookie,endOfCookie);
   }
   return result;
}

// ClearCookie  - Removes a cookie by setting an expiration date
//                three days in the past
// 
function ClearCookie(name, path) {
   var ThreeDays = 3 * 24 * 60 * 60 * 1000;
   var expDate = new Date();
   expDate.setTime(expDate.getTime() - ThreeDays);
   document.cookie = name + "=ImOutOfHere; path=" + path + "; expires=" + expDate.toGMTString();
}

function AddIt(n) {
var ProdName = document.SELFORM.elements["PROD" + n].value;
if (document.SELFORM.elements["NUM" + n].value == "")
  return window.alert("No amount has been entered for " + ProdName + ".");
if (!isPositiveInteger(document.SELFORM.elements["NUM" + n].value))
	return window.alert("Please enter a correct number of " + ProdName + ".");
var ProdAmount = document.SELFORM.elements["NUM" + n].value;
var ProdPrice = document.SELFORM.elements["PRICE" + n].value;
var ProdWeight = document.SELFORM.elements["WEIGHT" + n].value;
var TotPrice = ProdAmount * ProdPrice;
var TotWeight = ProdAmount * ProdWeight;
var SizeSt = "";
var ColorSt = "";
var Sel = document.SELFORM.elements["SIZE" + n];
if (unescape(Sel) != "undefined") {
  if (Sel.options[Sel.selectedIndex].value == "")
    return window.alert("No " + "SIZE" + " has been entered for " + ProdName + ".");
  else
    SizeSt = " size: " + Sel.options[Sel.selectedIndex].value;
}
Sel = document.SELFORM.elements["COLOR" + n];
if (unescape(Sel) != "undefined") {
  if (Sel.options[Sel.selectedIndex].value == "")
    return window.alert("No " + "COLOR" + " has been entered for " + ProdName + ".");
  else
    ColorSt = " color: " + Sel.options[Sel.selectedIndex].value;
}
var DescSt =  ProdName + SizeSt + ColorSt;
var StToAdd = escape(ProdAmount + "|" + DescSt + "|" + TotPrice + "|" + TotWeight);
var PrevCart = GetCookie("cart");
if (PrevCart == null)
  document.cookie = "cart=" + StToAdd + "|" + "; path=/";
else  
  document.cookie = "cart="+ PrevCart + StToAdd + "|" + "; path=/";
  
return window.alert(ProdAmount + "  " + DescSt + " has been added to the shopping cart");
}

function RemoveItem(n) {
  var theCart = unescape(GetCookie("cart"));
  var c = 0;
  var r = 0;
  var lastId = 0;
  var theStart, theEnd;
  var id = theCart.indexOf("|");
  while (id != -1) { 
    //var val = theCart.slice(lastId, id);
    switch (c % 4) {
     case 0 :
        if (r == n) theStart = lastId;
        break;
     case 3 :
        if (r == n) theEnd = id ;
        r++;          
    } //end switch
    if (r > n) break
    lastId = id + 1;
    id = theCart.indexOf("|", lastId);
    c++;
  }  //end while
  var PreSt = theCart.slice(0,theStart);
  var PostSt = theCart.slice(theEnd + 1);
	theCart = PreSt + PostSt;
  if (theCart == "")
    ClearCookie("cart", "/");
  else
    document.cookie = "cart=" + escape(theCart) + "; path=/";
  document.REMFORM.submit();    
}

function EmptyCart() {
  ClearCookie("cart", "/");
  document.REMALLFORM.submit();
}

function AbandonSession() {
  ClearCookie("cart", "/");
  document.ABANDONFORM.submit();
}

function OpenPicWin(Pic, Alt) {
	PicWin = window.open("","PicWindow",
	 "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,menubar=no,width=640,height=480");
	PicWin.document.open();
	PicWin.document.writeln("<html><head><title>" + Alt + "-Picture Zoom" + "</title></head>");
	PicWin.document.writeln('<body><font face="Arial" size="2">');
	PicWin.document.writeln("<div align=\"center\"><img name=\"picture\" src=\"" + Pic + "\" border=0 alt=\"" + Alt +"\"></div><br><br>");
	PicWin.document.writeln("<form action=\"\"><div align=\"center\"><input type=\"button\" name=\"CloseBtn\" value=\"Close\" onclick=\"window.close();return true;\"></div></form>");
	PicWin.document.writeln("</font></body></html>");
	PicWin.document.close();
	if (navigator.appName == "Microsoft Internet Explorer") {
		var w = parseInt(PicWin.document.images("picture").scrollWidth) + 30;
		if (w > screen.width) w = screen.width;
		var h = parseInt(PicWin.document.images("picture").scrollHeight) + 120;
		PicWin.resizeTo(w, h);
	}
	PicWin.focus(); 
}

function OpenShipWin(location) { 
 	ShipWin = window.open(location,"ShipWindow",
 "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,width=640,height=480");
 //ShipWin.document.close()
 ShipWin.focus();
}

function OpenExInfoWin(location) { 
 ExInfoWin = window.open(location,"ExInfoWindow",
 "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,width=640,height=480");
 //ExInfoWin.document.close()
 ExInfoWin.focus(); 
}

function OpenLinkWin(location) { 
 LinkWin = window.open(location,"LinkWindow");
 //LinkWin.document.close()
 LinkWin.focus(); 
}

function CloseWindow(AWindow) {
	if (navigator.appName == "Netscape") {
		if (AWindow) {
			if (AWindow.document != "undefined") {
			 	AWindow.close();
				AWindow = null;
			}
		}
	}
	else {
		if (navigator.appName == "Microsoft Internet Explorer") {
			if (AWindow) {
			 	AWindow.close();
				AWindow = null;
			}
		}
	}
}

function CloseWindows() {
	CloseWindow(PicWin);
	CloseWindow(ShipWin);
	CloseWindow(ExInfoWin);
}

function CheckEmptySearchBox(theField) {
  if (theField.value == "") {   
    theField.focus();
    alert("You must provide a keyword to search for");
    return false;
  }	
	return true;	
}
