
/**
 * simple browser detection.
 */

var isOpera = navigator.userAgent.indexOf("Opera") > -1;
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;

/**
 * popup to confirm a deletion.
 *
 */

function confirmDelete( form, conditions, message ) {

}

/**
 * popUp a window given the URL
 *
 * @param URL
 * @param window name
 * @param width of window
 * @param height"
 */

function popUp (webpage, windowName, width, height) {
  window.open (webpage,
               windowName,
               "scrollbars=yes,width="+width+", height="+height);
}

/**
 * popup quickhelp for the given ID.
 *
 * @param id (lang string)
 */

function quickhelp( id ) {
  var width = 400;
  var height = 200;
  var windowprops = "left=50,top=50,scrollbars,resizable,width=" + width + ",height=" + height;
  preview = window.open("quickhelp.php?id=" + id, "NodeworxQuickhelp", windowprops);
}

/**
 * highlight the bad forms elements on the page.
 *
 * @param list of elements.
 */
function highlightBadFormElem( /* variable no. of arguments */ ) {
  if( arguments.length < 1 ) return;

  for( var i = 0; i < arguments.length; i++ ) {
    // Figure out if we were passed the object itself, or the object
    // name

    if( typeof( arguments[i] ) == 'string' ) {
      elem = document.getElementById( arguments[i] );

      if( elem != null ) {
        elem.className = 'bad_form_element';
      }
    }
  }

  // Focus on the first element
  if( typeof( arguments[0] ) == 'string' )
    document.getElementById( arguments[0] ).focus();
  else
    arguments[0].focus();
}

/**
 * set checkboxes in the given form
 * to checked or unchecked.
 *
 * @param form
 * @param checkbox gropu name
 * @param true = checked.
 */

function setCheckboxes(formname, boxname, checkit) {

  boxes = document.forms[ formname ].elements[ boxname ];

  if( !isArray( boxes ) ) {
    if( !boxes.disabled ) {
      boxes.checked = checkit;
    }
  } else {
    for( var i = 0; i < boxes.length; i++ ) {
      if( !boxes[i].disabled ) {
        boxes[i].checked = checkit;
      }
    }
  }

  return( false );
}


function textboxSelect (oTextbox, iStart, iEnd) {
  switch(arguments.length) {
  case 1:
    oTextbox.select();
    break;

  case 2:
    iEnd = oTextbox.value.length;
    /* falls through */

  case 3:
    if (isIE) {
      var oRange = oTextbox.createTextRange();
      oRange.moveStart("character", iStart);
      oRange.moveEnd("character", -oTextbox.value.length + iEnd);
      oRange.select();
    } else if (isMoz){
      oTextbox.setSelectionRange(iStart, iEnd);
    }
  }

  oTextbox.focus();
}

function textboxReplaceSelect (oTextbox, sText) {

  if (isIE) {
    var oRange = document.selection.createRange();
    oRange.text = sText;
    oRange.collapse(true);
    oRange.select();
  } else if (isMoz) {
    var iStart = oTextbox.selectionStart;
    oTextbox.value = oTextbox.value.substring(0, iStart) + sText + oTextbox.value.substring(oTextbox.selectionEnd, oTextbox.value.length);
    oTextbox.setSelectionRange(iStart + sText.length, iStart + sText.length);
  }

  oTextbox.focus();
}

function autocompleteMatch (sText, arrValues) {

    for (var i=0; i < arrValues.length; i++) {
      if (arrValues[i].indexOf(sText) == 0) {
        return arrValues[i];
      }
    }

    return null;
}

function autocomplete(oTextbox, oEvent, arrValues) {

  switch (oEvent.keyCode) {
  case 38: //up arrow
  case 40: //down arrow
  case 37: //left arrow
  case 39: //right arrow
  case 33: //page up
  case 34: //page down
  case 36: //home
  case 35: //end
  case 13: //enter
  case 9:  //tab
  case 27: //esc
  case 16: //shift
  case 17: //ctrl
  case 18: //alt
  case 20: //caps lock
  case 8:  //backspace
  case 46: //delete
    return true;
    break;

  default:
    textboxReplaceSelect(oTextbox, String.fromCharCode(isIE ? oEvent.keyCode : oEvent.charCode));
    var iLen = oTextbox.value.length;

    var sMatch = autocompleteMatch(oTextbox.value, arrValues);

    if (sMatch != null) {
      oTextbox.value = sMatch;
      textboxSelect(oTextbox, iLen, oTextbox.value.length);
    }

    return false;
  }
}

/***** MENU SHIZZLE *****/

/**
 * make sure menus start hidden.
 */

if (document.getElementById ) {
  document.write('<style type="text/css">\n');
  document.write('.submenu{display: none;}\n');
  document.write('</style>\n');
}

/**
 * toggle a menu on/off.
 *
 * @param menu to toggle.
 */

function toggleMenu(obj){
  if(document.getElementById){
    var el = document.getElementById(obj);
    var ar = document.getElementById("menu").getElementsByTagName("span");

    if( el.style.display != "block" ) {
      for (var i=0; i<ar.length; i++) {
        if (ar[i].className=="submenu") {
          //ar[i].style.display = "none";
        }
      }
      el.style.display = "block";
    } else {
      el.style.display = "none";
    }
  }

  return( false );
}

/**
 * init the menus from the cookie so
 * we can save menu state on page
 * transitions.
 */

function initMenu(){
  var cookiename  = "iworx_cp_menu=";

  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf( cookiename );
    if (offset != -1) {
      offset += cookiename.length;
      end    = document.cookie.indexOf(";", offset);
      if( end == -1 ) {
        end = document.cookie.length;
      }
      value = unescape( document.cookie.substring(offset, end) );
      value = value.split( ":" );

      for( i = 0; i < value.length; i++ ) {
        if( document.getElementById( value[ i ] ) ) {
          document.getElementById( value[ i ] ).style.display = "block";
        }
      }
    }
  }
}

/**
 * save the menu state.
 */

function saveMenuState(){
  var sub    = 1;
  var blocks = "";

  // NOTE: CLWELLS: THIS IS GAY BUT SINCE OUR MENU GETS SHIT
  // ADDED TO IT AT A SLOW PACE IT SHOULD HOLD US FOR A WHILE.
  // THIS IS NEEDED SINCE PREVIOUSLY WE NEEDED TO HAVE 
  // SEQUENTIAL subX NUMBRE IN THERE OR STATE WOULDN'T BE SAVED
  // AFTER THE SEQUENCE WAS BROKEN.

  while( sub < 20 ) {
    if( document.getElementById( "sub" +sub ) &&
        document.getElementById( "sub" +sub ).style.display == "block" ) {
      blocks = blocks + "sub" + sub + ":";
    }
    sub++;
  }

  var cookiename = "iworx_cp_menu";
  var cookievalue= blocks + ";path=/";

  document.cookie = cookiename+"="+cookievalue;
}

/**
 * init the menu so we're consistent over pages.
 */

if( window.addEventListener ) {
  window.addEventListener("load", initMenu, false);
} else if( window.attachEvent ) {
  window.attachEvent("onload", initMenu);
} else if( document.getElementById ) {
  window.onload=initMenu;
}

if(document.getElementById) {
  window.onunload=saveMenuState;
}


