var s = window.location.search;
var inline = s.indexOf("inline")!=-1;
var substance = s.indexOf("substance")!=-1;

function showCode() {
  var s = document.applet.getCode();
  show(s);
}

function show(s) {
  if(document.getElementById) {
     var div = document.getElementById("appletbar")
     var area = document.getElementById("texdraw");
     if(area!=null) {
       area.value = s;
     } 
    else {
     area = document.createElement("textarea");
     area.setAttribute("id", "texdraw");
     area.setAttribute("rows", "18");
     area.setAttribute("cols", "90");
     area.setAttribute("onClick", "select()");
     area.value = s;
     div.appendChild(area);
   }
  }
  else {
     window.alert("Missing Javascript function: getElementById");
  }
}

function loadDiagram() {
  if(document.getElementById) {
     var area = document.getElementById("texdraw");
     checkBox = document.getElementById("replace");
     document.applet.load(area.value, checkBox.checked);     
  }
  else {
     window.alert("Missing Javascript function: getElementById");
  }
}

function getJavaVersion() {
  var result = "-1";
  if (typeof navigator != 'undefined' && typeof navigator.javaEnabled != 'undefined') {
    result = "0"; // at least, java is enabled
    if (typeof java != 'undefined') {
     result = java.lang.System.getProperty("java.version");
    }
  }  
  return result;
}

function showApplet() {
var params = "<param name=\"archive\" value=\""+ getArchive() +"\"/>"+
"<param name=\"text\" value=\"\
Copyright (c) 2007-09 Jan Plate. All Rights Reserved.\
\
 * Redistribution and use in source and binary forms, with or without \
 * modification, are permitted provided that the following conditions are met:\
 *    \
 *  - Redistributions of source code must retain the above copyright notice, \
 *    this list of conditions and the following disclaimer. \
 *     \
 * - Redistributions in binary form must reproduce the above copyright notice, \
 *    this list of conditions and the following disclaimer in the documentation \
 *    and/or other materials provided with the distribution. \
 *     \
 *  - Neither the name of pasi nor of Jan Plate may be used to endorse or promote products derived \
 *     from this software without specific prior written permission. \
 *     \
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER 'AS IS', \
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, \
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR \
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, \
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, \
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; \
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, \
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE \
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, \
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \
\
\"/><param name=\"key\" value=\"NDJ7N3wz\"/><param name=\"mayscript\" value=\"Y\"/>" +
(inline? "<param name=\"handlesOutput\" value=\"false\"/>": "");
var a = [];
a[0] = "<!--[if !IE]> Firefox and others will use outer object -->\
 <object id=\"applet\" classid=\"java:" + getAppletClass() + "\" type=\"application/x-java-applet\" archive=\"" + getArchive() + "\" " + getDimension() + ">";
a[1] = params;
a[2] = "<!--<![endif]--> <!-- MSIE (Microsoft Internet Explorer) will use inner object -->\
 <object id=\"applet\" classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" codebase=\".\" " + getDimension() + "><param name=\"code\" value=\"" + getAppletClass() + "\"/>";
a[3] = params;
a[4] = "<strong>This browser does not support Java applets. You can download the latest Java plug-in <a href=\"http://www.java.com/\">here</a>.<br/></strong></object><!--[if !IE]> close outer object --> </object> <!--<![endif]-->" + 
	(inline? "<br/><table style=\"font-size: 85%\" cellspacing=\"6\" align=\"center\"><tr><td><input id=\"replace\" type=\"checkbox\"/> <label for = \"replace\">Replace current diagram</label></td><td><input value=\"Load Diagram from Code\" type=\"button\" style=\"padding:4px\" onClick=\"loadDiagram()\"/></td><td><input value=\"Generate Texdraw Code\" type=\"button\" style=\"padding:4px\" onClick=\"showCode()\"/></td></tr></table><br/><textarea id=\"texdraw\" rows=\"18\" cols=\"90\"></textarea>": "") + "<br/>";
var version = parseFloat(getJavaVersion());
if(version>=1.6) {  
  for(var i = 0; i<a.length; i++) {
    document.write(a[i]);
  }
} else if(version==0) {
document.write("<strong>Your browser has Java enabled but does not let this website check the version of the Java plug-in. In order to run the applet, you will need to have a Java plug-in of version 1.6 or higher (available <a href=\"http://www.java.com/\">here</a>).</strong><br/><br/>Trying anyway:<br/><br/>");
  for(var i = 0; i<a.length; i++) {
    document.write(a[i]);
  }
} else if(version<0) {
  document.write("<strong>Your browser does not have Java enabled. In order to run the applet, please enable Java or <a  href=\"http://www.java.com/\">click here</a> to download the latest plug-in (required version: 1.6 or higher).</strong>");
} else {
 document.write("<strong>This applet requires a Java plug-in version 1.6 or higher (currently installed: "+getJavaVersion()+"). You can download the latest version of the plug-in <a href=\"http://www.java.com/\">here</a>.</strong>");
}
}

function getDimension() {
  return inline? "width=\"100%\" height=\"600\"": "width=\"220\" height=\"125\"";
}

function getAppletClass() {
 return substance? "jp.pasi.AppletS.class": "jp.pasi.Applet.class";
}

function getArchive() {
  return substance? "pasi.jar, https://substance.dev.java.net/files/documents/3294/43243/substance-lite.jar": "pasi.jar";
}