function isEmail(pCampo)
{
	pCampo.value = trim(pCampo.value);
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(pCampo.value.match(emailExp)){
		return true;
	}else{
		alert("Indirizzo di posta elettronica errato");
		return false;
	}
}

function emailCheck(pCampo)
{	
	if (pCampo.value == "") {
		alert("campo vuoto");
		return false;
	}
	if (pCampo.value != "" && (pCampo.value.indexOf("@") < 2 || pCampo.value.indexOf(".") == -1))
	{
		alert("Indirizzo di posta elettronica errato");
		return false;
  	}	
	return true;
}

function numericCheck(pCampo, pHasDec, pHasSign, pSetFocus)
{
	var checkStr = pCampo.value;
	var allValid = true;
	var decPoints = 0;
	var sign = 0;

	var checkNumOK = "0123456789";
	if (pHasDec)
		checkNumOK += ".";
	if (pHasSign)
		checkNumOK += "-";

	for (i = 0; i < checkStr.length; i++)
	{
  		ch = checkStr.charAt(i);
    	for (j = 0; j < checkNumOK.length; j++)
		{
    		if (ch == checkNumOK.charAt(j))
       			break;
		}
			
   		if (j == checkNumOK.length)
   		{
   			allValid = false;
      		break;
		}
		
		if (ch == ".")
			decPoints++;
			
		if (ch == "-")
			sign++;
  	}

	if (!allValid)
  	{
		alert("Formato numerico non valido");
		if (pSetFocus)
	    	pCampo.focus();
    	return false;
  	}

	if (decPoints > 0 && !pHasDec)
	{
		alert("Inserire un numero senza decimali");
		if (pSetFocus)
	    	pCampo.focus();
    	return false;
	}
	else if (pHasDec)
	{
		if (decPoints > 1)
		{
			alert("Inserire un solo punto di separazione decimale");
			if (pSetFocus)
	    		pCampo.focus();
	    	return false;
		}
		else if (decPoints == 1)
		{
			if (pCampo.value.indexOf(".") == 0)
				pCampo.value = "0" + pCampo.value;
			else if (pCampo.value.indexOf(".") == pCampo.value.length - 1)
				pCampo.value = pCampo.value.substr(0, pCampo.value.length - 1);
		}
	}
	
	if (!pHasSign && sign > 0)
	{
		alert("Inserire un numero senza segno");
		if (pSetFocus)
	    	pCampo.focus();
    	return false;
	}
	else if (pHasSign)
	{
		if (sign > 1)
		{
			alert("Inserire un solo simbolo indicante il segno");
			if (pSetFocus)
	    		pCampo.focus();
	    	return false;
		}
		else if (sign == 1)
		{
			if (pCampo.value.indexOf("-") != 0)
			{
				alert("Il simbolo indicante il segno deve essere presente solamente come primo carattere");
				if (pSetFocus)
	    			pCampo.focus();
	    		return false;
			}
		}
	}
		
	return true;
}

function removeAmp(pStr)
{
	pStr = trim(pStr);
	var ret = "";
	var found = false;
	for (var i = 0; i < pStr.length; i++)
	{
		if (pStr.charAt(i) == "&")
			found = true;
		else if (found && pStr.charAt(i) == ";")
			found = false;
		else if (!found)
			ret = ret + pStr.charAt(i);		
	}
	
	return ret;
}

function numericCheck2(pCampo, pNumDec, pHasSign)
{
	var checkStr = removeAmp(pCampo);
	var allValid = true;
	var decPoints = 0;
	var sign = 0;

	var checkNumOK = "0123456789";
	if (pNumDec > 0)
		checkNumOK += ",";
	if (pHasSign)
		checkNumOK += "-+";

	for (i = 0; i < checkStr.length; i++)
	{
  		ch = checkStr.charAt(i);
    	for (j = 0; j < checkNumOK.length; j++)
		{
    		if (ch == checkNumOK.charAt(j))
       			break;
		}
			
   		if (j == checkNumOK.length)
   		{
   			allValid = false;
      		break;
		}
		
		if (ch == ",")
			decPoints++;
			
		if (ch == "-" || ch == "+")
			sign++;
  	}

	if (!allValid)
  	{
		pbMessage(pb_message_attenzione, "formato numerico non valido");
    	return "*";
  	}

	if (decPoints > 0 && pNumDec == 0)
	{
		pbMessage(pb_message_attenzione, "inserire un numero senza decimali");
    	return "*";
	}
	else if (pNumDec > 0)
	{
		if (decPoints > 1)
		{
			pbMessage(pb_message_attenzione, "inserire una sola virgola di separazione decimale");
	    	return "*";
		}
		else if (decPoints == 1)
		{
			var posDecimale = checkStr.indexOf(",");
			if (posDecimale == 0)
				checkStr = "0" + checkStr;
			else if (posDecimale == checkStr.length - 1)
				checkStr = checkStr.substr(0, checkStr.length - 1);
								
			//adesso taglio la parte decimale per rispecchiare il numero di decimali richiesto!
            var valoreCampo = checkStr.substring(0, posDecimale);

            //adesso controllo quante cifre decimali utilizzare
			var myDec = "";
            for (var jj = posDecimale + 1; jj < checkStr.length; jj++)
              myDec += checkStr.charAt(jj);

            while (myDec.length < pNumDec)
              myDec += "0";

            if (myDec.length > pNumDec)
              myDec = myDec.substring(0, pNumDec);
			  
			checkStr = valoreCampo + "," + myDec;
		}
	}
	
	if (!pHasSign && sign > 0)
	{
		pbMessage(pb_message_attenzione, "inserire un numero senza segno");
    	return "*";
	}
	else if (pHasSign)
	{
		if (sign > 1)
		{
			pbMessage(pb_message_attenzione, "inserire un solo simbolo indicante il segno");
	    	return "*";
		}
		else if (sign == 1)
		{
			if (checkStr.indexOf("-") != 0 && checkStr.indexOf("+") != 0)
			{
				pbMessage(pb_message_attenzione, "il simbolo indicante il segno deve essere presente solamente come primo carattere");
	    		return "*";
			}
			
			if (checkStr.indexOf("+") == 0)
			{
				//tolgo il simbolo + che e' implicito!
				checkStr = checkStr.substring(1);
			}
		}
	}
		
	return checkStr;
}

function dataCheck(pCampo)
{
	var checkStr = pCampo;
	var checkDataOK = "0123456789/";
	var numSlash = 0;
	var allValid = true;
	
	for (var i = 0; i < checkStr.length; i++)
	{
  		var ch = checkStr.charAt(i);
		
		if (ch == "/")
			numSlash++;
			
    	for (var j = 0; j < checkDataOK.length; j++)
		{
    		if (ch == checkDataOK.charAt(j))
       			break;
		}
			
   		if (j == checkDataOK.length)
   		{
   			allValid = false;
      		break;
		}
	}

	if (pCampo == "")
		return "";
	
	if (!allValid || numSlash != 2)
  	{
		pbMessage(pb_message_attenzione, "formato data non valido");
		return "*";
	}
		
	var pos1 = pCampo.indexOf("/");
	var pos2 = pCampo.indexOf("/", pos1 + 1);

	if ((pos1 != 1 && pos1 != 2) || (pos2 != 3 && pos2 != 4 && pos2 != 5))
	{	
		pbMessage(pb_message_attenzione, "formato data non valido");
		return "*";		
	}

	if (pCampo.charAt(pos1 + 1) == "/")
	{	
		pbMessage(pb_message_attenzione, "formato data non valido");
		return "*";		
	}
	
	var mDay = parseInt(pCampo.substr(0, pos1), 10);
	var mMonth = parseInt(pCampo.substr(pos1 + 1, pos2 - pos1 - 1), 10);
	var mYear = parseInt(pCampo.substr(pos2 + 1), 10);
	
	if (("" + mYear).length > 4)
	{
		pbMessage(pb_message_attenzione, "anno non valido");
		return "*";
	}
	
	if (mYear < 30)
		mYear += 2000;
	else if (pCampo.length - pos2 - 1 <= 2)
		mYear += 1900;

	if (mMonth < 1 || mMonth > 12)
	{
		pbMessage(pb_message_attenzione, "mese non valido");
		return "*";		
	}
	else
	{
		if (mMonth == 1  || mMonth == 3 || mMonth == 5 || mMonth == 7 || mMonth == 8 || mMonth == 10 || mMonth == 12)
		{
			if (mDay < 1 || mDay > 31)
			{
				pbMessage(pb_message_attenzione, "giorno non valido");
				return "*";
			}
		}
		else if (mMonth == 2)
		{
			//Controllo se l'anno e' bisestile
			var NumD = 28;
			if (((mYear % 4 == 0) && mYear % 100 != 0) || mYear % 400 == 0)
  				NumD = 29;
			
			if (mDay < 1 || mDay > NumD)
			{
				pbMessage(pb_message_attenzione, "giorno non valido");
				return "*";
			}
		}
		else
		{
			if (mDay < 1 || mDay > 30)
			{
				pbMessage(pb_message_attenzione, "giorno non valido");
				return "*";
			}
		}
	}
	
	//Riformatto la data come 00/00/0000
	var newData = ""
	if (mDay < 9)
		newData = "0" + mDay;
	else
		newData = "" + mDay;
		
	newData += "/";

	if (mMonth < 9)
		newData += "0" + mMonth;
	else
		newData += mMonth;
		
	newData += "/" + mYear;
	
	pCampo = newData;
		
	return pCampo;
}

function timeCheck(pCampo)
{
	var checkStr = pCampo;
	var checkTimeOK = "0123456789:";
	var numDuePunti = 0;
	var allValid = true;
	
	for (var i = 0; i < checkStr.length; i++)
	{
  		var ch = checkStr.charAt(i);
		
		if (ch == ":")
			numDuePunti++;
			
    	for (var j = 0; j < checkTimeOK.length; j++)
		{
    		if (ch == checkTimeOK.charAt(j))
       			break;
		}
			
   		if (j == checkTimeOK.length)
   		{
   			allValid = false;
      		break;
		}
	}

	if (pCampo == "")
		return "";
	
	if (!allValid || numDuePunti > 1 || pCampo.length > 5)
  	{
		pbMessage(pb_message_attenzione, "formato ora non valido");
		return "*";
	}
		
	var pos1 = pCampo.indexOf(":");
	var mHours = 0;
	var mMinutes = 0;
	if (pos1 != -1)
	{
		//ho trovato i due punti. adesso verifico se sono nella posizione corretta		
		if (pos1 != 1 && pos1 != 2)
		{	
			pbMessage(pb_message_attenzione, "formato ora non valido");
			return "*";		
		}
		
		mHours = parseInt(pCampo.substr(0, pos1), 10);
		if (pos1 < pCampo.length - 1)
			mMinutes = parseInt(pCampo.substr(pos1 + 1), 10);
	}
	else
		mHours = parseInt(pCampo, 10);

	if (mHours < 0 || mHours > 23)
	{
		pbMessage(pb_message_attenzione, "ora non valida");
		return "*";		
	}

	if (mMinutes < 0 || mMinutes > 59)
	{
		pbMessage(pb_message_attenzione, "minuti non validi");
		return "*";
	}
	
	//Riformatto l'orario come 00:00
	var newTime = ""
	if (mHours < 9)
		newTime = "0" + mHours;
	else
		newTime = "" + mHours;
		
	newTime += ":";

	if (mMinutes < 9)
		newTime += "0" + mMinutes;
	else
		newTime += mMinutes;
			
	pCampo = newTime;
		
	return pCampo;
}

function trim(str) 
{
	var resultStr = "";
	
	if (str != null)
	{
		resultStr = trimLeft(str);
		resultStr = trimRight(resultStr);
	}
	
	return resultStr;
} // end Trim

function trimLeft(str) 
{
	var resultStr = "";
	var i = len = 0;
	
	// Return immediately if an invalid value was passed in
	if (str + "" == "undefined" || str == null)	
		return "";

	// Make sure the argument is a string
	str += "";

	if (str.length == 0) 
		resultStr = "";
	else 
	{	
  		// Loop through string starting at the beginning as long as there
  		// are spaces.
		//	  	len = str.length - 1;
		len = str.length;
					
  		while ((i <= len) && (str.charAt(i) == " "))
			i++;
	
   	// When the loop is done, we're sitting at the first non-space char,
 		// so return that char plus the remaining chars of the string.
  		resultStr = str.substring(i, len);
  	}
			
  	return resultStr;
} // end TrimLeft
			
function trimRight(str) 
{
	var resultStr = "";
	var i = 0;
	
	// Return immediately if an invalid value was passed in
	if (str + "" == "undefined" || str == null)	
		return "";

	// Make sure the argument is a string
	str += "";
		
	if (str.length == 0) 
		resultStr = "";
	else 
	{
  		// Loop through string starting at the end as long as there
 		// are spaces.
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == " "))
 			i--;
			 			
 			// When the loop is done, we're sitting at the last non-space char,
	 		// so return that char plus all previous chars of the string.
	  		resultStr = str.substring(0, i + 1);
	  	}
	  	
	  	return resultStr;  	
} // end TrimRight

var virgolette = '"';
var m_illegalCar = "";
function chkIllegaCar(pCampo)
{
	m_illegalCar = "";
	if (pCampo.type == "text" || pCampo.type == "textarea")
	{
		var IndFirstCarOk = -1;
		for (var i = 0; i < pCampo.value.length; i++)
		{
			if (pCampo.value.substr(i, 1) != " " && IndFirstCarOk == -1)
				IndFirstCarOk = i;

			if (pCampo.value.substr(i, 1) == virgolette)
			{
				m_illegalCar = virgolette;
				return 1;
			}

			if (pCampo.value.substr(i, 1) == "#")
			{
				m_illegalCar = "#";
				return 1;
			}

			if (pCampo.value.substr(i, 1) == "'")
			{
				m_illegalCar = "'";
				return 1;
			}
		}

		if (IndFirstCarOk >= 0)
		{
			var strTmp = pCampo.value;

			pCampo.value = strTmp.substr(IndFirstCarOk);
			
			IndLastCarOk = -1;
			for (i = pCampo.value.length - 1; i >= 0; i = i - 1)
			{
				if (pCampo.value.substr(i, 1) != " " && IndLastCarOk == -1)
					IndLastCarOk = i;
			}
			
			if (IndLastCarOk >= 0)
			{
				strTmp = pCampo.value;

				pCampo.value = strTmp.substr(0, IndLastCarOk + 1);
			}			
		}
		else
			pCampo.value = "";
	}
	return 0;
}
