/*
**
**    items.js - Script support for items.htm in the
**               Cuddly Critters website.
**
** 06/28/06 - jf - Created.
**
*/


ELECNT = 7
ITEMCNT = 100
TOTALSZ = (ELECNT * ITEMCNT)
TOTALITEMS = 17


var itemData = new Array
var cartData = new Array(TOTALSZ)

var paymentType = new String("Email")


/*
**    This function will normalize a floating-point number to monetary
** format with two decimal places and trailing zeroes.
*/

function to_dollars(value)
{
var temp = new String(value);

    if (temp.indexOf(".") != -1)
    {
      temp = temp.substring(0, (temp.indexOf(".") + 3));

      if (temp.charAt(temp.indexOf(".") + 1) == "")
      {
        temp = (temp + "0");
      }

      if (temp.charAt(temp.indexOf(".") + 2) == "")
      {
        temp = (temp + "0");
      }
    }
    else
    {
      temp = (temp + ".00");
    }

    return(temp);
}


/*
**    Calculates the subtotal for the given item.
*/

function get_subtotal(item)
{
var subtotal = new String("");

    switch (item)
    {
      case "t1":
        document.itemForm.t1Sub.value = to_dollars(document.itemForm.t1Qty.value *
          document.itemForm.t1Price.value);
        break;

      case "t2":
        document.itemForm.t2Sub.value = to_dollars(document.itemForm.t2Qty.value *
          document.itemForm.t2Price.value);
        break;

      case "t3":
        document.itemForm.t3Sub.value = to_dollars(document.itemForm.t3Qty.value *
          document.itemForm.t3Price.value);
        break;

      case "t4":
        document.itemForm.t4Sub.value = to_dollars(document.itemForm.t4Qty.value *
          document.itemForm.t4Price.value);
        break;

      case "t5":
        document.itemForm.t5Sub.value = to_dollars(document.itemForm.t5Qty.value *
          document.itemForm.t5Price.value);
        break;

      case "t6":
        document.itemForm.t6Sub.value = to_dollars(document.itemForm.t6Qty.value *
          document.itemForm.t6Price.value);
        break;

      case "t7":
        document.itemForm.t7Sub.value = to_dollars(document.itemForm.t7Qty.value *
          document.itemForm.t7Price.value);
        break;

      case "m1":
        document.itemForm.m1Sub.value = to_dollars(document.itemForm.m1Qty.value *
          document.itemForm.m1Price.value);
        break;

      case "m2":
        document.itemForm.m2Sub.value = to_dollars(document.itemForm.m2Qty.value *
          document.itemForm.m2Price.value);
        break;

      case "tt1":
        document.itemForm.tt1Sub.value = to_dollars(document.itemForm.tt1Qty.value *
          document.itemForm.tt1Price.value);
        break;

      case "p1":
        document.itemForm.p1Sub.value = to_dollars(document.itemForm.p1Qty.value *
          document.itemForm.p1Price.value);
        break;

      case "a1":
        document.itemForm.a1Sub.value = to_dollars(document.itemForm.a1Qty.value *
          document.itemForm.a1Price.value);
        break;

      case "c1":
        document.itemForm.c1Sub.value = to_dollars(document.itemForm.c1Qty.value *
          document.itemForm.c1Price.value);
        break;

      case "v1":
        document.itemForm.v1Sub.value = to_dollars(document.itemForm.v1Qty.value *
          document.itemForm.v1Price.value);
        break;

      case "tb1":
        document.itemForm.tb1Sub.value = to_dollars(document.itemForm.tb1Qty.value *
          document.itemForm.tb1Price.value);
        break;

      case "tb2":
        document.itemForm.tb2Sub.value = to_dollars(document.itemForm.tb2Qty.value *
          document.itemForm.tb2Price.value);
        break;

      case "tb3":
        document.itemForm.tb3Sub.value = to_dollars(document.itemForm.tb3Qty.value *
          document.itemForm.tb3Price.value);
        break;

      default:
        window.alert("Unknown item: " + item);
        break;
    }
}


/*
**    Displays the subtotal for a given item.
*/

function show_subtotal(item)
{
    switch (item)
    {
      case "t1":
        document.write(document.itemForm.t1Sub.value);
        break;

      case "t2":
        document.write(document.itemForm.t2Sub.value);
        break;

      case "t3":
        document.write(document.itemForm.t3Sub.value);
        break;

      case "t4":
        document.write(document.itemForm.t4Sub.value);
        break;

      case "t5":
        document.write(document.itemForm.t5Sub.value);
        break;

      case "t6":
        document.write(document.itemForm.t6Sub.value);
        break;

      case "t7":
        document.write(document.itemForm.t7Sub.value);
        break;

      case "m1":
        document.write(document.itemForm.m1Sub.value);
        break;

      case "m2":
        document.write(document.itemForm.m2Sub.value);
        break;

      case "tt1":
        document.write(document.itemForm.tt1Sub.value);
        break;

      case "p1":
        document.write(document.itemForm.p1Sub.value);
        break;

      case "a1":
        document.write(document.itemForm.a1Sub.value);
        break;

      case "c1":
        document.write(document.itemForm.c1Sub.value);
        break;

      case "v1":
        document.write(document.itemForm.v1Sub.value);
        break;

      case "tb1":
        document.write(document.itemForm.tb1Sub.value);
        break;

      case "tb2":
        document.write(document.itemForm.tb2Sub.value);
        break;

      case "tb3":
        document.write(document.itemForm.tb3Sub.value);
        break;

      default:
        window.alert("Unknown item (2): " + item);
        break;
    }
}


/*
**    Calculates the order total.
*/

function get_total()
{
    document.itemForm.total.value = 0;

    if (document.itemForm.t1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t1Sub.value);
    }

    if (document.itemForm.t2Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t2Sub.value);
    }

    if (document.itemForm.t3Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t3Sub.value);
    }

    if (document.itemForm.t4Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t4Sub.value);
    }

    if (document.itemForm.t5Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t5Sub.value);
    }

    if (document.itemForm.t6Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t6Sub.value);
    }

    if (document.itemForm.t7Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.t7Sub.value);
    }

    if (document.itemForm.m1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.m1Sub.value);
    }

    if (document.itemForm.m2Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.m2Sub.value);
    }

    if (document.itemForm.tt1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.tt1Sub.value);
    }

    if (document.itemForm.p1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.p1Sub.value);
    }

    if (document.itemForm.a1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.a1Sub.value);
    }

    if (document.itemForm.c1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.c1Sub.value);
    }

    if (document.itemForm.v1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.v1Sub.value);
    }

    if (document.itemForm.tb1Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.tb1Sub.value);
    }

    if (document.itemForm.tb2Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.tb2Sub.value);
    }

    if (document.itemForm.tb3Sub.value != "")
    {
        document.itemForm.total.value += Number(document.itemForm.tb3Sub.value);
    }

    document.itemForm.total.value = to_dollars(Number(document.itemForm.total.value));

    return(document.itemForm.total.value);
}


/*
**    Displays the order total.
*/

function show_total()
{
    document.write(document.itemForm.total.value);
}


/*
**    Calculates the tax for California orders.
*/

function calculate_tax()
{
var subTotal = Number(0);
var tax = Number(0);
var total = Number(0);

    subTotal = Number(get_total());
    tax = (subTotal * .0725);
    total = (subTotal + tax);
    document.itemForm.caTax.value = to_dollars(tax);
    document.itemForm.total.value = to_dollars(total);
}


/*
**    This function verifies that the user has entered required data
** fields before sending the form.
*/

function check_data()
{
    if (document.itemForm.total.value == "")
    {
      alert("Please select all desired items, then click in the 'Total' box");
      return(false);
    }

    document.itemForm.total.value = to_dollars((Number(document.itemForm.total.value) + Number(document.itemForm.cchandling.value)));

    if (document.itemForm.theFirstName.value == "")
    {
      alert("Please enter your first name");
      return(false);
    }

    if (document.itemForm.theLastName.value == "")
    {
      alert("Please enter your last name");
      return(false);
    }

    if (document.itemForm.theAddress1.value == "")
    {
      alert("Please enter your address");
      return(false);
    }

    if (document.itemForm.theCity.value == "")
    {
      alert("Please enter your city");
      return(false);
    }

    if (document.itemForm.theState.value == "")
    {
      alert("Please enter your state or province");
      return(false);
    }

    if (document.itemForm.theZip.value == "")
    {
      alert("Please enter your ZIP or postal code");
      return(false);
    }

    return(true);
}


function set_pay_type_email()
{
    paymentType = "Email";
}


function set_pay_type_paypal()
{
    paymentType = "PayPal";
}


/*
**    Set up the PayPal form in case the user wants to use PayPal,
** and then post the data to the PayPal server.
*/

function set_paypal_data()
{
var i = new Number(0)
var n = new Number(0)
var ppFormData = new String("https://www.paypal.com/cgi-bin/webscr?")
var tmpStr = new String("")

    if (check_data() == false)         /* Make sure we have all the info */
    {
        return(false);                 /* Need more info from user */
    }

    document.itemForm.invoice.value = make_invoice_number();        /* Create an invoice number */

    if (paymentType == "Email")        /* Email only, so just bail */
    {
        return(true);
    }

    /*
    ** Copy customer data from CC form to PayPal form
    */

    document.itemForm.item_name.value = "Cuddly Critters Sales Items";
    document.itemForm.quantity.value = 1;
    document.itemForm.amount.value = document.itemForm.total.value;

    /*
    ** Use the customer's address from the order form.
    */

    document.itemForm.first_name.value = document.itemForm.theFirstName.value;
    document.itemForm.last_name.value = document.itemForm.theLastName.value;
    document.itemForm.address1.value = document.itemForm.theAddress1.value;
    document.itemForm.address2.value = document.itemForm.theAddress2.value;
    document.itemForm.city.value = document.itemForm.theCity.value;
    document.itemForm.state.value = document.itemForm.theState.value;
    document.itemForm.zip.value = document.itemForm.theZip.value;

    n = document.itemForm.theCountry.selectedIndex;

    if (document.itemForm.theCountry.options[n].text == "Other")
    {
        document.itemForm.lc.value = document.itemForm.otherCountry.value;
    }
    else
    {
        document.itemForm.lc.value = document.itemForm.theCountry.options[n].text;
    }

    /*
    ** Construct the data string to send to PayPal's server
    */

    with (document.itemForm)
    {
        ppFormData = (ppFormData + "cmd" + "=" + cmd.value);
        ppFormData = (ppFormData + "&" + "invoice" + "=" + invoice.value);
        ppFormData = (ppFormData + "&" + "business" + "=" + business.value);
        ppFormData = (ppFormData + "&" + "return" + "=" + "http://www.cuddlycritters.org/thankyou.htm");
        ppFormData = (ppFormData + "&" + "currency_code" + "=" + currency_code.value);
        ppFormData = (ppFormData + "&" + "bn" + "=" + bn.value);
        ppFormData = (ppFormData + "&" + "lc" + "=" + lc.value);
        ppFormData = (ppFormData + "&" + "cn" + "=" + "We welcome your comments");
        ppFormData = (ppFormData + "&" + "item_name" + "=" + item_name.value);
        ppFormData = (ppFormData + "&" + "quantity" + "=" + quantity.value);
        ppFormData = (ppFormData + "&" + "amount" + "=" + amount.value);
        ppFormData = (ppFormData + "&" + "address_override" + "=" + address_override.value);
        ppFormData = (ppFormData + "&" + "first_name" + "=" + first_name.value);
        ppFormData = (ppFormData + "&" + "last_name" + "=" + last_name.value);
        ppFormData = (ppFormData + "&" + "address1" + "=" + address1.value);
        ppFormData = (ppFormData + "&" + "address2" + "=" + address2.value);
        ppFormData = (ppFormData + "&" + "city" + "=" + city.value);
        ppFormData = (ppFormData + "&" + "state" + "=" + state.value);
        ppFormData = (ppFormData + "&" + "zip" + "=" + zip.value);
    }

    /*
    ** Convert special characters to form post format
    */

    for (i = 38; i < ppFormData.length; i++)         /* Start looking after address header */
    {
        if (ppFormData.charAt(i) == " ")             /* Check for space and replace with "+" */
        {
            tmpStr = ppFormData.substr(0, i);
            tmpStr = tmpStr + "+";
            ppFormData = tmpStr + ppFormData.substr((i + 1), ppFormData.length);
        }

        if (ppFormData.charAt(i) == "/")             /* Check for slash and replace with "%2F" */
        {
            tmpStr = ppFormData.substr(0, i);
            tmpStr = tmpStr + "%2F";
            ppFormData = tmpStr + ppFormData.substr((i + 1), ppFormData.length);
        }

        if (ppFormData.charAt(i) == ":")             /* Check for colon and replace with "%3A" */
        {
            tmpStr = ppFormData.substr(0, i);
            tmpStr = tmpStr + "%3A";
            ppFormData = tmpStr + ppFormData.substr((i + 1), ppFormData.length);
        }
    }

    window.location = ppFormData;                    /* Go to the PayPal site, passing all the info */
    return(true);
}


/*
** Create a unique invoice number based on the current date and time.
*/

function make_invoice_number()
{
var tmpStr = new String("")
var invoice = new String("")
var timeDate = new Date()

    /*
    ** For some as yet unknown reason, the getMonth() and getUTCMonth()
    ** methods return a value that is one less than what it should be.
    ** The following line is a kludge to make it come out right.
    */

    tmpStr = String(timeDate.getUTCMonth() + 1)

    invoice = (timeDate.getUTCFullYear().toString() + "-" + tmpStr +
                "-" + timeDate.getUTCDate().toString() + "-" + timeDate.getUTCHours().toString() +
                "-" + timeDate.getUTCMinutes().toString() + "-" + timeDate.getUTCSeconds().toString());

    return(invoice);
}
