function clientEmailFunction(subjectVal, emailContentVal){
	location.href = "mailto:?subject="+subjectVal+"&body="+emailContentVal;
}
function sendPublicMessage(msgText)
{
	//alert("Sending Message " + msgText);
	if(typeof(frames['chatFrame'].sendPublicMessage) == "function")
		frames['chatFrame'].sendPublicMessage(msgText);
}

function sendStatus(status)
{
	//alert("Sending Message " + msgText +" to " + to);
	if(typeof(frames['chatFrame'].sendStatus) == "function")
		frames['chatFrame'].sendStatus(status);
}

function openPopup(url)
{
        window.open(url,'','width=600,height=300');
}

function openPageInNewWindow(url)
{
	window.open(url);
}

function openPage(url)
{
	window.location = url;
}

function reloadPage()
{
	window.location.reload();
}

function sendPrivateMessage(to, msgText)
{
	//alert("Sending Message " + msgText +" to " + to);
	if(typeof(frames['chatFrame'].sendPrivateMessage) == "function")
		frames['chatFrame'].sendPrivateMessage(to,msgText);
}

//From here on its desktopsharing code
var isVEncoder = false;

var desktopAppletAlreadyLoaded = false;
function loadDesktopSharingApplet(){
	if(desktopAppletAlreadyLoaded || javaLoadDone) return;
	var frame = document.getElementById("appletFrame");
	//alert(isVEncoder);
	if(!(window.domain === undefined))
	{
		frame.src = "static/desktopSharingApplet.jsp?launchMode="+launchMode+"&vencoder="+isVEncoder+"&domain="+escape(document.domain);
	}
	else
	{
		frame.src = "static/desktopSharingApplet.jsp?launchMode="+launchMode+"&vencoder="+isVEncoder;
	}
//	alert(frame.src);
	desktopAppletAlreadyLoaded = true;
}

function isAppletLoading()
{
	return (desktopAppletAlreadyLoaded && !javaLoadDone);
}

function isAppletLoaded()
{
	return javaLoadDone;
}

var swtApplet = false;

function isSWTBrowser()
{
	return swtApplet;
}

function registerSWTApplet()
{
	swtApplet = true;
}
// desktopSharing-host 
//Applets have to be embedded using object and embed for them to work on both ff and ie 
//The same applet is specified in both object and embed tags -> each browser interprets either the object tag or the embed tag but not both.
//The catch is the applet name shouldn't be the same. So, to get around it, we give different names and obtain the proper name using this function.
function getBrowserIndependentApplet(){
	var applet ;
	if(swtApplet)
	{
		return (new SWTApplet());
	}
	
	applet = document.hqtoolIE;
	if(isValidAppletElement(applet)) return applet;
	
	applet = document.hqtool;
	if(isValidAppletElement(applet)) return applet;

	if(frames['appletFrame'] != null){
		applet = frames['appletFrame'].document.hqtoolIE;
		if(isValidAppletElement(applet)) return applet;
	}
	
	if(frames['appletFrame'] != null){
		applet = frames['appletFrame'].document.hqtool;
		if(isValidAppletElement(applet)) return applet;
	}
	
	return applet;
}

function isValidAppletElement(applet){
	return (applet != null) && (typeof(applet) != "undefined") && (typeof(applet.swfLoaded) != "undefined");
}

function deskTopSharing_handleEvent(event){
	getBrowserIndependentApplet().handleRDCMessage(event);
}
function desktopSharingJava_SetFlowName(flowname){
	getBrowserIndependentApplet().desktopSharingJava_SetFlowName(flowname);
}
function desktopSharingJava_SetServerUrl(serverurl){
	getBrowserIndependentApplet().desktopSharingJava_SetServerUrl(serverurl);
}
function desktopSharingJava_ConnectToServer(){
	getBrowserIndependentApplet().desktopSharingJava_ConnectToServer();
}
function desktopSharingJava_StartStreaming(){
	//alert(getBrowserIndependentApplet());
	try
	{
		if(getBrowserIndependentApplet() === undefined || getBrowserIndependentApplet() == null)
		{
			setTimeout('showDesktopError()', 1000);
			return false;
		}
		else
		{
			return getBrowserIndependentApplet().desktopSharingJava_StartStreaming();
		}
	}
	catch(e)
	{
		//alert(e);
	}
}

function showDesktopError()
{
	alert("Unable to launch Screen Sharing.\nInstall Java and restart your browser to enable this.");
	desktopStreamingStopped();
}
function desktopSharingJava_StopStreaming(){
	return getBrowserIndependentApplet().desktopSharingJava_StopStreaming();
}
function desktopSharingJava_SetBitRate(rate){
	getBrowserIndependentApplet().desktopSharingJava_SetBitRate(rate);
}
function desktopSharingJava_SetKeyFrameDelay(millis){
	getBrowserIndependentApplet().desktopSharingJava_SetKeyFrameDelay(millis);
}
function desktopSharingJava_SetNumIntraFrames(intraframes){
	getBrowserIndependentApplet().desktopSharingJava_SetNumIntraFrames(intraframes);
}
function desktopSharingJava_SetFrameDelay(millis){
	getBrowserIndependentApplet().desktopSharingJava_SetFrameDelay(millis);
}

function showCustomInviteDialog()
{
	try
	{
		displayInviteDialog(flashvars.flow);
	}
	catch(e)
	{
	}
}
function customMicrophoneMute(state)
{
	try
	{
		muteMicrophone(state);
	}
	catch(e)
	{
	}
}

function debugIt(str){
	alert(str);
}
//desktop flash interop javascritp 
var javaLoadDone = false;
var swfLoadDone = false;

var swfConsoleType = 'clientConsole';

function getSwfObject(consoleName){
	var client = window.getFlashMovie(consoleName);
	return client;
}


function getTheFlashMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
    	return window[movieName]
    }
    else {
    	return document[movieName]
    }
}


var swfNotified = false;
function notifySwfOfJavaLoad(){
	
	var client = getTheFlashMovie(swfConsoleType);
	
	if(client != null && typeof(client.javaAppletLoaded) == "function"){
		try
		{
			if(!swfNotified)
			{
				client.javaAppletLoaded();
			}
		}
		catch(e)
		{
		}
			
	}
	else{
		//try in 5 secs again although this should never be triggered if <flash>.javaAppletLoaded is present.
		setTimeout("notifySwfOfJavaLoad()",5000);
	}
}

function notifyJavaOfSwfLoad(){
	// NOT REQUIRED AS OF NOW
	
	/*var app = getBrowserIndependentApplet();
	
	if(app != null && typeof(app.swfLoaded) == "function")
	{
		getBrowserIndependentApplet().swfLoaded();
	}
	else
	{
		// Try in 5 seconds again
		//setTimeout("notifyJavaOfSwfLoad()",5000);
	}*/
}
function javaAppletLoaded(){
	javaLoadDone = true;
	//alert("Applet Loaded");
	setTimeout('notifyChildren()', 600);
}

function notifyChildren()
{
	if(swfLoadDone){
		notifySwfOfJavaLoad();
		//notifyJavaOfSwfLoad();		
	}
	try
	{
		appletLoaded();
	}catch(e)
	{
	}
}

function installPresenterSoftware()
{
	getBrowserIndependentApplet().jsInstallPresenterSoftware();
}
function flashLoaded(){
	swfLoadDone = true;
	if(javaLoadDone){
		//notifyJavaOfSwfLoad();
		notifySwfOfJavaLoad();
	}	
}
function refreshPage()
{
	alert("You need to restart your browser for the settings to take effect.");
	//window.location.reload();
}

function desktopStreamPublishStart(){
	var client = getTheFlashMovie(swfConsoleType);
	if(client != null && typeof(client.desktopStreamPublishStart) == "function"){
		client.desktopStreamPublishStart();
	}
	else{
		alert("Error in notifying desktop stream publish start");
	}
}

function desktopStreamingStopped()
{
	var client = getTheFlashMovie(swfConsoleType);
	if ((client != null) && (typeof(client.desktopStreamingStopped) == "function"))
	{
		client.desktopStreamingStopped();
	}
	else
	{
		//alert("Error in notifying desktop streaming stopped");
	}
}

function desktopSharingInitialized()
{
	if ((parent != null) && (parent != this) && (typeof(parent.desktopSharingInitialized) == "function"))
	{
		parent.desktopSharingInitialized();
	}
}

function desktopSharingPlaybackStarted()
{
	if ((parent != null) && (parent != this) && (typeof(parent.desktopSharingPlaybackStarted) == "function"))
	{
		parent.desktopSharingPlaybackStarted();
	}
}

function desktopSharingPlaybackStopped()
{
	if ((parent != null) && (parent != this) && (typeof(parent.desktopSharingPlaybackStopped) == "function"))
	{
		parent.desktopSharingPlaybackStopped();
	}
}

//Detect if the browser is IE or not.
//If it is not IE, we assume that the browser is NS.
var isIE = document.all ? true : false;
		
//If NS -- that is, !IE -- then set up for mouse capture
if (!isIE)
{
	document.captureEvents(Event.MOUSEMOVE);
}

document.onmousemove = handleMouseMove;

function handleMouseMove(e)
{
	var evt;
	
	evt = window.event || e;
	computeContentPosition(evt);
}

var contentX = null;
var contentY = null;

function computeContentPosition(evt)
{
	contentX = evt.screenX - evt.clientX;
	contentY = evt.screenY - evt.clientY;
}

function getContentX()
{
	if (contentX != null)
	{
		return contentX;
	}
	else
	{
		return null;
	}
}

function getContentY()
{
	if (contentY != null)
	{
		return contentY;
	}
	else
	{
		return null;
	}
}

function desktopSharingJava_SetCombinedVideoFlowName(flowName){
	getBrowserIndependentApplet().desktopSharingJava_SetCombinedVideoFlowName(flowName);
}

function desktopSharingJava_SetCombinedVideoServerURL(serverURL){
	getBrowserIndependentApplet().desktopSharingJava_SetCombinedVideoServerURL(serverURL);
}

function desktopSharingJava_StartCombinedVideoStreaming(x, y, width, height)
{
	var contX;
	var contY;
	
	contX = getContentX();
	contY = getContentY();
	
	return getBrowserIndependentApplet().desktopSharingJava_StartCombinedVideoStreaming(x, y, width, height);
}

function desktopSharingJava_StopCombinedVideoStreaming()
{
	return getBrowserIndependentApplet().desktopSharingJava_StopCombinedVideoStreaming();
}

function desktopSharingJava_SetCombinedVideoRegion(x, y, width, height)
{
	var contX;
	var contY;
	
	contX = getContentX();
	contY = getContentY();
	if ((contX != null) && (contY != null))
	{
		getBrowserIndependentApplet().desktopSharingJava_SetCombinedVideoRegion((contX + x), (contY + y),
				                                                                width, height);
	}
}

// hq tool javascript
var pluginPresent = false;
var systemCapable = "Unavailable";
var callbackClientName;

function assignCallBack(param)
{
	callbackClientName = param;
}

function checkPlugin()
{
	/*try
	{
		getBrowserIndependentApplet().jsCheckAndStopFME();
	}
	catch(err)
	{
	}*/
}

function sendMessageToPlugin(message)
{
	getBrowserIndependentApplet().handleMessage(message);
}


function encoderStreamStopped(status,code)
{
	try
	{
		setTimeout('streamStopped(\''+status+'\',\''+code+'\')', 300);
	}
	catch(err)
	{
	}
}

function stopRecording()
{
	//alert('Ending event');
	try
	{
		getFlashMovie(callbackClientName).endEvent();
	}catch(err)
	{
	}
}

function streamStopped(status,code)
{
	try
	{
		getFlashMovie(callbackClientName).streamStopped(status,code);
	}catch(err)
	{
	}
}

var port = 5432;

function setControlPort(controlPort)
{
	port = parseInt(controlPort+"");
}

function getControlPort()
{
	/*
	 * Want to make sure we are returning int here.
	 */
	
	return parseInt(port+"");
}

function setPluginPresent(status)
{
	if(status == "true")
	{
		pluginPresent = true;
	}
	else
	{
		pluginPresent = false;
	}
	try
	{
		getFlashMovie(callbackClientName).pluginPresent();
	}
	catch(err)
	{
	}
}

function displayHighPerformanceModeWarning()
{
	try
	{
		getFlashMovie(callbackClientName).displayHighPerformanceModeWarning();
	}
	catch(err)
	{
	}
}

function setSystemCapablity(status)
{
	systemCapable = status;
	try
	{
		getFlashMovie(callbackClientName).systemCapability(status);
	}
	catch(err)
	{
	}
}
function isSystemCapable()
{
	return systemCapable;
}

function isPluginPresent()
{
	return pluginPresent;
}
function startEncoder(cam,camDimension,mic,url,stream,bitrate,framesize, codec)
{
	try
	{
		//alert(codec);
		if(codec == null)
		{
			getBrowserIndependentApplet().jsStartStream(cam,camDimension, mic, url, stream, bitrate,framesize, "flv");
		}
		else
		{
			//alert(codec);
			getBrowserIndependentApplet().jsStartStream(cam,camDimension, mic, url, stream, bitrate,framesize, codec);
		}
	}
	catch(err)
	{
	}
}

function stopEncoder()
{
	try
	{
		getBrowserIndependentApplet().jsStopAll();
	}
	catch(err)
	{
	}
}

function getFlashMovie(movieName)
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	  return (isIE) ? window[movieName] : document[movieName];
}

function isInternetExplorer()
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return isIE;
}

function installEncoder()
{
	if(getBrowserIndependentApplet() == null)
	{	
		loadDesktopSharingApplet();
	}
	else
	{
		try
		{
			//alert('here');
			if(isVEncoder && !isPluginPresent())
			{
				getBrowserIndependentApplet().jsInstallEncoder();
			}
		}
		catch(err)
		{ 
		}
	}
}

var participantsWithoutPlugin = [];

function addParticipantWithoutPlugin(usr)
{
	try
	{
		participantsWithoutPlugin[usr] = usr;
	}
	catch(e)
	{
		
	}
}

function getListOfParticipantsWithoutPlugin()
{
	var xmlString = '<?xml version="1.0" encoding="UTF-16"?>';
	xmlString += '<contacts>';
	for(i in participantsWithoutPlugin)
	{
		xmlString += '<skypeID>';
		xmlString += participantsWithoutPlugin[i];
		xmlString += '</skypeID>';
	}
	xmlString += '</contacts>';
	try
	{
		inviteParticipantsWithoutPlugin(xmlString);
	}
	catch(e)
	{
		
	}
}

var vhDriverInstallActive = false;
var appletAdded = false;
function installVHDriver()
{
	if(!navigator.javaEnabled()){
		var answer = confirm('You Require the latest version of Java to continue. \n Would you liek to install Java from http://java.com');
		if(answer)
			window.open('http://jdl.sun.com/webapps/getjava/BrowserRedirect?locale=en&host=java.com:80');
	}
	else{
		if(getBrowserIndependentApplet() == null)
		{	
			addApplet("hqtool","com.vivu.browserplugin.ViVuHQTool","/ViVuFlashWeb/plugin/browserplugin.jar?t=<%=(new Date()).getTime()%>",0,0,"enable Java",'installVHDriver');
		}
		else
		{
			try
			{
				getBrowserIndependentApplet().jsInstallVHdriver();
				vhDriverInstallActive = true;
			}
			catch(err)
			{ 
				vhDriverInstallActive = false;
			}
		}
	} 	
}

function addApplet(id,code,codebase,width,height,alt,message){
	  if(appletAdded)
		  return;
	  var a=document.createElement('applet');
	  a.setAttribute('id',id);
	  a.setAttribute('code',code);
	  a.setAttribute('archive',codebase);
	  a.setAttribute('width',width);
	  a.setAttribute('height',height);
	  a.setAttribute('alt',alt);
	  a.setAttribute('Message',message);
	  document.getElementById('appletDiv').appendChild(a);
	  appletAdded = true;
	}

/*
 * Camera list
 */

var cameraList = "";
function setCameraList(list)
{
	cameraList = list;
	
	try
	{
		getFlashMovie(callbackClientName).setCameraList(list);
	}
	catch(err)
	{
	}
}

function getCameraList()
{
	return cameraList;
}

// THis is the code for running encoder and desktop sharing while running in the swt mode
function SWTApplet()
{
	this.handleRDCMessage = function(event)
	{
		handleRDCMessage(event);
	}
	this.desktopSharingJava_SetFlowName = function(event)
	{
		desktopSharingJava_SetFlowName(event);
	}
	this.desktopSharingJava_SetServerUrl = function(serverurl)
	{
		desktopSharingJava_SetServerUrl(serverurl);
	}
	this.desktopSharingJava_ConnectToServer = function()
	{
		desktopSharingJava_ConnectToServer();
	}
	this.desktopSharingJava_StartStreaming = function()
	{
		return desktopSharingJava_StartStreaming();
	}
	this.desktopSharingJava_StopStreaming = function()
	{
		return desktopSharingJava_StopStreaming();
	}
	this.desktopSharingJava_SetBitRate = function(rate)
	{
		desktopSharingJava_SetBitRate(rate);
	}
	this.desktopSharingJava_SetKeyFrameDelay = function(millis)
	{
		desktopSharingJava_SetKeyFrameDelay(millis);
	}
	this.desktopSharingJava_SetNumIntraFrames = function(intraframes)
	{
		desktopSharingJava_SetNumIntraFrames(intraframes);
	}
	this.desktopSharingJava_SetFrameDelay = function(millis)
	{
		desktopSharingJava_SetFrameDelay(millis);
	}
	
	this.desktopSharingJava_SetCombinedVideoFlowName = function(flowName)
	{
		desktopSharingJava_SetCombinedVideoFlowName(flowName);
	}
	
	this.desktopSharingJava_SetCombinedVideoServerURL = function(serverURL)
	{
		desktopSharingJava_SetCombinedVideoServerURL(serverURL);
	}
	
	this.desktopSharingJava_StartCombinedVideoStreaming = function(x, y, width, height)
	{
		return desktopSharingJava_StartCombinedVideoStreaming(x, y, width, height);
	}
	
	this.desktopSharingJava_StopCombinedVideoStreaming = function()
	{
		return desktopSharingJava_StopCombinedVideoStreaming();
	}
	
	this.desktopSharingJava_SetCombinedVideoRegion = function(x, y, width, height)
	{
		desktopSharingJava_SetCombinedVideoRegion(x, y, width, height);
	}
	
	this.sendMessageToPlugin = function(message)
	{
		sendMessageToPlugin(message);
	}
	
	this.swfLoaded = function()
	{
		swfLoaded();
	}
	this.jsCheckAndStopFME = function()
	{
		jsCheckAndStopFME();
	}
	this.jsStartStream = function(cam,camDimension, mic, url, stream, bitrate,framesize, codec)
	{
		jsStartStream(cam,camDimension, mic, url, stream, bitrate,framesize, codec);
	}
	this.jsStopAll = function()
	{
		jsStopAll();
	}
}