/** Store vehicle data*/ var _DEFAULT_DATA = $H({}); //default data. var _RETURN_IN_PAGE = false; //true when retry in page /* IDs in page */ var IDs={ // 'edition': 'edition_', REMOVED BY rel. 1.1 'searchBtn':'searchByModel', 'boite': 'boite_', 'fuel': 'fuel_', 'hp': 'hp_', 'body': 'theBody_', 'model': 'model_', 'make': 'make_', 'date': 'regDateId', 'typeMine': 'typeMine', 'div': 'carSearch', 'brandInPage' : 'theProgModelBrand' }; var Objs = null; function init(){ Objs = $A([ $( IDs['make'] ) , $( IDs['model'] ) , $( IDs['body'] ) , $( IDs['fuel'] ) , $( IDs['hp'] ) , $( IDs['boite'] ) , $( IDs['searchBtn'] ) ]); $( IDs['make'] ).privateName = 'make'; $( IDs['make'] )._fn = newSearchModel.bind( $( IDs['make'] ) , [ $H({}) ] ); $( IDs['make'] ).removeEvents().addEvent( 'change' , $( IDs['make'] )._fn ); $( IDs['model'] )._fn = newSearchEdition.bind( $( IDs['model'] ) , [ $H({}) ] ); $( IDs['model'] ).removeEvents().addEvent( 'change' , $( IDs['model'] )._fn ); $( IDs['model'] ).privateName = 'model'; $( IDs['body'] )._fn = change.bind( $( IDs['body'] ) , [ true ] ); $( IDs['body'] ).addEvent( 'change' , $( IDs['body'] )._fn ); $( IDs['body'] ).fields = [ 'desc_body' ] ; //, 'desc_body' ]; $( IDs['body'] ).privateName = 'body'; $( IDs['body'] ).populate = _populate; $( IDs['fuel'] )._fn = change.bind( $( IDs['fuel'] ) , [ true ] ); $( IDs['fuel'] ).addEvent( 'change' , $( IDs['fuel'] )._fn ); $( IDs['fuel'] ).fields = [ 'cod_fuel' , 'desc_fuel' ]; $( IDs['fuel'] ).privateName = 'fuel'; $( IDs['fuel'] ).populate = _populate; $( IDs['hp'] )._fn = change.bind( $( IDs['hp'] ) , [ true ] ); $( IDs['hp'] ).addEvent( 'change' , $( IDs['hp'] )._fn ); $( IDs['hp'] ).fields = [ 'cod_power' , 'desc_power' ]; $( IDs['hp'] ).privateName = 'hp'; $( IDs['hp'] ).populate = _populate; $( IDs['boite'] )._fn = ( function( flag ){ change.apply( this , [ flag ] ); if ( this.records.length > 15 ) return buildTable.apply( $( IDs['searchBtn'] ) , [ null , null , this ] ); }).bind( $( IDs['boite'] ) , [ true ] ); $( IDs['boite'] ).addEvent( 'change' , $( IDs['boite'] )._fn ); $( IDs['boite'] ).fields = [ 'cod_gear' , 'desc_gear' ]; $( IDs['boite'] ).privateName = 'boite'; $( IDs['boite'] ).populate = _populate; $( IDs['searchBtn'] ).removeEvents().addEvent( 'click' , function(){ if ( $( IDs['boite'] ).records ) buildTable.apply( $('searchByModel') , [ ] ) ; }); } function _populate(){ var arr = this.records.query( this.fields , null , true , this.fields[1] ) ; var field1 = this.fields[0]; var field2 = ( this.fields[1] ) ? this.fields[1] : field1; $each( arr , function( record ){ new Element( 'option' , { 'value' : record[ field1 ], 'selected': ( record[ field1 ] == _DEFAULT_DATA.get( this.privateName ) ) }).setHTML( record[ field2 ] ).inject( this ); } , this ); } /**Fat: 10/12/2008 * apply this funciton on each combo element in page */ function change( blankAll , _default ){ if ($('sectionInsuredCarAjaxActivity') && this.value) { $('sectionInsuredCarAjaxActivity').setStyle('display', 'block'); } var index = Objs.indexOf( this ); for ( var i = ( index + 1 ) ; ( i < Objs.length ) && blankAll ; i++ ) //> clearCombo( Objs[ i ] ); if ( this.getTag() == 'select' && ( this.value == '' ) ) return this; if ( this.records && ( ( index + 1 ) != Objs.length ) ){ var nextElement = Objs[ index + 1 ]; var arr = this.records.query( '*' , [{ 'field' : this.fields[0] , 'operator' : '==', 'value' : this.value }] ); nextElement.records = arr; if ( arr.length < 16 /* && !_RETURN_IN_PAGE */ ) { //> $( IDs['searchBtn'] ).records = arr; return buildTable.apply( $( IDs['searchBtn'] ) , [ null , null , this ] ); } if ( nextElement.populate ){ nextElement.populate( _default ); } } if ($('sectionInsuredCarAjaxActivity')) { $('sectionInsuredCarAjaxActivity').setStyle('display', 'none'); } } /* populate the div */ function populate( record ){ Logger.info('Populate START'); /*Make seleziono la marca nel combo */ var make = ( record && record[0].make ) ? record[0].make : undefined; Logger.debug('Populate - make: ' + make); if ( make ){ $ES( 'option' , $( IDs['make'] ) ).each( function( opt ){ if ( opt.value.toLowerCase() == make.toLowerCase() ){ opt.selected = true; Logger.debug('Populate - make found'); return Array.SKIP_EACH; } }); } /* Model: se ho un modello nella nel JSON lo cerco nel combo. Se lo trovo lo seleziono altrimenti svuoto il combo e aggiungo un option col modello della ricerca */ var model = ( record && record[0].model ) ? record[0].model : undefined; Logger.debug('Populate - model: ' + model); if ( model ){ var found = false; $ES ( 'option' , $( IDs['model'] ) ).each( function( opt ){ if ( opt.value.toLowerCase() == model.toLowerCase() ){ found = true; opt.selected = true; Logger.debug('Populate - make found'); return Array.SKIP_EACH; } }); if ( ! found ){ clearCombo( $( IDs['model'] ) ); new Element('option',{ 'value': model , 'selected':true, 'title': model }).setHTML( model ).inject( $( IDs['model'] ) ); Logger.debug('Populate - model added'); } } if ( ! model ){ clearCombo( $( IDs['model'] ) ); openCarNotFoundInfo(); return Logger.debug('Populate - model NOT FOUND'); } /* BODY */ Logger.debug('Populate - populate body'); var arrBody = record.query( [ 'cod_body' , 'desc_body' ] , null , true , 'desc_body' ); /*if ( record.length < 16 && ! _RETURN_IN_PAGE ) { //> $( IDs['searchBtn'] ).records = record; return buildTable.apply( $( IDs['searchBtn'] ) , [ null , $( IDs['model'] ) ] ); }*/ clearCombo( $( IDs['body'] ) ); Logger.debug('Populate - body found: ' + (arrBody.length-1) ); $( IDs['body'] ).records = record; $( IDs['body'] ).populate( 'desc_body' ); var index = Objs.indexOf( $( IDs['body'] ) ); for ( var i = index ; i < Objs.length ; i++ ){ if ( _DEFAULT_DATA.get( Objs[ i ].privateName ) ) Objs[ i ]._fn(); } if ($('sectionInsuredCarAjaxActivity')) { $('sectionInsuredCarAjaxActivity').setStyle('display', 'none'); } Logger.info('Populate END'); } /**Fat: * Svuoto un elemento e, se si tratta di un 'select', aggiunto un option vuota */ function clearCombo( element ){ $( element ); if ( window.ie ) element.records = undefined; else delete element.records if ( element.getTag() != 'select' ) return ; element.empty(); new Element( 'option' , { 'value' : '' , 'selected' : 'true' } ).setHTML("Sélectionner").inject( element ); } /**Fat: * svuoto tutti i campi del div */ function clearAllFields( obj ){ var index = Objs.indexOf( obj ); for ( var i = ( index + 1 ) ; i < Objs.length ; i++ ) //> clearCombo( Objs[ i ] ); } /* Search model by make ( change on 'make' select ) */ function newSearchModel ( defaultData , showData){ defaultData = defaultData || $H({}); if ($('sectionInsuredCarAjaxActivity') && this.value) { $('sectionInsuredCarAjaxActivity').setStyle('display', 'block'); } this.onComplete = function(rsp){ if ( rsp && rsp.model ){ var undefinedModel = undefined; for (var i =0 ; i< rsp.model.length ; i++ ){ //> var element = rsp.model[i]; if ( element['key'] && element['value'] ){ new Element('option', { 'value': element['key'], 'selected': ( defaultData.hasKey('model') && defaultData.get('model').trim().toLowerCase() == element['key'].trim().toLowerCase() ) }).setHTML( element['value'] ).inject( $( IDs['model'] ) ); } } newSearchEdition.apply( $( IDs['model'] ) , [ defaultData , showData ] ); } if ($('sectionInsuredCarAjaxActivity')) { $('sectionInsuredCarAjaxActivity').setStyle('display', 'none'); } }; clearAllFields( this ); i