/*********************************************/
/**************** Objeto Ajax ****************/
/*********************************************/
function url_encode(str) {
	var hex_chars = '0123456789ABCDEF';
	var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
	var n, strCode, hex1, hex2, strEncode = '';  
	for(n = 0; n < str.length; n++) {
		if(noEncode.test(str.charAt(n))) {
			strEncode += str.charAt(n);
		}
		else {
			strCode = str.charCodeAt(n);
			hex1 = hex_chars.charAt(Math.floor(strCode / 16));
			hex2 = hex_chars.charAt(strCode % 16);
			strEncode += '%' + (hex1 + hex2);
		}
	}
	return strEncode;
}

function getElementById(id){
	return document.getElementById(id);
}

_textoProgresso="Carregando...";

function ajax() {}
ajax.getProgressHtml = function() {
	return "<div style=\"margin: 1px 0px 0px 4px; font-weight:bold; float:left;\">"+_textoProgresso+"</div>";
	
}
ajax.showProgress = function(id) {
	if (id != "")
	{
		getElementById(id).innerHTML = ajax.getProgressHtml();
		getElementById(id).style.display = 'block';
	}
}
ajax.getErrorHtml = function(p_req) {
	return "<p class='erro'>" + "(" + p_req.status + ") " + p_req.statusText + "</p>"
}
ajax.getRequest = function() {
	var xmlHttp;
	try { xmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); return xmlHttp; } catch (e) {}
	try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); return xmlHttp; } catch (e) {}
	try { xmlHttp = new XMLHttpRequest(); return xmlHttp; } catch(e) {}
	alert("Esse browser não tem recursos para uso do AJAX!");
	return null;
}
ajax.abrir = function(url,objetoId,p_successCallBack,p_errorCallBack,p_pass) {
	//p_successCallBack: callback function for successful response
	//p_errorCallBack: callback function for erroneous response
	//p_pass: string of params to pass to callback functions
	var params = p_pass.split("&");
	var novoAjax = ajax.getRequest();
	if (novoAjax != null) {
		p_busyReq = true;
		if(objetoId != ''){
			ajax.showProgress(objetoId);
		}
		novoAjax.onreadystatechange = function() {
			if (novoAjax.readyState == 4) {
				p_busyReq = false;
				window.clearTimeout(toId);
				if (novoAjax.status == 200) {
					p_successCallBack(objetoId,novoAjax,params);
				} else {
					if(objetoId != ''){
						getElementById(objetoId).innerHTML = ajax.getErrorHtml(novoAjax);
					}else{
						alert(novoAjax.statusText);
					}
				}
			}
		}
		novoAjax.open("POST", url, true);
		if(params.length > 1 || (params.length == 1 && params[0] != '')) {
			novoAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			novoAjax.send(p_pass);
		}else{
			novoAjax.send(null);
		}
		var toId = window.setTimeout( function() {if (p_busyReq) novoAjax.abort();}, ajax.timeout );
	}
}
ajax.timeout = 5000;//5 seconds
/************* Fim do Objeto Ajax *************/


/************ enquete *******************/
function inicia() {
	var req;
	try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch(e) {
 		try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(ex) {
  		try { req = new XMLHttpRequest(); }
			catch(exc) {
   			alert("Esse browser não tem recursos para uso do AJAX!");
   			req = null;
  		}
 		}
	}
	return req;
}

function enquete_votar(id_enquete) {
	var obj = document.getElementById('msg');
	var v = enquete_validar();
	if(v==false) {
		obj.innerHTML = 'Escolha uma opção para votar.';
	}
	else {
		ajax = inicia();
		if(ajax) {
			obj.style.color = '#000000';
			ajax.open("POST","paginas/enquete/votar.php", true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 1) {
					obj.innerHTML = 'Enviando dados...';
				}
				if(ajax.readyState == 4 ) {
					if(ajax.status == 200) {
						if(ajax.responseText==0) obj.innerHTML = 'ERRO!';
						if(ajax.responseText==1) obj.innerHTML = 'VOTO REGISTRADO COM SUCESSO!';
						if(ajax.responseText==2) obj.innerHTML = 'VOCÊ JÁ VOTOU...';
					}
				}
			}
		}
		var dados = "id_resposta="+v+"&id_enquete="+id_enquete;
		ajax.send(dados);
	}
	setTimeout("document.getElementById('msg').innerHTML='';",5000);
}

function enquete_parciais(id_enquete,obj) {
	var posY = findPosY(obj);
	var posX = findPosX(obj);
	enquete_posiciona(posY,posX);
	var obj = document.getElementById('parciais');
	ajax = inicia();
	if(ajax) {
		ajax.open("POST","paginas/enquete/parciais.php", true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 1) {
				obj.innerHTML = 'Carregando...';
			}
			if(ajax.readyState == 4 ) {
				if(ajax.status == 200) {
					var texto=ajax.responseText;
					texto=texto.replace(/\+/g," ");
					texto=unescape(texto);
					obj.innerHTML = texto;
//					divX = obj.offsetHeight;
					enquete_vis_parciais(1);
				}
			}
		}
	}
	var id_enquete = "id_enquete="+id_enquete;
	ajax.send(id_enquete);
}