// JavaScript Document
function onSubmit()
{
	if(checkEmail("email","email non corretta"))
	{
		$.post("subscribe.php", { email: trim(document.getElementById("email").value) },
		function(data){
			if(data == '-1')
			{
				$(".error").css("visibility","hidden");
				$(".success").css("visibility","visible");
				$("input#email").focus(
					function(event)
					{
						$(".success").css("visibility","hidden");
						$("input#email").unbind('focus');
					}
				).keydown(
					function(event)
					{
						$(".success").css("visibility","hidden");
						$("input#email").unbind('keydown');
					}
				);
			} else {
				$(".success").css("visibility","hidden");
				$(".error").css("visibility","visible");
				$("input#email").focus(
					function(event)
					{
						$(".error").css("visibility","hidden");
						$("input#email").unbind('focus');
					}
				).keydown(
					function(event)
					{
						$(".error").css("visibility","hidden");
						$("input#email").unbind('focus');
					}
				);
			}
   		});
	} else {
//alert("email non corretta");
	}
}

function resetText(id, defaultText)
{
	document.getElementById(id).style.color = "#ACAEAF";
	if(trim(document.getElementById(id).value) == defaultText)
	{
		document.getElementById(id).value = "";
	}
}

/* Utility di validazione */

function email(value) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);	
}

function checkEmail(id, defaultValue) //vuole l'ID del campo
{
	var value = trim(document.getElementById(id).value);
	if( value == "" || value == defaultValue)
	{
		document.getElementById(id).value = defaultValue;
		document.getElementById(id).style.color = '#FF0000';
		return false;
	}else
	{
		if(!email(value))
		{
			document.getElementById(id).value = defaultValue;
			document.getElementById(id).style.color = '#FF0000';
			return false;
		}
	}
	return true;
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}
