function comparedate(date1,date2){
	var retour=-1;
	if (date1!='-1' && date2!='-1'){
		var dd1=date1.split('/');
		var dd2=date2.split('/');
		
		var d1=new Date(parseInt(parseFloat(dd1[2])), parseInt(parseFloat(dd1[1]))-1, parseInt(parseFloat(dd1[0])) );
		var d2=new Date(parseInt(parseFloat(dd2[2])), parseInt(parseFloat(dd2[1]))-1, parseInt(parseFloat(dd2[0])) );
		if(d1>d2) retour=1;
		if(d1==d2) retour=0;
		if(d1<d2) retour=2;
	}
//	alert(date1+" "+d1+" - "+date2+" "+d2+" : "+retour);
	return retour;
}

function initwidget(langue){
		if(langue=="" || langue==null){langue='fr';}
		document._arkianeg3 = { 
			jqEnvoi:jQuery('#btrechercher').hide(),
			jqNbsem:jQuery('#nbsem'),
			jqIm:jQuery('#imme'),
			jqCt:jQuery('#crit_txt'),
			jqPers:jQuery('#pers').val(-1).attr('disabled','true').change( function() { document._arkianeg3.change(); } ), 
			jqSitu:jQuery('#crit1').val(-1).attr('disabled','true').change( function() { document._arkianeg3.change(); } ), 
			jqConf:jQuery('#crit2').val(-1).attr('disabled','true').change( function() { document._arkianeg3.change(); } ), 
			jqMultipass:jQuery('#multipass').removeAttr('checked').attr('disabled','true').change( function() { document._arkianeg3.change(); } ),
			jqDuree:jQuery('#date_fin').val(-1).attr('disabled','true').change( function() { document._arkianeg3.change(); } ), 
			jqDateDebut:jQuery('#date_debut').val(-1).change( function() { 
				var that = document._arkianeg3;
				that.jqDateFin.html('');
				jQuery('option:lt(' + (that.jqDateDebut[0].selectedIndex + (that.jqNbsem.length == 0 ? 4 : parseInt(that.jqNbsem.val()) )) + '):gt(' + (that.jqDateDebut[0].selectedIndex-1) + ')', that.jqDateFinModele).each( function() { that.jqDateFin.append(jQuery(this).clone()); } );
				that.jqDateFin[0].disabled = false;
				document._arkianeg3.change(); 
			}), 
			jqDateFin:jQuery('#date_fin').val(-1).change( function()  {
				document._arkianeg3.change(); 
			} ), 
			jqDateFinModele:jQuery('#date_fin_modele'), 
			change:function() { 
				if( this.jqDateDebut.val() != '-1' && this.jqDateFin.val() != '-1' ) { 
					this.appel( this.jqPers ); 
					this.appel( this.jqSitu ); 
					this.appel( this.jqConf ); 
					this.appel( this.jqMultipass ); 
				}},
			appel:function(jqObj) { 
				if( jqObj.length > 0 )
				{
					if ( (comparedate(this.jqDateDebut.val(),'24/06/2011')==1) && (comparedate(this.jqDateFin.val(),'28/08/2011')==2) ){
						jQuery('#blocmultipass').show();
					} else{	
						if(jQuery('#multipass').length>0){
							this.jqMultipass[0].checked=false;
							jQuery('#blocmultipass').hide();
						}	
					}

					if( jqObj[0].name!='multipass' ){
						jQuery('.arkiane-liste-critere:has(#' + jqObj[0].id + ')').toggleClass('attente');
						jqObj[0].disabled  = true;
						jqObj[0]._old_val = jqObj.val();
						var px;

						/*local*/
//						var basesite="http://192.168.0.171/resavalloire/";
//						var basejson="http://192.168.0.171/proxy-jsonp/";
						
						/*prod*/
						var basesite="http://www.resavalloire.com/";
						var basejson="http://jsonp.open-system.fr/";

						px = encodeURIComponent(basesite+langue+'/moteur-arkiane.aspx?qry=' + jqObj.attr('qry') + '&id=' + jqObj[0].id + '&dd=' + this.jqDateDebut.val() + '&df=' + this.jqDateFin.val() + '&pe=' + (this.jqPers.length == 0 ? '-1' : this.jqPers.val() ) + '&c1=' + (this.jqSitu.length == 0 ? '-1' : this.jqSitu.val() ) + '&c2=' + (this.jqConf.length == 0 ? '-1' : this.jqConf.val()) + '&im=' + (this.jqIm.length == 0 ? '-1' : this.jqIm.val() ) + '&ct=' + (this.jqCt.length == 0 ? '': this.jqCt.val() ) + '&multipass=' + (this.jqMultipass.length == 0 ? '' : (this.jqMultipass[0].checked == 0 ? '' : this.jqMultipass.val() )));
//						else px = encodeURIComponent(basesite+'uk/moteur-arkiane.aspx?qry=' + jqObj.attr('qry') + '&id=' + jqObj[0].id + '&dd=' + this.jqDateDebut.val() + '&df=' + this.jqDateFin.val() + '&pe=' + (this.jqPers.length == 0 ? '-1' : this.jqPers.val() ) + '&c1=' + (this.jqSitu.length == 0 ? '-1' : this.jqSitu.val() ) + '&c2=' + (this.jqConf.length == 0 ? '-1' : this.jqConf.val()) + '&im=' + (this.jqIm.length == 0 ? '-1' : this.jqIm.val() ) + '&ct=' + (this.jqCt.length == 0 ? '': this.jqCt.val() ) + '&multipass=' + (this.jqMultipass[0].checked == 0 ? '' : this.jqMultipass.val() ) );
						jQuery.ajax( { url:basejson+"jsonp.aspx?xsl=1&px=" + px, dataType:"jsonp", success:function(data){ document._arkianeg3.callback(data); } } );
						
				
					} else {
						jqObj[0].disabled  = false;
					}
			
				}
			}, 
			callback:function( data ) {
				var html = "";
				for(i=0;i<data.LINES.length;i++)
					html += "<option value='" + data.LINES[i].crit_val + "'>" + data.LINES[i].crit_lib + " (" + data.LINES[i].crit_nb + ")</option>";
				jQuery('#'+data.id).html( html )[0].disabled = false;
				jQuery('#'+data.id).val( jQuery('#'+data.id)[0]._old_val );
				jQuery('.arkiane-liste-critere:has(#' + data.id + ')').toggleClass('attente');
				jQuery('#btrechercher').show();
			}
		}
}

function initwidgetprestataire(){		
		document._arkianeg3 = { 
			jqEnvoi:jQuery('#btrechercher').hide(),
			jqDuree:jQuery('#date_fin').val(-1).attr('disabled','true').change( function() { document._arkianeg3.change(); } ), 
			jqDateDebut:jQuery('#date_debut').val(-1).change( function() { 
				var that = document._arkianeg3;
				that.jqDateFin.html('');
				jQuery('option:lt(' + (that.jqDateDebut[0].selectedIndex + 4) + '):gt(' + (that.jqDateDebut[0].selectedIndex-1) + ')', that.jqDateFinModele).each( function() { that.jqDateFin.append(jQuery(this).clone()); } );
				that.jqDateFin[0].disabled = false;
				that.jqEnvoi.show();
			}), 
			jqDateFin:jQuery('#date_fin').val(-1), 
			jqDateFinModele:jQuery('#date_fin_modele')
		}
}
