//Our XmlHttpRequest object to get the auto suggest (GLOBAL)
var searchReq = getXmlHttpRequestObject();

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser is too old!");
	}
}

// function that URL escapes a string
function encode(uri) 
{
  if (encodeURIComponent) 
  {
    return encodeURIComponent(uri);
  }
  if (escape) 
  {
    return escape(uri);
  }
}

// function returns node value, regarding browser
function getNodeValue(node) { 
  if(node.textContent) { // firefox
    return node.textContent;
  }
  if(node.text) { // ie
    return node.text;    
  }
  if(node.innerText) {
    return node.innerText; // ie html
  }
}


// transforms all the children of an xml node into an array
function xmlToArray(resultsXml)
{
  // initiate the resultsArray
  var resultsArray= new Array();  
  // loop through all the xml nodes retrieving the content  
  for(i=0;i<resultsXml.length;i++)
    resultsArray[i]=getNodeValue(resultsXml.item(i));
  // return the node's content as an array
  return resultsArray;
}

function clear_input(obj, value) {
	if(obj.value == value) {
		obj.value = '';
	}
} 

function poll_submit(poll_id) {
    var poll = document.getElementById('poll');
    var answers = poll.getElementsByTagName('input');
    for (var i = 0; i < answers.length; i++) { 
        if(answers[i].getAttribute("name") == 'poll_answer') {
            if(answers[i].checked) {
                url = '/web.php?ajax=poll&poll='+ poll_id + '&answer=' + answers[i].value;
                var xmlHttpRequestObject = getXmlHttpRequestObject();
                xmlHttpRequestObject.open("GET", url, true);
                xmlHttpRequestObject.onreadystatechange = function() {
                //-------------
                    if (xmlHttpRequestObject.readyState == 4) {
                      if (xmlHttpRequestObject.status == 200) {
                        var response = xmlHttpRequestObject.responseText;
                        var xmldoc = xmlHttpRequestObject.responseXML;
                        poll = xmldoc.getElementsByTagName('poll');
                        if(poll[0]) {  // poll exist
                            var html = '<div>' + poll[0].getAttribute('question') + '</div>';
                            html = html + '<div id="poll">'; 
                            
                            var answers = poll[0].getElementsByTagName('answer');
                            for(i=0;i<answers.length;i++) {
                                html = html + '<div><span style="padding-left:5px;">'+ answers[i].getAttribute('answer') + '</span>';
                                var percent_width = (answers[i].getAttribute('percent') < 2 ? 2 : answers[i].getAttribute('percent'));
                                html = html + '<div><div style="float:right;">'+ answers[i].getAttribute('percent') + '%</div><div style="background-color:#D8D8D8; width:' + percent_width + 'px;">&nbsp;</div></div></div>';                            
                            }
                            
                            html = html + '<div style="margin-top:3px;">Število glasov: ' + poll[0].getAttribute('total_count') + '</div>';
                        
                            document.getElementById('poll_content').innerHTML = html;
                        }
                      }
                    }
                //---------------    
                    } 
                xmlHttpRequestObject.send(null);
            }
        }
    }
}

function show_points(id) {
	var points_div = document.getElementById('points_'+id);
	if(points_div != null) {
    points_div.style.display = (points_div.style.display == '') ? 'none' : '';
	}
}

