var optionFlags = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x" ];
var Options = new Array(24);
Options[0] = [ "Size: ", "1", "1&#189;", "2", "2&#189;", "3", "3&#189;", "4", "4&#189;", "5", "5&#189;", "6", "6&#189;", "7", "7&#189;", "8", "8&#189;", "9", "9&#189;", "10", "10&#189;", "11", "11&#189;", "12", "12&#189;", "13", "13&#189;", "14", "14&#189;", "15", "15&#189;", "16" ];
Options[1] = [ "Type: ", "Youth", "Adult" ];
Options[2] = [ "Fitting: ", "Narrow", "Medium", "Wide" ];
Options[3] = [ "Size: ", "Small", "Medium", "Large", "X-Large" ];
Options[4] = [ "Color: ", "White", "Black", "Red", "Green", "Blue", "Purple", "Gray" ];
Options[5] = [ "Tartan: ", "Antrim", "Armagh", "Carlow", "Cavan", "Clare", "Connacht", "Cork", "Donegal", "Down", "Dublin", "Fermanagh", "Galway", "Irish National", "Kerry", "Kildare", "Kilkenny", "Laois", "Leitrim", "Limerick", "Londonderry", "Longford", "Louth", "Mayo", "Monaghan", "Offlay", "Oreil", "Roscommon", "Sligo", "Strathclyde", "Tara Murphy", "Tipperary", "Tyrone", "Ulster Red", "Waterford", "Westmeath", "Wexford", "Wicklow" ];
Options[6] = [ "Gem Stone: ", "Amber", "Moonstone", "Carnelian", "Lapis", "Malachite", "Amethyst", "Turquoise", "Black Onyx" ];
Options[7] = [ "Color: ", "Red", "Green", "Blue", "Silver", "Gold", "White", "Yellow", "Rose" ];
Options[8] = [ "Delivery: ", "Email", "Mail", "Both" ];
Options[9] = [ "Finish: ", "Natural", "Wood" ];
Options[10] = [ "Lid: ", "Knotted", "Shingles" ];
Options[11] = [ "Size: ", "Petite", "Small", "Medium", "Large" ];
Options[12] = [ "Color: ", "Red", "Green", "Blue", "Violet", "Pink" ];
Options[13] = [ "Gem Stone: ", "Clear", "Ruby" ];
Options[14] = [ "Gem Stone: ", "January-Garnet", "February-Amethyst", "March-Bloodstone", "April-Clear Quartz", "May-Green Agate", "June-Moonstone", "July-Carnelian", "August-Jade", "September-Aventurine", "October-Jasper", "November-Lapis", "December-Turquoise" ];
Options[15] = [ "Tip: ", "Square", "Round" ];
Options[16] = [ "Style: ", "Flex-Sole", "Standard" ];
Options[17] = [ "Brooch: ", "Sterling Silver", "Pewter" ];
Options[18] = [ "Style: ", "Circle Shamrocks", "Square Shamrocks", "Single Shamrock", "Sprig Shamrocks", "Random Shamrocks" ];
Options[19] = [ "Country: ","Australia", "Italy", "Canada", "Ireland", "England", "France", "Bahamas" ];
Options[20] = [ "Set: ", "Clip and Links", "Clip and Keychain" ];
Options[21] = [ "Length: ", "Championship", "Knee" ];
Options[22] = [ "Size: ", "Mini", "Small/Medium", "Large/X-Large" ];
Options[23] = [ "Style: ", "Navy Border", "Green Border", "Wine Border" ];

function Update(Sku,Qty){

  var offset;
  var Order;
  var value;
  var CookieData;
  var AllCookies = "";

  value = Math.floor(Qty.value);
  if(isNaN(value) || value < 1){ value = 1; }
  else{ Qty.value = value; }

  CookieData = new String(unescape(document.cookie));
  AllCookies = CookieData.split(";");

  if(AllCookies[0].indexOf("Order=", 0) != -1) AllCookies[0] = AllCookies[0].substring(AllCookies[0].indexOf("=", 0)+1, AllCookies[0].length);

  offset = AllCookies[0].indexOf(Sku, 0);

  if(offset != -1){
     offset = AllCookies[0].indexOf("~", offset) + 1;
     Order = AllCookies[0].substring(0, offset);
     offset = AllCookies[0].indexOf("~", offset);
     document.cookie = "Order=" + escape(Order + value + AllCookies[0].substring(offset, AllCookies[0].length));

     document.location=document.location;
  }
}

function Delete(Sku){

  var offset;
  var Order;
  var CookieData;
  var AllCookies = "";

  CookieData = new String(unescape(document.cookie));
  AllCookies = CookieData.split(";");

  AllCookies[0] = AllCookies[0].substring(AllCookies[0].indexOf("=", 0)+1, AllCookies[0].length);
  AllCookies[1] = AllCookies[1].substring(AllCookies[1].indexOf("=", 0)+1, AllCookies[1].length);

  offset = AllCookies[1].indexOf(Sku, 0);

  if(offset != -1){
     Custom = AllCookies[1].substring(0, offset);
     offset = AllCookies[1].indexOf("|", offset) + 1;
     CookieData = escape(Custom + AllCookies[1].substring(offset, AllCookies[1].length));
     if(CookieData == "") document.cookie = "Custom=Empty";
     else document.cookie = "Custom=" + CookieData;
  }

  offset = AllCookies[0].indexOf(Sku, 0);

  if(offset != -1){
     Order = AllCookies[0].substring(0, offset);
     offset = AllCookies[0].indexOf("|", offset) + 1;
     CookieData = escape(Order + AllCookies[0].substring(offset, AllCookies[0].length));
     if(CookieData == "") document.cookie = "Order=Empty";
     else document.cookie = "Order=" + CookieData;

     document.location=document.location;
  }
}

function DeleteAll(){

  var ExpTime = new Date();
  ExpTime.setTime(1000);

  document.cookie = "Order=;expires=" + ExpTime.toGMTString();
  document.location=document.location;
}

function ShowCart(){

  var offset;
  var Sku;
  var Qty;
  var Name;
  var Price;
  var SubTotal = 0;
  var Discount = 0;
  var Tax = 0;
  var Shipping = 0;
  var Total = 0;
  var CookieData;
  var AllCookies = "";


  CookieData = new String(unescape(document.cookie));
  AllCookies = CookieData.split(";");
  AllCookies[0] = AllCookies[0].replace(/\+/g, " ");
  if(AllCookies[1]) AllCookies[1] = AllCookies[1].replace(/\+/g, " ");

  document.write("<table border=\"0\" cellPadding=\"0\" width=\"460\"><tr><td bgColor=\"#000000\" vAlign=\"top\" width=\"100%\"> \
                 <table border=\"0\" cellPadding=\"4\" width=\"100%\"><tr><td bgColor=\"#ffcc33\" colSpan=\"2\"> \
                 <span class=\"bold\">Your CeltiCottage Shopping Cart</span></td></tr><tr><td bgColor=\"#ffffff\" colSpan=\"2\">");

  if(AllCookies[0].indexOf("Order=Empty", 0) != -1 || AllCookies[0] == ""){

     document.write("<span class=bold>Your Shopping Cart is Empty!</span>");
  }
  else{

     document.write("\n<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\">\n\t<tr bgcolor=\"#dddddd\" align=\"center\"> \
                      <td><b>Item</b></td><td><b>Description</b></td><td><b>Qty</b></td> \
                      <td><b>Price</b></td><td><b>Total</b></td><td><b>Delete</b></td></tr>\n<form>");

     offset = AllCookies[0].indexOf("=", 0);
     AllCookies[0] = AllCookies[0].substring(offset+1, AllCookies[0].length-1);

     var AllOrders = AllCookies[0].split("|");

     for(i = 0; i < AllOrders.length; i++){

         var EachProduct = AllOrders[i].split("~");
         Sku = EachProduct[0];
         Qty = EachProduct[1];
         Name = EachProduct[2];
         Price = EachProduct[3];

         SubTotal += Qty*Price;

         //Parse Product Options
         offset = Sku.indexOf("-", 0);
         multiple = false;

         if(offset != -1){
            Name = Name + "<br>";
            for(j = 0; j < optionFlags.length; j++){

                if((where = Sku.indexOf(optionFlags[j], offset)) != -1){

                    if(multiple == true) Name = Name + ", ";
                    Temp = Sku.substring(where+1, Sku.length);
                    Name = Name + Options[j][0] + Options[j][parseInt(Temp)];
                    multiple = true;

                    if((where = AllCookies[1].indexOf(Sku, 0)) != -1){
                       Temp = AllCookies[1].substring(AllCookies[1].indexOf("~", where)+1, AllCookies[1].indexOf("|", where));
                       if(Temp != "nomessage" && Temp != "") Name = Name + ", Custom: " + Temp;
                    }
                }
            }
            if(multiple == false) Name = Name + "Style: " + Sku.substring(offset+1, Sku.length);
         }
         document.write("\t<tr height=35 bgcolor=#d6eee6><td width=50>" + Sku + "</td><td width=380>" + Name + "</td>");
         document.write('<td width30><input type="text" size="2" value=' + Qty + ' onchange="javascript:Update(\'' + Sku + '\', this)" maxlength="2"></td>');
         document.write("<td width=55>$" + FmtPrice(Price) + "</td><td width=55>$" + FmtPrice(Qty*Price) + "</td>");
         document.write("<td align=top><a href=javascript:Delete(\'" + Sku + "\')><img align=top border=0 src=images/delete.gif alt=Delete></a></td></tr>");
     }
     document.write("</form>");
     Total = SubTotal + Tax + Shipping;
     document.write("<tr bgcolor=dddddd><td align=right colspan=4><b>Sub Total&nbsp;</b></td><td bgcolor=d6eee6>$" + FmtPrice(SubTotal) + "</td><td>&nbsp;</td></tr>");
     document.write("<tr bgcolor=dddddd><td align=right colspan=4>(5% Virginia residents only) <b>Tax&nbsp;</b></td><td bgcolor=d6eee6>$" + FmtPrice(Tax) + "</td><td>&nbsp;</td></tr>");
     document.write("<tr bgcolor=dddddd><td align=right colspan=4><b>Freight&nbsp;</b></td><td bgcolor=d6eee6>$" + FmtPrice(Shipping) + "</td><td>&nbsp;</td></tr>");
     document.write("<tr bgcolor=dddddd><td align=right colspan=4><b>Total&nbsp;</b></td><td bgcolor=d6eee6>$" + FmtPrice(Total) + "</td><td>&nbsp;</td></tr>");
     document.write("</table>\n\n");
  }
  document.write("</td></tr></table></td></tr></table>");
}

function FmtPrice(each){

        var price = "0.00";
        var str = new String(each);

        if(str.length >= 2){
                price = str.substring(0, str.length - 2);
                price = price + "." + str.substring(str.length - 2, str.length);
        }
        return price;
}

function CheckDetails(){

  if(document.cookie.indexOf("Order=Empty", 0) == 0){
     alert("Your shopping cart is empty - please hit the \"Home\" button to purchase products");
     return false;
  }

  with(document.checkout){
       if(last.value == "" || first.value == "" || address1.value == "" || city.value == "" ||
          phone.value == "" || email.value == ""){
                alert("You must complete all needed \"Billing Information\" fields before continuing.");
                return false;
       }
       else if(country[country.selectedIndex].value == "United States" && (zip.value == "" || state.value == "")){
                alert("You must complete all needed \"Billing Information\" fields before continuing.");
                return false;
       }

       if(slast.value == "" && sfirst.value == "" && saddress1.value == "" && scity.value == "" && szip.value == ""){
          scountry.selectedIndex = country.selectedIndex;
          sfirst.value = first.value;
          slast.value = last.value;
          saddress1.value = address1.value;
          saddress2.value = address2.value;
          scity.value = city.value;
          sstate.value = state.value;
          szip.value = zip.value;
          sphone.value = phone.value;          
       }

       if(slast.value == "" || sfirst.value == "" || saddress1.value == "" || scity.value == "" ||
          (scountry[scountry.selectedIndex].value == "United States" && (sstate.value == "" ||
           szip.value == "" || sphone.value == ""))){
          alert("You must complete all needed \"Shipping Information\" fields before continuing.");
          return false;
       }
  }
  return true;
}

function CheckCC(){

  var ret = false;
  var type = "";
  var now = new Date();

  with(document.checkout){

       if(document.cookie.indexOf("Order=Empty", 0) == 0){
          alert("Your shopping cart is empty - please hit the \"Home\" button to purchase products");
          return ret;
       }

       if(Total.value == "0"){
          submit();
          ShowThanks();
          return true;
       }

       if(ccname.value == "" || cctype[cctype.selectedIndex].value == "" ||
          ccnumber.value == "" || cccvv.value == "" || month[month.selectedIndex].value == "" ||
          year[year.selectedIndex].value == ""){
          alert("You must complete all fields of the \"Credit Card Information\" form before continuing.");
       }
       else{
          cctemp = new Array(16);
          ccsum = 0;

          ccnum = ccnumber.value.replace(/\D/g, "", ccnumber.value);
          cclen = ccnum.length-1;
          ccnumber.value = ccnum;

          for(i = cclen, j = 0; i >= 0; i--, j++){
              cctemp[j] = (j % 2) ? ccnum.charAt(i) * 2 : ccnum.charAt(i);
              if(cctemp[j] >= 10){
                 cctemp[j] = cctemp[j].toString();
                 cctemp[j] = parseInt(cctemp[j].substr(0,1)) + parseInt(cctemp[j].substr(1,1));
              }
          }
          for(j = 0; j <= cclen; j++) ccsum += parseInt(cctemp[j]);

          if(ccsum % 10){
             alert("Please enter valid credit card data.");
          }
          else{
             //Check Master Card
             if(/^[5][1-5]/.test(ccnumber.value) && ccnumber.value.length == 16) type = "Master Card";
             //Check Visa
             else if(/^[4]/.test(ccnumber.value) && (ccnumber.value.length == 13 || ccnumber.value.length == 16)) type = "Visa";
             //Check AMEX
             else if(/^[3][4,7]/.test(ccnumber.value) && ccnumber.value.length == 15) type = "AMEX";
             //Check Discover
             else if(ccnumber.value.indexOf('6011') == 0 && ccnumber.value.length == 16) type = "Discover";
             //Bad Credit Card

             if(type == cctype[cctype.selectedIndex].value){
                if(now.getFullYear() < year[year.selectedIndex].value)
                    ret = true;
                else if(now.getFullYear() == year[year.selectedIndex].value && now.getMonth() < month[month.selectedIndex].value)
                    ret = true;

                if(ret == false) alert("Your credit card is expired, please enter a different card number.");
             }
             else
                alert("Please enter valid credit card data.");
          }
       }
  }
  return ret;
}
