/*
Copyright (c) 2006 The Broad Institute

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
   $Author: sbrudz $
   $Date: 2006-12-12 15:12:35 -0500 (Tue, 12 Dec 2006) $
   $Id: ChemBank.js 2457 2006-12-12 20:12:35Z sbrudz $
*/

/****************************** Toolbar Scripts *******************************/
var toolbarId = "toolbar";
var toolbarHolderId = "main_toolbar";
var headerId = "main_header";
var headerBottom = 0;

var toolbar = null;
var toolbarPane = null;

function getToolbar() {
  if (toolbar == null) toolbar = document.getElementById(toolbarId);
  return toolbar;
}

function getToolbarPane() {
  if (toolbarPane == null) toolbarPane=document.getElementById(toolbarHolderId);
  return toolbarPane;
}

function getHeaderBottom() {
   if (headerBottom == 0) {
      var header = document.getElementById(headerId);
      headerBottom = header.offsetTop + header.offsetHeight;
   }
   return headerBottom;
}

function toolbarSetSelected(elementId) {
   var bar = getToolbar();
   if (bar != null) {
      for (i = 0; i < bar.childNodes.length; i++) {
         var node = bar.childNodes[i];
         if (node.nodeName == "LI") { node.className = "off"; }
      }
   }
   document.getElementById(elementId).className = "on";
}

function findScrollTop() {
   if (window.pageYOffset != null) return window.pageYOffset;
   if (document.body.scrollWidth != null) return document.body.scrollTop;
   return (null);
}

function moveToolbar() {
   var y = findScrollTop();
   if (y < getHeaderBottom()) {
      y = getHeaderBottom() + 1;
      if (navigator.appName == "Microsoft Internet Explorer") y += 19;
   }
   var bar = getToolbarPane();
   bar.style.display = 'none';
   bar.style.top = y + 'px';
   bar.style.display = 'block';
}

// This is here because of an IE bug.
function toolbarStartList() {
   if (navigator.appName != "Microsoft Internet Explorer") return;
   var bar = getToolbar();
   if (bar != null) {
       for (i = 0; i < bar.childNodes.length; i++) {
         var node = bar.childNodes[i];
         if (node.nodeName == "LI") {
            node.onmouseover = function() {
               if (this.className != "on") this.className = "over";
            }
            node.onmouseout = function() {
               if (this.className == "over") this.className = "off";
            }
         }
      }
   }
}

/******************************** Form Scripts ********************************/
function executeFormAction(button,action) {
	var element = button;
	// walk up the dom tree to find the parent form
	while ((element = xParent(element,true)) != null) {
	  if (element.action && element.submit) {
	    // this must be the form
	    element.action = action;
    }
  }
}

function hoverField(me) {
   if(me.className != "textSelected") {
      if (me.className == "text") me.className = "textHover";
      else me.className = "textFilledHover";
   }
}

function hoverOffField(me) {
   if (me.className != "textSelected") {
      if (me.className == "textHover") me.className = "text";
      else me.className = "textFilled";
   }
}

function focusField(me) {
   if (me.className.indexOf("text") == 0) me.className = "textSelected";
   else me.className = "selected";
}

function blurField(me) {
   if (me.className.indexOf("text") == 0) {
      if (me.value.length > 0) me.className = "textFilled";
      else me.className = "text";
   }
   else me.className = "filled";
}

function nextField(me, form) {
   if (me.value.length == me.maxLength) {
      var next = me.tabIndex;
      if (next < form.elements.length) form.elements[next].focus();
   }
}

function submitenter(myfield,e) {
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	else 
		return true;

	if (keycode == 13) {
		myfield.form.submit();
		return false;
	}

	return true;
}

/********************** Highlighting a row in a table form ********************/
function selectRow(node) {
   var row = node.parentNode.parentNode;
   if (row.className == "") row.className = "selected";
   else if (row.className.indexOf(" selected") > -1) {
      row.className = row.className.replace(" selected", "");
   }
   else if (row.className.indexOf("selected") > -1) {
      row.className = row.className.replace("selected", "");
   }
   else row.className = row.className + " selected";
}

function unselectRows(node) {
   for (i = 0; i < node.childNodes.length; i++) {
      if (node.childNodes[i].nodeName == "TABLE") {
         var table = node.childNodes[i];
         for (j = 0; j < table.childNodes.length; j++) {
            if (table.childNodes[j].nodeName == "TBODY") {
               var tbody = table.childNodes[j];
               for (k = 0; k < tbody.childNodes.length; k++) {
                  if (tbody.childNodes[k].nodeName == "TR") {
                     var row = tbody.childNodes[k];
                     if (row.className.indexOf(" selected") > -1) {
                        row.className = row.className.replace(" selected", "");
                     }
                     else if (row.className.indexOf("selected") > -1) {
                        row.className = row.className.replace("selected", "");
                     }
                  }
               }
            }
         }
      }
   }
}
   
/********************************* Utility Functions ***********************/

function getRadioButtonValue(radio)
{
  for (var i = 0; i < radio.length; i++) {
    if (radio[i].checked == true) return radio[i].value;
  }
  return null;
}

function setRadioAndSelect(value, selectItem, radioItem, other1, other2) {
  // if the value is in the radioItem, then select that
  // radio button and disable the selectItem and any others provided
  // to the function.  Otherwise select the last radioItem
  selectItem.disabled = false;
  radioItem[0].checked = true;
  for (i=0; i<radioItem.length; i++) {
    if (radioItem[i].value == value) {
      radioItem[i].checked = true;
      selectItem.disabled = true;
      if (other1 != null) other1.disabled = true;
      if (other2 != null) other2.disabled = true;
      break;
    }
  }
  // if nothing matched in the radio list, select the last one and look for
  // the value in the selection list
  if (selectItem.disabled == false) {
	radioItem[radioItem.length-1].checked = true;
	for (i=0; i<selectItem.options.length; i++) {
	  if (selectItem.options[i].value == value) {
	    selectItem.options[i].selected = true;
	    break;
	  }
	}
  }
} 

function setRadio(radioItem, value) {
  radioItem[0].checked = true;
  for (i=0; i<radioItem.length; i++) {
    if (radioItem[i].value == value) {
      radioItem[i].checked = true;
    }
  }
}

function debugMessage(aMessage) {
  alert(aMessage);
}

function writeToPopupWindow(content) {
 top.consoleRef=window.open('','myconsole',
  'lwidth=600,height=50'
   +',menubar=0'
   +',toolbar=0'
   +',status=0'
   +',location=0'
   +',scrollbars=1'
   +',resizable=1')
 top.consoleRef.document.writeln(
  '<html><head><title>Console</title></head>'
   +'<body 	style="font-family: serif; font-size: 12px;" bgcolor=white onLoad="self.focus()">'
   +content
   +'</body></html>'
 )
 top.consoleRef.document.close()
}

function promptForParameter(linkHref, paramName, message) {
	var paramValue = prompt(message,'');
	if (paramValue == null) {
		return false;
	}
	else if (linkHref.lastIndexOf('?') == -1) {
		linkHref += '?' + paramName + '=' + paramValue;
	}
	else {
		linkHref += '&' + paramName + '=' + paramValue;
	}
	window.location.href = linkHref;
	return true;
}

