/**
 * CAjaxBoy
 * @version 1.0.4.0
 */

function CAjaxBoy ()
{
	document.AjaxBoy = this;
	this.charset = 'UTF-8';
	/* XMLHttpRequest */
	this.req = null;
	// Run when xml load begin //
	this.loadBegin = null;
	this.loadOk = null;
	this.loadError = null;
	// Create XMLHttpRequest Object //
	this.createXHR = function ()
  {
		if(window.XMLHttpRequest) // branch for native XMLHttpRequest object
		{
			try {this.req = new XMLHttpRequest();}
			catch(e) {return null;}
		}
		else if(window.ActiveXObject) // branch for IE/Windows ActiveX version
		{
			try {this.req = new ActiveXObject("Msxml2.XMLHTTP");}
			catch(e)
			{
				try {this.req = new ActiveXObject("Microsoft.XMLHTTP");}
				catch(e) {return null;}
			}
		}
		this.req.onreadystatechange = this.processReqChange;
	};

	this.replaceContent = function (id, new_content)
	{
		block = document.getElementById(id);
		if (!block) return;
		try {
			block.innerHTML = new_content? new_content : '';
		}
		catch(e) {
			alert('Block id="'+id+'" treat error!');
		}
	};

	this.treatReceiveData = function ()
	{
		xml = this.req.responseXML.getElementsByTagName("xml");
		// Clear blocks //
		clear = xml[0].getElementsByTagName("clear");
		if (clear)
			for( i=0; i<clear.length; i++)
				this.replaceContent(clear[i].getAttribute('block_id'), '');
		// Replace content blocks //
		content = xml[0].getElementsByTagName("content");
		if (content)
			for( i=0; i<content.length; i++)
				this.replaceContent(content[i].getAttribute('block_id'), content[i].firstChild.nodeValue);
		// Set page title //
		page_title = xml[0].getElementsByTagName("title");
		if (page_title && page_title.length > 0)
			document.title = page_title[0].firstChild.nodeValue
		// Set page status //
		page_status = xml[0].getElementsByTagName("status");
		if (page_status && page_status.length > 0)
			window.status = page_status[0].firstChild.nodeValue
		// Run eval blocks //
		eval_code = xml[0].getElementsByTagName("eval");
		if (eval_code)
			for( i=0; i<eval_code.length; i++)
			{
					eval(eval_code[i].firstChild.nodeValue);
			}
	};

	this.processReqChange = function ()
	{
		try
		{
			if (document.AjaxBoy.req.readyState == 4 )
			{
				if (document.AjaxBoy.req.status == 200 ) {
					document.AjaxBoy.treatReceiveData();
					document.AjaxBoy.req.abort();
					if (document.AjaxBoy.loadOk)
						document.AjaxBoy.loadOk();

				}
				else
					throw(new Error());
			}
		}
		catch(e) {
			document.AjaxBoy.req.abort();
			if (document.AjaxBoy.loadError) document.AjaxBoy.loadError();
		}
	};

	this.runGetQuery = function (url)
	{
		this.createXHR();
		this.req.open('GET', url, true);
		this.req.send(null);
		if (this.loadBegin) this.loadBegin();
	};

	this.runPostQuery = function (form)
	{
    var data = new CAjaxForm().getFormData(form);

    if (form.method.toString().toUpperCase()  == 'POST') {
      	url =  form.action;
    	divide = form.action.toString().search(/\?/);
      	if (divide!=-1) {
      		url =  form.action.toString().slice(0, divide);
      		data = form.action.toString().slice(divide+1)+'&'+data;
    	}
		this.createXHR();
	    this.req.open('POST', url);
	    this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset='+this.charset);
	    this.req.send(data);
	    if (this.loadBegin) this.loadBegin();
    }
    else {
      this.runGetQuery(form.action + (form.action.toString().search(/\?\S+=/)!=-1 ? '&'+data : '?'+data));
    }
	};

}
