// -------------------------------------------------------------------------------------------
//  Verifica para qual objeto(XMLHttpRequest) o browser oferece suporte
// -------------------------------------------------------------------------------------------

	try {
		xmlhttp = new XMLHttpRequest();
	} catch(ee) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				xmlhttp = false;
			}
		}
	}
	
//--------------------------------------------------------------------------------------------
//  Função AJAX para obter um resultado (select)
//--------------------------------------------------------------------------------------------

	var resultados
	
	function iniciaAjax(linksql,vars,carregando)
	{
		var args = iniciaAjax.arguments.length
		//esconde descrição do erro
		document.getElementById("erroJavaDesc").style.display = 'none'
		document.getElementById("erroJava").style.width = '150px'
		//esconde erro
		document.getElementById("erroJava").style.display = 'none'
		//mostra carregando
		document.getElementById(carregando).style.display = 'block'
		
		//Monta a url com a querysting da gerencia
		xmlhttp.open("POST", linksql, true);
		
		xmlhttp.onreadystatechange = function()
		{
			//quando estiver concluido
			if (xmlhttp.readyState==4)
			{
				try {
					//Obtém o retorno
					t = xmlhttp.responseText
					
					//Trata o retorno
					t = unescape(t.replace(/\+/g," "))
					t = t.replace(/\n/g,"") //adicionado em 17/10/08
					//alert(t)
					
					//Transforma em objeto
					resultados = eval(t)
					
					if (args == 3) mostraResultados(resultados)
					else mostraResultadosVerificacao(resultados)
					
					document.getElementById(carregando).style.display = 'none'
				}
				catch(e)
				{
					document.getElementById(carregando).style.display = 'none'
					if (e.description) { desc_erro = e.description } else { desc_erro = e }
					document.getElementById("erroJavaDesc").innerHTML = desc_erro
					document.getElementById("erroJava").style.display = 'block'
				}
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
		
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate") //elimina cache
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0") //elimina cache
		xmlhttp.setRequestHeader("Pragma", "no-cache") //elimina cache
		
		xmlhttp.send(vars) 
	}
	
//--------------------------------------------------------------------------------------------
//  Função AJAX para sem obter resultado (insert, update, delete), Acao é para saber qual msg mostrar apos a acao
//--------------------------------------------------------------------------------------------

	function iniciaAjaxExecuta(linksql,vars,acao,carregando)
	{
		//esconde descrição do erro
		document.getElementById("erroJavaDesc").style.display = 'none'
		document.getElementById("erroJava").style.width = '150px'
		//esconde erro
		document.getElementById("erroJava").style.display = 'none'
		//mostra carregando
		document.getElementById(carregando).style.display = 'block'
		var args = iniciaAjaxExecuta.arguments.length
		
		//Monta a url com a querysting da gerencia
		xmlhttp.open("POST", linksql, true);
		
		xmlhttp.onreadystatechange = function() {
			//quando estiver concluido
			if (xmlhttp.readyState==4)
			{
				try
				{
					/*if(t = xmlhttp.responseText)
					{
						t = unescape(t.replace(/\+/g," "))
						alert(t)
					}*/
					if (args == 4 && acao == "1000") { } //chamada ajax para saida da area restrita
					else if (args == 4) mostraResultados(acao) //chamada ajax normal
					else mostraResultadosVerificacao(acao) //chamada ajax da area restrita
						
					document.getElementById(carregando).style.display = 'none'
				}
				catch(e)
				{
					document.getElementById(carregando).style.display = 'none'
					if (e.description) { desc_erro = e.description } else { desc_erro = e }
					document.getElementById("erroJavaDesc").innerHTML = desc_erro
					document.getElementById("erroJava").style.display = 'block'
				}
			}
		}
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
		
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate") //elimina cache
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0") //elimina cache
		xmlhttp.setRequestHeader("Pragma", "no-cache") //elimina cache

		xmlhttp.send(vars) 
	}
	
// -------------------------------------------------------------------------------------------
//  Área restrita do cooperado
// -------------------------------------------------------------------------------------------

	function mostraResultadosVerificacao(resultados)
	{
		if (resultados == 1)
		{
			iniciaAjaxExecuta ("http://www.unimedvr.com.br/session/cooperado_ajax.php","crm="+document.form_area_restrita_cooperado.area_restrita_carteira_cooperado.value,2,"carregando",50,50)
		}
		if (resultados == 0)
		{
			document.getElementById("msgErroAreaRestritaCooperado").innerHTML = 'Senha não confere'	
			document.getElementById("msgErroAreaRestritaCooperado").style.display = 'block'
		}
		if (resultados == 2)
		{
			document.form_area_restrita_cooperado.submit()	
		}
		if (resultados == 3)
		{
			document.getElementById("msgErroAreaRestritaColaborador").innerHTML = 'Senha não confere'	
			document.getElementById("msgErroAreaRestritaColaborador").style.display = 'block'
		}
		if (resultados == 4)
		{
			iniciaAjaxExecuta ("http://www.unimedvr.com.br/session/colaborador_ajax.php","crm="+document.form_area_restrita_colaborador.area_restrita_usuario_colaborador.value+"&key="+document.form_area_restrita_colaborador.area_restrita_senha_colaborador.value,5,"carregando",50,50)
		}
		if (resultados == 5)
		{
			document.form_area_restrita_colaborador.submit()	
		}
		if (resultados == 6)
		{
			document.getElementById("msgErroAreaRestritaCliente").innerHTML = 'Dados não conferem'	
			document.getElementById("msgErroAreaRestritaCliente").style.display = 'block'
		}
		if (resultados == 7)
		{
			iniciaAjaxExecuta ("http://www.unimedvr.com.br/session/cliente_ajax.php","crm="+document.form_area_restrita_cliente.area_restrita_carteira_cliente.value,8,"carregando",50,50)
		}
		if (resultados == 8)
		{
			document.form_area_restrita_cliente.submit()	
		}
		if (resultados == 9)
		{
			document.getElementById("msgErroAreaRestritaCorretora").innerHTML = 'Senha não confere'	
			document.getElementById("msgErroAreaRestritaCorretora").style.display = 'block'
		}
		if (resultados == 10)
		{
			iniciaAjaxExecuta ("http://www.unimedvr.com.br/session/corretora_ajax.php","crm="+document.form_area_restrita_corretora.area_restrita_usuario_corretora.value+"&key="+document.form_area_restrita_corretora.area_restrita_senha_corretora.value,11,"carregando",50,50)
		}
		if (resultados == 11)
		{
			document.form_area_restrita_corretora.submit()	
		}
		if (resultados == 13)
		{
			iniciaAjaxExecuta ("http://www.unimedvr.com.br/session/credenciado_ajax.php","crm="+document.form_area_restrita_credenciado.area_restrita_carteira_credenciado.value,14,"carregando",50,50)
		}
		if (resultados == 12)
		{
			document.getElementById("msgErroAreaRestritaCredenciado").innerHTML = 'Senha não confere'	
			document.getElementById("msgErroAreaRestritaCredenciado").style.display = 'block'
		}
		if (resultados == 14)
		{
			document.form_area_restrita_credenciado.submit()	
		}
	}


// -------------------------------------------------------------------------------------------
//  Monta variáveis POST automaticamente a partir de um formulario para enviar via AJAX
// -------------------------------------------------------------------------------------------

function criaPosts(form)
{
	var post = ""
	
	for (var i=0; i<document.form.elements.length; i++)
	{
			campoForm = document.form.elements[i]
			post += "&" + campoForm.name + "=" + escape(campoForm.value)
	}
	
	return post
}

