var send = create_XMLHttpRequest();
var watch_action = "";
var member_watch_action = "";

function change_watch(article_id, action)
{
	// Set the action to what is passed if it is not already set on the page.
	if( watch_action.length == 0 )
	{
		watch_action = action;
	}

	if( watch_action == "add")
	{
		add_to_watch_list(article_id);
	}
	else
	{
		delete_from_watch_list(article_id);
	}
}

function change_member_watch(member_id, action)
{

	// Set the action to what is passed if it is not already set on the page.
	if( member_watch_action.length == 0 )
	{
		member_watch_action = action;
	}

	if( member_watch_action == "add")
	{
		add_to_member_watch_list(member_id);
	}
	else
	{
		delete_from_member_watch_list(member_id);
	}
}



function add_to_favourites( advice_id )
{
	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data to Advice Add to Favourites.
		send.open('GET', '/ajax/advice_favourite_add/?advice_id=' + advice_id, true);
		send.onreadystatechange = add_to_favourites_complete;
		send.send(null);
	}
}

function delete_favourite( advice_id )
{
	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data to Advice Add to Favourites.
		send.open('GET', '/ajax/advice_favourite_delete/?advice_id=' + advice_id, true);
		send.onreadystatechange = delete_favourite_complete;
		send.send(null);
	}

}

function add_to_favourites_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 Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			// Change the A tag text and the action
	 			html_element = document.getElementById('html_favourite');
	 			html_element.innerHTML = xml_get_field(xmldoc, 'html_favourite');
		   	}
	   	}
	}
}

function delete_favourite_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 Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			html_element = document.getElementById('html_favourite');
	 			html_element.innerHTML = xml_get_field(xmldoc, 'html_favourite');
		   	}
	   	}
	}
}



function add_to_watch_list(article_id)
{
	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data to Advice Add to Favourites.
		send.open("GET", "/ajax/?type=article_add_to_watch_list&article_id=" + article_id, true);
		send.onreadystatechange = add_to_watch_list_complete;
		send.send(null);
	}
}

function delete_from_watch_list(article_id)
{
	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data to Advice Add to Favourites.
		send.open("GET", "/ajax/?type=article_delete_from_watch_list&article_id=" + article_id, true);
		send.onreadystatechange = delete_from_watch_list_complete;
		send.send(null);
	}

}

function add_to_watch_list_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 Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			// Get vars from XML
	 			var article_id = xml_get_field(xmldoc, "article_id");

	 			a_tag = document.getElementById('change_watch');
	 			a_tag.innerHTML = "Remove from Watch List";
	 			watch_action = "delete";
		   	}
	   	}
	}
}

function delete_from_watch_list_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 Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			// Get vars from XML
	 			var article_id = xml_get_field(xmldoc, "article_id");

	 			a_tag = document.getElementById('change_watch');
	 			a_tag.innerHTML = "Add to Watch List";
	 			watch_action = "add";
		   	}
	   	}
	}
}



function add_to_member_watch_list(member_id)
{
	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data
		send.open("GET", "/ajax/?type=member_add_to_watch_list&watch_member_id=" + member_id, true);
		send.onreadystatechange = add_to_member_watch_list_complete;
		send.send(null);

	}
}

function delete_from_member_watch_list(member_id)
{

	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data
		send.open("GET", "/ajax/?type=member_remove_from_watch_list&watch_member_id=" + member_id, true);
		send.onreadystatechange = remove_from_member_watch_list_complete;
		send.send(null);

	}

}

function add_to_member_watch_list_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 Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			a_tag = document.getElementById('change_member_watch_link');
	 			a_tag.innerHTML = "Remove member from watch list";
	 			member_watch_action = "delete";
		   	}
	   	}
	}
}

function remove_from_member_watch_list_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 Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			a_tag = document.getElementById('change_member_watch_link');
	 			a_tag.innerHTML = "Add member to watch list";
	 			member_watch_action = "add";
		   	}
	   	}
	}
}



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');
		   	}
	   	}
	}
}



var vote_stats_1_display = 'none';
var vote_stats_2_display = 'none';
var vote_stats_3_display = 'none';
var vote_stats_4_display = 'none';
var vote_stats_5_display = 'none';

function show_votes( vote_num )
{
	var div = document.getElementById("vote_stats_" + vote_num);

	var display = eval("vote_stats_" + vote_num + "_display");

	if( display == 'none' )
	{
		div.style.display = 'block';

		eval("vote_stats_" + vote_num + "_display = 'block';");
	}
	else
	{
		div.style.display = 'none';

		eval("vote_stats_" + vote_num + "_display = 'none';");
	}
}
