// ===================================================================
// 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, temp_language){
	
	$("#"+idForm).bind('submit', function(evt){

        $("#"+idForm+" span").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.id.length > 0)
            {
                //alert(idForm + ' - elemento: ' + el + ' ' + el.id + ' ' + el.type + ' + css: ' + $(el).css("display") );
            	
				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 == 'select-one' && $(el).val() == '0')
						{
		                    $('#'+idForm+' #span_'+el.id).addClass('error');
		                    count_errors++;
						}
						else 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.id.length > 0)
					            {
									if (el.type == 'select-one' || el.type == 'password' || el.type == 'text' || el.type == 'hidden' || el.type == 'textarea' || (el.type == 'checkbox' && el.checked) || (el.type == 'radio' && el.checked)){
					    				if ($(el).attr('rel') != 'not_post')
					                    {
					                        if (numPar > 0) dataToSend += '&'
					    					dataToSend += $(el).attr('name') + '=' + $(el).val();
					                        numPar++;
					                    }
									}
					            }
							});
							
							$("#"+idForm+" *").each(function(index, el){
					            if (el.id.length > 0)
					            {
									if ((el.type == 'checkbox' && el.checked)){
					    				if ($(el).attr('rel') == 'newsletter')
										{
											$.ajax({
												type: "POST",
												url: '/ws/newsletter.php',
												data: dataToSend,
												dataType: "html",
												complete: function (data){}
											});
										}
									}
					            }
							});
							
							$.ajax({
					  			type: "POST",
								beforeSend: startLoading(""+idForm+""),
					  			url: '/ws/registration.php',
								data: dataToSend,
								dataType: "html",
								complete: function (data){
									stopLoading(""+idForm+"");
									if (data.responseText == "1")
									{
										
										
										//Login(email, password, temp_language, $("#targetCheckout").val());
										
										$.ajax({
											type: "POST",
											beforeSend: '',
											url: '/ws/login.php',
											data: "username="+email+"&password="+password,
											dataType: "html",
											complete: function (data){
												if ($("#targetCheckout").val() == "1")
												{
													location.href = '/'+temp_language+'/checkout/';
												}
												else
												{
													//$('#loginFormArea').slideUp();
													$('#personalData').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");
													}
													$.scrollTo(0, 800);
												}
												
											}
										});
										
										
										
									}
								}
							});
				        	
				        }
						
					}
					else
					{
						$.scrollTo(0, 800);
			            $('#'+idForm+' #span_email').addClass('error');
			            $('#'+idForm+' #alert2_email').show();
			            count_errors++;
					}
				}
			});
		}
		
        if (count_errors > 0)
        {
        	$.scrollTo(0, 800);
            return false;
        }
		return false;
	});
}


function Login_OLD(username, password, temp_language, checkout)
{
	$.ajax({
		type: "POST",
		beforeSend: '',
		url: '/ws/login.php',
		data: "username="+username+"&password="+password,
		dataType: "html",
		complete: function (data){
			if (checkout == "1")
			{
				location.href = '/'+temp_language+'/checkout/';
			}
			else
			{
				//$('#loginFormArea').slideUp();
				$('#personalData').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");
				}
			}
		}
	});
}

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+"Label").css("display") == "none")
		{
			$("."+prefix+"Label").show();
			$("."+prefix+"Label *").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+"Label").hide();
			$("."+prefix+"Label *").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();
					}
				}
			});
		}
	});
}

function WhoIs(UserType){
var LabelToHideA="companyNameInput";
var LabelToHideB="vatNumberInput";
var LabelToHideC="resellerInfo";
var LabelToHideAB="companyName";
var LabelToHideBB="vatNumber";
var LabelToHideDB="checkbox_newsletter_1";

if ( UserType == 'P' )
	{
		$("."+LabelToHideA).addClass("nodisplay");
		$("."+LabelToHideB).addClass("nodisplay");
		$("."+LabelToHideC).addClass("nodisplay");
		$("#"+LabelToHideAB).attr('rel', '');
		$("#"+LabelToHideBB).attr('rel', '');
		$("#"+LabelToHideDB).val("0015");
	}
else
	{
		$("."+LabelToHideA).removeClass("nodisplay");
		$("."+LabelToHideB).removeClass("nodisplay");
		$("."+LabelToHideC).removeClass("nodisplay");
		$("#"+LabelToHideAB).attr('rel', 'required');
		$("#"+LabelToHideBB).attr('rel', 'required');
		$("#"+LabelToHideDB).val("MAIKIIBOX");
	}
}

