// Generic AJAX library
//
// Author: Derek Ellison
//
// Created: 10/11/2006
//
// how to use:
//
// add a js event to a field that calls ajaxSubmit
// eg. <input type=text name=email onBlur="ajaxSubmit('validate_email.php?email='+this.value,'emailResult')">
// then write a function called emailResult, where the function parameter holds the php output string:
// function emailResult(result) {
//	 if (result=="duplicate") {
//		alert("This email address already exists!");
//	 }
// }


var http = GetXmlHttpObject();
var ajax_result_function = '';
var http2 = GetXmlHttpObject();
var ajax_result_function2 = '';

// works like submit with an action, only this is performed in real-time by AJAX
// the php page should return results as a string
// result_function is the js function where the array of results should be sent
function ajaxSubmit(action,result_function) {

	if (http == null) {
		return;
	}
	
	ajax_result_function = result_function;
	
	//sends the rules and value to the php page (action) to be validated
	http.open("GET", action, true);
  
	http.onreadystatechange = handleHttpResponse; 	// handle what to do with the feedback 
	http.send(null);  
}

// use this ajax function in cases where 2 AJAX events may fire concurrently
function ajaxSubmit2(action,result_function) {

	if (http2 == null) {
		return;
	}

	ajax_result_function2 = result_function;
	  
	//sends the rules and value to the php page (action) to be validated
	http2.open("GET", action, true);
  
	http2.onreadystatechange = handleHttpResponse2; 	// handle what to do with the feedback 
	http2.send(null);  
}

function GetXmlHttpObject(handler)
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

function handleHttpResponse() {
	if (http.readyState == 4) {
  		if (ajax_result_function) {
  			eval(ajax_result_function+'(http.responseText);');
  		}
  	}
}

function handleHttpResponse2() {
	if (http2.readyState == 4) {
  		if (ajax_result_function2) {
  			eval(ajax_result_function2+'(http2.responseText);');
  		}
  	}
}

// set PHP session variable from AJAX
function setSessionVariable(name,value,result_function) {
	ajaxSubmit('/common/ajax/set_session_var.php?name='+name+'&value='+value,result_function);
}

// ajax functions used by page class
var ajax_tables = new Array();
function render_ajax_tables(result) {
	for (i in ajax_tables) {
		eval(ajax_tables[i]+"()");
	}
}
function display_ajax_result(result) {
	if (result) {
		alert(result);
	}
}

// calls display_ajax_result then reloads the current window
function display_ajax_result_reload(result) {
	display_ajax_result(result);
	document.location.reload();
}

// uses ajaxSubmit, but asks user for either a reason or else a confirmation first
// uses display_ajax_result() to display the result text (if any)
// get_reason (boolean) determines whether to get back a reason from the user or just get an "OK" confirmation
// reload (boolean) determines whether the page should reload after the ajax completes
function ajaxAction(question,get_reason,action_script,reload) {
	if (get_reason) {
		var reason = prompt(question+" Please provide a reason for this action:","");
	} else {
		var reason = confirm(question);
	}
	if (reason) {
		ajaxSubmit(action_script+"&reason="+reason,"display_ajax_result"+(reload ? "_reload" : ""));
	}
}

