
var DIGITS="0123456789";
var ALPHABETICS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var ALPHANUMERICS = ALPHABETICS + DIGITS;
var MAIL = "@.-_";
var WHITESPACE = " \t\n\r";
var SPECIAL = "אטילעש'\"!?()/.,;-";
var ALL = ALPHANUMERICS + WHITESPACE + SPECIAL;

var LBL_NOME="Nome";
var LBL_COGNOME="Cognome";
var LBL_INDIRIZZO="Indirizzo";
var LBL_CITTA="Citta'";
var LBL_MAIL="Email";
var LBL_TELEFONO="Telefono";

var LBL_INVALIDCHARSPREFIX="Il campo";
var LBL_INVALIDCHARSSUFFIX="contiene caratteri non validi";
var LBL_EMPTYPREFIX="Il campo";
var LBL_EMPTYSUFFIX="e' obbligatorio";
var LBL_WRONGFORMATPREFIX="Il formato del campo";
var LBL_WRONGFORMATSUFFIX="e' errato";

function checkString(theString,universe)
{	
	var currentChar;
	for(i=0;i<theString.length;i++)
	{
		currentChar=theString.charAt(i);
		if (universe.indexOf(currentChar)==-1) 
		{					
			return false;
		}
	}
	return true;
}

function checkNoMailId(theString)
{
	var index = theString.indexOf("@");
	var id = "";
	if (index>0)
	{
		id= theString.substring(0, index);
		if (id.length>0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
			return true;
	}
}

function isEmptyForMail(theString)
{	
	if (theString==null) return true;
	if (theString.length<1) return true;
	if (theString=="") return true;
	if (checkString(theString,WHITESPACE)) return true;
	if (checkNoMailId(theString)) return true;
	return false;
}

function isEmpty(theString){
	if (theString==null) return true;
	if (theString.length<1) return true;
	if (theString=="") return true;
	if (checkString(theString,WHITESPACE)) return true;
	return false;
}

function contains(theString,universe)
{
	var currentChar;
	for(i=0;i<universe.length;i++)
	{
		currentChar=universe.charAt(i);
		if (theString.indexOf(currentChar)==-1) return false;
	}
	return true;
}

function warn(fieldName,mPrefix,mSuffix)
{
	alert (mPrefix + " " + fieldName + " " + mSuffix);	
	return false;

}

function checkNome(theObj,option)
{	
	var theString = theObj.value;	
	if(option=='1'){		
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_NOME,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
		else
		{			
			return warn(LBL_NOME,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
		}
	}
	else
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_NOME,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
	}
	return true;
}

function checkCognome(theObj,option)
{
	var theString = theObj.value;	
	if(option=='1'){
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_COGNOME,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
		else
		{
			return warn(LBL_COGNOME,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
		}
	}
	else
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_COGNOME,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
	}
	return true;
}

function checkIndirizzo(theObj,option)
{
	var theString = theObj.value;
	if(option=='1'){
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_INDIRIZZO,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
		else
		{
			return warn(LBL_INDIRIZZO,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
		}
	}
	else
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_INDIRIZZO,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
	}
	return true;
}

function checkCitta(theObj,option)
{
	var theString = theObj.value;
	if(option=='1')
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_CITTA,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
		else
		{
			return warn(LBL_CITTA,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
		}
	}
	else
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,ALL)) return warn(LBL_CITTA,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
	}
	return true;
}

function checkEmail(theObj,option)
{
	var theString = theObj.value;
	if(option=='1')
	{
	        if (!isEmpty(theString))
	        {
	                if (!checkString(theString,ALPHANUMERICS+MAIL)) return warn(LBL_MAIL,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
	                if ((!contains(theString,"@.")) || (theString.indexOf("@")!=theString.lastIndexOf("@"))) return warn(LBL_MAIL,LBL_WRONGFORMATPREFIX,LBL_WRONGFORMATSUFFIX);
			if (theString.lastIndexOf(".")==theString.length-1) return warn(LBL_MAIL,LBL_WRONGFORMATPREFIX,LBL_WRONGFORMATSUFFIX);
			if (theString.lastIndexOf("@.")>0) return warn(LBL_MAIL,LBL_WRONGFORMATPREFIX,LBL_WRONGFORMATSUFFIX);
	        }
	        else
	        {
	                return warn(LBL_MAIL,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
	        }
	}
	else
	{
		if (!isEmptyForMail(theString))
	        {
	                if (!checkString(theString,ALPHANUMERICS+MAIL)) return warn(LBL_MAIL,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
	                if ((!contains(theString,"@.")) || (theString.indexOf("@")!=theString.lastIndexOf("@"))) return warn(LBL_MAIL,LBL_WRONGFORMATPREFIX,LBL_WRONGFORMATSUFFIX);
			if (theString.lastIndexOf(".")==theString.length()) return warn(LBL_MAIL,LBL_WRONGFORMATPREFIX,LBL_WRONGFORMATSUFFIX);
	        }
	}
        return true;
}

function checkTelefono(theObj,option)
{
	var theString = theObj.value;
	if(option=='1')
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,DIGITS)) return warn(LBL_TELEFONO,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
		else
		{
			return warn(LBL_TELEFONO,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
		}
	}
	else
	{
		if (!isEmpty(theString))
		{
			if (!checkString(theString,DIGITS)) return warn(LBL_TELEFONO,LBL_INVALIDCHARSPREFIX,LBL_INVALIDCHARSSUFFIX);
		}
	}
	return true;
}


function checkMessaggio(theObj,theLabel,option)
{
	var theString = theObj.value;
	if(option=='1')
	{
		if (isEmpty(theString))
		{
			return warn(theLabel,LBL_EMPTYPREFIX,LBL_EMPTYSUFFIX);
		}
	}	
	return true;
}

function checkCombobox(theObj,theLabel,option)
{
	var theString = theObj.value;
	if(option=='1')
	{
		if(theString=="-")		
		{
			return warn(theLabel,"Selezionare una delle voci del campo","");
		}
	}	
	return true;
}

function informativaPrivacy(){
	window.open('http://servizi.mediaset.it/casting/PassaparolaInformativa.htm','Informativa','menubar=no,scrollbars=yes,status=no,width=536,height=600')
}

function inviaMail(theObj)
{ 				
	if((checkNome(theObj.nome,'0'))&&
	   (checkCognome(theObj.cognome,'0'))&&
	   (checkIndirizzo(theObj.indirizzo,'0'))&&
	   (checkCitta(theObj.citta,'0'))&&
	   (checkEmail(theObj.mail,'1'))&&
	   (checkTelefono(theObj.telefono,'1'))&&
		   (checkMessaggio(theObj.sos,'Sos','1')))
		{
		theObj.submit();
	}
}
function clearForm(theObj)
{
	theObj.nome.value='';
	theObj.cognome.value='';
	theObj.indirizzo.value='';
	theObj.citta.value='';
	theObj.mail.value='';
	theObj.telefono.value='';
	theObj.sos.value='';        
}
function inviaMail_tv(theObj)
{ 				
	if((checkCombobox(theObj.tipo,'Tipo di segnalazione','1'))&&
	   (checkNome(theObj.nome,'0'))&&
	   (checkCognome(theObj.cognome,'0'))&&
	   (checkIndirizzo(theObj.indirizzo,'0'))&&
	   (checkCitta(theObj.citta,'0'))&&
	   (checkEmail(theObj.mail,'1'))&&
	   (checkTelefono(theObj.telefono,'1'))&&
           (checkMessaggio(theObj.segnalazione,'Segnalazione','1')))
        {
		theObj.submit();
	}
}
function clearForm_tv(theObj)
{
	theObj.nome.value='';
	theObj.cognome.value='';
	theObj.indirizzo.value='';
	theObj.citta.value='';
	theObj.mail.value='';
	theObj.telefono.value='';
	theObj.segnalazione.value='';        
}

function inviaMail_Oss(theObj)
{ 				
	if((checkNome(theObj.nome,'0'))&&
	   (checkCognome(theObj.cognome,'0'))&&
	   (checkIndirizzo(theObj.indirizzo,'0'))&&
	   (checkCitta(theObj.citta,'0'))&&
	   (checkEmail(theObj.mail,'1'))&&
	   (checkTelefono(theObj.telefono,'1'))&&
           (checkMessaggio(theObj.sos,'Sos','1')))
        {
		theObj.submit();
	}
}
function clearForm_Oss(theObj)
{
	theObj.nome.value='';
	theObj.cognome.value='';
	theObj.indirizzo.value='';
	theObj.citta.value='';
	theObj.mail.value='';
	theObj.telefono.value='';
	theObj.sos.value='';        
}

function inviaMailPartecipa(theObj)
{ 		
	if((checkNome(theObj.nome,'1'))&&
	    (checkCognome(theObj.cognome,'1'))&&
	    (checkIndirizzo(theObj.indirizzo,'0'))&&
	    (checkCitta(theObj.citta,'0'))&&
	    (checkEmail(theObj.mail,'1'))&&
	    (checkTelefono(theObj.telefono,'1')))
        {
		theObj.submit();
	}
}
function clearFormPartecipa(theObj)
{
	theObj.nome.value='';
	theObj.cognome.value='';
	theObj.indirizzo.value='';
	theObj.citta.value='';
	theObj.mail.value='';
	theObj.telefono.value='';        
}



