/*
**
**    cc_memb.js - Script support for the member data form in the
**                 Cuddly Critters website.
**
** 06/28/06 - jf - Created.
**
*/


MAX_OPTIONS = 10

var postType = new String("no")
var memberType = new String("no")
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);
}


/*
**    This function verifies that the user has entered required data
** fields before sending the form.
*/

function check_data()
{
    if (document.ccMember.theFirstName.value == "")
    {
      alert("Please enter your first name");
      return(false);
    }

    if (document.ccMember.theLastName.value == "")
    {
      alert("Please enter your last name");
      return(false);
    }

    if (document.ccMember.theAddress1.value == "")
    {
      alert("Please enter your address");
      return(false);
    }

    if (document.ccMember.theCity.value == "")
    {
      alert("Please enter your city");
      return(false);
    }

    if (document.ccMember.theState.value == "")
    {
      alert("Please enter your state or province");
      return(false);
    }

    if (document.ccMember.theZip.value == "")
    {
      alert("Please enter your ZIP or postal code");
      return(false);
    }

    return(true);
}


function set_post_type_no()
{
    postType = "no";
}


function set_post_type_yes()
{
    postType = "yes";
}


function set_memb_type_no()
{
    memberType = "no";
}


function set_memb_type_yes()
{
    memberType = "yes";
}


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.
** Use the info from the CC form.
*/

function set_paypal_data()
{
var i = new Number(0)
var total = 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.ccMember.invoice.value = make_invoice_number();        /* Create an invoice number */

    if (paymentType == "Email")        /* Email only, so just bail */
    {
        return(true);
    }

    if (memberType == "yes")
    {
        document.ccMember.item_name.value = "Membership Donation";
    }
    else
    {
        document.ccMember.item_name.value = "Donation";
    }

    document.ccMember.quantity.value = Number(0);
    document.ccMember.amount.value = Number(0);

    with (document.ccMember.regularDonation)
    {
        for (i = 0; i < MAX_OPTIONS; i++)
        {
            if (options[i].selected != 0)
            {
                document.ccMember.quantity.value = 1;

                if (options[i].text == "Other")
                {
                    document.ccMember.amount.value = to_dollars(Number(document.ccMember.otherRegAmount.value));
                }
                else
                {
                    document.ccMember.amount.value = to_dollars(Number(options[i].text));
                }

                break;
            }
        }
    }

    with (document.ccMember.dreamFundDonation)
    {
        for (i = 0; i < MAX_OPTIONS; i++)
        {
            if (options[i].selected != 0)
            {
                if (options[i].text == "Other")
                {
                    document.ccMember.amount.value = to_dollars(Number(document.ccMember.amount.value) +
                      Number(document.ccMember.otherDreamFundAmount.value));
                }
                else
                {
                    document.ccMember.amount.value = to_dollars(Number(document.ccMember.amount.value) +
                      Number(options[i].text));
                }

                break;
            }
        }
    }

    /*
    ** Use the customer's address from the order form.
    */

    document.ccMember.first_name.value = document.ccMember.theFirstName.value;
    document.ccMember.last_name.value = document.ccMember.theLastName.value;
    document.ccMember.address1.value = document.ccMember.theAddress1.value;
    document.ccMember.address2.value = document.ccMember.theAddress2.value;
    document.ccMember.city.value = document.ccMember.theCity.value;
    document.ccMember.state.value = document.ccMember.theState.value;
    document.ccMember.zip.value = document.ccMember.theZip.value;

    with (document.ccMember.theCountry)
    {
        i = selectedIndex;

        if (options[i].text == "Other")
        {
            document.ccMember.lc.value = document.ccMember.otherCountry.value;
        }
        else
        {
            document.ccMember.lc.value = options[i].text;
        }
    }

    /*
    ** Construct the data string to send to PayPal's server
    */

    with (document.ccMember)
    {
        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);
}
