// copyright (c) 2006 Majigs, LLC

var built=false;var curAd=null;var adcolor=1;var innerType='poll';var curQuestion=null;var voted=false;var questions=[];function loginHook(){load();}
function logoutHook(){clearQuestion();curQuestion=null;g('mypollslist')._pm_loaded=0;g('myvoteslist')._pm_loaded=0;questions=[];}
loading=0;function load(){if(loading){return;}
loading=1;var uri='getpolls.php';if(curAd==null){uri+='?a=1';}
(new mjXR(uri,processGet,"UGET")).send();var td=g('questiontd');td.vAlign='middle';clearQuestion();showPollMessage("Loading...");}
function showNoPollMessage(){var td=g('questiontd');td.vAlign='middle';clearQuestion();h(g('resultslink'));vh(g('nextpoll'));showPollMessage("You've answered or skipped all of the poll questions.\n \nCheck back soon: polls are added daily.\n \nOr you can create your own poll.  Just click 'My Polls', then 'Create a poll'.");g('pollmessage').style.paddingTop='10px';}
function showPollMessage(str){var msg=g('pollmessage');cc(msg);aft(msg,str);g('pollmessage').style.paddingTop='25px';s(msg);}
function clearQuestion(){if(g('pollasker')){d(g('pollasker'));}
if(g('pollquestion')){d(g('pollquestion'));}
if(g('answers')){d(g('answers'));}}
function buildPollQuestion(){clearQuestion();curQuestion=shft(questions);var innerTxt='';var msg=g('pollmessage');h(msg);var questiontd=g('questiontd');questiontd.vAlign="top";var h2j_p0=c('P');h2j_p0.id='pollasker';cls(h2j_p0,'pollasker');at(h2j_p0,curQuestion[1]+' asks:');questiontd.insertBefore(h2j_p0,msg);var h2j_p1=c('P');h2j_p1.id='pollquestion';cls(h2j_p1,'pollquestion');at(h2j_p1,curQuestion[2]);innerTxt=curQuestion[2];ac(questiontd,h2j_p1);questiontd.insertBefore(h2j_p1,msg);var h2j_table1=table(3,0);h2j_table1.id='answers';cls(h2j_table1,'pollanswers');h2j_table1.border='0';var h2j_tbod1=tbod(h2j_table1);var i;for(i=3;i<curQuestion.length;i++){var h2j_tr1=c('TR');var h2j_td1=c('TD');var h2j_input1=c('INPUT');h2j_input1.type='radio';h2j_input1.id='ans'+(i-2);h2j_input1.onclick=pollVote;cls(h2j_input1,'radiosm');ac(h2j_td1,h2j_input1);ac(h2j_tr1,h2j_td1);var h2j_td2=c('TD');var h2j_span1=c('SPAN');cls(h2j_span1,'answer');at(h2j_span1,curQuestion[i]);h2j_span1.id='anstext'+(i-2);ac(h2j_td2,h2j_span1);ac(h2j_tr1,h2j_td2);ac(h2j_tbod1,h2j_tr1);innerTxt+=' '+curQuestion[i];}
ac(questiontd,h2j_table1);si(g('resultslink'));si(g('nextpoll'));g('nextpoll').style.fontWeight='normal';voted=false;updateInner(innerTxt);}
function processGet(xmlHttp,mjXR){loading=0;var polls=eval(xmlHttp.responseText);if(polls[0]=='LOGIN'){showLogin();preloginRequest=mjXR;loading=1;return;}
while(true){var x=pop(polls);if(x&&x[0]==-2){showAd(x[1]);}else{push(polls,x);break;}}
var i;for(i=1;i<polls.length;i++){push(questions,polls[i]);}
if(curQuestion==null){if(polls.length>1){buildPollQuestion();}else{showNoPollMessage();}}}
function nextPoll(){if(!voted){var uri='vote.php?q='+curQuestion[0]+'&v=0';(new mjXR(uri,doNothing,"UGET")).send();}
curQuestion=null;if(questions.length){buildPollQuestion();}else{load();}}
function doNothing(){}
function pollVote(){var vote=this.id.substr(3);var uri='vote.php?q='+curQuestion[0]+'&v='+vote;(new mjXR(uri,processVote,"UGET")).send();showPollVoteMessage('Sending your vote...');}
function pollResults(){var uri='vote.php?q='+curQuestion[0]+'&v=0';(new mjXR(uri,processVote,"UGET")).send();showPollVoteMessage('Getting poll results...');}
function showPollVoteMessage(str){h(g('answers'));h(g('resultslink'));vh(g('nextpoll'));showPollMessage('Getting poll results...');}
function processVote(xmlHttp,mjXR){var results=eval(xmlHttp.responseText);if(results[0]=='LOGIN'){showLogin();preloginRequest=mjXR;return;}
var resultArr=[];var countArr=[];var i;for(i=1;i<results.length;i++){resultArr[results[i][0]]=results[i][1];countArr[results[i][0]]=results[i][2];}
for(i=1;i<curQuestion.length-2;i++){var count=countArr[i]?countArr[i]:0;var b=c('B');at(b,resultArr[i]?resultArr[i]:'0%');re(g('ans'+i),b);at(g('anstext'+i),' ('+(count)+' vote'+(count==1?'':'s')+')');}
h(g('pollmessage'));s(g('answers'));vs(g('nextpoll'));g('nextpoll').style.fontWeight='bold';voted=true;}
function showMyPolls(){var iE=g('mypolls-i');iE.style.overflow='visible';iE.style.height='20px';showADiv('mypolls',fixMyPolls);}
function fixMyPolls(elem){var iE=g(elem.id+'-i');iE.style.overflow='hidden';var pos0=getPos(g('mypollsP'));var pos1=getPos(iE);iE.style.height=(elem.offsetHeight+pos0[1]-pos1[1]-16)+'px';iE.style.overflow='auto';iE.scrollTop=0;}
function showMyCreatedPolls(){var div=g('mypollslist');if(div._pm_loaded){f(div);return;}
myCreatedPollsLoad();}
function myCreatedPollsLoad(page){var div=g('mypollslist');cc(div);at(div,'Loading...');s(div);var uri='mypolls.php';mjXR._pm_page=(page?page:1);(new mjXR(uri,myPollsGet,"UGET")).send();}
function showMyVotes(){var div=g('myvoteslist');if(div._pm_loaded){f(div);return;}
myVotesLoad();}
function myVotesLoad(page){var div=g('myvoteslist');cc(div);at(div,'Loading...');s(div);var uri='myvotes.php';mjXR._pm_page=(page?page:1);(new mjXR(uri,myPollsGet,"UGET")).send();}
function myPollsGet(xmlHttp,mjXR){try{var results=eval(xmlHttp.responseText);}catch(e){alert(xmlHttp.responseText);alert(e.message);return;}
if(results[0]=='LOGIN'){showLogin();preloginRequest=mjXR;return;}
var div;if(results[0]=='MYPOLLS'){div=g('mypollslist');}else{div=g('myvoteslist');}
cc(div);var count=results[1];var i;for(i=2;i<results.length;i++){var poll=results[i];var tb=table(2,0);cls(tb,'mypoll');var tbody=tbod(tb);var tr=c('TR');var td=c('TD');td.colSpan=2;cls(td,'mypoll-question');at(td,poll[1]);ac(tr,td);ac(tbody,tr);if(poll[2]==-1){tr=c('TR');td=c('TD');cls(td,'mypoll-response');td.colSpan=2;var b=c('STRONG');at(b,'Rejected: ');ac(td,b);at(td,poll[3]);ac(tr,td);ac(tbody,tr);}else if(poll[2]==0){tr=c('TR');td=c('TD');td.colSpan=2;cls(td,'mypoll-response');at(td,'Poll not yet posted.');ac(tr,td);ac(tbody,tr);}else{var j;for(j=3;j<poll.length;j++){tr=c('TR');if(poll[j][3]){tr.style.textDecoration='underline';}
td=c('TD');cls(td,'mypoll-percent');at(td,poll[j][2]+' ('+poll[j][1]+')');ac(tr,td);td=c('TD');cls(td,'mypoll-answer');at(td,poll[j][0]);ac(tr,td);ac(tbody,tr);}}
ac(div,tb);}
div._pm_loaded=true;}
function showCreate(){var iE=g('create-poll-i');iE.style.overflow='visible';iE.style.height='20px';g('createQuestion').value='';g('createAnswer1').value='';g('createAnswer2').value='';g('createAnswer3').value='';g('createAnswer4').value='';s(g('createButtons'));vh(g('createButtons'));h(g('createDone'));showADiv('create-poll',fixCreate);}
function fixCreate(elem){s(g('create-poll-i'));var iE=g(elem.id+'-i');iE.style.overflow='hidden';var pos0=getPos(g('createP'));var pos1=getPos(iE);iE.style.height=(elem.offsetHeight+pos0[1]-pos1[1]-16-i(g('createButtons').offsetHeight))+'px';iE.style.overflow='auto';vs(g('createButtons'));g('createQuestion').focus();iE.scrollTop=0;}
function createPoll(){var iE=g('create-poll-i');var aE=g('createButtons');var lo=g('createLoading');sz(lo,iE.offsetWidth,iE.offsetHeight+aE.offsetHeight);lo.style.paddingTop=i(i(iE.offsetHeight)/2);h(iE);h(aE);h(g('createClose'));s(lo);var formStr='';formStr='q='+urlencode(g('createQuestion').value);formStr+='&a1='+urlencode(g('createAnswer1').value);formStr+='&a2='+urlencode(g('createAnswer2').value);formStr+='&a3='+urlencode(g('createAnswer3').value);formStr+='&a4='+urlencode(g('createAnswer4').value);(new mjXR('createpoll.php',createResponse,'POST',formStr)).send();}
function createResponse(x,mjXR){h(g('createLoading'));var ret=eval(x.responseText);if(ret[0]=='LOGIN'){showLogin();preloginRequest=mjXR;return;}
if(ret[0]=='OK'){s(g('createDone'));s(g('createClose'));}else{s(g('create-poll-i'));s(g('createButtons'));s(g('createClose'));alert(ret[2]);sl(g('create'+ret[1]));}}