var questionId = 0;

function StartAutoSave()
{
	setTimeout( "SaveQuestion()" , 60000 );
}

function SaveQuestion( timeout )
{	
	if( timeout == undefined )
	{
		timeout = true;
	}
	
	if( objQuestionId = document.getElementById('questionId') )
	{
		qId = document.getElementById('questionId').value;
		
		if( qId != 0 )
		{
			questionId = qId;
		}
	}
	
	var categoryId = 0;
	if( !( objCategoryId = document.getElementById('question_category_parent_id') ) ) 
	{		
		return false;	
	}
	
	categoryId = objCategoryId.options[ objCategoryId.selectedIndex ].value;
	
	if( !( title = document.getElementById('title') ) ) { return false;	}
	if( !( text = document.getElementById('text') ) ) { return false;	}
	if( !( correctanswer = document.getElementById('correctanswer') ) ) { return false;	}
	if( !( incorrect1 = document.getElementById('incorrect1') ) ) { return false;	}
	if( !( incorrect2 = document.getElementById('incorrect2') ) ) { return false;	}
	if( !( incorrect3 = document.getElementById('incorrect3') ) ) { return false;	}
	if( !( factoid = document.getElementById('factoid') ) ) { return false;	}
	if( !( source = document.getElementById('source') ) ) { return false;	}
		
	var xml = "<" + "?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
	
	xml += "<request>\n";		
	xml += "\t<questionId>" + questionId + "</questionId>\n";			
	xml += "\t<question_category_id>" + categoryId + "</question_category_id>\n";			
	xml += "\t<title><![CDATA[" + title.value + "]]></title>\n";
	xml += "\t<text><![CDATA[" + text.value + "]]></text>\n";
	xml += "\t<correctanswer><![CDATA[" + correctanswer.value + "]]></correctanswer>\n";
	xml += "\t<incorrect1><![CDATA[" + incorrect1.value + "]]></incorrect1>\n";
	xml += "\t<incorrect2><![CDATA[" + incorrect2.value + "]]></incorrect2>\n";
	xml += "\t<incorrect3><![CDATA[" + incorrect3.value + "]]></incorrect3>\n";
	xml += "\t<factoid><![CDATA[" + factoid.value + "]]></factoid>\n";
	xml += "\t<source><![CDATA[" + source.value + "]]></source>\n";
	
	xml += "</request>";
	
	ajax = new AjaxClient();
	ajax.requestFile = "/shared/includes/ajax/Objects/SubmitAQuestion.php";
	ajax.timeout = timeout;
	
	ajax.callbackFunction = function( objXml, strXml )
	{
		var success = ajax.GetXMLNodeValue( objXml, "success" );	
			
		if( success == "false" )
		{
			var message = ajax.GetXMLNodeValue( objXml, "message" );   			
			document.getElementById('auto-save-status').innerHTML = message;
		}
		else
		{
			questionId = ajax.GetXMLNodeValue( objXml, "questionId" );
			
			var d = new Date();
			var hours = d.getHours();
			var minutes = d.getMinutes();
			var amPm = "AM";
			
			if( hours > 12 )
			{
				hours -= 12;
				amPm = "PM";
			}
			
			if( minutes < 10 )
			{
				minutes = "0" + minutes;
			}
			
			var timeString = hours + ":" + minutes + " " + amPm ;

			document.getElementById('auto-save-status').innerHTML = "Last saved @ " + timeString;
			
			if( this.timeout )
			{
				setTimeout( "SaveQuestion()" , 60000 );
			}
		}

	}
	
	document.getElementById('auto-save-status').innerHTML = 'Saving...';
	
	ajax.SendRequest( xml );
	
}