/////////////////////////////////////////////////
// Events: Start
/////////////////////////////////////////////////
var EVENT_ADD_BUDDY = 10;
var EVENT_ADD_BLOCKED_BUDDY = 15;
var EVENT_ADD_BUDDY_TO_GROUP = 20;
var EVENT_PRESENCE = 30;
var EVENT_BUDDY_MESSAGE = 40;
var EVENT_APP_MESSAGE = 50;
var EVENT_ADMIN_MESSAGE = 60;
var EVENT_ERROR = 100;
/*
if(document.all.easoMain)
{

	strMCName = "/buddyList:";
}
else
	strMCName ="";
*/
strMCName = "/buddyList:";
function JB::ProcessIncomingAdminMessage(nAction, strReason)
{
	
	var strDebug = "E: ProcessIncomingAdminMessage(" + "nAction=" + nAction + ", strReason=" + strReason  + ")\n";
	easoMain.SetVariable( strMCName + "_eventData",strDebug );
	easoMain.SetVariable( strMCName + "_eventId",EVENT_ADMIN_MESSAGE );
	//document.all.ta.value += strDebug;
}

function JB::ProcessIncomingMessage(strFromName, strFromDomain, strFromSubDomain, strMessage)
{
	var strDebug = "E: ProcessIncomingMessage(" + "strFromName=" + strFromName + ", strFromDomain=" + strFromDomain + ", strFromSubDomain=" + strFromSubDomain + ", strMessage=" + strMessage + ")\n";
	easoMain.SetVariable( strMCName + "_eventData",strDebug );
	var strJID = strFromName + "/" + strFromDomain;// + "/" + strFromSubDomain;
	easoMain.SetVariable( strMCName + "_jid",strJID );
	easoMain.SetVariable( strMCName + "_fromName",strFromName );
	easoMain.SetVariable( strMCName + "_domain",strFromDomain );
	easoMain.SetVariable( strMCName + "_subDomain",strFromSubDomain );
	easoMain.SetVariable( strMCName + "_message",strMessage);
	easoMain.SetVariable( strMCName + "_eventId",EVENT_BUDDY_MESSAGE );
	//document.all.ta.value += strDebug;

}

function JB::ProcessIncomingAppMessage( strFromName, strFromDomain, strFromSubDomain, strMessage)
{
	var strDebug = "E: ProcessIncomingMessage(" + "strFromName=" + strFromName + ", strFromDomain=" + strFromDomain + ", strFromSubDomain=" + strFromSubDomain + ", strMessage=" + strMessage + ")\n";
	easoMain.SetVariable( strMCName + "_eventData",strDebug );
	var strJID = strFromName + "/" + strFromDomain;// + "/" + strFromSubDomain;
	easoMain.SetVariable( strMCName + "_jid",strJID );
	easoMain.SetVariable( strMCName + "_fromName",strFromName );
	easoMain.SetVariable( strMCName + "_domain",strFromDomain );
	easoMain.SetVariable( strMCName + "_subDomain",strFromSubDomain );
	easoMain.SetVariable( strMCName + "_message",strMessage);
	easoMain.SetVariable( strMCName + "_eventId",EVENT_APP_MESSAGE );
	//document.all.ta.value += strDebug;
}

function JB::ProcessIncomingPresence( strFromName, strFromDomain, strFromSubDomain, strType,  strShow,  strStatus)
{
	var strDebug = "E: ProcessIncomingPresence(" + "strFromName=" + strFromName + ", strFromDomain=" + strFromDomain + ", strFromSubDomain=" + strFromSubDomain + ", strType=" + strType + ", strShow=" + strShow + ", strStatus=" + strStatus + ")\n";
	easoMain.SetVariable( strMCName + "_eventData",strDebug );
	var strJID = strFromName + "/" + strFromDomain;// + "/" + strFromSubDomain;
	easoMain.SetVariable( strMCName + "_jid",strJID );
	easoMain.SetVariable( strMCName + "_fromName",strFromName );
	easoMain.SetVariable( strMCName + "_domain",strFromDomain );
	easoMain.SetVariable( strMCName + "_subDomain",strFromSubDomain );
	easoMain.SetVariable( strMCName + "_presenceType",strType );
	easoMain.SetVariable( strMCName + "_presenceShow",strShow );
	easoMain.SetVariable( strMCName + "_presenceStatus",strStatus );
	easoMain.SetVariable( strMCName + "_eventId",EVENT_PRESENCE );
	//document.all.ta.value += strDebug;
}

function JB::ProcessIncomingRosterMember( nAction,  strJID)
{
	var strDebug = "E: ProcessIncomingRosterMember(" + "nAction=" + nAction + ", strJID=" + strJID + ")\n";
	easoMain.SetVariable( strMCName + "_eventData",strDebug );
	easoMain.SetVariable( strMCName + "_jid",strJID );
	if(nAction==1)
		easoMain.SetVariable( strMCName + "_eventId",EVENT_ADD_BUDDY );
	if(nAction==7)
		easoMain.SetVariable( strMCName + "_eventId",EVENT_ADD_BLOCKED_BUDDY );
	//document.all.ta.value += strDebug;

}

function JB::ProcessIncomingRosterMemberGroup( nAction,  strJID,  strGroup)
{
	var strDebug = "E: ProcessIncomingRosterMemberGroup(" + "nAction=" + nAction + ", strJID=" + strJID + ", strGroup=" + strGroup + ")\n";
	easoMain.SetVariable( strMCName + "_eventData",strDebug );
	easoMain.SetVariable( strMCName + "_jid",strJID );
	easoMain.SetVariable( strMCName + "_groupName",strGroup );
	easoMain.SetVariable( strMCName + "_eventId",EVENT_ADD_BUDDY_TO_GROUP );
	//document.all.ta.value += strDebug;
}

function JB::ProcessError( nErrorType,  nErrorCode,  strError)
{
	var strDebug =  + "E: ProcessError(" + "nErrorType=" + nErrorType + ", nErrorCode=" + nErrorCode + ", strError=" + strError + ")\n";
	//easoMain.SetVariable( strMCName + "_nErrorType",nErrorType );
	//easoMain.SetVariable( strMCName + "_nErrorCode",nErrorCode );
	//easoMain.SetVariable( strMCName + "_strError",strError );
	easoMain.SetVariable( strMCName + "_eventId",EVENT_ERROR );
	//document.all.ta.value += strDebug;
}
/////////////////////////////////////////////////
// Events:End
/////////////////////////////////////////////////


/////////////////////////////////////////////////
// Functions called from Flash.
/////////////////////////////////////////////////

var strJabberSubDomain;

function bl_Connect(host, port, name, domain, subDomain, lKey)
{
	strJabberSubDomain = subDomain;
	try
	{
		//alert(host + "," + port + "," + name + "," + domain + "," + subDomain + "," + lKey);
		var connected = JB.IsConnected();
		//alert("connected=" + connected);
		if(connected == 1)
		{
			//SendJabberDisconnect();
			//DisconnectFromServer();
			//JB.SendJabberGetRoster(name,domain ,subDomain);
			//JB.SendJabberGetBlockedUsers(name,domain,subDomain);
			JB.SendJabberGetRoster(name,domain ,"");
			JB.SendJabberGetBlockedUsers(name,domain,"");
			setTimeout("SendJabberPresence();",10000);
			//JB.SendJabberPresence("chat", subDomain);
		}
		else
		{
			JB.ConnectToServer(host, port, name, domain, subDomain, lKey);
	
			JB.SendJabberConnect();
			JB.SendJabberAuthenticate();
			//JB.SendJabberGetRoster(name, domain, subDomain);
			//JB.SendJabberGetBlockedUsers(name,domain,subDomain);
			JB.SendJabberGetRoster(name, domain, "");
			JB.SendJabberGetBlockedUsers(name,domain,"");
			setTimeout("SendJabberPresence();",10000);
			//JB.SendJabberPresence("chat", subDomain);  //send subDomain for the status value
		}
	}
	catch(er)
	{

	}
}

function SendJabberPresence()
{
	JB.SendJabberPresence("chat", strJabberSubDomain);
}

function bl_Disconnect()
{
	try
	{
		var connected = JB.IsConnected();

		if(connected == 1)
		{
			//alert("sending disconnect");
			JB.SendJabberDisconnect();
			JB.DisconnectFromServer();
		}
	}
	catch(er){}
}

function bl_SendPresence(show,status)
{
	try
	{
		JB.SendJabberPresence(show, status);
	}
	catch(er){}
}

function bl_SendJabberMessage(name,domain,subDomain,body,nStoreSecs)
{
	try
	{
		//JB.SendJabberMessage(name, domain, subDomain, "", body, "", parseInt(nStoreSecs));
		JB.SendJabberMessage(name, domain, "", "", body, "", parseInt(nStoreSecs));
	}
	catch(er){}
}

function bl_SendJabberAppMessage(name,domain,subDomain,dataDomain,dataSubDomain,appData)
{
	try
	{
		JB.SendJabberAppMessage(name, domain, subDomain, dataDomain, dataSubDomain, appData);
	}
	catch(er){}
}

function bl_SendJabberSubscribe(name,domain,subDomain)
{
	try {
		JB.SendJabberSubscribe(name, domain, "");
	}
	catch(er){}
}

function bl_SendJabberUnsubscribe(name,domain,subDomain)
{
	try {
		JB.SendJabberUnsubscribe(name, domain, "");
	}
	catch(er){}
}

function bl_SendJabberBlockUser(name,domain,subDomain)
{
	JB.SendJabberUpdateBlockedUsers(name,domain,"", 2);
}

function bl_SendJabberUnblockUser(name,domain,subDomain)
{
	JB.SendJabberUpdateBlockedUsers(name,domain,"", 1);
}

function bl_AddBuddy(name,domain,subDomain,groupName)
{
	try
	{
		CONST_ADD = 0;
		//alert(name + "," + domain + "," + subDomain + "," + groupName);
		//JB.SendJabberUpdateRoster(name, domain, subDomain, groupName, CONST_ADD);
		JB.SendJabberUpdateRoster(name, domain, "", groupName, CONST_ADD);
		JB.SendJabberSubscribe(name, domain, "");
	}
	catch(er){}

}
function bl_DeleteBuddy(name,domain,subDomain,groupName)
{
	CONST_DELETE = 1;
	//JB.SendJabberUpdateRoster(name, domain, subDomain, groupName, CONST_DELETE);
	JB.SendJabberUpdateRoster(name, domain, "", groupName, CONST_DELETE);
	//alert("delete\n name:" + name + "\n domain:" + domain + "\n subDomain:" + subDomain + "\n groupName:" + groupName);
}

function bl_MoveBuddyGroup(name,domain,subDomain,oldGroupName,newGroupName)
{
	CONST_ADD = 0;
	CONST_DELETE = 1;
	//JB.SendJabberUpdateRoster(name, domain, subDomain, oldGroupName, CONST_DELETE);
	//JB.SendJabberUpdateRoster(name, domain, subDomain, newGroupName, CONST_ADD);
	JB.SendJabberUpdateRoster(name, domain, "", oldGroupName, CONST_DELETE);
	JB.SendJabberUpdateRoster(name, domain, "", newGroupName, CONST_ADD);
	//alert("delete\n name:" + name + "\n domain:" + domain + "\n subDomain:" + subDomain + "\n groupName:" + oldGroupName);
	//alert("add\n name:" + name + "\n domain:" + domain + "\n subDomain:" + subDomain + "\n groupName:" + newGroupName);

}

function bl_SetFlashMovieClip(strMovieClip)
{
	strMCName =	"/" + strMovieClip + ":";
}

