var MK_Spinner = false;
var objMkspinner = false;
// General utility Functions called from multiple external js
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;
		}
		arrayPageScroll = new Array('',yScroll);
		return arrayPageScroll;
	},

	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
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
	}

}


function mkportal_Spinner_Show() {
//Meo: The object will be created only if it doesn't exist yet. (Good for memory and performance)
	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();
	var arrayPageScroll = MkUtilsLib.getPageScroll();
	var top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 180) / 2);
	var 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';
}

// Meo entirely recode CLass
function MKP_ajax(url, options) {	

	var mka_postData = options.postBody || '';
	var mka_method = options.method || 'post';
	var mka_Complete = options.onComplete || null;
	var mka_update = options.update || null;
	var mka_sendReq = 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);

	}
	

	function Mka_getXmlHttpRequestObject() {
		if(window.XMLHttpRequest) {	return new XMLHttpRequest(); }
		else if(window.ActiveXObject) {
			try { req = new ActiveXObject('Msxml2.XMLHTTP.4.0'); } catch(e) { try {	req = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {req = false; }} return req; }
		else {	return false; }
	}
	
	Mka_start(url);
};


// Functions for Ajax shoutbox
function Switch_InstEd()  {

}

function instchatSave() {
	//MK_Spinner = new MKSpinner();
	mkportal_Spinner_Show();
	var url =  MKAJAX_SITEPATH + 'index.php?ind=urlobox&op=reg_data';
        message =document.getElementById('ta').value;
        if(message == "")
        {
            return false;
        }
        postData = "value="+encodeURIComponent(message).replace(/\+/g, "%2B");
      new MKP_ajax(url, {method: 'post', postBody: postData, onComplete: function(request) { instchatSaveComplete(request); }});
    }

function instchatSaveComplete(request) {
	
        if(request.responseText.match(/<error>(.*)<\/error>/)) {
            message = request.responseText.match(/<error>(.*)<\/error>/);
            if(!message[1]) {
                message[1] = "An unknown error occurred.";
            }
            alert("There was an error performing the update."+message[1]);
        }
        else if(request.responseText)
        {
            document.getElementById('instchat_2').innerHTML = request.responseText;
             document.getElementById('ta').value = "";
        }
	mkportal_Spinner_Hide();
}

function instblockSubmit() {
	instchatSave();
	Switch_InstEd();
	return false;
}

