var GLOB = {
    set: function (namespace, keys) {
        if (!GLOB[namespace]) {
            GLOB[namespace] = keys;
        } else {
            for (key in keys) {
                GLOB[namespace][key] = keys[key];
            }
        }
    },
    get: function (namespace, key) {
        if (!GLOB[namespace]) {
            return;
        }
        if (!key) {
            return GLOB[namespace];
        }
        return GLOB[namespace][key];
    }
};

function autocheckform()
{
	$(this).find('.witherror').removeClass('witherror');
	$(this).find('.autocheck_error').remove();
	var nb_errors = 0;
	$(this).find('.jsnotempty').each(function(){
		if( $(this).val()==''  )
		{
			$(this).after(' <span class="autocheck_error"><span>Erreur !</span></span>');
			$(this).parents('dd').addClass('witherror');
			nb_errors++;
		}
	})
	$(this).find('.jsmustbeidem').each(function(){
		if( $(this).val()!=$('.jsmustbeidemAs').val()  )
		{
			$(this).after(' <span class="autocheck_error"><span>Erreur !</span></span>');
			$(this).parents('dd').addClass('witherror');
			nb_errors++;
		}
	})
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	$(this).find('.jscheckmail').each(function(){
		if( !filter.test( $(this).val())  )
		{
			$(this).after(' <span class="autocheck_error"><span>' + GLOB.get("mail_invalide") + '</span></span>');
			$(this).parents('dd').addClass('witherror');
			nb_errors++;
		}
	})
		var filter = /^([0-9]+)$/;
	$(this).find('.jscheckinteger:visible').each(function(){
		if( !filter.test($(this).val()) && $(this).val())
		{
			$(this).after(' <span class="autocheck_error"><span>' + GLOB.get("entier_invalide") + '</span></span>');
			$(this).parents('dd').addClass('witherror');
			nb_errors++;
		}
	})
	var filter = /^([0-9]+\.{0,1}[0-9]{0,2})$/;
	$(this).find('.jscheckamount:visible').each(function(){
		$(this).val($(this).val().replace(",","."));
		if( !filter.test($(this).val()) && $(this).val())
		{
			$(this).after(' <span class="autocheck_error"><span>' + GLOB.get("montant_invalide") + '</span></span>');
			$(this).parents('dd').addClass('witherror');
			nb_errors++;
		}
	})
	$(this).find('.jscheckdate:visible').each(function(){		
		var date = $(this).val().replace(/\//g,'-');			
		date = date.split('-');		
		if (!(date[2]>1900 && date[2]<2200 && date[1]<13 && date[1]>0 && date[0]>0 && date[0]<32  ))	{
			$(this).after(' <span class="autocheck_error"><span>' + GLOB.get("date_invalide") + '</span></span>');
			$(this).parents('dd').addClass('witherror');
			nb_errors++;
		}
	});
	if(nb_errors)
	{
		return false;
	}
}

function applyAutoEmpty(obj)
{
	$(obj)
	.blur(function(event){
		if(!$(this).attr('value'))
			{$(this).css('color','#999'); $(this).attr('value',$(this).attr('alt')); }
	})
   .focus(function(event){
		if($(this).attr('value')===$(this).attr('alt'))
			{ $(this).attr('value',''); $(this).css('color',''); }
	})
	.each(function(){
		if(!$(this).attr('value') || $(this).attr('value')===$(this).attr('alt') )
			{$(this).css('color','#999'); $(this).attr('value',$(this).attr('alt'));  }
	});
}

$(document).ready(function(){

	  $(".jsAutoempty").each(function(){ applyAutoEmpty(this) })
	  
	$('.jsnotempty').each(function(){
		$(this).siblings('label').append(' <span class="mandatory" title="' + GLOB.get("field_mandatory") + '"><span>oblig.</span></span>');
		$(this).parents('form').submit(autocheckform);		
	});
	
	$('.jsSubmit').live('click',function(){
		$(this).parents('form').submit();
		return false;
	});
	
	$('.jsClose').live('click',function(){
		$(this).parents('.boxcart').fadeOut(); return false;
	});
	
	$('.jsMoreless').each(function(){
		$(this).wrap('<span class="moreless"></span>').before('<a href="#" class="moreless_t">&nbsp;</a><a href="#" class="moreless_b">&nbsp;</a>');
		if(parseInt($(this).val(),10)>0)
		{
			$(this).parents('tr').addClass('green');
		}
	}).live('keydown',function(e) {
		if( (e.keyCode==38 || e.keyCode==40) && (parseInt($(this).val())=='NaN' || $(this).val()==''))
		{
			$(this).val('0');
		}
		if(e.keyCode==38)
		{
			$(this).val( parseInt($(this).val())+1).trigger('change');
			return false;
		}
		else if(e.keyCode==40)
		{
			if(parseInt($(this).val())>0)
			{
				$(this).val( parseInt($(this).val())-1).trigger('change');
			}
			return false;
		}
	})
	.live('change',function(){
		$(this).val( Math.max(0,parseInt($(this).val(),10)) );
		if( $(this).val()=='NaN'){ $(this).val('0'); }
		
		if(parseInt($(this).val(),10)>0)
		{
			$(this).parents('tr').addClass('green');
		}
		else
		{
			$(this).parents('tr').removeClass('green');
		}
		if( $(this).hasClass('jsMorelessSubmit'))
		{
			$(this).parents('form').submit();
		}
	})
	;
	
	$('.moreless_t').live('click',function(){
		$(this).blur().siblings('input').val( parseInt($(this).siblings('input').val(),10)+1 ).trigger('change');
		return false;
	})
	$('.moreless_b').live('click',function(){
		$(this).blur().siblings('input').val( Math.max(0,parseInt($(this).siblings('input').val(),10)-1) ).trigger('change');
		return false;
	})
	
	$('.jsRestoFilterSel a').live('click',function(){
		$('.jsRestoFilterContent li.withEmpty').remove();
		$(this).blur().parents('li').addClass('active').siblings('li').removeClass('active');
		if( $(this).attr('rel')=='all')
		{
			$('.jsRestoFilterContent li').show();
		}
		else
		{
			$('.jsRestoFilterContent li').hide();
			if( $('.jsRestoFilterContent li.type_'+$(this).attr('rel') ).show().length == 0)
			{
				$('.jsRestoFilterContent').append('<li class="withEmpty">Aucun restaurant dans cette liste.</li>');
			}
		}
		return false;
	});
	
	$('.jsMultiAdd').live('click',function(){
		
		$(this).siblings('ul').find('li:hidden').clone()
			.appendTo( $(this).siblings('ul') ).show()
			.find('input').each(function(){ applyAutoEmpty(this); }).end()
			.find('input:first').focus();
		
	})
	
	
	$('.jsShowFade').fadeIn();
	$('.jsHide').hide();
	$('.jsShow').show();
	
	$('.jsAutofocus').focus();
	
	$('.jsDatepicker').datepicker({dateFormat:'dd/mm/yy'});
});
