// ===================================================================
// 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, destination){
    
	$("#"+idForm).bind('submit', function(evt){
		
		$('#loginError').hide();
		$('#loginError2').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)
        {
		$('#' + idForm +' #loginError').show();
		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++;
                    }
				}
			});
			
	
			if (idForm != 'loginFormTicket')
			{
				loginCheck = $.ajax({
					type: "POST",
					url: '/ws/login.php',
					data: dataToSend,
					dataType: "html",
					async: false,
					complete: function (dataTmp){
						if (dataTmp.responseText != "1")
						{
							$('#loginError2').show();
						}
					}
				}).responseText;
			}
			else{
			loginCheck = $.ajax({
					type: "POST",
					url: '/ws/loginTicket.php',
					data: dataToSend,
					dataType: "html",
					async: false,
					complete: function (dataTmp){
						if (dataTmp.responseText != "1")
						{
							$('#loginError3').show();
						}
					}
				}).responseText;
			}
			
			if (loginCheck == "1")
			{
				return true;
			}
			else
			{
				return false;
			}
			
		}
	});
	return false;

}


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

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

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;
    });
	
}

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


