/*\
 *	
 *	JavaScript function library for auto-detect client browser's user agent
 *	
\*/



//-- Declaration of global variables --\\

var garrIdentifier = new Array();

//-- [Browser] --\\

garrIdentifier["Browser"] = new Array();

//-- [Browser][Identifier] --\\

garrIdentifier["Browser"]["Identifier"] = 
	new Array(
		"MSIE",
		"Firefox",
		"Opera",
		"Netfront",
		"Safari",
		"Camino",
		"Netscape"
	)
;

//-- [Browser][Name] --\\

garrIdentifier["Browser"]["Name"] = 
	new Array(
		"Microsoft Internet Explorer",
		"Mozilla Firefox",
		"Opera Browser",
		"Netfront Browser",
		"Safari Browser",
		"Camino Browser",
		"Netscape Navigator"
	)
;

//-- [Platform] --\\

garrIdentifier["Platform"] = new Array();

//-- [Platform][Identifier] --\\

garrIdentifier["Platform"]["Identifier"] = 
	new Array(
		"Windows",
		"Windows NT",
		"Mac",
		"MacOS"
	)
;

//-- [Platform][Name] --\\

garrIdentifier["Platform"]["Name"] = 
	new Array(
		"Microsoft Windows",
		"Microsoft Windows NT",
		"Apple MacOS",
		"Apple MacOS"
	)
;

//-- Get MIME Type information from client's web browser --\\

if (
	window.navigator.mimeTypes
	&&
	(window.navigator.mimeTypes.constructor==Object)
) {
	for (var i=0; i<window.navigator.mimeTypes.length; i++) {
		if (!garrIdentifier["MIME"])
			garrIdentifier["MIME"] = new Array();
		if (garrIdentifier["MIME"].constructor!=Array)
			garrIdentifier["MIME"] = new Array();
		if (!garrIdentifier["MIME"]["Type"])
			garrIdentifier["MIME"]["Type"] = new Array();
		if (garrIdentifier["MIME"]["Type"].constructor!=Array)
			garrIdentifier["MIME"]["Type"] = new Array();
		if (!garrIdentifier["MIME"]["Description"])
			garrIdentifier["MIME"]["Description"] = new Array();
		if (garrIdentifier["MIME"]["Description"].constructor!=Array)
			garrIdentifier["MIME"]["Description"] = new Array();
		garrIdentifier["MIME"]["Type"][i] = window.navigator.mimeTypes[i].type;
		garrIdentifier["MIME"]["Description"][i] = window.navigator.mimeTypes[i].description;
	}
}

//-- Get browser information from client's web browser --\\

function getBrowser_Info() {
	//-- Declarations of variables --\\
	var arrResult,
			arrUserAgent,
			strUserAgent,
			strScreenSize,
			objRE
	;
	//-- Retrieve User Agent from client's web browser --\\
	strUserAgent = window.navigator.userAgent;
	if (
		strUserAgent.indexOf("(")
		&&
		strUserAgent.indexOf(")")
		&&
		(strUserAgent.indexOf(")")>strUserAgent.indexOf("("))
	) {
		//-- Separate retrieved User Agent into 3 parts: part1 (part2) part3 --\\
		arrUserAgent			= new Array();
		arrUserAgent[0]		= strUserAgent.substring(0, strUserAgent.indexOf("("));
		arrUserAgent[1]		= strUserAgent.substring(strUserAgent.indexOf("(")+1, strUserAgent.indexOf(")"));
		arrUserAgent[2]		= strUserAgent.substring(strUserAgent.indexOf(")")+1, strUserAgent.length);
		objRE							= new RegExp("^\s+|\s+$", "gi");
		arrUserAgent[0]		= arrUserAgent[0].replace(objRE, "");
		objRE							= new RegExp("^\s+|\s+$", "gi");
		arrUserAgent[1]		= arrUserAgent[1].replace(objRE, "");
		objRE							= new RegExp(" *; *", "gi");
		arrUserAgent[1]		= arrUserAgent[1].replace(objRE, ";");
		objRE							= new RegExp("^\s+|\s+$", "gi");
		arrUserAgent[2]		= arrUserAgent[2].replace(objRE, "");
		arrUserAgent[0]		= arrUserAgent[0].split(" ");
		arrUserAgent[1]		= arrUserAgent[1].split(";");
		arrUserAgent[2]		= arrUserAgent[2].split(" ");
		//-- Split each part of retrieved User Agent information into an array of information --\\
		strUserAgent	= null;
		strUserAgent	= new Array();
		for (var i=0; i<arrUserAgent[0].length; i++)
			strUserAgent[strUserAgent.length] = arrUserAgent[0][i];
		for (var i=0; i<arrUserAgent[1].length; i++)
			strUserAgent[strUserAgent.length] = arrUserAgent[1][i];
		for (var i=0; i<arrUserAgent[2].length; i++)
			strUserAgent[strUserAgent.length] = arrUserAgent[2][i];
		//-- Collect any existing information from the array of User Agent information --\\
		arrResult = null;
		for (var i=0; i<strUserAgent.length; i++) {
			//-- Collect Browser information: Name & Version --\\
			for (var j=0; j<garrIdentifier["Browser"]["Identifier"].length; j++) {
				if (strUserAgent[i].toLowerCase().indexOf(garrIdentifier["Browser"]["Identifier"][j].toLowerCase())>=0) {
					if (!arrResult)
						arrResult = new Array();
					if (arrResult.constructor!=Array)
						arrResult = new Array();
					if (!arrResult["Browser"])
						arrResult["Browser"] = new Array();
					if (arrResult["Browser"].constructor!=Array)
						arrResult["Browser"] = new Array();
					objRE = new RegExp("[A-Za-z ]+", "gi");
					arrResult["Browser"]["Name"] = strUserAgent[i].match(objRE);
					if (arrResult["Browser"]["Name"]!=null) {
						objRE = new RegExp("^ *| *$", "gi");
						arrResult["Browser"]["Name"] = arrResult["Browser"]["Name"].toString().replace(objRE, "");
						if (arrResult["Browser"]["Name"].toLowerCase()==garrIdentifier["Browser"]["Identifier"][j].toLowerCase())
							arrResult["Browser"]["Name"] = garrIdentifier["Browser"]["Name"][j];
					}
					objRE = new RegExp("[0-9]+\.*", "gi");
					arrResult["Browser"]["Version"] = strUserAgent[i].match(objRE);
					if (arrResult["Browser"]["Version"]!=null) {
						objRE = new RegExp("^ *| *$", "gi");
						arrResult["Browser"]["Version"] = arrResult["Browser"]["Version"].toString().replace(objRE, "");
					}
					if (arrResult["Browser"]["Version"]==null) {
						if (i<strUserAgent.length-1) {
							objRE = new RegExp("[0-9]+\.*", "gi");
							arrResult["Browser"]["Version"] = strUserAgent[i+1].match(objRE);
							if (arrResult["Browser"]["Version"]!=null) {
								objRE = new RegExp("^ *| *$", "gi");
								arrResult["Browser"]["Version"] = arrResult["Browser"]["Version"].toString().replace(objRE, "");
								i++;
							}
						}
					}
					break;
				}
			}
			//-- Collect Platform information: Name & Version --\\
			for (var j=0; j<garrIdentifier["Platform"]["Identifier"].length; j++) {
				if (strUserAgent[i].toLowerCase().indexOf(garrIdentifier["Platform"]["Identifier"][j].toLowerCase())>=0) {
					if (!arrResult)
						arrResult = new Array();
					if (arrResult.constructor!=Array)
						arrResult = new Array();
					if (!arrResult["Platform"])
						arrResult["Platform"] = new Array();
					if (arrResult["Platform"].constructor!=Array)
						arrResult["Platform"] = new Array();
					objRE = new RegExp("[A-Za-z ]+", "gi");
					arrResult["Platform"]["Name"] = strUserAgent[i].match(objRE);
					if (arrResult["Platform"]["Name"]!=null) {
						objRE = new RegExp("^ *| *$", "gi");
						arrResult["Platform"]["Name"] = arrResult["Platform"]["Name"].toString().replace(objRE, "");
						if (arrResult["Platform"]["Name"].toLowerCase()==garrIdentifier["Platform"]["Identifier"][j].toLowerCase())
							arrResult["Platform"]["Name"] = garrIdentifier["Platform"]["Name"][j];
					}
					objRE = new RegExp("[0-9]+\.*", "gi");
					arrResult["Platform"]["Version"] = strUserAgent[i].match(objRE);
					if (arrResult["Platform"]["Version"]!=null) {
						objRE = new RegExp("^ *| *$", "gi");
						arrResult["Platform"]["Version"] = arrResult["Platform"]["Version"].toString().replace(objRE, "");
					}
					break;
				}
			}
			//-- Collect Device information: screen size --\\

			strScreenSize = null;
			objRE = new RegExp("[0-9]+x[0-9]+", "gi");
			strScreenSize = strUserAgent[i].match(objRE);
			if (strScreenSize!=null) {
				if (!arrResult)
					arrResult = new Array();
				if (arrResult.constructor!=Array)
					arrResult = new Array();
				if (!arrResult["Screen"])
					arrResult["Screen"] = new Array();
				if (arrResult["Screen"].constructor!=Array)
					arrResult["Screen"] = new Array();
				arrResult["Screen"]["Width"]		= parseInt(strScreenSize.substr(0, strScreenSize.toLowerCase().indexOf("x")));
				arrResult["Screen"]["Height"]		= parseInt(strScreenSize.substr(strScreenSize.toLowerCase().indexOf("x")+1, strScreenSize.length));
				if (i>0) {
					if (!arrResult["Device"])
						arrResult["Device"] = new Array();
					if (arrResult["Device"].constructor!=Array)
						arrResult["Device"] = new Array();
					arrResult["Device"]["Name"] = strUserAgent[i-1];
				}
			}

		}
		//-- Collect MIME Type information: MIME Type availability --\\
		if (
			(garrIdentifier["MIME"] && (garrIdentifier["MIME"].constructor==Array))
			&&
			(garrIdentifier["MIME"]["Type"] && (garrIdentifier["MIME"]["Type"].constructor==Array))
			&&
			(garrIdentifier["MIME"]["Description"] && (garrIdentifier["MIME"]["Description"].constructor==Array))
		) {
			for (var j=0; j<garrIdentifier["MIME"]["Type"].length; j++) {
				switch (garrIdentifier["MIME"]["Type"][j].toLowerCase()) {
					//-- MIME Type: application/x-shockwave-flash --\\
					case "application/x-shockwave-flash":
						if (!arrResult)
							arrResult = new Array();
						if (arrResult.constructor!=Array)
							arrResult = new Array();
						if (!arrResult["MIME"])
							arrResult["MIME"] = new Array();
						arrResult["MIME"]["ShockwaveFlash"] = "Yes";
						break;
					//-- MIME Type: application/pdf --\\
					case "application/pdf":
						if (!arrResult)
							arrResult = new Array();
						if (arrResult.constructor!=Array)
							arrResult = new Array();
						if (!arrResult["MIME"])
							arrResult["MIME"] = new Array();
						arrResult["MIME"]["AcrobatPDF"] = "Yes";
						break;
				}
			}
		}
	}
	return arrResult;
}

