
function CKquantity(checkString) {
   strNewQuantity = "";

   for ( i = 0; i < checkString.length; i++ ) {
      ch = checkString.substring(i, i+1);
      if ( (ch >= "0" && ch <= "9") || (ch == '.') )
         strNewQuantity += ch;
   }

   if ( strNewQuantity.length < 1 )
      strNewQuantity = "1";

   return(strNewQuantity);
}


function AddToCart(thisForm) {

iNumberOrdered = 0;
iNumberOrdered = GetCookie("NumberOrdered");

if (iNumberOrdered >= 50) {
alert('your shopping cart can only hold a maximum of 50 items');
}

if (iNumberOrdered < 50) {

iNumberOrdered++;

if (thisForm.STOCKCODE == null) { strSTOCKCODE = ""; }
if (thisForm.STOCKCODE != null) { strSTOCKCODE = thisForm.STOCKCODE.value; }

if (thisForm.QUANTITY == null) { strQUANTITY = "1"; }
if (thisForm.QUANTITY != null) { strQUANTITY = thisForm.QUANTITY.value; }

if (thisForm.PRICE == null) { strPRICE = "0.00"; }
if (thisForm.PRICE != null) { strPRICE = thisForm.PRICE.value; }

if (thisForm.NAME == null) { strNAME = ""; }
if (thisForm.NAME != null) { strNAME = thisForm.NAME.value; }

if (thisForm.SHIPPING == null) { strSHIPPING = "0.00"; }
if (thisForm.SHIPPING != null) { strSHIPPING = thisForm.SHIPPING.value; }

if (thisForm.VARIATION == null) { strVARIATION = ""; }
if (thisForm.VARIATION != null) { strVARIATION = thisForm.VARIATION.value; }

if (strVARIATION == "please select...") {
alert("Please select your desired style...");
thisForm.VARIATION.focus();
return false;
}

dbUpdatedOrder = strSTOCKCODE + "|" + strQUANTITY + "|" + strPRICE + "|" + strNAME + "|" + strSHIPPING  + "|" + strVARIATION;

strNewOrder = "Order." + iNumberOrdered;
SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
SetCookie("NumberOrdered", iNumberOrdered, null, "/");

if ( strQUANTITY == 1 )
notice = strQUANTITY + " item added to your shopping cart.";
else
notice = strQUANTITY + " items added to your shopping cart.";
alert(notice);

}

}


function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}


function FixCookieDate (date) {
   var base = new Date(0);
   var skew = base.getTime();

   date.setTime (date.getTime() - skew);
}


function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while ( i < clen ) {
      var j = i + alen;
      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
      i = document.cookie.indexOf(" ", i) + 1;
      if ( i == 0 ) break;
   }

   return(null);
}


function SetCookie (name,value,expire,path,domain,secure) {
   var expire = new Date ();
   expire.setTime (expire.getTime() + (90 * 60000)); 
   var cookie = expire ;
   document.cookie = name + "=" + escape (value) +
                     ((expire) ? "; expires=" + expire.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}


function DeleteCookie (name,path,domain) {
   if ( GetCookie(name) ) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}


function setShippingCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
return true;
}


function moneyFormat(input) {
   var pounds = Math.floor(input);
   var tmp = new String(input);

   for ( var decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {
      if ( tmp.charAt(decimalAt)=="." )
         break;
   }

   var pence  = "" + Math.round(input * 100);
   pence = pence.substring(pence.length-2, pence.length)
           pounds += ((tmp.charAt(decimalAt+2)=="9")&&(pence=="00"))? 1 : 0;

   if ( pence == "0" )
      pence = "00";

   return(pounds + "." + pence);
}


function CartDetails() {
   iNumberOrdered = GetCookie("NumberOrdered");
   if ( iNumberOrdered == null || iNumberOrdered == 0 ) {
   document.write("<a href=\"shoppingcart.php\" class=\"basketlink\" style=\"text-decoration:none;\">0 items</a></div>");
   }
   if ( iNumberOrdered == 1 ) {
   document.write("<a href=\"shoppingcart.php\" class=\"basketlink\" style=\"text-decoration:none;\">1 items</a></div>");
   }
   if ( iNumberOrdered > 1 ) {
   document.write("<a href=\"shoppingcart.php\" class=\"basketlink\" style=\"text-decoration:none;\">" + iNumberOrdered + " items</a></div>");
   }
}


function RemoveFromCart(RemOrder) {
   if ( confirm("Click 'Ok' to remove this product from your shopping cart.") ) {
      NumberOrdered = GetCookie("NumberOrdered");
      for ( i=RemOrder; i < NumberOrdered; i++ ) {
         NewOrder1 = "Order." + (i+1);
         NewOrder2 = "Order." + (i);
         database = GetCookie(NewOrder1);
         SetCookie (NewOrder2, database, null, "/");
      }
      NewOrder = "Order." + NumberOrdered;
      SetCookie ("NumberOrdered", NumberOrdered-1, null, "/");
      DeleteCookie(NewOrder, "/");
   }
      javascript:location.reload(true);
}


function GetFromCart( fShipping ) {

   iNumberOrdered = GetCookie("NumberOrdered");

   if ( iNumberOrdered == null || iNumberOrdered == 0 ) {
   document.write("<br><br><br><b>Your shopping cart is currently empty!</b>");
   document.write("<br><br><br><br>");
}
   else {

   if( fShipping )
      WriteToForm( true, fShipping );
   else
      WriteToForm( true, 0 );
}
}


function WriteToForm( bDisplay, fShipping ) {
   iNumberOrdered = 0;
   fTotal         = 0;
   strTotal       = "";
   strShipping    = "";
   strOutput      = "";
   iNumberOrdered = GetCookie("NumberOrdered");

   if ( bDisplay )

      strOutput =	"<br><br><b>Your shopping cart currently contains the following items:</b><br><br><br>" +
			"<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" style=\"width:514px;\"><tr>" +
			"<td class=\"basiccontent\" style=\"width:249px;height:15px;vertical-align:middle;background:#ebffff;\"><div align=\"center\"><b>product(s)</b></div></td>" +
			"<td class=\"basiccontent\" style=\"width:75px;height:15px;vertical-align:middle;background:#ebffff;\"><div align=\"center\"><b>quantity</b></div></td>" +
			"<td class=\"basiccontent\" style=\"width:90px;height:15px;vertical-align:middle;background:#ebffff;\"><div align=\"center\"><b>price</b></div></td>" +
			"<td class=\"basiccontent\" style=\"width:100px;height:15px;vertical-align:middle;background:#ebffff;\"><div align=\"center\"><b>remove item</b></div></td></tr>";

   for ( i = 1; i <= iNumberOrdered; i++ ) {
      NewOrder = "Order." + i;
      database = "";
      database = GetCookie(NewOrder);

      Token0 = database.indexOf("|", 0);
      Token1 = database.indexOf("|", Token0+1);
      Token2 = database.indexOf("|", Token1+1);
      Token3 = database.indexOf("|", Token2+1);
      Token4 = database.indexOf("|", Token3+1);

      fields = new Array;
      fields[0] = database.substring( 0, Token0 );
      fields[1] = database.substring( Token0+1, Token1 );
      fields[2] = database.substring( Token1+1, Token2 );
      fields[3] = database.substring( Token2+1, Token3 );
      fields[4] = database.substring( Token3+1, Token4 );
      fields[5] = database.substring( Token4+1, database.length );

      fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
      fShipping  += (parseInt(fields[1]) * parseFloat(fields[4]) );
      strTotal    = moneyFormat(fTotal);
      strShipping = moneyFormat(fShipping);

   if ( bDisplay ) {
		strOutput += "<tr>";
		strOutput += "<td class=\"basiccontent\" style=\"width:249px;padding:5px;background:#ebffff;\"><a href=\"showproduct.php?StockCode=" + fields[0] + "\">" + fields[3] + "</a></td>";
		strOutput += "<td class=\"basiccontent\" style=\"width:75px;background:#ebffff;vertical-align:middle;\"><div align=\"center\">" + fields[1] + "</div></td>";
		strOutput += "<td class=\"basiccontent\" style=\"width:90px;background:#ebffff;vertical-align:middle;\"><div align=\"center\">£" + moneyFormat(fields[2]) + "/ea</div></td>";
		strOutput += "<td class=\"basiccontent\" style=\"width:100px;background:#ebffff;vertical-align:middle;\"><div align=\"center\"><img src=\"removefromcart.png\" width=\"20\" height=\"20\" alt=\"remove from cart\" style=\"cursor:pointer;\" onClick=\"RemoveFromCart("+i+")\"></div></td></tr>";
		strOutput += "</tr>";

                   }

   }

   if ( bDisplay ) {

		strOutput += "<tr><td colspan=\"4\" class=\"basiccontent\" style=\"width:514px;height:2px;\"><img src=\"spacer.gif\" width=\"514\" height=\"2\" alt=\"\"></td></tr>";
		strOutput += "<tr>";
		strOutput += "<td colspan=\"3\" class=\"basiccontent\" style=\"padding-left:165px;\"><b>SUBTOTAL</b></td>";
		strOutput += "<td class=\"basiccontent\"><div align=\"center\"><b>£" + strTotal + "</b></div></td>";
		strOutput += "</tr>";

		strOutput += "<tr>";
		strOutput += "<td class=\"basiccontent\" colspan=\"4\"><br><br>Please ensure all order details are correct and select your shipping option before proceeding.<br><br></td>";
		strOutput += "</tr>";

		strOutput += "</table>";
		strOutput += "<br><br>";

                   }

   document.write(strOutput);

}

