﻿//global flags for Browser and version Check also for meeting the recommended browswer and version
var gblnVersion = false;
var gblnBrowser = false;
var gblnRecBrw = false;
var gblnRecBrwVer = false;

/*************************************************************
*       Name: determine_browswer                             *
*    Purpose: To determine which browser the client is using *
*     Inputs: n/a                                            *
*    Outputs: A string consisting of the browser name or n/a *
*             if not one of the 4 standard browsers          *
* Created by: Donald McLean                                  *
* Created On: February 27th 2009                             *
* Updated by: Donald McLean                                  *
* Updated On: March 2nd 2009                                 *
*************************************************************/
function determine_browswer()
{
	var aszBwrs = new Array ("MSIE","Firefox","Opera","Chrome","Safari");
	var szBrowserDetails = navigator.userAgent;
	var intSearch = 0;
	
	for (i = 0; i < aszBwrs.length; i++)
	{
		intSearch = szBrowserDetails.indexOf(aszBwrs[i]);
		if (intSearch > -1) { return aszBwrs[i];}
	}//end for
	return "n/a";
}//end determine_browswer

/**********************************************************************
*       Name: checkSptBrw                                             *
*    Purpose: To determine if the client's browser is one that is     *
*             supported browser                                       *
*     Inputs: A string consisting of all the supported browsers in    *
*             format of ; seperating browsers, | seperating browser   *
*             from versions and ,  seperating browser versions        *
*             ex. "MSIE|7.0,6.0;Firefox|3.0,2.0;Opera|9.6;Chrome|1.0" *
*    Outputs: Sets global flags based if the clients browser is one   *
*             of the supported browsers                               *
* Created by: Donald McLean                                           *
* Created On: February 27th 2009                                      *
* Updated by: Donald McLean                                           *
* Updated On: March 2nd 2009                                          *
**********************************************************************/
function checkSptBrw(szSptBrws)
{
	var szBrowserDetails = navigator.userAgent;
	var aszBrowserAndVer = szSptBrws.split(";");
	var intSearch = 0;
	gblnVersion = false;
	gblnBrowser = false;
	for (i = 0; i < aszBrowserAndVer.length; i++) 
	{
		var aszBroswerInfo = aszBrowserAndVer[i].split("|");
		intSearch = szBrowserDetails.indexOf(aszBroswerInfo[0]);
		if (intSearch > -1)
		{
			gblnBrowser = true;
			var aszVersion = aszBroswerInfo[1].split(",");
			for (j = 0; j < aszVersion.length; j++)
			{
				szSearch = aszBroswerInfo[0] + "/" + aszVersion[j];
				if (aszBroswerInfo[0]=='MSIE') {szSearch = aszBroswerInfo[0] + " " + aszVersion[j]}
				intSearch = szBrowserDetails.indexOf(szSearch);
				if (intSearch > -1)
				{
					gblnVersion = true;
				}//end if
			}//end for
		}//end if
	}//end for
}//end function checkSptBrw

/*********************************************************************
*       Name: checkRecBrw                                            *
*    Purpose: To determine if the client's browser is the recommend  *
*             browser                                                *
*     Inputs: A string consisting of the recommend browsers in       *
*             format of | seperating browser from version            *
*             ex. "MSIE|7.0                                          *
*    Outputs: Sets global flags based if the clients browser is one  *
*             of the supported browsers                              *
* Created by: Donald McLean                                          *
* Created On: February 27th 2009                                     *
* Updated by: Donald McLean                                          *
* Updated On: March 2nd 2009                                         *
*********************************************************************/
function checkRecBrw(szRecBrw)
{
	var szBrowserDetails = navigator.userAgent;
	var aszRecBrw = szRecBrw.split("|");

	gblnRecBrw = false;
	gblnRecBrwVer = false;
	
	intSearch = szBrowserDetails.indexOf(aszRecBrw[0]);
	if (intSearch > -1)
	{
		gblnRecBrw = true;	
		szSearch = aszRecBrw[0] + "/" + aszRecBrw[1];
		if (aszRecBrw[0]=='MSIE') {szSearch = aszRecBrw[0] + " " + aszRecBrw[1]}
		intSearch = szBrowserDetails.indexOf(szSearch);
		if (intSearch > -1) {gblnRecBrwVer = true;}
	}//end if
}//end function checkRecBrw

/*********************************************************************************************
*       Name: CheckRequirements                                                              *
*    Purpose: To compare the clients PC to the Applications Requirements.                    *
*             The function checks:                                                           *
*                 A) Is the browser supported?                                               *
*                 B) Is the broswer version supported?                                       *
*                 C) Is the browser the recommended browswer?                                *
*                 D) Is the browser version the recommended browswer version?                *
*                 E) Are Cookies enabled?                                                    *
*                 F) Are Popups enabled?                                                     *
*                 G) Is Screen resolution set to optimal settings?                           *
*     Inputs: szAppName = name of the app                                                    *
*             szSptBrws = supported browswers                                                *
*             szRecBrw = recommended browser                                                 *
*             blnShowRecBrwMsg = true if you want a warning displayed  using the             *
*                                recommended browser                                         *
*             fltMGVer = minimum MapGuide Version required                                   *
*             blnCookies = true if cookies are required                                      *
*             blnPopups = true if popups are required                                        *
*             intWidth = minimum screen width                                                *
*             intHeight = minimum screen hieght                                              *
*    Outputs: returns true if meets requirements, else it navigates to Error page!           *
*             sets Id's for the errors:                                                      *
*             A) browserError - sets the display property to display                         *
*                browserError_img - sets innerText property to the error image               *
*                browserError_msg - sets innerText property to the error messsge             * 
*             B) browserError - sets the display property to display                         *
*                browserError_img - sets innerText property to the error image               *
*                browserError_msg - sets innerText property to the error messsge             *
*             C) browserWarning - sets the display property to display                       *
*                browserWarning_img - sets innerText property to the warning image           *
*                browserWarning_msg - sets innerText property to the warning messsge         * 
*             D) browserWarning - sets the display property to display                       *
*                browserWarning_img - sets innerText property to the warning image           *
*                browserWarning_msg - sets innerText property to the warning messsge         * 
*             E) cookiesError - sets the display property to display                         *
*                cookiesError_img - sets innerText property to the warning image             *
*                cookiesError_msg - sets innerText property to the warning messsge           *
*             F) popupError - sets the display property to display                           *
*                popupError_img - sets innerText property to the warning image               *
*                popupError_msg - sets innerText property to the warning messsge             *
*             G) resolutionWarning - sets the display property to display                    *
*                resolutionWarning_img - sets innerText property to the warning image        *
*                resolutionWarning_msg - sets innerText property to the warning messsge      *
*                resolutionWarning_msg2 - sets innerText property to the instruction messsge *
* Created by: Donald McLean                                                                  *
* Created On: February 18th 2009                                                             *
* Updated by: Donald McLean                                                                  *
* Updated On: February 27th 2009                                                                 *
*********************************************************************************************/
function CheckRequirements(szAppName,szBrw,szSptBrws,szRecBrw,fltMGVer,blnCookies,blnPopups,intWidth,intHeight,blnShowWng)
{
	/* Declaring Error and Warning flags */
	var blnErr = false;
	var blnWng = false;

    /***********************************************************************
	* Get the cookie information for checks                                *
	* A - D) Is browser & version supported? Is Browser recommend Version? *
	***********************************************************************/
	var szBrwVer = navigator.appVersion;
	var fltVer = parseFloat(szBrwVer);
	var szRecBrwInfo = "";
	var szReqBrwAndVer = "";
	var szReqBrw = "";
	var szDownloadPath = "";
	var szTarget = "";

	/******************************************************************************
	* Checks A - D) Is browser & version supported? Is Browser recommend Version? *
	******************************************************************************/
	var szSearch = "";
	
	/* Check A & B) for supported browser and version*/
	checkSptBrw (szSptBrws);

	/* check C & D) for recommended browser and version */
	if (blnShowWng) { checkRecBrw (szRecBrw); }

	var aszRecBrw = szRecBrw.split("|");
	if (aszRecBrw[0] == "MSIE")
	{ 
		if (aszRecBrw[0] == "8.0")
		{ szDownloadPath = "http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=341c2ad5-8c3d-4347-8c03-08cdecd8852b"; szTarget=" target=\"blank\""; }
		else
		{ szDownloadPath = "http://www.microsoft.com/downloads/details.aspx?FamilyId=9AE91EBE-3385-447C-8A30-081805B2F90B&displaylang=en"; szTarget=" target=\"blank\""; }
	}
	if (aszRecBrw[0] == 'Firefox')
	{ szDownloadPath = "http://www.cgis.com/downloads/FirefoxSetup3.0.4.exe"; }

	if (aszRecBrw[0] == 'MSIE') {aszRecBrw[0] = "Internet Explorer";}
	if (szBrw == 'MSIE') {szBrw = "Internet Explorer";}

	szReqBrw = aszRecBrw[0];
	szReqBrwVer = aszRecBrw[1];
	szReqBrwAndVer = aszRecBrw[0] + " " + aszRecBrw[1];

	/***************************************************************
	* Get the cookie information for check E) Are Cookies enabled? *
	***************************************************************/
	var blnCookieEnabled = false;
	blnCookieEnabled = (navigator.cookieEnabled)? true : false;

	/*******************************
	* Check F) Are Popups enabled? *
	*******************************/
	if (blnPopups)
	{
		var blnPopUpBlockerEnabled;
		var	popWin = window.open('about:blank','','width=1,height=1,left=0,top=0,scrollbars=no');
		if(popWin)
		{	popWin.close();
			blnPopUpBlockerEnabled = true;
		}//end if
		else
		{
			blnPopUpBlockerEnabled = false;
		}//end else
	}//emd if
	
	/***************************************************
	* get screen resolution information for check      *
	* G) Is Screen resolution set to optimal settings? *
	***************************************************/
	var intScreenWidth = screen.width;
	var intScreenHeight = screen.height;

	/********************
	* Displaying errors *
	*********************
	* Note: to change the file so it 
	* modifies elements in another
	* a page in another frame add
	* top.framename before
	* document.getElementById("")
	* where framename is the name of frame
	* with the elements to change
	********************************
	* A) Is the browser supported? *
	*******************************/
	if (!gblnBrowser)
	{
		blnErr = true;
		var div = document.getElementById("browserError");
		var msg = document.getElementById("browserError_msg");
		var img = document.getElementById("browserError_img");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Error.gif' />"
		msg.innerHTML = "It appears that you are using " + szBrw + ".  This browser is presently not supported. " + szAppName + " works best with " + szReqBrw + ".  Click <a class=\"ErrorOrWarningLink\" href=\"" + szDownloadPath + "\"" + szTarget + ">here</a> to get " + szReqBrw + ".";
	}//end if

	/***************************************
	* B) Is the broswer version supported? *
	***************************************/
	if (gblnBrowser && !gblnVersion)
	{
		blnErr = true;
		var div = document.getElementById("browserError");
		var msg = document.getElementById("browserError_msg");
		var img = document.getElementById("browserError_img");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Error.gif' />"
		msg.innerHTML = "It appears that you are using an incompatible version of " + szBrw + ".  The recommened browser for "+ szAppName + " is "+ szReqBrw + " version " + szReqBrwVer + ". Click <a class=\"ErrorOrWarningLink\" href=\"" + szDownloadPath + "\"" + szTarget + ">here</a> to get " + szReqBrw + ".";
	}//end if
	
	/**********************************************
	* C) Is the browser the recommended browswer? *
	**********************************************/
	if (gblnBrowser && blnShowWng && !gblnRecBrw) 
	{
		blnWng = true;
		var div = document.getElementById("browserWarning");
		var msg = document.getElementById("browserWarning_msg");
		var img = document.getElementById("browserWarning_img");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Warning.gif'/>"
		msg.innerHTML = "It appears that you are using " + szBrw + ".  This is supported, however " + szAppName + " works best with " + szReqBrw + ".  Click <a class=\"ErrorOrWarningLink\" href=\"" + szDownloadPath + "\"" + szTarget + ">here</a> to get " + szReqBrw + ".";
	}//end if
	
	/**************************************************************
	* D) Is the browser version the recommended browswer version? *
	**************************************************************/
	if (gblnBrowser && blnShowWng && gblnRecBrw && !gblnRecBrwVer)
	{
		blnWng = true;
		var div = document.getElementById("browserWarning");
		var msg = document.getElementById("browserWarning_msg");
		var img = document.getElementById("browserWarning_img");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Warning.gif'/>"
		msg.innerHTML = "It appears that you are using " + szBrw + ". " + szAppName + " works best with " + szReqBrw + " version " + szReqBrwVer + ".  Click <a class=\"ErrorOrWarningLink\" href=\"" + szDownloadPath + "\"" + szTarget + ">here</a> to get " + szReqBrw + ".";
	}//end if

	/**************************
	* E) Are Cookies enabled? *
	**************************/
	if (blnCookies && !blnCookieEnabled)
	{
		blnErr = true;
		var div = document.getElementById("cookiesError");
		var msg = document.getElementById("cookiesError_msg");
		var img = document.getElementById("cookiesError_img");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Error.gif'/>"
		msg.innerHTML = szAppName + " requires cookies to be enabled.  Please click <a class=\"ErrorOrWarningLink\" href=javascript:showOrHideInstructions('cookiesError_instructions');>here</a> for instructions on how to enable cookies for your browser.";
	}//end if
	
	/*************************
	* F) Are Popups enabled? *
	*************************/
	if (blnPopups && !blnPopUpBlockerEnabled)
	{
		blnErr = true;
		var div = document.getElementById("popupError");
		var msg = document.getElementById("popupError_msg");
		var img = document.getElementById("popupError_img");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Error.gif'/>"
		msg.innerHTML = szAppName + " requires popup blockers to be disabled for this site.  Please click <a class=\"ErrorOrWarningLink\" href=javascript:showOrHideInstructions('popupError_instructions');>here</a> for instructions on how to disable popup blockers for " + szAppName + ".";
	}//end if
	
	/***************************************************
	* G) Is Screen resolution set to optimal settings? *
	***************************************************/
	if (blnShowWng && (intScreenWidth < intWidth || intScreenHeight < intHeight))
	{
		blnWng = true;
		var div = document.getElementById("resolutionWarning");
		var msg = document.getElementById("resolutionWarning_msg");
		var img = document.getElementById("resolutionWarning_img");
		var msg2 = document.getElementById("resolutionWarning_msg2");
		div.style.display = "inline";
		img.innerHTML = "<img src='images/Warning.gif'/>"
		msg.innerHTML = szAppName + " works best with a screen resolution of " + intWidth + "x" + intHeight + " or greater.  It appears your screen resolution is set to " + intScreenWidth + "x" + intScreenHeight + ". Please click <a class=\"ErrorOrWarningLink\" href=javascript:showOrHideInstructions('resolutionWarning_instructions');>here</a> for instructions on how to alter your screen resolution.";
		msg2.innerHTML = "<b>Windows XP</b>:  Open Control Panel > double-click display icon > settings tab > move the \"resolution\" slider right until the resolution is " + intWidth + "x" + intHeight + " or higher.";
	}//end if
	
	//if there is no errors return true; if there is errors hide the map
	if (blnShowWng)
	{if (!blnErr && !blnWng) {return true;}}
	else
	{if (!blnErr) {return true;}}
	document.getElementById("theMap").style.display = "none";
	return false;
}//end CheckRequirements function

/***************************************************
*       Name: splitQueryStringValue                *
*    Purpose: splits the cookie value into an      *
*             associative array & seperates        *
*             property and value pairs = seperates *
*             property and value                   *
*      Input: szValue = value of the cookie        *
*     Output: associative array containg cookie    *
* Created by: Donald McLean                        *
* Created On: February 23rd 2009                   *
* Updated by: Donald McLean                        *
* Updated On: March 2nd 2009                       *
***************************************************/
function splitQueryStringValue(szValue)
{
	var separated_values = szValue.split("&");

	// loop through the list of name:values and load
	// up the associate array
	var property_value = "";
	var the_info = new Array();
	for (i = 0; i < separated_values.length; i++) 
	{
		property_value = separated_values[i];
		var broken_info = property_value.split("=");
		var the_property = broken_info[0];
		var the_value = broken_info[1];
		the_info[the_property] = the_value;
	}//end for	
	return the_info;
}//end function splitQueryStringValue

/***************************************************
*       Name: showOrHideInstructions               *
*    Purpose: set a given element to display       *
*      Input: element = element to dislpay         *
*     Output: n/a                                  *
* Created by: n/a                                  *
* Created On: n/a                                  *
* Updated by: Donald D. McLean                     *
* Updated On: March 3rd, 2009                      *
***************************************************/
function showOrHideInstructions(element)
{
	var row = document.getElementById(element);
	if (row.style.display == "inline")
		row.style.display = "none";
	else
		row.style.display = "inline";
}//end function showInstructions