//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){
	type = document.applets[0].getElementsByTagName('param')[1].value;
	if(type=="guest"){
		toggleVisible("create");
		document.createForm.name.value = 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 email = theForm.email.value;
	var emailUsed = returnRequest("functions.php?action=checkEmail&email="+email)
	var name = theForm.name.value;
	var nameUsed = returnRequest("functions.php?action=checkName&name="+name)
	var error = "";
	var alphaExp = /^[0-9a-zA-Z' ']+$/;

	//email checking
	if(emailUsed=="true" && email.length > 3){
		alert("Email Address is already registered");
		return false;
	}
	//password checking
	if(theForm.password1.value == "" || theForm.password2.value == ""){
		alert("You must enter a Password");
		return false;
	}
	if(theForm.password1.value != theForm.password2.value){
		alert("Passwords do not match");
		return false;
	}
	//name checking
	if(theForm.name.value == ""){
		alert("You must enter a Username");
		return false;
	}
	if(!theForm.name.value.match(alphaExp)){
		alert("Only numbers and letters are allowed in your Username");
		return false;
	}
	if(nameUsed=="true"){
		alert("Username is already registered");
		return false;
	}
	//terms checking
	if(theForm.terms.checked==false){
		alert("You must agree to the terms and conditions");
		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');
}
function toggleVisible(id){
	var e = document.getElementById(id);
	if(e.style.display == 'block'){
		e.style.display = 'none';
	}else{
		e.style.display = 'block';
	}
}




