//**************************************************************
// Generates a "random" number between 1 and highest dB count
//**************************************************************
function rand(){
	minN = 1;
	maxN = 189;
	//maxN = document.RAND.cnt.value
	num = (Math.round((Math.random()*maxN)+minN))
	return num;
}

//**************************************************************
// Calls the pullData function every 7 seconds
//**************************************************************
function fireAgain(){
	if(timer != undefined){
		clearTimeout(timer);
	}
	pullData();
}
	
function setResetTimer(){
	timer = setTimeout("pullData();",30000);
}

//**************************************************************
// Resent the timer and make the xmlhttpPost request
//**************************************************************
function pullData(){
	xmlhttpPost('qry_did_you_know.php?id=', rand(), 'displayResult');
	setResetTimer();
}

//**************************************************************
// Build the xmlhttpPost request 
//**************************************************************
function xmlhttpPost(strSubmitURL, strSubmitContent, strResultFunc) {
	
	var xmlHttpReq = false;
	var strResponse = '';
	
	// Create new XMLHTTPRequest object
	// ----------------
	// Mozilla
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
		//xmlHttpReq.overrideMimeType('text/xml'); // This is actually causing syntax errors in versions 1.5.0.8
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open('POST', strSubmitURL + strSubmitContent, true);
	xmlHttpReq.setRequestHeader("Content-Type", "text/xml");
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			strResponse = xmlHttpReq.responseText;
			
			switch (xmlHttpReq.status) {
				// Page-not-found error
				case 404:
					alert('Error: Not Found. The requested URL ' + strSubmitURL + ' could not be found.');
					break;
				//Display results in a full window for server-side errors
				case 500:
					handleErrFullPage(strResponse);
					break;
				default:
				//Call JS alert for generated error or debug messages
					if (strResponse.indexOf('Error:') > -1 || strResponse.indexOf('Debug:') > -1) {
						handleErrFullPage(strResponse);
					}
					//Call the desired result function
					else {
						eval(strResultFunc + '(strResponse);');
					}
					break;
			}
		}
	}
	xmlHttpReq.send(strSubmitContent);
}

//**************************************************************
// Handle Any Errors
//**************************************************************
function handleErrFullPage(strIn) {

	var errorWin;

	// Try creating new window and displaying error
	try {
		// Open window
		errorWin = window.open('', 'errorWin');
		errorWin.document.body.innerHTML = '<pre>' + strIn + '</pre>';
	}
	// If pop-up gets blocked, inform user
	catch(e) {
		alert('An error occurred, but the error message cannot be' +
			' displayed because of your browser\'s pop-up blocker.\n' +
			'Please allow pop-ups from this Web site.');
	}
}

//**************************************************************
// Parse out the data into the display
//**************************************************************
function displayResult(strIn) {
	var strContent = '';
	strResponseArray = strIn.split('||');
	
	strContent += strResponseArray[1]
	strContent += '<a id="more" href="javascript:void(0);" onClick="fireAgain();">more &raquo;</a></div>'
	
	document.getElementById('dyk').innerHTML = strContent;
}
