/*
 * Toggle the product navigation bar.
 */
function toggle_product_nav_item(item)
{
    // figure out the current state of the nav item
    var cur_state = $('section_' + item).getStyle('display');
    
    // toggle the options
    $('section_' + item).toggleBlind();
    
    // now update the bullet on the left side
    var bgd_pos = 'left ' + (cur_state == 'none' ? 'bottom' : 'top');
    $('section_heading_' + item).setStyle({backgroundPosition: bgd_pos});
}

/*
 * Show or hide the custom product on the local repair page.
 */
function toggle_custom_product()
{
    // figure out the current state
    var cur_state = $('custom_product').getStyle('display');
    
    // figure out if it should or shouldn't be shown
    var value = $('phone_type').value
    
    if((value == "Not On List" && (cur_state == "none")) ||
       (value != "Not On List" && (cur_state == "block")))
    {
        $('custom_product').toggleBlind();
    }
}

/*
 * Some utility functions.
 */
Element.addMethods(
    {
        toggleBlind: function(element)
        {
            element = $(element);
            Element.visible(element) ? new Effect.BlindUp(element, {duration: 0.3}): new Effect.BlindDown(element, {duration: 0.3});
            return element;
        }
    }
);

/*
 * This is a really rough test for validity.
 */
function is_appointment_valid(form)
{
    var valid = (form.phone.value.match(/^([ ()-.]?\d[ ()-.]?){10}$/) != null) || (form.email.value.match(/.+@.+/) != null);
    if(!valid)
    {
        alert("You must enter either a phone number or an email.");
    }
    
    return valid;
}

/*
 * Used for the automatic telephone entry system.
 */
function num_entered(my_id, next_id, num_to_enter)
{
    // get the box
    var el = document.getElementById(my_id);

    // make sure a number was entered
    while(isNaN(el.value) || (el.value.indexOf(" ") != -1))
    {
        // remove the last character entered
        el.value = el.value.substring(0, el.value.length - 1);
    }

    // if all digits are entered, we're done
    if((el.value.length == num_to_enter) && (next_id))
    {
        document.getElementById(next_id).focus();
    }
    
    // update the hidden input number to the new number
    $('customer_phone_number').value = $('phone_1').value + $('phone_2').value + $('phone_3').value;
}

// figure out sales tax rates based on the city
function get_sales_tax_rate()
{
    var rate = 0;
    switch($('billing_state').value)
    {
        // Chicago, IL
        case "IL":
            rate = .0975;
            break;
            
        // Seattle, WA
        case "WA":
            rate = .095;
            break;
    }
    
    return rate;
}

function update_checkout_totals()
{
    // get the current sales tax
    var sales_tax = parseFloat($('cart_sales_tax').innerHTML);
    
    // determine the current totals sans sales tax
    var total = parseFloat($('cart_total').innerHTML);
    total -= sales_tax;
    
    // figure out new sales tax and total
    sales_tax = total * get_sales_tax_rate();
    total += sales_tax;
    
    // update the totals on the page
    $('cart_sales_tax').innerHTML = sales_tax.toFixed(2);
    $('cart_total').innerHTML = total.toFixed(2);
}
