$(document).ready(function() {
	
	$('#contactform').submit(function(){
	
		var action = site_url + $(this).attr('action');
		
		$("#message").slideUp(750,function() {
		$('#message').hide();


 		$('#submit')
			.after('<img src="'+site_url+'images/ajax-loader.gif" class="loader" />')
			.attr('disabled','disabled');
		
		$.post(action, { 
			nom: $('#nom').val(),
			raisonsociale: $('#raisonsociale').val(),
			email: $('#email').val(),
			telephone: $('#telephone').val(),
			sujet: $('#sujet').val(),
			demande: $('#demande').val()
		},
			function(data){
				$('#message').html(data);
				$('#message').slideDown('slow');
				$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
				$('#contactform #submit').attr('disabled',''); 
				if(data.match('bien') != null) $('#contactform').slideUp('slow');
				
			}
		);
		
		});
		
		return false; 
	
	});
	
	$('#frmEspaceClients').submit(function(){
		
		login = $.trim($('#loginEspaceClient').val());
		pass = $.trim($('#passEspaceClient').val());
		action = site_url + $(this).attr('action');

		if ( login !='' && pass != '' ) {
			$('#submit')
				.after('<img src="'+site_url+'images/ajax-loader.gif" class="loader" />')
				.attr('disabled','disabled');
			
			$.post(action, { 
				login: login,
				pass: pass
			},
				function(data){

					if(data.match('Bienvenu') != null) {
						$('#box-login-client').slideUp('slow');
						
						url = site_url + 'index.php/espace-clients/mes-expeditions';
						if(data.match('BienvenuColl') != null) url = site_url + 'index.php/espace-collaborateurs';
						
						window.location.href = url;
					} else {
						$('#message').html(data);
						$('#message').slideDown('slow');
						$('#frmEspaceClients img.loader').fadeOut('slow',function(){$(this).remove()});
						$('#frmEspaceClients #submit').attr('disabled',''); 
					}
				}
			);
		} else {
			alert('Login et Pass obligatoire');
		}
		
		return false; 
	
	});
		
});


function openPopUp(url,w,h) {
	var iLeft = (screen.width  - w) / 2 ;
	var iTop  = (screen.height - h) / 2 ;
	var sOptions = "toolbar=0,status=0,resizable=0,scrollbars=1" ;
		sOptions += ",width=" + w ; 
		sOptions += ",height=" + h ;
		sOptions += ",left=" + iLeft ;
		sOptions += ",top=" + iTop ;
		window.open( url, "POPup", sOptions ) ;
}

function verifAjoutGazoil(myForm) {
	l = myForm.length;
	e = 0;
	for (i=0; i<l; i++) {
		v = $.trim(myForm.elements[i].value);
		if (v == '') e ++;
	}
	if (e>1) {
		alert('Veuillez remplir tous les champs');
		return false;
	}
	return true;
}

function verifAjoutMajVidange(myForm) {
	l = myForm.length;
	e = 0;
	for (i=0; i<l; i++) {
		v = $.trim(myForm.elements[i].value);
		if (v == '') e ++;
	}
	if (e>1) {
		alert('Veuillez remplir tous les champs');
		return false;
	}
	return true;
}
function claculPrixUnit(myForm) {
	r = myForm.gsr_prix.value/myForm.gsr_quantite.value;
	$('#prix_unit').html('Prix unitaire : ' + r);
}
