// JavaScript Document
// AJAX to call ajax.php which is a PHP file containing various MySQL interaction functions.

// a new bit to allow the calendar function to post data back to db
// this adds a 500 milisecond delay to allow the datafield innerHTML to be updated by the calendar app
// before we send it to a PHP file to update our MySQL db.

function delayedrequest(x) { 
setTimeout("sendRequest('updatedate','showDiv',document.getElementById('datefield').innerHTML," + x + ")",500);
}

// the main functions

function createXMLHttpRequest() { 
  
var ua; 
  
if(window.XMLHttpRequest) { 
try { 
ua = new XMLHttpRequest(); 
} catch(e) { 
ua = false; 
} 
} else if(window.ActiveXObject) { 
try { 
ua = new ActiveXObject("Microsoft.XMLHTTP"); 
} catch(e) { 
ua = false; 
} 
} 
return ua; 
} 

var req = createXMLHttpRequest(); 
  
function sendRequest(action,displayarea,variable1,variable2) { 
var randomnumber=Math.floor(Math.random()*1000000);
req.open('get', 'ajax.php?action=' + action + '&random=' + randomnumber + '&displayarea=' + displayarea + '&variable1=' + variable1 + '&variable2=' + variable2); 
req.onreadystatechange = handleResponse; 
req.send(null); 
} 

function makethemgo() {

if(req.readyState == 4){ 
var response = req.responseText; 
var update = new Array(); 
  
if(response.indexOf('||' != -1)) { 
update = response.split('||'); 
document.getElementById(update[0]).style.visibility="hidden";
document.getElementById(update[0]).style.position="absolute";
}
}
}


function handleResponse() { 
  
if(req.readyState == 4){ 
var response = req.responseText; 
var update = new Array(); 
  
if(response.indexOf('||' != -1)) { 
update = response.split('||'); 
document.getElementById(update[0]).innerHTML = update[1];
document.getElementById(update[0]).style.visibility="visible";
document.getElementById(update[0]).style.position="relative";
if(update[0] == "showDiv"){
setTimeout("makethemgo();",4000);
}

   if(update[2]){
   document.getElementById(update[2]).innerHTML = update[3]; 
   }
} 
} 
 
  
} 


