/*
    SimpleAjax - its a simple Ajax Class for handling various scripts (PHP, ASP, ASP.NET, PERL ...) response.
    Copyright (C) 2008 Simpletags.org
    Author: Marcin Rosinski

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	contact: marcin@simpletags.org
	web: www.simpletags.org
*/


function $_elem(id)
{
	return document.getElementById(id);
}

var SimpleAjax = {

	_xmlHttp_Simple: "",

	//POST
	ini: function(url, request, callback, options){

		this._xmlHttp_Simple=this.GetXmlHttpObject();
		if (this._xmlHttp_Simple==null){
		  alert ("Your browser does not support AJAX!");
		  return;
		} 
		
		this.options = options || {};
		this.options.json = this.options.json || false;
		
		url=url+"?sid="+Math.random();
		if (callback)
		{
			this._xmlHttp_Simple.onreadystatechange = function() 
			{
				if(SimpleAjax._xmlHttp_Simple.readyState==4)
				{
					if (SimpleAjax._xmlHttp_Simple.status == 200) 
					{
						if(SimpleAjax.options.json)
						{
							if (callback) callback(eval( '(' + SimpleAjax._xmlHttp_Simple.responseText + ')' ));
						}
						else
						{
							if (callback) callback(SimpleAjax._xmlHttp_Simple.responseText);
						}
					}
				}
			}	
		}

		this._xmlHttp_Simple.open("POST",url,true);
		this._xmlHttp_Simple.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    this._xmlHttp_Simple.setRequestHeader("Content-length", request.length);
	    this._xmlHttp_Simple.setRequestHeader("Connection", "close");
		this._xmlHttp_Simple.send(request);
		
	},
	
	//XMLHTTPOBJECT
	GetXmlHttpObject: function(){
		var xmlHttp=null;
		try{xmlHttp=new XMLHttpRequest();}
		catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}
	return xmlHttp;
	}

}