﻿(function($)
{
	$.fn.customInputFile = function()
	{
		var $button = $(this).parent().find('a.sfogliaPicture');
		var $attachment = $(this).find("#allegato");
		var $this = $(this);
		var pos = $this.offset();
		var offset = {
			left: $("#allegato").width() - 25,  
			top: $("#allegato").height() / 2
		};
		
		$attachment.css('opacity','0');
		$attachment.css('outline','0 none');
		
		$this.mouseenter(function()
		{
			$button.css({backgroundPosition:'bottom left'});
		})
		.mouseleave(function()
		{
			$button.css({backgroundPosition:'top left'});
		})
		.mousemove(function(e)
		{
			$attachment.css(
			{
				marginTop: e.pageY - pos.top - offset.top,
				marginLeft: e.pageX - pos.left - offset.left
			});
			
		});		
	}
})(jQuery);


(function($){

	$.fn.customSelect = function(pParams)
	{
		
		var params = $.extend({
			durationOpen: 300,
			defaultOption: 'Seleziona posizione',
			maxVoice: 4,
			zIndex: 104
		}, pParams);
		
		var isOpen = false;
		var $select = $(this);
		var $wrapper = $('<div style="z-index:'+params.zIndex+';"><span class="customSelectText">' + params.defaultOption + '</span><span class="customSelectButton"></span></div>');
		var $options = $('<ul></ul>');
		var heightLink = 40;
		var heightOptions;
		
		var selectWidth = $select.width();
		$select.css({width: 0, height:0, visibility: 'hidden', position:'absolute', zIndex: 99});
		
		var $voices = $select.find('option');
		
		
		$voices.each(function()
		{
			var $link =	$('<a href="#' + $(this).val()+ '">' + $(this).html() + '</a>');
			var $this = $(this);
			$link.click(function()
			{	
				$(this).blur();
				$wrapper.find('.customSelectText').html($(this).html());
				hideOptions();
				$select.val($this.val());
				return false;
			});
			$link.appendTo($options).wrap('<li />');
		});
		heightOptions = $voices.length * heightLink;
		if(heightOptions > params.maxVoice * heightLink)
		{
			heightOptions = params.maxVoice * heightLink;
			$options.find('a').css({width: (selectWidth-23)});
		}else
		{
			$options.find('a').css({width: (selectWidth-6)});	
		}
		$options.css({height : heightOptions});
		$all = $('<div class="customSelect" style="z-index:'+params.zIndex+'"></div>');
		$wrapper.appendTo($all);
		$options.appendTo($all);
		$select.after($all);
		
		$wrapper.click(function()
		{
			if(isOpen)
				hideOptions();
			else
			{
				$options.animate({height: heightOptions}, params.durationOpen, function()
				{
					isOpen = true;
					$(document).bind('click',hideOptions);
				});
			}
		});
		
		function hideOptions()
		{
			$options.css({display: 'none', height: 0});
			isOpen = false;
			$(document).unbind('click', hideOptions);
		}
	}
})(jQuery);

var error = 0;
var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
function checkForm()
{
	var errorAnchor = "";
	var nome = document.signup.firstName.value;
	var cognome = document.signup.lastName.value;
	var email =  document.signup.email.value;
	
	var reemail = document.signup.reemail.value;
	var password = document.signup.password.value;
	var repassword =  document.signup.repassword.value;
	
	error = 0;
	
	if ((nome == "") || (nome == "undefined"))
	{
	   if (errorAnchor == "") errorAnchor = "nome";
	   $('#error_firstName').css('visibility','visible');
	   $('#firstName').parent().addClass('error');
	   error++;
	} else
	{
	   $('#error_firstName').css('visibility','hidden');
	   $('#firstName').parent().removeClass('error');
	}
	
	if ((cognome == "") || (cognome == "undefined"))
	{	
	   if (errorAnchor == "") errorAnchor = "cognome";
	   $('#error_lastName').css('visibility','visible');
	   $('#lastName').parent().addClass('error');
	   error++;
	}else
	{
	   $('#error_lastName').css('visibility','hidden');
	   $('#lastName').parent().removeClass('error');
	} 
	
	if (!email_reg_exp.test(email) || (email == "") || (email == "undefined"))
	{  
	   if (errorAnchor == "") errorAnchor = "email2";
	   $('#error_email').css('visibility','visible');
	   $('#email').parent().addClass('error');
	   if(!email_reg_exp.test(email) && (email != "") && (email != "undefined") ) $('#error_email').html(INVALID_EMAIL);
	   else $('#error_email').html(MANDATORY_FIELD);
	   error++;
	} else
	{
	   $('#error_email').css('visibility','hidden');
	   $('#email').parent().removeClass('error');
	}	
	
	if ((reemail == "") || (reemail == "undefined"))
	{
	   if (errorAnchor == "") errorAnchor = "remail";
	   $('#error_reemail').css('visibility','visible');
	   $('#reemail').parent().addClass('error');
	   error++;
	}else
	{
	   $('#error_reemail').css('visibility','hidden');
	   $('#reemail').parent().removeClass('error');
	} 
	
	if(((email != "") || (email != "undefined")) )
	{
		if(email != reemail)
		{
		   if (errorAnchor == "" || errorAnchor == "remail") errorAnchor = "email2";
		   $('#error_reemail').css('visibility','visible');
		   $('#reemail').parent().addClass('error');
		   $('#error_email').css('visibility','visible');
	  	   $('#email').parent().addClass('error');
		   $('#error_email').html(EMAILS_MISMATCH);
		   $('#error_reemail').html(EMAILS_MISMATCH);
		   error++;
		}		
	}
	
	/**
	 * 
	 **/
	if (email != "") {
		$.ajax({
			async: false,
			type: "POST",
			url: "/ws/Registration.cfc",
			data: {
				method: "DoesEmailExist",
				email: email
			},
			dataType: "json",
			cache: false,
			error: function(){
				alert("Attenzione si è verificato un errore di comunicazione.");
			},
			success: function(objData){
				if (!objData.SUCCESS) {
					alert("Attenzione si è verificato un errore remoto.\n" + objData.DATA);
				}
				else if (objData.DATA.EXISTS) {
					if (errorAnchor == "") errorAnchor = "email";
					$("#error_email").css("visibility", "visible");
					$("#email").parent().addClass('error');
					$("#error_email").html(EMAIL_EXISTS);
					error++;
				}
			}
		});
	}
	/**
	 * 
	 **/
	
	if ((password == "") || (password == "undefined"))
	{
	   if (errorAnchor == "") errorAnchor = "password2";
	   $('#error_password').css('visibility','visible');
	   $('#password').parent().addClass('error');
	   error++;
	} else
	{
	   $('#error_password').css('visibility','hidden');
	   $('#password').parent().removeClass('error');
	}
	
	if ((repassword == "") || (repassword == "undefined"))
	{	   
	   if (errorAnchor == "") errorAnchor = "rpassword";
	   $('#error_repassword').css('visibility','visible');
	   $('#repassword').parent().addClass('error');
	   error++;
	} else
	{
	  $('#error_repassword').css('visibility','hidden');
	  $('#repassword').parent().removeClass('error');
	}
	
	
	if(((password != "") || (password != "undefined")) )
	{
		if(password != repassword)
		{
		   if (errorAnchor == "" || errorAnchor == "rpassword") errorAnchor = "password2";
		   $('#error_repassword').css('visibility','visible');
		   $('#repassword').parent().addClass('error');
		   $('#error_password').css('visibility','visible');
	  	   $('#password').parent().addClass('error');
		   $('#error_password').html(PASSWORDS_MISMATCH);
		   $('#error_repassword').html(PASSWORDS_MISMATCH);
		   error++;
		}		
	}

	if($('#privacy').attr("checked") != true)
	{
		if (errorAnchor == "") errorAnchor = "priv";
		$('#error_privacy').css('visibility','visible');
		error++;	
	}else
	{
	$('#error_privacy').css('visibility','hidden');	
	}
	
	
	if (error == 0)
	{
		 document.signup.submit();
	}
	
	if (errorAnchor != "")
	{
		var href = document.location.href;
		var idx;
		
		if ((idx = href.lastIndexOf("#")) >= 0)
		{
			href = href.substring(0, idx);
		}
		
		document.location.href = href + "#" + errorAnchor;
	}
}

function resetTemporaryRegistration()
{
	$("#country option:eq(0)").attr('selected', 'selected');
	$("#paymentmethod option:eq(0)").attr('selected', 'selected');
	$("#shipment option:eq(0)").attr('selected', 'selected');
	$("#nome").val('');
	$("#cognome").val('');
	$("#indirizzo").val('');
	$("#cap").val('');
	$("#citta").val('');
	$("#email").val('');
	$("#telefono").val('');
	$("#note").val('');
	$("#provincia option:eq(0)").attr('selected', 'selected');
	
	$("#ragionesociale").val('');
	$("#indirizzofatt").val('');
	$("#capfatt").val('');
	$("#cittafatt").val('');
	$("#telefonofatt").val('');
	$("#faxfatt").val('');
	$("#emailfatt").val('');
	$("#pivafatt").val('');
	$("#countryfatt option:eq(0)").attr('selected', 'selected');
	$("#provinciafatt option:eq(0)").attr('selected', 'selected');
	
	$("#nomesped").val('');
	$("#cognomesped").val('');
	$("#aziendasped").val('');
	$("#countrysped option:eq(0)").attr('selected', 'selected');
	$("#provinciasped option:eq(0)").attr('selected', 'selected');
	$("#indirizzosped").val('');	
	$("#capsped").val('');	
	$("#cittasped").val('');	
	$("#notesped").val('');	
}

function checkTemporaryRegistration()
{
	error = 0;
	
	checkText('nome');
	checkText('cognome');
	checkText('indirizzo');
	checkText('citta');
	checkNumber('cap');
	checkEmail('email');
	
	if(jQuery("#fatturaazienda").val() == 1)
	{
		checkText('ragionesociale');
		checkText('indirizzofatt');
		checkText('pivafatt');
		checkText('cittafatt');
		checkNumber('capfatt');
		checkEmail('emailfatt');
	}
	
	if(jQuery("#cambiainidirizzo").val() == 1)
	{
		checkText('nomesped');
		checkText('cognomesped');
		checkText('indirizzosped');
		checkText('cittasped');
		checkNumber('capsped');
	}
	
	//if(error == 0) jQuery("#registrazioneTemporaneaForm").submit();
}

function checkText(id)
{
	var value = jQuery.trim(jQuery("#"+id).val());
	if ((value == ""))
	{	
	   $('#error_'+id).css('visibility','visible');
	   $('#'+id).parent().addClass('error');
	   error++;
	}else
	{
	   $('#error_'+id).css('visibility','hidden');
	   $('#'+id).parent().removeClass('error');
	} 
}

function checkNumber(id)
{
	var value = jQuery.trim(jQuery("#"+id).val());
	if ((value == "") || isNaN(value))
	{	
	   if(isNaN(value)) $('#error_'+id).html(INVALID_NUMBER);
	   else $('#error_'+id).html(MANDATORY_FIELD);
	   $('#error_'+id).css('visibility','visible');
	   $('#'+id).parent().addClass('error');
	   error++;
	}else
	{
	   $('#error_'+id).css('visibility','hidden');
	   $('#'+id).parent().removeClass('error');
	} 
}

function checkEmail(id)
{
	var value = jQuery.trim(jQuery("#"+id).val());
	if (!email_reg_exp.test(value) || (value == "") || (value == "undefined"))
	{  
	   $('#error_'+id).css('visibility','visible');
	   $('#'+id).parent().addClass('error');
	   if(!email_reg_exp.test(value) && (value != "") && (value != "undefined") )
	   {
	   	 $('#error_'+id).html(INVALID_EMAIL);
	   }
	   else
	   {
		 $('#error_'+id).html(MANDATORY_FIELD);
	   }
	   error++;
	} else
	{
	   $('#error_email').css('visibility','hidden');
	   $('#'+id).parent().removeClass('error');
	}	
}

function checkSelect(id)
{
	var value = jQuery("#"+id).val();
	if(value == -1)
	{
		error = error + 1;	
	}
}

function resetText(id)
{
	 if($('#error_'+id).css('visibility') == "visible")
	 {
		 $('#error_'+id).css('visibility','hidden');
		 $('#'+id).parent().removeClass('error');
	 }
}
