
function writeGatewayInterface(gateway,interface) {
  var text = "";
  if (gateway=="0.0.0.0") {
    text = interface;
  } else {
    text = gateway;
  }
  debug("write:"+text);
  document.write(text);
}


function writeHhMmSs(sec) {
  var hrs = Math.floor(sec/3600);
  var min = Math.floor((sec%3600)/60);
  sec = sec % 60;

  var years = "0 years, ";
  var days = 0;
  if (hrs>23) {
    days = Math.round(hrs/24);
    hrs = Math.floor(hrs%24);
  }

  if(days>0 && days<10) {
    days = "00"+days+" days, ";
  } else if(days>0 && days<100) {
    days = "0"+days+" days, ";
  } else if(days>364) {
    years = Math.round(days/365) + " years, ";
    days = Math.floor(days%365) + " days, ";
  }


  if(hrs<10) hrs = "0" + hrs;
  if(sec<10) sec = "0" + sec;
  if(min<10) min = "0" + min;
  var text = hrs + ":" + min + ":" + sec;
  if (days!=0) {
    //text = days + hrs + ":" + min + ":" + sec;
    text = years + days + hrs + ":" + min + ":" + sec;
  }

  debug("write:"+text);
  document.write(text);
}


function writeRowRadio(radioName,txtName,localIp,ipAddrFromIPCP) {

  var ipValue = "";
  var staticChecked = "";
  var dynamicChecked = " CHECKED ";
  if (localIp!="0.0.0.0") {
    ipValue = localIp;
    staticChecked = " CHECKED ";
    dynamicChecked = "";
  }

  var text = "<" + "TR" + ">";
     text += "<" + "TD class='section-cell'" + ">" + "&nbsp;&nbsp;&nbsp;";
     text += "<" + "input type='radio' name=" + radioName + staticChecked + ">" + "Static";
     text += "<" + "/TD" + ">";
     text += "<" + "TD class='section-cell'" + ">";
     text += "<" + "INPUT TYPE='text' NAME=" + txtName + " value=" + ipValue + ">";
     text += "<" + "/TD" + ">";
     text += "<" + "/TR" + ">";
     text += "<" + "TR" + ">";
     text += "<" + "TD class='section-cell'" + ">" + "&nbsp;&nbsp;&nbsp;";
     text += "<" + "input type='radio' name=" + radioName + dynamicChecked + " value=" + ipAddrFromIPCP + ">" + "Dynamic" ;
     text += "<" + "/TD" + ">";
     text += "<" + "TD class='section-cell'" + ">" + "&nbsp;" + "<" + "/TD" + ">";
     text += "<" + "/TR" + ">";
  debug("write:"+text);
  document.write(text);
}



function getInterface(ph) {
  debug("ph:"+ph);
  if (ph=="ph0") {
    return "tel1";
  } else if (ph=="ph1") {
    return "tel2";
  } else if (ph=="ph2") {
    return "tel3";
  }
}

function getConfigStatus(activated) {
  debug("activated:"+activated);
  if(activated=="true") {
    return "Configured";
  } else if(activated=="false") {
    return "Disabled";
  } else {
    return "Un-Configured";
  }
}


function writePhoneInterfaces(Tel,ph,activated,address,state) {

   debug("ph:"+ph[0]+ "  activated:"+activated[0]+" address:"+address[0]+"  state:"+state[0] + " Tel[0]:"+Tel[0]);

   var text = "<"+"TR onMouseOver='SelectRow(this, true)' onMouseOut='SelectRow(this, false)'" +">";
   text += "<"+"TD class='section-cell'" +">" +Tel[0]+"<" +"/TD" +">";
   text += "<"+"TD class='section-cell'" +">" +getConfigStatus(activated[0])      +"<" +"/TD" +">";
   text += "<"+"TD class='section-cell'" +">" +address[0]        + "&nbsp;" + "<" +"/TD" +">";
   text += "<"+"TD class='section-cell'" +">" +state[0]          + "&nbsp;" + "<" +"/TD" +">";
   text += "<"+"/TR"+">"

   debug("ph:"+ph[1]+ "  activated:"+activated[1]+" address:"+address[1]+"  state:"+state[1] + " Tel[1]:"+Tel[1]);
   text += "<"+"TR onMouseOver='SelectRow(this, true)' onMouseOut='SelectRow(this, false)'" +">";
   text += "<"+"TD class='section-cell'" +">" +Tel[1]+"<" +"/TD" +">";
   text += "<"+"TD class='section-cell'" +">" +getConfigStatus(activated[1])      +"<" +"/TD" +">";
   text += "<"+"TD class='section-cell'" +">" +address[1]        + "&nbsp;" + "<" +"/TD" +">";
   text += "<"+"TD class='section-cell'" +">" +state[1]          + "&nbsp;" + "<" +"/TD" +">";
   text += "<"+"/TR"+">"

   if( Tel.length==3 ) {
     debug("ph:"+ph[2]+ "  activated:"+activated[2]+" address:"+address[2]+"  state:"+state[2]+ " Tel[2]:"+Tel[2]);
     text += "<"+"TR onMouseOver='SelectRow(this, true)' onMouseOut='SelectRow(this, false)'" +">";
     text += "<"+"TD class='section-cell'" +">" +Tel[2]+"<" +"/TD" +">";
     text += "<"+"TD class='section-cell'" +">" +getConfigStatus(activated[2])      +"<" +"/TD" +">";
     text += "<"+"TD class='section-cell'" +">" +address[2]        + "&nbsp;" + "<" +"/TD" +">";
     text += "<"+"TD class='section-cell'" +">" +state[2]          + "&nbsp;" + "<" +"/TD" +">";
     text += "<"+"/TR"+">"
   }

   debug("write:"+text);
   document.write(text);
}


function writeComboAgeingTime(name,value) {
  var text = "";
  var selNorm = " selected ";
  var selFast = " ";
  if (value=="fast") {
   selNorm = " ";
   selFast = " selected ";
  }

  text += "<" + "SELECT NAME="           + name    + ">";
  text += "<" + "OPTION value='normal' " + selNorm + ">" + "normal" + "<" + "/OPTION" + ">";
  text += "<" + "OPTION value='fast' "   + selFast + ">" + "fast"   + "<" + "/OPTION" + ">";
  text += "<" + "/SELECT" + ">";

  debug("write:"+text);
  document.write(text);
}

function writeTextAgeingTime(name,value) {
  var text = "";

  // ARGA-104: If the Ageing Timer value is null or blank, set it to 300
  if ( value =="" ) {
    value = 300;
  }
  text = "<" + "input type='text' maxlength='6' name=" + name + " value=" + value + ">";
  debug("write:"+text);
  document.write(text);
}


function writeGwaMgcpGwaPort( radioName, arrayImGwaMgcpTel,
                        arrayImGwaPortName,
                        arrayImGwaPortTypology,
                        arrayImGwaPortCountry,
                        arrayImGwaPortCodecs,
                        arrayImGwaPortPhysicalIF,
                        parameterFromRadio ) {

  for ( var i=0; i<arrayImGwaPortName.length; i++ ) {
     var portname     = !(arrayImGwaPortName.length>0)       ? arrayImGwaPortName:arrayImGwaPortName[i]
     var physicalport = !(arrayImGwaPortPhysicalIF.length>0) ? arrayImGwaPortPhysicalIF:arrayImGwaPortPhysicalIF[i]
     var type         = !(arrayImGwaPortTypology.length>0)   ? arrayImGwaPortTypology:arrayImGwaPortTypology[i]
     var country      = !(arrayImGwaPortCountry.length>0)    ? arrayImGwaPortCountry:arrayImGwaPortCountry[i]
     var codecs       = !(arrayImGwaPortCodecs.length>0)     ? arrayImGwaPortCodecs:arrayImGwaPortCodecs[i]



     var arraymgcp    = !(arrayImGwaMgcpTel.length>0)        ? arrayImGwaMgcpTel:arrayImGwaMgcpTel[0]
     var arraytel     = arraymgcp.split(",");
     var address      = "";

     if ( physicalport.indexOf("0")>0 ) {
        address = arraytel[0];
     } else if ( physicalport.indexOf("1")>0 ) {
        address = arraytel[1];
     } else if ( physicalport.indexOf("2")>0 ) {
        address = arraytel[2];
     } else if ( physicalport.indexOf("3")>0 ) {
        address = arraytel[3];
     }

     var text = "";
     text += "<" + "TR onMouseOver='SelectRow(this, true)' onMouseOut='SelectRow(this, false)'" + ">"
     text += "<" + "TD class='section-cell'" + ">" + "&nbsp;"
     text += "<" + "input type='radio' name=" + radioName + " value=\"" + portname + "\"" + ">"
     text += "<" + "/TD" + ">"
     text += "<" + "TD  class='section-cell'"  + ">" + "&nbsp;" + unTag(address)
     text += "<" + "/TD" + ">"
     text += "<" + "TD class='section-cell'"  + ">" + "&nbsp;"
     text += "<" + "/TD" + ">"
     text += "<" + "TD class='section-cell'" + ">" + "&nbsp;"


     debug("parameterFromRadio:"+parameterFromRadio);

     physicalport = physicalport.replace("ph0","tel1");
     physicalport = physicalport.replace("ph1","tel2");
     physicalport = physicalport.replace("ph2","tel3");

// ARGA-130:  NOTE, the variable "parameterFromRadio" is always fixed to contain
//            the path of the first VoIP Node (ImGwaPorts.<node1>).  To address
//            ARGA-130, this variable is no longer used.
//     text += "<" + "A CLASS='PORTLINK' onClick=" + "\"configurePort('" + parameterFromRadio + "')\"" + ">"
     text += "<" + "U" + ">" + "<" + "A CLASS='PORTLINK' onClick=" + "\"configurePort('ImGwaPorts." + portname + "')\"" + ">"
                 + physicalport + "<" + "/A" + ">" + "<" + "/U" + ">"
     text += "<" + "/TD" + ">"
     text += "<" + "TD class='section-cell'" + ">" + "&nbsp;" + type
     text += "<" + "/TD" + ">"
     text += "<" + "TD  class='section-cell'" + ">" + "&nbsp;" + country
     text += "<" + "/TD" + ">"
     text += "<" + "TD  class='section-cell'" + ">" + "&nbsp;" + codecs
     text += "<" + "/TD" + ">"
     text += "<" + "/TR" + ">"


     if (!( address=="" && type=="" && country=="" && codecs=="" )) {
       debug("write:"+text);
       document.write(text);
     }


  }

}

// This function replaces any HTML characters with the appropriate HTML
// escape characters to prevent them from messing up the HTML display.
// This function was created to address ARGA-193
function replaceTag ( strText ) {
  var strFiltered = unTag ( strText );
  document.write ( strFiltered );
}

function unTag ( strText ) {
  var strFiltered = strText.replace(/&/g,"&amp;");
  strFiltered = strFiltered.replace(/</g,"&lt;");
  strFiltered = strFiltered.replace(/>/g,"&gt;");
  return strFiltered;
}
