// JavaScript Document
var pageContent = "";
var lastQuestionAsked = "";

function AJAX_getAnswersMinibar(whoid, whatid) {
	var xmlHttp;
	var rootPath = "http://saas.byu.edu/admissionsServices/fastanswers/";
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				alert('Your browser does not support AJAX!');
			}
		}
	}
	
	var params;
	var question = document.fastanswerminiform.fastanswerminiquestionbox.value;
	document.fastanswerminiform.fastanswerminiquestionboxhidden.value = question;
	if ((question != 'Type a question here') && (question.length != 0) && (lastQuestionAsked != question)) {
		lastQuestionAsked = question;
		pageContent = document.getElementById('Content').innerHTML;
		//document.getElementById('Content').innerHTML = '<p style="font-size: 160%; padding-top: 1em; text-align: center;">Loading Answers...</p>';
		
		document.getElementById('Content').innerHTML = '<div style="margin-top: 1em; font-size: 160%; text-align: center;"><p style="color: #8495A9;">Loading Answers</p><object width="55" height="10"><param name="movie" value="' + rootPath + 'loading.swf"><embed src="' + rootPath + 'loading.swf" width="55" height="10"></embed></object></div>';
		
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				var response = xmlHttp.responseText;
				document.getElementById('Content').innerHTML = '<a href="javascript: clearMinibar();" class="clearMinibarButton">Clear Results</a><div id="faAnswers" style="padding: 1em;"><p><b>You Asked:</b> ' + question + "</p>" + response + '</div>';
				document.getElementById('fastanswerminiquestionboxhidden').value = "";
			}
		}
		
		params = 'question=' + question.replace(/ /g, '%20');
		params += '&type=minibar';
		params += '&whoid=' + document.fastanswerminiform.fastanswerwho.value;
		params += '&whatid=' + document.fastanswerminiform.fastanswerwhat.value;
		xmlHttp.open('POST',rootPath + 'fastanswer_components/getAnswers.php?whoid=' + whoid + '&whatid=' + whatid,true);
		xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlHttp.setRequestHeader('Content-length', params.length);
		xmlHttp.setRequestHeader('Connection', 'close');
		xmlHttp.send(params);
	} else {
		if (lastQuestionAsked != question) {
			alert('Please enter a valid question.');
		} else {
			document.getElementById("fastanswerminiquestionbox").value = "";
			document.getElementById("fastanswerminiquestionbox").focus();
		}
	}
}

function clearMinibar() {
	try {
		document.getElementById('Content').innerHTML = pageContent;
	} catch (e) { }
}