var debug = false;// set this var to false to hide alert if sometime is wrong!

function getById(idElement) {return document.getElementById(idElement);}
function getsByName(name) {return document.getElementsByName(name);}
function openElement(idElement) {setDisplayBlock(idElement);}
function closeElement(idElement) {setDisplayNone(idElement);}

function setInnerHTML(idElement, text) {
  var obj = getById(idElement);
  if (obj==null){
    if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
    return false;
  } 
  obj.innerHTML=text;
  return true;
}

function getInnerHTML(idElement) {
  var obj = getById(idElement);
  if (obj==null) {
    if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
    return "";
  }
  return obj.innerHTML;   
} 

function setDisplayInLine(idElement) {
  var obj=getById(idElement);
  if(obj!= null)obj.style.display = "inline";
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

function setDisplayBlock(idElement) {
  var obj = getById(idElement);
  if( obj != null )
    obj.style.display = "block";
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

function setDisplayNone(idElement) {
  var obj = getById(idElement);
  if( obj != null )
    obj.style.display = "none";
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

function setTitleAttr(idElement, value) {
  var obj = getById(idElement);
  if( obj != null )
    obj.title = value;
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}


function clearTextBox(idElement) {
  var obj = getById(idElement);
  if(obj!=null){
    obj.value = ""; 
    obj.focus();
  }
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}
function setTextBox(idElement, value, color) {
  var obj = getById(idElement);
  if(obj!=null) {
    if(color==null) color='#000000';
    obj.style.color=color;
    obj.value = value;
  }
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

function getObjValue(idElement) {
  var obj = getById(idElement);
  if( obj != null )
    return obj.value;
  else if( debug )
    alert('DEBUG: object with id [' + idElement + '] is null');
}

function getSelectValue(idElement) {
  var obj = getById(idElement);
  if( obj != null )
    return obj.options[obj.selectedIndex].value;
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

function getRadioValue(nameElement) {
  var objCollection = getsByName(nameElement);
  if( objCollection != null ) {
    for(i=0; i < objCollection.length; i++) {
      if(objCollection[i].checked)
        return objCollection[i].value;
    }    
  }    
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

function setObjValue(idElement, value) {
  var obj=getById(idElement);
  if(obj!=null)
    obj.value = value;
  else if(debug) alert('DEBUG: object with id [' + idElement + '] is null');
}

// To find the left position;
function getPositionLeft(This) {
  var el = This;
  var pL = 0;
  while(el) {
    pL += el.offsetLeft;
    el = el.offsetParent;
  }
  return (pL)
}

// To find the top position;
function getPositionTop(This) {
  var el = This;
  var pT = 0;
  while(el) {
    pT += el.offsetTop;
    el = el.offsetParent;
  }
  return (pT)
}

function copyToClipboard(textToCopy) {
  if (window.clipboardData)
    window.clipboardData.setData("Text", textToCopy);
  else if (window.netscape) { 
    alert("copyToClipboard function works only with IE");
    return false;
  }
}

//Example: <font color=”#008000?>greeting = formatString(’Hello {0} & {1} ‘, ‘John’, ‘Jane’);</font>
function formatString(str) {
  for(i = 1; i < arguments.length; i++) { 
    str = str.replace("{" + (i - 1) + "}", arguments[i]);  
  }
  return str; 
}

// time is an integer representing seconds
function getFormattedTime(time){
  if(time == null)return("");
  
  var lblHour   = MESSAGES['lbl.hours'];
  var lblMinute = MESSAGES['lbl.minutes'];
  var lblSecond = MESSAGES['lbl.seconds'];
  
  if(time > 60) {                    
    var seconds = time % 60;       
    var minutes = (time - seconds) / 60;
    
    if(minutes > 60){                     
      var minLeft = minutes % 60;                   
      var hours   = (minutes - minLeft) / 60; 
      
      if(hours == 1)
        lblHour = MESSAGES['lbl.hour'];  
      if(minLeft == 1)
        lblMinute = MESSAGES['lbl.minute'];  
      if(seconds == 1)
        lblSecond = MESSAGES['lbl.second'];
      
      return(hours + " " + lblHour + ", " + minLeft + " " + lblMinute + ", " + seconds + " " + lblSecond);   
    }
    else {   
      if(minutes == 1)
        lblMinute = MESSAGES['lbl.minute'];  
      if(seconds == 1)
        lblSecond = MESSAGES['lbl.second'];
      return(minutes + " " + lblMinute + ", " +  seconds + " " + lblSecond);   
    }   
  }
  else {   
    if(time==1) lblSecond=MESSAGES['lbl.second'];
    return(time + " " + lblSecond);
  }   
}

//############# GESTIONE COOKIE #####
function getCookie(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++) {
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) 
      return (unescape(asCookie[1]));
  } 
  // SE non esiste il cookie richiesto
  return("");
}

function setCookie(sNome, sValore, iGiorni) {
  var dtExpires = new Date();
  dtExpires.setTime(new Date().getTime() + 24 * iGiorni * 3600000);
  document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
}

function delCookie(sNome) {setCookie(sNome, "");}
//####### FINE GESTIONE COOKIE #####

//String1 stringa in cui devi cercare la String2;
function inStr(String1, String2) {
  var a = 0;
  
  if (String1 == null || String2 == null)
    return (false);
  
  String1 = String1.toLowerCase();
  String2 = String2.toLowerCase();
  
  a = String1.indexOf(String2);
  if (a == -1)
    return 0;
  else
    return a + 1;
}

function isAlphaNumeric(value) {
  var flag = true;
  for(var j=0; j<value.length; j++) {
    var alphaa = value.charAt(j);
    var hh = alphaa.charCodeAt(0);
    if((hh > 47 && hh < 58))
      continue;
    else
      flag = false;
  }
  return flag;
}  