<!-- *** ROBY FUNCTION ****  --------------------------------------------------------------------------->
function strReverse(st){
	var i,s="";
	for(i=st.length-1;i>=0;i--)
	s += st.charAt(i);
		
	return s;
}
	
	
function strLeftTrim(t){
	var i,st=t;
	while(st.length>0 && st.charAt(0)==" ")
	st = st.substring(1,255)
		
	return(st);
}
	
function strTrim(st){
	var t = strReverse(st), i;
	t =strLeftTrim(t);
		
	t = strReverse(t);
		
	return(strLeftTrim(t));
}

function strRightDif(st,n){
	// Funzione che restituisce la stringa st diminuita a destra di n caratteri
	// ex: strRigthDif("pippo",2) restituisce "pip"
	
	var t = strReverse(st);
	t = t.substring(n,255);
	return(strReverse(t));
}
function strLeftDif(st,n){
	// Funzione che restituisce la stringa st diminuita a sinistra di n caratteri
	// ex: strLeftDif("pippo",2) restituisce "ppo"
	return(st.substring(n,255));
}

function hasValue(objType, obj, objValue)
{
    if (objType == "TEXT" || objType == "PASSWORD")
	{
    	if (obj.value.length == 0) 
      		return false;
    	else 
      		return true;
    }
    else if (objType == "SELECT")
	{
        for (i=0; i < obj.length; i++)			
	    	{
		if (obj.options[i].selected)
			return true;
	}
       	return false;	
	}
    else if (objType == "SINGLE_VALUE_RADIO" || objType == "SINGLE_VALUE_CHECKBOX")
	{
		if (obj.checked)
			return true;
		else
       		return false;	
	}
    else if (objType == "RADIO" || objType == "CHECKBOX")
	{
        for (i=0; i < obj.length; i++)
    	{
		   if (obj[i].checked)
			 return true;
		}
       	return false;	
	}
}
function checkInteger(objValue)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	
    if (objValue.length == 0)
        return true;
    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;
    //The first character can be + -  blank or a digit.
	check_char = objValue.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	   return checkNumber(objValue);
    else
	   return false;
}
function checkRange(objValue, minValue, maxValue)
{
    // check minimum
    if (minValue != null)
	{
        if (objValue < minValue)
		  return false;
	}
    // check maximum
    if (maxValue != null)
	{
	    if (objValue > maxValue)
		  return false;
	}
	
    //All tests passed, so...
    return true;
}
function checkNumber(objValue)
{
    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(objValue.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < objValue.length; i++)
	{
		check_char = number_format.indexOf(objValue.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks
		}
	    else if (trailing_blank)
		     return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
}


function checkDate(objValue)
{
    //Returns true if value is a eurodate format or is NULL
    //otherwise returns false	
    if (objValue.length == 0)
        return true;
    //Returns true if value is a date in the dd/mm/yyyy format
	isplit = objValue.indexOf('/');
	if (isplit == -1)
	{
		isplit = objValue.indexOf('.');
	}
	if (isplit == -1 || isplit == objValue.length)
		return false;
    sDay = objValue.substring(0, isplit);
	monthSplit = isplit + 1;
	isplit = objValue.indexOf('/', monthSplit);
	if (isplit == -1)
	{
		isplit = objValue.indexOf('.', monthSplit);
	}
	if (isplit == -1 ||  (isplit + 1 )  == objValue.length)
		return false;
    sMonth = objValue.substring((sDay.length + 1), isplit);
	sYear = objValue.substring(isplit + 1);
	if (!checkInteger(sMonth)) //check month
		return false;
	else
	if (!checkRange(sMonth, 1, 12)) // check month
		return false;
	else
	if (!checkInteger(sYear)) //check year
		return false;
	else
	if (!checkRange(sYear, 1970, 2020)) //check year
		return false;
	else
	if (!checkInteger(sDay)) //check day
		return false;
	else
	if (!checkDay(sYear, sMonth, sDay)) //check day
		return false;
	else
		return true;
}
function checkDay(checkYear, checkMonth, checkDay)
{
	maxDay = 31;
	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}
	return checkRange(checkDay, 1, maxDay); //check day
}

//----- CONTROLLO: MAIL -------- 
function checkEMail(objValue)
{
	apos=objValue.indexOf("@");
	dotpos=objValue.lastIndexOf(".");
	lastpos=objValue.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2) 
	{return false;}
	else {return true;}
}
function check(objForm, objType, objValidate, objRequired, objName, obj, objValue, minValue, maxValue)
// objForm: oggetto form
// objType: tipo dell'oggetto ( TEXT )
// objValidate: controllo di sintassi per STRING, INTEGER, FLOAT, DATE, EMAIL
// objRequired: oggetto richiesto
// objName: nome dell'oggetto da visualizzare in caso di errore
// obj: oggetto
// objValue: valore dell'oggetto
// minValue: valore minimo consentito, se impostato a null non viene considerato
// maxValue: valore massimo consentito, se impostato a null non viene considerato
{
  var err = 0;
  var strErr = "";
  var decimal_format = ".";
  var check_char;
  // Controllo se obj ha un valore
  if (objRequired)
  {
     if (!hasValue(objType, obj, objValue))
	 {
        strErr = objName + " obbligatorio/a" + "\n";
		return strErr;
	 }
  }
	
  // Controllo il tipo di dato inserito in obj
  switch (objValidate)
  {
  case "INTEGER":
     if (!checkInteger(objValue))
	 {
	    strErr = objName + " non valido" + "\n";
	    return strErr;
	 }
	 break;
  case "FLOAT":
	 if (!checkNumber(objValue))
	 {
	   if (objValue != '') 
	   {
	   strErr = objName + " non valido" + "\n";
	   return strErr;
	   }
	 }
	 break;
  case "DATE":
	 if (!checkDate(objValue))
	 {
	   strErr = objName + " non valido" + "\n";
	   return strErr;
	 }
	 break;
  case "EMAIL":
	 if (!checkEMail(objValue))
	 //if (!emailvalidation(objValue))
	 {
	   strErr = objName + " non valido" + "\n";
	   return strErr;
	 }
	 break;
  }
  // Controllo il range del objValue
  if ((minValue != null) || (maxValue != null))
  {
	 if (!checkRange(objValue, minValue, maxValue))
	 {
	   if ((minValue == null) && (maxValue != null))
	   {
	      strErr = objName + " deve essere <= " + maxValue + "\n"      
	   }	
	   else if ((minValue != null) && (maxValue == null))
	   {
	      strErr = objName + " deve essere >= " + minValue + "\n"
	   }	 
	   else //((minValue != null) && (maxValue != null))
	   {
	      strErr = objName + " non compreso nell'intervallo da " + minValue + " a " + maxValue + "\n";
	   }	  
	   return strErr;
	 }
  }
  
  return strErr;
}


