//	Class DataViewAjax 3.0
//	Autor: Marcelo Disconzi	
//	last update: 25/04/2008
//	new features: 
//		- small object xmlhttp
//		- only one argument in ajaxRetriev (-4)			
//		- argument path in constructor, to init xmlhttp.open method
//------------------------------------------------------------------------------------------------------------
function sendInArray() // class to send vars in Ajax Requests, great, and is mine
{
	this.add = add;
	this.get = get;
	this.send = send;
	this.arr = new Array;
	this.size = size;
	function add(item)
	{
		if(arguments.length == 1)
		 item[1] = item[1].toString().toUpperCase().trim();
		this.arr.push(item);		
	}
	
	function size()
	{
		return this.arr.length;		
	}

	function get(pos)
	{
		return this.arr[pos][1];		
	}
	this.getFull = function(pos)
	{
		return this.arr[pos];		
	}
	
	function send()
	{
		var s = '';
		
		for(var i = 0; i<this.arr.length; i++)
			s += this.arr[i][0]+'='+url_encode(this.arr[i][1].toString())+'&';
		
		return s.substr(0,s.length-1);		
	}
}
//------------------------------------------------------------------------------------------------------------
function Ajax(path,togrid) //Constructor, togrid:used whit a grid
{
	this.xmlhttp = (window.XMLHttpRequest)  ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	this.xmlhttp.open("POST",path,true);
	this.execInside = function(){};
	this.execInsideComp = function(){};
	this.vars = new sendInArray();
	this.ajaxRetriev = ajaxRetriev;
	this.ajaxRetrievXML = ajaxRetrievXML;
	this.showLoading = showLoading;
	this.sendInArray = sendInArray;
	
	//------------------------------------------------------------------------------------------------------------
	function showLoading(box,str)
	{
        if(!showLoading.arguments.length) return false;
        
		//gE('foot_loading').innerHTML = "<img src = 'images/loadings/04.gif' />";

		var tex = (showLoading.arguments.length > 2) ? showLoading.arguments[2] : '';
		
		var pad = (box.clientHeight/2) - 16; //3351-1196
			
		box.innerHTML = "<span class = 'loading'><img style = 'padding-top:"+pad+"px' src = 'images/spinner.gif' /> <b>"+str+"</b></span>";
	
        //secondary loading
    }
	//------------------------------------------------------------------------------------------------------------
	function ajaxRetriev(box)
	{
		ajaxRetriev.xmlhttp = this.xmlhttp; // to work inside function onreadystatechange
		ajaxRetriev.execInside = this.execInside.toString();
		ajaxRetriev.execInsideComp = this.execInsideComp.toString();//
			
		this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    this.xmlhttp.send(this.vars.send());
		
		this.xmlhttp.onreadystatechange = function()
		{
			if(ajaxRetriev.xmlhttp.readyState == 4)
			{
				if(ajaxRetriev.xmlhttp.status == 200)
				{
					var wr =  url_decode(ajaxRetriev.xmlhttp.responseText);
		            wr = wr.replace(/\+/g," ");
		            wr = unescape(wr);
					box.innerHTML = wr;
					var zx = (document.ISGECKO) ? eval(ajaxRetriev.execInside) : eval("window.zx = "+ajaxRetriev.execInside);
					zx();
					//exec inside complement
					var eic = (document.ISGECKO) ? eval(ajaxRetriev.execInsideComp) : eval("window.eic = "+ajaxRetriev.execInsideComp);
					eic();
					// thanks to http://forum.imasters.com.br/index.php?showtopic=170987
				}
			}
	    }
	}
	//------------------------------------------------------------------------------------------------------------
	function ajaxRetrievXML()
	{
		ajaxRetrievXML.xmlhttp = this.xmlhttp; // to work inside function onreadystatechange
		ajaxRetrievXML.execInside = this.execInside.toString();
		ajaxRetrievXML.execInsideComp = this.execInsideComp.toString();

        this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
 	    this.xmlhttp.send(this.vars.send());
		this.xmlhttp.onreadystatechange = function()
		{
	        if(ajaxRetrievXML.xmlhttp.readyState == 4)
			{
				if(ajaxRetrievXML.xmlhttp.status == 200)
				{
					var wr =  ajaxRetrievXML.xmlhttp.responseXML;
					var zx = (document.ISGECKO) ? eval(ajaxRetrievXML.execInside) : eval("window.zx = "+ajaxRetrievXML.execInside);
					zx();
					var eic = (document.ISGECKO) ? eval(ajaxRetrievXML.execInsideComp) : eval("window.eic = "+ajaxRetrievXML.execInsideComp);
					eic();
				}
			}
	    }
	}
	
}

//------------------------------------------------------------------------------------------------------------
    function url_encode(str) { 
        var hex_chars = "0123456789ABCDEF"; 
        var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
        var n, strCode, hex1, hex2, strEncode = ""; 

        for(n = 0; n < str.length; n++) { 
            if (noEncode.test(str.charAt(n))) { 
                strEncode += str.charAt(n); 
            } else { 
                strCode = str.charCodeAt(n);
                hex1 = hex_chars.charAt(Math.floor(strCode / 16));
                hex2 = hex_chars.charAt(strCode % 16);
                strEncode += "%" + (hex1 + hex2);
            } 
        } 
        return strEncode; 
    } 

    // url_decode version 1.0 
    function url_decode(str) { 
        var n, strCode, strDecode = ""; 

        for (n = 0; n < str.length; n++) { 
            if (str.charAt(n) == "%") { 
                strCode = str.charAt(n + 1) + str.charAt(n + 2); 
                strDecode += String.fromCharCode(parseInt(strCode, 16)); 
                n += 2; 
            } else { 
                strDecode += str.charAt(n); 
            } 
        } 

        return strDecode; 
    }
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}