// ===================================================================
// Login / Log out functions
// ===================================================================

labels = new Array();
labels[1] = new Array();
labels[1]['it'] = 'Benvenuto';
labels[1]['en'] = 'Welcome';
labels[2] = new Array();
labels[2]['it'] = '';
labels[2]['en'] = '';
labels[3] = new Array();
labels[3]['it'] = 'Torna indietro';
labels[3]['en'] = 'Back';

function resetRegistration(){
	$("#registrationForm").bind('reset', function(evt){
        var count_errors = 0;
		
        $("#registrationForm *").each(function(index, el) {
            $('#span_'+el.id).removeClass('error');
        });
    });
}

function submitRegistration(idForm, labels, temp_language){
    
	$("#"+idForm).bind('submit', function(evt){
        
        $("#"+idForm+" *").each(function(index, el) {
            $('#'+idForm+' #span_'+el.id).removeClass('error');
        });
        
        $('#'+idForm+' #alert_email').hide();
        $('#'+idForm+' #alert2_email').hide();
        $('#'+idForm+' #alert_password').hide();
        $('#'+idForm+' #alert2_password').hide();
        $('#'+idForm+' #alert_password_confirm').hide();
        $('#'+idForm+' #alert2_password_confirm').hide();
        
		
        var count_errors = 0;
		var email = ""; 
		var password = ""; 
		var passwordConfirm = "";
		
		$("#"+idForm+" *").each(function(index, el) {
			if ($(el).css("display") != "none")
			{
				if ( el.type == 'text' ||  el.type == 'password' || el.type == 'checkbox' || el.type == 'textarea' || el.type == 'select-one' ) {
					if ($(el).attr('rel') == 'required' && el.type == 'checkbox')
					{
						if ($(el).is(':checked') == false) {
		                    $('#'+idForm+' #span_'+el.id).addClass('error');
		                    count_errors++;
						}
					}
					else if ($(el).attr('rel') == 'required' && $(el).val() == '') {
	                    $('#'+idForm+' #span_'+el.id).addClass('error');
	                    count_errors++;
					}
				}
			}
			
			if ($(el).attr('rel') == 'required_email') {
				if ( !$(el).val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/) || $(el).val() == '' ) {
                    $('#'+idForm+' #span_'+el.id).addClass('error');
                    $('#'+idForm+' #alert_'+el.id).show();
                    count_errors++;
				}
				else
				{
					email = $(el).val();
				}
			}
			
			if ($(el).attr('rel') == 'required_password') {
				if ($(el).val() == '' ) {
                    $('#'+idForm+' #span_'+el.id).addClass('error');
                    $('#'+idForm+' #alert_'+el.id).show();
                    count_errors++;
				}
				else if ($(el).val().length < 6)
				{
                    $('#'+idForm+' #alert2_'+el.id).show();
                    count_errors++;
				}
				else
				{
					password = $(el).val();
				}
			}
			
			if ($(el).attr('rel') == 'required_password_confirm') {
				if ($(el).val() == '' ) {
                    $('#'+idForm+' #span_'+el.id).addClass('error');
                    $('#'+idForm+' #alert_'+el.id).show();
                    count_errors++;
				}
				else
				{
					passwordConfirm = $(el).val();
					if (passwordConfirm != password) {
		                $('#'+idForm+' #alert2_'+el.id).show();
		                count_errors++;
					}
				}
			}
			
		});
		
		if (email.length > 0)
		{
			$.ajax({
				type: "POST",
				beforeSend: '',
				url: '/ws/checkUsername.php',
				data: "username="+email,
				dataType: "html",
				complete: function (data){	
					var checkUsernameValid = data.responseText;
					if (checkUsernameValid == "1")
					{
				        if (count_errors > 0)
				        {
				            return false;
				        }
				        else
				        {

							dataToSend = '';
				            numPar = 0;
							$("#"+idForm+" *").each(function(index, el){
								if (el.type == 'select-one' || el.type == 'password' || el.type == 'text' || el.type == 'hidden' || el.type == 'textarea' || (el.type == 'checkbox' && el.checked)){
				    				if ($(el).attr('rel') != 'not_post')
				                    {
				                        if (numPar > 0) dataToSend += '&'
				    					dataToSend += $(el).attr('name') + '=' + $(el).val();
				                        numPar++;
				                    }
								}
							});
							
							$.ajax({
					  			type: "POST",
								beforeSend: startLoading(""+idForm+""),
					  			url: '/ws/registration.php',
								data: dataToSend,
								dataType: "html",
								complete: function (data){
									stopLoading(""+idForm+"");
									if (idForm == "registrationFormPage" && data.responseText == "1")
									{
										Login(email, password, temp_language);
									}
								}
							});
				        	
				        }
						
					}
					else
					{
			            $('#'+idForm+' #span_email').addClass('error');
			            $('#'+idForm+' #alert2_email').show();
			            count_errors++;
					}
				}
			});
		}
		
        if (count_errors > 0)
        {
            return false;
        }
		return false;
	});
}


function Login(username, password, temp_language)
{
	$.ajax({
		type: "POST",
		beforeSend: '',
		url: '/ws/login.php',
		data: "username="+username+"&password="+password,
		dataType: "html",
		complete: function (data){
			location.href = '/'+temp_language+'/checkout/';
		}
	});
}

function CheckUsername (username)
{
	var checkResponse = "0";
	
	$.ajax({
		type: "POST",
		beforeSend: '',
		url: '/ws/checkUsername.php',
		data: "username="+username,
		dataType: "html",
		complete: function (data){
			checkResponse = data.responseText;
			return checkResponse;
		}
	});
	
	return checkResponse;	
}

function reloadForm ()
{
    $('#responseRegistration_form').css('display', 'none');
	$('#registrationForm').css('display', 'block');
}

function CheckOtherAddress(prefix) {
	$("#"+prefix+"check").bind('click', function(evt){
		if ($("."+prefix+"Row").css("display") == "none")
		{
			$("."+prefix+"Row").show();
			$("."+prefix+"Row *").each(function(index, el) {
				if ( el.type == 'text' ||  el.type == 'password' || el.type == 'checkbox' || el.type == 'textarea' || el.type == 'select-one' ) {
					if ($(el).attr('rel') == 'required') {
						$(el).show();
					}
				}
			});
		}
		else
		{
			$("."+prefix+"Row").hide();
			$("."+prefix+"Row *").each(function(index, el) {
				if ( el.type == 'text' ||  el.type == 'password' || el.type == 'checkbox' || el.type == 'textarea' || el.type == 'select-one' ) {
					if ($(el).attr('rel') == 'required') {
						$(el).hide();
					}
				}
			});
		}
		
		
	});
}


