function CM_HTTPReq(method, url, callbackFunction)
{
	var self = this;
	this._method = method;
	this._url = url;
	this._callbackFunction = callbackFunction;
	this._handle = null;
	this._form = '';
	this._param = '';
	this._contentType = 'text/html';
	this._body = (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
	this._showTransparentLayer = false;
	this._showOutput = false;
	this._showDialog = false;
	this._outputElm = document.getElementById('CMAX_container');
	this._contentElm = document.getElementById('CMAX_content');
	this._loadingElm = document.getElementById('CMAX_loading');
	
	this.getXmlHttpObject = function()
	{
		self._handle = null;
		
		try
		{
			self._handle = new XMLHttpRequest();
		}
		catch (e)
		{
			try
			{
				self._handle = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				self._handle = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		
		if (self._handle == null)
		{
			alert ("Internet Tarayıcınız HTTP_Request Desteklemiyor!");
			return false;
		}
		
		return true;
	}
	
	this.formDataToQueryString = function()
	{
		
		tmp = '';
		
		for (i = 0; i < this._form.elements.length; i++)
		{
			elm = this._form.elements[i];
			
			switch (elm.type)
			{
				case 'text':
				case 'select-one':
				case 'hidden':
				case 'password':
				case 'textarea':
					tmp += elm.name + '=' + (elm.value) + '&'
					break;
			}
		}
		return tmp;
	}
	
	this.stateChanged = function()
	{	
		if (self._handle.readyState == 4 || self._handle.readyState == "complete")
		{
			if (self._handle.status == 200) 
			{
				if(self._showDialog)
				{
					self.showDialog(self._handle.responseText);
				}
				if(self._showOutput)
				{
					self.showOutput(self._handle.responseText);
				}
				
				if(self._callbackFunction)
				{
					eval(self._callbackFunction);
				}
				
			}
			else
			{
				alert("Hata!!!");
			}
			
			document.getElementById("CMAX_loading").style.display = "none";
		}
	}
	
	this.doRequest = function()
	{
		
		if(!this.getXmlHttpObject())
		{
			alert('Hata!!!');
			return false;
		}

		if(this._showTransparentLayer)
		{
			this.showTransparentLayer();
		}
	
		this.centerElement(this._loadingElm);

		this._loadingElm.style.display = "block";

		this._handle.onreadystatechange = this.stateChanged;

		this._handle.open(this._method, this._url, true);
	
		this._handle.setRequestHeader("Content-type", this._contentType);
		this._handle.setRequestHeader("Content-length", this._param.length);
		this._handle.setRequestHeader("Connection", "close");
		this._handle.send(this._param);
	}
	
	this.showOutput = function(str)
	{
		this._contentElm.innerHTML = str;
		this.executeEmbeddedScripts(str);
	}
	
	this.showDialog = function(str)
	{
		this._outputElm.style.display = 'block';
		this._contentElm.innerHTML = str;
		this.executeEmbeddedScripts(str);
		this.centerElement(this._outputElm);
	}
	
	this.hideDialog = function()
	{
		this._outputElm.style.display = 'none';
		this.hideTransparentLayer();
	}
	
	this.setForm = function(F)
	{
		this._form = F;
		this._param = this.formDataToQueryString();
	}
	
	this.setParam = function(param)
	{
		this._param = param;
	}
	
	this.setContentType = function(str)
	{
		this._contentType = str;
	}
	
	this.setOutputElm = function(elm)
	{
		this._outputElm = elm;
	}
	
	this.setContentElm = function(elm)
	{
		this._contentElm = elm;
	}
	
	this.setLoadingElm = function(elm)
	{
		this._loadingElm = elm;
	}
	
	this.centerElement = function(elm)
	{
		
		docwidth = document.getElementById('base').offsetWidth;
		docheight = Math.min(this._body.scrollHeight, this._body.clientHeight);
		
		_top = this._body.scrollTop + Math.ceil((docheight - elm.clientHeight) / 2);
		//_left = this._body.scrollLeft + Math.ceil((docwidth - elm.clientWidth) / 2);
		
		//alert(docwidth + ' ' + docheight + ' ' + _top + ' ' + _left + ' ' + elm.clientHeight + ' ' + elm.clientWidth)
		if(_top < 100)
			_top = 100;
		elm.style.top = _top + "px";
		//elm.style.left = _left + "px";	
	}
	
	this.showTransparentLayer = function()
	{
		this.hideSelectElement();
		
		tmp = document.getElementById('CMAX_transparent_layer');
	
		tmp.style.width = this._body.scrollWidth;
		tmp.style.height = this._body.scrollHeight;
		
		tmp.style.display = 'block';
	}
	
	this.hideTransparentLayer = function()
	{
		document.getElementById('CMAX_transparent_layer').style.display = 'none';
		this.showSelectElement();
	}
	
	this.showSelectElement = function()
	{
		elms = document.getElementsByTagName('select');
		
		for(i=0; i<elms.length; i++)
		{
			elms[i].style.display = 'block';
		}
	}
	
	this.hideSelectElement = function()
	{
		elms = document.getElementsByTagName('select');
		
		for(i=0; i<elms.length; i++)
		{
			elms[i].style.display = 'none';
		}
	}
	
	this.myBindexOf = function(myObject,mySearch,myPos)
	{
		if (myPos==null)
			myPos=0;
		if (typeof(myObject)!='object')
			return -1;
		if(!myObject.indexOf)
		{
			for(var i=myPos; i < myObject.length; i++)
			if(myObject[i]==mySearch)
				return i;
			return -1;
		}
		else
			return myObject.indexOf(mySearch,myPos);
	}

	this.executeEmbeddedScripts = function(response)
	{
		if (!this._contentElm)
			return;
		var bSaf = (this.myBindexOf(navigator.userAgent,'Safari',0) != -1);
		var bOpera = (this.myBindexOf(navigator.userAgent,'Opera',0) != -1);
		var bMoz = (navigator.appName == 'Netscape');
				
		var myHead = document.getElementsByTagName('head');
		
		if ((!bSaf)&&(!bOpera)&&(!bMoz))
			this._contentElm.innerHTML = response; // World's most retarded IE fix
		
		var st = this._contentElm.getElementsByTagName('SCRIPT');
		var strExec;
		var scripts = st.length;
		var i = 0;
		for(j = 0; j < scripts; j++)
		{
			var scriptsAtStart = st.length;
			if (bSaf)
			{
				strExec = st[i].innerHTML; st[i].innerHTML = "";
			}
			else if (bOpera)
			{
				strExec = st[i].text; st[i].text = "";
			}
			else if (bMoz)
			{
				strExec = st[i].textContent; st[i].textContent = "";
			}
			else
			{
				strExec = st[i].text; st[i].text = "";
			}
			
			try
			{
				var myScript = document.createElement("script");
				myHead.item(0).appendChild(myScript);
				myScript.type = "text/javascript";
				myScript.text=strExec;
				var html = this._contentElm.innerHTML;
				// jscript would prematurely eject if it encountered the full closing tag, even encapsulated in a string object
				var endScriptL = '<' + '/script>';  
				var endScriptU = '<' + '/SCRIPT>';
				var pos2 = this.myBindexOf(html,strExec,0);
				var pos = this.myBindexOf(html,endScriptL,pos2) >= 0 ? this.myBindexOf(html,endScriptL,pos2) : this.myBindexOf(html,endScriptU,pos2);
				pos += String(endScriptL).length;
				html = html.substr(0,pos) + html.substr(pos);
				this._contentElm.innerHTML = html;
				if(scriptsAtStart==st.length)
					i++;
			}
			catch(e)
			{
				alert("Sayfada hata var: "+e);
			}
		}
	}
}

var CM_HTTP;

function CMAX_postForm(F, callback, from)
{
	CM_HTTP = new CM_HTTPReq(F.method, F.action, callback);
	
	CM_HTTP.setContentType('application/x-www-form-urlencoded; charset=UTF-8');
	
	CM_HTTP.setForm(F);
	
	CM_HTTP._showDialog = true;
	
	CM_HTTP.doRequest();
	
	return false;
}

function CMAX_showDialog(url, callback)
{
	CM_HTTP = new CM_HTTPReq('get', url, callback);

	CM_HTTP.setContentType('text/html; charset=UTF-8');
	
	CM_HTTP._showDialog = true;
	
	CM_HTTP._showTransparentLayer = false;
	
	CM_HTTP.doRequest();

	return false;
}

function CMAX_showURL(url, output_elm, loading_elm, callback)
{
	CM_HTTP = new CM_HTTPReq('get', url, callback);
	
	CM_HTTP.setContentType('text/html; charset=UTF-8');
	
	CM_HTTP.setContentElm(document.getElementById(output_elm));
	
	if(loading_elm)
	{
		CM_HTTP.setLoadingElm(document.getElementById(output_elm));
	}
	
	CM_HTTP._showOutput = true;
	
	CM_HTTP._showTransparentLayer = false;
	
	CM_HTTP.doRequest();
	
	return false;
}

function CMAX_cancelDialog(reloadStatus)
{
	if(CM_HTTP == null)
		return false;
	
	CM_HTTP.hideDialog();
	
	if(reloadStatus)
		window.location.reload();
		
	return true;
}

function loginPostCallBck()
{
	window.location.reload();
}

function _commentPagingCallBack()
{
	window.location.href = window.location.href.replace('#comment_top', '') + '#comment_top';	
}

function _addCommentCallback()
{
	//createRTEElm('content');
}
//
function submitCommentForm(form, action)
{
	if(form.content.value == "")
	{
		//alert("Yorum içeriği boş geçilemez.");
		return false;
	}

	form.action = action;

	return CMAX_postForm(form, '');
}

function cleanupCommentForm()
{
	var elm = document.getElementById('comment_form');
	if(elm)
	{
		elm.content.value = '';
		elm.reply.value = 0;

		elm = document.getElementById('reply_to');
		elm.innerHTML = '';
	}

	return false;
}

function setCommentReply(idx, member)
{
	var elm = document.getElementById('comment_form');
	if(elm)
	{
		elm.reply.value = idx;

		elm = document.getElementById('reply_to');
		elm.innerHTML = '<span>@' + member + '</span><span class="remove" onclick="clearCommentReply();">x</span>';
	}
}

function clearCommentReply()
{
	var elm = document.getElementById('comment_form');
	if(elm)
	{
		elm.reply.value = 0;

		elm = document.getElementById('reply_to');
		elm.innerHTML = '';
	}
}