//=============================================================================

// Display prompt string s in status bar.
function gwf_statusPrompt(s)
{
  window.status = s
}

function gwf_confirmationPrompt (PromptString)
{
  return confirm(PromptString);
}

var gwf_debugFlag = false;
var gwf_padding   = '0000000000';
var gwf_whitespace = " \t\n\r";
var gwf_digits = "0123456789";
var gwf_lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var gwf_uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var gwf_defaultEmptyOK = true;

function gwf_prompt (Ctrl, PromptString, noFocus)
{
  alert (PromptString)
  if (!noFocus) Ctrl.focus();
  return;
}

function gwf_isAny(s)
{
  return true;
}

function gwf_formatAny(s)
{
  return s;
}

//=============================================================================
//***************************** FUNCTIONS ON STRINGS **************************

// Check whether string s is empty.
function gwf_isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

// Returns true if character c is an English letter
// (A .. Z, a..z).
function gwf_isLetter (c)
{
  return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Returns true if character c is a letter or digit.
function gwf_isLetterOrDigit (c)
{
  return (gwf_isLetter(c) || gwf_isDigit(c))
}

function gwf_isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

// Returns true if string s is empty or
// whitespace characters only.
function gwf_isWhitespace(s)
{
  var i;
  if (gwf_isEmpty(s)) return true;
  for (i = 0; i < s.length; i++)
  {
   // Check that current character isn't whitespace.
   var c = s.charAt(i);
   if (gwf_whitespace.indexOf(c) == -1) return false;
  }
  // All characters are whitespace.
  return true;
}

// Removes all characters which appear in string bag from string s.
function gwf_stripCharsInBag (s, bag)
{
  var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (i = 0; i < s.length; i++)
  {
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

// Removes all characters which do NOT appear in string bag
// from string s.
function gwf_stripCharsNotInBag (s, bag)
{
  var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.
  for (i = 0; i < s.length; i++)
  {
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) != -1) returnString += c;
  }
  return returnString;
}

// Removes all whitespace characters from s.
function gwf_stripWhitespace (s)
{
  return gwf_stripCharsInBag (s, gwf_whitespace)
}

//------------- Used by gwf_validateDate() -------------------
function gwf_stripCharString (InString, CharString)
{
  var OutString="";
  for (var Count=0; Count < InString.length; Count++)
  {
    var TempChar=InString.substring (Count, Count+1);
    var Strip = false;
    for (var Countx = 0; Countx < CharString.length; Countx++)
        {
      var StripThis = CharString.substring(Countx, Countx+1)
      if (TempChar == StripThis)
          {
        Strip = true;
        break;
      }
    }
    if (!Strip)
      OutString=OutString+TempChar;
  }
  return (OutString);
}

// Return true/false if the string contains allowed characters only
// May be it should return true also on empty strings
function gwf_allowInString (InString, RefString)
{
  if(InString.length==0) return (false);
  for (var Count=0; Count < InString.length; Count++)
  {
    var TempChar= InString.substring (Count, Count+1);
    if (RefString.indexOf (TempChar, 0)==-1)
      return (false);
  }
  return (true);
}

// Returns true if single character c (actually a string)
// is contained within string s.
// I assume that latest version of Javascript make provision for the
// indexOf type of function
function gwf_charInString (c, s)
{
  // ????? Is it actually needed?
  for (i = 0; i < s.length; i++)
  {
    if (s.charAt(i) == c) return true;
  }
  return false
}

//----------------------- TRIM SECTION ----------------------------
// Removes initial (leading) whitespace characters from s.
function gwf_stripInitialWhitespace (s)
{
  var i = 0;
  while ((i < s.length) && gwf_charInString (s.charAt(i), gwf_whitespace))
     i++;
  return s.substring (i, s.length);
}

// Removes final whitespace characters from s.
function gwf_stripFinalWhitespace (s)
{
  var i = 0;
  i = s.length;
  while ((i > 0) && gwf_charInString (s.charAt(i-1), gwf_whitespace))
    i--;
  return s.substring (0,i);
}

function gwf_trim (s)
{
  var string = gwf_stripInitialWhitespace (s);
  string = gwf_stripFinalWhitespace (string);
  return string;
}

//La funzione converte il carattere Euro,
//i doppi apici e l'apice singolo rivolti sia a destra che a sinistra
function gwf_escape_textarea (s)
{
	var ret = '';
	for(i=0; i < s.length; i++) {
		
		if(s.charCodeAt(i) == 8364) {
			ret = ret + 'E';
		} else if(s.charCodeAt(i) == 8220) {
			ret = ret + '"';
		} else if(s.charCodeAt(i) == 8221) {
			ret = ret + '"';
		} else if(s.charCodeAt(i) == 8216) {
			ret = ret + '\'';
		} else if(s.charCodeAt(i) == 8217) {
			ret = ret + '\'';
		} else {
			ret = ret + s.charAt(i);
		}
	}
	
	//alert(s);
	//alert(ret);
  return ret;
}

function gwf_isNumeric(s)
{
    var i;
    for (i=0;i<s.length;i++)
      if (gwf_digits.indexOf(s.charAt(i))<0) return false;

    return true;
}

function gwf_checkFileExtensions(s, csvExtensions)
{
   var ndx = s.lastIndexOf(".");
   if (ndx < 0) return false;
   
   var fileExtension = s.substring(ndx + 1).toUpperCase();
   if (csvExtensions.toUpperCase().indexOf(fileExtension) >= 0)  return true;
   
   window.alert("File supportati: " + fileExtension + "/" + csvExtensions);
   return false;    	
}

//=============================================================================
//=================== VALIDATION FUNCTIONS REPOSITORY =========================
//=============================================================================

// function gwf_isAlphabetic (s), gwf_formatAlphabetic (s)

// DOMAIN: ALPHABETIC ---------------------------------------------------
function gwf_isAlphabetic (s)
{
  var i;
  if (gwf_isEmpty(s))  return false;
     
  // Search through string's characters one by one
  // until we find a non-alphabetic character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++)
  {
   // Check that current character is letter.
   var c = s.charAt(i);
   if (!gwf_isLetter(c))
       return false;
  }
  
  // All characters are letters.
  return true;
}

function gwf_formatAlphabetic(s)
{
    return s;
}

// DOMAIN: EMAIL ---------------------------------------------------
function gwf_isEmail (s)
{
  // there must be >= 1 character before @, so we
  // start looking at character position 1
  // (i.e. second character)
  var i = 1;
  var sLength = s.length;
  // look for @
  while ((i < sLength) && (s.charAt(i) != "@"))
  {
    i++
  }
  if ((i >= sLength) || (s.charAt(i) != "@"))
  {
    if (gwf_debugFlag) alert("Valore del campo errato!");
    return false;
  }
  else i += 2;
  // look for .
  while ((i < sLength) && (s.charAt(i) != "."))
  {
    i++
  }
  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != "."))
  {
    if (gwf_debugFlag)  alert("Valore del campo errato!");
    return false;
  }
  else
  {
    if (gwf_debugFlag)  alert("Immissione dato corretta!");
    return true;
  }
}

function gwf_formatEmail(s)
{
    return s;
}

// DOMAIN: ALPHANUMERIC ---------------------------------------------------
function gwf_isAlphanumeric(s)
{
    var alphaChars = gwf_digits + gwf_uppercaseLetters + gwf_lowercaseLetters;
    var i;
    for (i=0;i<s.length;i++)
      if (alphaChars.indexOf(s.charAt(i))<0) return false;

    return true;
}

function gwf_formatAlphanumeric(s)
{
    return s;
}

// DOMAIN: PASSWORD ---------------------------------------------------
function gwf_isPassword(s)
{
    var alphaChars = gwf_digits + gwf_uppercaseLetters + gwf_lowercaseLetters;
    var i;
    for (i=0;i<s.length;i++)
      if (alphaChars.indexOf(s.charAt(i))<0) return false;

    return true;
}

function gwf_formatPassword(s)
{
    return s;
}

// DOMAIN: PHONE ---------------------------------------------------
function gwf_isPhone(s)
{
    var phoneChars = gwf_digits + " +-/()";
    var i;
    for (i=0;i<s.length;i++)
      if (phoneChars.indexOf(s.charAt(i))<0) return false;

    return true;
}

function gwf_formatPhone(s)
{
    return s;
}

// DOMAIN: ZIP ---------------------------------------------------
function gwf_isZip(s)
{
    return s.length == 5 && gwf_isNumeric(s);
}

function gwf_formatZip(s)
{
    return s;
}

// DOMAIN: NAME ---------------------------------------------------
function gwf_isName(s)
{
    var nameChars = gwf_digits + gwf_uppercaseLetters + gwf_lowercaseLetters +
                    "' -";
    var i;
    for (i=0;i<s.length;i++)
      if (nameChars.indexOf(s.charAt(i))<0) return false;

    return true;

}

function gwf_formatName(s)
{
    return s;
}

// DOMAIN: IP ---------------------------------------------------
function gwf_isIP(realstring)
{
   var pos = -1,oldpos=-1;
   var number;
   var strNumber = " ";
   var s = realstring+".";
   var i=0;
   for(i=0;i<4;i++)
   {

    oldpos = pos+1;
    pos = s.indexOf(".",oldpos);

    if (pos<0) return false;
    strNumber = s.substring(oldpos,pos);

    if (!gwf_isNumeric(strNumber)) return false; //prevents parseInt from accepting strings like '1x'
    number = parseInt(strNumber);

    if ( isNaN(number) || number<0 || number>255) return false;
   }

   return true;
}

function gwf_formatIP(s)
{
    return s;
}


// DOMAIN: PERCENTAGE ---------------------------------------------------
function gwf_isPercentage(s)
{
  return s.length<=2 && gwf_isNumeric(s);
}

function gwf_formatPercentage(s)
{
    return s;
}



// DOMAIN: INTEGER ---------------------------------------------------
function gwf_isInteger (s)
{
  var i;
  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if (!gwf_isDigit(c))
        {
          if (gwf_debugFlag)  alert("Valore del campo non valido!");
          return false;
        }
  }
  var val = parseInt(s);
  if ( isNaN(val) )
  {
    if (gwf_debugFlag) alert("Valore del campo non valido!");
    return false;
  }
  else
  {
    if (gwf_debugFlag) alert("Immissione dato corretta!");
    return true;
  }
}

function gwf_formatInteger(s)
{
  return s;
}

// DOMAIN: POSITIVEINTEGER ---------------------------------------------------
function gwf_isPositiveInteger (s)
{
  return gwf_isIntegerInRange(s, 1, 1000000);
}

function gwf_formatPositiveInteger(s)
{
  return s;
}

// DOMAIN: NONNEGATIVEINTEGER ---------------------------------------------------
function gwf_isNonNegativeInteger (s)
{
  var retVal =  gwf_isIntegerInRange(s, 0, 1000000);

  return retVal;
}

function gwf_formatNonNegativeInteger(s)
{
  return s;
}

// DOMAIN: INTEGERINRANGE ---------------------------------------------------
function gwf_isIntegerInRange(s, low, high)
{
  var value = gwf_isInteger(s);
  if (value)
  {
     var iValue = parseInt(s);
     if (iValue>=low && iValue<=high)  return true;
  }
  return false;
}

function gwf_formatIntegerInRange(s, low, high)
{
  return s;
}


// DOMAIN: FLOAT ---------------------------------------------------
function gwf_isFloat (s)
{
  var i;
  var goodDelimiter = ".";
  var badDelimiter  = ",";
  var seenDelimiter = false;
  var tmpStr = "";

  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if ( (c == goodDelimiter) && !seenDelimiter)
    {
      seenDelimiter = true;
      tmpStr = tmpStr + c;
    }
    else if ((c == badDelimiter) && !seenDelimiter)
         {
           c = goodDelimiter;
           tmpStr = tmpStr + c;
           seenDelimiter = true;
         }
    else if (!gwf_isDigit(c))
         {
           if (gwf_debugFlag) alert("Valore del campo non valido!");
           return false;
         }
         else tmpStr = tmpStr + c;
  }
  // il tipo č quello giusto!
  // procedo alla formattazione prima di restituire il valore
  var val = parseFloat(tmpStr);
  if ( isNaN(val) )
  {
    if (gwf_debugFlag) alert("Valore del campo non valido!");
    return false;
  }
  else
  {
    if (gwf_debugFlag) alert("Immissione dato corretta!");
    return true;
  }
}

function gwf_formatFloat (s)
{
  var i;
  var goodDelimiter = ".";
  var badDelimiter  = ",";
  var tmpStr = "";

  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if ( (c == badDelimiter) )
    {
      c = goodDelimiter;
    }

    tmpStr = tmpStr + c;
  }

  return tmpStr;
}

// DOMAIN: EURO ---------------------------------------------------
function gwf_isEuro(s)
{
  var i;
  var goodDelimiter = ".";
  var seenDelimiter = false;
  var tmpStr = "";
  var index = 0;
  var indexcomma = 0;
  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    index ++;
    if ( (c == goodDelimiter) )
    {
      if (!seenDelimiter)
      {
        seenDelimiter = true;
        tmpStr = tmpStr + c;
      }
      else
      {
        // valori tipo ".80."
        if (gwf_debugFlag) alert("Valore del campo non valido!");
        return false;
      }
    }
    else
    {
      if (!gwf_isDigit(c))
      {
        // valori tipo "a"
        if (gwf_debugFlag) alert("Valore del campo non valido!");
        return false;
      }
      else
      {
        if (seenDelimiter)
        {
          indexcomma ++;
          //prompt ("c= " + c +" indexcomma= " + indexcomma);
          if (indexcomma > 2)
          {
            // valori tipo "1.345"
            if (gwf_debugFlag) alert("Valore del campo non valido, sono ammesse al pių 2 cifre dopo la virgola!");
            return false;
          }
        }
        tmpStr = tmpStr + c;
      }
    }
  }

  return true;
}


// Alessio

// al valore     restituisce     il valore:
//    X              ->            X.00
//    X.Y            ->            X.Y0
//    .              ->            0.00
//    .XY            ->            0.XY
//    X.YZW          ->            X.YZ
function gwf_formatEuro (s)
{
  var i;
  var goodDelimiter = ".";
  var seenDelimiter = false;
  var tmpStr = "";
  var index = 0;
  var indexcomma = 0;
  for (i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    index ++;
    if ( (c == goodDelimiter) )
    {
      if (!seenDelimiter)
      {
        seenDelimiter = true;
        tmpStr = tmpStr + c;
      }
      else
      {
        // valori tipo ".80."
        if (gwf_debugFlag) alert("Valore del campo non valido!");
        return null;
      }
    }
    else
    {
      if (!gwf_isDigit(c))
      {
        // valori tipo "a"
        if (gwf_debugFlag) alert("Valore del campo non valido!");
        return null;
      }
      else
      {
        if (seenDelimiter)
        {
          indexcomma ++;
          //prompt ("c= " + c +" indexcomma= " + indexcomma);
          if (indexcomma > 2)
          {
            // valori tipo "1.345" -> ritorna 1.34
            if (gwf_debugFlag) alert("Valore del campo non valido!");
            return tmpStr;
          }
        }
        tmpStr = tmpStr + c;
      }
    }
  }

  if (c == goodDelimiter)
  {
    // valori tipo "1" -> ritorna "1.00"
    tmpStr = tmpStr + "00";
    return tmpStr;
  }

  if (!seenDelimiter)
  {
    // valori tipo "1" -> ritorna "1.00"
    tmpStr = tmpStr + ".00";
    return tmpStr;
  }

  if (indexcomma == 1)
  {
    // valori tipo "1.6" -> ritorna "1.60"
    tmpStr = tmpStr + "0";

    return tmpStr;
  }

  return tmpStr;
}

// DOMAIN: DATE ---------------------------------------------------
function gwf_isValidDate(s)
{
	 
  // determines if the date string passed represents a valid date.
  // returns 0 if the date is valid
  // returns 1 if the date is not in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
  //      m/d/ccyy & m-d-ccyy are also acceptable
  // returns 2 if the date is not a legal date (i.e. 02/30/1999)
  var retval = 0
  var aMMDDCCYY
  var dtest
  // use a regular expression pattern match to determine if the date format is valid
  if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(s))
  {
    //dtest = new Date(s);
    if (/\//.test(s))
    {
      aMMDDCCYY = s.split("/");
    }
    else
    {
      if (/-/.test(s))
      {
        aMMDDCCYY = s.split("-");
      }
      else
      {
        aMMDDCCYY = Array(s.substr(0,2), s.substr(2,2), s.substr(4,4))
      }
    }
    dtest = new Date(aMMDDCCYY[1] + "/" + aMMDDCCYY[0] + "/" + aMMDDCCYY[2]);
    //alert("dTest returned: " + dtest);
    if (dtest.getMonth() + 1 != aMMDDCCYY[1] || dtest.getDate() != aMMDDCCYY[0] || dtest.getFullYear() != aMMDDCCYY[2])
    {
      retval = 2
    }
  }
  else
  {
    retval = 1
  }
  return retval
}

function gwf_StdDateFormat(s)
{
  // receives a date string in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
  // returns a date string in the standard format: mm/dd/ccyy.
  var retval = ""
  var aMMDDCCYY
  var gg
  var mm

  if (gwf_isValidDate(s) == 0)
  {
    //lasciare 2 volte
    s = s.replace(".", "-");
    s = s.replace(".", "-");
    s = s.replace("/", "-");
    s = s.replace("/", "-");

    if (/-/.test(s))
	{
		aMMDDCCYY = s.split("-");
		gg = aMMDDCCYY[0];
		mm = aMMDDCCYY[1];
		if(gg.length == 1 ) gg = "0" + gg;
		if(mm.length == 1 ) mm = "0" + mm;
		retval = gg + "/" + mm + "/" + aMMDDCCYY[2];
	}
	else
	{
		retval = s.substr(0,2) + "/" + s.substr(2,2) + "/" + s.substr(4,4);
	}
  }

  return retval;
}

//richiama le due funzioni precedenti
function gwf_isDate(s)
{
  var goodDate = "";
  	
  goodDate = gwf_StdDateFormat(s);


  var val = "";
  val = gwf_isValidDate(s);
 
  if (val!=0)
  {
    if (gwf_debugFlag) alert("Valore del campo non valido!");
    return false;
  }
  else
  {
    if (gwf_debugFlag) alert ("Immissione corretta!");
    if (gwf_debugFlag) alert("Formato corretto: " +goodDate);
  }
  return true;
}

function gwf_formatDate(s)
{
  return gwf_StdDateFormat(s);
}

// The date received is supposed in the format: dd/MM/YYYY
function gwf_formatDate2YYYYMMdd(s)
{
  var aDDMMYYYY;
  aDDMMYYYY = s.split("/");
  return aDDMMYYYY[2] + aDDMMYYYY[1] + aDDMMYYYY[0];
}

// The date received is supposed in the format: dd/MM/YYYY HH:mm
function gwf_formatDateTime2YYYYMMdd_HHmmss(s)
{
  var dateAndTime = s.split(" ");
  
  // The date received might miss the time part.
  if (dateAndTime.length == 1)
  {
     var aDDMMYYYY;
     aDDMMYYYY = s.split("/");
     return aDDMMYYYY[2] + aDDMMYYYY[1] + aDDMMYYYY[0];
  }

  var aDDMMYYYY;
  aDDMMYYYY = dateAndTime[0].split("/");
  return aDDMMYYYY[2] + aDDMMYYYY[1] + aDDMMYYYY[0] + " " + dateAndTime[1];
}

function gwf_OrderedDateFormat(s)
{
  // receives a date string in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
  // returns a date string in the standard format: mm/dd/ccyy.
  var retval = ""
  var aDDMMCCYY
        var mm
        var dd

  if (gwf_isValidDate(s) == 0)
  {
    if (/\//.test(s))
    {
       aDDMMCCYY = s.split("/");
       retval = (aDDMMCCYY[2] + "/" + aDDMMCCYY[1] + "/" + aDDMMCCYY[0]);
    }
    else
    {
      if (/-/.test(s))
      {
        aDDMMCCYY = s.split("-");
      }
      else
      {
        aDDMMCCYY = Array(dd, mm, s.substr(4,4));
      }
      retval = (aDDMMCCYY[2] + "/" + aDDMMCCYY[1] + "/" + aDDMMCCYY[0]);
    }

    // if ( s.substr(0,2).indexOf("/")>0) mm = "0" + s.charAt(0);
    // else mm = s.substr(0,2);
    //
    // temp = s.indexOf("/");
    //
    // if (s.substr(temp+1,2).indexOf("/")>0) dd = "0" + s.charAt(temp+1);
    // else dd = s.substr(temp+1,2);
    //
    // retval = (  s.substr(s.lastIndexOf("/")+1) + "/" + mm + "/" + dd);

  }

  return retval;
}

// This function checks to see if the provided values describe a regular interval or
// not. The parameters passed are supposed to be two fields defined in the <gwf_form:date> tag
// since the check assumes the availability of hidden field in the format YYYYmmdd or YYYYmmdd hh:mm:ss
// good for comparison.
function gwf_isInterval(fieldDateFrom, fieldDateTo)
{
   fieldFrom = eval(fieldDateFrom.form.name + '.' + fieldDateFrom.name +  '_assystem');
   fieldTo   = eval(fieldDateTo.form.name + '.' + fieldDateTo.name +  '_assystem');
   if (gwf_isEmpty(fieldFrom.value) || gwf_isEmpty(fieldTo.value))  return true;
   if (fieldTo.value < fieldFrom.value)
   {
      return false;
   }

   return true;
}

// DOMAIN: TIME ---------------------------------------------------
// Functions for TIME in the form HH:mm.
function gwf_isTime(s, round)
{
  // determines if the time string passed represents a valid time.
  // returns true/false if the time is valid/invalid
  var retval = false;
  var HHmm
  var h;
  var m;
  var s;

  // use a regular expression pattern match to determine if the Time format is valid
  // if (/^(\d\d?)([\.:]\d\d?)?|(\d\d?\.\d\d?)$/.test(s))
  if (/^(\d\d?)([\.:]\d\d?)?([\.:]\d\d?)?$/.test(s))
  {
    if (/\./.test(s))
    {
      HHmm = s.split(".");
    }
    else
    {
      if (/:/.test(s))
      {
        HHmm = s.split(":");
      }
      else
      {
        HHmm = new Array(s);
      }
    }

    if (HHmm.length > 0) h = parseInt(HHmm[0], 10);
    else                 h = 0;
    
    if (HHmm.length > 1) m = parseInt(HHmm[1], 10);
    else                 m = 0;
    
    if (HHmm.length > 2) s = parseInt(HHmm[2], 10);
    else                 s = 0;

    if (h > 23)   retval = false;
    if (m > 59)   retval = false;
    if (s > 59)   retval = false;

    if (h < 10)  h = "0" + h;
    if (m < 10)  m = "0" + m;
    if (s < 10)  s = "0" + s;
    
    // alert("#" + h + "#" + m + "#" + s + "#");
    retval = true;
  }
  else
  {
    retval = false;
  }

  return retval
}

// Functions for TIME in the form HH:mm.
function gwf_formatTime(s, round)
{
  var HHmm
  var h;
  var m;
  var s;

  if (/\./.test(s))
  {
      HHmm = s.split(".");
  }
  else
  {
      if (/:/.test(s))
      {
        HHmm = s.split(":");
      }
      else
      {
        HHmm = new Array(s);
      }
  }

  if (HHmm.length > 0) h = parseInt(HHmm[0], 10);
  else                 h = 0;
  
  if (HHmm.length > 1) m = parseInt(HHmm[1], 10);
  else                 m = 0;
  
  if (HHmm.length > 2) s = parseInt(HHmm[2], 10);
  else                 s = 0;

  if (h < 10)  h = "0" + h;

  if (round > 0) m = m - (m % round);
  if (m < 10)  m = "0" + m;

  if (s < 10)  s = "0" + s;

  return h + ":" + m + ":" + s;
}

// Functions for TIME in the form HH:mm.
function gwf_fromTimeToMin(s)
{
  var h;
  var m;
  var s;

  if (/\./.test(s))
  {
      HHmm = s.split(".");
  }
  else
  {
      if (/:/.test(s))
      {
        HHmm = s.split(":");
      }
      else
      {
        HHmm = new Array(s);
      }
  }

  if (HHmm.length > 0) h = parseInt(HHmm[0], 10);
  else                 h = 0;
  
  if (HHmm.length > 1) m = parseInt(HHmm[1], 10);
  else                 m = 0;
  
  if (HHmm.length > 2) s = parseInt(HHmm[2], 10);
  else                 s = 0;

  if (gwf_debugFlag) alert("Returning: " + (h * 60) + m);
  return (h * 60) + m;
}

//********************** FUNCTIONS FOR RADIOS  AND CHECKS *********************
function gwf_isRadioSelected(radio_group)
{
 var ok = false;

 if (radio_group.length) {
     for (var i = 0; i < radio_group.length; i++)
     {
       if (radio_group[i].checked)
       {
         ok = true;
         break;
       }
     }
 }
 else  if (radio_group.checked)  ok = true;
 return ok;
}

function gwf_radioSelectedValue(radio_group)
{
 var res = false;

 if (radio_group.length) {
     for (var i = 0; i < radio_group.length; i++)
     {
       if (radio_group[i].checked)
       {
         res = radio_group[i].value;
         break;
       }
     }
 }
 else  if (radio_group.checked)  res = radio_group.value;
 return res;
}

function gwf_radioSelectedTitle(radio_group)
{
 var res = false;

 if (radio_group.length) {
     for (var i = 0; i < radio_group.length; i++)
     {
       if (radio_group[i].checked)
       {
         res = radio_group[i].title;
         break;
       }
     }
 }
 else  if (radio_group.checked)  res = radio_group.title;
 return res;
}

//conta quanti checkbox, appartenenti ad uno stesso gruppo(stesso nome), sono checked
function gwf_numberOfSelections(checkbox_group)
{
 var num = 0;

 if (checkbox_group.length) {
    for (var i = 0; i < checkbox_group.length; i++)
    {
       if (checkbox_group[i].checked)  num++;
    }
 }
 else  if (checkbox_group.checked)     num++;
 return num;
}

function gwf_listSelection(combo)
{
 for (var i = 0; i < combo.options.length; i++)
 {
    if (combo.options[i].selected)  return combo.options[i].value;
 }
 return false;
}

