﻿// script dedicated to event page

$(document).ready(function() {
    SetUpEventRegistrationForm();
});


/**
* when the event is by invitation only we display a link on the page to show a form
* where people can register. This form shows up in a dialog controled via jquery.
* In this function we wrap the form into the dialog first and then attach an event handler
* to the link to show the dialog box.
**/
function SetUpEventRegistrationForm() {

    var formBox = $("#ctl00_ContentArea_EventRegistrationForm_EventRegistrationWrapper");
    var button = $("#ctl00_ContentArea_EventRegistrationForm_ok");

    // using the dialog features move the content of the dialog to the very end of the page.
    // this behavior is a workaround for some IE issue. But then, our fields are out of the form
    // and therefore cannot be submitted. That's why here, we add some behavior on the click event on
    // the "register" button. It simply moves the content of the dialog (our fields) to the end of the
    // form element (only one in asp.net pages) and close the dialog box. Then the form is submitted beacause
    // the click follow up with regular behavior and is now inside the form element. Hope this helps :)
    button.click(function() {
        // those following two tests are part of the ASP.NET clien API. To make the page is actually
        // valid before submitting the form.
        if (typeof (Page_Validators) != "undefined" && typeof (Page_ClientValidate) == 'function') {
            if (Page_ClientValidate()) {
                formBox.dialog('close');
                formBox.hide();
                formBox.appendTo('form');
            }
        }
    });
    
    
    // preparing the dialog box
    formBox.dialog({
        autoOpen: false,
        title: 'Registration form',
        draggable: 'true',
        minWidth: 500,
        width: 500,
        modal:true
    });

    // atttaching the open dialog event handler to the link click event
    $("#eventRegister").click(function() {
        formBox.dialog('open');
    });
    
    // we move the message box to the top of the page to make sure it's visible
    $("#ctl00_ContentArea_EventRegistrationForm_MessageWrapper").prependTo($("#DefaultContentAreaWrapper"));

    // open the dialog box by default if the URL has the register=true parameter
    var register = $.getUrlVar('register');
    if (register != undefined && register.toLowerCase() == "true") {
        formBox.dialog('open');
    }
}

