// JavaScript Document
// Declare vars
// const section
var _pollingExtPageId 	= 153382;
// on external page
var pollingExtTitleDIV 		= "pollingTitle"
var pollingExtAnswersDIV 	= "pollingAnswers"
var pollingExtResultsDIV 	= "pollingResults";
var pollingExtDebugDIV 		= "pollingDebug";
var pollingExtFormName		= "FrmExtPolling";
// div on main module
var _pollingMainDIV 		= "pollingMainDIV";
var _pollingContentDIV 		= "pollingContentDIV";
var _pollingTitleDIV 		= "pollingTitleDIV";
var _pollingAnswersDIV 		= "pollingAnswersDIV";
var _pollingResultsDIV 		= "pollingResultsDIV";
var _pollingLoadingDIV 		= "pollingLoadingDIV";


var loading = 	'<div id="loading" style="text-align:center;">';
	loading +=	'<img src="/site_images/wait22.gif" alt="A carregar..." border="0" style="margin-top:10px;" />';
	loading +=	'<div>A carregar...</div></div>';
 

//---------------------------------------------------------------------------
onerror=rHandleErr

//-----------------------------------------------------------------------------------
function rHandleErr () {
	pollHideLayer(_pollingMainDIV);
	return true;
}

function LoadPollingModule() {	
	try{
		pollGetExternalInfo();		
	}catch(e){
		//alert(e.message);
		pollHideLayer(_pollingMainDIV);
	}
}
function pollGetExternalInfo() {
	pollHideLayer(_pollingAnswersDIV);
	pollShowLayer(_pollingLoadingDIV);
	x = createXMLHTTP(checkXMLHTTP());
	if (x == null) {pollHideLayer(pollingMainDIV); }
	else
	{
		//alert(pollReturnPageUrl(_pollingExtPageId));
		//prompt("",pollReturnPageUrl(_pollingExtPageId));
		x.open("GET", pollReturnPageUrl(_pollingExtPageId) ,true);
		x.onreadystatechange=function() {
			if (x.readyState==4 && x.status == 200){
				s = x.responseText;
				//alert(s);
				pollRenderResult(s);
				pollHideLayer(_pollingLoadingDIV);
			}			
		}
		x.send(null)
	}
}

// render result
function pollRenderResult(s) {
	var title = pollGetDivContent(s, pollingExtTitleDIV).trim();
	var answers = pollGetDivContent(s, pollingExtAnswersDIV).trim();
	var results = pollGetDivContent(s, pollingExtResultsDIV).trim();

	//pollHideLayer(_pollingContentDIV);
	//pollShowLayer(_pollingLoadingDIV);

	if (title.length > 0) {
		//show answers
		pollSetContent(title, _pollingTitleDIV);
		if (answers.length > 0) {
			//show answers
			pollSetContent(answers, _pollingAnswersDIV)
		}
		else {
			// show results
			//pollHideLayer(_pollingAnswersDIV);
			pollSetContent(results, _pollingAnswersDIV)
		}	
	}
	else {
		pollHideLayer(_pollingMainDIV);	
	}
	pollShowLayer(_pollingAnswersDIV);
	//pollShowLayer(_pollingContentDIV);
}
// write contents to div
function pollSetContent(s, div) {
	document.getElementById(div).innerHTML = s;
}
// get div contents
function pollGetDivContent(s,di) {
	var a = '<deco_ajax id="'+di+'">';
	var b = '</deco_ajax>';
	u = s.substring(s.indexOf(a)+a.length);
	return (u.substring(0, u.indexOf(b)));
}
// render loading
function pollRenderLoading(content) {
	pollHideLayer(_pollingContentDIV);
	pollGetElement(_pollingLoadingDIV).innerHTML = content; 
	pollShowLayer(_pollingLoadingDIV);
}
//hide layer
function pollHideLayer(layerID) {
	document.getElementById(layerID).style.display = "none";
}
//Show layer
function pollShowLayer(layerID) {
	document.getElementById(layerID).style.display = "block";
}
// get element by id
function pollGetElement(elementID) {
	return document.getElementById(elementID);
}
// build check page url
function pollReturnPageUrl(pId) {
		
		var d = new Date();
		var x = d.getTime() + Math.round(1000*Math.random());
		//return "/default.aspx?"+pollGQS("FrmExtPolling")+"&time="+x;
		return "/default.aspx?show="+pId+"&"+pollGQS("FrmExtPolling");
}
//get querystring from form elements
function pollGQS(s) {
	return (formData2QueryString(document.forms[s]));
}
function getRadioButtonValue (radio)
{   
	var s_radiovalue = "";
	for (var i = 0; i < radio.length; i++)
	{   
		if (radio[i].checked) 
		{
			s_radiovalue = radio[i].value;
			break; 
		}
	}
	return s_radiovalue;
}

function pollValidate()
{
	var s_value = getRadioButtonValue(document.forms["FrmExtPolling"].pollValue);
	if (s_value=="")
	{
		alert("Escolha uma opção.");
		return;
	}
	document.forms["FrmExtPolling"].mode.value = 3;
	pollGetExternalInfo();
}