function ajax_request(url)
{
	url += '&r=' +  Math.random();
	var httpRequest;
	if( window.XMLHttpRequest ){
		httpRequest = new XMLHttpRequest();
		if(httpRequest.overrideMimeType ) {
			httpRequest.overrideMimeType('application/json');
		}
	}else if( window.ActiveXObject ){
		try{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){/*nothing*/}
		}
	}

	if( httpRequest ){
		httpRequest.onreadystatechange = function(){ feedback_handler(httpRequest) };
		httpRequest.open('GET', url, true);
		httpRequest.send('');
	}else{
		ajax_error("Error: httpRequest failed");
	}
}

function ajax_error(error_text){
	alert(error_text);
	feeback_submitted = false;
}

function feedback_handler( httpRequest ) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			var jsonRaw = httpRequest.responseText;
			jsonRaw = jsonRaw.replace(/(^[^{]*|[^}]*$)/, '');
			var obj = eval("(" + jsonRaw + ")");

			votes 		= obj.votes;
			votes_most	= obj.votes_most;
			feedback_draw_box();
		}
		else{
			ajax_error("Error: Unable to show vote results");
		}
	}
}

feeback_submitted = false;

function feedback_submit()
{
	if( feeback_submitted ){
		return false;
	}
	var i;
	var vote1 = false;
	var vote0 = false;
	var rating1 = document.f_rating.rating1;
	var rating0 = document.f_rating.rating0;
	for(i=0; i < rating1.length; i++){
		if( rating1[i].checked ){
			vote1 = rating1[i].value;
			break;
		}
	}
	for(i=0; i < rating0.length; i++){
		if( rating0[i].checked ){
			vote0 = rating0[i].value;
			break;
		}
	}
	if( !vote1 &&!vote0 ){
		return false;
	}

	feeback_submitted = true;

	var URL = '/includes/likeit-AJAX.php?u=' + usn;
	URL += '&v1=' + vote1 + '&v0=' + vote0
	ajax_request(URL);
}

function feedback_draw_box()
{
	var i,barUnitW;
	barUnitW = start_barUnitW; //max bar width / 100 percent
	var headings = start_headings;
	var labels = start_labels;
	var out = '';
	out += '<div class="youlikeset">';
	out += '<table cellspacing="0" cellpadding="0" border="0">';
	for(i = 0; i < votes.length; i++){
		if( i == 3 ){
			out += '<tr>';
				out += '<td colspan="3">';
					out += '<div style="height:5px;"></div>';
				out += '</td>';
			out += '</tr>';
		}
		out += '<tr>';
			if( i == 0 || i == 3 ){
				out += '<th rowspan="3">';
					out += (i==0)? headings[0]:headings[1];
				out += '</th>';
			}
			out += '<td>';
				out += labels[i];
			out += '</td>';
			out += '<td>';
				out += '<div class="youlike';
				if( votes_most[i] ){
					out += 'most';
				}
				out += '" style="width:';
				out += Math.floor(barUnitW * votes[i]);
				out += 'px;"></div>';
				out += votes[i] + '%';
			out += '</td>';
		out += '</tr>';
	}
	out += '</table></div>';

	document.getElementById('feedback').innerHTML = out;
}
