function isNetscape()
{
  isnetscape = false;
  if(navigator.appName.substring(0,8) == "Netscape" )
  {
    isnetscape = true;
  }
  return isnetscape;
}

function myDecodeURIComponent(textURIEncoded)
{
  value = textURIEncoded;
  try
  {
    value = decodeURIComponent(textURIEncoded);
  }
  catch(error)
  {
    value = textURIEncoded;
  }
  return value;
}

function cancellaData(formDati, nomeCampoData)
{
  elementi = formDati.elements;
  if((elementi != null) && (elementi != undefined))
  {
    for(i = 0; i < elementi.length; i++)
    {
      if((elementi[i] != null) && (elementi[i] != undefined) && (!isEmpty(elementi[i].name)))
      {  
        if(elementi[i].name.indexOf(nomeCampoData) == 0)
        {
          elementi[i].value = "";
        }
      }  
    }
  }
}

function setNavigation(theForm, navigatorName, valore)
{
  var elementi = theForm.elements;
  elementName = navigatorName + ".thisPage"
  for(i = 0; i < elementi.length; i++)
  {
    if(elementi[i].name.indexOf(elementName) == 0)
    {
      elementi[i].value = myDecodeURIComponent(valore);
      break;
    }      
  }
  theForm.submit();
}

function setNavigatorPage(theForm, navigatorName, valore)
{
  elementName = navigatorName + "_currentPage";
  obj = theForm.elements.namedItem(elementName);
  if((obj != null) && (obj != undefined) && (!isEmpty(obj.name)))
  {  
    obj.value = myDecodeURIComponent(valore);
  }
  theForm.submit();
}

function selectOption(select, value)
{
  if((select != null) && (! isEmpty(value)))
  {    
    value = myDecodeURIComponent(value);
    elements = select.options;
    if((elements != null) && (elements.length > 0))
    {
      for(i = 0; i < elements.length; i++)
      {
        if(value == elements[i].value)
        {  
          select.selectedIndex = i;
          break;  
        }  
      }
    }
  }
}

function selectMultipleOption(select, values, valueseparator)
{
  if((select != null) && (! isEmpty(values)))
  {    
    select.selectedIndex = -1;
    values = myDecodeURIComponent(values);
    vals = tokenizeString(values, valueseparator, false);
    if((vals != null) && (vals != undefined) && (vals.length > 0))
    {
      elements = select.options;      
      if((elements != null) && (elements.length > 0))
      {
        for(i = 0; i < vals.length; i++)
        {
          for(j = 0; j < elements.length; j++)
          {
            if(vals[i] == elements[j].value)
            {  
              elements[j].selected = true;
              break;
            }
          }
        }
      }
    }
  }
}

function selectOptionByID(select, IDvalue)
{
  if((select != null) && (! isEmpty(IDvalue)))
  {    
    IDvalue = myDecodeURIComponent(IDvalue);
    elements = select.options;
    if((elements != null) && (elements.length > 0))
    {
      for(i = 0; i < elements.length; i++)
      {
        if(IDvalue == elements[i].id)
        {  
          select.selectedIndex = i;
          break;  
        }  
      }
    }
  }
}

function selectOptionByIdPart(select, IDPartvalue)
{
  if((select != null) && (! isEmpty(IDPartvalue)))
  {    
    IDvalue = myDecodeURIComponent(IDPartvalue);
    elements = select.options;
    if((elements != null) && (elements.length > 0))
    {
      for(i = 0; i < elements.length; i++)
      {
        if(elements[i].id.indexOf(IDvalue) >= 0)
        {  
          select.selectedIndex = i;
          break;  
        }  
      }
    }
  }
}

function selectOptionRounding(select, valore)
{
  if(!isEmpty(valore))
  {
    value = parseInt(valore);
    elements = select.options;
    size = elements.length;      
    idx = 0;
    for(i = 0; i < size; i++)
    {
      if(elements[i].value >= value)
      {
        idx = i;
        break;
      }
    }
    if((idx >= 0) && (idx < size))
    {
      value = elements[idx].value;
      selectOption(select, value);
    }
  }
}

function setTextValue(text, value)
{ 
  if((text != null) && (! isEmpty(value)))
  {    
    value = myDecodeURIComponent(value);
    if(text.value != null)
    {
      text.value = value;
    }
  }
}

function selectCheckBox(checkbox, value)
{
  if((checkbox != null) && (! isEmpty(value)))
  {    
    if(checkbox.value != null)
    {      
      value = Trim(value).toLowerCase();
      if((value == '1') || (value == 'true') || (value == 'yes') || (value == 'si'))
      {
        checkbox.checked = true;
      }
    }
  }
}


function getSelectedRadio(radioButtons)
{
  selected = null;
  if((radioButtons != null) && (radioButtons != undefined))
  {     
    if(eNumero(radioButtons.length))
    {
      for(i = 0; i < radioButtons.length; i++)
      {
        if(radioButtons[i].checked == true)
        {
          selected = radioButtons[i];
          break;
        }
      }
    }
    else if((radioButtons.value != null) && (radioButtons.value != undefined)) // E' uno solo
    {
      if(radioButtons.checked == true)
      {      
        selected = radioButtons;
      }
    }
  }
  return selected;
}

function selectRadioByValue(checkbox, value)
{
  if((checkbox != null) && (! isEmpty(value)))
  {    
    if(checkbox.value != null)
    {
      value = Trim(myDecodeURIComponent(value)).toLowerCase();
      if(Trim(checkbox.value).toLowerCase() == value)
      {
        checkbox.checked = true;
      }
    }
  }
}

function selectMultipleRadioByValue(radioarray, value)
{
  
  if((radioarray != null) && (! isEmpty(value)))
  {    
    if(!eNumero(radioarray.value))
    {
      for(i = 0; i < radioarray.length; i++)
      {
        selectRadioByValue(radioarray[i], value);
      }
    }
    else
    {
      selectRadioByValue(radioarray, value);
    }
  }
}

function LTrim(text)
{
  if(text == null || text.length == 0)
  {
    return "";      
  }      
  idx = 0;
  text = myDecodeURIComponent(text);
  while(text.charAt(idx) == ' ')
  {
    ++idx;
    if(idx >= text.length)
      break; 
  }
  if(idx < text.length)
  {
    return text.substring(idx);
  }
  else  
    return "";
}

function RTrim(text)
{
  if(text == null || text.length == 0)
  {
    return "";      
  }
  text = myDecodeURIComponent(text);
  idx = text.length - 1;
  while(text.charAt(idx) == ' ')
  {
    --idx;
    if(idx < 0)
      break;
  }
  if(idx >= 0)
  {
    return text.substring(0, (idx + 1));
  }    
  else  
    return "";
}

function Trim(text)
{
  testo = LTrim(text);
  testo = RTrim(testo);
  return testo;
}

function isEmpty(text)
{
  if(text == null)
  {
    return true;  
  }
  else
  {
    testo = Trim(text);
    if(testo.length == 0)
    {
      return true;
    }
    else
      return false;
  }
}

function rendiNumero(text)
{
  text = Trim(myDecodeURIComponent(text));
  if(text.indexOf(",") >= 0)
  {
    text = text.replace(".", "");
    text = text.replace(",", ".");
  }
  while((text.indexOf("0") == 0) && (text.length > 1) && ((text.indexOf(".") < 0) || (text.indexOf(".") > 1)))
  {
    text = text.substring(1);
  }
  return Trim(text);
}

function eNumero(text)
{
  text = myDecodeURIComponent(text);
  if(isEmpty(text))
  {
    return false;  
  }
  else
  {        
    testo = rendiNumero(text);
    ok = (!isNaN(testo));
    ok = (ok && isFinite(testo));  
    return ok;
  }
}

function openPopupAtPosition(iTop, iLeft, width, height, redirect) 
{
  innerITop = iTop;
  innerILeft = iLeft;  
  windowOuterHeight = window.outerHeight;
  windowOuterWidth = window.outerWidth;
  if(isEmpty(innerITop) || (!eNumero(innerITop)) || (parseInt('' + innerITop) < 0))
  {
    if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
    {    
      windowOuterHeight = window.screen.height;
      if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
      {      
        windowOuterHeight = height;
      }      
    }
    innerITop = Math.round((windowOuterHeight / 2) - (height / 2));
  }
  if(isEmpty(innerILeft) || (!eNumero(innerILeft)) || (parseInt('' + innerILeft) < 0))
  {
    if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
    {      
      windowOuterWidth = window.screen.width;
      if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
      {      
        windowOuterWidth = width;
      }
    }
    innerILeft = Math.round((windowOuterWidth / 2) - (width / 2));
  }

  var OpenWindow = myDecodeURIComponent(redirect);
  var params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top='+innerITop+',left='+innerILeft
  popup = window.open(OpenWindow, "Popup", params);
}

function openPopupAtPositionWithStatus(iTop, iLeft, width, height, redirect) 
{
  innerITop = iTop;
  innerILeft = iLeft;  
  windowOuterHeight = window.outerHeight;
  windowOuterWidth = window.outerWidth;
  if(isEmpty(innerITop) || (!eNumero(innerITop)) || (parseInt('' + innerITop) < 0))
  {
    if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
    {    
      windowOuterHeight = window.screen.height;
      if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
      {      
        windowOuterHeight = height;
      }      
    }
    innerITop = Math.round((windowOuterHeight / 2) - (height / 2));
  }
  if(isEmpty(innerILeft) || (!eNumero(innerILeft)) || (parseInt('' + innerILeft) < 0))
  {
    if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
    {      
      windowOuterWidth = window.screen.width;
      if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
      {      
        windowOuterWidth = width;
      }
    }
    innerILeft = Math.round((windowOuterWidth / 2) - (width / 2));
  }

  var OpenWindow = myDecodeURIComponent(redirect);
  var params = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top='+innerITop+',left='+innerILeft
  popup = window.open(OpenWindow, "Popup", params);
}

function openPopupAtPositionNoScrollbar(iTop, iLeft, width, height, redirect) 
{
  innerITop = iTop;
  innerILeft = iLeft;  
  windowOuterHeight = window.outerHeight;
  windowOuterWidth = window.outerWidth;
  if(isEmpty(innerITop) || (!eNumero(innerITop)) || (parseInt('' + innerITop) < 0))
  {
    if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
    {    
      windowOuterHeight = window.screen.height;
      if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
      {      
        windowOuterHeight = height;
      }      
    }
    innerITop = Math.round((windowOuterHeight / 2) - (height / 2));
  }
  if(isEmpty(innerILeft) || (!eNumero(innerILeft)) || (parseInt('' + innerILeft) < 0))
  {
    if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
    {      
      windowOuterWidth = window.screen.width;
      if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
      {      
        windowOuterWidth = width;
      }
    }
    innerILeft = Math.round((windowOuterWidth / 2) - (width / 2));
  }

  var OpenWindow = myDecodeURIComponent(redirect);
  var params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height + ',left=' + innerILeft + ',top=' + innerITop;
  popup = window.open(OpenWindow, "Popup", params);
}

function openPopupAtPositionNoScrollbarResizable(iTop, iLeft, width, height, redirect) 
{
  innerITop = iTop;
  innerILeft = iLeft;  
  windowOuterHeight = window.outerHeight;
  windowOuterWidth = window.outerWidth;
  if(isEmpty(innerITop) || (!eNumero(innerITop)) || (parseInt('' + innerITop) < 0))
  {
    if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
    {    
      windowOuterHeight = window.screen.height;
      if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
      {      
        windowOuterHeight = height;
      }      
    }
    innerITop = Math.round((windowOuterHeight / 2) - (height / 2));
  }
  if(isEmpty(innerILeft) || (!eNumero(innerILeft)) || (parseInt('' + innerILeft) < 0))
  {
    if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
    {      
      windowOuterWidth = window.screen.width;
      if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
      {      
        windowOuterWidth = width;
      }
    }
    innerILeft = Math.round((windowOuterWidth / 2) - (width / 2));
  }

  var OpenWindow = myDecodeURIComponent(redirect);
  var params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=' + width + ',height=' + height + ',top='+innerITop+',left='+innerILeft
  popup = window.open(OpenWindow, "Popup", params);
}

function openPopupAtPositionScrollbarNoResizable(iTop, iLeft, width, height, redirect) 
{
  innerITop = iTop;
  innerILeft = iLeft;  
  windowOuterHeight = window.outerHeight;
  windowOuterWidth = window.outerWidth;
  if(isEmpty(innerITop) || (!eNumero(innerITop)) || (parseInt('' + innerITop) < 0))
  {
    if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
    {    
      windowOuterHeight = window.screen.height;
      if(isEmpty(windowOuterHeight) || (!eNumero(windowOuterHeight)))
      {      
        windowOuterHeight = height;
      }      
    }
    innerITop = Math.round((windowOuterHeight / 2) - (height / 2));
  }
  if(isEmpty(innerILeft) || (!eNumero(innerILeft)) || (parseInt('' + innerILeft) < 0))
  {
    if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
    {      
      windowOuterWidth = window.screen.width;
      if(isEmpty(windowOuterWidth) || (!eNumero(windowOuterWidth)))
      {      
        windowOuterWidth = width;
      }
    }
    innerILeft = Math.round((windowOuterWidth / 2) - (width / 2));
  }

  var OpenWindow = redirect;
  var params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=' + width + ',height=' + height + ',top='+innerITop+',left='+innerILeft
  popup = window.open(OpenWindow, "Popup", params);
}

function changeImageSRC(imageObj, src)
{
  if((imageObj != null) && (imageObj != undefined) && (!isEmpty(src)))  
  {
    imageObj.src = src;
  }
}

function isValidEmailAddress(indirizzo) 
{
  if(isEmpty(indirizzo))
  {
    return false;
  }
  else
  {
    indirizzo = myDecodeURIComponent(indirizzo);
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

function isValidItalianTaxNumber(cf) 
{
  if(isEmpty(cf))
  {
    return false;
  }
  else
  {
    cf = myDecodeURIComponent(cf);
    var valido = "";
    var regv = null;

    if (cf.length == 16) // codice fiscale
    {
      regv = new RegExp(/[A-Za-z]{6}\d{2}[A-Za-z]\d{2}[A-Za-z]\d{3}[A-Za-z]$/);
    }
    else if (cf.length == 11)// partita iva
    {
      regv = new RegExp(/\d{11}/);
    }
    else
    {
      return false;
    }
    if (regv.test(cf))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

function getValidDate(dd, mm, yyyy, hh, mi, ss, ms)
{
  var date = null;
  try
  {
    if(isEmpty(hh))
    {
      hh = "0";
    }
    if(isEmpty(mi))
    {
      mi = "0";
    }
    if(isEmpty(ss))
    {
      ss = "0";
    }
    if(isEmpty(ms))
    {
      ms = "0";
    }
    if(
      eNumero(dd) && eNumero(mm) && eNumero(yyyy) && 
      eNumero(hh) && eNumero(mi) && eNumero(ss) && eNumero(ms)
    )
    {
      if(isValidDate(dd, mm, yyyy))
      {
        date = new Date(parseInt(yyyy), (parseInt(mm) - 1), parseInt(dd), parseInt(hh), parseInt(mi), parseInt(ss), parseInt(ms));
      }
    }
  }
  catch(exception)
  {    
    date = null;
  }
  return date;
}

function meseBisestile(mese, anno)
{
  bisestile = false;
  if((mese == 2) && ((((anno % 4) == 0) && ((anno % 100) != 0)) ||  ((anno % 400) == 0)))
  {
    bisestile = true;
  }
  return bisestile;
}

function isValidDate(dd, mm, yyyy)
{
  valid = eNumero(dd);  
  valid = valid && eNumero(mm);
  valid = valid && eNumero(yyyy);
  if(valid)
  {
    day = parseInt(rendiNumero(dd));
    month = parseInt(rendiNumero(mm)) - 1;
    year = parseInt(rendiNumero(yyyy));
    valid = valid && (day >= 1) && (day <= 31);
    valid = valid && (month >= 0) && (month <= 11);
    valid = valid && (year > 1900);  
    if(valid)
    {
      var date = new Date(year, month, day, 0, 0, 1);
      valid = valid && (date.getDate() == day);
      valid = valid && (date.getMonth() == month);
      valid = valid && (date.getFullYear() == year);  
    }
  }
  return valid;
}

function tokenizeString(value, separator, returnseparator)
{
  tokens = new Array();
  if(!isEmpty(value))
  {
    i = 0;
    if(!isEmpty(separator))
    {
      idxStart = 0;
      idxEnd = value.indexOf(separator);
      token = "";
      while((idxStart >= 0) && (idxEnd >= 0) && (idxStart < value.length) && (idxEnd < value.length))
      {        
        tokens[i++] = Trim(value.substring(idxStart, idxEnd));
        if(returnseparator)
        {
          tokens[i++] = separator;
        }
        idxStart = idxEnd + 1;
        idxEnd = value.indexOf(separator, idxStart);
      }
      if((idxEnd < idxStart) && (idxStart < value.length))
      {
        tokens[i++] = Trim(value.substring(idxStart));        
      }
    }
    else
    {
      tokens[i++] = value;
    }
  }
  return tokens;
}