var xmlHttp;

function updatePage(pageName){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url=pageName+".php?c="+"helloworld"+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedMainContent;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}//end of updatePage

function updatePage(pageName,arg){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url=pageName+".php?"+arg+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedMainContent;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}//end of updatePage overloaded

function stateChangedMainContent(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){document.getElementById("content").innerHTML= xmlHttp.responseText;}
}//end of stateChangedMainContent

function schoolNameChange(name){
	if (name != ""){
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
		var url="getCollege.php?c=helloworld"+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChangedSchoolName;
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttp.send("school="+name);
	}else{document.getElementById("schoolSelect").style.visibility= "hidden";}
}//end of school NameChange

function stateChangedSchoolName(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		var i=0;
		var nameArray = xmlHttp.responseText.split(";");
		if (xmlHttp.responseText == ""){
			document.getElementById("schoolSelect").style.visibility= "hidden";
		}else{
			document.getElementById("schoolSelect").style.visibility= "visible";
			document.getElementById("schoolSelect").innerHTML= "";
			for (i=0;i<nameArray.length;i++){
				document.getElementById("schoolSelect").innerHTML += "<option value=\""+nameArray[i]+ "\">"+nameArray[i]+"</option>";
			}//end of for loop
		}
 	}
}//end of stateChangedSchoolName

function schoolSelected(){
	if(document.getElementById("schoolSelect").selectedIndex != -1){
		document.getElementById("regSchool").value = document.getElementById("schoolSelect").options[document.getElementById("schoolSelect").selectedIndex].value;
		document.getElementById("schoolSelect").style.visibility= "hidden";
		document.getElementById("schoolSelect").selectedIndex = -1;
	}
}//end of schoolSelected

function checkRegForm(form){
	var errormsg ="<h3>Errors:</h3><p>";
	var check = errormsg;
	document.getElementById("registerformError").style.visibility= "hidden";

	if (form.username.value.length<3 || form.username.value.length>20){errormsg += "Invalid Username. Usernames must be at least three(3) characters long.<br>";}
	if (form.password.value.length<6 || form.password.value.length>30){errormsg += "Invalid Password. Passwords must be at least six(6) characters long.<br>";}
	if (form.password.value != form.confirmPassword.value){errormsg += "Failed to confirm password. Passwords must match.<br>";}
	if (form.firstname.value.length==0){errormsg += "First name is required.<br>";}
	if (form.lastname.value.length==0){errormsg += "Last name is required.<br>";}
	if (form.gender.value.length==0){errormsg += "Gender is required.<br>";}
	if (document.getElementById("coordinatorBox").checked == true){
		if (form.address1.value.length==0){errormsg += "Address is required.<br>";}
		if (form.city.value.length==0){errormsg += "City is required.<br>";}
		if (form.state.value.length==0){errormsg += "State is required.<br>";}
		if (form.zip.value.length==0){errormsg += "Postal Code is required.<br>";}
		if (form.country.value.length==0){errormsg += "Country is required.<br>";}
	}
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(form.email.value) == false){errormsg += "Email is invalid.<br>";}

	if (errormsg == check){	
		register(form);
	}else{
		document.getElementById("registerformError").innerHTML= errormsg;
		document.getElementById("registerformError").style.visibility= "visible";
		return false;
	}
}//end of checkRegistrationForm

function register(form){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url="forms/register.php?c="+"helloworld"+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedRegister;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	if (form.coordinatorBox.checked == true){
		xmlHttp.send("username="+encodeURI(form.username.value)+"&password="+encodeURI(form.password.value)+"&firstname="+encodeURI(form.firstname.value)+"&lastname="+encodeURI(form.lastname.value)+"&middleinitial="+encodeURI(form.middleinitial.value)+"&gender="+encodeURI(form.gender.value)+"&school="+encodeURI(form.school.value)+"&email="+encodeURI(form.email.value)+"&timezone="+encodeURI(form.tz.value)+"&address1="+encodeURI(form.address1.value)+"&address2="+encodeURI(form.address2.value)+"&city="+encodeURI(form.city.value)+"&state="+encodeURI(form.state.value)+"&zip="+encodeURI(form.zip.value)+"&country="+encodeURI(form.country.value)+"&phone="+encodeURI(form.phone.value));
	}else{
		xmlHttp.send("username="+encodeURI(form.username.value)+"&password="+encodeURI(form.password.value)+"&firstname="+encodeURI(form.firstname.value)+"&lastname="+encodeURI(form.lastname.value)+"&middleinitial="+encodeURI(form.middleinitial.value)+"&gender="+encodeURI(form.gender.value)+"&school="+encodeURI(form.school.value)+"&email="+encodeURI(form.email.value)+"&timezone="+encodeURI(form.tz.value));
	}
}//end of register

function stateChangedRegister() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
 		if (xmlHttp.responseText=="badschool"){
			document.getElementById("registerformError").innerHTML="Your college/university does not exist in the CSL database. Select from the dropdown menu or contact CSL to add.";
			document.getElementById("registerformError").style.visibility= "visible";}
		else if (xmlHttp.responseText=="taken"){
			document.getElementById("registerformError").innerHTML= "This username is already taken";
			document.getElementById("registerformError").style.visibility= "visible";
		}else if (xmlHttp.responseText=="alreadyCoordinator"){
			document.getElementById("registerformError").innerHTML= "There is already a coordinator for your school.";
			document.getElementById("registerformError").style.visibility= "visible";}
		else if (xmlHttp.responseText=="email taken"){
			document.getElementById("registerformError").innerHTML= "There is already an account associated with this email.";
			document.getElementById("registerformError").style.visibility= "visible";}
		else{document.getElementById("form_container").innerHTML= xmlHttp.responseText;}
	}
}//end of stateChangedRegister

function checkLineupForm(form){

	var errormsg ="<h3>Errors:</h3><p>";
	var check = errormsg;
	document.getElementById("lineupformError").style.visibility= "hidden";

	if (form.username1.value.length ==0){errormsg += "Enter player username for game 1.<br>";}
	if (form.race1.value.length==0){errormsg += "Enter player race for game 1.<br>";}
	if (form.username2.value.length ==0){errormsg += "Enter player username for game 2.<br>";}
	if (form.race2.value.length==0){errormsg += "Enter player race for game 2.<br>";}
	if (form.username3.value.length ==0){errormsg += "Enter player username for game 3.<br>";}
	if (form.race3.value.length==0){errormsg += "Enter player race for game 3.<br>";}
	if (form.username4.value.length ==0){errormsg += "Enter player username for game 4.<br>";}
	if (form.race4.value.length==0){errormsg += "Enter player race for game 4.<br>";}
	if (form.time.value.length==0){errormsg += "Enter available time to play.<br>";}
	if (form.username.value.length==0 || form.password.value.length==0){ errormsg += "Authenticate your submission.<br>";}

	if (errormsg == check){	
		lineup(form);
	}else{
		document.getElementById("lineupformError").innerHTML= errormsg;
		document.getElementById("lineupformError").style.visibility= "visible";
		return false;
	}
}//end of checkLienupForm

function lineup(form){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url="forms/lineup.php?c="+"helloworld"+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedLineup;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send("username1="+encodeURI(form.username1.value)+"&race1="+encodeURI(form.race1.value)+"&username2="+encodeURI(form.username2.value)+"&race2="+encodeURI(form.race2.value)+"&username3="+encodeURI(form.username3.value)+"&race3="+encodeURI(form.race3.value)+"&username4="+encodeURI(form.username4.value)+"&race4="+encodeURI(form.race4.value)+"&time="+encodeURI(form.time.value)+"&url="+encodeURI(form.url.value)+"&username="+encodeURI(form.username.value)+"&password="+encodeURI(form.password.value)+"&map1="+encodeURI(form.map1.value)+"&map2="+encodeURI(form.map2.value)+"&map3="+encodeURI(form.map3.value)+"&map4="+encodeURI(form.map4.value)+"&map5="+encodeURI(form.map5.value));
}//end of confirmLineup

function stateChangedLineup() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		if (xmlHttp.responseText=="bad submission"){document.getElementById("lineupformError").innerHTML= "We could not verify your coordinator status. Please check your username/password combination.";
			document.getElementById("lineupformError").style.visibility= "visible"; return false;}
		if (xmlHttp.responseText=="already submitted"){document.getElementById("lineupformError").innerHTML= "Your lineup for this round has already been submitted. Changes are not allowed.";
			document.getElementById("lineupformError").style.visibility= "visible"; return false;}	
 		if (xmlHttp.responseText=="1"){document.getElementById("lineupformError").innerHTML= "Player 1 is not registered with your school or appears in the lineup more than once.";
			document.getElementById("lineupformError").style.visibility= "visible";}
		else if (xmlHttp.responseText=="2"){document.getElementById("lineupformError").innerHTML= "Player 2 is not registered with your school or appears in the lineup more than once.";
			document.getElementById("lineupformError").style.visibility= "visible";}
		else if (xmlHttp.responseText=="3"){document.getElementById("lineupformError").innerHTML= "Player 3 is not registered with your school or appears in the lineup more than once.";
			document.getElementById("lineupformError").style.visibility= "visible";}
		else if (xmlHttp.responseText=="4"){document.getElementById("lineupformError").innerHTML= "Player 4 is not registered with your school or appears in the lineup more than once.";
			document.getElementById("lineupformError").style.visibility= "visible";}
		else{document.getElementById("content").innerHTML= xmlHttp.responseText; getSessionId();}
	}
}//end of stateChangedLogin

function getSessionId(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url="getSessionId.php?c="+"helloworld"+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedSessionId;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangedSessionId(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){sessionId = xmlHttp.responseText; loginForum();}
}//end of sateChangedSessionId

function loginForum(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url="forums/ucp.php?mode=login"+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedLoginForum;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send("username="+encodeURI(username)+"&password="+encodeURI(password)+"&sid="+encodeURI(sessionId)+"&login=Login&redirect=index.php");
}//end of loginForum

function stateChangedLoginForum(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){showForums();}
}//end of stateChangedLoginForum


function showForums(){
	document.getElementById("content").innerHTML= "<iframe id=\"forumFrame"+Math.random()+"\" src=\"/forums/index.php?sid="+Math.random()+"\" name=\"forums"+Math.random()+"\" frameborder=\"0\" width=\"980px\" height=\"1400px\" onload=\"setForumHeight(this)\"></iframe>";
}

function coordinatorBoxChecked(){
	if (document.getElementById("coordinatorBox").checked == true){document.getElementById("coordinatorForm").style.display= "block";}
	else{document.getElementById("coordinatorForm").style.display= "none";}
}//end of coordinatorBoxChecked

function checkLogin(form){

	var errormsg ="";
	document.getElementById("loginformError").style.visibility= "hidden";

	if (form.username.value.length ==0 || form.password.value.length ==0){errormsg += "Required Field Missing";}
	
	if (errormsg == ""){	
		sendlogin(form);
	}else{
		document.getElementById("loginformError").innerHTML= errormsg;
		document.getElementById("loginformError").style.visibility= "visible";
	}
}//end of checkLogin

function sendlogin(form){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){alert ("Browser does not support HTTP Request"); return;}
	var url="auth_user.php?c="+"helloworld"+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedLogin;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send("username="+encodeURI(form.username.value)+"&password="+encodeURI(form.password.value));
}//end of login

function stateChangedLogin() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		if (xmlHttp.responseText == 'bad login'){
			document.getElementById("loginformError").innerHTML = "Username/Password Combination Invalid";
			document.getElementById("loginformError").style.visibility = "visible";
		}else{ document.getElementById("login").innerHTML = xmlHttp.responseText;}
	}
}//end of stateChangedLogin

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{xmlHttp=new XMLHttpRequest();}
	catch (e){
 	//Internet Explorer
 		try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
 		catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
 	}
	return xmlHttp;
}//end of GetXmlHttpObject