/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttpSB1 = false;
var xmlHttpSB2 = false;
var cid;
var mbpage;
var memberid;
var mprevresponse;
var wprevresponse;
var prevsplit;
var prevsnippet;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttpSB1 = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttpSB1 = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttpSB1 = false;
  }
}
@end @*/

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttpSB2 = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttpSB2 = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttpSB2 = false;
  }
}
@end @*/

if (!xmlHttpSB1 && typeof XMLHttpRequest != 'undefined') {
  xmlHttpSB1 = new XMLHttpRequest();
}

if (!xmlHttpSB2 && typeof XMLHttpRequest != 'undefined') {
  xmlHttpSB2 = new XMLHttpRequest();
}

// getElementById Special to handle quirky browsers
// most will use getElementById()
function getElementById_sSB(id){
  var obj = null;
  if(document.getElementById){
  /* Prefer the widely supported W3C DOM method, if available:- */
  obj = document.getElementById(id);
  }else if(document.all){   /* Branch to use document.all on document.all only browsers. Requires that IDs are unique to the page and do not coincide with NAME attributes on other elements:- */
  obj = document.all[id];
  }
  /* If no appropriate element retrieval mechanism exists on this browser this function always returns null:- */
  return obj;
  }

function getNewShoutBoxData(tmemberid, first) {

	memberid = tmemberid;
	
  // Build the URL to connect to
  var currentDate = new Date()
  var d1 = currentDate.getDate()
  var d2 = currentDate.getSeconds()
  var d3 = currentDate.getMinutes()
  var d4 = currentDate.getHours()
  var d5 = currentDate.getDay()
  var d6 = currentDate.getMonth()
  var d7 = currentDate.getYear()
	

  //var url = "/message_board/newshoutbox/sb.php?rand=" + (Math.floor(Math.random()*99999999)) + (Math.floor(Math.random()*99999999)) + (Math.floor(Math.random()*99999999)) + (Math.floor(Math.random()*99999999));
	var url = 'http://www.kingwood.com/message_board/newshoutbox/sb.txt?' + d1 + d2 + d3 + d4 + d5 + d6 + d7 + (Math.floor(Math.random()*99999999));
  
  // Open a connection to the server
  xmlHttpSB1.open("GET", url, true);

  // Setup a function for the server to run when it's done
  xmlHttpSB1.onreadystatechange = updateShoutBox;

  // Send the request
  xmlHttpSB1.send(null);
	}

function getWhoIsOnline() {

  // Build the URL to connect to
  var currentDate = new Date()
  var d1 = currentDate.getDate()
  var d2 = currentDate.getSeconds()
  var d3 = currentDate.getMinutes()
  var d4 = currentDate.getHours()
  var d5 = currentDate.getDay()
  var d6 = currentDate.getMonth()
  var d7 = currentDate.getYear()
	
  //var url = "/message_board/newshoutbox/sb.php?rand=" + (Math.floor(Math.random()*99999999)) + (Math.floor(Math.random()*99999999)) + (Math.floor(Math.random()*99999999)) + (Math.floor(Math.random()*99999999));
	var url = 'http://www.kingwood.com/message_board/newshoutbox/online.txt?' + d1 + d2 + d3 + d4 + d5 + d6 + d7 + (Math.floor(Math.random()*99999999));
	
  // Open a connection to the server
  xmlHttpSB2.open("GET", url, true);

  // Setup a function for the server to run when it's done
  xmlHttpSB2.onreadystatechange = updateWhoIsOnline;

  // Send the request
  xmlHttpSB2.send(null);
}

function invisibleGetWhoIsOnline() {

	//var url = "/message_board/newshoutbox/get_who_is_online.php?rand=" + (Math.floor(Math.random()*99999));
  
  // Open a connection to the server
  //xmlHttpSB2.open("GET", url, true);

  // Setup a function for the server to run when it's done
  //xmlHttpSB2.onreadystatechange = invisibleUpdateWhoIsOnline;

  // Send the request
  //xmlHttpSB2.send(null);
}

function updateShoutBox() {
	var newstuff;
  if (xmlHttpSB1.readyState == 4) {
    var mresponse = xmlHttpSB1.responseText;
		if(mresponse != mprevresponse) {
			var splitResults = mresponse.split(prevsnippet);
			//alert('SNIPPET: '+prevsnippet);
			//alert('MATCH?: '+mresponse.split(prevsnippet));
			//alert('SPLIT1: '+splitResults[0]);
			//alert('SPLIT2: '+splitResults[1]);
			
			if(splitResults[1])  {
				if(splitResults[1] != prevsplit)  {
					updateShoutBoxDisplay(splitResults[1]);
					prevsplit = splitResults[1];
					prevsnippet = mresponse.substr(mresponse.length-200);
					}
				}
			else  {
				updateShoutBoxDisplay(splitResults[0]);
				prevsnippet = splitResults[0].substr(splitResults[0].length-200);
				}
		  }
		mprevresponse = mresponse;
    setTimeout('getNewShoutBoxData(\''+memberid+'\', 0)', 1500);
	  }
  }

function updateWhoIsOnline() {
  if (xmlHttpSB2.readyState == 4) {
    var wresponse = xmlHttpSB2.responseText;
		if(wresponse != wprevresponse) {
     updateWhoIsOnlineDisplay(wresponse);
		 }
		 wprevresponse = wresponse;
		 setTimeout('getWhoIsOnline()', 3500);
		}
}

function invisibleUpdateWhoIsOnline() {
  if (xmlHttpSB2.readyState == 4) {
    var wresponse = xmlHttpSB2.responseText;
		if(wresponse != wprevresponse) {
     // do nothing...
		 }
		 wprevresponse = wresponse;
		 setTimeout('invisibleGetWhoIsOnline()', 3500);
		}
}

function scrollAgain()  {
	IFRAME_TEXT.scrollTo(0,document.getElementById("IFRAME_TEXT").contentWindow.document.body.scrollHeight);
	}
