//
// Js/Ajax Engine  
// Autor: Petcu Mihai-Costin
// Email: petcu_mihai2001@yahoo.com
// Version: v1.1
// Date: 10.01.2009
//

function xmlHttp (){
	
	this.xmlHttp = null;
	this.engine = 'ajax.php';
	this.asynchron = false;
	this.method = 'GET';
	this.loadingIcoId = null; 
	
	this.constructor = function(){
		try{
			this.xmlHttp = new XMLHttpRequest();
		}catch(e){
			try{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				alert ("XMLHTTP(Ajax) nu este suportat de Browser-ul tau!");
				return;  
			}
		}
	}
	
	this.destructor = function(){
		this.xmlHttp = null;
	}
	
	this.setAsynchron = function(value){
		this.asynchron = value;
	}
	
	this.setMethod = function(value){
		this.method = value;
	}
	
	this.setLoadingIcoId = function(id){
		this.loadingIcoId = id;
	}
	
	this.viewLoadingIco = function(){
		if(this.loadingIcoId !== null){
			if(this.xmlHttp.readyState >= 1){
				document.getElementById(this.loadingIcoId).style.display = 'block';
			}
			if(this.xmlHttp.readyState == 4){
				document.getElementById(this.loadingIcoId).style.display = 'none';
				return true;
			}
		}
		return false;
	}
	
	this.request = function(php_action, params){
		//request
		var url = this.engine+'?action='+php_action;
		if(params != undefined || params != null){
			url += params;
		}
		this.xmlHttp.open(this.method, url, this.asynchron);
		if(this.method == 'POST'){
			this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      		this.xmlHttp.setRequestHeader("Content-length", params.length);
     		this.xmlHttp.setRequestHeader("Connection", "close");
      		this.xmlHttp.send(params);
		}else{
			this.xmlHttp.send(null);
		}
		
		//response
		this.viewLoadingIco();
		return this.requestResponse();
	}
	
	this.requestResponse = function(){
		if(this.xmlHttp.readyState == 4){
  			return this.xmlHttp.responseText;
  		}
	}
	
	this.constructor();
}
