// ajaxConnect: AJAX function. Takes reqType (either "POST" or "GET"), theURL (the URL to fetch),
// 						asynch (boolean - asynchronous or not?), and respHandle (the
//						function to call when the HTTP readyState changes). respHandle
//						should read the AJAX data from request.responseText.

var request = null;

function ajaxConnect(reqType,theURL,asynch,respHandle) {

	if(window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	}
	
	else if(window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if(!request) { request = new ActiveXObject("Msxml2.XMLHTTP"); }
	}
	
	if(request) {
		if(reqType.toUpperCase() != "POST") {
			request.onreadystatechange = respHandle;
			request.open(reqType,theURL,asynch);
			request.send(null);
			initRequest(reqType,theURL,asynch,respHandle);
		} else {
			var post_data = arguments[4];
			request.onreadystatechange = respHandle;
			request.open(reqType,theURL,asynch);
			if(post_data != null && post_data.length > 0) {
				request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				request.send(post_data);
			}
		}
	} else {
		alert("AJAX not supported.");
	}

}

// ajaxMiddleman: Converts AJAX response ("true" or "false") to boolean. Takes handler_exp
// 						(well formed JS expression that's called once the conversion is done).
//						handler_exp should contain "_BOOL_" to signify where the boolean value should go.
//						Also replaces any percentage signs with double quotes - useful when coding this
//						function into HTML event handlers, since you can only use one pair of double quotes,
//						one pair of single quotes, and one pair of escaped single quotes.
//						This function won't really do anything otherwise. :)

function ajaxMiddleman(handler_exp) {
	if(request.responseText == "true") {
		bool_exp = handler_exp.replace(/_BOOL_/g,"true");
	} else {
		bool_exp = handler_exp.replace(/_BOOL_/g,"false");
	}
	bool_exp = bool_exp.replace(/%/g,"\"");
	eval(bool_exp);
}

// verifyPostCode: Simple verification function. Takes postcode (the postal code to test),
// 						and handler_exp (a well formed JS expression that's called when the
//						response is recieved. handler_exp should read "true" from 
//						request.responseText if the postal code is valid in the US or Canada,
//						and "false" from request.responseText otherwise.

function verifyPostCode(postcode,handler_exp) {
	ajaxConnect("GET","query.php?postcode="+postcode,true,function(){ if(request.readyState == 4 && request.status == 200) { eval(handler_exp); } });
}

// verifyUsername: Simple verification function. Takes username (the username to test),
// 						and handler_exp (a well formed JS expression that's called when the
//						response is recieved. handler_exp should read "true" from 
//						request.responseText if the username exists,
//						and "false" from request.responseText otherwise.

function verifyUsername(name,handler_exp) {
	ajaxConnect("GET","query.php?username="+name,true,function(){ if(request.readyState == 4 && request.status == 200) { eval(handler_exp); } });
}

// verifyEmail: Used in signup.php and account.php

function verifyEmail() {
	input_element_arr = document.getElementsByName("email");
	addr = input_element_arr[0].value;
	exp = /^[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+.[A-Za-z0-9._%-]{2,4}$/;
	if(!(exp.test(addr))) {
		handleAlert("email", true);
		form_error = true;
	} else {
		handleAlert("email",false);
	}
}

function verifyLength(element,length) {
	input_element_arr = document.getElementsByName(element);
	if(input_element_arr[0].value.length < length) {
		handleAlert(element, true);
		form_error = true;
	} else {
		handleAlert(element, false);
	}
}

function verifyConfirm(element) {
	input_element_arr = document.getElementsByName(element);
	input_twin_arr = document.getElementsByName(element+"2");	
	if(input_element_arr[0].value.length > 0 && input_element_arr[0].value != input_twin_arr[0].value) {
		handleAlert(element+"2", true);
		form_error = true;
	} else {
		handleAlert(element+"2", false);	
	}	
}

// stealthFire: Shows/hides items. Takes idName (id of item to show/hide).

function stealthFire(idName) {
	element = document.getElementById(idName);
	if(element.style.display != "none") {
		element.style.display = "none";
	} else {
		element.style.display = "block";
	}
}
