// ===================================================================
// 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 showLoginCheckout ()
{
	if ($('#loginFormContent').load('/loginCheckout.php')) { showLogin(); }
}

function UpdateVat()
{
    $.ajax({
        type: "POST",
        url: "/ws/cartUpdateItem.php",
        data: "cmd=updateVat",
        success: function(data) {
      		data = data.split("|");
      		if (data[1].length > 0)
      			$("#cartTotal").html(data[1]);
      		if (data[0].length > 0)
      			$("#cartTotalVat").html(data[0]);
          }
      });
}

function submitLogin(idForm, checkout) {
	
	$("#"+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 (checkout == "1" && data.responseText == "1")
					{
						$('#loginFormArea').slideUp();
						$("#loginMenu").load("/loginMenu.php");
						$("#loginFormArea").load("/login.php");
						if (BrowserName() != "Internet Explorer") {
							$('#plus_white').rotate(45);
						}
						else
						{
							$('#plus_white').attr("src", "/images/plus_white.png");
						}
						UpdateVat();
						ViewCartBoxCheckout();
						$("#customerData").load("/customerData.php");
					}
					else if (data.responseText == "2")
					{
						if (checkout == "1")
							$('#loginError_checkout').show();
						else
							$('#loginError').show();
					}
					else
					{
						$('#loginFormArea').slideUp();
						$("#loginMenu").load("/loginMenu.php");
						$("#loginFormArea").load("/login.php");
						$("#cartUserTypeReference").val("C");
						if (BrowserName() != "Internet Explorer") {
							$('#plus_white').rotate(45);
						}
						else
						{
							$('#plus_white').attr("src", "/images/plus_white.png");
						}
						UpdateVat();
						$("#customerData").load("/customerData.php");
						pL1 = $.url.param("pL1");
						if ( pL1 == 'product' )
						 {
							$('#content_main').load( '/product.php?pL1=product&productGroupId=' + $.url.param("productGroupId") + '&page=' + $.url.param("page") );
						 }
						$('#menuProductsList').load( '/productsList.php?pL1=' + pL1 );
						$('#cartPage').load( '/cart.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+'/';
		}
	});
}

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


