// **********************************************
// Asynchronous JavaScript and XML
// written by James Campbell of Entacom Computers
// with much help from fleshTH @ #php on SwiftIRC
// e-mail: james@raztech.com.au
// **********************************************

// Function:	ajaxRequestObject()
// Use:			Create our AJAX browser object - nothing AJAX will work without this
// Reference: 	http://rajshekhar.net/blog/archives/85-Rasmus-30-second-AJAX-Tutorial.html
function ajaxRequestObject() {
	var ajaxObject;  // The variable that makes Ajax possible!
	
	try{
		// Try creating object for Opera 8.0+, Firefox, Safari
		ajaxObject = new XMLHttpRequest();
	} catch (e){
		// If not using any of the above, try creating the object for Internet Explorer users
		try{
			ajaxObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser does not support AJAX. This page cannot function correctly.");
				return false;
			}
		}
	}
	return ajaxObject;
}

// Function:	ajaxFillActivities()
// Use:	 		This function does a number of things, all in the 1 hit;
// 				First - 	Adds the data from the "add_activities" <div> tag on page rm_jsa.php to the database table: tblJSA_Activities
//				Second - 	Refreshes the main "activity" drop down box on page rm_jsa.php
//				Third - 	Performs a loop looking for additional appended "activity_#" drop down boxes, and clones the <option> elements
//							of the primary "activity" drop down box.

function ajaxFillActivities(){
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = eval(ajaxRequest.responseText);
			var oSelect = document.getElementById("activity")
			oSelect.options.length = 0 // Clear the existing options of the 'activity' select box.
			for (var i = 0;i<returnData.length;i++) {
				// Fill our main "activity" select box with new options from the data returned from the remote call.
    			oSelect.options[oSelect.options.length] = new Option(returnData[i].jsa_activitieslistTitle, returnData[i].jsa_activitieslistID);
			}
			
			// The following code resets all appended "activity_#" fields and resets them with the same <option> elements as the primary "activity" field
			var options = oSelect.options
			var selects = document.getElementsByTagName("select")
			for (var j = 0;j<selects.length;j++) {
				if (selects[j].id.indexOf("activity_") != -1) {
					var s = selects[j]
					s.options.length = 0
					for (var i = 0;i<options.length;i++) {
						// Non-IE browsers: Try using cloneNode to fill our options out - effective and clean way of replicating the <option> elements.
						try {
							s.options[i] = options[i].cloneNode(true)
						}
						// Internet Explorer: If cloneNode fails, catch the error and use another method (not preferred) to fill our options boxes out.
						catch (err) {
							s.options[i] = new Option(options[i].innerText,options[i].value)
						}
					}
				}
			}
		}
	}
	var data = document.getElementById("jsa_activitieslistTitle").value;
	var queryString = "?action=add_activity&title=" + data;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}


// Function:	ajaxAddMeasure(activityID)
function ajaxAddHazard(activityID){
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	var data = document.getElementById("jsa_hazardslistTitle").value;	// Retrieve the title of the Hazard from the jsa_hazardsTitle input field on rm_jsa.php
	
	var activity = document.getElementById("jsa_hazardslistActivity").value;	// Retrieve the Activity ID to link the Hazard to
	var queryString = "?action=add_hazard&title=" + data + "&activity=" + activity;	// Build the query string to pass to our AJAX handler (ajax_rpc.php)
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true); // Send the request to our AJAX handler - this function does not need a response as it is only inserting data.
	ajaxRequest.send(null);
}

// Function:	ajaxAddInherent(activityID)
function ajaxAddInherent(activityID){
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	var data = document.getElementById("jsa_inherentlistTitle").value;	// Retrieve the title of the Inherent Risk from the jsa_inherentlistTitle input field on rm_jsa.php
	
	var activity = document.getElementById("jsa_inherentlistActivity").value;	// Retrieve the Activity ID to link the Inherent Risk to
	var queryString = "?action=add_inherent&title=" + data + "&activity=" + activity;	// Build the query string to pass to our AJAX handler (ajax_rpc.php)
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true); // Send the request to our AJAX handler - this function does not need a response as it is only inserting data.
	ajaxRequest.send(null);
}

// Function:	ajaxAddMeasure(hazardID)
function ajaxAddMeasure(hazardID){
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	var data = document.getElementById("jsa_measureslistTitle").value;	// Retrieve the title of the Hazard from the jsa_hazardsTitle input field on rm_jsa.php
	
	var activity = document.getElementById("jsa_measureslistActivity").value;	// Retrieve the Activity ID to link the Hazard to
	var queryString = "?action=add_measure&title=" + data + "&activity=" + activity;	// Build the query string to pass to our AJAX handler (ajax_rpc.php)
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true); // Send the request to our AJAX handler - this function does not need a response as it is only inserting data.
	ajaxRequest.send(null);
}

// Function:	ajaxAddResidual(activityID)
function ajaxAddResidual(activityID){
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	var data = document.getElementById("jsa_residuallistTitle").value;	// Retrieve the title of the Inherent Risk from the jsa_inherentlistTitle input field on rm_jsa.php
	
	var activity = document.getElementById("jsa_residuallistActivity").value;	// Retrieve the Activity ID to link the Inherent Risk to
	var queryString = "?action=add_residual&title=" + data + "&activity=" + activity;	// Build the query string to pass to our AJAX handler (ajax_rpc.php)
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true); // Send the request to our AJAX handler - this function does not need a response as it is only inserting data.
	ajaxRequest.send(null);
}


// Function:	ajaxGetHazards(activityID)
// Use:			This function is used to retrieve the Hazards for the chosen Activity, triggered by selecting the "activity" / "activity_#" drop down box on page rm_jsa.php
//				variable "activityID" should reference one of the "activity" / "activity_#" drop down boxes.
function ajaxGetHazards(activityID,hazardID){
	
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = ajaxRequest.responseText;
			document.getElementById(hazardID).innerHTML = returnData;
		}
	}
	var activity = document.getElementById(activityID).value;
	var queryString = "?action=get_hazards&activity=" + activity + "&target=" + hazardID;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}

// Function:	ajaxGetInherent(activityID)
// Use:			This function is used to retrieve the Hazards for the chosen Activity, triggered by selecting the "activity" / "activity_#" drop down box on page rm_jsa.php
//				variable "activityID" should reference one of the "activity" / "activity_#" drop down boxes.
function ajaxGetInherent(activityID,inherentID){
	
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = ajaxRequest.responseText;
			document.getElementById(inherentID).innerHTML = returnData;
		}
	}
	var activity = document.getElementById(activityID).value;
	var queryString = "?action=get_inherent&activity=" + activity + "&target=" + inherentID;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}

// Function:	ajaxGetResidual(activityID)
// Use:			This function is used to retrieve the Hazards for the chosen Activity, triggered by selecting the "activity" / "activity_#" drop down box on page rm_jsa.php
//				variable "activityID" should reference one of the "activity" / "activity_#" drop down boxes.
function ajaxGetResidual(activityID,residualID){
	
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = ajaxRequest.responseText;
			document.getElementById(residualID).innerHTML = returnData;
		}
	}
	var activity = document.getElementById(activityID).value;
	var queryString = "?action=get_residual&activity=" + activity + "&target=" + residualID;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}

// Function:	ajaxGetMeasures(activityID)
// Use:			This function is used to retrieve the Measures for the chosen Hazard, triggered by selecting the "hazard" / "hazard_#" drop down box on page rm_jsa.php
//				variable "hazardID" should reference one of the "hazard" / "hazard_#" drop down boxes.
function ajaxGetMeasures(activityID,measureID){
	
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = ajaxRequest.responseText;
			document.getElementById(measureID).innerHTML = returnData;
		}
	}
	var activity = document.getElementById(activityID).value;
	var queryString = "?action=get_measures&activity=" + activity + "&target=" + measureID;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}
function ajaxGetNextJSA(){
	
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = ajaxRequest.responseText;
			document.getElementById("jsaNumber").value = returnData;
		}
	}
	var job = document.getElementById("jsaJobNo").value;
	var queryString = "?action=get_lastjsa&id=" + job;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}
function ajaxGetPPE(employeeID,accessLevel){
	
	// Create our AJAX object using ajaxRequestObject()
	var ajaxRequest = new ajaxRequestObject()
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
			var returnData = ajaxRequest.responseText;
			document.getElementById("ppe_table").innerHTML = returnData;
		}
	}
	var id = employeeID;
	var queryString = "?action=get_ppe&id=" + id + "&access=" + accessLevel;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);
}

function ajaxFillJobInfo(){
	var ajaxRequest = new ajaxRequestObject() // Create our AJAX object using the 'ajaxRequestObject' function.

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		// Check the ready state of the request - if its ready, respond with our data!
		if(ajaxRequest.readyState == 4){
				var ajaxDisplay = new Array(); // Create an array to store our returned data in.
            	ajaxDisplay = ajaxRequest.responseText.split('|'); // This data is seperated by |, so we split it into our ajaxDisplay array.
            	document.getElementById("jsaJobDescription").value = ajaxDisplay[0];	// Modify input form
				document.getElementById("jsaCustomer").value = ajaxDisplay[1];
				document.getElementById("jsaCustomerAddress").value = ajaxDisplay[2];
		}
	}
	var data = document.getElementById("jsaJobNo").value;
	var queryString = "?action=get_job&id=" + data;
	ajaxRequest.open("GET", "includes/ajax_rpc.php" + queryString, true);
	ajaxRequest.send(null);

}
function ajaxLoading(elementID) {
	document.getElementById(elementID).innerHTML = "<br /><center><img src='images/loading.gif' /><center>";
}

// Popup Windows
function popupWindow(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=800,height=600,scrollbars=yes');
return false;
}

// Show a <div> HTML tag
function showdiv(divID) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(divID).style.visibility = 'visible';
	}
	else {
		if (document.layers) { // Netscape 4
			document.divID.visibility = 'visible';
		}
	else { // IE 4
		document.all.divID.style.visibility = 'visible';
		}
	}
}

// Hide a <div> HTML tag
function hidediv(divID) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(divID).style.visibility = 'hidden';
	}
	else {
		if (document.layers) { // Netscape 4
			document.divID.visibility = 'hidden';
		}
	else { // IE 4
		document.all.divID.style.visibility = 'hidden';
		}
	}
}