$(document).ready(function(){

	//Chargement du menu Accordion
	$("#Menu").accordion({autoHeight: false, collapsible: true, active: 0}); 
		
	//Auto Scroll
	$(window).scroll(function() { 
		var offset = $(document).scrollTop();
		$("#floatMenu").css('margin-top',offset);
	});
	
	//Autocomplete sur le champs NAF
	$('#formR-naf').autocomplete({
		minLength:3,
		source: function(request, response) {
			$.getJSON('./pages/nafajax.php', { q: request.term },
				function(data) { response(data); }
			);
		}
    });
	
	//Champs de recherche
	$('input[name=formR[siret]]').focus();
	$('form[name=form_recherche]').submit(function(){
		var reg = /[a-z]{3}/i;
		var siret = $('input[name=formR[siret]]').val();
		if(siret.match(reg)){
			$('input[name=formR[raisonSociale]]').attr('value',siret);
			$('input[name=formR[siret]]').attr('value','');
		}
		return true;
	});
	$('input.button').button();
	$('input.button:reset').click(function(){
		$('input:text','form[name=form_recherche]').val('');
		return false;
	});

	//Boite de dialog surveillance
	$('.dialogsurv').live('click', function(){
		var title = $(this).attr('title');
		var href = $(this).attr('href')+'&dialog=1';
		var dialogOpts = {
			bgiframe: true,
			title: title,
			width: 500,
			height: 350,
			modal: true,
			open: function(event, ui) {
				$(this).html('Chargement...');
				$(this).load(href);
				$('#dialogsurv').keypress(function(e){
					if (e.keyCode == 13){
						e.preventDefault();
						survSubmit();
					}
				});
			},
			buttons: {
				Ok: function() { survSubmit(); },
				Annuler: function() { $(this).dialog('close'); }
			},
			close: function() { $('#dialogsurv').remove(); }
		};
		$('<div id="dialogsurv"></div>').dialog(dialogOpts);
		return false;
	});	
	
	function survSubmit(){
		var buttons = $('#dialogsurv').dialog('option','buttons');
		for( var button in buttons ){
			$(":button:contains('"+button+"')").attr('disabled','disabled');
		}
		var formAction = $('#frmSurv').attr('action');
		var href = $(location).attr('href'); 
		var serData = $('#frmSurv').serialize();
		$('#frmSurv').replaceWith('<div id="frmSurv">Mise sous surveillance en cours...</div>');	
		$.post(formAction, serData, function(data) {
			if(data==''){
				$('#frmSurv').html("L'entité a été placé sous surveillance.\n");
				$(location).attr('href', href);
			}else{
				$('#frmSurv').html(data);
			}
		});
	}
	
	$('a[idobjet]').click(function()
	{
		idObject=$(this).attr('idobjet');
		$('#document'+idObject).html('Construction du document en cours...');
 		$.ajax({
  			type: "POST",
  			url: "/",
  			data: "page=getmarques&idObject="+idObject,
  			success: function(msg){ $('#document'+idObject).html(msg); },
 			error: function(){ $('#document'+idObject).html('Erreur'); }
 		});
	});
	
	//Tooltip date de maj
	$('.datemaj').qtip({
		content: $(this).attr('alt'),
		style: { name: 'cream', width: 170 },
		position: { 
			corner: { target: 'bottomMiddle', 
			tooltip: 'topMiddle' } }
	});
		
	//Tooltip de surveillance
	$('u[tooltip]').each(function(){
		$(this).qtip({
			content: $(this).attr('tooltip'),
			style: { name: 'dark' },
			position: { 
				corner: { target: 'topMiddle', 
				tooltip: 'bottomMiddle' } }
		});
	});
});


/*
 * Vérifie l'adresse email fournit en paramètre
 */
function checkEmail(eMail){
	var isMail = eMail.length;
	var verif = /^[a-z0-9_.-]+@[a-z0-9_.-]+[.][a-z]{2,4}$/i;
	if(!isMail || verif.exec(eMail)==null){
		return false;
	} else { 
		return true; 
	}	
}