var aActiveFields = new Array();
//pole prevedene na string pomoci toString pro snadnejsi vzhledavani v poli
var sIciDestCodes = '';

function dummy(){}

/**
 * Trida pro funkcionalitu booking formulare
 */
function Booking() {
    /**
     * @var DOMobject Objekt s rezervacnim formularem
     */
    this.elForm = jQ('#bookingIbsForm');
    /*
     *
     */
    this.checkForm = new CheckForm('#bookingIbsForm');
}

/**
 * Nastavuje formular po vybrani tridy business
 */
Booking.prototype.setDates = function() {

    // Datum odletu
    var dateDeparture = new Date();
    dateDeparture.setTime( dateDeparture.getTime() + 3 * 24 * 3600 * 1000 );

    // Datum navratu
    var dateReturn = new Date();
    dateReturn.setTime( dateReturn.getTime() + 6 * 24 * 3600 * 1000 );
    
    // Maximalni datum je aktualni plus 360 dni
    var dateEnd = new Date();
    dateEnd.setTime( dateEnd.getTime() + 360 * 24 * 3600 * 1000 );

    // Nastavuje polozku odlet
    jQ(this.elForm).find('#departure0_ibs').val(
        dateDeparture.getDate() + '.' + (dateDeparture.getMonth()+1) + '.' + dateDeparture.getFullYear()
    );
    jQ(this.elForm).find('#bookingDay0').val(dateDeparture.getDate());
    jQ(this.elForm).find('#bookingMonthSel0').val((dateDeparture.getMonth()+1) + '/' + dateDeparture.getFullYear());

    // Nastavuje polozku prilet    
    jQ(this.elForm).find('#return0_ibs').val(
        dateReturn.getDate() + '.' + (dateReturn.getMonth()+1) + '.' + dateReturn.getFullYear()
    );
    jQ(this.elForm).find('#bookingDay1').val(dateReturn.getDate());
    jQ(this.elForm).find('#bookingMonthSel1').val((dateReturn.getMonth()+1) + '/' + dateReturn.getFullYear());
    
    // Nastavuje maximalni datum pro booking
    jQ(this.elForm).find('#maxDate').val(
        dateEnd.getDate() + '.' + (dateEnd.getMonth()+1) + '.' + dateEnd.getFullYear()
    );
}

/**
 * Nastavuje formular po vybrani tridy business
 */
Booking.prototype.switchBusiness = function() {
    jQ(this.elForm).find('#prefCabinCheck').change(function() {
        var strVal = jQ(this).val();
        // Predava priznak tridy pro hidden
        jQ('#prefCabinHidden_ibs').val(strVal);
    });
}

/**
 * Nastavuje action formulare dle zadani promo kodu
 */
Booking.prototype.sendPromoIbs = function() {

    var thisObject = this;

    // Meni action na zaklade promo kodu
    jQ(this.elForm).find('#bookingSubmit').click(function() {
        var strVal = jQ(thisObject.elForm).find('#promocodeBooking').val();
        var strAction = '';
        if (strVal && strVal != '') {
            // Presmerovava z jinymi udaji
            strPassword = jQ(thisObject.elForm).find('#promoPassword').val();
            strAction = jQ(thisObject.elForm).find('#actionPromocode').val();
            strAction = strAction + '?login=' + strVal + '&password=' + strPassword + '&next=login';
            
            window.open(strAction); // Rusi odeslani formulare
            return false;
        }
    });
}

/**
 * Zobrazuje vyber letiste na cestu zpet
 */
Booking.prototype.switchReturnFrom = function() {
    
    var thisObject = this;
    
    jQ(this.elForm).find('#bookingTypeOfTrip').change(function() {
        var strVal = jQ(this).val();
        
        // Zobrazuje default
        if (strVal == 'OJ') {
            
            // Nastavuje kontroly na formulare
            thisObject.checkForm.isEmpty('DEP_1', '#bookingFrom1');
            thisObject.checkForm.isEmpty('ARR_1', '#bookingTo1');
            
            jQ('#bookingReturnFrom').addClass('show').removeClass('hide');
            jQ('#return0_ibsFrame').addClass('show').removeClass('hide');
        // Skryva pole pro datum davratu
        } else if (strVal == 'OW') {

            // Odnastavuje kontroly z formularu            
            thisObject.checkForm.unbind('submit.DEP_1', '#bookingFrom1');
            thisObject.checkForm.unbind('submit.ARR_1', '#bookingTo1');
            
            jQ('#bookingReturnFrom').addClass('hide').removeClass('show');
            jQ('#return0_ibsFrame').addClass('hide').removeClass('show');
        // Zobrazuje zadani dalsi destinaci            
        } else {

            // Odnastavuje kontroly z formularu
            thisObject.checkForm.unbind('submit.DEP_1', '#bookingFrom1');
            thisObject.checkForm.unbind('submit.ARR_1', '#bookingTo1');
            
            jQ('#bookingReturnFrom').addClass('hide').removeClass('show');
            jQ('#return0_ibsFrame').addClass('show').removeClass('hide');  
        }
    });
}

/**
 * Fce testuje hodnotu ve formulari datumu odledu
 */ 
Booking.prototype.checkDepDate = function(depCalendar, retCalendar) {

    var thisObject = this;

    // Nastavuje udalost na zmenu hodnoty
    jQ('#departure0_ibs').change(function() {

        var depVal = jQ(this).val();
        var elReturn = jQ('#return0_ibs');
        
        var depDate = thisObject.getCorrectDate(depVal);

        if (depDate != null) {    
            if (depCalendar.minDate.getTime() <= depDate.getTime() && depDate.getTime() <= depCalendar.maxDate.getTime()) {

                jQ(thisObject.elForm).find('#bookingDateRange').addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDeparture').addClass('hide').removeClass('show');
                
                // Nastavuje hodnoty do hidden formularu pro IBS
                jQ(thisObject.elForm).find('#bookingDay0').val(depDate.getDate());
                jQ(thisObject.elForm).find('#bookingMonthSel0').val(
                    (depDate.getMonth()+1) + '/' + depDate.getFullYear()
                );
                
                retCalendar.minDate.setDate(depDate.getDate());
                retCalendar.minDate.setMonth(depDate.getMonth());
                retCalendar.minDate.setFullYear(depDate.getFullYear());
                
                var arrVal = jQ(elReturn).val();
                var arrDate = thisObject.getCorrectDate(arrVal);   

                // Upravuje hodnotu datumu navratu
                if ( (arrDate == null) || ((arrDate.getTime() < depDate.getTime()) || (arrDate.getTime()>depCalendar.maxDate.getTime())) ) {
                    jQ(elReturn).val(
                        depDate.getDate() + '.' + (depDate.getMonth()+1) + '.' + depDate.getFullYear()  
                    );
                }
                jQ(elReturn).change();
            } else {
                jQ(thisObject.elForm).find('#bookingDeparture').addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDateRange').addClass('show').removeClass('hide');
            }
        } else {
           jQ(thisObject.elForm).find('#bookingDeparture').addClass('show').removeClass('hide');
        }
    });
}

/**
 * Fce testuje hodnotu ve formulari datumu navratu
 * 
 * @param Caledar retCalendar Objekt sd kalendarem pro zadavani navratu
 */ 
Booking.prototype.checkRetDate = function(retCalendar) {
    
    var thisObject = this;
    
    jQ('#return0_ibs').change(function() {
        
        var retVal = jQ(this).val();

        var retDate = thisObject.getCorrectDate(retVal);
        if (retDate != null) {
            if (retCalendar.minDate.getTime() <= retDate.getTime() && retDate.getTime() <= retCalendar.maxDate.getTime()) {
                
                jQ(thisObject.elForm).find('#bookingReturn').addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDateRange').addClass('hide').removeClass('show');

                // Nastavuje hodnoty do hidden formularu pro IBS
                jQ(thisObject.elForm).find('#bookingDay1').val(retDate.getDate());
                jQ(thisObject.elForm).find('#bookingMonthSel1').val(
                    (retDate.getMonth()+1) + '/' + retDate.getFullYear()
                );

            } else {
                jQ(thisObject.elForm).find('#bookingReturn').addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDateRange').addClass('show').removeClass('hide');
            }
        } else {
            jQ(thisObject.elForm).find('#bookingReturn').addClass('show').removeClass('hide');
        }
    });
    
}

/**
 * Kontroluje hodnotu ve formularich, pri zvolenem type of trip
 */ 
Booking.prototype.checkTypeOfTrip = function() {

    var thisObject = this;
    
    // Nastavuje udalost na zmenu hodnoty
    jQ(this.elForm).submit(function() {

        // Testuje, jestli je zvolen typ cesty i pro zadani vice letu
        var typeTripVal = jQ(this).find('#bookingTypeOfTrip').val();
        if (typeTripVal == 'OJ') {
            var strFrom2 = jQ(this).find('#from2_ibs').val();
            var strTo2 = jQ(this).find('#to2_ibs').val();            
            
            var elFrom = jQ(this).find('#bookingFrom1');
            if (strFrom2 != '') {
                jQ(elFrom).removeClass().addClass('show')
            } else {
            
            }
            if (strTo2 != '') {
            
            }
        }
    });
}

Booking.prototype.getCorrectDate = function(strDate) {
	
	var testDate = null;
	
	var checkReg = RegExp('^(([0-3][0-9]|[0-9])\.([1]{1}[0-2]{1}|[0]{1}[1-9]{1}|[1-9]{1})\.[2]{1}[0]{1}[0-9]{2})$');
    if (checkReg.test(strDate) == true) {

       	var viewDate = new Date();
        
        var parseDate = strDate.split('.', 3);
   		for (var index in parseDate) {
			intValue = parseDate[index] * 1;
			index = index * 1;
			switch(index) {
				case 0:
				  viewDate.setDate(intValue);
				  var day = intValue;
				  break;
				case 1:
    			  intValue = intValue - 1; // Index mesice 0..11
				  var month = intValue;					
				  viewDate.setMonth(intValue);
				  break;
				case 2:
				    var year = intValue;		
					viewDate.setFullYear(intValue);
				  break;
			}
   		}
   		
   		if ( 
   		    day == (viewDate.getDate() * 1) &&
   		    month == (viewDate.getMonth() * 1) &&
            year == (viewDate.getFullYear() * 1)
   		) {
   		    testDate = viewDate;
   		}
   	}

	return testDate;
}

/**
 * Fce jez se vola po vytvoreni domu
 * vola veskere potrebne metody pro kontrolu rezervace
 *
 * @param Caledar depCalendar Objekt sd kalendarem pro zadavani odledu
 * @param Caledar calendarRet Objekt sd kalendarem pro zadavani navratu
 */ 
function onReadyBooking() {

    var bookingIBS = new Booking();

    bookingIBS.setDates();

    // Seznam letist
    if (typeof destOpenAirportListId == 'undefined') {
        var destOpenAirportListId = new Array('airportList_dep0','airportList_arr0','airportList_dep1','airportList_arr1');
    }
    var airportList = new AirportList('#idActiveAirportList', '#aiportListPopup', '#select_aiportListPopup', '#airportListTR', '#airportListContainer');
    airportList.setAirportList(destOpenAirportListId);

    // Minimalni datum
    var minDateDep = new Date();
    // Maximalni
    var maxDateDep = bookingIBS.getCorrectDate(jQ('#maxDate').val());

    var minDateRet = new Date();
    var maxDateRet = new Date();
    maxDateRet.setTime(maxDateDep.getTime());
    
    var calendarDep = new Calendar('#departure0_ibs', '#calendarDeparture', '#departure0_ibs', minDateDep, maxDateDep, false);
    calendarDep.setCal();
    
    var calendarRet = new Calendar('#return0_ibs', '#calendarReturn', '#return0_ibs', minDateRet, maxDateRet, false);
    calendarRet.setCal();

    bookingIBS.switchBusiness();
    bookingIBS.switchReturnFrom();
    bookingIBS.sendPromoIbs();
    bookingIBS.checkDepDate(calendarDep, calendarRet);
    bookingIBS.checkRetDate(calendarRet);

    bookingIBS.checkForm.isEmpty('DEP_0', '#bookingFrom0');
    bookingIBS.checkForm.isEmpty('ARR_0', '#bookingTo0');
    bookingIBS.checkForm.isEmpty('ID_LOCATION', '#bookingSaleLocation');    
    
    // Naseptavac
    setWhisperer('from1_ibs', 'from1target_ibs', 'selectTargetListFrom1_ibs');
    setWhisperer('to1_ibs', 'to1target_ibs', 'selectTargetListTo1_ibs');
    setWhisperer('from2_ibs', 'from2target_ibs', 'selectTargetListFrom2_ibs');
    setWhisperer('to2_ibs', 'to2target_ibs', 'selectTargetListTo2_ibs');
}