var send = create_XMLHttpRequest();

var advice_auto_save = 150000; // 2min30secs = 150 seconds
var advice_save_hide = 30000; // remove the message after 30seconds

var write_advice_modified = false;

init_functions[init_functions.length] = "self.setTimeout('write_advice_save_timer()', " + advice_auto_save + ");";

function FCKeditor_OnComplete( editorInstance )
{
    editorInstance.Events.AttachEvent( 'OnSelectionChange', write_advice_form_modified ) ;
}

function write_advice_form_modified()
{
	write_advice_modified = true;
}

function write_advice_unload(f)
{
	if( write_advice_modified == true )
	{
		var warning_message = "";
		warning_message  = "Warning: Are you sure you want to continue without saving the changes to your Article?\n\n";
		warning_message += "Clicking OK will allow you to continue on your way and all changes will be lost.\n\n";
		warning_message += "Clicking Cancel will save all the changes to the Article.";
		if( confirm(warning_message) == false )
		{
			write_advice_save(null);
		}
	}
}

function write_advice_save_timer()
{
	// Reload the auotsave again
	self.setTimeout('write_advice_save_timer()', advice_auto_save);

	if( write_advice_modified == false )
	{
		// don't auto save
		return;
	}

	var el = document.getElementById("autosaving");

	el.style.display = "block";
	el.innerHTML = "Auto-Saving Article...";

	write_advice_save(write_advice_save_complete);
}

function write_advice_save( finish_function )
{

	var f = document.write_advice;

	var advice_version_content = FCKeditorAPI.__Instances['advice_version_content'].GetHTML();

	var parameters = "advice_id=" + f.advice_id.value + "&advice_version_id=" + f.advice_version_id.value + "&advice_version_title=" + escape(f.advice_version_title.value) + "&advice_version_content=" + escape(advice_version_content) + "&advice_anonymous=";

	if( f.advice_anonymous.checked == true )
	{
		parameters += "1";
	}
	else
	{
		parameters += "0";
	}

	parameters += "&hash=" + escape(f.form_hash.value);

	if( send.readyState == 4 || send.readyState == 0 )
	{
		// Send data to save
		send.open('POST', "/ajax/article_autosave/", true);
		send.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		send.setRequestHeader("Content-length", parameters.length);
		send.setRequestHeader("Connection", "close");

		if( finish_function != null )
		{
			send.onreadystatechange = finish_function;
		}

		send.send(parameters);
	}
}

function write_advice_save_hide()
{
	var el = document.getElementById("autosaving");
	el.style.display = "none";
}

function write_advice_save_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");

			var el = document.getElementById("autosaving");

			// Successfully Deleted
	 		if( success.toUpperCase() == "TRUE" )
	 		{
	 			// Get vars from XML
	 			var advice_id = xml_get_field(xmldoc, "advice_id");
	 			var advice_version_id = xml_get_field(xmldoc, "advice_version_id");
	 			var hash = xml_get_field(xmldoc, "hash");

	 			var f = document.write_advice;

	 			f.advice_id.value = advice_id;
	 			f.advice_version_id.value = advice_version_id;
	 			f.form_hash.value = hash;

	 			// Change text to saved.
	 			el.innerHTML = "Article Auto-Saved";

	 			// Hide the save box.
	 			self.setTimeout('write_advice_save_hide()', advice_save_hide);

	 			write_advice_modified = false;
		   	}
		   	else
		   	{
		   		// Failure.
	 			el.innerHTML = "Error Auto-Saving your Article. Please make sure you save it by clicking on the Publish or Draft button at the bottom of the page.";
		   	}
	   	}
	}
}