/////////////////////////////////////////////////////////////////////////////////////
// Common JavaScript functions
//    Author: Enrique G. Villegas
//      Date: 07/22/2005
// Important:
// 1. The following line must be included in the html to be able to access these functions:
//    <script type="text/javascript" src="../script/operation.js"></script>
// 2. Place a copy of this script file in the "/script" folder. The hierarchy of the
//    folder must have the same level where the html folder is located.
//    For example:
//            ..\PPoE\Page_1.htm
//            ..\script\operation.js
/////////////////////////////////////////////////////////////////////////////////////
function displayNoSelectedMsg() {
  alert ('Error: No entry selected. Please select a table entry.');
}

function displayOperationFailedMsg() {
  alert("Error: Operation failed.");
}


function displayCommand(command) {
  // Important: This function is for debug purpose.
  // Comment out this line before the release.
  debug(command);
}


function debug(value) {
  // Important: This function is for debug purpose.
  // Comment out this line before the release.
  //alert(value);
  }

function isOperationOk() {
  // This function handles errors in an operation
  return true;
}

function getSelectedItem(listRadioItems) {
  if(listRadioItems==null) {
    debug("Radio list object is null.");
    return "";
  }
  // This condition assumes that the radio list is not an array
  if(!(listRadioItems.length>0)) {
     if(listRadioItems.checked) {
       return listRadioItems.value;
     }
     displayNoSelectedMsg();
     return "";
  }

  // Assumes that the radio list is an array
  var selectedItem = "";
  for (var index=0 ; index<listRadioItems.length; index++) {
   if(listRadioItems[index].checked) {
     selectedItem = listRadioItems[index].value;
     break;
   }
  }

  if (selectedItem=="") {
    displayNoSelectedMsg();
  }
  return selectedItem;
}



function getSelectedIndex(listRadioItems) {
  if(listRadioItems==null) {
    debug("Radio list object is null.");
    return "";
  }
  // This condition assumes that the radio list is not an array
  if(!(listRadioItems.length>0)) {
     if(listRadioItems.checked) {
       return 1;
     }
     displayNoSelectedMsg();
     return "";
  }

  // Assumes that the radio list is an array
  var selectedItem = "";
  for (var index=0 ; index<listRadioItems.length; index++) {
   if(listRadioItems[index].checked) {
     selectedItem = index + 1;
     break;
   }
  }
  if (selectedItem=="") {
    displayNoSelectedMsg();
  }
  return selectedItem;
}

function sendCLINoPrompt(frmExecute, command, htmlFileName) {
  command += "\n+" + htmlFileName;
  frmExecute.elements[0].value = command;
  displayCommand(command);
  frmExecute.submit();
}

function sendCLI(frmExecute, command, htmlFileName) {
  if (needPrompt(command)) {
    if (confirm("Are you sure?")==false) {
      return;
    }
  }

  command += "\n+" + htmlFileName;
  frmExecute.elements[0].value = command;
  displayCommand(command);
  frmExecute.submit();
}

function sendCLIwParam(frmExecute, htmlFileName, command,  listRadioItems) {
  var commandParam = getSelectedItem(listRadioItems);
  if(commandParam=="") {
    return;
  }
  sendCLI(frmExecute, command + " " + commandParam, htmlFileName);
}

function sendCLIwParamColOne(frmExecute, htmlFileName, command,  listRadioItems) {
  var commandParam = getSelectedItem(listRadioItems);
  if(commandParam=="") {
    return;
  }

  var iPos = commandParam.lastIndexOf(".");
  commandParam = commandParam.substring(iPos+1);
  sendCLI(frmExecute, command + " " + commandParam, htmlFileName);
}

function sendCLIwParamParsedInt(frmExecute, htmlFileName, command,  listRadioItems) {
  var commandParam = getSelectedIndex(listRadioItems);
  if(commandParam=="") {
    return;
  }
  sendCLI(frmExecute, command + " " + commandParam, htmlFileName);
}


function forwardPage(formArgs,listRadioItems,htmlFileName) {
  var selectedItem = getSelectedItem(listRadioItems);
  if (selectedItem=="") {
    return;
  }
  formArgs.elements[1].value=htmlFileName;
  formArgs.elements[2].value=selectedItem;
  debug("forwarding to page "+htmlFileName + "\n" +
        "Parameter:"+selectedItem);
  formArgs.submit();
}


function needPrompt(strCommand) {
  var bRet = false;
  var arrM = new Array("delete","clear","restart", "enable", "disable", "system config set");
  for (i=0;i<arrM.length;i++){
    if( strCommand.indexOf(arrM[i]+ " ") >= 0 &&
        strCommand.indexOf("pppoe set transport ") == -1 &&
        strCommand.indexOf("voip sip protocol set ") == -1 &&
        strCommand.indexOf("voip mgcp protocol set ") == -1 &&
        strCommand.indexOf("mpeg-mon enable ") == -1 ) { // with suffix " "
      bRet = true;
      break;
    } else if( strCommand.indexOf(" "+arrM[i]) >= 0 &&
               strCommand.indexOf("pppoe set transport ") == -1 &&
               strCommand.indexOf("voip sip protocol set ") == -1  &&
               strCommand.indexOf("voip mgcp protocol set ") == -1 &&
               strCommand.indexOf("mpeg-mon enable ") == -1 ) { // with prefix " "
      bRet = true;
      break;
    }
  }
  return bRet;
}

function isDeviceRG656(deviceCode) {

  var deviceCriterion = deviceCode;
  if (parseInt(deviceCode)<10) {
    deviceCriterion = "0" + deviceCode;
  }

  var rx656 = " 06 17 18 19 22";
  var bRet = false;
  if (rx656.indexOf(deviceCriterion)>0) {
    bRet = true;
  }
  return bRet;
}


function getNAT(interfacevalue,addressvalue) {
  var value = "";
  if(interfacevalue!="") {
    value = interfacevalue;
  }

  if(addressvalue!="") {
    value = addressvalue;
  }
  return value;
}

function isEmptyNAT(NATValue) {
  var bRet = false;
  if (NATValue!="") {
    bRet = true;
  }
  return bRet;
}
