/*******************************************************************************
ajax versão 1.0
fishguil@hotmail.com

*******************************************************************************/


function ajax() {
};

ajax.prototype.iniciar = function() {
	try {this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e1) {
		try {this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			try {this.xmlhttp = new XMLHttpRequest();
			} catch (e3) {
				try {this.xmlhttp = window.createRequest();
				} catch (e4) {this.xmlhttp=false;}
			}
		}
	}
	this.param = new Array();
	return true;
}

ajax.prototype.ocupado = function() {
	estadoAtual = this.xmlhttp.readyState;
	return (estadoAtual && (estadoAtual < 4));
}

ajax.prototype.processa = function() {
	if (this.xmlhttp.readyState == 4 && this.xmlhttp.status == 200) {
		return true;
	}
}

ajax.prototype.parametro = function(nome,valor){
	if (!this.param){
		this.param = new Array();
	}
	this.param[this.param.length] = nome+'='+escape(valor);
}

ajax.prototype.smartform = function(frm){
	var el = frm.elements;
	for(i=0; i<el.length; i++){
		switch(el[i].type){
			case "hidden":{this.parametro(el[i].name,el[i].value);break;}
			case "password":{this.parametro(el[i].name,el[i].value);break;}
			case "text":{this.parametro(el[i].name,el[i].value);break;}
			case "textarea":{this.parametro(el[i].name,el[i].value);break;}
			case "radio":{if(el[i].checked){this.parametro(el[i].name,el[i].value)};break;}
			case "checkbox":{if(el[i].checked){this.parametro(el[i].name,el[i].value)};break;}
			case "select-one":{this.parametro(el[i].name,el[i].value);break;}
			case "select-multiple":{
				for (j = 0; j < el[i].options.length; j++)
					this.parametro(el[i].name+'[]',el[i][j].value);
				break;
			}
		}
	}
}

ajax.prototype.formulario = function(nome){
	this.smartform(document[nome]);
}


ajax.prototype.parentParam = function(){
	var query = window.location.search.substring(1);
  return query.split('&');
}

ajax.prototype.smartParam = function(){
  var str = this.param.join('&');
  str += (str) ? '&' : '';
  str += this.parentParam().join('&');
  return str;
}

ajax.prototype.enviar = function(url, metodo, async) {
    sessionTimeExtra();
    if (document.getElementById('sessaoexpira')) {
        document.getElementById('sessaoexpira').innerHTML='Sessão: ' + (maxsessionlifetime--) + ' minuto(s).';
    }
	if (!this.xmlhttp){
		this.iniciar();
	}
	if (!this.ocupado()){
		var hnd = this;
		if(metodo == "GET") {
			if(this.param.length){
				url += (url.indexOf('?')!=-1) ? '&' : '?' ;
				url += this.smartParam();
			}
			if(async){this.xmlhttp.open("GET", url, true);
			}else{this.xmlhttp.open("GET", url, false);}
			this.xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			this.xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			this.xmlhttp.setRequestHeader("Pragma", "no-cache");
			if(async){this.xmlhttp.onreadystatechange = function(){async(hnd);};}
			this.xmlhttp.send(null);
		} else if(metodo == "OPEN") {
			if(this.param.length){
				url += (url.indexOf('?')!=-1) ? '&' : '?' ;
				url += this.smartParam();
			}
			//window.open(url,"width=600,height=480");
      popup(url, 840, 740);
		} else {
			if(async){this.xmlhttp.open("POST", url, true);
			}else{this.xmlhttp.open("POST", url, false);}
			this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			this.xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
			this.xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
			if(async){this.xmlhttp.onreadystatechange = function(){async(hnd);};}
			this.xmlhttp.setRequestHeader("Pragma", "no-cache");
			this.xmlhttp.send(this.smartParam());
		}
		if(this.processa()){
			return true;
		}
	}
	return false;
}

ajax.prototype.receber = function(){
	if(this.processa()){
		return this.xmlhttp.responseText;
	} else {
		return false;
	}
}

ajax.prototype.receberData = function(){
	if(this.processa()){
		return this.xmlhttp.responseBody;
	} else {
		return false;
	}
}

ajax.prototype.receberHeader = function(name){
	if(this.processa()){
		if(name){
			return this.xmlhttp.getResponseHeader(name);
		}
		return this.xmlhttp.getAllResponseHeaders();
	} else {
		return false;
	}
}

ajax.prototype.limpar = function(){
	this.iniciar();
	this.param = new Array();
}

ajax.prototype.receberXML = function(elem){
	var node = this.xmlhttp.responseXML.getElementsByTagName(elem);
	if (node)
		return node;
	return false
}

ajax.prototype.item = function(node,index,attr,ref){
	if(index){
		if (attr){
			res = node(index).getAttribute(attr);
		}else{
			res = node(index).nodeValue;
		}
	} else {
		if (attr){
			res = node.nextNode().getAttribute(attr);
		}else{
			res = node.nextNode().nodeValue;
		}
	}
	if(ref){
		try {document.getElementById(ref).innerHTML = res;
		} catch(e) {document.getElementById(ref).value = res;}
	}else{
		return res
	}
}

/*******************************************************************************
tbAJX versão 1.0
fishguil@hotmail.com

*******************************************************************************/
function tbAJX(){
};

tbAJX.prototype.setForm = function(formfield){
	this.formfield = formfield;
}

tbAJX.prototype.carregaForm = function(countfield, orderfield, offsetfield, lengthfield){
	this.countfield = countfield;
	this.orderfield = orderfield;
	this.offsetfield = offsetfield;
	this.lengthfield = lengthfield;
}

tbAJX.prototype.carregaTabela = function(tablespan, infospan, firstbutton, previousbutton, nextbutton, lastbutton){
	this.tablespan = tablespan;
	this.infospan = infospan;
	this.firstbutton = firstbutton;
	this.previousbutton = previousbutton;
	this.nextbutton = nextbutton;
	this.lastbutton = lastbutton;
}

tbAJX.prototype.listar	= function(){ return true; };
tbAJX.prototype.alterar = function(){ return false; };
tbAJX.prototype.total	 = function(){ return 0; };
tbAJX.prototype.tabela	= function(){ return ""; };

tbAJX.prototype.carregar = function(){
  if(this.formfield){
  	this._ct  = document[this.formfield][this.countfield];
  	this._or  = document[this.formfield][this.orderfield];
  	this._of  = document[this.formfield][this.offsetfield];
  	this._ln  = document[this.formfield][this.lengthfield];
  	this._bt1 = document[this.formfield][this.firstbutton];
  	this._bt2 = document[this.formfield][this.previousbutton];
  	this._bt3 = document[this.formfield][this.nextbutton];
  	this._bt4 = document[this.formfield][this.lastbutton];
  }else{
  	this._ct  = document.getElementById(this.countfield);
  	this._or  = document.getElementById(this.orderfield);
  	this._of  = document.getElementById(this.offsetfield);
  	this._ln  = document.getElementById(this.lengthfield);
    this._bt1 = document.getElementById(this.firstbutton);
    this._bt2 = document.getElementById(this.previousbutton);
    this._bt3 = document.getElementById(this.nextbutton);
    this._bt4 = document.getElementById(this.lastbutton);
  }
  this._tb  = document.getElementById(this.tablespan);
  this._nf  = document.getElementById(this.infospan);
	var hnd = this;
	this._bt1.onclick = function(){hnd.click1();};
	this._bt2.onclick = function(){hnd.click2();};
	this._bt3.onclick = function(){hnd.click3();};
	this._bt4.onclick = function(){hnd.click4();};
	this._ct.value = this.total;
	this._tb.innerHTML = this.tabela;
	this.formatar();
	this.indexar();
}

tbAJX.prototype.formatar = function(){
	var tbls, cols, rows, t, c, r, el, cl, ob;
	var tb = this._tb.getElementsByTagName("table");
	if(tb.length)
	for(var i = 0; i<tb.length; i++){
		ob = this;
		tb[i].className = "tbAJX";
		//columns
		cols = tb[i].getElementsByTagName("th");
		for(c = 0; c<cols.length; c++){
			el = cols[c];
			el.className = "lt";
			el.onmouseout	= function(){this.className = "lt";};
			el.onmouseover = function(){this.className = "sl";};
			el.onmousedown = function(){this.className = "dk";};
			el.onmouseup	 = function(){this.className = "sl";};
			ob = this;
			el.onclick		 = function(){ob.ordenar(this.id);};
		}
		//rows
		rows = tb[i].getElementsByTagName("tr");
		for(r = 0; r<rows.length; r++){
			el = rows[r];
			//skip headers
			if (el.getElementsByTagName("td").length){
				if(r%2){
					el.className = "lt";
					el.onmouseout	= function(){this.className = "lt";};
				}else{
					el.className = "dk";
					el.onmouseout	= function(){this.className = "dk";};
				}
				el.onmouseover = function(){this.className = "sl";};
				el.onmousedown = function(){this.className = "dk";};
				el.onmouseup	 = function(){this.className = "sl";};
				if(ob.alterar)
          el.ondblclick	= function(){ob.alterar(this.id,true);};
			}
		}
	}
}

tbAJX.prototype.ordenar = function(ord){
	var idx = (this._or.value)?parseInt(this._or.value):1;
	if(ord == idx || ord == (-1)*idx){
		this._or.value = (-1)*idx;
	}else{
		this._or.value = ord;
	}
	this._of.value = 0;
	this.listar();
	this.indexar();
}

//reformar função
tbAJX.prototype.indexar = function(){
	var cnt = (this._ct.value)?parseInt(this._ct.value):0;
	var off = (this._of.value)?parseInt(this._of.value):1;
	var len = (this._ln.value)?parseInt(this._ln.value):50;
	off = (off>cnt) ? cnt : off;
	len = (len>cnt) ? cnt : len;
	var pag	 = Math.floor((off+len)/len);
	var npag	= Math.ceil(cnt/len);
	off = (off == 0) ? (cnt) ? 1 : 0 : off;
	pag = isNaN(pag) ? (cnt) ? 1 : 0 : pag;
	npag = isNaN(npag) ? (cnt) ? 1 : 0 : npag;
	pag = (pag>npag) ? npag : pag;
	var str = 'Mostrando página '+pag+' de '+npag+', de '+off+' até '+cnt+' Registro(s):';
	try{this._nf.innerHTML = str;
	}catch(e){this._nf.value = str;}
}

tbAJX.prototype.click1 = function(){
	var off = (this._of.value)?parseInt(this._of.value):0;
	if(off != 0){
		this._of.value = 0;
		this.listar();
		this.indexar();
	}
}

tbAJX.prototype.click2 = function(){
	var off = (this._of.value)?parseInt(this._of.value):0;
	var len = (this._ln.value)?parseInt(this._ln.value):50;
	if(off != 0){
		off -= len;
		this._of.value = (off<0)?0:off;
		this.listar();
		this.indexar();
	}
}

tbAJX.prototype.click3 = function(){
	var cnt = (this._ct.value)?parseInt(this._ct.value):0;
	var off = (this._of.value)?parseInt(this._of.value):0;
	var len = (this._ln.value)?parseInt(this._ln.value):50;
	if((off+len) < cnt){
		off += len;
		this._of.value = (off>cnt)?cnt:off;
		this.listar();
		this.indexar();
	}
}

tbAJX.prototype.click4 = function(){
	var cnt = (this._ct.value)?parseInt(this._ct.value):0;
	var off = (this._of.value)?parseInt(this._of.value):0;
	var len = (this._ln.value)?parseInt(this._ln.value):50;
	if((off+len) < cnt){
		this._of.value = (Math.floor(cnt/len)*len);
		this.listar();
		this.indexar();
	}
}
