// flag ajax support
ajax = ((ie && !mac) || mz || op) ? 1:0;

// begin generic ajax communication module
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) {
			xmlhttp = false;
		}
	}
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object
// end ajax module






// customize for each use
function handleHttpResponse() {
  if (http.readyState == 4) {  // meaning the request is complete
    var response = http.responseText;
   
    if (response.indexOf('invalid') == -1) {
		if (response.indexOf("?xml") != -1) {
			
			if (http.responseXML) {	// did we get a valid xml document?	
				var xmlDocument = http.responseXML; 
			
				if (xmlDocument) 	
				formatResults(xmlDocument);			
			}
		}	
		else {
			// Split the comma delimited response into an array
			results = http.responseText.split(",");		
		}
	}
	
	// no matter what the outcome, we stop working
	loading = false;   
  }
}




// customize for each use
function acknowledgeHttpResponse() {
  if (http.readyState == 4) {  // meaning the request is complete
    var response = http.responseText;
	//alert(response);
	// no matter what the outcome, we stop working
	sending = false;   
  }
}





if (url == "undefined" || url == "")
	url = "http://www.smartdesignclients.com/edg/acs/php/getEvents.php?module="; // The server-side script

// frequency divided by 1000 = seconds
var pollFrequency = 30000;

var loading = false;
function retrieveEvents(module) {
	if (!loading && http) {
		//if (module=="access") alert('get events ' + module);
		if (ajax && module!="") {
		
			var ids = "|";
			ar = eval("array_"+module);
			for (i=0; i<ar.length; i++) {
				if (ar.length!=0) {
				ids += ar[i].uqid + "|";
				}
			}
			
			now = new Date();
			min = now.getMinutes();
			sec = now.getSeconds();
			unique = ""+min+sec;
			
			//document.getElementById("loading").style.display="inline";
			http.open("GET", url + escape(module) + "&ids=" + escape(ids) + "&unique=" + escape(unique), true);
		
			http.onreadystatechange = handleHttpResponse;
			loading = true;
			http.send(null);
		}
	}
	
	
	
	if (module=="events") {
		setTimeout("pollEvents()",pollFrequency);
	}
	else {	
		header = "header_"+module;
		// if this module is active on this page	
		if (document.getElementById(header)) {
			if (document.getElementById(header).style.display!="none") {
				setTimeout("retrieveCycle('"+module+"')",pollFrequency);
			}
		}
	}	
}





var sending = false;
function updateServer(uqid,action,module) {

	if (!sending && http) {
		if (ajax) {
		
			now = new Date();
			min = now.getMinutes();
			sec = now.getSeconds();
			cache = ""+min+sec;
		
			if(module!=false)
			http.open("GET", sendurl + "uqid=" + escape(uqid) + "&action=" + escape(action) + "&module=" + escape(module)  + "&cache=" + escape(cache), true);
			
			else
			http.open("GET", sendurl + "uqid=" + escape(uqid) + "&action=" + escape(action) + "&cache=" + escape(cache), true);

		
			http.onreadystatechange = acknowledgeHttpResponse;
			sending = true;
			http.send(null);
		
		
		}
	}
	
	// keep trying until the channel is open
	else if (http) {
	
		if (module!=false)
		setTimeout("updateServer('"+uqid+"', '"+action+"', '"+module+"')",1000);
		else
		setTimeout("updateServer('"+uqid+"', '"+action+"')",1000);
	}

}



function setListener(reader) {
	if (ajax && !sending && !loading) {
		
		now = new Date();
		min = now.getMinutes();
		sec = now.getSeconds();
		cache = ""+min+sec;

		http.open("GET", readerurl + "reader=" + escape(reader) + "&cache=" + escape(cache), true);
		
		http.onreadystatechange = readerHttpResponse;
		http.send(null);
		sending = true;
		
	}
	else {
		if (!abort)
		setTimeout("setListener('"+reader+"')",2000);
	}	
}

function readerHttpResponse() {
  if (http.readyState == 4) {  // meaning the request is complete
    var response = http.responseText;
	if (!isNaN(response) && !abort) {
		sim(response);
	}
	else setTimeout("setListener('" + response + "')",2000);
	
	// no matter what the outcome, we stop working
	sending = false;   
  }
}













pause=false;
function updateTime() {
	if (pause) return;
	
	time1 = document.getElementById("lastupdated");
	time2 = document.getElementById("lastupdated_repeat");
		
	var now = new Date();
	var day  = now.getDate();
	var month = now.getMonth() + 1;
	var year = now.getFullYear();
	year+=""
	year = year.substring(2);
		
	part = "am";	
	var	hrs = now.getHours();
	var	m = now.getMinutes();
	var min = (m < 10) ? '0' + m : m;
	
	
	if (hrs==0) hrs = 12;
	
	if (hrs>12) {
		hrs-= 12;
		part = "pm";
	}
		
	var string = "Updated " + month + "/" + day + "/" + year + " " + hrs + ":" + min + part + " PST";
	
	if (time1) {
		cur = time1.innerHTML;
		if (cur != string) {
			time1.innerHTML = string;
			if (time2) time2.innerHTML = string;
		}
	}
	
}




function formatResults(xmlDoc) {
	updateTime();

	// here's the specific information
	var module = xmlDoc.getElementsByTagName('module').item(0).firstChild.data;
	//alert(module);
	var uqid = xmlDoc.getElementsByTagName('uqid');
	
	var date = xmlDoc.getElementsByTagName('date');
	var time = xmlDoc.getElementsByTagName('time');			
	
	var event = xmlDoc.getElementsByTagName('event');			
	var action = xmlDoc.getElementsByTagName('action');
	var reason = xmlDoc.getElementsByTagName('reason');
	
	var person = xmlDoc.getElementsByTagName('person');
	var group = xmlDoc.getElementsByTagName('group');	
	var operator = xmlDoc.getElementsByTagName('operator');
	
	var door = xmlDoc.getElementsByTagName('door');			
	var building = xmlDoc.getElementsByTagName('building');
	
	var filename = xmlDoc.getElementsByTagName('filename');
	var path = xmlDoc.getElementsByTagName('path');			
	
	var nextpoll = xmlDoc.getElementsByTagName('nextpoll');
	if (nextpoll.item(0)) pollFrequency = nextpoll.item(0).firstChild.data	
	else pollFrequency = 30000;
							
	// number of total items
	var length = uqid.length;
	var output ="";
			
			
			
				
	if (length>0) {
	
	
	for (i=0; i<length; i++) {
	
			nextpoll=theuqid=thedate=thetime=theperson=thegroup=theoperator=theevent=theaction=thedoor=thebuilding=thefilename=thepath=thereason="";
			
			if (uqid.item(i)) theuqid = uqid.item(i).firstChild.data;
			
			if (date.item(i)) thedate = date.item(i).firstChild.data
			if (time.item(i)) thetime = time.item(i).firstChild.data
			if (action.item(i)) theaction = action.item(i).firstChild.data
			
			if (person.item(i)) theperson = person.item(i).firstChild.data
			if (group.item(i)) thegroup = group.item(i).firstChild.data			
			
			if (operator.item(i)) theoperator = operator.item(i).firstChild.data
			if (event.item(i)) theevent = event.item(i).firstChild.data
			if (door.item(i)) thedoor = door.item(i).firstChild.data
			
			if (building.item(i)) thebuilding = building.item(i).firstChild.data
			if (filename.item(i)) thefilename = filename.item(i).firstChild.data;
			if (path.item(i)) thepath = path.item(i).firstChild.data;					
		
			if (reason.item(i)) thereason = reason.item(i).firstChild.data
			
		
		// little bit of client-side processing... 
		// this might go server-side later...
		if (theperson.length>0 && theperson.indexOf(")") < 0) {
			namearray = theperson.split(" ");
			thesort = namearray.pop();
		}
		else thesort = "~";
		
		if (thebuilding.length>0) {
			thebuilding = thebuilding.replace(/Building/,"Bldg.");
		}
		
		// end client-side processing
		
					
		switch(module) {
		
			case "recent":
			temp_recent[temp_recent.length] = new recentitems(theuqid, thedate, thetime, theaction, theperson, theoperator);	
			break;	
			
			case "access":
			temp_access[temp_access.length] = new accessitems(theuqid, thedate, thetime, theevent, thesort, theperson, thedoor + ", " + thebuilding);	
			break;					
			
			case "report":
			temp_report[temp_report.length] = new reportitems(theuqid,thefilename,thepath);
			break;
				
			case "hardware":
			temp_hardware[temp_hardware.length] = new hardwareitems(theuqid, thedate, thetime, theevent,thereason,thedoor + ", " + thebuilding);	
			break;						

			case "notify":
			temp_notify[temp_notify.length] = new notifyitems(theuqid, thedate, thetime, theevent, thesort, theperson, thedoor + ", " + thebuilding);	
			break;
		
			case "events":
			temp_events[temp_events.length] = new eventitems(theuqid, thedate, thetime, thedoor, thebuilding, theevent, thereason, thesort, theperson, thegroup); 
			break;
			
			default:
			break;
		
		}
	}




			
	if (module=="events") {
	
		//alert(temp_events.length);
		tbody = document.getElementsByTagName("TBODY");
		for (i=0; i<tbody.length; i++) {
			if (tbody[i].id)
				addEvent(tbody[i].id);
		}
	}
	else
	newItem(module);





}

}































