// this just makes things easier
doc = window.document;

/*
 * Insures that the height of the page content is at least
 * the size of the viewport.
 */
function setHeights()
{
    // get the window height (depends on browser type)
    var usable_height = (navigator.appName.indexOf("Microsoft") != -1) ?
        document.documentElement.clientHeight : window.innerHeight;
    
    // figure out the total usable area for the content wrapper
    usable_height -= (Element.getHeight("header") +
                      Element.getHeight("nav_bar") +
                      Element.getHeight("footer") + 20);

    // get the current height of the content wrapper
    var main_height = Element.getHeight("content_wrapper");

    // if it's smaller than the usable height, reset it
    if(main_height < usable_height)
    {
        doc.getElementById("content_wrapper").style.height = usable_height + "px";
    }

    // figure out the size of the main content area
    var main_height = Element.getHeight("main_content");

    // figure out how far down the product navigation goes
    var p_nav_height = Element.getHeight("product_nav") -
                       Element.getHeight("nav_bar") - 50;

    // reset the main content height if it's too small
    if(main_height < p_nav_height)
    {
        doc.getElementById("main_content").style.height = p_nav_height + "px";
    }
}

/*
 * 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;
}

/*
 * iPhone wizard selection helper script.
 */
function iphone_wizard_next_step(step_num)
{
    // array of steps
    var steps = ["iphone_type", "whats_wrong", "location", "diy"];
    var current_step = steps[step_num];
    
    // make sure all steps beyond the current step are invisible
    for(var tmp_step = step_num + 1; tmp_step != steps.length; tmp_step++)
    {
        if($(steps[tmp_step]).getStyle('display') != 'none')
        {
            var tmp = document.getElementsByName(steps[tmp_step]);
            for (var i=0; i < tmp.length; i++)
            {
                tmp[i].checked = false;
            }

            if(tmp_step > step_num + 1)
            {
                Effect.SwitchOff($(steps[tmp_step]));
            }
        }
    }
    
    // get any values that have been selected
    var iphone_type = $$('input:checked[type="radio"][name="iphone_type"]').pluck('value');
    var whats_wrong = $$('input:checked[type="radio"][name="whats_wrong"]').pluck('value');
    var location = $$('input:checked[type="radio"][name="location"]').pluck('value');
    var diy = $$('input:checked[type="radio"][name="diy"]').pluck('value');
    
    // now enable the next step in the process
    switch(current_step)
    {
        case "iphone_type":
            // done if 2G or show next page
            if(iphone_type == "2G")
            {
                window.location = "/products/sorry?phone=iPhone%202G";
            }
            else
            {
                $('whats_wrong').appear({duration: 1.5});
            }
            break;

        case "whats_wrong":
            // done if it's something other than a screen
            if(whats_wrong == "something_else")
            {
                window.location = "/products/request-repair";
            }
            else
            {
                $('location').appear({duration: 1.5});
            }
            break;
                      
        case "location":
            // done if they're in a repair center city
            if(location != "somewhere_else")
            {
                window.location = iphone_type + "/" + whats_wrong + "/" + location;
            }
            else
            {
                window.location = iphone_type + "/" + whats_wrong;
            }
            break;
     
        //
        // TODO: For now we're not doing this step.
        //
        case "diy":
            // done no matter what
            if(diy == "no")
            {
                window.location = iphone_type + "/" + whats_wrong;
            }
            else
            {
                if(whats_wrong == "touchscreen-replacement")
                {
                    window.location = iphone_type + "/screen-repair-kit";
                }
                else if(whats_wrong == "touchscreen-and-lcd-replacement")
                {
                    window.location = iphone_type + "/screen-repair-kit";
                }
                else if(whats_wrong == "lcd-only-replacement")
                {
                    window.location = iphone_type + "/lcd-only-repair-kit";
                }
            }
            break;
    }
}