/*
 * class ajaxHandler
 *
 * This class handles modal boxes, ajax requests, and the frontend for banned keywords filtering
 *
 */
function ajaxHandler () {
	// Properties
	this.response = null;

	// Methods
	this.sendAjaxRequest = sendAjaxRequest; // does the actual sending of an ajax request
	this.handleResponse = handleResponse; // decides what to do with the response
	this.getQueryString = getQueryString; // parses a form (& its data) into a query string in the form var1=value&var2=value etc...
	this.submitForm = submitForm; // handles an ajax post of data, and the display of the response
}


/*
 * function submitForm
 *
 *	Summary:
 *		- This function submits a form to the server, and displays the server's response to it in a modal div (we're assuming any other type of submit will either run through checkKeywords, or be a standard (ie, non-ajax) submit)
 *
 *	Arguments:
 *		- form_id: the id of the form we're submitting
 *
 *	Returns: Nothing
 */
function submitForm (form_id) {
	// Get the submit information from the form
	form = document.getElementById(form_id);

	submit_url = form.action;
	submit_data = this.getQueryString(form_id)+'&ajaxSubmit=true';
	
	this.sendAjaxRequest('POST', submit_url, submit_data, function (response) {
																ajax.handleResponse(response);
															}
														);
}

/*
 * function sendAjaxRequest
 *
 *	Summary:
 *		- This function sends an ajax request to the server, and returns the response
 *
 *	Arguments:
 *		- method: either "GET" or "POST"
 *			- defaults to "GET"
 *		- url: the script we're submitting the data to
 *		- data: the data to submit
 *			- defaults to: null
 *
 *	Returns:
 *		xmlHTTP.responseText: the response from the server
 */
function sendAjaxRequest (method, url, data, return_function) {
	var xmlHTTP;

	if (window.XMLHttpRequest) {
		xmlHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return null;
	}
	
	if (xmlHTTP) {
		xmlHTTP.open(method, url, true);
		
		xmlHTTP.onreadystatechange = function () {
					if (xmlHTTP.readyState == 4) { return_function(xmlHTTP.responseText); }
				}
		if (method == 'POST') xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		xmlHTTP.send(data);
	}
}

/*
 * function getQueryString
 *
 *	Summary:
 *		- This function parses a form (& it's data) into an xml submitable string in the form: var1=value&var2=value2 etc...
 *
 *	Arguments:
 *		- form_id: the id of the form which is being submitted
 *
 *	Returns:
 *		- the formatted query string
 */
function getQueryString (form_id) {
	form = document.getElementById(form_id);
	
	// next, formulate the request data string
	var form_elements = form.elements;
	var request_string = '';
	
	// iterate over each form element, and add it to the request string
	for (var i=0; i < form_elements.length; i ++) {
		// if it's not a submit, radio, or checkbox, just add it to the string
		if (form_elements[i].type != 'radio' && form_elements[i].type != 'checkbox' && form_elements[i].type != 'submit') request_string += '&'+form_elements[i].name+'='+escape(form_elements[i].value);
		
		// if it's a radio or checkbox, make sure it's checked before you add it to the string
		if (form_elements[i].type == 'radio' || form_elements[i].type == 'checkbox') {
			if (form_elements[i].checked == true) request_string += '&'+form_elements[i].name+'='+escape(form_elements[i].value);
		}
	}
	request_string = request_string.substr(1); // remove the first ampersand
	
	return request_string;
}

/*
 * function handleResponse
 *
 *	Summary:
 *		- This function decides how to handle a response from the server
 *
 *	Arguments:
 *		- response: the response the server gave
 *
 *	Returns: Nothing
 */
function handleResponse (response) {
	if (response == 'error') document.getElementById('error-login').style.visibility='visible';
	else location.replace(response);
}