var send = create_XMLHttpRequest();

function submit_article_vote(article_id, vote_rating)
{
	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data
		send.open("GET", "/ajax/advice_vote/?type=advice_vote&advice_id=" + article_id + "&vote_rating=" + vote_rating, true);
		send.onreadystatechange = submit_article_vote_complete;
		send.send(null);
	}
}

function submit_article_vote_complete()
{

	if (send.readyState == 4)
	{
		// Response code was 200, OK!
		if( send.status == 200 )
		{
			// Get XML data
	 		var xmldoc = send.responseXML;
			var success = xml_get_field(xmldoc, "success");

			// Successfully Voted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			var vote_rating = xml_get_field(xmldoc, "vote_rating");
	 			var advice_rating_text = xml_get_field(xmldoc, "advice_rating_text");

	 			if( parseInt(vote_rating) > 0 )
	 			{
	 				// Pass through each of the 5 checkbox on the article page.
		 			for( checkbox_number = 1; checkbox_number <= 5; checkbox_number++ )
		 			{
		 				var vote_box = document.getElementById('vote_rating_' + checkbox_number);

		 				// check only the one that the user selected.
		 				if( checkbox_number == vote_rating )
		 				{
		 					vote_box.checked = true;
		 				}
		 				else
		 				{
		 					vote_box.checked = false;
		 				}
		 			}
	 			}

	 			// Unhide the 'leave helpful comment' text
	 			var el = document.getElementById("advice_has_voted");
	 			el.style.display = "inline";

	 			// Update all the advice score texts
				var el_list = getElementsByClassName(document, '*', 'advice_rating_text');
				for( i = 0; i < el_list.length; i++ )
				{
					el_list[i].innerHTML = advice_rating_text;
				}
		   	}
		   	else
		   	{
		   		alert('There was a error adding your vote to the article, please try again later');
		   	}
	   	}
	}
}