// JavaScript Document
var xmlhttp;

try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

	
var ajax = {
	//ajax busca bairro
	resultado_enquete : function() { 
		//document.getElementById('price').innerHTML = "Recalculando...";
		
		param = "&acao=1&enq_perg="+document.frmenquete.enq_perg.value;
		xmlhttp.open("POST", "resultado.php", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		
		xmlhttp.onreadystatechange = function(){
				//document.getElementById('price').innerHTML = "Recalculando...";
				if(xmlhttp.readyState == 4) { 
					if(xmlhttp.status == 200){ 
						document.getElementById('enquete').innerHTML = xmlhttp.responseText;
					}else{ //Não encotra os dados
						document.getElementById('enquete').innerHTML = "Erro: returned status code " + xmlhttp.status + " " + xmlhttp.statusText;
					}
				}
			}
			xmlhttp.send(param);
	},
	
	formulario_enquete : function() { 
		//document.getElementById('price').innerHTML = "Recalculando...";
		
		param = "&acao=3&enq_perg="+document.frmenquete.enq_perg.value;
		xmlhttp.open("POST", "resultado.php", true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		
		xmlhttp.onreadystatechange = function(){
				//document.getElementById('price').innerHTML = "Recalculando...";
				if(xmlhttp.readyState == 4) { 
					if(xmlhttp.status == 200){ 
						document.getElementById('enquete').innerHTML = xmlhttp.responseText;
					}else{ //Não encotra os dados
						document.getElementById('enquete').innerHTML = "Erro: returned status code " + xmlhttp.status + " " + xmlhttp.statusText;
					}
				}
			}
			xmlhttp.send(param);
	},
	
	votar_enquete : function() { 
		//document.getElementById('price').innerHTML = "Recalculando...";
		
		var totElementos = document.frmenquete.length;
		var msgErro = "";
		var opcaoEscolhida = false;
		var i = 0;
		var valor;
		for (i=0; i < totElementos; i++) {
			var tipo = document.frmenquete[i].type;
			if(tipo == "checkbox" || tipo == "radio"){
				if (document.frmenquete[i].checked){valor= document.frmenquete[i].value; opcaoEscolhida = true; break;}
			}
		}

		if (!opcaoEscolhida){
			msgErro = "Escolha uma das opções";
		}
		else{
			if (i < totElementos - 1){
				i ++;
			}
		}
	
		if (msgErro != ""){
			alert(msgErro); 
		} else {
			param = "&acao=2&enq_perg="+document.frmenquete.enq_perg.value+"&opcao="+valor;
			xmlhttp.open("POST", "resultado.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			
			xmlhttp.onreadystatechange = function(){
					//document.getElementById('price').innerHTML = "Recalculando...";
					if(xmlhttp.readyState == 4) { 
						if(xmlhttp.status == 200){ 
							document.getElementById('enquete').innerHTML = xmlhttp.responseText;
						}else{ //Não encotra os dados
							document.getElementById('enquete').innerHTML = "Erro: returned status code " + xmlhttp.status + " " + xmlhttp.statusText;
						}
					}
				}
				xmlhttp.send(param);
			}
	}  
}