var SFLIGHT = new Interface("IBE",0,"sflight");
var countInitReq = 0;

SFLIGHT.InitOnLoad = function(){
	if ( countInitReq == 100 ) return;

    var mustHave = ['FormBase', 'DateSelector'];
   	var injObj   = { SFlightSearch:[], DateSelector:[] };

   	for(i=0; i < mustHave.length; i++) {
        if(typeof(window[mustHave[i]])=='undefined'){
        	countInitReq++;
        	window.setTimeout("IBE.InitOnLoad()",20); return;
        }
    }

    this.Inject(injObj);
    this.Init();
};

/**
*@constructor
*@extends FormBase
*@requires Terminauswahl
*@requires Zielauswahl
*@requires Reisende
*@requires Kalender
*/

function SFlightSearch(){
   	FormBase.apply(this);
    Target.apply(this,[]);

    this.BookingAction = function(){

    	if ( this.req['fTargetArea'] != 'FALSE' ) {
	        var bform=document.forms[this.formName];

	      	bform.elements["ibeInput[depDate]"].value=bform.elements["ibeInput[depDay]"].value+"."+bform.elements["ibeInput[depMonth]"].value;
	       	bform.elements["ibeInput[retDate]"].value=bform.elements["ibeInput[retDay]"].value+"."+bform.elements["ibeInput[retMonth]"].value;

	       	if( typeof ( bform.elements["ibeInput[area]"]) !='undefined') {
	       		var area = bform.elements["ibeInput[area]"].options[bform.elements["ibeInput[area]"].selectedIndex].value.split("|");
	       		bform.elements["ibeInput[area]"].options[bform.elements["ibeInput[area]"].selectedIndex].value = area[0];
	       	}
    	}else {
    		var bform=document.forms[this.formName];

	      	bform.elements["ibeInput[depDate]"].value=bform.elements["ibeInput[depDay]"].value+"."+bform.elements["ibeInput[depMonth]"].value;
	       	bform.elements["ibeInput[retDate]"].value=bform.elements["ibeInput[retDay]"].value+"."+bform.elements["ibeInput[retMonth]"].value;

	       	if ( bform.elements["ibeInput[dest]"].value == "12" ) {
	       		bform.elements["ibeInput[dest]"].value = "-1";
	       		bform.elements["ibeInput[flightDuration]"].value = "0";
	       	}else if(bform.elements["ibeInput[dest]"].value == "13" ) {
	       		bform.elements["ibeInput[dest]"].value = "-1";
	       		bform.elements["ibeInput[flightDuration]"].value = "1";
	       	}else if(bform.elements["ibeInput[dest]"].value == "14" ) {
	       		bform.elements["ibeInput[dest]"].value = "-1";
	       		bform.elements["ibeInput[flightDuration]"].value = "2";
	       	}

	       	/*if( typeof ( bform.elements["ibeInput[area]"]) !='undefined') {
	       		var area = bform.elements["ibeInput[area]"].options[bform.elements["ibeInput[area]"].selectedIndex].value.split("|");
	       		bform.elements["ibeInput[area]"].options[bform.elements["ibeInput[area]"].selectedIndex].value = area[0];
	       	}*/

	       	if ( bform.elements["ibeInput[dest]"].selectedIndex != 0 ) {
	       		bform.elements["ibeInput[village]"].value = "";
	       		bform.elements["ibeInput[area]"].value = "";
	       	}
    	}

       	return this.ChkFormular();
    };

    this.ChkFormular = function(){
        var bform=document.forms[this.formName];
        if(this.req['depDate']>this.req['retDate']){
            alert("Ihr Rückreisedatum liegt vor dem Hinreisedatum");
            return false;
        }
        var tmpy=bform.zimmer2;
        if(typeof(tmpy)!='undefined'){
            if(bform.zimmer[6].checked==true && bform.zimmer2.selectedIndex==0){
                alert("Bitte wählen Sie ein Zimmertypen aus.");
                bform.zimmer2.focus();
                return false;
            }
        }

        return true;
    };

    /**
    *Wird nach dem Laden der Seite ausgef?hrt
    */

     this.Init = function(){
        if(!document.forms[this.formName]) {  return; }
        with( this ) {
			termin_hin = this.req['depDate'].split("-")[2]+'.'+this.req['depDate'].split("-")[1]+'.'+this.req['depDate'].split("-")[0];
			termin_rueck = this.req['retDate'].split("-")[2]+'.'+this.req['retDate'].split("-")[1]+'.'+this.req['retDate'].split("-")[0];

			this.fieldDepDayDate = "sFlightInput[outbound_date_day]";
			this.fieldDepMonthDate = "sFlightInput[outbound_date_month]";

			this.fieldRetDayDate = "sFlightInput[inbound_date_day]";
			this.fieldRetMonthDate = "sFlightInput[inbound_date_month]";


			this.erster_termin = this.req['erster_termin'];
			this.letzter_termin_hin = this.req['letzter_termin_hin'];
			this.letzter_termin_rueck = this.req['letzter_termin_rueck'];
			this.mode = this.req['mode'];
			InitDays(1);
            InitMonth(1);
            InitDays(0);
            InitMonth(0);
            UpdateDate('dep');
            UpdateDate('ret');
        }
    };

	this.flightChangeSelectDatesDep = function( calendar ) {
		var y = flightDepCal.date.getFullYear();
		var m = flightDepCal.date.getMonth(); // integer, 0..11
		var d = flightDepCal.date.getDate();
		document.forms["sflight"].elements["sFlightInput[outbound_date_day]"].value = d;
		if ( m < 9 ) {
			document.forms["sflight"].elements["sFlightInput[outbound_date_month]"].value = "0"+(m+1)+"."+y;
		}else {
			document.forms["sflight"].elements["sFlightInput[outbound_date_month]"].value = (m+1)+"."+y;
		}
		SFLIGHT.SetDay ( "dep" );
		SFLIGHT.SetMonth ( "dep" );
		SFLIGHT.changeCalendar( "dep", "flightDepCal" );
	};

	this.flightChangeSelectDatesRet = function() {
		var y = flightArrCal.date.getFullYear();
		var m = flightArrCal.date.getMonth();
		var d = flightArrCal.date.getDate();
		document.forms["sflight"].elements["sFlightInput[inbound_date_day]"].value = d;
		if ( m < 9 ) {
			document.forms["sflight"].elements["sFlightInput[inbound_date_month]"].value = "0"+(m+1)+"."+y;
		}else {
			document.forms["sflight"].elements["sFlightInput[inbound_date_month]"].value = (m+1)+"."+y;
		}
		SFLIGHT.SetDay ( "ret" );
		SFLIGHT.SetMonth ( "ret" );
		SFLIGHT.changeCalendar( "ret", "flightArrCal" );
	};

    this.changeCalendar = function( direction, which ) {
		var calendar;
		switch( which ) {
			case 'flightDepCal':
				calendar = flightDepCal;
			break;
			case 'flightArrCal':
				calendar = flightArrCal;
			break;
/*			case 'packageDepCal':
				calendar = packageDepCal;
			break;
			case 'packageArrCal':
				calendar = packageArrCal;
			break;*/
		}
		if ( direction != "") {
			var nTag = this.getValue((direction=="dep")?this.fieldDepDayDate:this.fieldRetDayDate);
	  		var strMon = this.getValue((direction=="dep")?this.fieldDepMonthDate:this.fieldRetMonthDate);
	  		arrMon = strMon.split(".");
			calendar.setDate( new Date( arrMon[1], arrMon[0] - 1, nTag ) );
		}
 		else {
			direction = "dep";
			var nTag = this.getValue((direction=="ret")?this.fieldRetDayDate:this.fieldDepDayDate);
	  		var strMon = this.getValue((direction=="ret")?this.fieldRetMonthDate:this.fieldDepMonthDate);
	  		arrMon = strMon.split(".");
			calendar.setDate( new Date( arrMon[1], arrMon[0] - 1, nTag ) );
			direction = "ret";
			var nTag = this.getValue((direction=="ret")?this.fieldRetDayDate:this.fieldDepDayDate);
	  		var strMon = this.getValue((direction=="ret")?this.fieldRetMonthDate:this.fieldDepMonthDate);
	  		arrMon = strMon.split(".");
			calendar.setDate( new Date( arrMon[1], arrMon[0] - 1, nTag ) );
		}
     };

	 this.disallowDateDep = function( date ) {
	 	var curDate = new Date();
	 	if( date.getTime() > ( curDate.getTime() + 360 * 86400 * 1000 ) || date.getTime() < ( curDate.getTime() ) ) {
			return true;
		}
		return false;
	 };

	 this.disallowDateRet = function( date ) {
	 	var curDate = new Date();
	 	if( date.getTime() > ( curDate.getTime() + 360 * 86400 * 1000 ) || date.getTime() < ( curDate.getTime() ) ) {
			return true;
		}
		return false;
	 };

	 this.disallowAllDates = function(date){
	 	return true;
	 };
}
