// BEGIN $Id: //mms/mms46X/apps/subscriber/src/main/webapp/docroot/lcommon/html/common/common.js#3 $

// This file contains shared MMS apps Javascript.
//
// The including page can also define a "var debug=true" to turn on
// the debugging during development. This will cause debug output to
// be sent to a pop up dialog.

// We need to at least define the debug var or it will cause errors in browsers like IE 6
var debug;
var ie5=(document.getElementById&&navigator.appName.indexOf("Internet Explorer")!=-1);

function hint(s) {
  window.status = (s ? s : '');
  return true;
}

// Check validation of the given form and submit form if valid.
// Allows specification of the action URI to invoke.
// Used in onclick handlers of non-submit buttons used to submit a form
// where different buttons on the same page submit to different actions.
function goSetAction(form, action, validate) {
    if (validate) {
        isValid = eval("validate" + form + "(document.forms['" + form + "'])");
        if (!isValid) {
            return;
        }
    }
    document.forms[form].action = action;
    document.forms[form].submit();
    return;
}

// Check validation of the given form and submit form if valid.
// Used in onclick handler of non-submit buttons used to submit a form.
function go(form, validate) {
    if (validate) {
        isValid = eval("validate" + form + "(document.forms['" + form + "'])");
        if (!isValid) {
            return;
        }
    }
    document.forms[form].submit();
    return;
}

// Check validation of the given form and return true if valid,
// else false.  Used in onclick handler of submit buttons.
function goSubmitButton(form) {
   isValid = eval("validate" + form + "(document.forms['" + form + "'])");
   if (!isValid) {
       return false;
   }
   return true;
}

function goToLink(id) {
    var anchor = document.getElementById(id);
    document.location.href = anchor.href;
}

function goToURL(url) {
    document.location.href = url;
}

var defaultSelectAll="selectall";
var defaultTarget="selected";

  // toggle all the selectable items
  //
  // form - form to check and do sumit if there are selected items
  // field - name of field to check, default is "selectall"
  // target - name of field to set, default is "selected"
  //
  function selectallChanged(form, selectall, target) {

  if (debug) {alert("selectallChanged: form name="+form.name); };
    var fieldName;
    var targetName;
    if (null==selectall) {
        fieldName=defaultSelectAll;
    } else {
        fieldName=selectall;
    }
    if (null==target) {
        targetName=defaultTarget;
    } else {
        targetName=target;
    }
  if (debug) {alert("form.elements["+fieldName+"].checked="+form.elements[fieldName].checked); };

    if (form.elements[fieldName].checked == true) {
      setAll(form, true, targetName);
    } else {
      setAll(form, false, targetName);
    }
  }

  // select all the selectable items
  //
  // form - form to check and do sumit if there are selected items
  // field - name of field to check, default is "select"
  //
  function setAll(form, flag, field) {
 if (debug) {alert("setAll: "+flag); };
    var fieldName;
    if (null==field) {
        fieldName=defaultTarget;
    } else {
        fieldName=field;
    }
    for (i=0; i< form.elements.length; i++) {
      if (form.elements[i].name==fieldName) {
        form.elements[i].checked = flag;
      }
    }
  }

  function updateSelectAll(form) {
    var fieldName = 'selected';
    var selectAllFieldName = 'selectall';
    var flag = true;
    for (i=0; i < form.elements.length; i++) {
      if (form.elements[i].name == fieldName) {
        if (form.elements[i].checked == false) {
          if (debug) {alert("Form element checked" + form.elements[i].checked) }
          flag = false;
        }
      }
    }
    form.elements[selectAllFieldName].checked = flag;
  }

  // core function for doSubmit methods
  // returns true if form submit can proceed
  function doSubmitCore(noItemError, noneSelectedError, form, field) {
    var fieldName;
    if (debug)
    {
        alert("doSubmit form="+form.name + "\nfield="+field);
    }
    if (null==field) {
        fieldName="selected";
    } else {
        fieldName=field;
    }
    var numselected = 0;
    var noItems = true;
    for (i=0; i< form.elements.length; i++) {
      if (form.elements[i].name==fieldName) {
        noItems = false;
        if (form.elements[i].checked == true) {
          numselected++;
        }
      }
    }
    if (noItems ) {
      alert(noItemError);
      return false;
    } else {
      if (numselected == 0) {
        alert(noneSelectedError);
        return false;
      }
    }
    return true;
  }

  // submit the form if there are selected items
  //
  // noItemError - error message if there are no selectable items
  // noneSelectedError - error message if no items has been selected
  // form - form to check and do sumit if there are selected items
  // field - name of field to check, default is "select"
  //
  function doSubmit(noItemError, noneSelectedError, form, field) {
    if ( doSubmitCore(noItemError, noneSelectedError, form, field) == false) return;
    if (debug) { alert("submitting form");}
    form.submit();
  }

  // submit the form if there are selected items and after confirmation
  //
  // noItemError - error message if there are no selectable items
  // noneSelectedError - error message if no items has been selected
  // form - form to check and do sumit if there are selected items
  // field - name of field to check, default is "select"
  //
  function doSubmitConfirm(noItemError, noneSelectedError, confirmMesg, form, field) {
    // check first to see if anything selected
    if ( doSubmitCore(noItemError, noneSelectedError, form, field) == false) return;
    // then confirm submit
    if (confirm(confirmMesg)) {
      if (debug) { alert("submitting form");}
      form.submit();
    }
  }

  // return true to submit the form if there are selected items and after confirmation
  //
  // noItemError - error message if there are no selectable items
  // noneSelectedError - error message if no items has been selected
  // form - form to check and do sumit if there are selected items
  // field - name of field to check, default is "select"
  //
  function doConfirm(noItemError, noneSelectedError, confirmMesg, form, field) {
    // check first to see if anything selected
    if ( doSubmitCore(noItemError, noneSelectedError, form, field) == false) return false;
    // then confirm submit
    if (confirm(confirmMesg)) {
      if (debug) { alert("submitting form");}
      return true;
    }
    return false;
  }

  // submit the form if there are selected items, then perform usual validation on the form
  //
  // noItemError - error message if there are no selectable items
  // noneSelectedError - error message if no items has been selected
  // form - form to check and do sumit if there are selected items
  // field - name of field to check, default is "select"
  //
  function doSubmitValidate(noItemError, noneSelectedError, form, field) {
    if ( doSubmitCore(noItemError, noneSelectedError, form, field) == false) return;
    if (debug) { alert("submitting form");}
    // submit form with validation
    go(form.name, true);
  }

  function trimWhiteSpace(strText) {
    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
      strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while(strText.substring(strText.length-1,strText.length) == ' ')
      strText = strText.substring(0,strText.length-1);

    return strText;
  }

  // return the file name from a file path
  //
  function getFileNameFromPath(filepath) {
    var filename = '';
    if (filepath.indexOf('/') > -1)
        filename = filepath.substring(filepath.lastIndexOf('/')+1,filepath.length);
    else
        filename = filepath.substring(filepath.lastIndexOf('\\')+1,filepath.length);
    return filename;
  }

  // return the display name from a file path by extracting the base name
  //
  function getDisplayNameFromPath(filepath) {
    return getFileNameFromPath(filepath);
  }

  // dynamic menus
  var menuIdArray = new Array();

  function addMenuIdToArray(id) {
    for(i = 0; i < menuIdArray.length; i++) {
      var menuId = menuIdArray[i];
      if (menuId == id) {
        return;
      }
    }
    menuIdArray[menuIdArray.length] = id;
  }

  function hideAllMenus() {
    for(i = 0; i < menuIdArray.length; i++) {
      var menuId = menuIdArray[i];
      var comboMenu = document.getElementById(menuId);
      if ( comboMenu != null ) {
        comboMenu.style.visibility = "hidden";
      }
    }
  }

  function toggleMenu(id){
    var comboMenu=document.getElementById(id);
    if (comboMenu != null) {
      var menuStyle = comboMenu.style;
      if (menuStyle.visibility == "" || menuStyle.visibility == "hidden") {
        hideAllMenus();
        menuStyle.visibility = "visible";
        addMenuIdToArray(id);
      } else {
        menuStyle.visibility = "hidden";
      }
    }
  }

  // Function called when clicking anywhere on the page
  function bodyOnClick() {
      return hideAllMenus();
  }
