var reservationOldOnload = window.onload;

window.onload = function()
{
    if (typeof reservationOldOnload == 'function')
    {
         reservationOldOnload();
    }

    window.reservationInstance = new reservation();
    reservationInstance.init();
}


reservation = function ()
{
    this.calendarContainer = null;
    this.calendarNavigationIds = ['calendarPreviousMonthLink', 'calendarNextMonthLink'];



}



reservation.prototype.init = function()
{
    this.calendarContainer = document.getElementById('calendarContainer');
    if (!this.calendarContainer)
    {
        return;
    }

    this.loadCalendarBlockBehaviour();
    this.assignFormEvents();
}

reservation.prototype.loadCalendarBlockBehaviour = function ()
{
    // collect month links
    var script = this;
    var calendarTable = document.getElementById('calendarTable');
    if (!calendarTable)
    {
        return null;
    }

    var links = calendarTable.getElementsByTagName( 'a' );


    for (var i=0; i<links.length; i++ )
    {
        var link = links[i];
        if (!link)
        {
            continue;
        }
        link.blockUrl = link.href + '&block=calendar&random=' + Math.random();
        link.onclick = function()
        {
            var el = this;
            loadXmlHttp(el.blockUrl, script.processCalendarBlockResponse, script);
            el = null;
            return false;
        }
		
    }
	var table = document.getElementById('calendarTable'); 
	var cells = table.getElementsByTagName('td');
	




}
reservation.prototype.processCalendarBlockResponse = function (xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
    script.calendarContainer.innerHTML = xmlhttp.responseText;
    script.loadCalendarBlockBehaviour();

    var jsValidation = new jsValidationScript();
    jsValidation.init();
    script.assignFormEvents();
}


reservation.prototype.assignFormEvents = function()
{
    var form = document.getElementById('reservationForm');
    if (!form)
    {
        return null;
    }

    var script = this;

    if (typeof form.onsubmit != 'function')
    {
        window.setTimeout('window.reservationInstance.assignFormEvents()', 300);
        return false;
    }


    form.oldOnsubmit = form.onsubmit;
    form.onsubmit = function()
    {

        var validationResult = this.oldOnsubmit();
        if (!validationResult)
        {
            return false;
        }


        Blocks.submitFormAsBlock (this, 'step2', script.step2Callback, script);
        return false;
    }

}


reservation.prototype.assignForm2Events = function()
{

    var form = document.getElementById('reservationForm2');
    if (!form)
    {
        return null;
    }

    var script = this;

    form.oldOnsubmit = form.onsubmit;
    form.onsubmit = function()
    {
        var validationResult = this.oldOnsubmit();
        if (!validationResult)
        {
            return false;
        }

        Blocks.submitFormAsBlock (this, 'finish', script.finishCallback, null);
        return false;
    }

}

reservation.prototype.step2Callback = function (xmlhttp, script)
{
    var box = document.getElementById('resOptions');
    if (!box)
    {
        return null;
    }
    box.innerHTML = xmlhttp.responseText;


    var jsValidation = new jsValidationScript();
    jsValidation.init();
    script.assignForm2Events();
}


reservation.prototype.finishCallback = function (xmlhttp, params)
{
    var box = document.getElementById('resOptions');
    if (!box)
    {
        return null;
    }
    box.innerHTML = xmlhttp.responseText;
}


function displayBorder(el) {
	
}