$(document).ready(function(){
	setEventsPageRegister();
});

function setEventsPageRegister(){
	$('#btnRegister').click(function(){
		register();
	});
	
	$('#optCountry').change(function(){
		$.ajax({
			url: "gestion/ajax/register/loadListProvinces.php",
			data: ({
				countryId:$(this).val()
			}),
			async: false,
			type: "POST",
			success: function(data){
				if(data != ''){
					$('#optProvince').html(data);
					$('#provincesContainer').show();
				}
				else{
					$('#optProvince').html(data);
					$('#provincesContainer').hide();
				}
			}
		});
	}).change();
}

function register(){
	if(validateFormRegister()){
		var username = $('#txtUsername').val();
		var password = $('#txtPassword').val();
		var firstname = $('#txtFirstname').val();
		var lastname = $('#txtLastname').val();
		var email = $('#txtEmail').val();
		var company = $('#txtCompany').val();
		var address = $('#txtAddress').val();
		var countryId = $('#optCountry').val();
		var provinceId = $('#optProvince').val();
		var city = $('#txtCity').val();
		var postalCode = $('#txtPostalCode').val();
		var telephone = $('#txtTelephone').val();
		var telephoneExt = $('#txtTelephoneExt').val();
		var telephone2 = $('#txtTelephone2').val();
		var telephone2Ext = $('#txtTelephone2Ext').val();
		
		$.ajax({
			url: "gestion/ajax/register/register.php",
			data: ({
				username:username,
				password:password,
				firstname:firstname,
				lastname:lastname,
				email:email,
				company:company,
				address:address,
				countryId:countryId,
				provinceId:provinceId,
				city:city,
				postalCode:postalCode,
				telephone:telephone,
				telephoneExt:telephoneExt,
				telephone2:telephone2,
				telephone2Ext:telephone2Ext,
			}),
			async: false,
			type: "POST",
			success: function(data){
				$('.message').hide();
				var infos = data.split('|');
				switch(parseInt(infos[0])){
					case 0:
						$('#messageSuccess').show();
						break;
					case 1:
						$('#messageError').show();
						break;
					case 2:
						$('#errorExistUsername').show();
						break;	
					case 3:
						$('#errorExistEmail').show();
						break;	
				}
			}
		});
	}
}

function validateFormRegister(){
	var username = $('#txtUsername').val();
	var password = $('#txtPassword').val();
	var firstname = $('#txtFirstname').val();
	var lastname = $('#txtLastname').val();
	var email = $('#txtEmail').val();
	var countryId = $('#optCountry').val();
	var provinceId = $('#optProvince').val();
	var valid = true;
	
	$('.message').hide();
	
	if(username==''){
		valid = false;
		$('#errorEmptyUsername').show();
	}
	
	if(password==''){
		valid = false;
		$('#errorEmptyPassword').show();
	}
	
	if(firstname==''){
		valid = false;
		$('#errorEmptyFirstname').show();
	}
	
	if(lastname==''){
		valid = false;
		$('#errorEmptyLastname').show();
	}
	
	if(email==''){
		valid = false;
		$('#errorEmptyEmail').show();
	}
	else if(!isEmail(email)){
		valid = false;
		$('#errorNotEmail').show();
	}
	
	if(countryId == 0){
		valid = false;
		$('#errorEmptyCountry').show();
	}
	
	if($('#optProvince option').length > 0 && provinceId == 0){
		valid = false;
		$('#errorEmptyProvince').show();
	}
	
	if(valid == false){
		$('#messageError').show();
	}

	return valid;
}
