$(document).ready(function(){

	$('input[name="cancel"]').click(function(){
		window.location.href = "/";
	});

	$('select[name="months"]').change(function(){
		$('input[name="total"]').val($('option:selected',this).val()*$('input[name="prix"]').val());
	});
	
	$('form[name="payingmail"]').submit(function(){
		if( $('input[name="email"]').attr('readonly') != true )
		{
			email 		= $('input[name="email"]').val();
			nom			= $('input[name="nom"]').val();
			prenom		= $('input[name="prenom"]').val();	
			adresse		= $('input[name="adresse"]').val()+" \n "+$('input[name="adresse2"]').val();
			telephone	= $('input[name="telephone"]').val();
			total		= $('input[name="total"]').val();
			months		= $('select[name="months"] option:selected').val();
			langue		= $('input[name="langue"]').val();
			
			
			if( verifmail(email) == false )
			{
				alert("Mail incorrect");
				return false;
			}
			
			if( email == "" || nom == "" || prenom == "" || adresse == "" || telephone == "" )
			{
				alert("Complétez les champs marqués d'un *");
				return false;
			}
			
			$.ajax({
				type: "POST",
				url: "/ajax/payingmail.php",
			   	data: {email: email, nom: nom, prenom: prenom, adresse: adresse, telephone: telephone, total: total, duree: months, langue: langue},
				success: function(msg){
					$('input').attr('readonly','readonly');
					$('select').attr('disabled','disabled');
					$('input[name="duree"]').val($('select[name="months"] option:selected').val());
					//alert(msg);
					$('div#payement').html(msg);
					
					
					//alert( "Data Saved: " + msg );
					
					load_actions()
				}
			});
		
			return false;
		}
		else 
		{
			dateexptmois	= $('select[name="dateexptmois"] option:selected').val();
			dateexptann 	= $('select[name="dateexptann"] option:selected').val();
			
			if( $('input[name="facturation"]:checked').val() == "credit" && ( $('input[name="titulairecarte"]').val() == "" || $('input[name="numerocarte"]').val() == "" || dateexptmois == 0 || dateexptann == 0 ) )
			{
				//alert('toto');
				alert("Complétez les champs marqués d'un *");
				return false;
			}
			//}
		}
	});
});

function verifmail( mailteste ){
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');

	if( reg.test(mailteste) ) {
		return(true);
	} else {
		return(false);
	}
}

function load_actions()
{
	$('input[name="facturation"]').click(function(){
		
		$('#payementpaypal').hide();
		$('input[name="envoi"]').show();
		$('#methodepayement').hide();
		
		if( $('input[name="facturation"]:checked').val() == "paypal" )
		{
			$('#payementpaypal').show();
			$('input[name="envoi"]').hide();
		}else if( $(this).val() == "credit" )
		{
			$('#methodepayement').show();
		}
	});
	
	$('form[name="payingmail"]').submit(function(){
		return true;
	});
}
