
function getHTTPObject() {
   var xmlhttp;
   try {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
       try {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (e) {
           xmlhttp = false;
       }
   }
   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
       try {
           xmlhttp = new XMLHttpRequest();
       } catch (e) {
           xmlhttp = false;
       }
   }
   return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object

function addVote(pollId, answerId) {
   
    http.open('get', '/blog/scripts/pollvote.php?pollid=' + pollId + '&answerid=' + answerId, true);

    http.onreadystatechange = function () {

        if ((http.readyState == 4) && (http.status == 200)) {
            
            var xmlDoc = http.responseXML;

            var pollDetails = xmlDoc.documentElement.getElementsByTagName("poll");
            var pollAnswers = xmlDoc.documentElement.getElementsByTagName("answer");
            
            var pollVotes = parseFloat(pollDetails[0].getAttribute("pollvotes"));          

            var pollHTML = '<table style="text-align: left; width: 100%;">';

            for (var i = 0; i < pollAnswers.length; i++) {

                var name = pollAnswers[i].getAttribute("label");
                var total = parseFloat(pollAnswers[i].getAttribute("newtotal"));
                var percent = Math.round((total / pollVotes) * 100);
                var barwidth = percent - 2;
                var bar;

                if(i % 2) { bar = 2; } else { bar = 1; }

                pollHTML += '<tr>'
                          + '<td width="10%" style="text-align: center;">' + name + '</td>'
                          + '<td width="40%">';

                if (total > 0) {
                    pollHTML += '<img src="/blog/images/bar-' + bar + '-end.jpg" width="1%" height="8px" />'
                              + '<img src="/blog/images/bar-' + bar + '.jpg" width="' + barwidth + '%" height="8px" />'
                              + '<img src="/blog/images/bar-' + bar + '-end.jpg" width="1%" height="8px" />';
                } else {
                    pollHTML += '-';
                }
                    
                pollHTML += '</td>'
                          + '<td width="40%" style="text-align: right;">' + total + ' (' + percent + '%)</td>'
                          + '</tr>';

            }

            pollHTML += '<tr><td colspan="3" style="text-align: center; font-weight: bold;">Thank you for voting!</td></tr>';

            pollHTML += '</table>';

            var pollContainer = document.getElementById('poll-' + pollId + '-inner');
    
            pollContainer.innerHTML = pollHTML;


        }

    }

    http.send(null);

}

function disableButton() {

	var addButton = document.getElementById('add-button');
	addButton.innerHTML = 'Please Wait ...';
	addButton.className = 'button-disabled';

}

function contentAlert(id) {

	var alertArea = document.getElementById('alert-' + id);

	alertArea.innerHTML = '';

	var confirmYes = createElement( 'alert-' + id + '-confirm', 'A', '', 'Yes' );
		confirmYes.className = 'confirm-link-yes';
		addEventHandler( confirmYes, 'click', function(){ alertComment(id); } );

	var confirmCancel = createElement( 'alert-' + id + '-confirm', 'A', '', 'No' );
		confirmCancel.className = 'confirm-link-no';
		addEventHandler( confirmCancel, 'click', function(){ cancelAlert(id); } );

	var confirmContainer = createElement( 'alert-' + id + '-container', 'DIV', '', 'You are about to report this comment for breaching our policy, which can be found <a href="/blog/policy.html" target="_blank">here</a>. <strong>Are you sure?</strong> ' );
		confirmContainer.appendChild(confirmYes);
		confirmContainer.appendChild(confirmCancel);

	alertArea.appendChild(confirmContainer);

	confirmContainer.className = 'alert-confirm';

}

function alertComment( id ) {

	var commentalert = getHTTPObject();

	commentalert.open('get', '/blog/scripts/report.php?commentid=' + id, true);

    commentalert.onreadystatechange = function () {

        if ((commentalert.readyState == 4) && (commentalert.status == 200)) {

			var alertArea = document.getElementById('alert-' + id);
			alertArea.innerHTML = '<a class="alert-reported">Comment Reported</a>';

		}

	}

	commentalert.send(null);

}

function cancelAlert( id ) {

	var alertArea = document.getElementById('alert-' + id);
	alertArea.innerHTML = '<a class="alert-link" onclick="contentAlert(' + id + ');">Report Comment</a>';

}

function createElement  (id, type, className, html, name) {
 
	var p, subtype;

	if(( p = type.indexOf( ':' ) ) !== -1 ){
		subtype	= type.substring( p+1, type.length );
		type	= type.substring( 0, p );
	}

	var elm = document.createElement( type );
		if ( id )			elm.id				= id;
		if ( name )			elm.name			= name;
		if ( className )	elm.className		= className;

	if ( subtype ){    
		elm.type = subtype;
		if( html ) elm.value = html;
	} else {
		if ( html ) { elm.innerHTML = html; }
	}

	return elm;

}

function addEventHandler(element, name, observer, useCapture) {
 
    if (element.addEventListener) {
        element.addEventListener(name, observer, useCapture);
    } else if (element.attachEvent) {
        element.attachEvent('on' + name, observer);
    } else {
        return false;
    }

}