
//Functions for the right menu images
var html1 = "<a href=\"http://www.gardenbuildingsdirect.co.uk/Garden-Furniture\" id=\"RightCrossBanner\" rel=\"nofollow\"><img src=\"/assets/images/upsell-banners/furniture.jpg\" alt=\"Garden Furniture Now Available\" /></a>";
var html2 = "<a href=\"http://www.gardenbuildingsdirect.co.uk/Barbecues\" id=\"RightCrossBanner\" rel=\"nofollow\"><img src=\"/assets/images/upsell-banners/barbecues.jpg\" alt=\"Barbecues Now Available\" /></a>";
var html3 = "<a href=\"http://www.gardenbuildingsdirect.co.uk/Outdoor-toys\" id=\"RightCrossBanner\" rel=\"nofollow\"><img src=\"/assets/images/upsell-banners/outdoor-toys.jpg\" alt=\"Outdoor Toys Now Available\" /></a>";

var altCrossRotate = Math.floor((Math.random()*3)+1);
function rotateCrossBanner() {	
	if (altCrossRotate==1) {
		$("#linking").html(html1);
	} 
	else if (altCrossRotate==2) {
		$("#linking").html(html2);
	}
	else if (altCrossRotate==3) {
		$("#linking").html(html3);
	}
}

function showBillyOhAdvantagePopup() {
    triggerMacBox({
        boxID: "BillyOhAdvantagePopup",
        width: 742,
        ajaxUrl: "/ProductPage2/AJAX/BillyOhAdvantage.aspx"
    });
}
function showDeliveryProcessPopup() {
    triggerMacBox({
        boxID: "DeliveryProcessPopup",
        width: 484,
        ajaxUrl: "/ProductPage2/AJAX/DeliveryProcess.aspx"
    });
}


function showAssemblyPopup() {
    triggerMacBox({
        boxID: "AssemblyInformation",
        width: 500,
        ajaxUrl: "/assembly/assembly-popup.aspx"
    });
}

function showSalesLinePopup() {
    triggerMacBox({
        boxID: "SalesLineInformation",
        width: 300,
        ajaxUrl: "/hoursinfo.aspx"
    });
}

function showCantFindItPopup() {
    triggerMacBox({
        boxID: "CantFindIt",
        width: 388,
        ajaxUrl: "/CantFindItAJAX.aspx"
    });
}
function submitCantFindItForm() {
    var name = $(".cfwlf-name").val();
    var telephone = $(".cfwlf-telephone").val();
    var email = $(".cfwlf-email").val();
    var comments = $(".cfwlf-comments").val();
    var valid = true;
    if (name.length == 0) {
        valid = false;
    }
    else if ((telephone.length == 0)&&(email.length == 0)) {
        valid = false;
    }
    else if (comments.length == 0) {
        valid = false;
    }
    else {
        //check for telephone regex match
        var notNumbers = /[^0-9 ]*/ig;
        var anyLetters = (notNumbers.exec(telephone)[0].length > 0) ? true : false;
        if (anyLetters) valid = false;
    }
    var dataStr = "name=" + encodeURIComponent(name) + "&telephone=" + encodeURIComponent(telephone) + 
        "&email=" + encodeURIComponent(email) + "&comments=" + encodeURIComponent(comments);
    if (valid) {
        //do inserts etc
        $.ajax({
            type: "POST", 
            url: "/CantFindItSubmitAJAX.aspx",
            data: dataStr,
            success: function(data, textStatus) {
                try {
                    var rtnObj = eval(data)[0];
                    if (rtnObj.success) {
                        $(".cfwlf-form").remove();
                        $(".cfwlf-success-message").show();
                    }
                    else {
                        $(".cfwlf-form").remove();
                        $(".cfwlf-error-message").show();
                    }
                }
                catch (e) {
                    $(".cfwlf-form").remove();
                    $(".cfwlf-error-message").show();
                }
            },
            error: function(ajaxObj, textStatus, errorThrown) {
                $(".cfwlf-form").remove();
                $(".cfwlf-error-message").show();
                $("body").append(ajaxObj.responseText);
            }
        });
    }
    else {
        $(".error-msg").show();
    }
}

$(document).ready(function() {
    $("#front-page-content #gbd-features #billyoh-advantage").click(function() {
        showBillyOhAdvantagePopup(); return false;
    });
    $("#front-page-content #gbd-features #delivery-process").click(function() {
        showDeliveryProcessPopup(); return false;
    });
    $("#cant-find-it-link").click(function(e){
        e.preventDefault();
        showCantFindItPopup();
    });
    //For the right menu images
    rotateCrossBanner();
    //hide right menu if window is too small
    hideShowRhCol();
});
$(window).resize(function() {
    hideShowRhCol();
});
function hideShowRhCol() {
    //hide the right hand menu if the window is too small for it
    if (($("body").attr("class") != "default")
        &&($(window).width() < 1090)) {
        $("#rh-col2").hide();
    }
    else {
        $("#rh-col2").show();            
    }
}

function submitCallbackForm() {    
    //check the telephone number first... no point letting them ask for a callback if there's no number :-p
    var telephone1 = $("#cmb-telephone1").val();
    var telephone2 = $("#cmb-telephone2").val();
    if (telephone1.length == 0)
    {
        alert("Please enter a phone number in the 'Telephone 1' field");
        return;
    }
    var name = $("#cmb-name").val();
    var email = $("#cmb-email").val();
    var productid = $("#cmb-prodid").val();
    var reason = $("#cmb-reason option:selected").val();
    var time = $("#cmb-time option:selected").val();
    var question = $("#cmb-question").val();
    
    var url = "/submitCallbackForm.aspx";
    var data = "type=ajax&cmb-name=" + name + "&cmb-email=" + email + "&cmb-telephone1=" + telephone1 + "&cmb-telephone2=" + telephone2 + "&cmb-prodid=" + productid + "&cmb-reason=" + reason + "&cmb-time=" + time + "&cmb-question=" + question;
    var response = $.ajax(
    {
        type: "POST",
        url: url,
        data: data,
        complete: function(response)
        { 
            var rsp = response.responseText;
            if (rsp == "SUCCESS")
            {
                $(".cmbEntryForm").css("display", "none");
                $(".cmbSuccess").css("display", "block");
                $(".cmbError").css("display", "none");
            }
            else 
            {
                $(".cmbEntryForm").css("display", "none");
                $(".cmbSuccess").css("display", "none");
                $(".cmbError").css("display", "block");
            }
        }
    });
}

function submitPriceMatchForm()
{
    var name = $("#pmName").val();
    var email = $("#pmEmail").val();
    var telephone = $("#pmTelephone").val();
    var priceOption = $(".pmOption:checked").val();
    var priceFound = $("#pmPrice").val();
    var location = $("#pmLocation").val();
    var otherInfo = $("#pmOtherInfo").val();
    
    var url = "/submitPriceBeaterForm.aspx";
    var data = "type=ajax&pmName=" + name + "&pmEmail=" + email + "&pmTelephone=" + telephone + "&pmOption=" + priceOption + "&pmPrice=" + priceFound + "&pmLocation=" + location + "&pmOtherInfo=" + otherInfo;
    var response = $.ajax(
    {
        type: "POST",
        url: url,
        data: data,
        complete: function(response)
        { 
            var rsp = response.responseText;
            if (rsp == "SUCCESS")
            {
                $(".pmEntryForm").css("display", "none");
                $(".pmSuccess").css("display", "block");
                $(".pmError").css("display", "none");
            }
            else 
            {
                $(".pmEntryForm").css("display", "none");
                $(".pmSuccess").css("display", "none");
                $(".pmError").css("display", "block");
            }
        }
    });
}

function submitAskAQuestionForm(productid)
{
    var name = $("#aqName").val();
    var email = $("#aqEmail").val();
    var telephone = $("#aqTelephone").val();
    var question = $("#aqQuestion").val();
    
    var url = "/submitQuestionForm.aspx";
    var data = "type=ajax&productid=" + productid + "&aqName=" + name + "&aqEmail=" + email + "&aqTelephone=" + telephone + "&aqQuestion=" + question;
    var response = $.ajax(
    {
        type: "POST",
        url: url,
        data: data,
        complete: function(response)
        { 
            var rsp = response.responseText;
            if (rsp == "SUCCESS")
            {
                $(".aqEntryForm").css("display", "none");
                $(".aqSuccess").css("display", "block");
                $(".aqError").css("display", "none");
            }
            else 
            {
                $(".aqEntryForm").css("display", "none");
                $(".aqSuccess").css("display", "none");
                $(".aqError").css("display", "block");
            }
        }
    });
}