
/*Global variables*/
var colArray = new Array();
var timer = new Object();

/*---------------------------------------------------------------------------------------------*/

function findItemPos(obj) {
  var curLeft = 0;
  var curTop = 0;
  if (!obj) {
    obj = window.event.srcElement;
  }
  if (obj.offsetParent) {
    curLeft = obj.offsetLeft
    curTop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curLeft += obj.offsetLeft
      curTop += obj.offsetTop
    }
  }
  return [curLeft, curTop];
}

/*---------------------------------------------------------------------------------------------*/

// browser generic functions
function setStyleValue(elementName, property, value) {
  if (document.getElementById) {
    document.getElementById(elementName).style[property] = value;
  } else {
    document[elementName][property] = value;
  }
}

function getStyleValue(elementName, property) {
  var returnVal;
  if (document.getElementById) {
    returnVal = document.getElementById(elementName).style[property];
  } else {
    returnVal = document[elementName][property];
  }
  return returnVal;
}

function setPropValue(elementName, property, value) {
  if (document.getElementById)  {
    document.getElementById(elementName)[property] = value;
  } else {
    document[elementName][property] = value;
  }
}

function getPropValue(elementId, property) {
  var returnVal;
  if (document.getElementById) {
    returnVal = document.getElementById(elementId)[property];
  } else {
    returnVal = document[elementId][property];
  }
  return returnVal;
}

function checkElement(elementId) {
  if (document.getElementById) {
    return document.getElementById(elementId) != null;
  } else {
    return document[elementId] != null;
  }
}

//----------------------------------------------------------------------------------------

function menu(menuName, obj, direction, offset) {
  //direction can be up, down, or right
  if (obj) {
    var itemPos = findItemPos(obj);
    var menuHeight = document.getElementById(menuName).clientHeight;
    switch (direction) {
      case 'up':
        itemPos[1] = itemPos[1] - menuHeight - offset;
      break;
      case 'down':
        itemPos[1] = itemPos[1] + offset;
      break;
      case 'right':
        itemPos[0] = itemPos[0] + offset;
      break;
    }
    setStyleValue(menuName, 'left', itemPos[0]);
    setStyleValue(menuName, 'top', itemPos[1]);
  }
  setStyleValue(menuName, 'visibility', 'visible');
}

function popMenu(menuName, obj, direction, offset) {
  window.clearTimeout(timer[menuName]);
  menu(menuName, obj, direction, offset);
}

function closeMenu(menuName) {
  timer[menuName] = setTimeout('setStyleValue(\'' + menuName + '\',\'visibility\', \'hidden\')', 200);
}

//---------------------------------------------------------------------------------------

function buildArray(protName, protOrgName, flag) {
  var iconSate = 'hide';
  /*flag for isoforms only, true = show column, false = hide column */
  colArray[colArray.length] = new Array(protName, protOrgName, flag, iconSate);
}

//---------------------------------------------------------------------------------------

function rebuildTable(orgName, protOrgName) {
  for (var i = 0; i < colArray.length; i++) {
    if (colArray[i][0] == orgName && colArray[i][1] != protOrgName) {
      if (colArray[i][2] == 'true') {
        colArray[i][2] = 'false';
      } else {
        colArray[i][2] = 'true';
      }
    }
    if (colArray[i][1] == protOrgName) {
      if (colArray[i][3] == 'hide') {
        colArray[i][3] = 'show';
        document.getElementById(protOrgName + "Iso").innerHTML = "&#9660;&nbsp;Show Isoforms";
      } else {
        colArray[i][3] = 'hide';
        document.getElementById(protOrgName + "Iso").innerHTML = "&#9658;&nbsp;Hide Isoforms";
      }
    }
  }
  buildTable('siteTable');
}

/*--------------------------------------------------------------------------------------------*/

function makeVisible(item) {
  if (!document.all) {
    item.position = 'relative';
    item.visibility = 'visible';
  }
  if (document.getElementById) {
    item.style.position = 'relative';
    item.style.visibility = 'visible';
  }
}

/*--------------------------------------------------------------------------------------------*/

function makeHidden(item) {
  if (!document.all) {
    item.position = 'relative';
    item.visibility = 'hidden';
  }
  if (document.getElementById) {
    item.style.position = 'relative';
    item.style.visibility = 'hidden';
  }
}

//----------------------------------------------------------------------------------

function buildTable(itemName, rebuild) {
  var newTable = document.createElement("table");
  var newTableBody = document.createElement("tbody");
  var newRow = document.createElement("tr");
  var newCell;
  var newChild;
  for (var i = 0; i < colArray.length; i++) {
    if (colArray[i][2] == "true") {
      newCell = document.createElement("td");
      newChild = document.getElementById(colArray[i][1]).cloneNode(true);
      newCell.appendChild(newChild);
      makeVisible(newCell.firstChild);
      newRow.appendChild(newCell);
      //create empty cell
      newCell = document.createElement("td");
      newChild = document.createTextNode("");
      newCell.appendChild(newChild);
      newRow.appendChild(newCell);
    }
  }
  newTableBody.appendChild(newRow);
  newTable.appendChild(newTableBody);
  newTable.setAttribute("id", itemName);
  newTable.setAttribute("border", "0");
  newTable.setAttribute("cellSpacing", "0");
  newTable.setAttribute("cellPadding", "0");
  newTable.style.marginLeft = '15px';
  var childNode = document.getElementById(itemName);
  var parentNode = childNode.parentNode;
  parentNode.replaceChild(newTable, childNode);
}

// refresh an item
function reloadImg(imgId, imgSrc) {
  document.getElementById(imgId).src = imgSrc + "?" + Math.random();
}

//--------------------------------------------------------------------------------------------------------------------------------

function adjustHeaderFooter(newWidth, pageWidth) {
  if (newWidth > pageWidth) {
    document.getElementById('titleCol0').width = parseInt(document.getElementById('titleCol0').width) + newWidth - pageWidth;
    document.getElementById('header1').width = newWidth - pageWidth + 20;
    document.getElementById('header2').width = newWidth - pageWidth + 20;
    document.getElementById("footer").width = newWidth - pageWidth + 20;
  }
}

//--------------------------------------------------------------------------------------------------------------------------------

function chromeless(url, width, height, name, hideScrollbars) {
  var newWindow = window.open(url, name, 'height=' + height + ',width=' + width + ',scrollbars=' + (hideScrollbars === true ? 'no' : 'yes') + ', toolbar=no, location=no, status=no, menubar=no, resizable=yes, dependent=no');
  newWindow.focus();
};

//--------------------------------------------------------------------------------------------------------------------------------

function getSessionAttribute(attribute) {
  return retrieveURL("attributeAction.do?get=" + attribute + "&" + Math.random());
}

function putSessionAttribute(attribute, value) {
  return retrieveURL("attributeAction.do?put=" + attribute + "&value=" + value + "&" + Math.random());
}

//--------------------------------------------------------------------------------------------------------------------------------

function retrieveURL(url) {
  var req = null;
  var result = null;
  if (window.XMLHttpRequest) { // Non-IE browsers
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req) {
    req.open("GET", url, false);
    req.send("");
    result = req.responseText;
  } else {
    result = null;
  }
  return result;
}

//------------------------------------------------------------------------------------------------------------------------------------

var selectWin = null;

function loadSelectWindow(url) {
  var width = 440;
  var height = 465;
  var winTop = 0;
  var winLeft = 0;
  if (selectWin && !selectWin.closed && selectWin.location) {
    alert("You must close the selection window before selecting a new one");
    selectWin.focus();
  } else {
    if (screen) {
      winLeft = (screen.width - width) /2;
      winTop = (screen.height - height) /2;
    }
    var winWidth = width + "px";
    var winHeight = height + "px";
    selectWin = window.open(url, "selectWindow", "top="+winTop+",left="+winLeft+",width="+winWidth+",height="+winHeight+",scrollbars=no, toolbar=no, location=no, status=no, menubar=no, resizable=no, dependent=yes, modal=yes");
  }
}

// select box cannot be used due to a bug in IE where the select box will be on top of the select window
// the text and values are converted to array and then put into a textarea
function putSelection(item, textArray, idArray) {
  var textObj = document.getElementById(item+"Text");
  var idObj = document.getElementById(item+"Id");
  textObj.value = textArray.join("\n");
  idObj.value = idArray.join("\n");
  selectWin.close();
}

function getTextArray(item) {
  var textObj = document.getElementById(item+"Text");
  var textArray = new Array();
  if (textObj.value != "") {
    textArray = textObj.value.split("\n");
  }
  return textArray;
}

function getIdArray(item) {
  var idObj = document.getElementById(item+"Id");
  var idArray = new Array();
  if (idObj.value != "") {
    idArray = idObj.value.split("\n");
  }
  return idArray;
}

//------------------------------------------------------------------------------------------------------------------------------------

function helpWindow(tag) {
  var newWindow = window.open('helpAction.do#'+tag, 'helpWin', 'height=1000, width=1024, scrollbars=yes, toolbar=no, location=no, status=no, menubar=no, resizable=yes, dependent=no');
  newWindow.focus();
}

//------------------------------------------------------------------------------------------------------------------------------------

function elementIndex(elementArray, element) {
  var index;
  if (elementIndex.indexOf) {
    index = elementIndex.indexOf(element);
  } else {
    for (var i=0; i<elementArray.length; i++) {
      if (elementArray[i] == element) {
        index = i;
      }
    }
  }
  return index;
}

//------------------------------------------------------------------------------------------------------------------------------------

function clearForm(formName) {
  var form = document.getElementById(formName);
  for (var j=0; j<form.length; j++) {
    if(form.elements[j].type.toLowerCase() == "checkbox" || form.elements[j].type.toLowerCase() == "radio") {
        form.elements[j].checked = false;
    }
    else {
        if (form.elements[j].name != "queryId" &&
            form.elements[j].name != "from" &&
            form.elements[j].name != "organizeBy" &&
            form.elements[j].name != "msData" &&
            form.elements[j].name != "formType" &&
            form.elements[j].name != "pageSize") {
          form.elements[j].value = "";
        }
    }
  }
}

//------------------------------------------------------------------------------------------------------------------------------------

function documentHeight() {
  var yWithScroll, xWithScroll;
  if (window.innerHeight && window.scrollMaxY) {// Firefox
    yWithScroll = window.innerHeight + window.scrollMaxY;
    xWithScroll = window.innerWidth + window.scrollMaxX;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    yWithScroll = document.body.scrollHeight;
    xWithScroll = document.body.scrollWidth;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    yWithScroll = document.body.offsetHeight;
    xWithScroll = document.body.offsetWidth;
  }
  yWithScroll += 50;
  return yWithScroll;
}