﻿$(document).ready(function() {
    initPopUps();
});

function initPopUps() {
    rigCallMeBackDialog();
    rigRoadShowDialog();
    rigVebraWebDialog();
}

var callBack = true;

function rigCallMeBackDialog() {
    $("#CallMeBackDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        draggable: false,
        resizable: false,
        height: 'auto',
        width: 660,
        modal: true,
        buttons: {
    },
    close: function() {
        callBack = true;
    }

});
callMeBackClick();
}

function callMeBackClick() {
    $("#callback").click(function() {
        if (callBack) {
            callBack = false;
            pageTracker._trackPageview("call-me-back");
            $.ajax({
                type: "GET",
                url: "/partialview/call-me-back",
                success: function(theResponse) {               
                    $("#CallMeBackDialog .popUpForms").html(theResponse);
                    $("#sendCallBack").click(function() {
                        sendCallBack();
                    });
                    $("#CallMeBackDialog").dialog("open");
                    pageTracker._trackPageview("/call-me-back");
                }
            });
        }
        return false;
    });
}

function sendCallBack() {
    var valid = true;
    var name = $("#CallMeBackDialog #cbName").val();
    var telephone = $("#CallMeBackDialog #cbTelephone").val();
    var email = $("#CallMeBackDialog #cbEmail").val();
    var postcode = $("#CallMeBackDialog #cbPostcode").val();

    var department = "";

    if ($("#CallMeBackDialog input[name='Sales']").is(':checked')) {
        department += " Sales,"
    }

    if ($("#CallMeBackDialog input[name='Support']").is(':checked')) {
        department += " Software/Technical Support,"
    }

    if ($("#CallMeBackDialog input[name='Finance']").is(':checked')) {
        department += " Finance,"
    }

    if (department == "") {
        $("#CallMeBackDialog #departmentValidation").html("<em>Please select one of the above departments</em>").show();
        valid = false;
    }

    if (name == "" || telephone == "" || email == "" || postcode == "" || department == "") {
        $("#CallMeBackDialog #departmentValidation").html("<em>Some required information is missing.<br/>Please check all fields and try again.</em>").show();
        valid = false;
    }

    department = department.substring(0, department.length - 1);
    
    if (valid) {
        $("#CallMeBackDialog #sendCallBack").attr({ value: "Sending...", disabled: "true" });
        $.ajax({
            type: "POST",
            url: "/post",
            data: { Name: name, Telephone: telephone, Email: email, Postcode: postcode, Department: department, Template: "callBack" },
            success: function(theResponse) {
                $("#CallMeBackDialog .popUpForms").html(theResponse);
                pageTracker._trackPageview("/call-me-back/thank-you");
            }
        });
    }

}

function rigRoadShowDialog() {
    $("#RoadShowDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        draggable: false,
        resizable: false,
        height: 390,
        width: 620,
        modal: true,
        buttons: {
    },
    close: function() {
        $.ajax({
            type: "GET",
            url: "/vebra-roadshows",
            success: function(theResponse) {
                var $holder = theResponse;
                $("#RoadShowDialog .popUpForms").html($(".right_content", $holder).html());
                $("#RoadShowDialog .popUpForms h1").width("600px");
                $("#RoadShowDialog .popUpForms .CMSLinks #Delete").parent("li").remove();
                //$("#RoadShowDialog .popUpForms h1").hide();
                $("#sendRoadshow").click(function() {
                    $("#sendRoadshow").attr({ value: "Sending...", disabled: "true" });
                    sendRoadShow();
                });
            }
        });
    }

});
callRoadShowClick();
}

function callRoadShowClick() {
    $("#roadshow img").click(function() {
        pageTracker._trackPageview("/vebra-roadshows");
        $.ajax({
            type: "GET",
            url: "/vebra-roadshows",
            success: function(theResponse) {
                var $holder = theResponse;
                $("#RoadShowDialog .popUpForms").html($(".right_content", $holder).html());
                $("#RoadShowDialog .popUpForms h1").width("600px");
                $("#RoadShowDialog .popUpForms .CMSLinks #Delete").parent("li").remove();
                //$("#RoadShowDialog .popUpForms h1").hide();
                $("#sendRoadshow").click(function() {
                    $("#sendRoadshow").attr({ value: "Sending...", disabled: "true" });
                    sendRoadShow();
                });
                $("#RoadShowDialog").dialog("open");
            }
        });
        return false;
    });
}

function sendRoadShow() {

    var name = $("#RoadShowDialog #rsName").val();
    var email = $("#RoadShowDialog #rsEmail").val();
    var telephone = $("#RoadShowDialog #rsTelephone").val();

    $.ajax({
        type: "POST",
        url: "/post",
        data: { Name: name, Email: email, Telephone: telephone, Template: "RoadShow" },
        success: function(theResponse) {
            var $holder = theResponse;
            $("#RoadShowDialog .popUpForms").html($(".right_content", $holder).html());
            pageTracker._trackPageview("/vebra-roadshows/thank-you");
        }
    });
}

function rigVebraWebDialog() {
    $("#VebraWebDialog").dialog({
        bgiframe: true,
        autoOpen: false,
        draggable: false,
        resizable: false,
        height: 'auto',
        width: 620,
        modal: true,
        buttons: {
    },
    close: function() {
        $.ajax({
            type: "GET",
            url: "/vebralive-web",
            success: function(theResponse) {
                var $holder = theResponse;

                $("#VebraWebDialog .popUpForms").html($(".right_content", $holder).html());
                $("#VebraWebDialog .popUpForms .CMSLinks #Delete").parent("li").remove();
                $("#VebraWebDialog #vebraLiveForm").attr("id", "vebraPopLiveForm");
                $("#VebraWebDialog #sendVebraWeb").attr("id", "sendPopVebraWeb").click(function() {
                    $("#sendPopVebraWeb").attr({ value: "Sending...", disabled: "true" });
                    sendVebraWeb();
                });
            }
        });
    }

});
callVebraWebClick();
}

function callVebraWebClick() {
    $("#vebraWeb").click(function() {
        pageTracker._trackPageview("/vebralive-web");
        $.ajax({
            type: "GET",
            url: "/vebralive-web",
            success: function(theResponse) {
                var $holder = theResponse;

                $("#VebraWebDialog .popUpForms").html($(".right_content", $holder).html());
                $("#VebraWebDialog .popUpForms .CMSLinks #Delete").parent("li").remove();
                $("#VebraWebDialog #vebraLiveForm").attr("id", "vebraPopLiveForm");
                $("#VebraWebDialog #sendVebraWeb").attr("id", "sendPopVebraWeb").click(function() {
                    sendVebraWeb();
                });
                $("#VebraWebDialog").dialog("open");
            }
        });
        return false;
    });

    $("#vebraWebLink").click(function() {
        pageTracker._trackPageview("/vebralive-web");
        $.ajax({
            type: "GET",
            url: "/vebralive-web",
            success: function(theResponse) {
                var $holder = theResponse;

                $("#VebraWebDialog .popUpForms").html($(".right_content", $holder).html());
                $("#VebraWebDialog .popUpForms .CMSLinks #Delete").parent("li").remove();
                $("#VebraWebDialog #vebraLiveForm").attr("id", "vebraPopLiveForm");
                $("#VebraWebDialog #sendVebraWeb").attr("id", "sendPopVebraWeb").click(function() {
                    sendVebraWeb();
                });
                $("#VebraWebDialog").dialog("open");
            }
        });
        return false;
    });

    $("#sendVebraWeb").click(function() {
        sendWebVebraWeb();
    });
}

function openVebraWeb() {
    pageTracker._trackPageview("/vebralive-web");
    $("#VebraWebDialog").dialog("open");
    return false;
}

function sendVebraWeb() {

    var name = $("#VebraWebDialog #vwName").val();
    var email = $("#VebraWebDialog #vwEmail").val();
    var telephone = $("#VebraWebDialog #vwTelephone").val();
    var postcode = $("#VebraWebDialog #vwPostcode").val();
    var location = $("#VebraWebDialog #Location").val();
    var valid = true;

    if (name == "") {
        alert("Your full name is required");
        valid = false;
        return false;
    }

    if (email == "") {
        alert("Your email address is required");
        valid = false;
        return false;
    }

    if (postcode == "") {
        alert("Your postcode is required");
        valid = false;
        return false;
    }

    if (valid) {
        $("#sendPopVebraWeb").attr({ value: "Sending...", disabled: "true" });
        $.ajax({
            type: "POST",
            url: "/post",
            data: { Name: name, Email: email, Telephone: telephone, Location: location, Postcode: postcode, Template: "VebraWeb" },
            success: function(theResponse) {
                var $holder = theResponse;
                $("#VebraWebDialog .popUpForms").html($(".right_content", $holder).html());
                pageTracker._trackPageview("/vebralive-web/thank-you");
            }
        });
    }
}

function sendWebVebraWeb() {

    var name = $("#right_cont #vwName").val();
    var email = $("#right_cont #vwEmail").val();
    var telephone = $("#right_cont #vwTelephone").val();
    var postcode = $("#right_cont #vwPostcode").val();
    var location = $("#right_cont #Location").val();
    var valid = true;

    if (name == "") {
        alert("Your full name is required");
        valid = false;
        return false;
    }

    if (email == "") {
        alert("Your email address is required");
        valid = false;
        return false;
    }

    if (postcode == "") {
        alert("Your postcode is required");
        valid = false;
        return false;
    }

    if (valid) {
        $("#right_cont #vwName").attr("name", "Name");
        $("#right_cont #vwEmail").attr("name", "Email");
        $("#right_cont #vwTelephone").attr("name", "Telephone");
        $("#vebraLiveForm").submit();
    }
}
