var TerraExplorer;
var CurrentVersion = "4.6.2";
var InstallOK = true;
var OpenFileType = "";
var OpenFileURL = "";

//TODO: Set the path of these variables to your web site

var MyPath;
MyPath = "http://www.skylinesoft.com/interactive/detect";

//InstallURL - Determines the path for TerraExplorer install
var InstallURL;
InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/install/TE.cab";

//InstallVersion - Determines the minimum version for TerraExplorer install
//Format: "X,X,X,X"
var InstallVersion;
InstallVersion = "4,6,2,7";

//UpdateURL - Determines the default path for TerraExplorer updates
var UpdateURL;
UpdateURL = "http://www.skylinesoft.com/interactive/terraexplorer/install/basic.cab";

//StartPageURL - Determines the default path for the start page displayed from Help Menu -> Terraexplorer Home
var StartPageURL;
StartPageURL = "http://www.skylinesoft.com/Interactive/TerraExplorer/texp_startpage.asp?te";

//QuickGuideURL - Determines the default path for TerraExplorer quick guide help
// Help Menu -> TerraExplorer Help Topics F1
var QuickGuideURL;
QuickGuideURL = "http://www.skylinesoft.com/Interactive/TerraExplorer/texp_quickguide.asp?te";

//FlightInstructionsURL - Determines the default path for TerraExplorer flight instructions
// Help Menu -> Flight instructions
var FlightInstructionsURL;
FlightInstructionsURL = "http://www.skylinesoft.com/Interactive/TerraExplorer/texp_flight-instructions.htm";

//ManualDownloadURL - Determines the default path for TerraExplorer manual download
var ManualDownloadURL;
ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp";

//Inject a div for loading TerraExplorer 
document.write( "<!-- LoadingDiv -->" +
				"<DIV id='LoadingDiv' name='LoadingDiv' " +
				"style='position:absolute;" +
				"width:340;" +
				"height:220px;" +
				"z-index:0;" +
				"left: 50px;" +
				"top: 80px;" +
				"background-color: ffffff;" +
				"layer-background-color: ffffff;" +
				"border: 10px solid #808080;" +
				"visibility: hidden'>" +
				"<Span id = 'DivText' name = 'DivText'> "+
				"<img src='"+MyPath+"/Sky-Delivering.gif' border=0><br><center><br><span> Loading TerraExplorer v"+CurrentVersion+" Installation Kit. </span><br><span> This may take few minutes. </span><br><br><img src='"+MyPath+"/loading-anim.gif' border=0></center>" +
				"</Span>" +
				"<Span id = 'myObj' name = 'myObj'></Span>" +
				"</DIV>" +
				"<!-- LoadingDiv -->" );



function DetectInstallRun (URL, Type)//Type = "Fly" or "WebPage"
{
	var ret = DetectTE();
	var InnerHTMLText = "";
	var StatusText = "";
	OpenFileURL = URL;
	OpenFileType = Type;
	
	if (ret)	
	{
		LoadingDiv.style.visibility = 'visible';
		
		
		InnerHTMLText = "<OBJECT ID='TE' CLASSID='CLSID:3a4f9191-65a8-11d5-85c1-0001023952c1' " + 
			"onreadystatechange = 'onTEInstalled()' " +
			"onerror = 'InstallOK = false' "+ 
			"style = 'visibility:hidden' "+
			" codebase='" + InstallURL + "#Version=" + InstallVersion +"'>" +
			"<PARAM NAME='UpdateURL' VALUE=" + UpdateURL + ">"	+
			"<PARAM NAME='StartPageURL' VALUE=" + StartPageURL+ ">"	+
			"<PARAM NAME='QuickGuideURL' VALUE=" + QuickGuideURL+ ">"	+
			"<PARAM NAME='FlightInstructionsURL' VALUE=" + FlightInstructionsURL+ ">"	+
			"</OBJECT>";
			

			//StatusText = "<br><br><center> Downloading... </center>";
			//LoadingDiv.all ("DivText").innerHTML = StatusText;
			LoadingDiv.all ("myObj").innerHTML = InnerHTMLText;
			
			
	}
}

function DetectInstallRunLanguage (URL, Type, Language)//Type = "Fly" or "WebPage"
{
	var ret = DetectTE();
	var InnerHTMLText = "";
	var StatusText = "";
	OpenFileURL = URL;
	OpenFileType = Type;

	if (Language == "French")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/French/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=French";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/French/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Fr_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Fr_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Fr_texp_startpage";
	 }
	if (Language == "German")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/German/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=German";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/German/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Gr_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Gr_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Gr_texp_startpage";
	 }
	if (Language == "Spanish")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/Spanish/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=Spanish";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/Spanish/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sp_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sp_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sp_texp_startpage";
	 }
	if (Language == "TraditionalChinese")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/TraditionalChinese/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=TraditionalChinese";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/TraditionalChinese/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Tc_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Tc_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Tc_texp_startpage";
	 }
	if (Language == "SimplifiedChinese")
	 {	
		InstallURL = "http://www.skylinesoft.com/interactive/terraexplorer/SimplifiedChinese/install/TE.cab";
		ManualDownloadURL = "http://www.skylinesoft.com/interactive/terraexplorer/texp_manual-download.asp?Lang=SimplifiedChinese";
		UpdateURL =  "http://www.skylinesoft.com/interactive/terraexplorer/SimplifiedChinese/install/basic.cab";
		QuickGuideURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sc_QuickGuide_300";
		FlightInstructionsURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sc_FlightInstructions_300";
		StartPageURL = "http://www.skylinesoft.com/terraexplorerlinks.asp?linkID=Sc_texp_startpage";
	 }

	if (ret)	
	{
		LoadingDiv.style.visibility = 'visible';
		
		
		InnerHTMLText = "<OBJECT ID='TE' CLASSID='CLSID:3a4f9191-65a8-11d5-85c1-0001023952c1' " + 
			"onreadystatechange = 'onTEInstalled()' " +
			"onerror = 'InstallOK = false' "+ 
			"style = 'visibility:hidden' "+
			" codebase='" + InstallURL + "#Version=" + InstallVersion +"'>" +
			"<PARAM NAME='UpdateURL' VALUE=" + UpdateURL + ">"	+
			"<PARAM NAME='StartPageURL' VALUE=" + StartPageURL+ ">"	+
			"<PARAM NAME='QuickGuideURL' VALUE=" + QuickGuideURL+ ">"	+
			"<PARAM NAME='FlightInstructionsURL' VALUE=" + FlightInstructionsURL+ ">"	+
			"</OBJECT>";
			

			//StatusText = "<br><br><center> Downloading... </center>";
			//LoadingDiv.all ("DivText").innerHTML = StatusText;
			LoadingDiv.all ("myObj").innerHTML = InnerHTMLText;
			
			
	}
}

function onTEInstalled()
{
	LoadingDiv.style.visibility = 'hidden';
	if (LoadingDiv.readyState == 'complete')  
	{
		if (InstallOK) 
		{
			if (OpenFileType == 'Fly')
			{
			    var TE2 = new ActiveXObject("TerraExplorer2.TerraExplorer");
			    TE2.Load (OpenFileURL);
			}
			else if (OpenFileType == 'WebPage')
			{
				window.parent.location.href =  OpenFileURL;
			}
			else 
				alert ('Error');
		}
		else
		{
			str = "Error: Cannot run automated installation.\nIf you are using Microsoft Windows XP Service Pack 2 follow these instructions:\n1. Click on the Cancel button. \n2. Click on the security warning that appears at the top of this window\n3. Select the 'Allow this page to install ActiveX controls' option\n4. Click on the demo click again.\n\nTo Manually install TerraExplorer click OK or Cancel to abort."
			if (window.confirm(str) == true) 
			{
				document.location.href = ManualDownloadURL;
			}						
		}
	}	
}

/*DetectTE - Checks if TerraExplorer is already installed on the client computer.*/
function DetectTE()
{ 
    try
	{
		// Check if TE is installed
		TeInstalled = new ActiveXObject("TEDetect.TETest"); 

		if (TeInstalled != null)
		{
			TeInstalled = null;	// Make sure that the "TEDetect.TETest" ActiveX will not stay alive (otherwise it will interrupt the installation of the new version)

			//Make sure we can run TerraExplorer
			TerraExplorer = new ActiveXObject("TerraExplorer2.TerraExplorer");

			if (TerraExplorer != null)
			{
				TerraExplorer = null;

				return true;
			}
			else
			{
				//It is abnormal to get here. Prompt for manual install 
				alert("Error: Cannot run TerraExplorer");

				if ( window.confirm("Error: Can not run automated installation.\nTo Manually install TerraExplorer click OK or Cancel to abort") == true)
				{
					document.location.href = ManualDownloadURL;
				}
			}

		}
		else
		{
			throw -1;	// TE is not installed.
		}

		TerraExplorer = null;
	}
	catch(e)
	{	
		// Try install TE
		ret = window.confirm ("TerraExplorer Viewer by Skyline Software Systems is not installed.\n Click OK to automatically install TerraExplorer.\n\n Note: This may take a few minutes...");
		// if ret = OK change cursor
		return ret
	}


	return false;
}