﻿// JScript File
function Poll(){
this.charttype="";
}

function savePoll(pollid,stylesheetid,question,answers,allowmultipleanswers,allowotheranswer,allowmultiplevotes, categoryid,url,displaytype,tags){     
       answerstring = "";
       for(var i=0;i<answers.length;i++){             
            if(i>0){
                answerstring += "|" + answers[i];        
            }else{
                answerstring = answers[i];
            }
       }               
       new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:false,postBody:'question=' + escape(question)+ '&pollid=' + pollid + '&answers=' + escape(answerstring) + '&url=' + escape(url) + '&categoryid=' +  categoryid + '&allowmultiplevotes=' + allowmultiplevotes + '&allowotheranswer=' + allowotheranswer + '&allowmultipleanswers=' + allowmultipleanswers + '&stylesheetid=' + stylesheetid + '&action=savepoll&tags=' + escape(tags) + '&displaytype=' + displaytype + '', onSuccess:function(t){         
            var rnValue = t.responseText.split(',');
            this.pollid = rnValue[0];
            document.getElementById("frm_PollId").value = this.pollid;
            document.getElementById("frm_AnswerPreview").disabled = true;
            document.getElementById("frm_Save").disabled = true;  
            var nextHtml =  "<span class='good'>"+ lexicon.LEX_POLL_SAVED_SUCCESSFUL+ "<a href='/poll.aspx?pollid=" + this.pollid
                +"'>" + lexicon.LEX_CLICKING_HERE +"</a>.<br />" + lexicon.LEX_POLL_SAVED_SUCCESSFUL_JSCODE + rnValue[1] +"</span><br /><div>"
                +"<input type='button' class='buttonbig' value='"+lexicon.LEX_ADD_NEW_POLL+"' onclick=\"document.location='/polladdedit.aspx';\"/></div>"
                showSuccessfulMsg(nextHtml,true);
        }, onFailure:function(t){
            showSuccessfulMsg("");
        alert(t.responseText);
    }}); 
}
function getPoll(pollid, htmlid){              
        
       new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:false,postBody:'pollid=' + pollid + '&action=getpoll', onSuccess:function(t){         
            var obj = document.getElementById(htmlid);   
                     
            obj.innerHTML = t.responseText; 
                                  
        }, onFailure:function(t){
        //alert(t.responseText);
    }}); 
}

function getLocalPoll(pollid,htmlid,asyn){
       var oHtml = document.getElementById(htmlid);
       var oWait = showWaitDiv(oHtml,false,true);
       if (oWait) {
            oWait.style.paddingTop = "180px";
            oWait.style.marginLeft = "100px";
       }
       asyn = asyn == true ? true : false;
      //for poll.aspx
       new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:asyn,postBody:'pollid=' + pollid + '&action=getlocalpoll', onSuccess:function(t){         
           
            var obj = document.getElementById(htmlid);
//            if(asyn == true && t.responseText.length <=4){
//              var charttype = document.getElementById("frm_DisplayType").value;
//              
//              if (charttype.length > 0) { 
//                var oPoll = new Poll();
//                oPoll.getfusionpoll(pollid,htmlid,charttype,680,350);
//                document.getElementById("divFlash").display = "block";
//                document.getElementById("frm_DisplayType").disabled = false;
//              }
//            } else { 
//                if(obj.innerHTML != "undefined" && t.responseText != "undefined"){
//                    obj.innerHTML = t.responseText;
//                }
//            }
            
            if(obj.innerHTML != "undefined" && t.responseText != "undefined"){
                obj.innerHTML = t.responseText;
            }
                      
        }, onFailure:function(t){
            //alert(t.responseText);
    }}); 
}
 



function getFusionPoll(pollid, htmlid,charttype,w,h){         
    var width = "800";
    var height = "300";     
    if(w){
        width = w;
    }
    if(h){
        height = h - 25;
    }
       switch(charttype.toUpperCase()){
        case "2DPIECHART":
            fzn_swf = "FCF_Pie2D.swf";
            break;
        case "3DPIECHART":
            fzn_swf = "FCF_Pie3D.swf";
            break;
        case "PIECHART":
            fzn_swf = "FCF_Pie2D.swf";
            break;
        case "BARCHART":
            fzn_swf = "FCF_Bar2D.swf";
            break; 
        case "2DDONUT":
            fzn_swf = "FCF_Doughnut2D.swf";
            break;
        case "FUNNEL":
            fzn_swf = "FCF_Funnel.swf";
            break;
        case "LINE":
            fzn_swf = "FCF_Line.swf";
            break;                     
        default:
            fzn_swf = "FCF_Bar2D.swf";
            break;
       }
       //function(swf, id, w, h, debugMode, registerWithJS, c, scaleMode, lang, detectFlashVersion, autoInstallRedirect)
       var debugMode = 0;
       var regJS = false;
       var bgColor = false;
       var scaleMode = null;
       var lang = "utf-8";
	   var chart = new FusionCharts("/assets/flash/charts/" + fzn_swf, "ChartId", width, height,debugMode,regJS,bgColor,scaleMode,lang);
	   chart.setDataURL(encodeURI("/Ajaxservices/poll/fusionchartxml.aspx?pollid=" + pollid));	
	   if (chart.render(htmlid) && document.location.href.indexOf("widget-content.aspx") > 0)
	   {
	        var htmElement = (typeof htmlid == 'string') ? document.getElementById(htmlid) : htmlid;
	        var tmpDiv = document.createElement("span");
	        //tmpDiv.innerHTML = default_widget_copyright;
	        
	        htmElement.appendChild(tmpDiv);
	   }
	   if(document.location.href.indexOf("poll.aspx")<=0){
	     //document.getElementById(htmlid).innerHTML += "<div class='spacer'></div><div><a href='/poll.aspx?pollid=" + pollid + "' style='font-size:10px;' target='_parent'>Detailed Poll Results</a></div>"   
	   }
	   showSuccessfulMsg("");
}

function getPollByWidget(pollid,htmlid,charttype,w,h){

       var obj = document.getElementById(htmlid);
       
       obj.innerHTML = "";

       if (pollid && pollid.length > 0)
       {
           //var oWait = showWaitDiv(obj,false,true);
           //oWait.style.paddingTop = "50px";
           //oWait.style.marginLeft = "30px";
          //for poll.aspx
           new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:false,postBody:'pollid=' + pollid + "&w=" + w + "&h=" + h + '&action=getpollbywidget', onSuccess:function(t){         
//                if(t.responseText.length <=1){
//                  if (charttype.length > 0) { 
//                    var oPoll = new Poll();
//                    oPoll.getfusionpoll(pollid,htmlid,charttype,w,h);
//                  }
//                } else {
//                    if(obj.innerHTML != undefined && t.responseText != undefined){
//                        obj.innerHTML = t.responseText + "<input type='hidden' id='oWidgetValue' value='1' />";
//                    }
//                    
//                }
                if(obj.innerHTML != undefined && t.responseText != undefined){
                    obj.innerHTML = t.responseText + "<input type='hidden' id='oWidgetValue' value='1' />";
                }
            }, onFailure:function(t){
                //alert(t.responseText);
        }}); 
    }
}

function getChartType(pollid){
     new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:false,postBody:'pollid=' + pollid + '&action=getcharttype', onSuccess:function(t){         
            this.charttype  = t.responseText;
        }, onFailure:function(t){
      //  alert(t.responseText);
    }}); 
}

function getPublicPoll(pollid, htmlid){              
       
       new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:false,postBody:'pollid=' + pollid + '&action=getpublicpoll', onSuccess:function(t){         
            var obj = document.getElementById(htmlid);  
            if(obj.innerHTML != "undefined" && t.responseText != "undefined"){          
             obj.innerHTML = t.responseText;                       
            }
        }, onFailure:function(t){
       // alert(t.responseText);
    }}); 
}

function answerPreview(question,answers,displaytype,styleid,htmlid) {
    var answerstring = ""; 
    if (typeof answers == 'object') {
        for(var i=0;i<answers.length;i++){             
            if(i>0){
                answerstring += "|" + answers[i];        
            }else{
                answerstring = answers[i];
            }
        }
    } else { answerstring = answers;}
    var w = document.getElementById(htmlid).offsetWidth - 110;
    new Ajax.Request("/ajaxservices/poll/pollPreview.aspx",
    { method:'post',asynchronous:true,postBody:'question=' + escape(question) + '&w=' + w +  '&displaytype=' + displaytype + '&answers=' + escape(answerstring) + '&stylesheetid=' + styleid + '&action=answerpreview', onSuccess:function(t){ 
           var stylesheet = getStyleSheet(styleid);
           stylesheet = stylesheet.replace(/^\s+|\s+$/g, '');
           
           try{ applyStyleSheet(stylesheet); } catch(e) {}
           
           var htmlText = t.responseText; //+ "<style type='text/css'>" + stylesheet + "</style>";
           document.getElementById(htmlid).innerHTML =  htmlText;
                      
        }, onFailure:function(t){
           alert(t.responseText);
    }}); 
}


function getPollByURL(url){              
       
       new Ajax.Request("/ajaxservices/poll/poll.aspx", {method:'post',asynchronous:false,postBody:'url=' + escape(url) + '&action=getpollbyurl', onSuccess:function(t){         
            return t.responseText;
            
        }, onFailure:function(t){
       // alert(t.responseText);
    }}); 
}


Poll.prototype.getlocalpoll = getLocalPoll;
Poll.prototype.getfusionpoll = getFusionPoll;
Poll.prototype.getpoll = getPoll;
Poll.prototype.getpublicpoll = getPublicPoll;
Poll.prototype.savepoll = savePoll;
Poll.prototype.answerPreview = answerPreview;
Poll.prototype.getPollByWidget = getPollByWidget;
Poll.prototype.getpollbyurl = getPollByURL;
 
