//
// CmxBrowserIE.js
//
// Purpose	IE specific javascript functions for all web pages.
//

function ClientMgrCreate(sClientVersion, sStatusMsg)
{
 
	defaultStatus = sStatusMsg;
	try
	{
		var CMObj = new ActiveXObject("RASclntmgr.LaunchpadMgr");
		defaultStatus = " ";
		
		var sCurrClientVersion = CMObj.GetVersion();

		// Client version on client should be greater or equal to Titan Version.
		if(IsCurrVersionGreaterOrEqual(sCurrClientVersion, "1.0.0.43"))
			return (CMObj);
		// else need to download client
		else
		{
			// To check if reboot is needed or not
			var bUpgrade = CMObj.UpgradeRequest();
			if(bUpgrade == "0")
			{
				location="CmxDownload.php?pgid=CmxPgid_330&reboot=true";   //reboot needed
				return (null);
			}	
			else
			{
				location="CmxDownload.php?pgid=CmxPgid_330";
				return (null);
			}				
		}
	}
	catch(e)
	{
		location="CmxDownload.php?pgid=CmxPgid_330";
		return (null);
	}
}

function ClientMgrCreateWithDownloadCheck(sClientVersion, sForceClientDownload,sStatusMsg)
{



	defaultStatus = sStatusMsg;	
	try
	{
		var CMObj = new ActiveXObject("RASclntmgr.LaunchpadMgr");
		defaultStatus = " ";		
		var sCurrClientVersion = CMObj.GetVersion();		
		// Client version on client should be greater or equal to Titan Version.
		if(IsCurrVersionGreaterOrEqual(sCurrClientVersion, "1.0.0.43"))
		{
			//If Client Version On Local machine is greater or equal to value in Config
			//we don't have to do anything
			
			if(IsCurrVersionGreaterOrEqual(sCurrClientVersion, sClientVersion) == false)
			{
				if(sForceClientDownload == "1")
				{
					location="CmxDownload.php?pgid=CmxPgid_330&reboot=true&Forced=true";
					return (null);
				}
			}
			return (CMObj);
		}
		else // else need to download client
		{
			// To check if reboot is needed or not
			var bUpgrade = CMObj.UpgradeRequest();
			if(bUpgrade == "0")
			{
				location="CmxDownload.php?pgid=CmxPgid_330&reboot=true";   //reboot needed
				return (null);
			}	
			else
			{
				location="CmxDownload.php?pgid=CmxPgid_330";
				return (null);
			}				
		}
	}
	catch(e)
	{
    /*
		location="CmxDownload.asp?pgid=CmxPgid_330";
		return (null);
		*/

		//2008-05-08 修改人;沉 解决反复下载的问题
		var bE0 = false;
		var bE1 = false;
		var bE2 = false;
		var sMsg0 = "";
		var sMsg1 = "";
		var sMsg2 = "";
		var sErrCtrol = ""; 

            sErrCtrol = "创建控件RASclntmgr.LaunchpadMgr出现错误,错误信息:" + e.message + "\n";


			 bE0 = true;
           try
              {
		      var CMObj = new ActiveXObject("GetMachineCodeForClient.GetMC");
			  var s = CMObj.GetMachineCode();
			  }
			  catch(e)
		      {
			    bE1 = true;
				sMsg1 = e.message;
				sErrCtrol += "创建控件GetMachineCodeForClient.GetMC出现错误,错误信息:" + sMsg1 + "\n";
			  }

			try
			{
			 var Ekey = new ActiveXObject("EKeyComexe.EKey.1");
			 var s = Ekey.IsDeviceReady();
			}
			catch(e)
			{
			   bE2 = true;
			   sMsg2 = e.message;
			   sErrCtrol += "创建控件EKeyComexe.EKey.1出现错误,错误信息:" + sMsg2 + "\n";
			}

        if( bE0 && bE1 && bE2)
		{
			location="CmxDownload.php?pgid=CmxPgid_330";
			return (null);		
		}

		var sErr = e.message;
		sErr = sErr.toLowerCase();
		if(sErr.indexOf("automation")!=-1)
		{
		  var s = "\n";
		      s+="上述问题的可以尝试的解决办法:\n\n";
			  s+="首先,请确保 IE|程序|管理加载项 中没有禁用上述出错的控件.如果禁用了请改为启用,再重新启IE,访问,即可解决问题.\n\n如果没有加载上述控件,则请把当前站点添加到受信任站点(可信站点),在点自定义级别,把与ActiveX控件相关的设置全改为启用,\n确定保存后,重新启动IE访问.\n\n";
			  s+="若上述操作后,仍然不能解决此问题,建议您再重新下载安装一下客户端软件,或者与我们客户服务部门联系.";
		  alert(sErrCtrol + s);
		  location="CmxDownload.php?pgid=CmxPgid_330";
		  return(null);		 
		}
		else
		{
			sMsg0 = sMsg1!="" ? "\nError Message 1:"+sMsg1:"";
			sMsg0 += sMsg2!="" ?"\nError Message 2:"+sMsg2:"";
			alert(e.message + sMsg0);
			location="CmxDownload.php?pgid=CmxPgid_330";
			return (null);
		}

	}
}

function ClientMgrInvoke(oClientMgr, sEventName, sParamString)
{
	try
	{
		var sRet = oClientMgr.BrahmaInvoke(sEventName, sParamString);
				return sRet;
	}
	catch(e)
	{   
		alert("oClientMgr.BrahmaInvoke method error! file:CmxBrowserIE.js functionName:ClientMgrInvoke  ErrorInfo: "+e.message);
		return ("");
	}
}

function GetClientId()
{
	var sClientId = "";
	try
	{
		var CMObj = new ActiveXObject("RASclntmgr.LaunchpadMgr");
		defaultStatus = " ";
		var sClientParam = CMObj.BrahmaInvoke("GetClientPrefs", "") + "";
		sClientId = RetrieveValue("ClientId", sClientParam);
	}
	catch(e)
	{
   		// no need to do anything in case of an error
	}
	var sURL ="CmxMultiQuery.php?pgid=CmxPgid_327&NS=F&ClientId=" + sClientId;
	window.location = sURL;
}

//--- Start Menu Functions ---

// Calculate X position
function AbsLeft(obj)
{
	// IE only code. Recursive.
	return obj == null ? 0 : AbsLeft(obj.offsetParent) + obj.offsetLeft;
}
	
// Calculate Y position	
function AbsTop(obj)
{
	// IE only code. Recursive.
	return obj == null ? 0 : AbsTop(obj.offsetParent) + obj.offsetTop;
}

// Add the contents of the NM:MENU element to the start menu control
function AddStartMenu(elParent, sParent)
{
	// IE only code
	var coll = elParent.children;
	for(var i = 0; i < coll.length; i++)
	{
		var el = coll.item(i);
		if(el.id != "m-1")
		{	
		StartMenu.AddItem(el.id, el.DisplayName, "", sParent);
		if (el.tagName == "MENU")
			AddStartMenu(el, el.id);
	}
}
}

// Setup the start menu control with items from the <NM:MENU id="StartMenuInfo"> XML
function StartMenuSetup()
{
	StartMenu.RemoveAllItems();
	var elMenu = document.all["StartMenuInfo"];
	if (elMenu == null)
		return;
	AddStartMenu(elMenu, "");
}

// Set the form control based on the values in the NM:MENUITEM		
function SetShortcutForm(sFormProp, sAttribute, elMenuItem)
{
	var el = document.all[sFormProp];
	if (el != null && elMenuItem[sAttribute] != null)
	{
		el.value = elMenuItem[sAttribute];
	}
}

// Handle a click on a start menu item	
function StartMenuItemClick(id)
{
	try
	{
		var sId = id + "";
		var elStart = document.all["StartMenuInfo"];
		var elMenuItem = elStart.all[sId];
		if (elMenuItem == null)
			return;
		SetShortcutForm("DisplayName", 				"DisplayName", elMenuItem);
		SetShortcutForm("Description", 				"Description", elMenuItem);
		SetShortcutForm("WorkingDirectory",			"WorkingDirectory", elMenuItem);
		SetShortcutForm("ApplicationPath", 			"ApplicationPath", elMenuItem);
		SetShortcutForm("CommandLineParameters",	"CommandLineParameters", elMenuItem);	
	}
	catch(e)
	{
		// no need to do anything in case of an error
	}
}

// Handle a click on the start menu
function StartMenuClick(button)
{
	try
	{
		var el = document.all["selecttype"];
			
		if(el[1].checked==true)
		{		
		StartMenuSetup(); // This function is pretty fast even for large menus
		var x = AbsLeft(button) + 2 - document.body.scrollLeft;
		var y = AbsTop(button) + button.offsetHeight + 2 - document.body.scrollTop;
		StartMenu.Popup(x, y);
	}
	}
	catch(e)
	{
		// no need to do anything in case of an error
	}
}

// Sets the query string value of a particular param
function SetQueryStringParam(sURL, sParam, sValue)
{
		var iParamName = sURL.indexOf(sParam);
		if (iParamName == -1)
		{
			if (sURL.charAt(sURL.length-1) != "&")
				sURL += "&";
			sURL += sParam + sValue;
			return sURL;
		}
		var iEndParamName = sURL.indexOf("&", iParamName);
		if (iEndParamName == -1)
			iEndParamName = sURL.length;
		sURL = sURL.substr(0, iParamName) + sParam + sValue + sURL.substr(iEndParamName);
		return sURL;
}


// Handle a click on the start form server drop down
function ServerFormChange(elSelect)
{
	try
	{
		var sServerName = elSelect.options[elSelect.selectedIndex].value;
		
		// Replace the server name value
		var sURL = SetQueryStringParam(window.location.href, "ServerNameValue=", sServerName);
		window.location.href = sURL;
	}
	catch(e)
	{
		// no need to do anything in case of an error
	}
}

function ClearAll()
{
		
		document.all["DisplayName"].value =" ";
		document.all["Description"].value =" ";
		document.all["WorkingDirectory"].value =" ";
		document.all["ApplicationPath"].value =" ";
		document.all["CommandLineParameters"].value =" ";
}

function OnClick_Radio(tmp)
{
	var SHELL_CONNECTION_SETTING_ID = -4;
	var BLANK_CONNECTION_SETTING_ID = -5;
	try
	{
		if(tmp==1)
		{
			document.all["startmn"].src="Images\\Start_disabled.gif";
			document.all["ConnectionSettingID"].value = SHELL_CONNECTION_SETTING_ID;

			if(document.all["ConnectionSettingID"].value == "")
				document.all["ConnectionSettingID"].value = BLANK_CONNECTION_SETTING_ID;
			StartMenuItemClick("m-1");
		}
		else
		{
			document.all["startmn"].src="Images\\StartMenuDown.gif";
			document.all["ConnectionSettingID"].value = BLANK_CONNECTION_SETTING_ID;
			ClearAll();
		}
	}
	catch(e)
	{
		// The catch will be hit if there are no application servers online
	}
}	

function NativeLaunch_GetClientId()
{
	try{
		var CMObj = new ActiveXObject("RASclntmgr.LaunchpadMgr");
		var sClientParam = CMObj.BrahmaInvoke("GetClientPrefs", "") + "";
		return RetrieveValue("ClientId", sClientParam);
	}
	catch(e)	
	{ 
		return "";
	}
}	
function OnEnableClientFileSystemSharingChange(elSelect)
{
	if(elSelect.options[elSelect.selectedIndex].value == 0)
	{
		document.forms("NMForm").EnableRemovableDriveSharing.selectedIndex = 1;
		document.forms("NMForm").EnableFixedDriveSharing.selectedIndex = 1;
		document.forms("NMForm").EnableNetworkDriveSharing.selectedIndex = 1;
		document.forms("NMForm").EnableCDROMDriveSharing.selectedIndex = 1;
		
		document.forms("NMForm").EnableRemovableDriveSharing.disabled = 1;
		document.forms("NMForm").EnableFixedDriveSharing.disabled = 1;
		document.forms("NMForm").EnableNetworkDriveSharing.disabled = 1;
		document.forms("NMForm").EnableCDROMDriveSharing.disabled = 1;
	}
	else if(elSelect.options[elSelect.selectedIndex].value == 1)
	{
		document.forms("NMForm").EnableRemovableDriveSharing.disabled = 0;
		document.forms("NMForm").EnableFixedDriveSharing.disabled = 0;
		document.forms("NMForm").EnableNetworkDriveSharing.disabled = 0;
		document.forms("NMForm").EnableCDROMDriveSharing.disabled = 0;

		document.forms("NMForm").EnableRemovableDriveSharing.selectedIndex = 0;
		document.forms("NMForm").EnableFixedDriveSharing.selectedIndex = 0;
		document.forms("NMForm").EnableNetworkDriveSharing.selectedIndex = 0;
		document.forms("NMForm").EnableCDROMDriveSharing.selectedIndex = 0;
	}
	
}
