﻿function clearSearch(searchBox) {
    if (searchBox.value == "Search...") searchBox.value = "";
}
function search(qid) { window.location = '/app/search.aspx?q=' + document.getElementById(qid).value + '&page=1'; return false; }
function setCommandSupressDefault(strCommandName, strCommandValue, strConfirm) { var bConfirm; if (strConfirm != null && strConfirm != "") { bConfirm = confirm(strConfirm); } else { bConfirm = true; } if (bConfirm == true) { document.getElementById("command_name").value = strCommandName; document.getElementById("command_value").value = strCommandValue; document.getElementById("aspnetForm").submit(); } return bConfirm; }
function setCommand(strCommandName, strCommandValue, strConfirm) { var bConfirm; if (strConfirm != null && strConfirm != "") { bConfirm = confirm(strConfirm); } else { bConfirm = true; } if (bConfirm == true) { document.getElementById("command_name").value = strCommandName; document.getElementById("command_value").value = strCommandValue; } return bConfirm; }

var tabSelector = { links: null, tabs: null, init: function(linksID, tabsID) { var linksBox = document.getElementById(linksID); var tabsBox = document.getElementById(tabsID); links = linksBox.getElementsByTagName("A"); tabs = tabsBox.getElementsByTagName("DIV"); }, showTab: function(linkI, tabI) { for (i = 0; i < links.length; i++) { if (i == linkI) { links[i].className = 'on'; tabs[i].className = 'on'; } else { links[i].className = 'off'; tabs[i].className = 'off'; } } return false; } }

function createCustomiseAndBuy() {

    //  this builds the functionality for the customise options on the
    //  product page. The price is recalculated on each option
    //  change and retrieved via web service

    //  call the update when select boxes are changed
    $(".options select").change(function() {
        updateCustomiseAndBuy(false);
    });

    //  call the update when add to basket is clicked
    $(".addtobasket").click(function () {
        updateCustomiseAndBuy(true);
    });
}


//  the updateCustomiseAndBuy function is called whenenever a user changes an option 
//  in the customise and buy panel.
function updateCustomiseAndBuy(bAddToBasket) {
    var product = $(".customiseandbuy").find("input:first").val();
    var options = "";
    var quantity = $(".quantity").val();
    var notes = $(".specifics textarea").val();

    //  handle null
    if (bAddToBasket == null) bAddToBasket = false;

    //  get options.
    $(".options select").each(function() {
        options = options + $(this).val() + " , ";
    });
    if (options.length > 0) options = options.substr(0, options.length - 3);

    if (notes == "If you have any specific cutting or assembly requirements please detail here...") notes = "";

    //  build the JSON string.
    var JSON = "{" +
               " 'Product_ID' : '" + product + "' , " +
               " 'ProductOptions' : [ " + options + " ] , " +
               " 'Quantity' : '" + quantity + "' , " +
               " 'Notes' : '" + notes + "' , " +
               " 'AddToBasket' : '" + bAddToBasket + "'" +
               "}";

    //  call the web service passing the product and options
    //  and then update the price.
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Assets/Seagull/WebServices/Seagull.asmx/CustomiseAndBuy",
        data: JSON,
        dataType: "json",
        success: function (response) {
            var oResponse = eval("(" + response.d + ")");

            //  Update prices.
            $("span.totalprice").html(oResponse.Data);
            $("span.product-rrp").html("<strike>RRP: " + oResponse.DataTwo + "</strike>");
            $("span.product-saving").html("Saving: " + oResponse.DataThree);
            $("span.ex-vat").html(oResponse.DataFour);

            //  look for redirect
            if (bAddToBasket) {
                window.location = "/basket.aspx"
            }

        },
        error: function (response) {

            //  if an error has occured display for debugging purposes.
            alert(response.responseText);
        }
    });
}
function cleartext(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
        thisfield.style.color = "#999999";
    }
}
function recalltext(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
        thisfield.style.color = "#999999";
    }
}
$(function () {

    //  Create the navigation
    $("#navigation ul li").hover(
        function () {
            $(this).addClass("on");
            $(this).find("ul").show();
        },
        function () {
            $(this).removeClass("on");
            $(this).find("ul").hide();
        }
    );
    $("#navigation ul li").find("ul").hide();

    //  create product tabs
    var tabContainers = $('div.tabs div.tabcontainer > div');

    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide().filter(this.hash).show();

        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();

});
