//THE BASIC AJAX FUNCTIONS ========================================================================================
var xmlHttp = new Array(15);
function ajaxFunction(){
	var xmlHttp;
	try{// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}
function makeRequest(url, theDiv, i){
	xmlHttp[i] = ajaxFunction();
	var obj = document.getElementById(theDiv);
	xmlHttp[i].open("GET", url, true);
	xmlHttp[i].onreadystatechange = function(){
		if (xmlHttp[i].readyState == 4 && xmlHttp[i].status == 200) {
			obj.innerHTML = xmlHttp[i].responseText;
		}
	}
	xmlHttp[i].send(null);
}
function returnRequest(url){
	xmlHttp = ajaxFunction();
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);
	return xmlHttp.responseText;
}
//HI-SCORE LIST FUNCTIONS ===============================================================================
function insertList(list, score){
	var url = "functions.php?action=insertList&list="+list+"&score="+score;
	makeRequest(url,"topScores", 3);
}
function insertScore(list, score, level, userName){
	var url = "functions.php?action=insertScore&list="+list+"&score="+score+"&level="+level+"&userName="+userName;
	makeRequest(url,"topScores", 3);
}
//SHOUT BOX FUNCTIONS ===================================================================================
function getChat(list){
	var url = "functions.php?action=getChat&list="+list;
	makeRequest(url,"shouts", 1);
}
function postChat(list){
	var message = addslashes(document.shoutForm.message.value);
	var url = "functions.php?action=postChat&list="+list+"&message="+message;
	makeRequest(url, "shouts", 2);
	document.shoutForm.message.value = "";
}
//VALIDATE FORM FUNCTIONS ===============================================================================
function validateForm(theForm){
	var error = "";
	var alphaExp = /^[0-9a-zA-Z' ']+$/;

	//email checking
	var email = theForm.email1.value;
	if(theForm.email1.value == "" || theForm.email2.value == ""){
		error+="You must enter a valid Email Address\n";}
	if(theForm.email1.value != theForm.email2.value){
		error+="Email Addresses do not match\n";}
	if(returnRequest("functions.php?action=checkEmail&email="+email) != "false"){
		error+="Email Address is already registered\n";}

	//password checking
	if(theForm.password1.value == "" || theForm.password2.value == ""){
		error+="You must enter a Password\n";}
	if(theForm.password1.value != theForm.password2.value){
		error+="Passwords do not match\n";}

	//name checking
	var name = theForm.name.value;
	if(theForm.name.value == ""){
		error+="You must enter a Username\n";}
	if(!theForm.name.value.match(alphaExp)){
		error+="Only numbers and letters are allowed in your Username\n";}
	if(returnRequest("functions.php?action=checkName&name="+name) != "false"){
		error+="Username is already registered\n";}
	//terms checking
	if(theForm.agreeToTerms.checked==false){
		error+="You must agree to the terms and conditions\n";}
	if(error != ""){
		alert(error);
		return false;}
	return true;
}
//OTHER STUFF ===========================================================================
function addslashes(str){
    return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}
function load(url){
	var load = window.open(url,'','scrollbars=yes,menubar=no,height=600,width=600,resizable=yes,toolbar=no,location=no,status=yes');
}




