// Vérification de l'enregistrement

function Ajax_AccountProfile_UpdateCancel() 
{
	$.ajax
	({
		type: 'POST',
		url: "/ajax/AccountProfile_UpdateCancel.php",		
		dataType: 'html',

		success: function (reponse) 
		{    
    		$("div#upgrade").hide();
    		$("div#upgrade").html("");

    		$("#free input").attr("disabled",accountProfileUpdateButtonFree);
    		$("#small input").attr("disabled",accountProfileUpdateButtonSmall);
    		$("#medium input").attr("disabled",accountProfileUpdateButtonMedium);
    		$("#large input").attr("disabled",accountProfileUpdateButtonLarge);
    		$("#extralarge input").attr("disabled",accountProfileUpdateButtonExtraLarge);
		},

		error: function () {
			// alert('error');
		}

	});// End Ajax
}

function Ajax_AccountProfile_UpdateConfirm(pendingSubscription,pendingAmountPaid) 
{
	$.ajax
	({
		type: 'POST',
		url: "/ajax/AccountProfile_UpdateConfirm.php",		
		data: "pendingSubscription="+pendingSubscription+"&pendingAmountPaid="+pendingAmountPaid,
		
		dataType: 'json',

		success: function (reponse) 
		{    
			// send to paypal => validate specific form
		    $("form#paypalForm input[name='custom']").val(reponse.custom);
		    $('form#paypalForm input[name=item_name]').val(reponse.itemName);
		    $('form#paypalForm input[name=amount]').val(reponse.amount);
		    $('form#paypalForm input[name=quantity]').val(reponse.quantity);
		    $('form#paypalForm input[name=invoice]').val(reponse.custom);
		    $('form#paypalForm input[name=return]').val(reponse.returnSuccess);
		    $('form#paypalForm input[name=cancel_return]').val(reponse.returnCancel);
		    $('form#paypalForm input[name=notify_url]').val(reponse.notifyUrl);
		    $('form#paypalForm input[name=first_name]').val(reponse.firstName);
		    $('form#paypalForm input[name=last_name]').val(reponse.lastName);

		    $('form#paypalForm').submit();
		},

		error: function () {
			 alert('error');
		}

	});// End Ajax
}

function Ajax_AccountProfile_UpdateOffer(pendingSubscription) 
{
	$.ajax
	({
		type: 'POST',
		url: "/ajax/AccountProfile_UpdateOffer.php",		
		data: "pendingSubscription="+pendingSubscription,
		
		dataType: 'html',

		success: function (reponse) 
		{
    		if (reponse != "")
    		{
    			
    			A_reponse = reponse.split("[#]");
    			
	    		$('div#upgrade').html(A_reponse[0]);
	    		$('div#upgrade').show();
	
			    $("input#upgradeCancel").click(
			    	function()
			    	{
			    		Ajax_AccountProfile_UpdateCancel();
			    	}
			    );
			    
			    $("input#upgradeConfirm").click(
			    	function()
			    	{
			    		Ajax_AccountProfile_UpdateConfirm(A_reponse[1],A_reponse[2]);
			    	}
			    );

				accountProfileUpdateButtonFree = $('#free input').attr('disabled');
				accountProfileUpdateButtonSmall = $('#small input').attr('disabled');
				accountProfileUpdateButtonMedium = $('#medium input').attr('disabled');
				accountProfileUpdateButtonLarge = $('#large input').attr('disabled');
				accountProfileUpdateButtonExtraLarge = $('#extralarge input').attr('disabled');
				
				if (A_reponse[0] != "")
    			{
					$("div.offer input").attr("disabled","disabled");
				}
    		}
		},

		error: function () {
			// alert('error');
		}

	});// End Ajax
}

function Ajax_AccountProfile() 
{
	
	// recuperation des champs du formulaire
	var firstname = $('#firstname').val();
	var lastname = $('#lastname').val();
	var gender = $('[name=gender]:checked').val();
	var emailaddress = $('#emailaddress').val();
	var country = $('#country option:selected').attr('id');
	var month = $('#month option:selected').attr('id');
	var day = $('#day option:selected').attr('id');
	var year = $('#year option:selected').attr('id');
	var sex = "";//$('#sex option:selected').attr('id');
	var language = $('#language option:selected').attr('id');
	var username = $('#username').val();
	var password = $('#password').val();
	var passwordconfirmation = $('#passwordconfirmation').val();
	
	$.ajax
	({
		type: 'POST',
		url: "/ajax/AccountProfile.php",		
		data: "firstname="+firstname+"&gender="+gender+"&emailaddress="+emailaddress+"&country="+country+"&lastname="+lastname+"&month="+month+"&day="+day+"&year="+year+"&language="+language+"&username="+username+"&password="+password+"&passwordconfirmation="+passwordconfirmation,
		
		dataType: 'json',

		success: function (reponse) 
		{    		
			$("#personnal ul li").remove();
			$("#account ul li").remove();
			
			if (reponse.sessionTest == "false")
    		{
    			document.location.href = "";
    		}
			else if (reponse.state=="success")
			{
				// cache le formulaire
				$("div.section").hide();
				// renome le titre de la page
				$("div.form div.title").html(reponse.confirmTitle);
				// affiche le message de confirmation
				$("div.form div.confirm").html(reponse.confirmMessage);
				// changement de l'identification (affichage déconnexion)
				$('div.navigation > div.identification > div.feedback').html(reponse.identificationMessage);
				
			}
			else
			{
				// PERSONNAL
				
				if (reponse.alertFirstname!="")
				{
					var TalertFirstname = reponse.alertFirstname.split('[#]');
					
					$('#personnal ul').append('<li class="'+TalertFirstname[1]+'">'+TalertFirstname[0]+'</li>');
				}
				
				if (reponse.alertLastname!="")
				{
					var TalertLastname = reponse.alertLastname.split('[#]');
					
					$('#personnal ul').append('<li class="'+TalertLastname[1]+'">'+TalertLastname[0]+'</li>');
				}
				
				if (reponse.alertGender!="")
				{
					var TalertGender = reponse.alertGender.split('[#]');
					
					$('#personnal ul').append('<li class="'+TalertGender[1]+'">'+TalertGender[0]+'</li>');
				}
				
				if (reponse.alertMail!="")
				{
					var TalertMail = reponse.alertMail.split('[#]');
					
					$('#personnal ul').append('<li class="'+TalertMail[1]+'">'+TalertMail[0]+'</li>');
				}
				
				if (reponse.alertBirthDate!="")
				{
					var TalertBirthDate = reponse.alertBirthDate.split('[#]');
					
					$('#personnal ul').append('<li class="'+TalertBirthDate[1]+'">'+TalertBirthDate[0]+'</li>');
				}
				
				// ACCOUNT
				
				if (reponse.alertUsername!="")
				{
					var TalertUsername = reponse.alertUsername.split('[#]');
					
					$('#account ul').append('<li class="'+TalertUsername[1]+'">'+TalertUsername[0]+'</li>');
				}
				
				if (reponse.alertPassword!="")
				{
					var TalertPassword = reponse.alertPassword.split('[#]');
					
					$('#account ul').append('<li class="'+TalertPassword[1]+'">'+TalertPassword[0]+'</li>');
				}
				
				if (reponse.alertPasswordConfirm!="")
				{
					var TalertPasswordConfirm = reponse.alertPasswordConfirm.split('[#]');
					
					$('#account ul').append('<li class="'+TalertPasswordConfirm[1]+'">'+TalertPasswordConfirm[0]+'</li>');
				}
				
				if (reponse.alertPasswordValid!="")
				{
					var TalertPasswordValid = reponse.alertPasswordValid.split('[#]');
					
					$('#account ul').append('<li class="'+TalertPasswordValid[1]+'">'+TalertPasswordValid[0]+'</li>');
				}
								
			}
		},

		error: function () {
			// alert('error');
		}

	});// End Ajax

}

