// ===================================================================
// 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'] = 'Login non valido';
labels[2]['en'] = 'Login not valid';
labels[3] = new Array();
labels[3]['it'] = 'Torna indietro';
labels[3]['en'] = 'Back';

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

function submitLogin(idForm, labels, temp_language){
    
	$("#"+idForm).bind('submit', function(evt){
		
		$('#loginError').hide();
		$('#loginFormPageError').hide();
        
        $("#"+idForm+" *").each(function(index, el) {
            $('#'+idForm+' #span_'+el.id).removeClass('error');
        });
		
        var count_errors = 0;
		
		$("#"+idForm+" *").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).val() == '') {
                    $('#'+idForm+' #span_'+el.id).addClass('error');
                    count_errors++;
				}
			}
		});
		
        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/login.php',
				data: dataToSend,
				dataType: "html",
				complete: function (data){
					stopLoading(""+idForm+"");
					if (idForm == "loginFormPage" && data.responseText == "1")
					{
						location.href = '/'+temp_language+'/checkout/';
					}
					else if (idForm == "loginFormPage" && data.responseText == "2")
					{
						$('#loginFormPageError').show();
					}
					else if (data.responseText == "2")
					{
						$('#loginError').show();
					}
					else
					{
						$('#loginBox').slideUp();
						$("#area_operativa").load("/area_operativa.php");
					}
				}
			});
		}
		return false;
	});
}


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

function LogOut(languageId)
{
	$.ajax({
		type: "POST",
		beforeSend: '',
		url: '/ws/logout.php',
		data: '',
		dataType: "html",
		complete: function (data){
			location.href = '/'+languageId+'/';
			$("#area_operativa").load("/area_operativa.php");
		}
	});
}

function ResendPassword()
{
	$("#resendPasswordForm").bind("submit", function(evt){
		
	        $("#resendPasswordForm *").each(function(index, el) {
	            $('#resendPasswordForm #span_'+el.id).removeClass('error');
	            $('#resendPasswordForm #alert_'+el.id).hide();
	        });
			
	        var count_errors = 0;
			
			$("#resendPasswordForm *").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).val() == '') {
	                    $('#resendPasswordForm #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() == '' ) {
                        $('#resendPasswordForm #span_'+el.id).addClass('error');
                        $('#resendPasswordForm #alert_'+el.id).show();
                        count_errors++;
					}
				}
				
				
			});
			
	        if (count_errors > 0)
	        {
	            return false;
	        }
			else
	        {
				dataToSend = '';
	            numPar = 0;
				$("#resendPasswordForm *").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("resendPasswordForm"),
		  			url: '/ws/newpassword.php',
					data: dataToSend,
					dataType: "html",
					complete: function (data){
						stopLoading("resendPasswordForm");
						$("#resendPasswordForm").hide();
						if (data.responseText == "1")
							$("#resendPasswordResponse").show();
						else
							$("#resendPasswordResponseFailed").show();
					}
				});
			}
			return false;
    });
	
}


