/*
===========================================================================
http://www.freakz.ro/ajax/base.js

Simple AJAX class, by Shocker, www.Freakz.ro / www.ShockingSoft.com, version 2.1

PLEASE DO NOT REMOVE THIS SECTION IF YOU USE IT ON YOUR SITES


Sample:
---------------------------
function callback(response) 
 {
	alert("Website responded: "+response);
 }
function myTimeOut()
 {
	alert("Request timed out!");
 }
AJAX=new _AJAX(); //Create an instance of my _AJAX object.

AJAX.setTimeout(myTimeOut, 1000); //if the request takes longer than 1000ms, it will call myTimeOut and denies callback function to be called
AJAX.GET("http://www.freakz.ro/test.php", callback);
AJAX.clearTimeout();
AJAX.GET("http://www.freakz.ro/test.php", callback); //the timeout settings have been deleted, no timeout active

// POST requests
AJAX.addParam("id", 5); //Adding a POST parameter
AJAX.addParam("name", "Shocker Freakz");
AJAX.addParam("test", "blabla");
AJAX.delParam("test"); //Delete the "test" POST parameter
AJAX.POST("http://www.freakz.ro/test.php",callback); // This will make a request with "id=5&name=Shocker%20Freakz" as POST data
AJAX.clearParams(); //Clear POST parameters
AJAX.populateParams("my_form"); //automatically creates a list of parameters (this will include each input from the my_form form)

AJAX.POST_Form("http://www.freakz.ro/test/post.php", callback, "data"); //all-in-one: POST url, callback function, form from where to get the parameters
---------------------------

Changelog: ( check original file for updates: http://www.freakz.ro/ajax/base.js )
25-11-2008: (v1.5)
	- TimeOut implemented
	- requests now return false on "reuqest not started"
30-11-2008: (v1.6)
	- Fixed error caused on Internet Explorer by consecutive requests
09-12-2008: (v2.0)
	- Added POST requests
07-01-2009: (v2.1)
	- Fixed the same error that caused problems with consecutive requests, now works fine on all major browsers

===========================================================================
*/

// Core
var _httpR;
var _cbF;
var _toF;
var _toV;
var AJAX_Done=true;

function AJAX_CanRequest() { return AJAX_Done; }
function LockAJAX() { AJAX_Done=false; }
function UnlockAJAX() { AJAX_Done=true; }

function _AJAX()
 {
	this.initialize=ajax_init;
	this.initialize();
 }


function ajax_callback_fct()
 {
	if (typeof(_cbF)!=="function") return;
	AJAX_Done=_httpR.readyState == 4;
	if (AJAX_Done) ajax_delTimeout();
	if (AJAX_Done && _httpR.status == 200) { _cbF(_httpR.responseText); }
 }

function ajax_init()
 {
	try { this.httpR=new XMLHttpRequest(); }
	catch (e)
	  {
	   try { this.httpR=new ActiveXObject("Msxml2.XMLHTTP"); }
	   catch (e)
	    {
	     try { this.httpR=new ActiveXObject("Microsoft.XMLHTTP"); }
	     catch (e) { return false; }
	    }
	  }
	_httpR=this.httpR;

	this.request=ajax_startRequest;
	this.ajax_callback=ajax_callback_fct;
	this.GET=ajax_GET;
	this.POST=ajax_POST;
	this.clearParams=ajax_POST_clearParams;
	this.addParam=ajax_POST_addParam;
	this.delParam=ajax_POST_delParam;
	this.paramsCount=ajax_POST_paramsCount;
	this.params=ajax_POST_buildParamsString;
	this.populateParams=ajax_POST_populateParamsFromForm;
	this.POST_Form=ajax_POST_Form;

	this.timeOut=0;
	this.timeOutFunction=null;
	this.setTimeout=ajax_setTimeout;
	this.timeOutVar=null;
	this.clearTimeout=ajax_clearTimeout;

	this.requestInProgress=function(){return !AJAX_CanRequest();}

	this.paramsNames=new Array();
	this.paramsValues=new Array();
 }

function ajax_startRequest(type, url)
 {
	if (type!==undefined && url !== undefined)
	 {
		LockAJAX();
		this.httpR.open(type, url, true);
		this.httpR.onreadystatechange=this.ajax_callback;
		if(type=="GET")	this.httpR.send(null);
		else
		 {
			this.httpR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.httpR.setRequestHeader("Content-length", this.params().length);
			this.httpR.setRequestHeader("Connection", "close");
			this.httpR.send(this.params());
		 }
	 }
	if (this.timeOut!=0)
	 {
		_toV=setTimeout("ajax_timeOutInternal()", this.timeOut);
		_toF=this.timeOutFunction;
	 }
	else { _toV=null; _toV=null; }
		
 }

// GET
function ajax_GET(url,callback)
 {
	if (!AJAX_CanRequest()) return false;
	_cbF=callback;
	this.request("GET", url);
	return true;
 }

// Timeout
function ajax_setTimeout(callback, timeout)
 {
	this.timeOut=timeout;
	this.timeOutFunction=callback;
 }

function ajax_delTimeout()
 {
	clearTimeout(_toV);
 }

function ajax_timeOutInternal()
 {
	_httpR.abort();
	UnlockAJAX();
	_toF();
 }

function ajax_clearTimeout()
 {
	this.timeOut=0;
	this.timeOutFunction=null;
 }

// POST
if(!Array.indexOf)
 {
	Array.prototype.indexOf = function(obj)
	 {
		for(var i=0; i<this.length; i++)
		 {
			if(this[i]==obj){ return i; }
		 }
		return -1;
	 }
 }


function escp(data)
 {
	return data.replace("[]", "%5B%5D");
 }
function ajax_POST_buildParamsString()
 {
	out="";
	if (this.paramsCount()==0) return "";
	for (i=0; i<this.paramsCount(); i++)
	 {
		out+="&"+this.paramsNames[i]+"="+escape(this.paramsValues[i]);
	 }
	return out.substr(1);
 }

function ajax_POST_paramsCount()
 {
	return this.paramsNames.length;
 }

function ajax_POST_addParam(paramName, paramValue, forceAdd)
 {
	if (paramName=="") return;
	if (typeof forceAdd == "undefined") forceAdd=false;

	if (this.paramsNames.length!=0)
	 {
		idx=this.paramsNames.indexOf(paramName);
		if (idx == -1 || forceAdd) count=this.paramsCount();
			else count=idx;
	 }
	else {count=0};

	this.paramsNames[count]=paramName;
	this.paramsValues[count]=paramValue;
 }

function ajax_POST_delParam(paramName)
 {
	idx=this.paramsNames.indexOf(paramName);
	if (idx == -1) return;
	this.paramsNames.splice(idx,1);
	this.paramsValues.splice(idx,1);
 }

function ajax_POST_clearParams()
 {
	count=this.paramsCount();
	this.paramsNames.splice(0, count);
	this.paramsValues.splice(0, count);
 }

function ajax_POST_populateParamsFromForm(formName)
 {
	frm=document.forms[formName];
	if (typeof frm == "undefined" || frm.elements.length<1) return false;
	elem=frm.elements;
	for (i=0; i<elem.length; i++)
	 {
		forceAdd=(elem[i].type == "checkbox" || elem[i].type == "radio");
		if ( (forceAdd && elem[i].checked) || !forceAdd)
			this.addParam(escp(elem[i].name), elem[i].value, forceAdd);
	 }
 }

function ajax_POST(url, callback)
 {
	if (!AJAX_CanRequest()) return false;
	_cbF=callback;
	this.request("POST", url);
	return true;
 }

function ajax_POST_Form(url, callback, form_name)
 {
	this.clearParams();
	this.populateParams(form_name);
	this.POST(url, callback);
 }