function show_hide(el2show, el2hide)
{
    document.getElementById(el2show).style.display = 'block';
    document.getElementById(el2hide).style.display = 'none';
}

function openWindow(url, name, width, height, options)
{
    var wnd = window.open(url, name, "width=" + width + ",height=" + height +
	         ",left=" + (screen.width - width) / 2 + ",top=" + (screen.height - height) / 2 + "," + options);
    wnd.creator = self;
    wnd.focus();
    return wnd;
}

function openPopupWindow(url, name, width, height, options)
{
    var wnd;
    if ( window.showModalDialog && navigator.appVersion.indexOf("MSIE 7") != -1 ) {
        window.showModalDialog(url, window, "dialogWidth:" + width + "px;" + "dialogHeight:" + height + "px;center:yes;" + options);
    }
    else {
        wnd = window.open(url, name, "width=" + width + ",height=" + height +
	         ",left=" + (screen.width - width) / 2 + ",top=" + (screen.height - height) / 2 + "," + options);
	    wnd.creator = self;
        wnd.focus();
    }
    return false;
}

function showElement(id, show, x, y)
{
    var el = document.getElementById(id);
    if ( el != null ) {
        if ( show ) {
            if ( x != null )
                el.style.left = x;
            if ( y != null )
                el.style.top = y;
        }
        el.style.display = show ? "block" : "none";
    }
    return false;
}

function switchPopup(id)
{
    var shown_name = id + "_shown";
    var shown;
    try {
        shown = eval(shown_name);
        alert(shown);
    }
    catch ( err ) {
        eval("var " + shown_name + " = 0;");
        shown = 0;
    }
    
    showElement(id, shown ? "none" : "block");
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange (s, a, b)
{   if (isEmpty(s))
     if (isIntegerInRange.arguments.length == 1) return false;
     else return (isIntegerInRange.arguments[1] == true);

  // Catch non-integer strings to avoid creating a NaN below,
  // which isn't available on JavaScript 1.0 for Windows.
  if (!isInteger(s, false)) return false;

  // Now, explicitly change the type to integer via parseInt
  // so that the comparison code below will work both on
  // JavaScript 1.2 (which typechecks in equality comparisons)
  // and JavaScript 1.1 and before (which doesn't).
  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}

function isInteger(s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit(c)
{
  return ((c >= "0") && (c <= "9"))
}

function loadSpectrum(logoImg, appletContainer, id) {
	if($.browser.msie) {
		$('#' + appletContainer).html("<object classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" codebase=\"http://java.sun.com/update/1.5.0/jinstall-1_5_0_05-windows-i586.cab\" id=\"JSVApplet\" height=\"400\" width=\"750\">" +
							"<param name=\"code\" value=\"jspecview.applet.JSVApplet.class\" />" +
							"<param name=\"archive\" value=\"jspecview.jar\" />" +
							"<param name=\"script\" value=\"load /FilesHandler.ashx?type=blob&id=" + id + "; interface tab; backgroundcolor #ffffff;\" />" +
							"<param name=\"scriptable\" value=\"true\" />" +
							"<param name=\"mayscript\" value=\"true\" /></object>");
	}
	else if($.browser.mozilla) {
		$('#' + appletContainer).html("<object classid=\"java:jspecview.applet.JSVApplet.class\" type=\"application/x-java-applet;version=1.5\" archive=\"jspecview.jar\" id=\"JSVApplet\" height=\"400\" width=\"750\" >" +
							"<param name=\"script\" value=\"load /FilesHandler.ashx?type=blob&id=" + id + "; interface tab; backgroundcolor #ffffff;\" />" +
							"<param name=\"mayscript\" value=\"true\" /></object>");
	}
	else {
		$('#' + appletContainer).html("<strong>This browser does not have a Java Plug-in or needs upgrading.<br />" +
							"<a href=\"http://java.sun.com/products/plugin/downloads/index.html\">" +
							"Get the latest Sun Java Plug-in from here.</a></strong>");
	}

    $('#' +logoImg).fadeOut('slow', function() {
        $('#' + appletContainer).fadeIn('slow');
    });
}

function loadCIF(logoImg, appletContainer, id, isApproved) {
	if($.browser.msie) {
		$('#' + appletContainer).html("<object name=\"jmolApplet0\" id=\"jmolApplet0\" classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" codebase=\"http://java.sun.com/update/1.5.0/jinstall-1_5_0_05-windows-i586.cab\" width=\"500\" height=\"500\">" +
							"<param name=\"progressbar\" value=\"true\" />" +
							"<param name=\"progresscolor\" value=\"blue\" />" +
							"<param name=\"boxbgcolor\" value=\"white\" />" +
							"<param name=\"boxfgcolor\" value=\"black\" />" +
							"<param name=\"boxmessage\" value=\"Downloading JmolApplet ...\" />" +
							"<param name=\"name\" value=\"jmolApplet0\" />" +
							"<param name=\"archive\" value=\"JmolApplet0.jar\" />" +
							"<param name=\"mayscript\" value=\"true\" />" +
							"<param name=\"codebase\" value=\"jmol\" />" +
							"<param name=\"code\" value=\"JmolApplet\" />" +
							"<param name=\"script\" value=\"load /FilesHandler.ashx?type=blob&id=" + id + "\" />" +
							"<p style=\"background-color:yellow; color:black; width:500px;height:500px;text-align:center;vertical-align:middle;\">" +
							"You do not have Java applets enabled in your web browser, or your browser is blocking this applet.<br />" +
							"Check the warning message from your browser and/or enable Java applets in<br />" +
							"your web browser preferences, or install the Java Runtime Environment from <a href=\"http://www.java.com\">www.java.com</a><br /></p></object>");
	}
	else if($.browser.mozilla) {
		$('#' + appletContainer).html("<object name=\"jmolApplet0\" id=\"jmolApplet0\" classid=\"java:JmolApplet\" type=\"application/x-java-applet\" width=\"500\" height=\"500\" >" +
							"<param name=\"progressbar\" value=\"true\" />" +
							"<param name=\"progresscolor\" value=\"blue\" />" +
							"<param name=\"boxbgcolor\" value=\"white\" />" +
							"<param name=\"boxfgcolor\" value=\"white\" />" +
							"<param name=\"boxmessage\" value=\"Downloading JmolApplet ...\" />" +
							"<param name=\"name\" value=\"jmolApplet0\" />" +
							"<param name=\"archive\" value=\"JmolApplet0.jar\" />" +
							"<param name=\"mayscript\" value=\"true\" />" +
							"<param name=\"codebase\" value=\"jmol\" />" +
							"<param name=\"script\" value=\"load /FilesHandler.ashx?type=blob&id=" + id + "\" />" +
							"<p style=\"background-color:yellow; color:black; width:500px;height:500px;text-align:center;vertical-align:middle;\">" +
							"You do not have Java applets enabled in your web browser, or your browser is blocking this applet.<br />" +
							"Check the warning message from your browser and/or enable Java applets in<br />" +
							"your web browser preferences, or install the Java Runtime Environment from <a href=\"http://www.java.com\">www.java.com</a><br /></p></object>");
	}
	else {
		$('#' + appletContainer).html("<strong>This browser does not have a Java Plug-in or needs upgrading.<br />" +
							"<a href=\"http://java.sun.com/products/plugin/downloads/index.html\">" +
							"Get the latest Sun Java Plug-in from here.</a></strong>");
	}

    $('#' + logoImg).fadeOut('slow', function() {
        $('#' + appletContainer).fadeIn('slow');
    });
}

function loadScript(doc, sScriptSrc, callbackfunction)
{
     //gets document head element
     var oHead = doc.getElementsByTagName('head')[0];
     if(oHead)
     {
         //creates a new script tag
         var oScript = doc.createElement('script'); 

         //adds src and type attribute to script tag
         oScript.setAttribute('src', sScriptSrc);
         oScript.setAttribute('type', 'text/javascript');

         //calling a function after the js is loaded (IE)
         var loadFunction = function()
             {
                 if (this.readyState == 'complete' || this.readyState == 'loaded')
                 {
                    if( callbackfunction != null ) callbackfunction();
                 }
             };
         oScript.onreadystatechange = loadFunction;

         //calling a function after the js is loaded (Firefox)
         oScript.onload = callbackfunction;

         //append the script tag to document head element
         oHead.appendChild(oScript);
     }
}

function adjust_iframe()
{
	var frmWindow = $('iframe')[0];
	if ($.browser.mozilla)
		frmWindow.style.height = frmWindow.contentDocument.height + 30 + 'px';
	else if ($.browser.msie)
		frmWindow.style.height = frmWindow.contentWindow.document.body.scrollHeight + 20 + 'px';
}
