// Author: James A Hill - augman85@gmail.com
// Author: Bryan Smith - bryanesmith@gmail.com
function getObj(n) {
  if (document.getElementById) {
    return document.getElementById(n);
  } else if (document.all) {
    return document.all[n];
  } else if (document.layers) {
    return document.layers[n];
  }
}
function getPos(o) {
  var curleft = curtop = 0;
  if (o.offsetParent) {
    do {
      var borders = getBorderWidths(o);
      var scroll = getScrollDimensionsForElem(o);
      curleft += (o.offsetLeft + borders[3] - scroll[0]);
      curtop += (o.offsetTop + borders[0] - scroll[1]);
    } while (o = o.offsetParent);
  }
  return [curleft,curtop];
}
/**
 * Returns the inner width of an element (inside the borders).
 */
function getWidth(elem) {
  function _Convert(val) {
    if (!val) {return;}
    val = val.replace("px","");
    if (isNaN(val)) {return 0;}
    return parseInt(val);
  }
  var currentStyle;
  if (elem.currentStyle) { currentStyle = elem.currentStyle; }
  else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
  else { currentStyle = elem.style; }
  return (elem.offsetWidth -
    _Convert(currentStyle.marginLeft) -
    _Convert(currentStyle.marginRight) -
    _Convert(currentStyle.borderLeftWidth) -
    _Convert(currentStyle.borderRightWidth));
}
function getBorderWidths(elem) {
  function _Convert(val) {
    if (!val) {return;}
    val = val.replace("px","");
    if (isNaN(val)) {return 0;}
    return parseInt(val);
  }
  var currentStyle;
  if (elem.currentStyle) { currentStyle = elem.currentStyle; }
  else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
  else { currentStyle = elem.style; }
  var array = new Array();
  array[0] = _Convert(currentStyle.borderTopWidth);
  array[1] = _Convert(currentStyle.borderRightWidth);
  array[2] = _Convert(currentStyle.borderBottomWidth);
  array[3] = _Convert(currentStyle.borderLeftWidth);
  return array;
}
function getHeight(elem) {
  function _Convert(val) {
    if (!val) {return;}
    val = val.replace("px","");
    if (isNaN(val)) {return 0;}
    return parseInt(val);
  }
  var currentStyle;
  if (elem.currentStyle) { currentStyle = elem.currentStyle; }
  else if (window.getComputedStyle) {	currentStyle = document.defaultView.getComputedStyle(elem, null); }
  else { currentStyle = elem.style; }
  return (elem.offsetHeight -
    _Convert(currentStyle.marginTop) -
    _Convert(currentStyle.marginBottom) -
    _Convert(currentStyle.borderTopWidth) -
    _Convert(currentStyle.borderBottomWidth));
}
function getScreenSize() {
  var myWidth = 0, myHeight = 0;
  if (typeof(window.innerWidth) == 'number') {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if (typeof(window.innerWidth) == 'string') {
    myWidth = window.innerWidth.replace("px", "");
    myHeight = window.innerHeight.replace("px", "");
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}
function getScrollDimensions() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}
function getScrollDimensionsForElem(o) {
    return [o.scrollLeft, o.scrollTop];
}
function goTo(url) {
  document.location.href = url;
}
function showFrameTopRight(f,o,changeLeft,changeTop) {
  var frame = getObj(f);
  var pos = getPos(o);
  frame.style.left = (pos[0]+changeLeft) + 'px';
  frame.style.top = (pos[1]+changeTop) + 'px';
  showObj(frame);
}
function show(e) {
  getObj(e).style.display = 'block';
}
function showObj(e) {
  e.style.display = 'block';
}
function showInline(e) {
  getObj(e).style.display = 'inline';
}
function showInlineObj(e) {
  e.style.display = 'inline';
}
function hide(e) {
    getObj(e).style.display = 'none';
}
function hideObj(e) {
    e.style.display = 'none';
}
function clear(e) {
    getObj(e).innerHTML = '';
}
function clearObj(e) {
    e.innerHTML = '';
}
function changeRegionSet(c,r) {
    // get the country ID
    var country = getObj(c);
    var countryID = country.options[country.selectedIndex].value;
    // get the new inner HTML for the region (AJAX) and send reponse to a function
    ajax.update("https://proteomecommons.org/scripts/location/selectRegions.jsp?c="+countryID, r);
}
function getURLSafeString(string) {
    string = string.replace("%", "%25");
    string = string.replace(" ", "%20");
    string = string.replace("!", "%21");
    string = string.replace("\"", "%22");
    string = string.replace("#", "%23");
    string = string.replace("&", "%26");
    string = string.replace("'", "%27");
    string = string.replace("+", "%2b");
    string = string.replace(",", "%2c");
    string = string.replace("/", "%2f");
    string = string.replace("=", "%3d");
    string = string.replace("?", "%3f");
    return string;
}
function getHTMLSafeString(string) {
    string = string.replace("<", "&lt;");
    string = string.replace(">", "&gt;");
    string = string.replace("\"", "&quot;");
    return string;
}
function popupOptionOver(o) {
    o.style.background = '#ccc';
}
function popupOptionOut(o) {
    o.style.background = '#fff';
}

// allow toggling of scroll functions
var allowScroll = true;
function handleScroll() {
    if (allowScroll == false){
        scroll(0,0);
    }
}
function setAllowScroll(allow) {
    allowScroll = allow;
    if (allow == false) {
        document.body.style.overflow = 'hidden';
    } else {
        document.body.style.overflow = 'auto';
    }
}

function isEmpty(id) {
    return getObj(id).value == '';
}
// check sign in validity
function checkSignInForm(email,pass) {
    var isProblem = false;
    var error = 'The following errors have been found in your submission:<ul>';
    if (isEmpty(email)) {
        isProblem = true;
        error = error + '<li><b>Email</b> cannot be blank.</li>';
    }
    if (isEmpty(pass)) {
        isProblem = true;
        error = error + '<li><b>Password</b> cannot be blank.</li>';
    }
    error = error + '</ul>';
    if (isProblem) {
        showErrorPopup(error, 'Submission Error');
        return false;
    } else {
        return true;
    }
}

function isInternetExplorer() {
  return false;
}

function isFirefox() {
  return true;
}

// for browse pages
function changePage(f, p, go) {
    var pageNum = getObj(p);
    if (go == "first") {
        pageNum.selectedIndex = 0;
    } else if (go == "last") {
        pageNum.selectedIndex = pageNum.selectedIndex-1
    } else if (go == "next") {
        pageNum.selectedIndex = pageNum.selectedIndex+1
    } else if (go == "final") {
        pageNum.selectedIndex = pageNum.options.length-1;
    }
    if (f != null) {
      getObj(f).submit();
    }
}

/**
 * Popup any location with specified height and width
 *
 * USAGE
 *   <a href="page.html" onclick="return popupDocument('page.html', 200, 400)">Popup document</a>
 *
 *   Creates link that will popup a document 200 pixels wide and 400 pixels tall.
 */
function popupDocument (href, width, height) {

  var newwindow = window.open (href,'_blank','height='+height+',width='+width+',resizable=yes,scrollbars=yes');
  if (window.focus) {
    newwindow.focus();
  }

  return false;
}

/**
 * Given any text with multiple lines, use this object to parse through the lines one at a time.
 */
function InputText(text) {
    // replace all forms of carriage return with the one we're looking for
    this.text = text.replace(/(\r\n|\r|\n)/g, '\n');

    // method removes and returns the next line of text
    this.getNextLine = function() {
        // nothing left
        if (this.text == null) {
            return null;
        }
        // get the end of the line
        var endOfLine = -1;
        for (var index = 0; index < this.text.length; index++) {
            if (this.text.charAt(index) == '\n') {
                endOfLine = index;
                break;
            }
        }
        // get the text to return
        var returnText;
        if (endOfLine != -1) {
            returnText = this.text.substring(0, endOfLine);
            this.text = this.text.substring(endOfLine+1);
            // nothing left? make text null instead of blank
            if (this.text == '') {
                this.text = null;
            }
        } else {
            returnText = this.text;
            this.text = null;
        }
        return returnText;
    }
}

/**
 * Makes so any string object can call the method.
 * Example:    myString.trim()
 */
String.prototype.trim = function() {
    text = this.replace(/^\s+/, '');
    return text.replace(/\s+$/, '');
};
String.prototype.forURL = function() {
    text = this.replace("%", "%25");
    text = text.replace(" ", "%20");
    text = text.replace("!", "%21");
    text = text.replace('"', "%22");
    text = text.replace("#", "%23");
    text = text.replace("&", "%26");
    text = text.replace("'", "%27");
    text = text.replace("+", "%2b");
    text = text.replace(",", "%2c");
    text = text.replace("/", "%2f");
    text = text.replace("=", "%3d");
    text = text.replace("?", "%3f");
    return text;
};
String.prototype.forHTML = function() {
    text = this.replace("&", "&amp;");
    text = text.replace("<", "&lt;");
    text = text.replace(">", "&gt;");
    text = text.replace(/(\r\n|\r|\n)/g, "<br/>");
    text = text.replace('"', "&quot;");
    return text;
};
String.prototype.forJSArg = function() {
    text = this.replace("'", "\\'");
    text = text.replace('"', "&quot;");
    return text;
};
String.prototype.forInput = function() {
    return this.replace('"', "&quot;");
};

function handleClick(Obj,imagen)  {
    if(document.getElementById(Obj).style.display=='none') {
        document.getElementById(Obj).style.display = "";
        document.getElementById(imagen).src="images/minus.gif";
        document.getElementById(imagen).title="hidden";
        //document.getElementById("texttip").style.display = "none";
    }
      else {
          document.getElementById(Obj).style.display = "none";
          document.getElementById(imagen).src="images/plus.gif";
          document.getElementById(imagen).title="show up";
          //document.getElementById("texttip").style.display = "";
      }
}

function refreshImageList(pdbid, CDK2id) {
         //       alert(getObj('rightImages').innerHTML);
                //alert('imagelist.jsp?CDK2id='+CDK2id+"&pdbid="+pdbid);
                //ajax.update('imagelist.jsp?CDK2ids='+CDK2ids+"&pdbids="+pdbids, 'rightImages');
                if(CDK2id.length==0 && pdbid.length==0){
                    loadXMLDoc('imagelist.jsp?restart=1', 'rightImages');
                }else{
                    loadXMLDoc('imagelist.jsp?CDK2id='+CDK2id+'&pdbid='+pdbid, 'rightImages');
                }
}
function removeFromImageList(removedpdbid, removedCDK2id) {
                if(removedpdbid.length==0 && removedCDK2id.length==0){
                    loadXMLDoc('imagelist.jsp?restart=1', 'rightImages');
                }else
                {
                    loadXMLDoc('imagelist.jsp?removedCDK2id='+removedCDK2id+'&removedpdbid='+removedpdbid, 'rightImages');
                }
}
