/////////////////////
/// Ajax for all ///

var actionCommand;
var isBusy;
var script;
var gets;
var search_age;
var http = getHTTPObject(); // We create the HTTP Object
	
function handleHttpResponse() {
	if (http.readyState != 4) {
		return;
	} else {
		try {
			if(http.status == 200) {
				isBusy = false;
				results = http.responseText;
				setTimeout(actionCommand, 0);
			} else {
				try {
					//alert("ERROR: "+http.statusText);
				} catch(e) {
					//alert("error: "+e.description);
				} 
			}
		} catch(E) {
			try {
				ajaxHelper(script, gets);
			} catch(ee) {
				alert("ErroR: "+E.description+": "+ee.description);
			}
		}
	}
}

function ajaxHelper(url, options) {
	script = url;
	gets = options;
	if(isBusy) {
		http.onreadystatechange = function() {}
		http.abort();
	}
   // http.open("POST", url+options, true);
   // isBusy = true;
   // http.onreadystatechange = handleHttpResponse;
   // http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // if i decide to use POST
   // http.send(options);
	http.open("GET", url+options, true);
	isBusy = true;
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}


/////////
// Individual js functions to be executed
/////////

// ik_product left nav
function fillLeftNav(cat_id, parent_id) {
	actionCommand = "fillDiv('left_nav', results); fillBottomNav();";
	url = "/menuBuilder.php";
	opts = "?menu_select="+cat_id+"&pid="+parent_id;
	ajaxHelper(url, opts);
}

function fillMainNav() {
	actionCommand = "fillDiv('main_nav', results); fillBottomNav();";
	url = "/menuBuilder.php";
	opts = "";
	ajaxHelper(url, opts);
}

/*
* Igor Z made this modification due to the SiteBrand code and some browsers. 
* SiteBrand causes the entire document to shift out of balance and tehrefore 
* causes document.getElementById to fail until the doc shifts back into place.
* To fix this issue, we try - catch the error and put a timeout with a nifty array-caching tool
* to supplement our error.
* NOTES: First in last out is supplemented with a reverse upon out. One element reversed is 
* still the same element.
* --Igor Z; ( igor@ikorb.com )
*/
var myContent = new Array();

function fillDiv(div_id, content) {
	var div_elem = document.getElementById(div_id);
	try {
		div_elem.innerHTML = content;
	} catch (e){
		myContent.push(content);
		setTimeout("fillDiv('"+div_id+"', (myContent.reverse()).pop())", 1);
	};
}

function fillBottomNav() {
	actionCommand = "fillDiv('bottom_nav', results);";
	url = "/menuBuilder.php";
	opts = "?bottom_menu=1";
	ajaxHelper(url, opts);
}


/////////////
// browser detect 
////////////
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: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			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.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
