function urlencode(str) {
    return escape(str.replace(/\//g, '\\') );
}

function updateResultSearch()
{
	var search_params = new Array();
	search_params.push( $('#searchMarque').val() );
	search_params.push( $('#searchPetrol').val() );
	search_params.push( $('#annee').val() );
	search_params.push( $('#km').val() );
	search_params.push( $('#price').val() );
	$.ez( 'ezjscautopolis::searchResult::' + search_params.join( '::' ), null, function( data ){
		$('#search_count').html( data.content );
	} );
}

function updateCatalogueSearch()
{
	var search_params = new Array();
	search_params.push( 80 );
	// Get selected types
	search_params.push( urlencode( $('input:checked[name="type[]"]').map(function() { return $(this).val(); }).get().join()  ));
	search_params.push( $('#carburant').val() );
	search_params.push( $('#place').val() );
	// Get Marque Type Filter 
	search_params.push( urlencode( $('input:checked[name="marque"]').map(function() { return $(this).val(); }).get().join()  ));
	search_params.push( urlencode( $('input:checked[name="car_brands[]"]').map(function() { return $(this).val(); }).get().join()  ));
	// Get PriceType Filter 
	search_params.push( urlencode( $('input:checked[name="search_by"]').map(function() { return $(this).val(); }).get().join()  ));
	search_params.push( $('#price').val() );
	search_params.push( $('#month').val() );
	$.ez( 'ezjscautopolis::catalogueSearch::' + search_params.join( '::' ), null, function( data ){
		$('#search_count').html( data.content );
	} );

}

function compareVehcles()
{
	var n = $( '#vehcles_rows input:checked' ).length;
	if( n >= 5 )
	{
		$( '#vehcles_rows input:not(:checked)' ).attr( 'disabled', 'disabled' );
	}
	else
	{
		$( '#vehcles_rows input:not(:checked)' ).removeAttr( 'disabled' );
	}
	$( '#vehcle_compare_length span' ).html( n );
}

function changeCompare( evt )
{
	var select = $( '#' + evt.currentTarget.id );
	var divs = select.parents( 'div' );
	$.ez( 'ezjscautopolis::compareVehicule::' + select.val(), null, function( data ) {
			var div = divs.get(0);
			div.innerHTML= data.content;
			updateCompareSelect();
			} );
}

function updateRecallForm()
{
	var select = $( '#planat_car' ).val();
	if( select != '' )
	{
		$.ez( 'ezjscautopolis::fetchVehicule::' + select, null, function( data ) {
			$( '#you_are' ).val( data.content.you_are );
			$( '#marque' ).val( data.content.marque );
			$( '#modele' ).val( data.content.modele );
			$( '#chassis' ).val( data.content.chassis );
			$( '#plaque' ).val ( data.content.plaque );
			$( '#km' ).val ( data.content.km );
			$( '#year' ).val ( data.content.year );
			$( '#circulation' ).val( data.content.circulation );
			$( '#remarque' ).innerHTML = data.content.remark;
		} );
	}
}

function updateCompareSelect()
{
	$( '#compare select' ).change( function( evt ) { changeCompare( evt ); } );
}

function searchAvailablesMeetings()
{
	var brand = $( '#marque' ).val();
	$.ez( 'ezjscautopolis::fetchDisabledDays::' + brand, null, function( data ) {
		availablesDays = data.content;
	} );
}

function haveMeetings( date ) {
	var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
	for(i=0; i < availablesDays.length; i++ ) {
		if( $.inArray( (m+1)  + '-' + d + '-' + y, availablesDays ) != -1 ) {
			return [true];
		}
	}
	return [false];
}

function noAvailablesDays( date )
{
	var noWeekend = jQuery.datepicker.noWeekends( date );
	return noWeekend[0] ? haveMeetings( date ) : noWeekend;
}

function noWeekEnd( date )
{
	var noWeekend = jQuery.datepicker.noWeekends( date );
	var current = new Date();
	return noWeekend[0] ? [ date>current ] : noWeekend;
}

$(window).load( function() {
	$( '#searchMarque' ).change( function() { updateResultSearch(); } );
	$( '#searchPetrol' ).change( function() { updateResultSearch(); } );
	$( '#vehcles_rows input:checkbox' ).change( function() { compareVehcles(); } );
	$( '#planat_car' ).change( function() { updateRecallForm(); } );
	$( '#MeetingFindButton' ).click( function() {
		searchAvailablesMeetings();
		return false;
	} );
	$( '#location_cars' ).attr( 'style', 'display:none;' );
	$( '#location_input input' ).click( function() {
		if( $( '#location' ).val() == 'yes' ) {
			$( '#location_cars' ).attr( 'style', 'display:block;' );
		} else {
			$( '#location_cars' ).attr( 'style', 'display:none;' );
		}
	});

	$( 'input[name="email"]').focus( function() {
		if( $(this).val() == 'Votre email' )
			$(this).val( '' );
	} );

$( '#redirect_url' ).change( function() {
			window.location = $(this).val();
	} );

} );

