<!--

IEwrap = (navigator.appName == "Netscape") ? " ":"\n";
Netreload = (navigator.appName == "Netscape") ? ' onResize="location.reload();"' : '';

//=================================================================
// fixBug - fixes a Netscape rounding bug
function fixBug(value) {
     neg = false;
    if (value < 0) {
      neg = true;
      value *= -1;
    }
    if (value<=0.99 ) newDollars = '0';
	  else newDollars = parseInt(value);
		newCents = parseInt((value+.0008 - newDollars) * 100);
		if (eval(newCents) <= 9) newCents='0'+newCents;
		newString = newDollars + '.' + newCents;
		if (neg == true) newString *= -1;
    return (newString);
	}
//=================================================================
 function setcookie(theCookie) {
    var theDate = new Date();
    theDate.setTime(theDate.getTime() +(14 * 24 * 60 * 60 * 1000));  
    in2weeks = theDate.toGMTString();
    document.cookie = "TMBcart="+theCookie+"; expires="+in2weeks+"; domain=missingbite.com; path=/";
}
//=================================================================
function getcookie(name) {
   re = new RegExp(name + "=([^;]+)");
   value = re.exec(document.cookie);
   return (value != null) ? unescape(value[1]) : "";
}
//=================================================================
//Loads the cart cookie into an object array "cart"
function cartarray() {
   dc =getcookie("TMBcart");
   dc = dc.substring(1,dc.length-1);     //Verison initial [ and final ]
   ar = dc.split("][");
   var cart = new Array();
   for (i=0; i<ar.length; i++) {
       temp = ar[i].split("|");
       cart[i] = {desc:temp[0], price:temp[1], quan:temp[2]};
    }
  return cart;
}
//=================================================================
function recalcsubtotal() {
    cart = cartarray();
    subtotal = 0;
    for (i=0; i < cart.length; i++) {
        itemtotal=eval(cart[i].price*cart[i].quan);
        subtotal += itemtotal;
	}
}

//=================================================================
function formatlist() {
//Reformat the cart for outgoing
// $1 = from the first "[" to the first "|" (description), drop the price, $2 = the last digits before the "]" (quantity)

  dc = getcookie("TMBcart");
 // do {
 //     ndx = dc.indexOf("[Gift");
 //     if  (ndx != -1) {
 //         end =  dc.indexOf("]",ndx)+1;
 //        dc = dc.substr(0,ndx) + dc.substring(end,dc.length);
 //     }
 //    } while (dc.indexOf("[Gift") != -1);

  re = /\[([^|]+)\|-?\d{1,4}\.\d{2}\|(\d+)\]/g;
  dc = dc.replace(re, "$1($2), ");
  dc = unescape(dc.substr(0,(dc.length-2)));    //Remove last comma and %00 (if there)

  return dc;    
}
//=================================================================
function addcert() {
do {
   it = prompt('Enter the promotion code \n you received in your email.','');
 } while((it != null && it !="") && (it.search(/\D/) != -1 || it.length !=9)); 

if (it != null && it !="")  {
   auc = 0;
   for (k=1; k<=auction.length; k++)  {
       if (!auction[k]) continue;
       if (auction[k].number == it) auc= k;
    }
   if (auc==0) alert ('This promotion code is not valid. \nPlease contact service@missingbite.com');
   else {
       rc =getcookie("TMBcart");
       if (rc.indexOf(auction[auc].desc) == -1) {
         rc += "["+auction[auc].desc+"|"+auction[auc].price+"|1]";
         setcookie(rc);
     }  }   
  }
  self.location.reload();
}
//=================================================================

//Show Cart Contents	
 function showcart() {
    cart = cartarray();
    subtotal = 0.00;
    for (i=0; i < cart.length; i++) {
        itemtotal=eval(cart[i].price*cart[i].quan);
        subtotal += itemtotal;
        document.writeln('<TR><TD>'+cart[i].desc+'</TD><TD ALIGN=CENTER>');
		document.writeln('<INPUT TYPE=TEXT NAME="quan'+i+'" VALUE="'+cart[i].quan+'" SIZE=3 MAXLENGTH=2 onChange="updateQuan(document.updateform)">');
        document.writeln('</TD><TD ALIGN=RIGHT>'+cart[i].price+'</TD>');
        document.writeln('<TD ALIGN=RIGHT>'+fixBug(itemtotal)+'</TD>');
        document.writeln('<TD ALIGN=CENTER><FONT SIZE=1><a href="javascript:removeItem('+i+')"<CENTER>');
        document.writeln('<IMG SRC="../std/delete.gif" BORDER=0></CENTER></A></FONT></TD></TR>');
    }
  document.write('<TR><TD COLSPAN=2 ALIGN=CENTER>');
  document.write('<A HREF="Javascript:updateQuan(document.updateform)">Update Quantities</A>');
  //document.write('<input type=button value="Update Quantities" onClick=updateQuan(document.updateform)>');
  document.writeln('</TD><TD COLSPAN=2 ALIGN=RIGHT>');
  document.write('<B>Sub Total&nbsp;&nbsp; $'+fixBug(subtotal)+'</B>');
  document.writeln('</TD><TD>&nbsp;</TD></TR>');

 // This is for contributions...	
 //   st = new String(subtotal * 0.1);
 //   ndx = st.indexOf('.');
 //   st = st.substring(0,ndx) + st.substr(ndx,3);
 //   document.write('<TR><TD COLSPAN=5 BGCOLOR=WHITE ALIGN=CENTER><FONT COLOR=RED>Your contribution to the American Red Cross: $'+st+'</FONT></TD></TR>');
 //-----------------
 
  document.writeln('</TABLE><BR></FORM>');	
}
	
//=================================================================
//Remove item from cart	
function removeItem(itemno) {
    cart = cartarray();
    newItemList = "";
    for (i=0; i < cart.length; i++) {
				if (i != itemno) newItemList += '['+ cart[i].desc +'|'+ cart[i].price +'|'+ cart[i].quan +']';
	}

    if ((newItemList.substr(1,4) == "Gift" || newItemList.substr(1,8) == "Discount") && newItemList.indexOf(']') == newItemList.length-1) newItemList = "";

	setcookie(newItemList);
	if (newItemList == "") self.location = "../index.html";
    else self.location.reload();
}
//=================================================================
function getInv(desc) {
    for (k=1; k<=oh.length-1; k++)  {
      if (!oh[k]) continue;
      if (oh[k].desc == desc) return oh[k].inv;
   }
 }
//=================================================================
//Update quantities of cart
function updateQuan(updateform) { 

if (quanUpdated == true) {      // When the quantity is set by max in inventory, we don't want a second update.
    quanUpdated = false;
    return;
  } 
  else quanUpdated = true;
  
 for (i=0; i < updateform.length; i++) {
     if (updateform[i].value.search(/\D/) != -1) {
         alert("Invalid quantity entered.");
         updateform[i].select();
         return;
     }
     if (updateform[i].value > 10) {
        alert("Please email us at service@missingbite.com for purchases of quantities over 10 to insure we have the items in stock.");
        return;
     }

   }
    newItemList = "";
    cart = cartarray();
    for (i=0; i < updateform.length; i++) {
         newquan = updateform[i].value;
	     	if (newquan== 0)  {                            // they wanna kill it
            if (updateform.length == 1)  {         // last entry in cart
                 removeItem(0); 
                 return; 
            }   else continue;                            //just drop it from the list
          } else {
            available = getInv(cart[i].desc);    //check inventory
            if (newquan > available) {
                alert('The quantity of the '+cart[i].desc+'s is limited.\n \nWe have updated your order to '+available+'.');
               newquan = available;
            }
          if (cart[i].desc.substring(0,4) == "Gift" || cart[i].desc.substring(0,8) == "Discount") newItemList += '['+ cart[i].desc +'|'+ cart[i].price +'|1]';
          else newItemList += '['+ cart[i].desc +'|'+ cart[i].price +'|'+ newquan +']'
       }
     }  
   
    if ((newItemList.substr(1,4) == "Gift" || newItemList.substr(1,8) == "Discount") && newItemList.indexOf(']') == newItemList.length-1) newItemList = "";
	setcookie(newItemList);
	recalcsubtotal();
	if (newItemList == "") self.location = "../index.html";
    else self.location.reload();
}
//=================================================================
//Display Checkout Page
function checkout() {
    cart = cartarray();
    subtotal = 0;
    for (i=0; i < cart.length; i++) {
        itemtotal=eval(cart[i].price*cart[i].quan);
        subtotal += itemtotal;
        document.write('<TR><TD>'+cart[i].desc+'</TD>');
		document.writeln('<TD ALIGN=RIGHT>'+cart[i].quan+'</TD><TD ALIGN=RIGHT>'+cart[i].price+'</TD>');
        document.writeln('<TD ALIGN=RIGHT>'+itemtotal+'</TD></TR>');
     }
    rc=getcookie("TMBorder").split(","); 
    orderID = rc[0];
    shipto = rc[1];
    total = rc[2];
    shiptype = rc[3];
    if (shipto == "US") {
        if (subtotal < 19) shippingcharge = 3.95;
        else shippingcharge = 8.95;
     }
     else if (shipto == "IN") {
     	if (subtotal < 19) shippingcharge = 10.00;
        else shippingcharge = 25.00;
     }
	 else if (shipto == "FR") shippingcharge = 0.00;

	document.writeln('<TR><TD COLSPAN=3><B><FONT COLOR=red>'+shiptype+'</FONT></B>');
    document.writeln('</TD><TD ALIGN=RIGHT>'+fixBug(shippingcharge)+'</TD></TR>');
	document.writeln('<TR><TD COLSPAN=3><B>Total To Be Charged</B>');
    document.writeln('</TD><TD ALIGN=RIGHT><B>');
    document.writeln(((shipto == "US")?"":"&nbsp;US ") +'$'+total+ '</B></TD></TR></TABLE><BR>');

    document.write('<FORM NAME="PPorder" METHOD="POST" ACTION="https://www.paypal.com/cgi-bin/webscr">');
	document.write('<INPUT TYPE="hidden" NAME="cmd" VALUE="_xclick">');
	document.write('<INPUT TYPE="hidden" NAME="business" VALUE="service@missingbite.com">'); 
	document.write('<INPUT TYPE="hidden" NAME="image_url" VALUE="https://secure.missingbite.com/std/TMBPayPal.jpg">');
	document.write('<INPUT TYPE="hidden" NAME="item_name" VALUE="' + formatlist() + '">');
	document.write('<INPUT TYPE="hidden" NAME="item_number" VALUE="' + orderID + '">');
	document.write('<INPUT TYPE="hidden" NAME="amount" VALUE="'+fixBug(subtotal) + '">');
	document.write('<INPUT TYPE="hidden" NAME="shipping" VALUE="' + fixBug(shippingcharge) + '">');
	document.write('<INPUT TYPE="hidden" NAME="return" VALUE="http://missingbite.com/scripts/thankyou.htm">');
    document.write('<INPUT TYPE="hidden" NAME="cancel_return" VALUE="http://missingbite.com/scripts/cancel.html">');
	document.writeln('</FORM>');

}
//=================================================================
function checkoutPP() {
    document.PPorder.submit();
} 

//=================================================================
 function checkout3() {
  rc=getcookie("TMBorder").split(","); 
  if (rc[4]) {
     fromAddr = rc[4];
     subj = "CC-Checkout: ";
  } else {
     fromAddr = "checkout@PayPal.com";
     subj = "PP-Checkout: ";
  }
 
  msg = "Country = " + rc[1] + "\nTotal = " + rc[2] + "\nCart = " + formatlist();
  
  document.writeln('<form name="thankyou" method="post" action="TMBmail.cgi">');
  document.writeln('<input type="hidden" name="FROMADDR" value='+fromAddr+'>');
  document.writeln('<input type="hidden" name="TOADDR" value="service@missingbite.com">');
  document.writeln('<input type="hidden" name="SUBJECT" value="'+subj+rc[0]+'">');
  document.writeln('<input type="hidden" name="NEWURL" value="http://missingbite.com/scripts/thankyou.html">');
  document.writeln('<input type="hidden" name="MESSAGE" value="'+msg+'">');
  document.writeln('</form>');
  document.thankyou.submit();
}

//-->