function getCookieVal(offset) {
   var endstr=document.cookie.indexOf(";", offset);
   if (endstr==-1) endstr=document.cookie.length;
   return unescape(document.cookie.substring(offset,endstr)); }

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) {
   var argv=SetCookie.arguments;
   var argc=SetCookie.arguments.length;
   var expires=(argc>2) ? argv[2]:null;
   var path=(argc>3) ? argv[3]:null;
   var domain=(argc>4) ? argv[4]:null;
   var secure=(argc>5) ? argv[5]:false;
   document.cookie=name+"="+escape(value)+((expires==null) ? "" : ("; expires="+expires.toGMTString()))+((path==null) ? "" : ("; path="+path))+((domain==null) ? "" : ("; domain="+domain))+((secure==true) ? "; 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"; }}

var load;
var Num=GetCookie("number");
var Item_List=new String(GetCookie("items"));
var I=Item_List.split("!!");
var Price_List=new String(GetCookie("prices"));
var P=Price_List.split("!!");
var Q_List=new String(GetCookie("quantity"));
var Q=Q_List.split("!!");
var S_List=new String(GetCookie("shipping"));
var S=S_List.split("!!");
var Url_List=new String(GetCookie("urls"));
var U=Url_List.split("!!");
var expdate=new Date();
expdate.setTime(expdate.getTime()+(24*60*60*1000));

function Decimal(n) {
   ns=new String(n);
   nsi=ns.indexOf('.');
   if (nsi!=-1) {
      nsil=nsi+3;
      l=ns.charAt(nsil);
      ln=new Number(l);
      if (ln>=5) n=n+.01;
      nr=new String(n);
      n=nr.substring(0,nsil);
      if (n.length<nsil) n+="0"; }
   else {
      n=ns; }
   return n; }

function getItems() {
   document.write('<select name="Items" size="1" ID="f">');
   if ((Num==null) || (Num<0)) {
      document.write('<option value="Null">Your Shopping Cart Is Empty</option>'); }
   else {
      var IL=new Array();
      for (var i=0; i<=Num; i++) {
         if ((I[i]!="undefined") && (I[i]!="")) {
         P[i]=new Number(P[i])*new Number(Q[i]);
         P[i]=Decimal(P[i]);
         Q[i]=Decimal(Q[i]);
         if (I[i].length>40) {
            IL[i]=I[i].substring(0,32)+"..."; }
         else {
            IL[i]=I[i]; }
         document.write('<option value='+U[i]+'>' + '"' +IL[i] + '"' +' x '+Q[i]+' = $'+P[i]+'</option>'); }}}
   document.write('<option value="Null">_______________________________________________</option></span></select>'); }

function getInput() {
   if ((Num!=null) && (Num>=0)) {
      for (var i=0; i<=Num; i++) {
         var ST=new Array(Num)
         if (i==0) {
            S[i]=new Number(S[i]);
         } else {
            S[i] = new Number(0);
         }
         S[i]=Decimal(S[i]);
         ST[i]=new Number(P[i]);
         if (i==0) {
            ST[i] = ST[i] +  new Number(S[i]);
         }
         ST[i]=Decimal(ST[i]);
         document.write('<input type="hidden" name="I[]" value="'+I[i]+'">\n');
         document.write('<input type="hidden" name="Q[]" value="'+Q[i]+'">\n');
         document.write('<input type="hidden" name="P[]" value="'+P[i]+'">\n');
         document.write('<input type="hidden" name="S[]" value="'+S[i]+'">\n');
         document.write('<input type="hidden" name="ST[]" value="'+ST[i]+'">\n'); }
      var SHT=0;
      for (var i=0; i<=Num; i++) {
         SHT=SHT+new Number(S[i]); }
      SHT=Decimal(SHT);
      document.write('<input type="hidden" name="SHT" value="'+SHT+'">');
      var SHST=0;
      for (var i=0; i<=Num; i++) {
         SHST=SHST+new Number(P[i]); }
      SHST=Decimal(SHST);
      document.write('<input type="hidden" name="SHST" value="'+SHST+'">'); }}

function Total() {
   if ((Num==null) || (Num<0)) {
      document.phpCart.Total.value="0.00"; }
   else {
      var T=new Array(Num)
      for (var i=0; i<=Num; i++) {
         T[i]=new Number(P[i])+new Number(S[i]); }
      var Total=0;
      for (var i=0; i<=Num; i++) {
         Total+=T[i]; }
      Total=Decimal(Total);
      document.phpCart.Total.value=Total; }
	  }

function turnOn(button) {
   if (load==1) {
      document[button].src=eval(button+"on.src"); }}

function turnOff(button) {
   if (load==1) {
      document[button].src=eval(button+"off.src"); }}

function Remove() {
   P=Price_List.split("!!");
   Q=Q_List.split("!!");
   S=S_List.split("!!");
   var val=(document.phpCart.Items.options[document.phpCart.Items.selectedIndex].value);
   if (val=="Null") {
      alert("Sorry, your selection is invalid and cannot be removed!"); }
   else {
      var item=(document.phpCart.Items.options[document.phpCart.Items.selectedIndex].text);
      if (confirm("Are you sure you want to remove "+item+" from your shopping cart?")) {
         var ix=document.phpCart.Items.selectedIndex;
         var ni=Num-1;
         for (var j=ix; j<=ni; j++) {
            I[j]=I[j+1]; }
         I[ni+1]="";
         var ei=I.join("!!");
         ri=/!!!!/
         var Items=ei.replace(ri,"!!");
         SetCookie("items",Items,expdate);
         for (var j=ix; j<=ni; j++) {
            P[j]=P[j+1]; }
         P[ni+1]="";
         var ep=P.join("!!");
         rp=/!!!!/
         var Prices=ep.replace(rp,"!!");
         SetCookie("prices",Prices,expdate);
         for (var j=ix; j<=ni; j++) {
            Q[j]=Q[j+1]; }
         Q[ni+1]="";
         var eq=Q.join("!!");
         rq=/!!!!/
         var Quantity=eq.replace(rq,"!!");
         SetCookie("quantity",Quantity,expdate);
         for (var j=ix; j<=ni; j++) {
            S[j]=S[j+1]; }
         S[ni+1]="";
         var es=S.join("!!");
         rs=/!!!!/
         var Shipping=es.replace(ri,"!!");
         SetCookie("shipping",Shipping,expdate);
         for (var j=ix; j<=ni; j++) {
            U[j]=U[j+1]; }
         U[ni+1]="";
         var eu=U.join("!!");
         ru=/!!!!/
         var Urls=eu.replace(ru,"!!");
         SetCookie("urls",Urls,expdate);
         var newNum=--Num;
         SetCookie("number",newNum,expdate);
         window.location="index.php?subject=buythebooks";
		 }}}

function View() {
   var loc=(document.phpCart.Items.options[document.phpCart.Items.selectedIndex].value);
   if (loc=="Null") {
      alert("Sorry, your selection is invalid and cannot be viewed!"); }
   else {
      window.location="index.php?subject=buythebooks";
	  }}

function Empty() {
   if ((Num==null) || (Num<0)) {
      alert("Sorry, your shopping cart is already empty!"); }
   else {
      if(confirm("Are you sure you want to delete all items from your shopping cart?")) {
         DeleteCookie("number");
         DeleteCookie("items");
         DeleteCookie("prices");
         DeleteCookie("quantity");
         DeleteCookie("shipping");
         DeleteCookie("urls");
         window.location="index.php?subject=buythebooks";
		 }}}

function Add1(id,item,price,ship,loc) {
   if (document.forms[1]) {
      var q=document.forms[1][id].value; }
   else {
      var q=document.forms[0][id].value; }
   qCheck=parseInt(q, 10);
   if ((qCheck==0) || (isNaN(qCheck)) || (q=="")) q=1;
   Num=GetCookie("number");
   var Items=GetCookie("items");
   var Prices=GetCookie("prices");
   var Quantity=GetCookie("quantity");
   var Shipping=GetCookie("shipping");
   var Shipping='5.95';

   var Urls=GetCookie("urls");
   if (Num==null) {
      SetCookie("number",0,expdate);
      SetCookie("items",item+"!!",expdate);
      SetCookie("prices",price+"!!",expdate);
      SetCookie("quantity",q+"!!",expdate);
      SetCookie("shipping",ship+"!!",expdate);
      SetCookie("urls",loc+"#"+id+"!!",expdate); }
   else {
	var ix="f";
	I=Items.split("!!");
      for (var j=0; j<=Num; j++) {
	   if (I[j]==item) var ix=j; }
	if (ix!="f") {
	   Q=Quantity.split("!!");
	   Q[ix]=new Number(Q[ix])+new Number(q);
         var eq=Q.join("!!");
         rq=/!!!!/
         var Quantity=eq.replace(rq,"!!");
         SetCookie("quantity",Quantity,expdate); }
	else {
	   var newN=++Num;
         SetCookie("number",newN,expdate);
         SetCookie("items",item+"!!"+Items,expdate);
         SetCookie("prices",price+"!!"+Prices,expdate);
         SetCookie("quantity",q+"!!"+Quantity,expdate);
         SetCookie("shipping",ship+"!!"+Shipping,expdate);
         SetCookie("urls",loc+"#"+id+"!!"+Urls,expdate); }}
  window.location="index.php?subject=buythebooks";
  }

function Check_Out() {
   if ((Num==null) || (Num<0)) {
      alert("Sorry, you do not have any items in your shopping cart!"); }
   else {
      document.phpCart.action.value="Order";
      document.phpCart.submit(); }}

//© This code is a copyright of WEBMASTERS.COM. All rights reserved. Unauthorized use is strictly prohibited.
