$(document).ready(function () {
	$(".datepicker").datepicker({ prevText: '', nextText: '', dateFormat: 'dd/mm/yy' });
	$('.requis').each(function(){
		$(this).filter('.checkbox2, .checkboxes').parent().parent().children('label').each(function(){
			texte = $(this).html() + ' <span style="color:#FF0000;">*</span>';
			$(this).html(texte);
		});
		$(this).not('.checkbox2, .checkboxes').parent().find('label').each(function(){
			texte = $(this).html() + ' <span style="color:#FF0000;">*</span>';
			$(this).html(texte);
		});
	});
	$('input[readonly=readonly]').css('background-color', '#EEEEEE').css('border', '0px').css('padding', '2px');
	$('form#AccountEditForm, form.controle').submit(function(){
		var autorise = true;
		
		$('.requis', this).each(function(){
			$(this).filter('select, input, textarea').css('background-color', '');
			
			$(this).filter('input[type=text], input[type=password]').each(function(){
				if($(this).val()=='') {
					autorise = false;
					$(this).css('background-color', '#FF9999');
				}
			});
			$(this).filter('select').each(function(){
				if($(this).val()==0) {
					autorise = false;
					$(this).css('background-color', '#FF9999');
					$('option', this).css('background-color', '#FF9999');
				}
			});
			$(this).filter('textarea').each(function(){
				if($(this).val()=='') {
					autorise = false;
					$(this).css('background-color', '#FF9999');
				}
			});
		});
		
		return autorise;
	});
	$('#btn_cancel').click(function(){
		history.back();
	});
	$('.numeric').numeric();
	
	$('.filterBtn').click(function(){
		$(this).parent().find('.filterChoose').show().position({
				of: $(this),
				my: 'right top',
				at: 'right bottom'
			}).expose({
				closeOnClick: true,
				closeOnEsc: false,
				onClose: function(event) {
					$('.filterChoose').hide();
				}
			});
	});
	$('.selectorPlageDate').change(function(){
		var selector_date = $(this);
		if($(this).val()=='other'){
			$(this).parent().find('.filterChoose').show().position({
				of: $(this),
				my: 'right top',
				at: 'right bottom'
			}).expose({
				closeOnClick: true,
				closeOnEsc: false,
				onClose: function(event) {
					$('.filterChoose').hide();
					from = selector_date.parent().find('.date_from');
					to = selector_date.parent().find('.date_to');
					selector_date.parent().find('.date_from, .date_to').datepicker("option","dateFormat","dd/mm/y")
					if(from.val()=='' || to.val()==''){
						selector_date.find('option[value=""]').attr('selected',true);
					}
					else{
						if(selector_date.find('option[value="'+from.val()+'-'+to.val()+'"]').length==0){
							selector_date.find('option:last').before('<option value="'+from.val()+'-'+to.val()+'">'+from.val()+'-'+to.val()+'</option>');
						}
						selector_date.find('option[value="'+from.val()+'-'+to.val()+'"]').attr('selected',true);
					}
					selector_date.parent().find('.date_from, .date_to').datepicker("option","dateFormat","dd/mm/yy");
				}
			});
			$(this).parent().find('.filterChoose .btnOk').click(function(){
				$.mask.close();
			});
			selector_date.parent().find('.date_from').datepicker('option','onSelect', function(dateText, inst) {
				date = $.datepicker.parseDate(
						inst.settings.dateFormat,
						dateText, inst.settings );
				selector_date.parent().find('.date_to').datepicker( "option", 'minDate', date );
			});
		}
	});
	$('.date_selector_quarter').buttonset();
	
	// Page index deals
	$('#deals_index').each(function(){
		$(".ui-checkboxes").buttonset();
		
		$(".filterChoose .btnOk").click(function(){
			$.mask.close();
			$(this).parents(".filterChoose").hide();
		});
		
		var funcCalculFilterChooseSelect = function(){
			nb_checked = $('input[type=checkbox]:checked',this).length;
			if(nb_checked == 0) $(this).parent().find('.filterBtn').val('All');
			if(nb_checked == 1) $(this).parent().find('.filterBtn').val($('input[type=checkbox]:checked',this).parent().find('.ui-button-text').text());
			if(nb_checked > 1) $(this).parent().find('.filterBtn').val('Filtered');
		};
		$('.filterChoose').each(funcCalculFilterChooseSelect);
		$('.filterChoose input[type=checkbox]').change(function(){
			$(this).parents('.filterChoose').each(funcCalculFilterChooseSelect);
		});
	});
});
function add_overlay(){
	$('body').append('<div class="ui-widget-overlay" style="z-index: 1001;"><\/div>');
	$('.ui-widget-overlay').css('width',$('body').width()).css('height',$('body').height());
}
function del_overlay(){
	$('.ui-widget-overlay').remove();
}
