/*
Thiago amaral soalheiro

Adekz.com

Copyright © 2006.

É expressamente proibida a copia parcial ou total de qualquer parte desse arquivo.

Versao 2.0 - 23/10/2006*/

function Pilha(){
	this.n = 0;
	this.V = new Array();
	this.push = function (el){
		this.V[this.n++]=el;
	}	
	this.pop = function (){
		return this.V[--this.n];
	}	
}


function parsing(txt){
	var i=0;
	var tam=txt.length;
	var c;
	var word="";
	var obj = new Obj();
	var nome="";
	var pilha=new Pilha();
	var divd=0;
	while(i<tam){
		c = txt.charAt(i++);
		if (c=="\\"){ 
			c = txt.charAt(i++);
			word+=c;
		}else if(c==","){
			divd=1;
			obj.add(nome, word);
			word="";
		}else if(c==":"){
			divd=1;
			nome = word;
			word="";
		}else if(c=="{"){
			divd=1;
			pilha.push(obj);
			pilha.push(nome);
			obj = new Obj();
		}else if(c=="}"){
			divd=1;
			var ta = obj;
			nome = pilha.pop();
			obj = pilha.pop();
			obj.add(nome,ta);
		}else{
			word+=c;
		}
	}
	if (divd==0) return txt;	
	return obj;
}

var SIZE=200;
var DEBUGANDO=0;
function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function $(name){
  if (document.getElementById)
  {
  	return document.getElementById(name);
  }
  else if (document.all)
  {
	return document.all[name];
  }
  else if (document.layers)
  {
   	return document.layers[name];
  }
}

function getFormAll(numform, destino){
	for (var i=0;i<document.forms[numform].elements.length; i++){
		if (document.forms[numform].elements[i].type=='radio' 
			|| document.forms[numform].elements[i].type=='checkbox' ){
			if (document.forms[numform].elements[i].checked){
				destino.add(document.forms[numform].elements[i].name,document.forms[numform].elements[i].value);
			}
		}else
			destino.add(document.forms[numform].elements[i].name,document.forms[numform].elements[i].value);
	}
}

function getObj(nme){
	return document.getElementById(nme);
}
 

function AjaxData()
{
	this.n=0;
	this.vk = new Array(256);
	this.vd = new Array(256);
	this.addArray = function (oba){
		for(x in oba){
			this.vk[this.n]=x;
			this.vd[this.n++]=$(x).value;
		}
	}
	this.add = function(key, data){
		this.vk[this.n]=key;
		this.vd[this.n++]=data;		
	}	
	this.make = function(){
		var ret="";
		for (var i=0;i<this.n ;i++){
			if (i!=0)
				ret+="&"+this.vk[i]+"="+escape(this.vd[i]);
			else
				ret+=this.vk[i]+"="+escape(this.vd[i]);
		}
		return ret;
	}
}

function AjaxObject(url, metodo, ajaxdata, retorno){
	this.url = url;
	this.metodo = metodo;
	this.ajaxdata = ajaxdata;
	this.retorno = retorno;
}

function Lista(){
	this.v= new Array(SIZE);
	for (var i=0; i<SIZE ; i++){
		this.v[i]=null;
	}
	this.ns=0;
	this.ne=0;
	this.add = function (obj){
		if (this.cheia()){ return false; }
		this.v[this.ne]=obj;
		this.ne = (this.ne+1)%SIZE;

	}
	this.cheia = function (){
		return ((this.ns == this.ne)&&(this.v[this.ns]!=null));
	}
	this.vazia = function(){
		return (this.ns == this.ne && this.v[this.ns]==null);
	}
	this.get =  function(){
		if (this.vazia()) return null;
		var obj = this.v[this.ns];
		backup = this.v[this.ns];
		this.v[this.ns]=null;
		this.ns = (this.ns+1)%SIZE;
		return obj;
	}	
	this.print =  function(){
		window.alert(this.ns+"//"+this.ne+"//"+this.v);
	}
}


function Ajax(){
   this.semaf=0;	
   this.lista = new Lista();
   this.xmlhttp=null;
   this.handle="";	
   if (window.XMLHttpRequest) {
      this.xmlhttp = new XMLHttpRequest();
   } else if (window.ActiveXObject) {
      this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
      alert("Seu navegador n&atilde;o suporta XMLHttpRequest.");
      return;
   }
   this.enviar = function (url, metodo, ajaxdata, retorno){
		this.lista.add(new AjaxObject(url, metodo, ajaxdata, retorno));
		tmp_executa();
   }
   this.start=function (time){
		this.handle = window.setInterval('tmp_executa();',time);
   }
   this.stop=function (){
		window.clearInterval(this.handle);
   }
}

function AjaxResponse(texto){
	try{
	return  parsing(texto);	
	}catch(xrt){
	
	}
	return texto;
}



function tmp_executa(){ 

	if (ajax.semaf==0){
	if (!ajax.lista.vazia()){

		ajax.semaf=1;
		var dao = ajax.lista.get();

		if (dao.metodo=="GET")
		    ajax.xmlhttp.open(dao.metodo, dao.url+"?"+dao.ajaxdata.make(), true);
		else
		    ajax.xmlhttp.open(dao.metodo, dao.url, true);

	    ajax.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	    ajax.xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	    ajax.xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");	
	    ajax.xmlhttp.setRequestHeader("Pragma", "no-cache");	

	    ajax.xmlhttp.onreadystatechange = function(){ 
			
			try{
				//alert(ajax.xmlhttp.readyState+"//"+ajax.xmlhttp.status);
				if (ajax.xmlhttp.readyState == 4) {
					if (ajax.xmlhttp.status == 200) {	
						   if (DEBUGANDO==1) {
								alert(ajax.xmlhttp.responseText);
						   }
						  var retornado = (ajax.xmlhttp.responseText); 
						  dao.retorno(AjaxResponse(retornado));	
						  ajax.semaf=0;	
							//alert('qsed');
						  try{/*alert('ended'); */$('loading').style.display = 'none'; }catch(ekl){}
					}else{
						alert('Falha no carramento ('+ajax.xmlhttp.status+').');						 
						ajax.semaf=0;	 
						try{/*alert('ended'); */$('loading').style.display = 'none'; }catch(ekl){}
					}
				}

			}catch(e){
				ajax.semaf=0;	
			}
		}

		if (dao.metodo=="GET")
		    ajax.xmlhttp.send(null); 		
		else{
			if (DEBUGANDO==1)
				alert(dao.url+"/"+dao.ajaxdata.make());
		    ajax.xmlhttp.send(dao.ajaxdata.make()); 		
		}
		try{ $('loading').style.display = ''; }catch(ekl){}

	}		
	}	
}



