function setup_sbk_catalog_form() {
    $("#id_how_did_you_hear").change(function() {
        $("#row_id_how_did_you_hear_other").hide();
        $("#row_id_how_did_you_hear_about_us_ad").hide();

        var e = $(this);
        if (e.val() == 'Other (please describe)') {
            $("#row_id_how_did_you_hear_other").show();

        } else if (e.val() == 'Ad (please let us know which ad you saw)') {
        $("#row_id_how_did_you_hear_about_us_ad").show();
        }

    });

    $("#id_sbk_catalog_form").submit(function() {
        var e = $("#id_how_did_you_hear");
        if (e.val() == 'Other (please describe)') {
            if (!$("#id_how_did_you_hear_other").val()) {
                alert("Please describe 'How did you hear about us'");
                return false;
            }

        } else if (e.val() == 'Ad (please let us know which ad you saw)') {
            if (!$("#id_how_did_you_hear_about_us_ad").val()) {
                alert("Please describe the Ad for 'How did you hear about us'");
                return false;
            }

        }

    });
}


function setup_free_books_form() {
    $("#id_sbk_free_books").submit(function() {
        var e1 = $("#id_email_address");
        var e2 = $("#id_confirm_email");

        if (e1.val() != e2.val()) {
            alert("Email address and Confirm Email Address must be the same");
            return false;
        }
    });
}

function sbk_setup_dealers_form() {
    $("#id_saleschannels").dropdownchecklist();
    $("#ddcl-id_saleschannels").click();

    $('#ddcl-id_saleschannels-ddw').unbind('hover');
    $('#ddcl-id_saleschannels').unbind('hover');
    $('#id_saleschannels').unbind('hover');

    $('#ddcl-id_saleschannels-ddw').unbind('focus');
    $('#ddcl-id_saleschannels').unbind('focus');
    $('#id_saleschannels').unbind('focus');

    $('#ddcl-id_saleschannels-ddw').hover(function() {
        $("#ddcl-id_saleschannels-ddw input").removeAttr("disabled");
    });

    $('#ddcl-id_saleschannels-ddw').focus(function() {
        $("#ddcl-id_saleschannels-ddw input").removeAttr("disabled");
    });

}

function load_product_format_url(e) {
    if ($(e).val()) {
        window.location = "/estore/product/" + $(e).val();
    }
}


function show_companion_items_tab() {
    $("#\\#tab3").click();
    $.scrollTo("#tabarea_content");
}

function setup_shipping_info_tooltip() {
    var target = "shipping_info_ttip";
    var content = "shipping_info_ttip_content";
    var width = 450;
    var ttip_pos = 'bottomLeft';

    $("#" + target).qtip({
       content: $("#" + content).html(),

       style: {
          name: 'cream',
          width: width,
          padding: 10,
          border: {
             width: 7,
             radius: 5
          }

       },

       position: {
          corner: {
             target: 'topRight',
             tooltip: ttip_pos
          }
       }
    });
}


function setup_purchase_order_tooltip() {
    var target = "purchase_order_info_ttip";
    var content = "purchase_order_info_ttip_content";
    var width = 320;

    $("#" + target).qtip({
       content: $("#" + content).html(),

       style: {
          name: 'cream',
          width: width,
          padding: 10,
          border: {
             width: 7,
             radius: 5
          }

       },

       position: {
          corner: {
             target: 'leftTop',
             tooltip: 'bottomLeft'
          }
       }
    });

    $("#id_pay_with_purchase_order_trigger").click(function () {
        var e = $("#id_purchase_order_text_select");
         $("#id_purchase_order_key_val").val(e.val());
         $("#purchase_order_holder").show();

         $("#id_pay_credit_card_enabled").hide();
         $("#id_pay_credit_card_disabled").show();

    });

    $("#id_purchase_order_number_val").change(function () {
        var e = $(this);
        $("#id_porder_code_key").val(e.val());
    });
}


function setup_purchase_order_disabled_tip() {
    // setup the 'why can't I pay with purchase order'
    if ($("#checkout_purchase_order_disabled_info")[0]) {
        var target = "purchase_order_disabled_ttip";
        var content = "purchase_order_disabled_ttip_content";
        var width = 340;
        var ttip_pos = 'bottomLeft';

        $("#" + target).qtip({
           content: $("#" + content).html(),

           style: {
              name: 'cream',
              width: width,
              padding: 15,
              border: {
                 width: 10,
                 radius: 5
              }

           },

           position: {
              corner: {
                 target: 'topRight',
                 tooltip: ttip_pos
              }
           }
        });
    }
}

function check_county() {
    var url = "/estore/cart/checkout/shipping/ajax/county/cities/";
    url = url + "?county=" + $("#id_county").val();
    $("#id_city").autocomplete({
            source: url,
            minLength: 2,
            select: function( event, ui ) {
                $("#id_city").val(this.value);
            }
   });
}


function setup_shipping_form() {
    $("#id_state").change(function() {
        var e = $(this);

        if (e.val() == 'US-CA') {
            $("#id_county").parent().parent().show();
            check_county();

        } else {
            $("#id_county").parent().parent().hide();
        }

    });

    $("#id_county").change(function() {
        check_county();
    });

    // force check upon first page load
    $("#id_state").change();
}


function setup_sbk_member_form() {
    $("#id_state").change(function() {
        var e = $(this);

        if (e.val() == 'US-CA') {
            $("#field_id_county").show();
            $("#label_id_county").show();
            check_county();

        } else {
            $("#field_id_county").hide();
            $("#label_id_county").hide();
        }

    });

    $("#id_county").change(function() {
        check_county();
    });

    // force check upon first page load
    $("#id_state").change();
    $("#id_county").change();
}

