var bCalcola = false;

function onTxtFocus() {
bCalcola = true
}

function onTxtBlur() {
bCalcola = false
calcCharLeft(document.Msg);
}

function tick() {

	if (bCalcola) {
		document.Msg.Testo.blur();
		document.Msg.Testo.focus();
	}
	timerID = setTimeout("tick()",100);


}

function calcCharLeft() {
		clipped = false
		maxLength = 124
        if (document.Msg.Testo.value.length > maxLength) {
	        document.Msg.Testo.value = document.Msg.Testo.value.substring(0,maxLength)
		    charleft = 0
		    clipped = true
        } else {
			charleft = maxLength - document.Msg.Testo.value.length
		}

        document.Msg.msgCL.value = charleft
        return clipped
}
function onCLFocus() {
	document.Msg.msgCL.blur();
}


function submitForms()
   {
   if ( (isTesto() ) && (isPref()) && (isTelefono()))
        {
         openit(300,630,"","sms")
		 document.Msg.submit()
		 document.Msg.reset()
         return true;
         } 
   else
      return false;
   }


function isTesto()
   {
   var str = document.Msg.Testo.value;
   if (str == "")
      {
      alert("\nInserisci il testo.")
      document.Msg.Testo.focus();
      return false;
      }

//   for (var i = 0; i < str.length; i++) 
//      {
//      var ch = str.substring(i, i + 1);
//      if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch < "1" || "9" < ch)) && ch != " " && ch != "0" &&  ch != "!" && ch != "?" && ch != "." && ch != "," && ch != "'" && ch != ":" && ch != ";" ) 
//          {
//         alert("\nIl campo TESTO puņ contenere solamente:\n\nlettere non accentate numeri spazi e \n\ni caratteri speciali ! ? ' , . : ; ");
//       document.Msg.Testo.select();
//       document.Msg.Testo.focus();
//       return false;
//        }
//      }
   return true;
   }

function isPref()
   {
   var str = document.Msg.Pref[document.Msg.Pref.selectedIndex].value;
    if (str == "")
      {
    alert("\nInserisci il prefisso.")
    document.Msg.Pref.focus();
    return false;
      }
   for (var i = 0; i < str.length; i++) 
      {
      var ch = str.substring(i, i + 1);
      if (((ch < "1" || "9" < ch) && ch != '0')) 
         {
         alert("\nIl campo PREFISSO accetta solamente numeri");
         document.Msg.Pref.focus();
         return false;
         }
      }
   return true;
   }

function isTelefono()
   {
   var str =  document.Msg.Telefono.value;
   if (str == "")
      {
      alert("\nInserisci il numero del cellulare.")
      document.Msg.Telefono.focus();
      return false;
      }
  for (var i = 0; i < str.length; i++) 
      {
      var ch = str.substring(i, i + 1);
      if (((ch < "1" || "9" < ch) && ch != '0')) 
         {
         alert("\nIl campo Numero accetta solamente numeri.");
         document.Msg.Telefono.select();
         document.Msg.Telefono.focus();
         return false;
         }
		if (document.Msg.Telefono.value.length < 6)
  {
    alert("Inserire almeno 6 numeri nel campo Numero.");
    document.Msg.Telefono.focus();
    document.Msg.Telefono.select();
    return false;
  }
      }

   return true;

   }
