function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}



window.onload = loadFunctions;

function loadFunctions() {
	ativarbtnVotar();
	
}

function gE(ID) {
	return document.getElementById(ID);
}

function gEs(tag) {
	return document.getElementsByTagName(tag);
}

function ativarbtnVotar() {
	
			
	
	gE('btnVotar').onclick = function() {
		exibirBgBody();
		boxCad();
	
	var obj;

	obj = document.frmCad;

	var idenquete;
	var idresposta;
	var i;
	
	for (i=0;i<obj.idresposta.length;i++)
	{
	   if (obj.idresposta[i].checked)
	   {
	   		break;
	   }
	      
	}
	
	idenquete = obj.idenquete.value;
	idresposta = obj.idresposta[i].value;

	var ajax = openAjax();
	var recipiente = gE('boxCad');
	ajax.open('GET', 'mostra_enquete.php?idresposta='+idresposta+'&idenquete='+idenquete, true); 
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 1) {
			loading(true);	
		} 
		if (ajax.readyState == 4) {
			if (ajax.status == 200) {
				loading(false);
				recipiente.innerHTML = ajax.responseText;
				btnOk();
			}
		}
	}
	ajax.send(null);
	return false;
	}
}

function exibirBgBody() {
	
	
	var tagBody = gEs('body').item(0);
	var sizesPage = getPageSize();
	var bgBody = document.createElement('div');
	bgBody.setAttribute('id','bgBody');
	bgBody.style.height = arrayPageSize[1] + 'px';
	bgBody.style.width = arrayPageSize[0] + 'px';
	if (!gE('bgBody')) {
		tagBody.insertBefore(bgBody, tagBody.firstChild);
	}	
}

function boxCad() {
	var objBody = gEs('body').item(0);
	var sizesPage = getPageSize();
	var boxCad = document.createElement('div');
	boxCad.setAttribute('id','boxCad');
	var wPage = arrayPageSize[0];
	var hPage = arrayPageSize[1];
	//boxCad.style.width = (wPage / 2) + 'px';
	boxCad.style.height = 'auto';
	boxCad.style.marginTop = '300px';
	//boxCad.style.marginLeft = -(wPage / 4) + 'px'; 
	objBody.insertBefore(boxCad, objBody.lastChild);
}

function loading(opt) {
	if (opt == true) {
		var refer = gE('bgBody');
		var referHeight = refer.offsetHeight;
		refer.style.textAlign = 'center';
		var img = document.createElement('img');
		img.setAttribute('src','../images/imgLoading.gif');
		img.setAttribute('id','loading');
		img.setAttribute('width','126');
		img.style.marginTop = '280px';
		if (!document.getElementById('loading')) {
			refer.insertBefore(img, refer.firstChild);
		}
	} else if (opt == false) {
		var imgLoading = gE('loading');
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}
function btnOk() {
	gE('btnOk').onclick = function() 
	{
		removerDivs();
		return false;
	}
}
function removerDivs() {
	var bgBody = gE('bgBody');
	var boxCad = gE('boxCad');
	bgBody.parentNode.removeChild(bgBody);
	if (boxCad) {
		boxCad.parentNode.removeChild(boxCad);
	}
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// 
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 

}
