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(identName) { 
    /*
     * @var CheckForm 
     */
    this.checkForm = new CheckForm('#bookingForm_' + identName);
    /**
     * @var DOMobject Objekt s rezervacnim formularem
     */
    this.elForm = jQ('#bookingForm_' + identName);
    /*
     * @var string identName
     */
    this.identName = identName;       
}

/**
 * Nastavuje datumy do polozek
 */
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 330 dni
    var dateEnd = new Date();
    dateEnd.setTime( dateEnd.getTime() + 330 * 24 * 3600 * 1000 );

    // Nastavuje polozku odlet
    jQ(this.elForm).find('#departure0_' + this.identName).val(
        dateDeparture.getDate() + '.' + (dateDeparture.getMonth()+1) + '.' + dateDeparture.getFullYear()
    );
    jQ(this.elForm).find('#bookingDay0_' + this.identName).val(dateDeparture.getDate());
    jQ(this.elForm).find('#bookingMonthSel0_' + this.identName).val((dateDeparture.getMonth()+1) + '/' + dateDeparture.getFullYear());

    // Nastavuje polozku prilet    
    jQ(this.elForm).find('#return0_' + this.identName).val(
        dateReturn.getDate() + '.' + (dateReturn.getMonth()+1) + '.' + dateReturn.getFullYear()
    );
    jQ(this.elForm).find('#bookingDay1_' + this.identName).val(dateReturn.getDate());
    jQ(this.elForm).find('#bookingMonthSel1_' + this.identName).val((dateReturn.getMonth()+1) + '/' + dateReturn.getFullYear());
    
    // Nastavuje maximalni datum pro booking
    jQ(this.elForm).find('#maxDate_' + this.identName).val(
        dateEnd.getDate() + '.' + (dateEnd.getMonth()+1) + '.' + dateEnd.getFullYear()
    );
}

/**
 * Nastavuje formular po vybrani tridy business
 */
Booking.prototype.switchBusiness = function() {
    
    var thisObject = this;

    jQ(this.elForm).find('#prefCabinCheck_' + this.identName).change(function() {
        var strVal = jQ(this).val();
        // Predava priznak tridy pro hidden
        jQ('#prefCabinHidden_' + thisObject.identName).val(strVal);
    });
}

/**
 * Zobrazuje vyber letiste na cestu zpet
 */
Booking.prototype.switchReturnFrom = function() {
    
    var thisObject = this;

    jQ(this.elForm).find('#bookingTypeOfTrip_' + this.identName).change(function() {
        var strVal = jQ(this).val();
        
        // Zobrazuje default
        if (strVal == 'OJ') {
            
            // Nastavuje kontroly na formulare
            thisObject.checkForm.isEmpty('DEP_1', '#bookingFrom1_' + thisObject.identName);
            thisObject.checkForm.isEmpty('ARR_1', '#bookingTo1_' + thisObject.identName);
            
            jQ('#bookingReturnFrom_' + thisObject.identName).addClass('show').removeClass('hide');
            jQ('#return0_ibsFrame_' + thisObject.identName).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.identName);
            thisObject.checkForm.unbind('submit.ARR_1', '#bookingTo1_' + thisObject.identName);
            
            jQ('#bookingReturnFrom_' + thisObject.identName).addClass('hide').removeClass('show');
            jQ('#return0_ibsFrame_' + thisObject.identName).addClass('hide').removeClass('show');
        // Zobrazuje zadani dalsi destinaci            
        } else {

            // Odnastavuje kontroly z formularu
            thisObject.checkForm.unbind('submit.DEP_1', '#bookingFrom1_' + thisObject.identName);
            thisObject.checkForm.unbind('submit.ARR_1', '#bookingTo1_' + thisObject.identName);
            
            jQ('#bookingReturnFrom_' + thisObject.identName).addClass('hide').removeClass('show');
            jQ('#return0_ibsFrame_' + thisObject.identName).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_' + this.identName).change(function() {

        var depVal = jQ(this).val();
        var elReturn = jQ('#return0_' + thisObject.identName);
        
        var depDate = thisObject.getCorrectDate(depVal);

        if (depDate != null) {    
            if (depCalendar.minDate.getTime() <= depDate.getTime() && depDate.getTime() <= depCalendar.maxDate.getTime()) {

                jQ(thisObject.elForm).find('#bookingDateRange_' + thisObject.identName).addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDeparture_' + thisObject.identName).addClass('hide').removeClass('show');
                
                // Nastavuje hodnoty do hidden formularu pro IBS
                jQ(thisObject.elForm).find('#bookingDay0_' + thisObject.identName).val(depDate.getDate());
                jQ(thisObject.elForm).find('#bookingMonthSel0_' + thisObject.identName).val(
                    (depDate.getMonth()+1) + '/' + depDate.getFullYear()
                );
                
                retCalendar.minDate = new Date(depDate.getFullYear(), depDate.getMonth(), depDate.getDate());
//                retCalendar.minDate.setMonth();
//              retCalendar.minDate.setFullYear();
                
                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_' + thisObject.identName).addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDateRange_' + thisObject.identName).addClass('show').removeClass('hide');
            }
        } else {
           jQ(thisObject.elForm).find('#bookingDeparture_' + thisObject.identName).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_' + this.identName).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_' + thisObject.identName).addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDateRange_' + thisObject.identName).addClass('hide').removeClass('show');

                // Nastavuje hodnoty do hidden formularu pro IBS
                jQ(thisObject.elForm).find('#bookingDay1_' + thisObject.identName).val(retDate.getDate());
                jQ(thisObject.elForm).find('#bookingMonthSel1_' + thisObject.identName).val(
                    (retDate.getMonth()+1) + '/' + retDate.getFullYear()
                );

            } else {
                jQ(thisObject.elForm).find('#bookingReturn_' + thisObject.identName).addClass('hide').removeClass('show');
                jQ(thisObject.elForm).find('#bookingDateRange_' + thisObject.identName).addClass('show').removeClass('hide');
            }
        } else {
            jQ(thisObject.elForm).find('#bookingReturn_' + thisObject.identName).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_' + thisObject.identName).val();
        if (typeTripVal == 'OJ') {
            var strFrom2 = jQ(this).find('#from2_' + thisObject.identName).val();
            var strTo2 = jQ(this).find('#to2_' + thisObject.identName).val();            
            
            var elFrom = jQ(this).find('#bookingFrom1_' + thisObject.identName);
            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 day, month, year = -1;
        
        var parseDate = strDate.split('.', 3);
   		for (var index in parseDate) {
			intValue = parseDate[index] * 1;
			index = index * 1;
			switch(index) {
				case 0:
				  //viewDate.setDate(intValue);
				  day = intValue;
				  break;
				case 1:
    			  intValue = intValue - 1; // Index mesice 0..11
				  month = intValue;					
				  //viewDate.setMonth(intValue);
				  break;
				case 2:
				    year = intValue;		
					//viewDate.setFullYear(intValue);
				  break;
			}
   		}
   		
   		if(day != -1 && month != -1 && year != -1) {
   		    viewDate = new Date(year, month, day);
   		}
   		
   		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(identName) {

    if (typeof identName == 'undefined') {
        identName = 'ibs';
    }
    
    var bookingIBS = new Booking(identName);

    bookingIBS.setDates();

    // Seznam letist
    if (typeof destOpenAirportListId == 'undefined') {
        var destOpenAirportListId = new Array('airportList_dep0_' + identName, 'airportList_arr0_' + identName, 'airportList_dep1_' + identName, 'airportList_arr1_' + identName);
    }
    var airportList = new AirportList('#idActiveAirportList' + identName, '#aiportListPopup' + identName, '#list_aiportListPopup' + identName);
    airportList.identName = identName;
    airportList.setAirportList(destOpenAirportListId);
  
    // Minimalni datum
    var minDateDep = new Date();
    minDateDep.setHours(0);
    minDateDep.setMinutes(0);
    minDateDep.setSeconds(0);
    minDateDep.setMilliseconds(0);
    // Maximalni

    var maxDateDep = bookingIBS.getCorrectDate(jQ('#maxDate_' + identName).val());

    var minDateRet = new Date();
    var maxDateRet = new Date();
    if (maxDateDep != null) {
        maxDateRet.setTime(maxDateDep.getTime());
    }    
    
    var calendarDep = new Calendar('#departure0_' + identName, '#calendarDeparture_' + identName, '#departure0_' + identName, minDateDep, maxDateDep, false);
    calendarDep.setCal();
    
    var calendarRet = new Calendar('#return0_' + identName, '#calendarReturn_' + identName, '#return0_' + identName, minDateRet, maxDateRet, false);
    calendarRet.setCal();

    bookingIBS.switchBusiness();
    bookingIBS.switchReturnFrom();
    bookingIBS.checkDepDate(calendarDep, calendarRet);
    bookingIBS.checkRetDate(calendarRet);

    bookingIBS.checkForm.isEmpty('DEP_0', '#bookingFrom0_' + identName);
    bookingIBS.checkForm.isEmpty('ARR_0', '#bookingTo0_' + identName);
    bookingIBS.checkForm.isEmpty('ID_LOCATION', '#bookingSaleLocation_' + identName);    

    // Naseptavac
    setWhisperer('from1_' + identName, 'from1target_' + identName, 'selectTargetListFrom1_' + identName, 'bookingSubmit_' + identName);
    setWhisperer('to1_' + identName, 'to1target_' + identName, 'selectTargetListTo1_' + identName, 'bookingSubmit_' + identName);
    setWhisperer('from2_' + identName, 'from2target_' + identName, 'selectTargetListFrom2_' + identName, 'bookingSubmit_' + identName);
    setWhisperer('to2_' + identName, 'to2target_' + identName, 'selectTargetListTo2_' + identName, 'bookingSubmit_' + identName);
}
