var
agt = navigator.userAgent.toLowerCase(),
agt_ver = parseInt(navigator.appVersion),
is_mozilla = (navigator.product == "Gecko"),
is_opera = (agt.indexOf("opera") != -1),
is_konqueror = (agt.indexOf("konqueror") != -1),
is_webtv = (agt.indexOf("webtv") != -1),
is_ie = ((agt.indexOf("msie") != -1) && (!is_opera) && (!is_webtv)),
is_netscape = ((agt.indexOf("compatible") == -1) && (agt.indexOf("mozilla") != -1) && (!is_opera) && (!is_webtv)),
is_win = (agt.indexOf("win" != -1)),
is_mac = (agt.indexOf("mac") != -1);

function getObj(name) {
	if (document.getElementById) { if(document.getElementById(name)) { return document.getElementById(name); } else {return false;}}
	else if (document.all) { if (document.all[name]) {return document.all[name]; } else {return false;}}
	else if (document.layers) { if (document.layers[name]) { return document.layers[name]; } else {return false;}}
}

// Meo: added in C 0.1.b
// General utility Functions called
var MkUtilsLib = {

	getPageScroll: function() {
		var yScroll;
		if(self.pageYOffset) {
			yScroll = self.pageYOffset;
		}else if(document.documentElement && document.documentElement.scrollTop) { // Explorer 6
			yScroll = document.documentElement.scrollTop;
		} else if(document.body) { // all other Explorers
			yScroll = document.body.scrollTop;
		}
		return ['',yScroll];
	},

	getPageSize: function() {
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;

		// for small pages with total width less then width of the viewport
		var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

		return [pageWidth,pageHeight,windowWidth,windowHeight];
	}
};

// Ajax Spinner loading indicator
var objMkspinner = false;
function mkportal_Spinner_Show() {
	if(!objMkspinner) {
		var image = MKAJAX_IMAGES_PATH + "loadspin.gif";
		var height = 180;
		var width = 180;
		objMkspinner = document.createElement("div");
		objMkspinner.style.position = "absolute";
		objMkspinner.style.zIndex = 1000;
		objMkspinner.style.textAlign = "center";
		objMkspinner.style.verticalAlign = "middle";
		objMkspinner.innerHTML = "<div style=\"text-align: center; border:2px solid #698490; padding: 6px; background: #FFF;\"><br /><img src=\"" + image + "\" border=\"\"><br /><br /><b>... Loading... </b><br /></div>";
		objMkspinner.style.width = width + "px";
		objMkspinner.style.height = height + "px";
		objMkspinner.style.display = 'none';
		objMkspinner.id = "mkspinner";
		var owner = document.getElementsByTagName("body").item(0);
		owner.insertBefore(objMkspinner, owner.firstChild);
	}
	var 	arrayPageSize = MkUtilsLib.getPageSize(),
		arrayPageScroll = MkUtilsLib.getPageScroll(),
		top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 180) / 2),
		left = ((arrayPageSize[0] - 20 - 180) / 2);

	objMkspinner.style.top = top + "px";
	objMkspinner.style.left = left + "px";
	objMkspinner.style.display = 'block';
}

function mkportal_Spinner_Hide() {
 	document.getElementById('mkspinner').style.display = 'none';
}

// Ajax Core Engine
function MKP_ajax(url, options) {	

	function Mka_getXmlHttpRequestObject() {
		if(window.XMLHttpRequest) {	return new XMLHttpRequest(); }
		else if(window.ActiveXObject) {
			try { var req = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch(e) { try { req = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) { req = false; }} return req; }
		else {	return false; }
	}

	var	mka_postData = options.postBody || '',
		mka_method = options.method || 'post',
		mka_Complete = options.onComplete || null,
		mka_update = options.update || null,
		mka_sendReq = new Mka_getXmlHttpRequestObject();	
	
	function Mka_returnOut() {

		if (mka_sendReq.readyState == 4 && mka_sendReq.status == 200) {
			if (mka_Complete){
				setTimeout(function(){mka_Complete(mka_sendReq);}, 10);
			}
			if (mka_update) {
				setTimeout(function(){mka_update.innerHTML = mka_sendReq.responseText;}, 10);
			}
			mka_sendReq.onreadystatechange = function(){};
		}

	}

	function Mka_start(url){

		mka_sendReq.open(mka_method, url, true);
		mka_sendReq.onreadystatechange = Mka_returnOut;
		if (mka_method == 'post') {
			mka_sendReq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (mka_sendReq.overrideMimeType){ mka_sendReq.setRequestHeader('Connection', 'close'); }
		}
		mka_sendReq.send(mka_postData);

	}

	Mka_start(url);
}