<!--

// ***************** AJAX stuff *************************

var ajaxIndication = "&AJAXED=true";

// creates a request object with which the data can be
// "tunneled" through
function create_request(){
	var req;					// the request object that will be created
	// if using a regular complaince standard browser
	if (window.XMLHttpRequest){
   	    req = new XMLHttpRequest();
   	}// if using Internet Explorer
	else if (window.ActiveXObject) {
   	    req = new ActiveXObject("Microsoft.XMLHTTP");
   	}
	
	if (!req) {
    	alert('Cannot create an XMLHTTP instance');
        return false;
    }else{
		return req;
	}
}

 /* the base ajax calling function
  * usually you want to set up a function that calls this one
  * and supplies it with the parameters for a specific <div>tag you need to work with
  * curDiv = the id of the div tag to put HTML into
  * url = the dynamic page waiting for post data, that will return the HTML
  * callback = the function that will be called when data is received by the browser
  * sendStr = a string of name=value pairs seperated by &s, which get sent to the url page
  * show = whether you want to change a hidden div to a visible one
  */
function base_ajax(tagID, url, sendStr, callback){
	var req = create_request();
	//alert(url);
	//alert(sendStr);
	if(req){
		req.open("POST", url, true);
		/* when the data has been completely loaded back into the browser
		  * and the status is OK, it puts the text string that was returned
		  * into the tag specified in curDiv, and shows the div (if specified)
		  */
		req.onreadystatechange = function(){
			if (req.readyState == 4 && req.status == 200) {
				var response = req.responseText;
				//alert(req.responseText);
				if(tagID != ""){
					var elem = document.getElementById(tagID);
					elem.innerHTML = response;
				}else{
					//alert(req.responseText);
				}
				
				//alert(callback)	
				if(callback != "" && callback != undefined){	
					eval(callback);
				}
			}	
		}
		
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		req.send(sendStr + ajaxIndication);
	}
}

function school_search(){
	var searchText = $("schoolSearchText").value;
	
	if(searchText == ""){
		$("schoolSearchResults").style.display = "none";
	}else{
		$("schoolSearchResults").style.display = "block";
	}
	
	base_ajax("schoolSearchResults", ADMIN_PATH + "/schools/search.php", "searchText=" + searchText, "");
}

function school_combine(schoolToCombine){
	var searchText = $("schoolSearchText").value;
	
	if(searchText == ""){
		$("schoolSearchResults").style.display = "none";
	}else{
		$("schoolSearchResults").style.display = "block";
	}
	
	base_ajax("schoolSearchResults", ADMIN_PATH + "/schools/search.php", "searchText=" + searchText + "&schoolToCombine=" + schoolToCombine, "");
}

function person_search(){
	var searchText = $("personSearchText").value;
	
	if(searchText == ""){
		$("personSearchResults").style.display = "none";
	}else{
		$("personSearchResults").style.display = "block";
	}
	
	base_ajax("personSearchResults", ADMIN_PATH + "/people/search.php", "searchText=" + searchText + "&personID=" + $("personID").value, "");
}


function validate_form_ajax(form, type){
	var req = create_request();
	var formAction = form.formAction.value;
	form.formAction.value = "validate";
	var formID;
	
	if(req){
		if(type.indexOf("special_") > -1){
			var parts = type.split("_");
			url = MODULES_URL + "/" + parts[1] + "/controllers/editInsert.php";
			formID = "specialForm";
		}
		else if(type.indexOf("base_") > -1){
			var parts = type.split("_");
			url = MODULES_URL + "/base/controllers/" + parts[1] + "/editInsert.php";
			formID = "specialForm";
		}
		else if(type == "custom"){
			url = form.validateURL.value;
			formID = form.id;
		}
		//alert(url);
		if(url != undefined){
			// open the connection
			req.open("POST", url, true);
		
			req.onreadystatechange = function(){
				if (req.readyState == 4 && req.status == 200) {
					var response = req.responseText;
					//alert("Sorry, I'm testing, this will go away shortly \n\n\n" + response);
					//alert(response);
					form.formAction.value = formAction;
					
					var errorObj = Json.evaluate(response);
					
					// if there are errors
					if(errorObj.errors.length > 0){
						show_form_errors(form, errorObj);
						window.location.href = "#topOfForm";
					}
					else if(response.indexOf("{'errors'") > -1){
						form.submit();
					}
					else{
						alert("Form did not validate");
						//alert(response);
					}
				}	
			}
			req.setRequestHeader('User-Agent','XMLHTTP/1.0');
			// set the header
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			var str = "";
		
			// get the form values string
			qString = $(formID).toQueryString();
		
			// send the request
			req.send(qString + str);
		}
	}
}

function validate_signup(form, type){
	var req = create_request();
	var formAction = form.formAction.value;
	form.formAction.value = "validate";
	var formID;
	
	if(req){
		url = form.validateURL.value;
		formID = form.id;
		
		//alert(url);
		if(url != undefined){
			// open the connection
			req.open("POST", url, true);
		
			req.onreadystatechange = function(){
				if (req.readyState == 4 && req.status == 200) {
					var response = req.responseText;
					//alert("Sorry, I'm testing, this will go away shortly \n\n\n" + response);
					//alert(response);
					form.formAction.value = formAction;
					
					var errorObj = Json.evaluate(response);
					
					// if there are errors
					if(errorObj.errors.length > 0){
						show_form_errors(form, errorObj);
						Recaptcha.reload();
						window.location.href = "#topOfForm";
					}
					else if(response.indexOf("{'errors'") > -1){
						form.submit();
					}
					else{
						alert("There was an error because the form did not validate. Please try again or refresh the page.");
						//alert(response);
					}
				}	
			}
			req.setRequestHeader('User-Agent','XMLHTTP/1.0');
			// set the header
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			var str = "";
		
			// get the form values string
			qString = $(formID).toQueryString();
		
			// send the request
			req.send(qString + str);
		}
	}
}

var surveyFormID = "surveyForm";

function validate_survey(){
	var req = create_request();

	var form = $(surveyFormID);
	var formAction = form.formAction.value;
	form.formAction.value = "validate";
	if(req){
		url = form.action;
		// open the connection
		req.open("POST", url, true);
	
		req.onreadystatechange = function(){
			if (req.readyState == 4 && req.status == 200) {
				var response = req.responseText;
				//alert(response);
				form.formAction.value = formAction;
				
				var errorObj = Json.evaluate(response);

				// if there are errors
				if(errorObj.errors.length > 0){
					show_form_errors(form, errorObj);
					$("errorMessage").style.display = "block";
					window.location.href = "#topOfForm";
					$("saveMessage").style.display = "none";
					save_survey();
				}
				else if(response.indexOf("{'errors'") > -1){
					form.submit();
				}
				else{
					alert("Form did not validate");
				}
			}
		}
		req.setRequestHeader('User-Agent','XMLHTTP/1.0');
		// set the header
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var str = "";
	
		// get the form values string
		qString = $(surveyFormID).toQueryString();
	
		// send the request
		req.send(qString + str);
	}
}

var numberOfSaves = 0;
var surveyInterval;

function save_survey(){
	//alert("hi");
	if(numberOfSaves > 10){
		//alert("Number of saves is greater than 5");
		clearTimeout(surveyInterval);
	}
	
	var form = $(surveyFormID);
	base_ajax("", form.action, form.toQueryString());
	$("statusUpdate").innerHTML += "Survey auto-saved at " + the_time() + "<br/>";
	numberOfSaves++;
}


function start_auto_save(){
	// save every 15 minutes
	surveyInterval = setInterval("save_survey()", 900000);
}

function the_time(){
	var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()

	var suffix = "AM";
	if (hours >= 12) {
		suffix = "PM";
		hours = hours - 12;
	}
	if (hours == 0) {
		hours = 12;
	}

	if (minutes < 10){
		minutes = "0" + minutes
	}
	
	return "<b>" + hours + ":" + minutes + " " + suffix + "</b>";
}

//-->
