// Common JS functions
function trim(str) {
    return str.replace(/^\s*|\s*$/g,"");
}

function writeMessage(msg)
{
	$('messageDIV').innerHTML = msg;
}



// JS Functions for creating and handling Embedded Applet starts here
function createDummyApplet() 
{
	var docW = '';
	var _app=navigator.appName;
	docW = '<object id="PMonApplet" name="PMonApplet" code="PMonApplet.class" codebase="." archive="PMonApplet.jar"';
	docW +=	'type="application/x-java-applet" style="width:1px; height:1px;" mayscript="true">';
	docW += '<param name=cache_archive value="PMonApplet.jar">';
	docW += '<param name=cache_version value="D.1.0.0">';
	//docW += '<param name=code value="PMonApplet.class">';
	
	if(_app=='Netscape') 
	{
	docW+='<COMMENT>';
		
	docW = '<embed id="PMonApplet" name="PMonApplet" code="PMonApplet.class"';
	docW += ' codebase="."  type="application/x-java-applet" style="width:1px; height:1px;"';
	docW += 'cache_archive="PMonApplet.jar"';
	docW += 'cache_option ="Plugin"'; 
	docW +=	'cache_version ="D.1.0.0"';
	
	
       docW+=' </embed>';
	
	docW+='</COMMENT>';
	}
	docW += '</object>';
	
	document.getElementById('PMonAppletDIV').innerHTML = docW;
}



//changes by mahendra introducing object tag instead of applet tag

function createEmbeddedApplet2(divId,userType)
{
window.status='loading please wait';

	var docW = '';
	var _app=navigator.appName;
	if(_app!='Netscape')
	
	docW = '<object id="composition" name="composition" code="com.harmotech.editor.gui.JScoreEmbedApplet.class"';
	
	if(userType!=1){
	docW += ' codebase="." archive="HarmonyScore.jar" type="application/x-java-applet" style="width:1px; height:1px;"';
	}
	if(userType==1){
	docW += ' codebase="." archive="sHarmonyScore.jar" type="application/x-java-applet" style="width:1px; height:1px;"';
	docW+='<param name=admin value="admin">';
	}
	
	//docW += ' UserId="<%=userId%>" mayscript="true">';
	docW += '<param name=cache_option value="Plugin">'; 
	if(userType!=1){
	docW += '<param name=cache_archive value="HarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar">';
	docW += '<param name=cache_version value="1.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0">';
	}
	if(userType==1){
	docW += '<param name=cache_archive value="sHarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar">';
	docW += '<param name=cache_version value="2.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0">';
	}
	
	//docW += '<param name=code value="com.harmotech.editor.gui.JScoreEmbedApplet.class">';
	docW += '<param name=UserId value="<%=userId%>">'; 
	docW += '</object>';
	if(_app=='Netscape') 
	{ 
	  docW+='<comment>';  
    	docW = '<EMBED id="composition" name="composition" code="com.harmotech.editor.gui.JScoreEmbedApplet.class"';
	docW += ' type="application/x-java-applet" style="width:1px; height:1px;"';
	if(userType==1){
	docW += 'cache_archive="sHarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar"';
	docW +=	'cache_version ="2.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0"';
	docW+='admin="admin"';
	}
	if(userType!=1){
	docW += 'cache_archive="HarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar"';
	docW +=	'cache_version ="1.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0"';
	}
	docW += 'cache_option ="Plugin"'; 
	
	docW += ' UserId="<%=userId%>" mayscript="true">';
	
      docW+='</EMBED>';
      docW+='</comment>';
      }
	
	
    
	

	document.getElementById(divId).innerHTML = '';
	document.getElementById(divId).innerHTML = docW;
	
}


function writeApplet()
{
var docW = '';
	
	var _app=navigator.appName;
	docW = '<object id="CheckJava" name="CheckJava" code="CheckJava.class"';
	docW += ' codebase="."  type="application/x-java-applet" style="width:1px; height:1px;"';
	
	//docW += ' UserId="<%=userId%>" mayscript="true">';
	docW += '<param name=cache_option value="Plugin">'; 
	
	
	//docW += '<param name=code value="com.harmotech.editor.gui.JScoreEmbedApplet.class">';
	
	if(_app=='Netscape') 
	{ 
	  docW+='<comment>';  
    	docW = '<EMBED id="CheckJava" name="CheckJava" code="CheckJava.class"';
	docW += 'codebase="."  type="application/x-java-applet" style="width:1px; height:1px;"';
	
	docW += 'cache_option ="Plugin"'; 
	
	docW += 'mayscript="true">';
	
      docW+='</EMBED>';
      docW+='</comment>';
      }
	
    docW += '</object>';
    document.write(docW);
	
}

function checkJava()
{
var _app=navigator.appName;
	var docW = '';
 
		try
		{
		if(_app!='Netscape') {
		var x = CheckJava.setMessage();
		alert(x);
		}
		}
		catch(e)
		{
		alert("The Java plugin is not installed! please click the Get Java link in footer, or go to www.java.com");
		}
        
   
}



function maximizeEmbeddedApplet()
{
	if(document.getElementById("composition") != null)
	{
		document.getElementById("composition").style.width="970px";
		document.getElementById("composition").style.height="225px";
	}
}
	
function minimizeEmbeddedApplet()
{
	if(document.getElementById("composition") != null)
	{
		document.getElementById("composition").style.width="1px";
		document.getElementById("composition").style.height="1px";
	}
}

function openFileFromEmbeddedApplet(compositionId)
{	
	try
	
	{
	
	
		$("composition").openFileFromServer(compositionId);
	}
	catch(e)
	{
		alert("The Java plugin is not installed! please click the Get Java link in footer, or go to www.java.com" );
	}	
}


var viewingCompositionFirstTime = true;

function showApplet(divId, compositionId,userType)
{

	// setTimeout is used to diaply a div (message and gif) 
	// while applet and composition are getting downloaded in back
	// Even tho delay is zero...use setTimeout otherwise message and gif do not appear
	// applet frezes everything while it is getting downloaded
	
	
	var delay = 0;
	
	// This function will show a popup box and freezes all other stuff on page
	sm('box', 300, 70);
	//sm('CheckJava',300,700);
	
	if(viewingCompositionFirstTime == true)
	{
		viewingCompositionFirstTime = false;
		
		setTimeout("createEmbeddedApplet2('"+divId+"','"+userType+"')", delay);
		
		//createEmbeddedApplet2('appletDIV');
	}
	
	setTimeout("openFileFromEmbeddedApplet('"+compositionId+"')", delay);
	//openFileFromEmbeddedApplet(compositionId);
	
	// This function will close the popup box
	setTimeout("hm('box')", delay); 
	//hm('box')
	setTimeout("maximizeEmbeddedApplet()", delay);
	//maximizeEmbeddedApplet('appletDIV');
//	writeApplet();
//	checkJava();
}


function showAppletForPreview(divId, compositionId,userType)
{

	// setTimeout is used to diaply a div (message and gif) 
	// while applet and composition are getting downloaded in back
	// Even tho delay is zero...use setTimeout otherwise message and gif do not appear
	// applet frezes everything while it is getting downloaded
	
	
	var delay = 0;
	
	// This function will show a popup box and freezes all other stuff on page
	sm('box', 300, 70);
	//sm('CheckJava',300,700);
	
	
		
		
		setTimeout("createEmbeddedApplet2('"+divId+"','"+userType+"')", delay);
		
		//createEmbeddedApplet2('appletDIV');
	
	
	setTimeout("openFileFromEmbeddedApplet('"+compositionId+"')", delay);
	//openFileFromEmbeddedApplet(compositionId);
	
	// This function will close the popup box
	setTimeout("hm('box')", delay); 
	//hm('box')
	setTimeout("maximizeEmbeddedApplet()", delay);
	//maximizeEmbeddedApplet('appletDIV');
//	writeApplet();
//	checkJava();
}
// JS Functions for creating and handling Embedded Applet ends here

// JS Functions for creating and handling Embedded Applet starts here
function createMainApplet(divId, userId,userType)
{
	var docW = '';
	var _app=navigator.appName;
	docW = '<object id="mainApplet" name="mainApplet" code="com.harmotech.editor.gui.JScoreApplet.class"';
	if(userType!=1){
	docW +=	' codebase="." archive="sHarmonyScore.jar" type="application/x-java-applet" style="width:1px; height:1px;" ';
	}
	if(userType==1){
	docW += ' codebase="." archive="sHarmonyScore.jar" type="application/x-java-applet" style="width:1px; height:1px;"';
	docW+='<param name=admin value="admin">';
	}
	//docW += 'UserId="'+userId+'" mayscript="true">';
	docW += '<param name=cache_option value="Plugin">'; 
	if(userType==1){
	docW += '<param name=cache_archive value="sHarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar">';
	docW += '<param name=cache_version value="2.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0">';
	}
	if(userType!=1){
	docW += '<param name=cache_archive value="HarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar">';
	docW += '<param name=cache_version value="1.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0">';
	}
	//docW += '<param name=code value="com.harmotech.editor.gui.JScoreApplet.class">';
	docW += '<param name=UserId value="'+userId+'">'; 
	docW += '<param name=appletName value="forumApplet">';
	if(_app=='Netscape') {
	docW+='<COMMENT>';
	docW = '<embed id="mainApplet" name="mainApplet" code="com.harmotech.editor.gui.JScoreApplet.class"';
	docW += ' codebase="."  type="application/x-java-applet" style="width:1px; height:1px;"';
	if(userType==1){
	docW += 'cache_archive="sHarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar"'
	docW +=	'cache_version ="2.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0"';
	}
	if(userType!=1){
	docW += 'cache_archive="HarmonyScore.jar,extlibs/batik-all-1.6.jar,extlibs/js.jar,extlibs/plugin.jar,extlibs/looks-2.2.1.jar"'
	docW +=	'cache_version ="1.F.7.0,A.0.0.0,B.0.0.0,C.0.0.0,D.0.0.0"';
	}
	docW += 'cache_option ="Plugin"'; 
	
	docW += 'UserId ="'+userId+'"  mayscript="true"'; 
	docW +='appletName="forumApplet"';
	
       docW+=' </embed>';
	docW+='</COMMENT>';   
	}
    docW += '</object>';
	
	document.getElementById(divId).innerHTML = docW;
	
}

function openNewScoreSVG() {
	try {
		//$('mainApplet').openNewScoreSVG();
	} catch(e) {
		alert(e);
	}
}

function showMainAppletFrame()
{
	try {
		$('mainApplet').show();
	} catch(e) {
		alert(e);
	}
}


// JS Functions for dynamic Login Box starts
function showLoginBox()
{
	minimizeEmbeddedApplet();
	sm('loginBox', 300, 150);
	$('userName').focus();
		
}
	
function closeLoginBox()
{
	if($('loginBox') != null)
		hm('loginBox');
}	

function login(b)
{
	var msg = "";
	if(isEmpty('userName')) 
	{
		msg = "User Name Field cannot be empty!";
		writeMessage(msg);
		$('userName').focus();
		return false;
	}
	
	if(!isEmailId('userName'))
	{
		msg = "User Name must be a valid Email Id!";
		writeMessage(msg);
		$('userName').focus();
		return false;
	}
	
	if(isEmpty('password')) 
	{
		msg = "Password Field cannot be empty!";
		writeMessage(msg);
		$('password').focus();
		return false;
	}	
	
	ajaxLogin(b);
}
	
function ajaxLogin(b)
{
	var page_request;
	if(window.ActiveXObject)
	{
		try
		{
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	else if (window.XMLHttpRequest) // if Mozilla, Safari etc
	{
		page_request = new XMLHttpRequest();
	}

	page_request.onreadystatechange=function()
	{
		//threadName = makehtml(threadName);
		loadpageafterlogin(page_request, b);
	}
	var loginUrl = "login.do?userName=" + $('userName').value + "&password=" + $('password').value;
	page_request.open('GET', loginUrl, true);
	page_request.send(null);
}

function loadpageafterlogin(page_request, b)
{
	if (page_request.readyState == 4 ) //&& (page_request.status==200 || window.location.href.indexOf("http")==-1)
	{
		var res = page_request.responseText;
		if(res == 'success')
		{
			// After successfully Logging in close the login box.
			closeLoginBox();
			// Send b == true if you have implemented afterLogin function
			// inside calling jsp to perform the further actions
			if(b)
			{				
				afterLogin();
			}
		}
		else {
			// If login fails, message which is sent in response from login action will be displayed
			writeMessage(res);
		}
	}
}
// JS Functions for dynamic Login Box ends

