function MySetCookie(name, value, days, path, domain, secure) 
{
	var expires = -1;
	if ( typeof days == "number" ) {
		var d = new Date();
		if ( days > 0 ) {
			d.setTime(d.getTime()+(days*24*60*60*1000));
			expires = d.toGMTString();
		}
		else if ( days == 0 ) {
			d.setTime(d.getTime()+(1*24*60*60*1000));
			d.setHours( 0, 0, 0 ,0 );
			expires = d.toGMTString();
		}
	}
	value = escape(value);
	document.cookie = name + "=" + value + ";"
		+ (expires != -1 ? " expires=" + expires + ";" : "")
		+ (path ? "path=" + path : "")
		+ (domain ? "; domain=" + domain : "")
		+ (secure ? "; secure" : "");
}

function BarChart(title, labelWidth, barWidth, barHeight, bgColor) 
{
	this.barList = new Array();
	this.totalList = new Array();

  this.labelWidth = labelWidth;
                
  this.barWidth = barWidth;
  this.barHeight = barHeight;
  this.bgColor = bgColor;
  this.title = title;
  this.alignCaption = "top";
                
  this.replace = function (divContainer) 
  {
		// Declare variables and create the header, footer, and caption.
    var table = document.createElement("TABLE");
    table.border = "0px";
    table.bgColor = this.bgColor;
		table.className = "polltable";
                    
    if (this.title.length > 0)
    {
			var caption = document.createElement("CAPTION");
			caption.className = "pollheader";
      caption.appendChild(document.createTextNode(this.title));
      caption.align = this.alignCaption;
      table.appendChild(caption);
    }
    
		var tbody = document.createElement("TBODY");
    table.appendChild(tbody);
                    
    var totalValue = 0;
    for (var i=0; i < this.barList.length; i++) 
    {
			totalValue += parseInt(this.barList[i].value, 10);
    }
                    
    for (i=0; i < this.barList.length; i++) 
    {
			var row = document.createElement("TR");
      tbody.appendChild(row);
                        
			// Put label in left cell align right/left
      var leftCell = document.createElement("TD");
			leftCell.className = "pollbarlabel";
			leftCell.style.width = this.labelWidth + "px";
      leftCell.appendChild(document.createTextNode(this.barList[i].label));
      row.appendChild(leftCell);
                        
      // Put bar in right cell containing a table align left
      var rightCell = document.createElement("TD");
			rightCell.className = "pollbar";
      var ratio=0;
      if( totalValue>0 )
	 ratio = this.barList[i].value/totalValue;
      this.addBar(rightCell, ratio, this.barList[i].color);
      row.appendChild(rightCell);
		}

    for (i=0; i < this.totalList.length; i++) 
    {
			var row = document.createElement("TR");
      tbody.appendChild(row);
                        
			// Put label in left cell align right/left
      var leftCell = document.createElement("TD");
			leftCell.className = "polltotal";
			leftCell.setAttribute("colspan", "2");
			leftCell.colSpan = 2;
			leftCell.style.width = "100%";
      leftCell.appendChild(document.createTextNode(this.totalList[i].label+this.totalList[i].value));
      row.appendChild(leftCell);
		}
	
		// Insert the table into the document tree.
		divContainer.parentNode.replaceChild(table,divContainer);
	};
     
	this.addBarData = function (label, value, color) 
	{
		var barData = new Object();
    this.barList[this.barList.length] = barData;
                    
		barData.label = label;
    barData.value = value;
    barData.color = color ? color : "black";
    return barData;
  };

	this.addTotalData = function (label, value) 
	{
		var TotalData = new Object();
		this.totalList[this.totalList.length] = TotalData;
                    
		TotalData.label = label;
    TotalData.value = value;
    return TotalData;
  };
       
	this.addBar = function (cell, ratio, color)
  { 
		var barTable = document.createElement("TABLE");
		barTable.className = "bartable";
                   
    var barTbody = document.createElement("TBODY");
    barTable.appendChild(barTbody);
    
    var barRow = document.createElement("TR");
    barTbody.appendChild(barRow);
    barRow.style.height = this.barHeight + "px";
    
    var barData = document.createElement("TD");
    barData.height = this.barHeight;
    var pixels = Math.round(ratio * this.barWidth);
    barData.width = pixels > 0 ? pixels : 1 + "px";
		// Use color of image?
		if (color.indexOf('.gif') > -1 || color.indexOf('.jpg') > -1 || color.indexOf('.png') > -1)
		{
	    var barDataBar = document.createElement("IMG");
			barDataBar.src = color;
			barDataBar.height = this.barHeight;
	    barDataBar.width = barData.width;
	    barData.appendChild(barDataBar);
		}
		else {
			barData.bgColor = color;

		}
		barRow.appendChild(barData);
    var labelData = document.createElement("TD");
    labelData.style.height = this.barHeight;
    labelData.appendChild(document.createTextNode(Math.round(Math.round(ratio * 1000)/10) + '%'));
    barRow.appendChild(labelData);
    
    barTable.cellspacing = "0px";
    barTable.cellpadding = "0px";
    
    cell.appendChild(barTable);
	};
}
//var starturl = "/modules/poll/";
var starturl = "";
  
var XMLHttpRequestObject = new Array();
var startpollcode = "";

function OnVote(votename,show,pollid)
{

	if (starturl.length < 1)
	{
			starturl = document.getElementById('pollurl').value;
	}

	// AK var theurl = starturl + "poll.php";
	//var theurl = starturl + "edoris.dll?app=server&com=dialog&tem=poll";
	var theurl = starturl + "?app=server&com=dialog&tem=poll";
	
	XMLHttpRequestObject[votename] = new Object();
  XMLHttpRequestObject[votename].color = new Array();
  XMLHttpRequestObject[votename].label = new Array();
	
	//dojo.io.cookie.deleteCookie(votename); 

	var voteallowed = false;
	if (dojo.io.cookie.getCookie(votename) == null) 
		voteallowed = true;

	// save the start html
	if (show != '2')
	{
		startpollcode = document.getElementById('voteouter').innerHTML;
	}
	else if (show == '2')
	{
		document.getElementById('voteouter').innerHTML = startpollcode;
		return;
	}

	if (voteallowed == false && show != '1')
	{
		alert(document.getElementById('pollvoted').value);
	}
	else {
		// AK var thefullurl = theurl + "?" + "votename=" + votename + "&show=" + show;
		var thefullurl = theurl + "&" + "poll_id=" + pollid + "&show=" + show;
		
		var voteChoices=document.getElementsByName(votename);
		var done = false;
		for(var i=0; i < voteChoices.length; i++)
		{
			// AK thefullurl += '&' + voteChoices[i].value + '=' + (voteChoices[i].checked ? 1 : 0);
			if ( voteChoices[i].checked && show != 1 ) {
				thefullurl += '&selected=' + voteChoices[i].getAttribute('label');
				done = true;
			}
			XMLHttpRequestObject[votename].color[i] = voteChoices[i].getAttribute('barcolor');
			XMLHttpRequestObject[votename].label[i] = voteChoices[i].getAttribute('value');
		}
		if ( done == false ) {
			thefullurl += '&selected=0';
		}
		else {
			if ( show != 1 )
				MySetCookie(votename, votename, 0, "/");
		}
		thefullurl += "&random=" + Math.random();
		MySetCookie("polltest", "polltest", 1, "/" ); 

		/*
		d = doSimpleXMLHttpRequest(thefullurl);
		d.addCallback(OnVoteProcess,votename,document.getElementById('pollbacktovote').value);
		d.addErrback(OnError);
		*/

		// an asynchronous request to foo.php that returns a JavaScript literal
		// which is eval()'d
		var bindArgs = {
		    url:        thefullurl,
		    mimetype:   "text/plain",
		    error:      function(type, errObj){
		        // handle error here
						OnError;
		    },
		    load:      function(type, data, evt){
		        // handle successful response here
						OnVoteProcess(votename,document.getElementById('pollbacktovote').value,data);
		    }
		};

		// dispatch the request
    var requestObj = dojo.io.bind(bindArgs);
	}
}

function OnClickEarlier(votename,show,pollid)
{

	if (starturl.length < 1)
	{
			starturl = document.getElementById('pollurl').value;
	}

	// AK var theurl = starturl + "poll.php";
	//var theurl = starturl + "edoris.dll?app=server&com=dialog&tem=poll";
	var theurl = starturl + "?app=server&com=dialog&tem=poll_earlier";
	
	XMLHttpRequestObject[votename] = new Object();
  XMLHttpRequestObject[votename].color = new Array();
  XMLHttpRequestObject[votename].label = new Array();
	
	//dojo.io.cookie.deleteCookie(votename); 

	var voteallowed = false;
	if (dojo.io.cookie.getCookie(votename) == null) 
		voteallowed = true;

	// save the start html
	if (show != '2')
	{
		startpollcode = document.getElementById('voteouter').innerHTML;
	}
	else if (show == '2')
	{
		document.getElementById('voteouter').innerHTML = startpollcode;
		return;
	}

	if (voteallowed == false && show != '1')
	{
		alert(document.getElementById('pollvoted').value);
	}
	else {
		// AK var thefullurl = theurl + "?" + "votename=" + votename + "&show=" + show;
		var thefullurl = theurl + "&" + "poll_id=" + pollid + "&show=" + show;
		
		var voteChoices=document.getElementsByName(votename);
		var done = false;
		for(var i=0; i < voteChoices.length; i++)
		{
			// AK thefullurl += '&' + voteChoices[i].value + '=' + (voteChoices[i].checked ? 1 : 0);
			if ( voteChoices[i].checked && show != 1 ) {
				thefullurl += '&selected=' + voteChoices[i].getAttribute('label');
				done = true;
			}
			XMLHttpRequestObject[votename].color[i] = voteChoices[i].getAttribute('barcolor');
			XMLHttpRequestObject[votename].label[i] = voteChoices[i].getAttribute('value');
		}
		if ( done == false ) {
			thefullurl += '&selected=0';
		}
		else {
			if ( show != 1 )
				MySetCookie(votename, votename, 0, "/");
		}
		thefullurl += "&random=" + Math.random();
		MySetCookie("polltest", "polltest", 1, "/" ); 

		/*
		d = doSimpleXMLHttpRequest(thefullurl);
		d.addCallback(OnVoteProcess,votename,document.getElementById('pollbacktovote').value);
		d.addErrback(OnError);
		*/

		// an asynchronous request to foo.php that returns a JavaScript literal
		// which is eval()'d
		var bindArgs = {
		    url:        thefullurl,
		    mimetype:   "text/plain",
		    error:      function(type, errObj){
		        // handle error here
						OnError;
		    },
		    load:      function(type, data, evt){
		        // handle successful response here
						OnVoteProcess(votename,document.getElementById('pollbacktovote').value,data);
		    }
		};

		// dispatch the request
    var requestObj = dojo.io.bind(bindArgs);
	}
}

function OnVoteProcess(votename,bactopolltext,response)
{
	//var result = response.responseText;
	//var variables = result.split('&');
	var variables = response.split('&');
      
	// Create an BarChart instance
	var chart = new BarChart(document.getElementById('pollheader').value, document.getElementById('labelwidth').value, document.getElementById('barwidth').value, document.getElementById('barheight').value,"");
	
	for (var i = 0; i < variables.length; i++)
	{
		var variable = variables[i];
    var item = variable.split('=');
		var colorimgstr = "";

		if (document.getElementById('colorimg').value == "color")
			colorimgstr = XMLHttpRequestObject[votename].color[i];
		else
			colorimgstr = "/images/"+XMLHttpRequestObject[votename].color[i]+".gif";

		if (i < variables.length-1) // Bar
	    chart.addBarData(XMLHttpRequestObject[votename].label[i], item[1], colorimgstr);
		else // Total amount 
	    chart.addTotalData(document.getElementById('totallabel').value, item[1]);
  }

 //chart.addFooterData(document.getElementById('pollfooter').value, theurl+"?showvotes=);

	var name = "votediv";
  var divContainer = document.getElementById(name);
	var pollfooterlink = document.getElementById('votefooterlink');
	if (divContainer) {
		// Generate HTML for the BarChart using the DOM
    chart.replace(divContainer);
  }
	if (pollfooterlink) { // HAndle the footer link
		pollfooterlink.innerHTML = bactopolltext;
		pollfooterlink.href = pollfooterlink.href.replace('\'1\'','\'2\'');
	}
	// alert(pollfooterlink.innerHTML);
	// pollbacktovote<a id="colorvotefooterlink" class="pollink" href="javascript:OnVote('colorvote',1)">View Current Results</a>
                
  XMLHttpRequestObject[votename] = null;
	dojo.io.cookie.deleteCookie("polltest");  
}

function OnError (errortext)
{
	//alert(errortext.responseText);
	dojo.io.cookie.deleteCookie("polltest");  
}

function OnVoteButton(votename,show,pollid,label)
{

            if (starturl.length < 1)
            {
                                    starturl = document.getElementById('pollurl').value;
            }

            // AK var theurl = starturl + "poll.php";
            //var theurl = starturl + "edoris.dll?app=server&com=dialog&tem=poll";
            var theurl = starturl + "?app=server&com=dialog&tem=poll";
            
            XMLHttpRequestObject[votename] = new Object();
  XMLHttpRequestObject[votename].color = new Array();
  XMLHttpRequestObject[votename].label = new Array();
            
            //dojo.io.cookie.deleteCookie(votename); 

            var voteallowed = false;
            if (dojo.io.cookie.getCookie(votename) == null) 
                        voteallowed = true;

            // save the start html
            if (show != '2')
            {
                        startpollcode = document.getElementById('voteouter').innerHTML;
            }
            else if (show == '2')
            {
                        document.getElementById('voteouter').innerHTML = startpollcode;
                        return;
            }

            if (voteallowed == false && show != '1')
            {
                        alert(document.getElementById('pollvoted').value);
            }
            else {
                        // AK var thefullurl = theurl + "?" + "votename=" + votename + "&show=" + show;
                        var thefullurl = theurl + "&" + "poll_id=" + pollid + "&show=" + show;
                        
                        var voteChoices=document.getElementsByName(votename);
                        var done = false;
                        //alert(label);
			for(var i=0; i < voteChoices.length; i++)
                        {
                                    // AK thefullurl += '&' + voteChoices[i].value + '=' + (voteChoices[i].checked ? 1 : 0);
                                    if ( voteChoices[i].getAttribute('label')==label && show != 1 ) {
                                                thefullurl += '&selected=' + voteChoices[i].getAttribute('label');
                                                done = true;
                                    }
                                    XMLHttpRequestObject[votename].color[i] = voteChoices[i].getAttribute('barcolor');
                                    XMLHttpRequestObject[votename].label[i] = voteChoices[i].getAttribute('value');
                        }
                        if ( done == false ) {
                                    thefullurl += '&selected=0';
                        }
                        else {
                                    if ( show != 1 )
                                                MySetCookie(votename, votename, 0, "/");
                        }
                        thefullurl += "&random=" + Math.random();
                        MySetCookie("polltest", "polltest", 1, "/" ); 

                        /*
                        d = doSimpleXMLHttpRequest(thefullurl);
                        d.addCallback(OnVoteProcess,votename,document.getElementById('pollbacktovote').value);
                        d.addErrback(OnError);
                        */

                        // an asynchronous request to foo.php that returns a JavaScript literal
                        // which is eval()'d
                        var bindArgs = {
                            url:        thefullurl,
                            mimetype:   "text/plain",
                            error:      function(type, errObj){
                                // handle error here
                                                                        OnError;
                            },
                            load:      function(type, data, evt){
                                // handle successful response here
                                                                        OnVoteProcess(votename,document.getElementById('pollbacktovote').value,data);
                            }
                        };

                        // dispatch the request
    var requestObj = dojo.io.bind(bindArgs);
            }
}


