﻿// JScript File

var xmlHttp11
var TempID



function AddPollVote(PollOptionID)
{ 
    TempID = GenerateTempID(); 
    xmlHttp11=GetXmlHttpObject()
    if (xmlHttp11==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="AjaxRequests/AJAXPoll.aspx?temp="+TempID+"&PollOptionID="+PollOptionID; 
    xmlHttp11.onreadystatechange=AddPollVote_CallBack
    xmlHttp11.open("GET",url,true)
    xmlHttp11.send(null)        
}


function AddPollVote_CallBack()
{
	if (xmlHttp11.readyState==1)
	{
		//document.getElementById("divPoll").innerHTML = "<i>Initializing..</i>";
	}
	if (xmlHttp11.readyState==2)
	{
		//document.getElementById("divPoll").innerHTML = "<i>Loading..</i>";
	}
	if (xmlHttp11.readyState==1)
	{
		//document.getElementById("divPoll").innerHTML = "<i>Processing..</i>";
	}
	if (xmlHttp11.readyState==4 || xmlHttp11.readyState=="complete")
    	{	
		    if (xmlHttp11.status == 200)      
		    { 
		        //document.getElementById("divPoll").innerHTML = "<br>Poll Added";;
		    }
	    }
}


function getCookie(name) 
{ 
    var index = document.cookie.indexOf(name + "="); 
    if (index == -1) 
    {
        return null; 
    }
    index = document.cookie.indexOf("=", index) + 1; // first character 
    var endstr = document.cookie.indexOf(";", index); 
    if (endstr == -1)
    {
         endstr = document.cookie.length; // last character
    } 
    return unescape(document.cookie.substring(index, endstr)); 
} 

function GetPollResult()
{
       var QuestionID ;
       GetVoteResult(document.getElementById("HiddenPollQuestionID").value,0);
}


function GetVoteResult(PollQuestionID,AlreadyVoted)
{ 
        TempID = GenerateTempID(); 
        xmlHttp11=GetXmlHttpObject()
        if (xmlHttp11==null)
        {
            alert ("Browser does not support HTTP Request")
            return
        }
        var url="AjaxRequests/AJAXPoll.aspx?temp="+TempID+"&PollQuestionID="+PollQuestionID+"&AlreadyVoted="+AlreadyVoted ; 
        xmlHttp11.onreadystatechange=GetVoteResult_CallBack
        xmlHttp11.open("GET",url,true)
        xmlHttp11.send(null)        
      
}


function GetVoteResult_CallBack()
{
	if (xmlHttp11.readyState==1)
	{
		document.getElementById("divPoll").innerHTML = "<i>Initializing..</i>";
	}
	if (xmlHttp11.readyState==2)
	{
		document.getElementById("divPoll").innerHTML = "<i>Loading..</i>";
	}
	if (xmlHttp11.readyState==1)
	{
		document.getElementById("divPoll").innerHTML = "<i>Processing..</i>";
	}	
	if (xmlHttp11.readyState==4 || xmlHttp11.readyState=="complete")
    	{	
		    if (xmlHttp11.status == 200)      
		    { 
		        document.getElementById("divPoll").innerHTML = xmlHttp11.responseText;
		    }
	    }
}


function GetPoll()
{ 
    TempID = GenerateTempID(); 
    xmlHttp11=GetXmlHttpObject()
    if (xmlHttp11==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }
    var url="AjaxRequests/AJAXPoll.aspx?temp="+TempID+"&Vote=Vote"; 
    xmlHttp11.onreadystatechange=GetPoll_CallBack
    xmlHttp11.open("GET",url,true)
    xmlHttp11.send(null)        
}


function GetPoll_CallBack()
{
	if (xmlHttp11.readyState==1)
	{
		document.getElementById("divPoll").innerHTML = "<i>Initializing..</i>";
	}
	if (xmlHttp11.readyState==2)
	{
		document.getElementById("divPoll").innerHTML = "<i>Loading..</i>";
	}
	if (xmlHttp11.readyState==1)
	{
		document.getElementById("divPoll").innerHTML = "<i>Processing..</i>";
	}
	if (xmlHttp11.readyState==4 || xmlHttp11.readyState=="complete")
    	{	
		    if (xmlHttp11.status == 200)      
		    { 
		        document.getElementById("divPoll").innerHTML = xmlHttp11.responseText;
		    }
	    }
}

function CheckPoll()
{
    var flagValid = false;
    var no = document.getElementById("hiddenPollOps").value;
    var type = document.getElementById("HiddenPollType").value;
    var i = 0;
    
    if(type == 0)
    {
        for(i = 0;i < no;i++)
        {
            if(document.getElementById("pollopt"+(i+1)).checked)
            {
                flagValid = true;
                break;
            }
        }
    }
    else
    {
        for(i = 0;i < no;i++)
        {
            if(document.getElementById("pollopt"+(i+1)).checked)
            {
                flagValid = true;
            }
        }
    }
    
    if(flagValid == false)
    {
        alert("Please select the option");
        return;
    }
   
   var cookieString = document.cookie; 
   if(cookieString.length != 0)
   { 
        if(getCookie("voted")=="true")
       {
           GetVoteResult(document.getElementById("HiddenPollQuestionID").value,1); 
           return;  
       }    
   }
       
    if(type == 0)
    {
        for(i = 0;i < no;i++)
        {
            if(document.getElementById("pollopt"+(i+1)).checked)
            {
                AddPollVote(document.getElementById("pollopt"+(i+1)).value);
                break;
            }
        }
    }
    else
    {
        for(i = 0;i < no;i++)
        {
            if(document.getElementById("pollopt"+(i+1)).checked)
            {
                AddPollVote(document.getElementById("pollopt"+(i+1)).value);
            }
        }
    }
    
    
    document.cookie = "voted="+true+";"; 
    var QuestionID;
    GetVoteResult(document.getElementById("HiddenPollQuestionID").value,0);
    
   
}


