// FUNCTION FOR AJAX VOTE SYSTEM
function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType)
httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject){ // if IE
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
} 
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}

var ajaxpack=new Object()
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.ajaxobj=createAjaxObj()
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=0 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
if (this.ajaxobj){
this.filetype=filetype
this.ajaxobj.onreadystatechange=callbackfunc
this.ajaxobj.open('GET', url+"?"+parameters, true)
this.ajaxobj.send(null)
}
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
ajaxpack.ajaxobj=createAjaxObj() //recreate ajax object to defeat cache problem in IE
if (this.ajaxobj){
this.filetype=filetype
this.ajaxobj.onreadystatechange = callbackfunc;
this.ajaxobj.open('POST', url, true);
this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.ajaxobj.setRequestHeader("Content-length", parameters.length);
this.ajaxobj.setRequestHeader("Connection", "close");
this.ajaxobj.send(parameters);
}
}
function processAjaxVote(){
var myajax=ajaxpack.ajaxobj;
var myfiletype=ajaxpack.filetype;
if (myajax.readyState == 4){ //if request of file completed
if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
if (myfiletype=="txt")
// SPLIT INCOMING AJAX RESPONSE
alert(divFinal);
else
var response_text = myajax.responseText;
var response_array = response_text.split(",");
var response_alert_text = response_array[0];
var response_record_id = response_array[1];
var response_vote_status = response_array[2];
var response_total_votes = response_array[3];
var response_total_value = response_array[4];
var response_max_rating = response_array[5];
var response_rating_object_width = response_array[6];
//
function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;  
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
// EDIT THE VALUE BELOW IF YOU CHANGE THE MAXIMUM RATING
var max_rating = response_max_rating;
//
var current_rating = number_format(response_total_value/response_total_votes,2,'.','');
// RESIZE VOTES DONE DIV
var rating_width = number_format(response_total_value/response_total_votes,2,'.','')*response_rating_object_width;
//
var vote_stars_div = document.getElementById('vote_stars_div_'+response_record_id);
vote_stars_div.style.width = rating_width+'px';
// REMOVE EACH INDIVIDUAL STAR *BUTTON*
var vote_div = '';
for (d=1; d <= max_rating; d++){
vote_div = document.getElementById('vote_div_'+response_record_id+'_'+d);
vote_div.innerHTML = '';
}
// 
// PLURAL TENSE?
if (response_total_votes > 1){
	var vote_tense = 'votes';
} else {
	var vote_tense = 'vote';
}
var vote_text_div = document.getElementById('vote_text_div_'+response_record_id);
vote_text_div.innerHTML = '('+response_total_votes+' '+vote_tense+' cast)';
// UPDATE MAIN RATING DIV WITH NEW ALT AND TITLE
var ratingblock_div = document.getElementById('ratingblock_'+response_record_id);
ratingblock_div.title = 'Currently '+current_rating+'/'+max_rating;
// SHOW ALERT
alert(response_alert_text);
//
}
}
}