var _info = navigator.userAgent;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0) || (_info.indexOf("AIX") > 0) || (_info.indexOf("OS/2") > 0) || (_info.indexOf("IRIX") > 0)));

/**
 * Calculates the hash of the given data.
 *
 * data      - content used to calculate hash.
 * algorithm - the hash algorithm to use.
 *
 * Returns the calculated hash as a base64 encoded String. 
 */
function calculateHash(data, algorithm)
{
	return document.SignHashApplet.calculateHash(data, algorithm);
}

/**
 * Calculates the hash of the file specified by given path.
 * 
 * filepath  - the path to the input file.
 * algorithm - the hash algorithm to use.
 *
 * Returns the calculated hash as a base64 encoded String.
 */
function calculateFileHash(filePath, algorithm)
{
	return document.SignHashApplet.calculateFileHash(filePath, algorithm);
}

/**
 * Sign the given data (hash), the result is placed into the field
 * specified by the result parameter.
 *
 * data      - the HTML field that contains the data to be signed.
 * algorithm - the HTML field that contains the certificate type algorithm.
 * result    - the HTML field that should contain the sign result when finished.
 *
 * Returns true if the sign operation was successful, else false.
 */
function sign(data, algorithm, result)
{
     try {	
	signature = document.SignHashApplet.sign(data.value, algorithm.value);
     } catch(e) {
        alert("An exception occurred during signing. Please try again. Error name: " + e.name + ". Error message: " + e.message); 
	return false;
     }

    var length = 0;
    
    if (_ns == true)
    {
        length = signature.length();
    }
    else
    { 
        length = signature.length;
    }
    
    if (length > 0)
    {
        result.value = signature;
        return true;
    }
    
	return false;
}	

/**
 * Calculates the hash of the file and signs the hash. The result is
 * placed into the field specified the result parameter.
 *
 * data          - the HTML field that contains the data to be signed.
 * algorithm     - the HTML field that contains the algorithm.
 * result        - the HTML field that contains the sign result when finished.
 */
function signFile(filePath, algorithm, result)
{
    hash = calculateFileHash(filePath.value, algorithm.value);
    hashLength = 0;
    
    if (_ns == true)
    {
        hashLength = hash.length();
    }
    else
    {
        hashLength = hash.length;
    }    
    
    if (hashLength > 0)
    {
        signature = document.SignHashApplet.sign(hash, algorithm.value);
	    var length = 0;
	    
	    if (_ns == true)
	    {
	        length = signature.length();
	    }
	    else
	    {
	        length = signature.length;
	    }
	    
	    if (length > 0)
	    {
	        result.value = signature;
	        return true;
	    }
    }
    
    return false;
}

/**
 * Adds the SignHashApplet to the webpage.
 */
function initRCT(javaCodeBase)
{
	if (typeof(javaCodeBase) == "undefined")
	{
        javaCodeBase='';
    }
    
    if (_ie == true)
    { 
        document.writeln('<object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ');
     	document.writeln(' codebase="https://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,5" ') ;
    	document.writeln(' WIDTH=0 HEIGHT=0 NAME="SignHashApplet"> ');
    	document.writeln('  <PARAM NAME=CODE VALUE="com.rabobank.rct.applet.SignHashApplet.class" > ');
    	document.writeln('  <PARAM NAME=CODEBASE VALUE="' + javaCodeBase + '" > ') ;
    	document.writeln('  <PARAM NAME=ARCHIVE VALUE="rct-applet.jar" > ');
    	document.writeln('  <PARAM NAME=NAME VALUE="Rabobank Crypto Toolbox" > ') ;
    	document.writeln('  <PARAM NAME=MAYSCRIPT VALUE="mayscript" > ') ;
    	document.writeln('  <PARAM NAME=rctdebug VALUE="true" > ') ;
    	document.writeln('  <param name="type" value="application/x-java-applet;version=1.5"> ');
    	document.writeln('  <param name="scriptable" value="true"> ');
        document.writeln('</object>' );
    }
    else if (_ns == true)
    {
        document.writeln('<embed ');
        document.writeln(' type="application/x-java-applet;version=1.5" ');
        document.writeln(' CODE="com.rabobank.rct.applet.SignHashApplet.class" ');
        document.writeln(' JAVA_CODEBASE="' + javaCodeBase + '" ') ;
        document.writeln(' ARCHIVE="rct-applet.jar" ');
        document.writeln(' NAME="SignHashApplet" ');
        document.writeln(' WIDTH=0 ');
        document.writeln(' rctdebug=true ');
        document.writeln(' HEIGHT=0 ');
        document.writeln(' MAYSCRIPT="mayscript" ');
        document.writeln(' scriptable=true ');
        document.writeln(' pluginspage="https://java.sun.com/products/plugin/index.html#download"> ');
        document.writeln('</embed>');
	}
	else
	{	    
	    document.writeln('<APPLET CODEBASE="' + javaCodeBase + '" CODE="com.rabobank.rct.applet.SignHashApplet.class" NAME="SignHashApplet" WIDTH=0 HEIGHT=0 mayscript="mayscript" archive="rct-applet.jar"><param name="rctdebug" value="true"></APPLET>');
    }
}

/**
 * Adds the SignHashApplet to the webpage.
 */
function initClientValidator(javaCodeBase)
{
	if (typeof(javaCodeBase) == "undefined")
	{
        javaCodeBase='';
    }
    
    if (_ie == true)
    { 
        document.writeln('<object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" ');
     	document.writeln(' codebase="https://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,5" ') ;
    	document.writeln(' WIDTH=600 HEIGHT=300 NAME="ValidateClient"> ');
    	document.writeln('  <PARAM NAME=CODE VALUE="com.rabobank.rct.applet.ValidateClient.class" > ');
    	document.writeln('  <PARAM NAME=CODEBASE VALUE="' + javaCodeBase + '" > ') ;
    	document.writeln('  <PARAM NAME=ARCHIVE VALUE="rct-applet.jar" > ');
    	document.writeln('  <PARAM NAME=NAME VALUE="Rabobank Crypto Toolbox" > ') ;
    	document.writeln('  <PARAM NAME=MAYSCRIPT VALUE="mayscript" > ') ;
    	document.writeln('  <PARAM NAME=rctdebug VALUE="true" > ') ;
    	document.writeln('  <param name="type" value="application/x-java-applet;version=1.5"> ');
    	document.writeln('  <param name="scriptable" value="true"> ');
        document.writeln('</object>' );
    }
    else if (_ns == true)
    {
        document.writeln('<embed ');
        document.writeln(' type="application/x-java-applet;version=1.5" ');
        document.writeln(' CODE="com.rabobank.rct.applet.ValidateClient.class" ');
        document.writeln(' JAVA_CODEBASE="' + javaCodeBase + '" ') ;
        document.writeln(' ARCHIVE="rct-applet.jar" ');
        document.writeln(' NAME="ValidateClient" ');
        document.writeln(' WIDTH=600 ');
        document.writeln(' rctdebug=true ');
        document.writeln(' HEIGHT=300 ');
        document.writeln(' MAYSCRIPT="mayscript" ');
        document.writeln(' scriptable=true ');
        document.writeln(' pluginspage="https://java.sun.com/products/plugin/index.html#download"> ');
        document.writeln('</embed>');
	}
	else
	{	    
	    document.writeln('<APPLET CODEBASE="' + javaCodeBase + '" CODE="com.rabobank.rct.applet.ValidateClient.class" NAME="ValidateClient" WIDTH=600 HEIGHT=300 mayscript="mayscript" archive="rct-applet.jar"><param name="rctdebug" value="true"></APPLET>');
    }
}