/*
Carmil 1
(c) 2006-2009 C. Ilan, Surveylyzer, Carmil.net
poll_basic_show.js
*/

if ( typeof(window.nssy) == 'undefined' ) { window.nssy= new Object (); };

function ShowPollPageHelper ()
{
this.showActionsForPublisher = function (idPoll)
{
var curuser= new CurrentUser();
if (curuser.isSignedIn())
{
if (curuser.isManager() || (curuser.getUid() == idPoll) )
{
showHtmlElement("idSyActionsBar0", true);
showHtmlElement("idSyInviteBulk", curuser.isManager());
}
}
};
};

function ShowPollPage ()
{
this.MAX_POST_RETRIES= 5;
this.TIMEOUT_RETRY_VOTE_MILLISEC= 5000;
this.objTuring= null;

this.mydebugger= new DebugHelper("ShowPollPage");
this.strtbl= new StringTable (getDocs() + "/polls/xml/strtbl_en.xml", "polls_basic_show", null);
this.specific= null;
this.audience= '';
this.idPoll= -1;
this.pollStatus= '';
this.docVote= null;
this.commentboard= '';
this.format= '';
this.captcha= 0;
this.lang= '';
this.isRLT= false;
this.refHost= "";
this.refCode= "";
this.styleCode= "";
this.pageHidden= "";
this.inviteeEmail= "";
this.invitationCode= "";
this.isVoteAlready= false;
this.callbackOnVote= function(syPoll, idPoll) {};
this.callbackOnResultsRequest= null ;
this.multiVote= '';
this.whoResults= 'none';
this.enableResultsLink= true;
this.retries= 0;

this.setBusy = function(isBusy)
{
document.body.style.cursor= (isBusy ? "wait" : "auto");
showHtmlElement("idSyPleaseWait", isBusy);
};

this.setLang= function(lang)
{
this.lang= lang;
this.isRLT= ((this.lang == "he") || (this.lang == "ar"));
 
this.strtbl.init(getDocs() + "/polls/xml/strtbl_en.xml", "polls_basic_show", this.lang);
};

this.enableRankButtons = function (que, maxtoptions)
{
var nodeSelect= document.getElementById( "idPollSelect_" + que );
if (nodeSelect == null)
{
return;
}
if (maxtoptions != nodeSelect.length)
{
throw 'assert';
}
var nodeMoveUp= document.getElementById( "idBtnMoveUp_" + que );
var nodeMoveDown= document.getElementById( "idBtnMoveDown_" + que );
var ix= nodeSelect.selectedIndex;
if ((ix < 0) || (ix >= maxtoptions) || (ix > nodeSelect.length))
{
nodeMoveUp.disabled= true;
nodeMoveDown.disabled= true;
return;
}
nodeMoveUp.disabled= (ix <= 0);
nodeMoveDown.disabled= (ix >= maxtoptions - 1);
};

this.moveRank = function (que, maxtoptions, step)
{
var nodeSelect= document.getElementById( "idPollSelect_" + que );
if (nodeSelect == null)
{
return;
}
if (maxtoptions != nodeSelect.length)
{
throw 'assert';
}
var ix= nodeSelect.selectedIndex;
var newpos= ix+step;
if ((newpos < 0) || (newpos >= maxtoptions) || (newpos >= nodeSelect.length))
{
return;
}

var ids1= nodeSelect.options[ix].id;
var ids2= nodeSelect.options[newpos].id;
swapSiblingNodes(ids1, ids2);

nodeSelect.selectedIndex= newpos;
this.enableRankButtons(que, maxtoptions);
};

this.changeRank = function (que, maxtoptions)
{
this.enableRankButtons(que, maxtoptions);
};

this.prepare = function (specific, audience, idPoll, pollStatus, commentboard, format, captcha, multiVote, whoResults, publisherNick, publisherId)
{
this.specific= specific;
this.audience= audience;
this.idPoll= idPoll;
this.pollStatus= pollStatus;
this.commentboard= commentboard;
this.format= format;
this.captcha= captcha;
this.multiVote= multiVote;
this.whoResults= whoResults;

if ((this.multiVote == null) || (this.multiVote == "") || (this.multiVote == "auto"))
{
this.multiVote= "warn";
}

var curuser= new CurrentUser();

{
var strRefHost= "";
if (!isNonEmptyString(strRefHost))
{
var splits= /^(.+[?])(.*[&])*(refhost[=])([A-Za-z0-9\.\-\%\_]+)([&].*)?$/.exec(document.location);
if ((splits != null) && (splits.length >= 4) && (splits[4] != null))
{
strRefHost= splits[4];
}
}
this.refHost= strRefHost;
 
}

 
if (this.pageHidden == "")
{
var strPageHidden= "";
if (!isNonEmptyString(strPageHidden))
{
strPageHidden= getQueryStringParam("hiddenpage", "");
}
this.pageHidden= (strPageHidden == null) ? "" : strPageHidden;
}

{
var strInviteeEmail= "";
if (!isNonEmptyString(strInviteeEmail))
{
strInviteeEmail= getQueryStringParam("invitee", "");
}
this.inviteeEmail= (strInviteeEmail == null) ? "" : strInviteeEmail;
}
{
var strInvitationCode= "";
if (!isNonEmptyString(strInvitationCode))
{
strInvitationCode= getQueryStringParam("invitation", "");
}
this.invitationCode= (strInvitationCode == null) ? "" : strInvitationCode;
}

{
var strSize= "";
if (!isNonEmptyString(strSize))
{
var splits= /^(.+[?])(.*[&])*(size[=])([0-9]+)([&].*)?$/.exec(document.location);
if ((splits != null) && (splits.length >= 4) && (splits[4] != null))
{
strSize= splits[4];
}
}
var nSize= parseInt(strSize, 10);
if ((isNaN(nSize)) || (nSize <= 0))
{
nSize= 100;
}
var nodeWhole= document.getElementById('idWholeQuestionnaire');
if (nodeWhole != null)
{
nodeWhole.style.fontSize= nSize + "%";
}
}

{
var strSkipAllIntro= "";
if (!isNonEmptyString(strSkipAllIntro))
{
var splits= /^(.+[?])(.*[&])*(skip[_]all[_]intro[=])([0-9]+)([&].*)?$/.exec(document.location);
if ((splits != null) && (splits.length >= 4) && (splits[4] != null))
{
strSkipAllIntro= splits[4];
}
}
var nSkipAllIntro= parseInt(strSkipAllIntro, 10);
if (nSkipAllIntro == 1)
{
showHtmlElement("idLogoHeader", false);
showHtmlElement("idBlogLinks", false);
showHtmlElement("idTitleMain", false);
showHtmlElement("idSummary", false);
}
}
 
this.isVoteAlready= (getMyCookie( "ap_recent_vote" ) == this.idPoll);

var nodeQuestionnaire= document.getElementById('idDivTheQuestionnaire');
if (this.audience == "visitors")
{
nodeQuestionnaire.style.display= "block";
}
else if (this.audience == "members")
{
if (curuser.isSignedIn())
{
nodeQuestionnaire.style.display= "block";
}
else
{
if (this.pollStatus == "construction")
{
 
nodeQuestionnaire.style.display= "block";
}
else
{
 
var msg='<div>' + this.strtbl.format("idShowPollOnlyRegistered", '<a href="javascript:void(0);" onclick="javascript: g_syPoll.signInFirst();">', '</a>') + '</div>';
writeXhtmlToElement("idDivQuestionnaireMsg", msg);
}
}
}

var msgVoteAlready= null;
if (this.pollStatus == "open")
{
if (curuser.isSignedIn() && isVoteCookie(this.idPoll, "user"))
{
msgVoteAlready= this.strtbl.get("idShowPollAlreadyVoteMember");
}
else if ( isVoteCookie(this.idPoll, "anonymous") )
{
if (this.multiVote == "warn")
{
msgVoteAlready= this.strtbl.get("idShowPollAlreadyVoteOnThisComp");
}
else if (this.multiVote == "prevent")
{
msgVoteAlready= this.strtbl.get("idShowPollAlreadyVoteOnThisCompPrevented");
}
}
else if (this.isVoteAlready)
{
msgVoteAlready= this.strtbl.get("idShowPollAlreadyVoteMember");
}
}

if (msgVoteAlready != null)
{
var nodeWarning= document.getElementById('idAlreadyVoteMsg');
removeAllChilds(nodeWarning);
nodeWarning.appendChild( document.createTextNode(msgVoteAlready) );
nodeWarning.style.direction= (this.isRLT ? "rtl" : "ltr");
}

 
var urlToQuaire= nssy.getScriptPath("show_poll");
urlToQuaire= appendQsParam(urlToQuaire, "publisher", publisherNick);
urlToQuaire= appendQsParam(urlToQuaire, "id_poll", this.idPoll);

var nodeDirectLink= document.getElementById("idDirectLink");
if (nodeDirectLink != null)
{
 
nodeDirectLink.value= urlToQuaire;
nodeDirectLink.readOnly= true;
}

var nodeInvite= document.getElementById("idInviteByMail");
if (nodeInvite != null)
{
var url= "mailto:yourfriend@mail.com?subject=" + 
escape( this.strtbl.format("idShowPollPleaseParticipateSubject", getServiceName() ) ) + 
"&body=" + escape( this.strtbl.get("idShowPollPleaseParticipateBody") + "\n\n" + urlToQuaire )
;
nodeInvite.href= url;
}

this.visibleLinkShowResults();

if (this.format == "standard")
{
g_syBoard=  new CommentsPage("idComments", this.idPoll, this.pollStatus, this.commentboard );
g_syBoard.go(this.lang);
}

{
var nodeIframe= document.getElementById('idSyAdsInPoll');
if (nodeIframe != null)
{
 
var urlAds= getCgibin() + "/ap_static.pl?resource=" + encodeURIComponent("/polls/ads1.html");
urlAds = appendQsParam( urlAds, "id_poll", this.idPoll);
urlAds = appendQsParam( urlAds, "lang_ui", this.lang);
if ((this.refCode != null) && (this.refCode != ""))
{
urlAds = appendQsParam( urlAds, "rcs", this.refCode);
}
if ((this.styleCode != null) && (this.styleCode != ""))
{
urlAds = appendQsParam( urlAds, "style", this.styleCode);
}
nodeIframe.src= urlAds;
}
}		
};

this.visibleLinkShowResults = function ()
{
var curuser= new CurrentUser();
var isSI= curuser.isSignedIn();
var isLinkShowResults=
(this.enableResultsLink) && 
(
(this.whoResults == "visitors") || 
((this.whoResults == "members") && isSI) ||
(
(this.whoResults == "participants") &&
(
(isVoteCookie(this.idPoll, "anonymous")) ||
(isSI && isVoteCookie(this.idPoll, "user")) 
)
)
);		
showHtmlElement("idSyDivLinkShowResults", isLinkShowResults);
}

this.showPollResults = function ()
{
 
if (this.callbackOnResultsRequest)
{
this.callbackOnResultsRequest(this, this.idPoll);
return;
}

var isNewWindow= (this.format == "bare-xhtml");
 
new PollsActions(this.lang).showPollResults(this.idPoll, isNewWindow);
};

this.validateCategoriesList = function (que, ids, minChoose, maxChoose, gui) 
{
if ((gui == "") || (gui == "auto"))
gui= "buttons";
var countSelected= 0;
var i= 0;
for ( ; i < ids.length; ++i ) 
{	
var nodeOption= document.getElementById( ids[i] ); 
if (nodeOption.tagName == "INPUT") 
{
if (nodeOption.checked)
{
++countSelected;
}
}
else if (nodeOption.tagName == "OPTION")
{
if (nodeOption.selected)
{
++countSelected;
}
}
}
var errmsg= "";
if ((countSelected == 0) && (gui == "dropdown"))
{
errmsg= this.strtbl.format("idShowPollPleaseChooseDropDown",  que);
}
else if (minChoose == maxChoose)
{
if (countSelected != minChoose)
{
if (minChoose > 1)
{
errmsg= this.strtbl.format("idShowPollPleaseSelectExactlyMany", minChoose, que);
}
else
{
errmsg= this.strtbl.format("idShowPollPleaseSelectExactlyOne", que);
}
}
}
else if (countSelected < minChoose)
{
if (minChoose > 1)
{
errmsg= this.strtbl.format("idShowPollPleaseSelectAtLeastMany", minChoose, que);
}
else
{
errmsg= this.strtbl.format("idShowPollPleaseSelectAtLeastOne", que);
}
}
else if (countSelected > maxChoose)
{
if (maxChoose > 1)
{
errmsg= this.strtbl.format("idShowPollPleaseSelectAtMostMany", maxChoose, que);
}
else
{
errmsg= this.strtbl.format("idShowPollPleaseSelectAtMostOne", que);
}
}

if (errmsg != "")
{
 
var nodeFocus=  document.getElementById( ids[0] );
if ((gui == "list") || (gui == "dropdown"))
{
 
}
else if (gui == "buttons")
{
 
 
}
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeFocus.focus();
window.alert(errmsg);
return false;
}

return true;
};

this.validateScaled = function(que, scalesize, scaletype, isIncludeMissing, countoptions, isAllowUnanswered)
{
 
var op;
for (op= 1; op <= countoptions; ++op)
{
var count= 0;
var i;
for (i=1; i <= scalesize; ++i)
{
var idInput= "idScaleOp_" + que + "_" + op + "_" + i;
var nodeInput= document.getElementById(idInput);
if (nodeInput.checked)
{
++count;
}
}
if (isIncludeMissing)
{
var idInput= "idScaleOp_" + que + "_" + op + "_missing";
var nodeInput= document.getElementById(idInput);
if (nodeInput.checked)
{
++count;
}
}
if ((!isAllowUnanswered) && (count != 1))
{
var errmsg= this.strtbl.format("idShowPollPleaseScale", que);
nssy.scrollToAnchor("idSyAnchorQue" + que);
alert(errmsg);
return false;
}
}
return true;
};

this.validateInterval = function (que, id, minrange, maxrange, isIntegerOnly, isBlankAllowed)
{
var nodeIntervalue= document.getElementById(id);
if (nodeIntervalue.value == "")
{
if (isBlankAllowed == 0)
{
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeIntervalue.focus();
var errmsg= this.strtbl.format("idShowPollPleaseNumberInRange", minrange, maxrange, que);
alert(errmsg);
return false;
}
return true;
}
if ( isNaN(nodeIntervalue.value) )
{
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeIntervalue.focus();
var errmsg= this.strtbl.format("idShowPollPleaseNumberInRange", minrange, maxrange, que);
alert(errmsg);
return false;
}
var intervalue= Number(nodeIntervalue.value);
if ((intervalue < minrange) || (intervalue > maxrange))
{
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeIntervalue.focus();
var errmsg= this.strtbl.format("idShowPollPleaseNumberInRange", minrange, maxrange, que);
alert(errmsg);
return false;
}
if ((isIntegerOnly != 0) && ( Math.floor(intervalue) != intervalue ))
{
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeIntervalue.focus();
var errmsg= this.strtbl.format("idShowPollPleaseIntegerInRange", minrange, maxrange, que);
alert(errmsg);
return false;
}

return true;
};

this.validateFreeText = function (que, id, isBlankAllowed, isMultiLine, maxLength)
{
var nodeFreeText= document.getElementById(id);
if (nodeFreeText.value == "")
{
if (isBlankAllowed == 0)
{
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeFreeText.focus();
var errmsg= this.strtbl.format("idShowPollPleaseFreeText", que);
alert(errmsg);
return false;
}
}
else
{
if ((maxLength > 0) && (nodeFreeText.value.length > maxLength))
{
nssy.scrollToAnchor("idSyAnchorQue" + que);
nodeFreeText.focus();
var errmsg= this.strtbl.format("idShowPollFreeTextTooLong", que, maxLength);
alert(errmsg);
return false;
}
}

return true;
}

 

this.buildXmlCategories = function (nodeParentAns, ids, listIdsMoreDetails)
{
var doc= nodeParentAns.ownerDocument;
for ( var iOp= 0; iOp < ids.length ; ++iOp )
{
var nodeOption= document.getElementById(ids[iOp]);
if (
((nodeOption.tagName == "INPUT") && (nodeOption.checked)) || 
((nodeOption.tagName == "OPTION") && (nodeOption.selected)) 
)
{
var op= doc.createElement("op");
op.setAttribute("idop", iOp+1);
if ((listIdsMoreDetails[iOp] != null) && (listIdsMoreDetails[iOp] != ""))
{
nodeMoreDetails= document.getElementById(listIdsMoreDetails[iOp]);

var optext= doc.createElement("text");
optext.appendChild( doc.createTextNode(nodeMoreDetails.value) );
op.appendChild(optext);
}
nodeParentAns.appendChild(op);
}
}
};

this.buildXmlRank = function (nodeParentAns, que, maxoptions)
{
var nodeSelect= document.getElementById( "idPollSelect_" + que );
if (nodeSelect == null)
{
return;
}
if (maxoptions != nodeSelect.length)
{
throw 'assert';
}
var doc= nodeParentAns.ownerDocument;
var iOp;
for ( iOp= 0; iOp < nodeSelect.length; ++iOp )
{
var op= doc.createElement("op");
op.setAttribute("idop", nodeSelect.options[iOp].value);
op.setAttribute("pos", iOp+1);
nodeParentAns.appendChild(op);
}
};


this.getScaleItemValue = function (scalesize, scaletype, ix)
{
var reply= 0;
if (scaletype == "twoways")
{
var halfScale= Math.floor(scalesize/2);
if (ix <= halfScale)
{
reply= ix - 1 - halfScale;
}
else
{
reply= ix + halfScale - scalesize;
}
}
else
{
reply= ix;
}
this.mydebugger.trace("getScaleItemValue: " + " ScaleSize:" + scalesize + " ScaleType:" + scaletype + " IX:" + ix + " Reply:" + reply);
return reply;
};

this.buildXmlScaled = function (nodeParentAns, que, scalesize, scaletype, isIncludeMissing, countoptions, isAllowUnanswered)
{
var doc= nodeParentAns.ownerDocument;
this.mydebugger.trace("buildXmlScaled: Que:" + que + " ScaleSize:" + scalesize + " ScaleType:" + scaletype + " isIncludeMissing:" + isIncludeMissing + " CountOptions:" + countoptions); 
var op;
for (op= 1; op <= countoptions; ++op)
{
var isRated= false;
var i;
for (i=1; i <= scalesize; ++i)
{
var idInput= "idScaleOp_" + que + "_" + op + "_" + i;
var nodeInput= document.getElementById(idInput);
if (nodeInput.checked)
{
isRated= true;
var nodeOp= doc.createElement("op");
nodeOp.setAttribute("idop", op);
var ansval= this.getScaleItemValue(scalesize, scaletype, i);
nodeOp.setAttribute("val", ansval);
nodeParentAns.appendChild(nodeOp);				
}
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
if (!isRated)
{
var nodeOp= doc.createElement("op");
nodeOp.setAttribute("idop", op);
nodeOp.setAttribute("missing", "missing");
nodeParentAns.appendChild(nodeOp);
}
}
return true;
};

this.buildXmlInterval = function (nodeParentAns, id)
{
var doc= nodeParentAns.ownerDocument;
var nodePollIntervalue= document.getElementById(id);
var intrval= doc.createElement("val");
intrval.appendChild( doc.createTextNode(nodePollIntervalue.value) );
nodeParentAns.appendChild(intrval);
};

this.buildXmlFreeText = function (nodeParentAns, id)
{
var doc= nodeParentAns.ownerDocument;
var nodePollFreeTextVal= document.getElementById(id);
var val= doc.createElement("text");
val.appendChild( doc.createTextNode(nodePollFreeTextVal.value) );
nodeParentAns.appendChild(val);
};

this.buildXmlStdAge = function (nodeParent)
{
var nodeReply= document.getElementById('idSelectStdQueAge');

var doc= nodeParent.ownerDocument;
var nodeVote= doc.createElement('std-age');
nodeVote.appendChild( doc.createTextNode(nodeReply.value) );
nodeParent.appendChild(nodeVote);
};

this.buildXmlStdGender = function (nodeParent)
{
var doc= nodeParent.ownerDocument;
var nodeVote= doc.createElement('std-gender');
if (document.getElementById('idStdQueFemale').checked)
{
nodeVote.appendChild( doc.createTextNode('female') );
}
else if (document.getElementById('idStdQueMale').checked)
{
nodeVote.appendChild( doc.createTextNode('male') );
}
nodeParent.appendChild(nodeVote);
};

 
this.restyleTableScaled = function (idTbl)
{
var nodeTbl= document.getElementById(idTbl);
var nodeTblBody= nodeTbl.getElementsByTagName("TBODY").item(0);
var trs= nodeTblBody.getElementsByTagName("TR");
var i;
for ( i= 0; i < trs.length; ++i)
{
var tr= trs.item(i);
tr.className= (i % 2 == 0) ? "clsTrEven" : "clsTrOdd" ;
}
this.mydebugger.trace("restyleTableScaled:" + idTbl + " TRs:" + trs.length);
};

this.randomizeNominalCategories = function (ids, listMissings, strCategoriesOrder)
{
var categories= ids.length;
if ((strCategoriesOrder == "random") || (strCategoriesOrder == "semirandom"))
{
var countSwaps= 0;
var swaps= 0;
var maxswaps= 90+(Math.round(Math.random()*1000) % 17);

for ( ; swaps < maxswaps ; ++swaps )
{
var ix1= (Math.round(Math.random()*1000) % categories);
var ix2= (Math.round(Math.random()*1000) % categories);

var isSwap= true;
if (
(ix1 == ix2) ||
( (strCategoriesOrder == "semirandom") && ((listMissings[ix1] != 0) || (listMissings[ix2] != 0)) )
)
{
isSwap= false;
}

if (isSwap)
{
if (swapSiblingNodes(ids[ix1], ids[ix2]))
{
++countSwaps;
}
}
}
}
};

 
this.presetMemberAge = function ()
{
var isSingedIn= isSignedIn();
var nodeAge= document.getElementById('idSelectStdQueAge');
if (nodeAge == null)
{
return;
}
if (isSingedIn)
{
var now= new Date();
var nowYear= now.getFullYear();
var birthYear= getMyCookie( "ap_birthyear" );
var age= (nowYear - birthYear);
 
if (age > 3)
{
if (age > 90)
age= 90;
nodeAge.value= age;
 
 
 
}
}
else
{
nodeAge.disabled= false;

var age= getMyCookie( "ap_anon_age" );
if (age > 3)
{
if (age > 90)
age= 90;
nodeAge.value= age;
}
}
};

this.presetMemberGender = function ()
{
var isSingedIn= isSignedIn();
var nodeMale= document.getElementById('idStdQueMale');
var nodeFemale= document.getElementById('idStdQueFemale');
if ((nodeMale == null) || (nodeFemale == null))
{
return;
}
if (isSingedIn)
{
var gender= getMyCookie( "ap_gender" );
if (gender == "male")
{
nodeMale.checked= true;
}
else if (gender == "female")
{
nodeFemale.checked= true;
}
 
 
 
 
}
else
{
nodeMale.disabled= false;
nodeFemale.disabled= false;

var gender= getMyCookie( "ap_anon_gender" );
if (gender == "m")
{
nodeMale.checked= true;
}
else if (gender == "f")
{
nodeFemale.checked= true;			
}
}
};

 

this.PrePollSignInCallback = function (parent)
{
this.parent= parent;
this.onSignedIn = function () 
{
var nodeQuestionnaire= document.getElementById('idDivTheQuestionnaire');
nodeQuestionnaire.style.display= "block";

this.parent.specific.presetMember();
removeAllChilds(document.getElementById('idDivQuestionnaireMsg'));
removeAllChilds(document.getElementById('idSignIn'));
};
};

 
this.signInFirst = function ()
{
doDynamicSignIn('idSignIn', new this.PrePollSignInCallback(this), '');
};

 
this.onSubmitPoll = function ()
{
this.mydebugger.trace("on-submit");

if ( !this.specific.validateAllQue() )
{
return;
}

this.mydebugger.trace("validation successful");
this.docVote= this.specific.getVoteXmlDocument();

this.mydebugger.trace(convertXmlToString(this.docVote));
this.submitPoll();
};

this.getQsDelimiter = function (url)
{
var delim= "&";
var pos= url.indexOf("?", 0);
if (pos < 0)
{
delim= "?";
}
return delim;
};

this.setHiddenPage = function (page)
{
this.pageHidden = page;
};

this.putHiddenPage = function ()
{
if ((this.pageHidden != null) && (this.pageHidden != ""))
{
var url= this.pageHidden;
url= url + this.getQsDelimiter(url);
url= url + "surveylyzer_qid=" + encodeURIComponent(this.idPoll);

 

var nodeFrame= document.createElement('IFRAME');
nodeFrame.height="10px";
nodeFrame.width="10px";
nodeFrame.src= url;
nodeFrame.frameBorder= 0;
nodeFrame.scrolling= "no";
nodeFrame.name= "hiddenpage";

document.body.appendChild(nodeFrame);
}
};

this.showPostScript = function ()
{
showHtmlElement("idDivTheQuestionnaire", false);
showHtmlElement("idSummary", false);
showHtmlElement("idSyPostScript", true);
 

nssy.scrollToAnchor("idTopPoll");
};

this.contineAfterVote0 = function ()
{	
setMyCookie( "ap_recent_poll", this.idPoll );
window.location.assign( getDocs() + "/polls/after_vote.html" );
};

this.submitPoll = function ()
{
if (this.pollStatus == "construction")
{
window.alert( this.strtbl.get("idShowPollPreviewOnly") );
this.showPostScript();
}
else if (this.pollStatus == "pending_open")
{
window.alert( this.strtbl.get("idShowPollNotYetOpen") );
}
else if (this.pollStatus == "open")
{
this.trySendVote();
}
else if ((this.pollStatus == "closed") || (this.pollStatus == "concluded") || (this.pollStatus == "archived"))
{
window.alert( this.strtbl.get("idShowPollAlreadyClosed") );
}
else
{
alert("Error (#F5B2) " + this.pollStatus);
}
};


this.getVoteQueryString = function ()
{
var turing_enc= (this.objTuring == null) ? 0 : this.objTuring.getTuringEnc();
var turing_inp= (this.objTuring == null) ? 0 : this.objTuring.getTuringUser();

var curuser= new CurrentUser();

var querystr= curuser.getQS() + 
"&id_poll=" + encodeURIComponent(this.idPoll) + 
"&turing_enc=" + encodeURIComponent(turing_enc) + 
"&turing=" + encodeURIComponent(turing_inp) + 
"&retries=" + encodeURIComponent(this.retries)
;

if ((this.inviteeEmail != null) && (this.inviteeEmail != ""))
{
querystr= querystr + "&invitee=" + encodeURIComponent(this.inviteeEmail) +
"&invitation=" + encodeURIComponent(this.invitationCode);
}

return querystr;
};

this.setCallbackOnVote = function (fnc)
{
this.callbackOnVote= fnc;
};

this.setCallbackOnResultsRequest = function (fnc)
{
this.callbackOnResultsRequest= fnc;
};

this.setEnableResultsLink = function (flag)
{
this.enableResultsLink= flag;
this.visibleLinkShowResults();
};

this.setReferrerCode = function (refcode)
{
this.refCode= refcode;
};

this.setStyle = function (style)
{
this.styleCode = style;
};

this.openPagePoweredBy = function ()
{
var url= getDocs() + "/polls/home.html";
if ((this.refCode != null) && (this.refCode != ""))
{
url = url + "?rcs=" + encodeURIComponent(this.refCode);
}
window.open( url, "_blank" );
};

this.onVoteOk = function (parent)
{
setVoteCookie(this.idPoll, "anonymous");
var curuser= new CurrentUser();
var isSingedIn= curuser.isSignedIn();
if (isSingedIn)
{
setVoteCookie(this.idPoll, "user");
}

this.putHiddenPage();
this.showPostScript();
this.visibleLinkShowResults();

if (this.callbackOnVote)
{
this.callbackOnVote(this, this.idPoll);
}
};

this.CallbackVote = function (parent)
{
this.parent= parent;

this.onXmlLoaded = function (xmlreq)
{
this.parent.setBusy(false);
enableHtmlElement("idBtnSubmit", true);
enableHtmlElement("idBtnSubmit2", true);
 
 

try
{
var doc = xmlreq.req.responseXML.documentElement;
var status = getValidTextOfFirstXmlElement( doc.getElementsByTagName('status') );
var debugerr = getValidTextOfFirstXmlElement( doc.getElementsByTagName('debug-err') );
if (status == "OK")
{
this.parent.onVoteOk();					
}
else if ((status == "USER_NOT_CONFIRMED") || (status == "FILTER_UNKNOWN_USER") || (status == "ANONYMOUS_NOT_ALLOWED"))
{
alert( this.parent.strtbl.get("idShowPollMustSignIn") );
doDynamicSignIn("idSignIn", new this.parent.CallbackSignInVote (this.parent), '');
}
else if (status == "TURING")
{
var turing_enc = getValidTextOfFirstXmlElement( doc.getElementsByTagName('turing-enc') );
if (this.parent.objTuring == null)
{
this.parent.objTuring= new TuringObj("idDivTuring");
this.objTuring.setLang(this.lang);
this.parent.objTuring.doDynamicTuring();
this.parent.objTuring.focus();
}
else
{
 
this.parent.objTuring.setTuringEnc(turing_enc);
}
alert( this.parent.strtbl.get("idShowPollTuringExpired") );
}
else if (status == "POLL_NOT_OPEN")
{
alert( this.parent.strtbl.get("idShowPollClosed") );
}
else if (status == "ALREADY_VOTED")
{
alert( this.parent.strtbl.get("idShowPollAlreadyVoted") );
}
else
{
this.parent.mydebugger.warn("Status: " + status + " Err: " + debugerr );
if (!this.parent.retryVote("Status: " + status + " Err: " + debugerr))
{
if (confirm( this.parent.strtbl.get("idErrGeneralOperationFailed") ))
{
alert("Status: " + status + "\nError: " + debugerr );
}
}
}
}
catch (exc)
{
this.parent.mydebugger.warn(convertErrObjToStr(exc));
if (!this.parent.retryVote(convertErrObjToStr(exc)))
{
if (confirm( this.parent.strtbl.get("idErrGeneralOperationFailed") ))
{
alert(convertErrObjToStr(exc));
}
}
}
};

this.onError = function (xmlreq, err)
{
this.parent.mydebugger.warn(convertErrObjToStr(err));
this.parent.setBusy(false);
enableHtmlElement("idBtnSubmit", true);
enableHtmlElement("idBtnSubmit2", true);
 
 

if (!this.parent.retryVote(convertErrObjToStr(err)))
{
if (confirm( this.parent.strtbl.get("idErrGeneralOperationFailed") ))
{
alert(convertErrObjToStr(err));
}
}
};
};

this.retryVote = function (reason)
{
this.mydebugger.trace("retryVote: " + this.retries + " " + reason);
if (this.retries < this.MAX_POST_RETRIES)
{
this.setBusy(true);
enableHtmlElement("idBtnSubmit", false);
enableHtmlElement("idBtnSubmit2", false);
 
 

++ this.retries;
var that= this;
setTimeout( function() { that.onRetryTimeout(); }, this.TIMEOUT_RETRY_VOTE_MILLISEC);
return true;
}
return false;
};

this.onRetryTimeout = function  ()
{
this.setBusy(false);
enableHtmlElement("idBtnSubmit", true);
enableHtmlElement("idBtnSubmit2", true);
 
 

this.doSendVote2();
};

this.doSendVote2 = function  ()
{
this.setBusy(true);
enableHtmlElement("idBtnSubmit", false);
enableHtmlElement("idBtnSubmit2", false);
 
 

 
var url= getCgibin() + "/ap_post_vote.pl?" + this.getVoteQueryString();
showHtmlElement("idPollSubmit", true);
var requestVote= new nssy.MyXmlRequest();
requestVote.loadAsync( url, new this.CallbackVote(this), null, this.docVote );	
};

this.doSendVote = function  ()
{
var isSingedIn= isSignedIn();
if (isSingedIn)
{
var isVotedUser= isVoteCookie(this.idPoll, "user");
if (isVotedUser)
{
alert( this.strtbl.get("idShowPollAlreadyVoted") );
return;
}
}

var isVotedAnonymous= isVoteCookie(this.idPoll, "anonymous");
if (isVotedAnonymous)
{
if (this.multiVote == "warn")
{
var isOk= confirm( this.strtbl.get("idShowPollConfirmRevoteOnComputer") );
if (!isOk)
{
return;
}
}
else if (this.multiVote == "prevent")
{
alert( this.strtbl.get("idShowPollAlreadyVoteOnThisCompPrevented") );
return;
}
}

{
var nodeAge= document.getElementById('idSelectStdQueAge');
if (nodeAge != null)
{
setMyCookie( "ap_anon_age", nodeAge.value );
}
var nodeMale= document.getElementById('idStdQueMale');
if ((nodeMale != null) && nodeMale.checked)
{
setMyCookie( "ap_anon_gender", "m" );
}
var nodeFemale= document.getElementById('idStdQueFemale');
if ((nodeFemale != null) && nodeFemale.checked)
{
setMyCookie( "ap_anon_gender", "f" );
}			
}
this.doSendVote2();
};

this.CallbackSignInVote = function (parent)
{
this.parent= parent;

this.onSignedIn = function () 
{
var nodeSignIn= document.getElementById("idSignIn");
removeAllChilds(nodeSignIn);
showHtmlElement("idPollSubmit", true);
this.parent.trySendVote();
};
};

this.trySendVote = function ()
{
var curuser= new CurrentUser();

if (this.audience == "visitors")
{
 
}
else if (this.audience == "members")
{
if (! curuser.isSignedIn())
{
showHtmlElement("idPollSubmit", false);
alert( this.strtbl.get("idShowPollMustSignIn") );
doDynamicSignIn("idSignIn", new this.CallbackSignInVote (this), '');
return;
}
}
else
{
this.mydebugger.warn("Audience: " + this.audience );
alert("Error (#2BE1) " + this.audience);
return;
}

var isCaptchaRequired= false;
if (this.captcha > 0)
{
isCaptchaRequired= true;
}
if (this.captcha == 0)
{
if (! curuser.isSignedIn())
{
isCaptchaRequired= true;
}
}

this.mydebugger.trace("CaptchaRequired: " + isCaptchaRequired + " (" + this.captcha + ")" );
if (isCaptchaRequired)
{
if (this.objTuring == null)
{
this.objTuring= new TuringObj("idDivTuring");
this.objTuring.setLang(this.lang);
this.objTuring.doDynamicTuring();
this.objTuring.focus();
return;
}
else if (this.objTuring.getTuringUser() == 0)
{
this.objTuring.focus();
alert( this.strtbl.get("idShowPollTuringRequired") );
return;
}
else
{
 
}
}

this.doSendVote ();
};
}

 

function getExportedFunctions ()
{
return [ 'ShowPollPage' ];
}
 

if (window.nssy && window.nssy.onLoadScript) { window.nssy.onLoadScript('poll_basic_show'); }

 
