

//-----------------------------------------------------------------------------
//	ib_fs_err_cb.js
//-----------------------------------------------------------------------------
//
//      FSErr
//
//      JS Class to perform flight search control checks and display
//      possibly the errors.
//
//      Methods
//
//      fsc_check( theForm, noPartyCheck )
//          performs a flight search control check
//          params:
//          theForm         the document form as you get it through
//                          document.forms[ "formname" ] or as it is bound to
//                          the 'this' on form submit.
//          noPartyCheck    boolean value, determines, whether or not to check
//                          the party.
//                          true    no party check will be done
//                          false   party check is performed
//
//      fsc_check_party( theForm )
//          perform party check
//
//
//      Synopsis
//
//      var specification = {
//          LBP:        ibe.LBP,
//          divivId:    'errordivId',
//          divWidth:    200,               // optional
//          hideIds:    [ "id1", "id2" ]    // optional
//      };
//
//      var specification_minimal = {
//          LBP:        ibe.LBP,
//          divivId:    'errordivId'
//      }
//
//      var errObject = new FSErr( specification_minimal );
//
//      ... and later ...
//
//      errObject.fsc_check( document.forms.formname, false );
//
//      or as an attribute to the submit button's html tag
//      onSubmit="errObject.fsc_check( this, false )";
//
//-----------------------------------------------------------------------------
//      rg      Wed Jul 23 2008
//-----------------------------------------------------------------------------

function FSErr( cfg ) {
    this.ourDivId           = cfg.divId || null;
    this.ourDiv             = xGetElementById( this.ourDivId );
    if ( !this.ourDivId || !this.ourDiv ) {
        var txt = this.ourDivId ? this.ourDivId : "no Id passed";
        alert( "Could not get ourDiv by Id " + txt );
        return null;
    }
    this.ourDivWidth        = cfg.divWidth || 0;
    this.idsToHideOnError   = cfg.hideIds  || [];

    this.LBP                = cfg.LBP;
    if ( !this.LBP ) {
        alert( "No LBP object passed --- cannot create an FSErr object!" );
        return null;
    }
    this.origin_node        = this.LBP.getOrigNode( );
    this.destination_node   = this.LBP.getDestNode( );

    this.errList            = [];       // gathers FSErr.errors before displaying
    this.errFocus           = null;     // form field to get focus


    this.hideIds =
        function( ) { xHideAllOf( this.idsToHideOnError ); };

    this.buildParagraph =
        function( txt, i ) {
            var para  = document.createElement( "p" );
            var txtEl = document.createTextNode( txt );
            para.appendChild( txtEl );
            para.className = ( i == 0 ) ? "first" : "sub";
            this.errList.push( para );
        };

    this.addErrList =
        function( txt, errFld ) {
            var paras = txt.split( FSErr.ERR_SEP );
            for ( var i = 0; i < paras.length; i++ ) {
                this.buildParagraph( paras[ i ], i );
            }
            if ( ! this.errFocus ) {
                this.errFocus = errFld;
            }
        };

    this.showErrList =
        function( ) {
            var para,
                i,
                innerDiv;

            while( this.ourDiv.hasChildNodes() ) {
                this.ourDiv.removeChild( this.ourDiv.firstChild );
            }

            if ( this.ourDivWidth != 0 ) {
                xWidth( this.ourDiv, this.ourDivWidth );
            }

            innerDiv = document.createElement( "div" );
            innerDiv.className = "innerDiv";
            this.ourDiv.appendChild( innerDiv );

            for ( i = 0; i < this.errList.length; i++ ) {
                innerDiv.appendChild( this.errList[ i ] );
            }

            this.errList = [];
            if ( this.errFocus ) {
		try {
                    if ( this.errFocus.focus ) {
			this.errFocus.focus( );
                    }
		}
		    catch( e ) {
		}
                this.errFocus = null;
            }
        };

    this.fsc_check =
        function( theForm, restricted ) {

            var chkParty = restricted ? true : false;
            var loc_mode = this.LBP.getLocMode( );
            this.errList = [];

            if ( !this.origin_node.value ) {
                this.addErrList( FSErr.errors[ "BAD_ORIG" ], this.origin_node );
            }
            var tlc1 = this.origin_node.value;
            var loc1 = tlc1 ? this.LBP.loc( tlc1 ) : null;

            if ( !this.destination_node.value ) {
                this.addErrList( FSErr.errors[ "BAD_DEST" ], this.destination_node );
            }
            var tlc2 = this.destination_node.value;
            var loc2 = tlc2 ? this.LBP.loc( tlc2 ) : null;

	    /*
            var my1 = theForm.outwardMonthYear.options[ theForm.outwardMonthYear.selectedIndex ].value;
            var d1  = theForm.outwardDay.options[ theForm.outwardDay.selectedIndex ].value;
            var my2 = theForm.returnMonthYear.options[ theForm.returnMonthYear.selectedIndex ].value;
            var d2  = theForm.returnDay.options[ theForm.returnDay.selectedIndex ].value;
	    */

	    try {
            var my1 = theForm.outwardMonthYear.value;
            var d1  = theForm.outwardDay.value;
            var my2 = theForm.returnMonthYear.value;
            var d2  = theForm.returnDay.value;

            if ( this.errList.length == 0 && loc_in_both_times ) {
                if ( ! loc_in_both_times( my1, d1, loc1, my2, d2, loc_mode ) ) {
                    this.addErrList( loc1.repr(), this.origin_node );
                }
                if ( tlc2 !== "RSW" && tlc2 !== "HAV" ) {
                    if ( ! loc_in_both_times( my1, d1, loc2, my2, d2, loc_mode ) ) {
                       this.addErrList( loc2.repr(), this.destination_node );
                    }
                }
            }

            if ( loc_origBeforeToday( my1, d1 ) )
                this.addErrList( FSErr.errors[ "BAD_DATE_ORIG" ], theForm.outwardDay );
            if ( loc_destBeforeToday( loc_mode, my2, d2 ) )
                this.addErrList( FSErr.errors[ "BAD_DATE_DEST" ], theForm.returnDay );
            if ( !loc_inSequence( my1, d1, my2, d2, loc_mode ) )
                this.addErrList( FSErr.errors[ "DATE_SEQ" ], theForm.outwardDay );

	    }
	    catch( e ) {
	    }

            if ( chkParty ) {
                if ( theForm.partyAdults ) {
		    /*
                    var ad = parseInt( theForm.partyAdults.options[ theForm.partyAdults.selectedIndex ].value );
                    var ch = parseInt( theForm.partyChildren.options[ theForm.partyChildren.selectedIndex ].value );
                    var bb = parseInt( theForm.partyInfants.options[ theForm.partyInfants.selectedIndex ].value );
		    */
                    var ad = parseInt( theForm.partyAdults.value );
                    var ch = parseInt( theForm.partyChildren.value );
                    var bb = parseInt( theForm.partyInfants.value );

                    if ( ad + ch + bb < 1 || ad + ch + bb > 6 || ad < bb || ad * 2 < ch ) {
                        this.addErrList( FSErr.errors[ "BAD_PARTY" ], theForm.partyAdults );
                    }
                }
            }

            if ( this.errList.length > 0 ) {
                this.showErrList( );
                return false;
            } else {
                return true;
            }
        };


    this.fsc_check_party =
        function( theForm ) {

            if ( theForm.partyAdults ) {
                var ad = parseInt( theForm.partyAdults.options[ theForm.partyAdults.selectedIndex ].value );
                var ch = parseInt( theForm.partyChildren.options[ theForm.partyChildren.selectedIndex ].value );
                var bb = parseInt( theForm.partyInfants.options[ theForm.partyInfants.selectedIndex ].value );

                if ( ad + ch + bb < 1 || ad + ch + bb > 6 || ad < bb || ad * 2 < ch ) {
                    this.addErrList( FSErr.errors[ "BAD_PARTY" ], theForm.partyAdults );
                }
            }

            if ( this.errList.length > 0 ) {
                this.showErrList( );
                return false;
            } else {
                return true;
            }
        };

    function h_getFormD8( my, day ) {	// converts form data into "YYYYMMDD"
       var m  = String( my % 12  + 1);
       while ( m.length < 2 ) m = "0" + m;
       var y  = String( (my - my % 12) / 12 + 1900 );
       while ( y.length < 4 ) y = "0" + y;
       var d = String( day );
       while ( d.length < 2 ) d = "0" + d;

       return y + m + d;
    }

    function h_DateToStr( d ) {		// converts JS Date into "YYYYMMDD"
        var yyyy = String(d.getFullYear());
        while (yyyy.length < 4) yyyy = "0" + yyyy;
        var mm = String(d.getMonth() + 1);
        while (mm.length < 2) mm = "0" + mm;
        var dd = String(d.getDate());
        while (dd.length < 2) dd = "0" + dd;
        return yyyy + mm + dd;
    }

    function loc_origBeforeToday( my, d ) {
        return h_getFormD8( my, d ) < h_DateToStr( new Date() );
    }

    function loc_destBeforeToday( loc_mode, my, d ) {
        return loc_mode == "RT" && h_getFormD8( my, d ) < h_DateToStr( new Date() );
    }

    function loc_in_both_times( my1, d1, loc, my2, d2, loc_mode ) {
        // is airport operating at the two dates of departure and arrival?
        var form1_d8     = h_getFormD8( my1, d1 );
        var form2_d8     = h_getFormD8( my2, d2 );
        var tlc_d8_min   = loc.from;
        var tlc_d8_max   = loc.until;
        return ( tlc_d8_min <= form1_d8 && form1_d8 <= tlc_d8_max &&
                 ( loc_mode == "OW" ||
                   ( tlc_d8_min <= form2_d8 && form2_d8 <= tlc_d8_max ) ) );
    }

    function loc_both_in_time( my, d, loc1, loc2 ) {
       // is a date constructed from the contents of a pair of select boxes
       // within the operation time of two locations making up a route
       var form_d8     = h_getFormD8( my, d );
       var tlc1_d8_min = loc1.from;
       var tlc1_d8_max = loc1.until;
       var tlc2_d8_min = loc2.from;
       var tlc2_d8_max = loc2.until;
       return ( tlc1_d8_min <= form_d8 && form_d8 <= tlc1_d8_max &&
                tlc2_d8_min <= form_d8 && form_d8 <= tlc2_d8_max );
    }

    function loc_inSequence( m1, d1, m2, d2, loc_mode ) {
        // form dates in sequence?
       if ( loc_mode == "RT" ) {
          if ( h_getFormD8( m1, d1 ) >  h_getFormD8( m2, d2 ) )
             return false;
       }
       return true;
    }

}

//-----------------------------------------------------------------------------
//	... our data
//-----------------------------------------------------------------------------
FSErr.ERR_SEP = "-|-";    // separate multiple lines of one error
FSErr.errors = {
    "BAD_DATE_ORIG":    "Belirtilen Gidiş tarihi geçmiş durumda, Lütfen değişik bir tarih verin.",
    "BAD_DATE_DEST":    "Belirtilen Dönüş tarihi geçmiş durumda, Lütfen değişik bir tarih verin.",
    "DATE_SEQ":         "Dönüş-Tarihi , Gidiş-Tarihinden önce.Lütfen uçuş-bilgilerinizi düzeltiniz.",
    "BAD_ORIG":         "Lütfen bir Uçuş noktası seçin",
    "BAD_DEST":         "Lütfen bir Varış noktası seçin",
    "BAD_PARTY":        "Lütfen Dikkat:"  + FSErr.ERR_SEP +
                        "Toplam yolcu sayısı her bir Rezervasyon için 6 kişiyi geçmemelidir.Eğer 6 kişiden fazla yolcu için rezervasyon yapmak istiyorsanız, daha fazla Rezervasyon numarası altında yapabilirsiniz." + FSErr.ERR_SEP +
                        "Her yetişkin sadece bir Bebek yolcu ile seyahat edebilir Her koltukta sadece bir adet Oksijen Maskesi bulunduğundan dolayı Bu Sınırlama Çocuklarınızın güvenliği için." + FSErr.ERR_SEP +
                        "Tüm Yetişkin Yolcular yanlarında en fazla 2 Çocuk (Uçuş günü 12 Yaş altı) ve bir Bebek yolcuyu indirimli Tarifeden uçağa alabilir.Daha fazla Çocuk yolcu için Yetişkin fiyatı ödenir."
};

//-----------------------------------------------------------------------------
//	... the end of	ib_fs_err.js
//-----------------------------------------------------------------------------
