 
function loginSubmit() {
    var cellNumberObj = document.getElementById( 'msisdn');
    var passwordObj = document.getElementById( 'password');
    if( cellNumberObj != null ) {
        if( cellNumberObj.value.length != cellNumberLength ) {
            alert(messages['error.invalidnumber']);
            cellNumberObj.focus();
            return false;
        }
        if( !isNumeric(cellNumberObj.value) ) {
            alert(messages['error.onlynumber']);
            cellNumberObj.focus();
            return false;
        }
    }
    if( passwordObj != null ) {
        if( passwordObj.value.length != 6) {
            alert(messages['error.invalidpassword']);
            passwordObj.focus();
            return false;
        }
        return true;
    }
}


function isEmail(str) {
    // Verifico che le siano supportate le regualar exp?
    var supported = 0;
    if (window.RegExp) {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported) 
        return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
}

function isNumeric(str) {
    var validChars = "0123456789";
    var isNumber=true;
    var chart;
    for (i = 0; i < str.length && isNumber == true; i++) { 
        chart = str.charAt(i); 
        if (validChars.indexOf(chart) == -1) 
            isNumber = false;
    }
    return isNumber;
}

function countChars(messageCharsNumber)
{
    var form = document.smsFormBean;
    if (form.smsText.value.length > messageCharsNumber)
    {
        oldString = form.smsText.value.substring(0, messageCharsNumber);
        form.smsText.value = oldString;
    }
    else 
    {
        form.availableChars.value = messageCharsNumber - form.smsText.value.length;
    }
}

function countCharsVariable()
{
    var form = document.smsFormBean;
    if (form.smsText.value.length > maxSmsChars)
    {
        oldString = form.smsText.value.substring(0, maxSmsChars);
        form.smsText.value = oldString;
    }
    else 
    {
        form.availableChars.value = maxSmsChars - form.smsText.value.length;
    }
}

function countMmsCharsVariable()
{
    var form = document.mmsFormBean;
    if (form.mmsText.value.length > mmsMaxChars)
    {
        oldString = form.mmsText.value.substring(0, mmsMaxChars);
        form.mmsText.value = oldString;
    }
    else
    {
        form.availableChars.value = mmsMaxChars - form.mmsText.value.length;
    }
}

function countCharsVariableFirstSms()
{
    var form = document.subscribeFormBean;
    if (form.smsText.value.length > maxSmsChars)
    {
        oldString = form.smsText.value.substring(0, maxSmsChars);
        form.smsText.value = oldString;
    }
    else 
    {
        form.availableChars.value = maxSmsChars - form.smsText.value.length;
    }
}

function manageKeyEnter(evt, actionToEval){
    var evt = (evt) ? evt : event;    
    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
    if (charCode==13||charCode==3)
    {
        eval(actionToEval);
        return false;
    }  
    return true;
}

function setaction(idForm) {
    var id_app=document.getElementById( idForm );
    id_app.submit();
}

function checkNumber(widget, label) {
    if(widget.value.length == 0) {
            alert('Il campo '+label + ' e\' obbligatorio.');
            return(false);
    }

    var re = new RegExp("\[^0-9.\]"); //not in 0-9
    if ( widget.value.match(re) )	{
            alert('Il campo '+label + ' contiene caratteri non validi.');
            return(false);
    }
    return(true);
} 

function isNumericPhone(str, label) {
    var validChars = "0123456789";
    var chart;
    if (str.length == 0){
        alert('Il campo '+label + ' e\' obbligatorio.');
        return(false);
    }
      for (i = 0; i < str.length; i++) { 
          chart = str.charAt(i); 
          if (validChars.indexOf(chart) == -1){
              alert("Il campo " + label + "contiene caratteri non validi.");
              return(false);
          }
          
      }
    return(true);
}

function isDate(campo,label) {
  var DId;	
  var DIm;	
  var DIy;	
  var data=campo.value;

  if(data.length != 6 && data.length != 8 && data.length != 10 && data.length !=0)
  {
	alert(label +" Errata!");
	campo.value = "" ;
	campo.focus();
	return(false);	
  }
  if(data.length == 6)
  {	
  	DId=data.substring(0,2);	
  	DIm=data.substring(2,4);	
        if(data.substring(4,6) <= "30")
  	 DIy="20" + data.substring(4,6);	
        else
  	 DIy="19" + data.substring(4,6);	
  }
  else
  {	
	if(data.length == 8)
	{	
  		DId=data.substring(0,2);	
  		DIm=data.substring(2,4);	
  		DIy=data.substring(4,8);	
  	}
  	else
  	{	
  		DId=data.substring(0,2);	
  		DIm=data.substring(3,5);	
  		DIy=data.substring(6,10);	
  	}
	
  }
  var isdata = 0;
  if (data == "" || data == "00/00/0000")
	return(true);

  for(var i=0; i<DId.length; i++)
  {
	if((DId.charAt(i) < "0" || DId.charAt(i) >"9"))
  	{
		isdata=1;
	}
  }

  for(var j=0; j<DIm.length; j++)
  {
	if((DIm.charAt(j) < "0" || DIm.charAt(j) >"9"))
  	{
		isdata=1;
	}
  }

  for(var r=0; r<DIy.length; r++)
  {
	if((DIy.charAt(r) < "0" || DIy.charAt(r) >"9"))
  	{
		isdata=1;
	}
  }

  if(parseFloat(DIm) < 1 || parseFloat(DIm) > 12)
  {	
	isdata=1;
  }
  if(parseFloat(DIy) < 1000 || parseFloat(DIy) > 9999)
  {	
	isdata=1;
  }
  if(parseFloat(DIm) == 1 || 
     parseFloat(DIm) == 3 ||
     parseFloat(DIm) == 5 ||
     parseFloat(DIm) == 7 ||
     parseFloat(DIm) == 8 ||
     parseFloat(DIm) == 10 ||
     parseFloat(DIm) == 12)
     { 
  	if(parseFloat(DId) < 1 || parseFloat(DId) > 31)
	{
	 isdata=1;
  	}
     }
  if(parseFloat(DIm) == 4 || 
     parseFloat(DIm) == 6 ||
     parseFloat(DIm) == 9 ||
     parseFloat(DIm) == 11)
     { 
  	if(parseFloat(DId) < 1 || parseFloat(DId) > 30)
	{
	 isdata=1;
  	}
     }
  if(parseFloat(DIm) == 2 ) 
     {
    	var iBis = parseFloat(DIy) % 4;
  	if( iBis == 0) //Bisestile
	{
  		if(parseFloat(DId) < 1 || parseFloat(DId) > 29)
		{
		 isdata=1;
  		}
	}
	else
	{
  		if(parseFloat(DId) < 1 || parseFloat(DId) > 28)
		{
		 isdata=1;
  		}
	}
     }
  if (isdata == 1) {
     alert(label + " Errata!");
     campo.value="";	
     campo.focus();
     return(false);
  }
  campo.value = DId + "/" + DIm + "/" + DIy
  return(true);
}

function checkEmail(string) {
    if (string.length == 0)
        return true;
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1){
        alert('Email non valido!');
        return false;
    }
    return true;
}


/* ### preview sound ### */
function playAudio(audioFile) {
   document.getElementById('soundPreview').src=audioFile;
}

function playSelectedAudio(selectObj) {
   selectObj = document.getElementById('choosenSound');
   audioFile = "../mmsSounds/" + selectObj.value;
   document.getElementById('soundPreview').src=audioFile;
   document.getElementById('soundPreviewFF').src=audioFile;
}


