﻿// holds an instance of XMLHttpRequest
var position;
var xmlHttp = createXmlHttpRequestObject()
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
																		"MSXML2.XMLHTTP.5.0",
																		"MSXML2.XMLHTTP.4.0",
																		"MSXML2.XMLHTTP.3.0",
																		"MSXML2.XMLHTTP",
																		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for(i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

// read a file from the server
function process(mnu,pos)
{
	// only continue if xmlHttp isn't void
	if(xmlHttp)
	{
		try
		{
			// initiate reading a file from the server
			if(mnu!=0)
			{			
				position=pos;
				xmlHttp.open("GET","./js/selectmenu.php?mnu=" + mnu +"&pos="+pos,true);
				xmlHttp.onreadystatechange = handleRequestStateChange;
				xmlHttp.send(null);			
			}
		}
		// display the error in case of failure
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

function handleRequestStateChange()
{
	// when readyState is 4, we are ready to read the server response
	if(xmlHttp.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if(xmlHttp.status == 200)
		{
			// do something with the response from the server
			handleServerResponse();
		}
		else
		{

			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function handleServerResponse()
{
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
	throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror")
	throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	// obtain arrays with image name
	idArray = xmlRoot.getElementsByTagName("id");
	titleArray = xmlRoot.getElementsByTagName("title");
	// generate HTML output
	var html = "<table width='100%' style='margin-left:15px;'><ul>";
	// iterate through the arrays and create an HTML structure
	for(i=1;i<idArray.length+1;i++)
	{
		if(i%3==0)
		{
			html+="<td><li><a href='./category.php?mnu="+idArray.item(i-1).firstChild.data+"&num="+position+"'>"+titleArray.item(i-1).firstChild.data+"</a></li></td></tr>";
		}
		else if(i%3==1)
		{
			html+="<tr height='30'><td><li><a href='./category.php?mnu="+idArray.item(i-1).firstChild.data+"&num="+position+"'>"+titleArray.item(i-1).firstChild.data+"</a></li></td>";
		}
		else
		{
			html+="<td><li><a href='./category.php?mnu="+idArray.item(i-1).firstChild.data+"&num="+position+"'>"+titleArray.item(i-1).firstChild.data+"</a></li></td>";
		}
	}
	html+= "</ul></table>";
	// obtain a reference to the <div> element on the page
	myDiv = document.getElementById("ShowSubMenu");
	// display the HTML output
	myDiv.innerHTML = html;
}// holds an instance of XMLHttpRequest
