﻿
function createCustomiseAndBuy() {

    //  this builds the functionality for the customise and buy options
    //  the price is recalculated on each option
    //  change and retrieved via web service

    //  call the update when select boxes are changed or inputs clicked.
    $(".productoptions select").change(function () {
        updateCustomiseAndBuy(false, $(this).val());
    });

    //  call the update when quantity select boxes are changed.
    $(".quantity").change(function() {
        updateCustomiseAndBuy(false);
    });

    //  create add to basket function
    $(".addtobasketbutton").click(function() {
        updateCustomiseAndBuy(true);
    });

    //  call it on start up.
    updateCustomiseAndBuy(false);
}

//  the updateCustomiseAndBuy function is called whenenever a user changes an option 
//  in the customise and buy panel
function updateCustomiseAndBuy(bAddToBasket, productoptionid) {
    var product = $(".product-info input:first").val();
    var options = "";
    var quantity = $("select.quantity").val();

    if (productoptionid == null) productoptionid = 0;

    //  set default values
    
    if (quantity == null) quantity = 1;

    //  show calculating message.
    $(".total-price").html("...");

    //  get options.
    $(".productoptions select").each(function () {
        options = options + "'" + $(this).val() + "' , ";
        
    });
    if (options.length > 0) options = options.substr(0, options.length - 3);
    
    //  build the JSON string.
    var JSON = "{" +
               " 'Product_ID' : '" + product + "' , " +
               " 'ProductOptions' : [ " + options + " ] , " +
               " 'Quantity' : '" + quantity + "' , " +
               " 'AddToBasket' : '" + bAddToBasket + "' , " +
               " 'ProductOptionID' : '" + productoptionid + "' " +
               "}";

    //  track in analytics
    if (bAddToBasket) {
        var pageTracker = _gat._getTracker('UA-791030-1');
        pageTracker._trackPageview('/addproducttobasket');
    }

    //alert(JSON)
    //  call the web service passing the product, options, accessories, delivery and
    //  and then update the price.
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Assets/Cranborne/WebServices/Cranborne.asmx/CustomiseAndBuy",
        data: JSON,
        dataType: "json",
        success: function (response) {
            var oResponse = eval("(" + response.d + ")");

            //  update prices.
            $(".total-price").html(oResponse.Data);
            $(".vat-free span").html(oResponse.DataTwo);
            $(".asset-link-" + oResponse.DataFive).html(oResponse.DataFour);
            //$(".asset-link-14").html(oResponse.DataFour);
            //alert(oResponse.DataFive)
            if (bAddToBasket) {
                $(".basketmessage").html(oResponse.DataThree);
                $(".itemadded").show();
            }

        },
        error: function (response) {

            //  if an error has occured display for debugging purposes.
            alert(response.responseText);
        }
    });
}

$(function () {
    //  Create Product Tabs
    var tabContainers = $('div.tabscontainer div.tabscontent div.inner > div');
    $('div.tabscontainer ul.tabNavigation a').click(function () {
        tabContainers.hide().filter(this.hash).show();

        $('div.tabscontainer ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();
});

function cleartext(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
        thisfield.style.color = "#666666";
    }
}
function recalltext(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
        thisfield.style.color = "#666666";
    }
}
$(function () {
//  set top drop down functionality
$(".image-gallery select").change(function () {

    //  change url
    if ($(this).val() != null && $(this).val() != "") {
        window.location = $(this).val();
    }
});

});
