// Send the information to the database
var req;
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;
var count = 0;
var winpops;

function popWindow(URL,name,options){
	//options can be width=X,height=Y,toolbar,location,directories,status,scrollbars,menubar,resizable
	if(options == '') options=' width=610, height=350 ';
	wp = window.open(URL,name,options);
	if(wp) winpops = wp;

}
function closeWindow(){
	winpops.close();
}
function InitializeTimer()
{
	// Set the length of the timer, in seconds
	secs = 3
	StopTheClock()
	StartTheTimer()
}

function StopTheClock()
{
	if(timerRunning)
	clearTimeout(timerID)
	timerRunning = false
}

function StartTheTimer()
{
	if (secs==0)
	{
		//StopTheClock()
		// Here's where you put something useful that's
		// supposed to happen after the allotted time.
		// For example, you could display a message:
		count++;
		updateDatabase('foo');
		secs = 20;

	}

	secs = secs - 1;
	if( secs > 0 ) {
		self.status = secs;
	} else {
		self.status = 'Sync';
	}
	timerRunning = true;
	timerID = self.setTimeout("StartTheTimer()", delay);
}


function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		// req.onreadystatechange = processReqChange;
		req.open("GET", url, false);
		req.send(null);
		// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			// req.onreadystatechange = processReqChange;
			req.open("GET", url, false);
			req.send();
		}
	}
	if( req.responseText == 'FFFFFF' ){
		StopTheClock();
		window.location.href = "https://nanologin.nrl.navy.mil/logout.php/"; //	processNoSession();  //
		winpops.close();
	} else {
		dA = req.responseText.split('+++');
		//window.alert('length:' + dA.length);
		for(i=0;i<dA.length;i++){
			//window.alert(dA[i]);

			gs = dA[i].split(':::');
			var re = new RegExp('\n','gi');
			var tag = gs[0].replace(re, '');

			//window.alert('calling fillIt with tag:' + tag + ' and data:' + gs[1]);

			fillIt(tag,gs[1]);
		}
	}
}
function fillIt(tagID,data){
	//answer = confirm("---" + tagID + "---");
	//alert('fillIt ' + document.getElementById(tagID).getAttribute('id'));
	document.getElementById(tagID).innerHTML = data;
	//window.alert('fillIt: DONE!');
}

function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
		} else {
			alert("There was a problem retrieving the XML data:\n" +
			req.statusText);
		}
	}
}
function trapLoginForm( form ){
	url = 'https://atriptoparadise.com/updateAgent.php?ACTION=SETMSNAME&NEWNAME=' + form.NAME.value + '&SETID=' + form.SETID.value;
	loadXMLDoc( url );
	return false;
}

function popDate( url, id ){
	document.getElementById( id ).className = 'visible';
	loadXMLDoc( url );
}
function unPopDate( id ){
	document.getElementById( id ).className = 'hidden';
}

function updateAgent( url ){
	loadXMLDoc( url );
}
function popShow( id ){
	document.getElementById( id ).className = 'visible';
}

/// this crap should be in WB/ADMIN/foo.js but am too lazy to set that up right now

function dbTypeSelect(){

	if( document.newTableForm.TABLETYPE.value == "standAlone" ){
		document.getElementById( 'core5Object' ).className = 'hidden';
	} else {
		document.getElementById( 'core5Object' ).className = 'visible';
	}
}

