var stages = new Array(8);
stages["START_STAGE"]=1;
stages["STORAGE_STAGE"]=2;
stages["PLUGINMANDOWNLOAD_STAGE"]=3; 
stages["BASE_STAGE"]=4; 
stages["CLIENTDOWNLOAD_STAGE"]=5; 
stages["CONTENTDOWNLOAD_STAGE"]=6; 
stages["SPAWN_STAGE"]=7; 
stages["STARTUP_STAGE"]=8;
stages["CONTENT_II_STAGE"]=9;

var PAPERMINT_ACTIVE = false;
var APPLET_STATUS = "";
var SEPARATE_WINDOW = false;
var ERROR = false;

function resetApplet(){
	PAPERMINT_ACTIVE = false;
	APPLET_STATUS = "";
	SEPARATE_WINDOW = false;
	ERROR = false;
}

var org_position = new Array();

function closeFullscreen(){
	for ( divid in org_position ){
		var n = document.getElementById(divid);			
		if ( n != null ){
			n.style.top = org_position[divid]["TOP"];
			n.style.left = org_position[divid]["LEFT"];
			n.style.width = org_position[divid]["WIDTH"];
			n.style.height = org_position[divid]["HEIGHT"];
		}
	}
	
	document.getElementById("fullscreen_bar").style.visibility="hidden";
}

function fullScreen(){	
	document.getElementById("applet").style.visibility="visible";
	var par = document.getElementById("applet");
	while ( par.parentNode ){
		if ( par.tagName == "DIV" ){
			if ( par.id.length > 0 ){
				org_position[par.id] = new Array();
				org_position[par.id]["TOP"] = par.style.top;
				org_position[par.id]["LEFT"] =  par.style.left;
				org_position[par.id]["WIDTH"] = par.style.width;
				org_position[par.id]["HEIGHT"]= par.style.height;
				if ( par.id != "applet" && par.id != "tab_pages"  ){
					par.style.top = 20+"px";
				}else{
					par.style.top = 0+"px";
				}
				par.style.left = 0+"px";
				par.style.height = '100%';
				par.style.width = '100%';
			}
		}		
		if ( par.parentNode ){
			par = par.parentNode;
		}
	}			
	document.applets.papermintclient.width = '100%';
	document.applets.papermintclient.height = '100%';	
	document.getElementById("fullscreen_bar").style.visibility="visible";
}

function onOpenPapermintTab(){
	if ( jreVersion == null ){
		displayInfoMessage(messages[browserlang]["NO_JAVA"]);
		load( false );
	}else if ( !validJavaVersion ){
		displayInfoMessage(messages[browserlang]["WRONG_JAVA_VERSION"]);
		load( false );
	}			
	if ( APPLET_STATUS == "FAIL" ){
		restartApplet();
		displayInfoMessage( messages[browserlang]["PLEASE_TRUST"] );
		showTrustMessage( true );
	}
}

function restartApplet(){
	document.getElementById("applet").innerHTML = "";
	eval( appletCall );
	hideInfoMessage();
}


//is called from the Java Applet
function updatePage( status ){
	//alert( status );
	APPLET_STATUS = status;
	if ( ERROR ){
		return;
	}
	if ( status == "START_STAGE" ){
		document.getElementById("clientstatus").innerHTML = messages[browserlang]["STATUS_LOADING"];
		load( true );
	}else if( status == "STORAGE_STAGE" ){
		showTrustMessage( false );
	}else if ( status == "CONTENT_II_STAGE" ){
		document.getElementById("clientstatus").innerHTML = messages[browserlang]["STATUS_LOGIN"];
		load( false );
	}else if ( status == "SUCCESS" ){
		onPapermintLoaded();
		document.getElementById("clientstatus").innerHTML = messages[browserlang]["STATUS_SUCCESS"];
	}else if ( status == "ERROR" ){
		document.getElementById("clientstatus").innerHTML = messages[browserlang]["STATUS_ERROR"];
		load( false );
		displayInfoMessage( messages[browserlang]["FAIL"] );
		PAPERMINT_ACTIVE = false;
	}else if ( status == "NEVERTRUSTASTRANGER" ){
		document.getElementById("clientstatus").innerHTML = messages[browserlang]["STATUS_TRUST"];

		var imsg = messages[browserlang]["PLEASE_TRUST"];
		if ( !jv1610 ){
			imsg += messages[browserlang]["RESTART_15"];
		}else{
			imsg += messages[browserlang]["RESTART_1610"];
		}
		displayInfoMessage( imsg );
		showTrustMessage( true );
		load( false );
	}else if ( status == "FAIL" ){
		onPapermintError();
		displayInfoMessage( messages[browserlang]["FAIL"] );
	}else if ( status == "RUNNING_TWICE" ){
		displayInfoMessage( messages[browserlang]["RUNNING_TWICE"] );
		onPapermintError()
	}else if ( status == "WINDOW_OPEN" ){
		SEPARATE_WINDOW = true;
	}
	document.getElementById("clientstatus").innerHTML += status;
	
	if ( stages[status] ){
		updateLoadingBar(stages.length,stages[status]);
	}		
}

function onPapermintError(){
	document.images['papermint_loading'].src = imgpath+"/mint_green_light.gif"
	load( false );
	PAPERMINT_ACTIVE = false;
	ERROR = true;
}

/**
Browser Applet detection
**/

function displayApplet ( host , java15, limitMem, startWindow, fullInstall ) {
	var div = document.getElementById("applet");
	
	if ( !validJavaVersion ){
		return;
	}
	
	var browserName=navigator.appName; 
	
	var appletcode = "We could not automatically detect your webbrowser please use the native installers instead!";
	
	var javaParam = '';
	if ( java15==true) {
		javaParam = '<PARAM name="java_version" value="1.5*">';
	}
	var windowParam = '';
	if ( startWindow==true ) {
		windowParam=' -W ';
	}
	var memParam = '256m';
	if ( limitMem==true ) {
		memParam = '56m';
	}
	
	var installParam = '';
	if ( fullInstall ){
		installParam = ' -Cprog';
	}
	
	var downloadurl = host+"webapp/";
	var libPath = downloadurl+'pmc_manager.jar,'+downloadurl+'lib-ext/pmc_common_1_0.jar,'+downloadurl+'lib-ext/ext-libs-start.jar,'+downloadurl+'lib-ext/commons-logging-1.1.1.jar,'+downloadurl+'lib-ext/resources.jar';
	var launchParams = '-slauncher '+windowParam+' -o'+host+' -dPMC -J'+installParam;
	if ( browserName == 'Opera') {
		launchParams = launchParams + ' -S';
	}
	
//	if (browserName=="Microsoft Internet Explorer"){ 
			appletcode = 
				"<applet " +
				' name="papermintclient" '+
				' width="670px" height="600px" '+
				' archive="'+libPath+'"' +
				' type="application/x-java-applet" '+
				' code="com.avaloop.pureclient.PureClient" MAYSCRIPT>' +
				'<param name="archive" value="'+libPath+'"/>'+
				'<param name="code" value="com.avaloop.pureclient.PureClient" />'+
				'<PARAM name="java_arguments" value="-Xmx'+memParam+' -Dsun.java2d.noddraw=true -Dsun.awt.noerasebackground=true">'+
				'<PARAM name="separate_jvm" value="true">'+
				'<param name="codebase_lookup" value="false" />'+
				'<PARAM name="classloader_cache" value="false">'+
				javaParam+
				'<param name="pluginman" value="'+launchParams+'"/>'+
				'<param name="mayscript" value="yes">'+
				'<param name="scriptable" value="true">'+
			'</applet>';

//	} else { 
// 		if ( (browserName=="Netscape") || (browserName=="Opera") ){
//  		appletcode = 
//  			"<object " +
//  			' width="500px" height="500px" '+
//  			' archive="'+libPath+'"' +
//			' type="application/x-java-applet" '+
//			' code="com.avaloop.pureclient.PureClient">' +
//			'<param name="archive" value="'+libPath+'"/>'+
//			'<param name="code" value="com.avaloop.pureclient.PureClient" />'+
//			'<PARAM name="java_arguments" value="-Xmx'+memParam+' -Dsun.java2d.noddraw=true">'+
//			'<PARAM name="separate_jvm" value="true">'+
//			'<param name="codebase_lookup" value="false" />'+
//			'<PARAM name="classloader_cache" value="false">'+
//			javaParam+
//			'<param name="pluginman" value="'+launchParams+'"/>'+
//			'</object>';
// 		}
//	}
	div.innerHTML = appletcode;
}		

/**

Browser information 

**/

function displayInfo() {
	var div = document.getElementById("info");
	window.onerror=null;

	//colors = window.screen.colorDepth;
	//document.form.color.value = Math.pow (2, colors);
	//if (window.screen.fontSmoothingEnabled == true)
	//	document.form.fonts.value = "Yes";
	//else document.form.fonts.value = "No";
	
	var javaEnabled = "No";	
	if (navigator.javaEnabled() == 1) javaEnabled="Yes";

	div.innerHTML =	"BROWSER: " + navigator.appName + "<br>";
	div.innerHTML =	div.innerHTML + "CODENAME: " + navigator.appCodeName+ "<br>";
	div.innerHTML =	div.innerHTML + "PLATFORM: " + navigator.platform+ "<br>";
	div.innerHTML =	div.innerHTML + "JAVA ENABLED: " + javaEnabled+ "<br>";
	/*
	document.form.version.value = navigator.appVersion;
	document.form.colordepth.value = window.screen.colorDepth;
	document.form.width.value = window.screen.width;
	document.form.height.value = window.screen.height;
	document.form.maxwidth.value = window.screen.availWidth;
	document.form.maxheight.value = window.screen.availHeight;
	document.form.codename.value = navigator.appCodeName;
	document.form.platform.value = navigator.platform;
	document.form.pages.value = history.length;
	if (navigator.javaEnabled() < 1) document.form.java.value="No";
	if (navigator.javaEnabled() == 1) document.form.java.value="Yes";

	if(navigator.javaEnabled() && (navigator.appName != "Microsoft Internet Explorer")) {
		vartool=java.awt.Toolkit.getDefaultToolkit();
		addr=java.net.InetAddress.getLocalHost();
		host=addr.getHostName();
		ip=addr.getHostAddress();		
   }*/
}

