/*  
 *             CREDITS
 *
 * 		AJAX Obiect Oriented
 *
 *				Part of
 *		IceCode - The Next Web Expression
 *
 *		Author  : Daniele Contarino
 *		Version : 1.02
 *		Contact : http://www.danielecontarino.it
 *
 */
 
function Ajax (file, method){
	this.file = file;
	this.method = method;
	this.conteiner = false;
	this.waiting = false;
	this.data = "";
	this.secureTicket = "";
	this.waitingMessagge;
	this.delay = -1;
	this.responseText;
	this.request=getXHR();
	this.onCompleteRequestFunction = null;
	
	this.setConteiner = function (conteiner){
		if(typeof conteiner == "object") this.conteiner = conteiner;
		else if(typeof conteiner != "null")this.conteiner = document.getElementById(conteiner);
	}

	this.setWaiting = function (waiting, waitingMessage){
		if(typeof waiting == "object") this.waiting = waiting;
		else if(typeof waiting != "null") this.waiting = document.getElementById(waiting);
		if(waiting) this.waitingMessage = waitingMessage;
	}

	this.setData = function (data){
		if( data != null &&  typeof data != "undefined"){
			this.data = data;
			//Se ho passato un intero form allora lo passo al parser
			if(typeof this.data == "object") this.data= form2data(this.data); 
		}	
	}

	this.setSecureTicket = function (secureTicket){
		if(typeof secureTicket != "null" &&  typeof secureTicket != "undefined") this.secureTicket = secureTicket;
	
	}

	this.setDelay = function (delay){	if(delay > 1) this.delay = delay; }
	
	this.getResponse = function (){ return this.responseText; }
	
	this.sendRequest = function (async){
		if(this.request){
			if(async) {
				var _self = this;
				this.request.onreadystatechange = function(){ loadAsyncData(this, _self); };

			}else if(this.waiting) this.waiting.innerHTML=this.waitingMessage;
			
			var params = "";
			
			if(this.data != "" && this.secureTicket != "") params = this.data+"&secureTicket="+this.secureTicket ;  
			else if(this.data != "" ) params = this.data ;  
			else if(this.secureTicket != "") params = "secureTicket="+this.secureTicket ;  
						
			try{
				if(this.method.toUpperCase() == "GET" ) {
					this.request.open(this.method, this.file+"?"+params, async);
					this.request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
					this.request.setRequestHeader("connection", "close");
					this.request.send(null);
					
				}else {
					this.request.open(this.method, this.file, async);
					this.request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
					this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
					this.request.send(params);
				}
				
			}catch (e) { return false;}
			
			if(!async) loadSyncData(this);

			return true;
		}
		return false;		
	}
	
	//Metodi Privati
	
	function loadAsyncData(ajaxRequest, callbackFunction) {
		if(ajaxRequest.readyState == 1 && callbackFunction.waiting) callbackFunction.waiting.innerHTML = callbackFunction.waitingMessage; 
		else if(ajaxRequest.readyState == 4)
			if(ajaxRequest.status == 200){
				callbackFunction.responseText = ajaxRequest.responseText.replace(/^\s+|\s+$/g,"");
				if( callbackFunction.onCompleteRequestFunction != null) 
					setTimeout(callbackFunction.onCompleteRequestFunction, 0);
				else{
					if(callbackFunction.conteiner){
						if(callbackFunction.delay == -1)	callbackFunction.conteiner.innerHTML = callbackFunction.responseText;
						else setTimeout( function() { callbackFunction.conteiner.innerHTML = callbackFunction.responseText;}, callbackFunction.delay);

					}
				}
				if(callbackFunction.conteiner != callbackFunction.waiting) callbackFunction.waiting.innerHTML = '';
				return true;
				
			}else return false;	
	}

	function loadSyncData(ajaxRequest) {
		ajaxRequest.responseText = ajaxRequest.request.responseText.replace(/^\s+|\s+$/g,"");
		if(ajaxRequest.conteiner){
			if(ajaxRequest.delay == -1) ajaxRequest.conteiner.innerHTML = ajaxRequest.responseText;
			else setTimeout( function() { ajaxRequest.conteiner.innerHTML = ajaxRequest.responseText;},this.delay);
		}		
	}

	function form2data(form) {
		var data="";
		for (var i=0;i<form.length;i++)
			if(form.elements[i].name!="")			
				if(form.elements[i].nodeName.toLowerCase() =="input" && form.elements[i].type == "checkbox" && !form.elements[i].checked  ) ;
				else if(form.elements[i].nodeName.toLowerCase() =="input" && form.elements[i].type == "radio" && !form.elements[i].checked  ) ;
				else data+=form.elements[i].name+"="+form.elements[i].value+"&";

		return data.substring(0,data.length-1);
	} 

	function getXHR () {
		var XHR = null, browserUtente = navigator.userAgent.toUpperCase();

		if (window.XMLHttpRequest) 	XHR = new XMLHttpRequest();
		else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0){
			if(browserUtente.indexOf("MSIE 5") < 0) XHR = new ActiveXObject("Msxml2.XMLHTTP");
			else XHR = new ActiveXObject("Microsoft.XMLHTTP");				
		}
		return XHR;
	}
}


var AjaxUtils = new function AjaxUtils(){
	this.checkBrowser = function(applicationName){
							var browserUtente = navigator.userAgent.toUpperCase();
							return (browserUtente.indexOf(applicationName) < 0) ;
						};

	this.useAsync = function(){
		var useAsync = true;
		if(this.checkBrowser("MSIE 4") || this.checkBrowser("MSIE 5")|| this.checkBrowser("MSIE 6")) useAsync = false;
		return useAsync;
	}
}


