﻿
var selectedPhone = new Array();
function track_url(url)
{
    pageTracker._trackPageview(url);
}

function changePhoneType(tomodify, phoneField)
{
    var newVal = jQuery(phoneField).val();
    jQuery("#hiddennumbers input").each(function()
    {
        if (jQuery(this).val() == newVal)
        {
            jQuery(this).val('');
        }
    });
    jQuery('#' + tomodify).val(newVal);
}

function disableItem()
{
    toLength = selectedPhone.length + 1;
    jQuery("#phoneNumbers select option").attr("disabled", "").each(function()
    {
        for (i = 1; i <= toLength; i++)
        {
            if (jQuery(this).val() == selectedPhone[i])
            {
                jQuery(this).attr("disabled", "disabled");
            }
        }
    });
}

jQuery.fn.attach_track_download = function()
{
    this.bind('click', function()
    {
        track_url(jQuery(this).attr('href'));
    });
    return this;
};

var tooltip_style = {
    width: 400,
    background: '#EEE',
    color: '#5B5B5B',
    border: {
        width: 1,
        radius: 0,
        color: '#333'
    }
}

var tooltip_position = {
    corner: {
        target: 'bottomMiddle',
        tooltip: 'topMiddle'
    }
}

var tooltip_position_right = {
    corner: {
        target: 'bottomLeft',
        tooltip: 'topRight'
    }
}

var tooltip_position_left = {
    corner: {
        target: 'bottomRight',
        tooltip: 'topLeft'
    }
}


jQuery(document).ready(function()
{
    //General - Track file downloads
    jQuery('a').not('[href$=.aspx],[href$=/],[href=#]').attach_track_download();

    //General - Track tab clicks
    jQuery('.tab-panel a').bind('click', function()
    {
        setTimeout(function() { track_url(window.location); }, 500);
    });

    //LYRA: This section might be for Price Pfister Only
    //Add Tool tips to the product listing
    jQuery('img[alt="ADA-Compliant Products"]').qtip({ content: { url: '/help/accessible_products.html' }, style: tooltip_style, position: tooltip_position, api: { onContentLoad: function() { track_url('/help/accessible_products.html'); } } }).parent().attr('title', '');
    jQuery('img[alt="Accessible Product"]').qtip({ content: { url: '/help/accessible_product.html' }, style: tooltip_style, position: tooltip_position, api: { onContentLoad: function() { track_url('/help/accessible_product.html'); } } });
    jQuery('img[alt="Green Products"]').qtip({ content: { url: '/help/green_products.html' }, style: tooltip_style, position: tooltip_position, api: { onContentLoad: function() { track_url('/help/green_products.html'); } } }).parent().attr('title', '');
    jQuery('img[alt="Green Product"]').qtip({ content: { url: '/help/green_product.html' }, style: tooltip_style, position: tooltip_position, api: { onContentLoad: function() { track_url('/help/green_product.html'); } } });
    //END of PP Only

    //Customer Support Add tooltips to the customer support help links

    jQuery('.SfpHelpLink').children('a').each(function()
    {
        var my_addr = jQuery(this).attr('href');
        var direction = jQuery(this).attr('rel');
        
        jQuery(this).qtip(
        { content: {
            url: my_addr
        },
            style: tooltip_style,
            position: ((direction == '') ? tooltip_position_right : tooltip_position_left),
            api: {
                onContentLoad: function()
                {
                    track_url(my_addr);
                }
            }
        }).click(function() { return false; });
    });



    //FAQ Add star rating
    var rating = jQuery("#rating");
    //We don't need the submit button since the stars will call submit();
    rating.children().not("select").hide();
    var caption = jQuery("<span/>");
    rating.stars({
        inputType: "select",
        cancelShow: false,
        captionEl: caption,
        callback: function(ui, type, value)
        {
            jQuery("#rating .SfpButton").click();
        }
    });
    caption.appendTo("#rating");


    //Product Registration Form
    /*
    var otherLocation = jQuery("#SFRegisteredProductControl .SfpTableInForm tbody tr:eq(0) td:eq(1)");
    var locationSelect = jQuery(".ddlPurchaseLocation");
    if (locationSelect.val() != 'Other Retail')
    {
        otherLocation.hide();
    }
    locationSelect.change(function()
    {
        if (jQuery(this).val() == 'Other Retail')
        {
            otherLocation.fadeIn();
        }
        else
        {
            jQuery("input", otherLocation).val('');
            otherLocation.fadeOut();
        }
    });
    */

    //Person Registration Form
    //Onload, show the phone boxes if there is a phone number in it.
    jQuery(".phoneBox").each(function()
    {
        if (jQuery(this).val() != "")
        {
            jQuery(this).parent().parent().show().attr("style", "").children("td .col3").hide();
            jQuery(this).parent().parent().next().show();
        }
    });

    //When an addmore link is click, show the new row and hide the old link
    jQuery(".addmore").click(function()
    {
        jQuery(this).hide().parent().parent().next().show().attr("style", "");
        return false;
    });

    //On Postback, select the proper "this is my"
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone1").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone1").val() == jQuery("#SFAccountControl1838_FormView1_tbHomePhone").val())
    {
        selectedPhone[1] = "SFAccountControl1838_FormView1_tbHomePhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype1").val("SFAccountControl1838_FormView1_tbHomePhone");
    }
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone1").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone1").val() == jQuery("#SFAccountControl1838_FormView1_tbWorkPhone").val())
    {
        selectedPhone[1] = "SFAccountControl1838_FormView1_tbWorkPhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype1").val("SFAccountControl1838_FormView1_tbWorkPhone");
    }
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone1").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone1").val() == jQuery("#SFAccountControl1838_FormView1_tbMobilePhone").val())
    {
        selectedPhone[1] = "SFAccountControl1838_FormView1_tbMobilePhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype1").val("SFAccountControl1838_FormView1_tbMobilePhone");
    }

    if (jQuery("#SFAccountControl1838_FormView1_tbPhone2").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone2").val() == jQuery("#SFAccountControl1838_FormView1_tbHomePhone").val())
    {
        selectedPhone[2] = "SFAccountControl1838_FormView1_tbHomePhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype2").val("SFAccountControl1838_FormView1_tbHomePhone");
    }
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone2").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone2").val() == jQuery("#SFAccountControl1838_FormView1_tbWorkPhone").val())
    {
        selectedPhone[2] = "SFAccountControl1838_FormView1_tbWorkPhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype2").val("SFAccountControl1838_FormView1_tbWorkPhone");
    }
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone2").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone2").val() == jQuery("#SFAccountControl1838_FormView1_tbMobilePhone").val())
    {
        selectedPhone[2] = "SFAccountControl1838_FormView1_tbMobilePhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype2").val("SFAccountControl1838_FormView1_tbMobilePhone");
    }

    if (jQuery("#SFAccountControl1838_FormView1_tbPhone3").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone3").val() == jQuery("#SFAccountControl1838_FormView1_tbHomePhone").val())
    {
        selectedPhone[3] = "SFAccountControl1838_FormView1_tbHomePhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype1").val("SFAccountControl1838_FormView1_tbHomePhone");
    }
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone3").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone3").val() == jQuery("#SFAccountControl1838_FormView1_tbWorkPhone").val())
    {
        selectedPhone[3] = "SFAccountControl1838_FormView1_tbWorkPhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype3").val("SFAccountControl1838_FormView1_tbWorkPhone");
    }
    if (jQuery("#SFAccountControl1838_FormView1_tbPhone3").val() != '' && jQuery("#SFAccountControl1838_FormView1_tbPhone3").val() == jQuery("#SFAccountControl1838_FormView1_tbMobilePhone").val())
    {
        selectedPhone[3] = "SFAccountControl1838_FormView1_tbMobilePhone";
        jQuery("#SFAccountControl1838_FormView1_lstphonetype3").val("SFAccountControl1838_FormView1_tbMobilePhone");
    }
    disableItem();


    //The next few calls are for the phone number swapping into the right fields
    jQuery("#SFAccountControl1838_FormView1_tbPhone1").blur(function()
    {
        var tomodify = jQuery("#SFAccountControl1838_FormView1_lstphonetype1").val();
        if (tomodify != "")
        {
            jQuery('#' + tomodify).val(jQuery(this).val());
        }
    });

    jQuery("#SFAccountControl1838_FormView1_tbPhone2").blur(function()
    {
        var tomodify = jQuery("#SFAccountControl1838_FormView1_lstphonetype2").val();
        if (tomodify != "")
        {
            jQuery('#' + tomodify).val(jQuery(this).val());
        }
    });

    jQuery("#SFAccountControl1838_FormView1_tbPhone3").blur(function()
    {
        var tomodify = jQuery("#SFAccountControl1838_FormView1_lstphonetype3").val();
        if (tomodify != "")
        {
            jQuery('#' + tomodify).val(jQuery(this).val());
        }
    });

    jQuery("#SFAccountControl1838_FormView1_lstphonetype1").change(function()
    {
        changeto = jQuery("#SFAccountControl1838_FormView1_lstphonetype1").val();
        if (changeto != "")
        {
            selectedPhone[1] = changeto;
            changePhoneType(changeto, "#SFAccountControl1838_FormView1_tbPhone1");
        }
        else
        {
            selectedPhone[1] = null;
        }
        disableItem();
    });

    jQuery("#SFAccountControl1838_FormView1_lstphonetype2").change(function()
    {
        changeto = jQuery("#SFAccountControl1838_FormView1_lstphonetype2").val();
        if (changeto != "")
        {
            selectedPhone[2] = changeto;
            changePhoneType(changeto, "#SFAccountControl1838_FormView1_tbPhone2");
        }
        else
        {
            selectedPhone[2] = null;
        }
        disableItem();
    });

    jQuery("#SFAccountControl1838_FormView1_lstphonetype3").change(function()
    {
        changeto = jQuery("#SFAccountControl1838_FormView1_lstphonetype3").val();
        if (changeto != "")
        {
            selectedPhone[3] = changeto;
            changePhoneType(changeto, "#SFAccountControl1838_FormView1_tbPhone3");
        }
        else
        {
            selectedPhone[3] = null;
        }
        disableItem();
    });

    //Ask a Question
    jQuery("#SFCaseControl1840_FormView1_btnSubmit").click(function()
    {
        var error_found = false;
        jQuery("#SFCaseControl1840_FormView1_Panel2 .SfpValidationError:visible").each(function()
        {
            error_found = true;
        });
        if (error_found)
        {
            jQuery("#SFCaseControl1840_FormView1_FailValidation").fadeIn();
        } else
        {
            jQuery("#SFCaseControl1840_FormView1_FailValidation").hide();
        }
    });

});
