function checkSurveyData() 
{
   
    var message = __context.GetServiceClientProxy('AtriplaUpgradeServices').ShowHideGet().value;
    var dvS = document.getElementById("dvSurvey");
    var dvR = document.getElementById("dvSurveyResult");
	if (dvS == null && dvR == null) 
	{ 	
	    return false; 
	}
    else
    {
        if(message!=null && message != 0)
        {
            //Result Table
            getSurveyResult();
            document.getElementById("dvSurveyResult").style.display = "block";//result
            document.getElementById("sciPoll").style.display = "block";
            document.getElementById("dvSurvey").style.display = "none";//question
        }
        else
        {
            //Question Table
            document.getElementById("dvSurveyResult").style.display = "none";//result
            document.getElementById("sciPoll").style.display = "none";
            document.getElementById("dvSurvey").style.display = "block";//question
            loadSurvey();
        }
    }
}

function loadSurvey()
{
    var arrAllQs = new Array();
    var arrNotYetShownQs = new Array();
    var SurveyList = __context.GetSurveyServicesClientProxy().GetPageSurveyData("index", 170);
    var SurveyData =  SurveyList.value;
    var ShownQKeys = __context.GetServiceClientProxy('AtriplaUpgradeServices').GetShownQ().value;
    
    for(var a=0;a < SurveyData.length; a++)
    {
        arrAllQs[a] = SurveyData[a].QuestKey;
    }
    arrAllQs.sort();
    var prevQues = 0;
    var _incr = 0;    

    for(var b=0;b < arrAllQs.length ; b++)
    {
        if (prevQues == arrAllQs[b])
        {
            prevQues = arrAllQs[b];
        }
        else
        {
            if(ShownQKeys.indexOf(arrAllQs[b]) == -1)
            {
                arrNotYetShownQs[_incr] = arrAllQs[b];
                _incr = _incr + 1;
            }
            prevQues = arrAllQs[b];
        }
    }
    arrNotYetShownQs.sort();        
        
    //Get Random Question
    var whichQuestion=get_random(arrNotYetShownQs.length);

    var _clientID = 0;
    var _QuestionText;
    for (var z=0; z< SurveyData.length; z++)
    {
        if (SurveyData[z].QuestKey == arrNotYetShownQs[whichQuestion] && ShownQKeys.indexOf(SurveyData[z].QuestKey)==-1)
        {
            _clientID = SurveyData[z].QuestClientID;
            _QuestionText = SurveyData[z].QuestText;
        }
    }
    
    if(typeof(_QuestionText)=='undefined' || _QuestionText=="")
    {
        AjaxProxy.ShowHideSet(1);
        checkSurveyData();
        return;
    }
    
    var qDiv = document.getElementById("dvSurveyQ");
    if(qDiv!=null)
    {
        spn=document.createElement("span");
        spn.setAttribute("id",_clientID);
        spn.style.styleFloat="left"; //("style","float: left; width: 200px; font-family: arial, sans-serif; font-size: 11px; margin: 5px 0px;");
        //spn.style.width="200px";
//        spn.style.fontFamily="arial, sans-serif";
//        spn.style.fontSize="11px";
        spn.style.margin="5px 0px";
        spn.setAttribute("class", "pollQuestion");
        spn.innerHTML=_QuestionText;
        qDiv.appendChild(spn);
    }
    
    // Iterate and fetch related answers for the current question     
    aDiv=document.getElementById("dvSurveyA");
    tbody = document.getElementById("tbSurvey");
    clearTable(tbody);
    j=0;
    if(aDiv!=null)
    {
        for (var i = 0; i < SurveyData.length; i++) 
        {
            if (SurveyData[i].QuestKey == arrNotYetShownQs[whichQuestion])
            {
                if(j==0) {tr = tbody.insertRow(tbody.rows.length);}
                
                l=document.createElement("label");
                //l.setAttribute("class",SurveyData[i].AnsText.length < 5?"Poll":SurveyData[i].AnsText.length < 10?"PollLong":"PollXLong");
                //l.setAttribute("style","float: left; line-height: 1em;"); REMOVED FOR ATRIPLA!
                //td='td'+i;
                td = tr.insertCell(-1);
                //td.innerHTML = SurveyData[i].AnsText;

                //if(tr.cells.length==1){td.setAttribute("class","first");} REMOVED FOR ATRIPLA!
                
                Ans=document.createTextNode(SurveyData[i].AnsText);
                if(Ans.nodeValue.indexOf('<BR>')>-1) {
                    arAns=Ans.nodeValue.split('<BR>');
                    br=document.createElement('BR');
                    l.appendChild(document.createTextNode(arAns[0]));
                    l.appendChild(br);
                    l.appendChild(document.createTextNode(arAns[1]));
                    l.setAttribute("style", "text-align: left;");
                }
                else
                {
                    l.appendChild(Ans);
                    l.setAttribute("style", "text-align: left;");
                }
                               
                if(document.all && !window.opera && document.createElement)
                {
                    r=document.createElement("<input type='"+SurveyData[i].AnsClientType+"' Name='awarenessParticipation' id='"+SurveyData[i].AnsClientID+"' />");                        
                }
                else
                {
                    r=document.createElement("input");                        
                    r.setAttribute("type",SurveyData[i].AnsClientType);
                    //r.className="radioAnswers";
                    r.setAttribute("id",SurveyData[i].AnsClientID);
                    r.setAttribute("name","awarenessParticipation");
                    r.setAttribute("value","true");
                }
//                aDiv.appendChild(r);
//                aDiv.appendChild(l);
                //td.setAttribute("valign", "top");
                td.setAttribute("style", "vertical-align: top; text-align: left;");
                td.setAttribute("width", "10%");
                td.appendChild(r);
                
                td = tr.insertCell(1);
                //td.setAttribute("valign", "top");
                td.setAttribute("style", "vertical-align: top; text-align: left;");
                td.appendChild(l);
                
                //j+=1;
                //if((j % 2 == 0) && (j > 0))
                //{
//                    br=document.createElement("br");
//                    aDiv.appendChild(br);
                    tr = tbody.insertRow(tbody.rows.length);
                //}
            }             
        }
        
        if(tbody.rows[tbody.rows.length-1].cells.length==0){tbody.deleteRow[tbody.rows.length-1];}
//        if(aDiv.lastChild.tagName=="BR"){aDiv.removeChild(aDiv.lastChild);}
        var btnDiv = document.getElementById("buttons");
        a=document.createElement("a");
        img=document.createElement("input");
        img.setAttribute("type","image");
        img.setAttribute("id","submit_116");
        //img.className="pollSubmit";
        img.setAttribute("value","Submit");
        img.setAttribute("name","submit");
        img.setAttribute("src","/images/07-quickPoll-submit.jpg");
//        img.style.marginTop="-1.25em";
        img.style.width="66px";
        img.style.height="28px";
        img.onclick=new Function("evt","javascript:customlinks('/quick_poll_submission');return QuestionNav();");
        a.appendChild(img);
        //aDiv.appendChild(a);
        btnDiv.appendChild(a);
        
    }
    __context.GetServiceClientProxy('AtriplaUpgradeServices').SessSet(arrNotYetShownQs[whichQuestion]);
}

function get_random(arrLen)
{
    var ranNum= Math.floor(Math.random()*arrLen);
    return ranNum;
}

function getSurveyResult()
{
//Question Primary Key
    var QuestionKey=__context.GetServiceClientProxy('AtriplaUpgradeServices').SessGet().value;
    //var CntList = __context.GetSurveyServicesClientProxy().GetAllAnsCount(QuestionKey);
    var CntList = __context.GetSurveyServicesClientProxy().GetAllAnswersCount(QuestionKey);  
      
    // Get Question Assigned to Page 
    var SurveyList = __context.GetSurveyServicesClientProxy().GetPageSurveyData("index",170);
    var SurveyData =  SurveyList.value;  
    var _QuestionText;
    var PageQuestAnswers = new Array();
    var _PageQuestCount = 0;
    
    var NoResponseAnswers = new Array();
    var _NoResponseCount = 0;
   
    for (var z=0; z< SurveyData.length; z++)
    {
        if (SurveyData[z].QuestKey == QuestionKey)
        {
            _QuestionText = SurveyData[z].QuestText;
            
            PageQuestAnswers[_PageQuestCount] = SurveyData[z];
            _PageQuestCount++;
        }       
    }
    
    var AnsLst =  CntList.value;
    
    var qDiv = document.getElementById("dvSurveyQnA");
    if(qDiv!=null)
    {
        qDiv.innerHTML=_QuestionText;
    }

    var tr, td, imgt;

    tbody = document.getElementById("tbSurveyResult");
    clearTable(tbody);
    
    tr = tbody.insertRow(tbody.rows.length); //Insert row to provide spacing between question and responses.
    
    var sum = 0;
    for (var j = 0; j < AnsLst.length; j++) 
    {
        sum = sum + AnsLst[j].AnsCount;
    }
    
    for(var q=0; q<PageQuestAnswers.length; q++)
    {
        var hasAnswer = new Boolean(false);
                
        for(var p=0; p<AnsLst.length; p++) //Check to see if the answer is in the response array.
        {
            if(PageQuestAnswers[q].AnsText == AnsLst[p].AnsText)
            {
                tr = tbody.insertRow(tbody.rows.length);
                td = tr.insertCell(-1);
                //td.setAttribute("valign", "top");
                td.setAttribute("width", "85%");
                td.setAttribute("style", "vertical-align: top;");
                td.innerHTML=AnsLst[p].AnsText;    
                        
        //        var img = document.createElement('img');
        //        img.setAttribute("src", "/images/poll.gif");   
        //        img.setAttribute("height", "10");  
        //        img.setAttribute("border", "0");  
                percentage = (AnsLst[p].AnsCount / sum * 100);
                percentage = Math.round(percentage);
        //        var w = percentage;
        //        img.setAttribute("width", w);
                
                td = tr.insertCell(-1);
                //td.appendChild(img);

                td = tr.insertCell(-1);
                //td.setAttribute("valign", "top");
                td.setAttribute("width", "15%");
                td.setAttribute("align", "right");
                td.setAttribute("style", "vertical-align: top");
                td.innerHTML=percentage + "%";
                
                tr = tbody.insertRow(tbody.rows.length);
                
                hasAnswer = true;
            }
            
            __context.GetServiceClientProxy('AtriplaUpgradeServices').ShowHideSet(0);
        }
        
        if(hasAnswer==false)
        {                       
            tr = tbody.insertRow(tbody.rows.length);
            td = tr.insertCell(-1);
            td.setAttribute("width", "85%");
        
            td.innerHTML=PageQuestAnswers[q].AnsText;
    
            td = tr.insertCell(-1);
            //td.setAttribute("valign", "top");
            td.setAttribute("style", "vertical-align: top;");
            td = tr.insertCell(-1);
        
            //td.setAttribute("valign", "top");
            td.setAttribute("width", "15%");
            td.setAttribute("align", "right");
            td.setAttribute("style", "vertical-align: top;");
            td.innerHTML="0%";
        
            tr = tbody.insertRow(tbody.rows.length);
        }
    }
    
    __context.GetServiceClientProxy('AtriplaUpgradeServices').SetShownQ(QuestionKey);
}

function QuestionNav()
{
    if(!checkAnswer())
        return false;

    __context.GetServiceClientProxy('AtriplaUpgradeServices').ShowHideSet(1);
}

function checkAnswer(){
    var check=false;
    aDiv=document.getElementById("dvSurveyA");
    radioAnswers=aDiv.getElementsByTagName('input');
    var check=false;
    for(var i=0;i<radioAnswers.length;i++)
    {
       if(radioAnswers[i].type == 'radio'){
            check = (check || radioAnswers[i].checked);
        }
    }
    return check;    
}

function clearTable(tbody) {
    while (tbody.rows.length > 0) {
        tbody.deleteRow(0);
    }
}