
// Script B?sicos Para Utiliza??o em todas as p?ginas


// Retorna o objeto da p?gina com o nome 
// passado como par?metro. Implementada
// para funcionamento em todos os browsers.
function getObj(name){
	if (document.getElementById){
	    return document.getElementById(name);
 	} else if (document.all){
    	return document.all[name];
	} else if (document.layers){
	   return document.layers[name];
    }
}	


// Checa o radio o valor item de um radio button apartir do valor passado.
function checarRadioButton(radio, value){
	for(i = 0; i < radio.length; i++){
		if(radio[i].value == value){
			radio[i].checked = true;
		}
	}
}
  
// Retorna o valor do radio button.
function buscaValorRadioButton(radio){ 
	for(i = 0; i < radio.length; i++){
		if(radio[i].checked){
			return radio[i].value
		}
	}
}

// Retorna o objeto HttpXmlRequest para quaisquer browsers.
function getXmlRequest(){
	xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

// Posta um formul?rio para a url passada, e seta o retorno no divid passado
function postAjaxForm(form, url, divid){
	urlParams = criaAjaxUrl(form, url);
	req = getXmlRequest();
	
    req.open("POST", url, true);
    
    req.onreadystatechange=function() {
	    if (req.readyState==4) {
		    atualizaAjaxHtml(req.responseText, divid);
		    escondeCarregando(divid);
	   	}
    }
    
	escreveCarregando(divid);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(urlParams);
}

// Atualiza o conteudo de um divid
function atualizaAjaxHtml(resposta, divid){
	getObj(divid).innerHTML= resposta;
}

// Cria a URL do Ajax apartir dos campos do formul?rio.
function criaAjaxUrl(form){
	elements = form.elements;
	url = '';
	sufixo = '&';
	for(i =0; i < elements.length; i++){
		if(i == (elements.length - 1)){
			sufixo = '';
		}
		isDisabled = elements[i].disabled;
		
		isChecked = true;
		if(elements[i].type == 'checkbox' || elements[i].type == 'radio'){
			if(elements[i].checked){
				isChecked = true;
			} else {
				isChecked = false;
			}
		}
			
		if(!isDisabled && isChecked){
			url = url + elements[i].name+'='+escape(elements[i].value) + sufixo;
		}
	}
	return url;
}

// Mantem o client processando em tantos milisegundos
function wait(millis){
	date = new Date();
	var curDate = null;

	do { var curDate = new Date(); }
	while(curDate-date < millis);
} 

function escreveCarregando(div){
	getObj("tabCarregandoAjax").style.display = '';
}

function escondeCarregando(div){
	getObj("tabCarregandoAjax").style.display = 'none';
}