function criaObjeto(){
    var obj;
	try{		
		// XmlHttpRequest para Firefox,	Opera, Safari e derivados.		
		obj = new XMLHttpRequest();	
	}	catch (e){		
		// XmlHttpRequest para Internet Explorer.		
		try{			
		// Internet Explorer 6.0+
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			// Internet Explorer.			
			obj = new ActiveXObject("Microsoft.XMLHTTP");		
		}	
	}	
	return obj;
}

var http = criaObjeto();
var http2 = criaObjeto();
var nomeDiv;

function execute(campo,acao,mensagem){
	if (mensagem != undefined){
		document.getElementById(campo).innerHTML = '<div id="carregando"><div class="box">'+ mensagem + '</div></div>';
	}
	nomeDiv = campo;
    http.open('GET',acao);
    http.onreadystatechange = processaResposta;
    http.send(null);
}

function processaResposta(){
    if(http.readyState == 4){
        if(http.status == 200){
            var resposta = http.responseText;
            document.getElementById(nomeDiv).innerHTML = resposta;
            if (resposta.indexOf('excluiu') >0){
            	alert("Exclusão efetuada com sucesso.");
            }
            
            // Formulário da reserva - envia para o pagseguro
            if (resposta.indexOf('paymentRequest')>0){ 
                inicio = resposta.indexOf('*')+1;
				fim = resposta.indexOf('**');
            	urlPagSeguro = resposta.substring(inicio,fim);                 
                               
                inicio = resposta.indexOf('-')+1;
				fim = resposta.indexOf('--');
            	percentual = resposta.substring(inicio,fim); 
                if(percentual < 100){
                    alert("O valor a ser pago agora corresponde a "+percentual+"% do valor total. O valor restante deverá ser pago no momento do recebimento das chaves.");
                }
                location.href = urlPagSeguro;
				//document.getElementById('reservarAgora').submit();
            }
            
            // Display de saida
            if (resposta.indexOf('displaySaida') > 0){
            	inicio = resposta.indexOf('-')+1;
				fim = resposta.indexOf('--');
            	texto = resposta.substring(inicio,fim);   
                document.getElementById('reservaFinal').value = texto;            	
            } 
            
            // Seta o valor a ser pago
            if (resposta.indexOf('pagamentoHidden') > 0){
            	inicio = resposta.indexOf('|')+1;
				fim = resposta.indexOf('||');
            	texto = resposta.substring(inicio,fim);   
                document.getElementById('itemAmount1').value = texto;            	
            } 
     
            
            if (resposta.indexOf('retornoDinamico') > 0){
            	inicio = resposta.indexOf('*')+1;
				fim = resposta.indexOf('**');
            	texto = resposta.substring(inicio,fim);            	
            	alert(texto);
            }              
        }else{
            document.getElementById(nomeDiv).innerHTML = "Erro.";
        }
      
    }
    
}


