$(function () {
    appointmentDialog().dialog({
        autoOpen: false,
        height: 570,
        width: 640,
        top: "7%",
        draggable: false,
        resizable: false,
        modal: true
    });
    $(".ui-datepicker-current-day a").removeClass("ui-state-active ui-state-hover");
    $('.ui-dialog-titlebar-close').click(clearValidation);
    $('.ui-widget-overlay').live("click", function () {
        //Close the dialog
        $("#AppointmentDialog").dialog("close");
        clearValidation();
    });
    $("#AppointmentTabs").tabs("div#AppointmentPanes > div.steps", function (event, index) { });
    $("#AppointmentBack").hide();
    $(".RequestedDate").val('');


    $(".AppointmentLink:not('body.sfPageEditor .AppointmentLink')").bind("click", eval(Enilon.BrowserConfiguration.AppointmentLinkFunctionName));
    


    tabs = $("#AppointmentTabs").data("tabs");
    $("#AppointmentTabs li").unbind('click');
    $("#AppointmentBack").bind("click", goBack);
    $("#AppointmentNext").bind("click", goNext);
    $("#AddRoomsButton").bind("click", addRoomsClick);
    $(".SelectionsDeleteButton").live("click", deleteSelectionsClick);
    $(".AppointmentSubmit").hide();
    $(".AppointmentSubmit").bind("click", submitAppointmentForm);

    //wireup promocode if there is one from Editable Appointment Callout
    if ($(".HiddenPromoCode").length > 0) {
        $("#PromotionalCode").val($(".HiddenPromoCode").val());
    }

    moveFieldsForTesting();
});



function moveFieldsForTesting() {
    //alert(Enilon.BrowserConfiguration.FormLayout);
    if (Enilon.BrowserConfiguration.FormLayout == 1) {
        $(".MoveForTest").appendTo("#MoveForTestLanding");
        $("#MoveForTestLanding").show();
    }
}

function clearValidation() {
    if ($('.error').length > 0) {
        $('.error').each(function () {
            $(this).remove();
        });
    }
}



function createConversionIframe() {
    var iframe = document.createElement("iframe");
    iframe.style.width = "0px";
    iframe.style.height = "0px";
    document.body.appendChild(iframe);
    iframe.src = "/CustomControls/TrackingCodes.aspx";
};


function checkValidation() {
    step = "#Step" + (tabs.getIndex() + 1);
    validator = findFormFields(step).validator({
        offset: [-30, -50]
    }).data("validator");
    var isValid = validator.checkValidity();
    return isValid;
}

function addRoomsClick(e) {
    var template = $($(".SelectionsDivTemplate")[0]).clone();
    template.hide();
    $("#SelectionsDiv").append(template);
    template.fadeIn();
}

function submitAppointmentForm(e) {
    tabs.next();
    $(".AppointmentSubmit").hide();


    saveAppointment();
}



function deleteSelectionsClick(e) {
    selectionsCount = $(".SelectionsDivTemplate").length;
    if (selectionsCount == 1) return;
    var parentRow = $(e.currentTarget).parent();
    parentRow.fadeOut(function () {
        parentRow.remove();
    });
}

function openDialog(e) {
    if( e != undefined )
      e.preventDefault();
    appointmentDialog().dialog("open");
    $("#AppointmentDialog").parent().animate({top:'+=10px'}, 100);
    trackPage(0);
}

function redirectMobileAppt() {
    var url = "/mobile/mobile-appt";
    if (Enilon.BrowserConfiguration.Name == "Kiosk")
        url += "?devicecode=" + Enilon.BrowserConfiguration.DeviceCode;
    window.location = url;
}

function appointmentDialog() {
    return $("#AppointmentDialog");
}

function returnToForm() {
    $("#MainForm").append(appointmentDialog());
    return true;
}
