/*
 * Creating the XMLHttpRequest object
 
 **************************************
 * This function checks to see what 
 * objects the browser supports in order
 * to create teh right kind of 
 * XMLHttpRequest type object to return
 *		createXMLHttpRequest()
 * @return returns an XMLHttpRequest
 *  type object of false
 * @type Object | Boolean
 * 
 */

function createXMLHttpRequest(){
	var request = false;
	
	/* Does this browser suppose teh XMLHttpRequest object? */
	if (window.XMLHttpRequest) {
		if(typeof XMLHttpRequest != 'undefined')
			/* Try to create a new XMLHttpRquest object */
			try{
				request = new XMLHttpRequest();
				}catch (e) {
					request = false;
				}
			/* Does this browser suppose ActiveX objects? */
			} else if (window.ActiveXObject) {
				/* Try to create a new ActiveX XMLHTTP object */
				try{
					request = new ActiveXObject('Msxml2.XMLHTTP');
				   } catch (e) {
				   		try{
				   			request = new ActiveXObject('microsoft.XMLHTTP');
				   		} catch (e) {
				   			request = false;
			   		}
		   	}
				 
	}
	return request;
} 

var request = createXMLHttpRequest();

function grabingData(URL, data, func){
	if (request) {
		request.open('GET', URL, true);
		request.onreadystatechange = func;
		request.send(data);
	}
}

function parseData(){
	if(request.readyState == 4){
		if(request.status == 200){
			var response = request.responseText;
			
			alert(response);
		}else{
			alert('There was a problem retreiving the data: \n' + request.statusText);
			
			request = null;
		}
	}
}
/***********************************************************************************************/
function changeMonth(month, year){
	var myurl = "cal.php?action=cal&month=" + month + "&year=" + year + "&junk=" + Math.random();
	var data = null;
	var func = updateCal;
	
	grabingData(myurl, data, func);
	
}
function updateCal(){
	if(request.readyState == 4){
		if(request.status == 200){
			var outBlock = document.getElementById('calendar');
			outBlock.innerHTML = request.responseText;
		}else{
			alert('There was a problem retreiving the data: \n' + request.statusText);
			request = null;
		}
	}
}
/***********************************************************************************************/
function signup(userID, schID, x){
	userLine = x;
	
	
	var myurl = "include/calFunctions.php?action=signup&userID=" + userID + "&schID=" + schID + "&junk=" + Math.random();
	var data = null;
	var func = updateSignup;
	
	grabingData(myurl, data, func);
}
function signup2(userID, schID, x){
	userLine = x;
	
	
	var myurl = "include/calFunctions.php?action=signup&userID=" + userID + "&schID=" + schID + "&junk=" + Math.random();
	var data = null;
	var func = updateSignup;
	
	grabingData(myurl, data, func);
}
function updateSignup(){
	if(request.readyState == 4){
		if(request.status == 200){
			var reply = request.responseText;
			var passBlock = document.getElementById('goalie_' + userLine);
			
			if(reply == "False"){
				passBlock.innerHTML = "You are not a goalie";
			}else if(reply == "userExists"){
				passBlock.innerHTML = "User already exists";
			}else if(reply == "success"){
				passBlock.innerHTML = "You've been added'";
				Effect.Fade(passBlock, { duration: 3.0 });
				sleep(6.0);
				self.location= (document.location.href);
			}else{
			alert('There was a problem retreiving the data: \n' + request.statusText);
			request = null;
		}
	}
	}
}
/**************************************************************************************************/
//Remove
/**************************************************************************************************/
function removeSelf(userid,schID, x){
	userLine = x;
	user_id = userid;

	
	var myurl = "include/calFunctions.php?action=removeSelf&schID=" + schID + "&userID=" + userid + "&junk=" + Math.random();
	var data = null;
	var func = updateSlot;
	
	grabingData(myurl, data, func);
}

function updateSlot(){
	if(request.readyState == 4){
		if(request.status == 200){
			var reply = request.responseText;
			var outBlock = document.getElementById('goalie_' + userLine);
			self.location= (document.location.href); 
			
		}else{
			alert('There was a problem deleting you: \n' + request.statusText);
			request = null;
		}
	}

}

/****************************************************************************************************/
//Check forms//
function checkLogin(){
	var user = document.getElementById('log');
	var passwd = document.getElementById('pwd');
	var alphaExp = /^[a-zA-Z ,\.\-]+$/;
	var phoneExp = /^[1-9]\d{2}\-\s?\d{3}\-\d{4}$/;
	var alphanumExp = /^[0-9a-zA-Z \.\,]{2,70}$/;
	var addressExp = /^[0-9a-zA-Z ./\-#,.]{2,70}$/;
	var address2Exp = /^[0-9a-zA-Z ./\-#,.]{0,70}$/;
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; 
	
	if(user.value == '' || passwd.value == ''){
		alert('Both fields must be filed in');
		exit0;
	}
	if(!user.value.match(alphanumExp)){
		alert('There is an invalid character in your user name');
		exit0;
	}
	if(!passwd.value.match(alphanumExp)){
		alert('There is an invalid character in your password');
		exit0;
	}
	
	
}
function popup(url)
{
 var width = 242;
 var height = 180;
 var left = (screen.width - width)/2;
 var top = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=yes';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,'windowname5', params);
 if (window.focus) {newwin.focus()}
 return false;
}
// bad implementation
function sleep(milliSeconds){
	var startTime = new Date().getTime(); // get the current time
	while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

