<repo_name>haresh-ram/firstChess<file_sep>/firstChess/src/main/webapp/sign.js
/**
 * 
 */

function openSignUp(){
	cleanFields();
	textfocus();
	document.getElementById("outerBox").style.cssText="display:none";
	document.getElementById("outerBoxSignUp").style.cssText="display:flex";
}

function closeSignUp(){
	cleanFields();
	textfocus();
	document.getElementById("outerBoxSignUp").style.cssText="display:none";
	document.getElementById("outerBox").style.cssText="display:flex";
}

function textfocus(){
	document.getElementById("field1").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("field2").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("text1").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("text2").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("text3").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("text5").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("countries").style.cssText="border: 1px solid rgb(70, 172, 255);"
	document.getElementById("label1").innerHTML="";
	document.getElementById("label2").innerHTML="";
	document.getElementById("label3").innerHTML="";
	document.getElementById("label4").innerHTML="";
	document.getElementById("label5").innerHTML="";
	document.getElementById("label6").innerHTML="";
	document.getElementById("label7").innerHTML="";
	document.getElementById("label8").innerHTML="";
}

function cleanFields(){
	document.getElementById("field1").value="";
	document.getElementById("field2").value="";
	document.getElementById("text1").value="";
	document.getElementById("text2").value="";
	document.getElementById("text3").value="";
	document.getElementById("text5").value="";
}


function signUpCheck(){
	var text1 = document.getElementById("text1");
	var text2 = document.getElementById("text2");
	var text3 = document.getElementById("text3");
	var text5 = document.getElementById("text5");
	var dropdown = document.getElementById("countries");
	var regx = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	if(text1.value.trim()=="" || text2.value.trim()=="" || text3.value.trim()=="" || dropdown.options[dropdown.selectedIndex].value == "" || text3.value.trim() != text5.value.trim() || regx.test(text2.value) == false){
		if(text1.value.trim()==""){
			text1.style.cssText="border:2px solid red;";
			document.getElementById("label3").innerHTML="Field cannot be Empty";
		}
		if(text2.value.trim()==""){
			text2.style.cssText="border:2px solid red;";
			document.getElementById("label4").innerHTML="Field cannot be Empty";
		}
		if(text3.value.trim()==""){
			text3.style.cssText="border:2px solid red;";
			document.getElementById("label5").innerHTML="Field cannot be Empty";
		}
		if(text5.value.trim()==""){
			text5.style.cssText="border:2px solid red;";
			document.getElementById("label8").innerHTML="Field cannot be Empty";
		}
		if(text3.value.trim() != text5.value.trim()){
			text3.style.cssText="border:2px solid red;";
			text5.style.cssText="border:2px solid red;";
			document.getElementById("label5").innerHTML="Passwords not matching";
		}
		if(dropdown.options[dropdown.selectedIndex].value == ""){
			dropdown.style.cssText="border:2px solid red;";
			document.getElementById("label6").innerHTML="Select your Country";
		}
		if(regx.test(text2.value) == false){
			text2.style.cssText="border:2px solid red;";
			document.getElementById("label4").innerHTML="Invalid EmailID";
		}
		return false;
	}else{
		
		var v=false;
		var xhttp = new XMLHttpRequest();
		
		xhttp.onreadystatechange = function(){
			if(this.readyState == 4 && this.status == 200){
				var res = this.responseText;
				
				if(res=="false"){
					text2.style.cssText="border:2px solid red;";
					document.getElementById("label4").innerHTML="E-mail ID already exists";
					v=false;
				}else
					v=true;
			}
		};
		
		xhttp.open("POST","chessLoginServlet?email="+document.getElementById("text2").value+"&action="+"signUpPriorCheck",false);
		xhttp.send();
		
		if(v==false)
			return false;
		else
			return true;		
		
	}
		
}

function checkValidLogin(){
	var field1 = document.getElementById("field1");
	var field2 = document.getElementById("field2");
	if(field1.value.trim()=="" || field2.value.trim()==""){
		if(field1.value.trim()==""){
			field1.style.cssText="border:2px solid red;";
			document.getElementById("label1").innerHTML="Field cannot be Empty"
		}
		if(field2.value.trim()==""){
			field2.style.cssText="border:2px solid red;";
			document.getElementById("label2").innerHTML="Field cannot be Empty"
		}
		return false;
	}else{
		
		var v=false;
		var xhttp = new XMLHttpRequest();
		
		xhttp.onreadystatechange = function(){
			if(this.readyState == 4 && this.status == 200){
				var res = this.responseText;
				
				if(res=="false"){
					document.getElementById("label7").innerHTML="Invalid E-mail/Password";
					v=false;
				}else
					v=true;
			}
		};
		
		xhttp.open("POST","chessLoginServlet?email="+document.getElementById("field1").value+"&password="+document.getElementById("field2").value+"&action="+"loginPriorCheck",false);
		xhttp.send();
		
		document.getElementById("field2").value="";
		
		if(v==false)
			return false;
		else
			return true;
	}
}

function openGame(){
	var gameCode = localStorage.getItem("gameCode");
	var xhttp = new XMLHttpRequest();
	xhttp.onreadystatechange = function(){
		if(this.readyState == 4 && this.status == 200){
			window.location.href="board.jsp";
		}
	};
	xhttp.open("POST","chessLoginServlet?gameCode="+gameCode+"&action="+"registerGame");
	xhttp.send();
}


function getProfile(){
	var xhttp = new XMLHttpRequest();
	var response;
	xhttp.onreadystatechange = function(){
		if(this.readyState == 4 && this.status == 200){
			response = this.responseText;
			var json = JSON.parse(response);
			document.getElementById("profileID").innerHTML = json.userID;
			document.getElementById("profileEmail").innerHTML = json.email;
			document.getElementById("profileCountry").innerHTML = json.country;
			document.getElementById("matchesPlayed").innerHTML = json.matchesPlayed;
			document.getElementById("matchesWon").innerHTML = json.matchesWon;
			document.getElementById("matchesLost").innerHTML = json.matchesLost;
			document.getElementById("matchesDraw").innerHTML = json.matchesDraw;
			document.getElementById("profileName").innerHTML = json.username;
		}
	};
	xhttp.open("POST","chessLoginServlet?action="+"getProfile");
	xhttp.send();
}

function codeSubmit(){
	var box = document.getElementById("gameCodeBox");
	if(box.value.trim()==""){
		box.style.cssText="border:2px solid red;";
		document.getElementById("codeLabel").innerHTML = "Field cannot be Empty";
		return false;
	}
	localStorage.setItem("gameCode",box.value);
	document.getElementById("gameCodeBox").value="";
	window.location.href="board.jsp";
}

function codeBoxFocus(){
	document.getElementById("gameCodeBox").style.cssText="border: 1px solid rgb(70, 172, 255);";
	document.getElementById("codeLabel").innerHTML="";
	document.getElementById("gameCodeBox1").style.cssText="border: 1px solid rgb(70, 172, 255);";
	document.getElementById("codeLabel1").innerHTML="";
}

function createGame(){
	document.getElementById("tab1").style.cssText = "background-color:black;color:white";
	document.getElementById("tab2").style.cssText = "background-color:rgb(52, 183, 255);color:black";
	document.getElementById("tab3").style.cssText = "background-color:rgb(52, 183, 255);color:black";
	if("" + (getComputedStyle(document.getElementById("codeCreateDiv")).display) == "none"){
		document.getElementById("codeDiv").style.cssText = "display:none";
		document.getElementById("codeDiv2").style.cssText = "display:none";
		document.getElementById("codeCreateDiv").style.cssText = "display:block";
		var rand = Math.floor(10000 + (Math.random() * 100000000));
		localStorage.setItem("gameCode",rand);
	}
}

function spectateJoin(){
	document.getElementById("tab3").style.cssText = "background-color:black;color:white";
	document.getElementById("tab2").style.cssText = "background-color:rgb(52, 183, 255);color:black";
	document.getElementById("tab1").style.cssText = "background-color:rgb(52, 183, 255);color:black";
	codeBoxFocus();
	document.getElementById("gameCodeBox1").value="";
	document.getElementById("codeCreateDiv").style.cssText = "display:none";
	document.getElementById("codeDiv").style.cssText = "display:none";
	document.getElementById("codeDiv2").style.cssText = "display:block";
	codeBoxFocus();
}

function joinGame(){
	document.getElementById("tab2").style.cssText = "background-color:black;color:white";
	document.getElementById("tab1").style.cssText = "background-color:rgb(52, 183, 255);color:black";
	document.getElementById("tab3").style.cssText = "background-color:rgb(52, 183, 255);color:black";
	codeBoxFocus();
	document.getElementById("gameCodeBox").value="";
	document.getElementById("codeCreateDiv").style.cssText = "display:none";
	document.getElementById("codeDiv").style.cssText = "display:block";
	document.getElementById("codeDiv2").style.cssText = "display:none";
}


function spectateGame(){
	var box = document.getElementById("gameCodeBox1");
	if(box.value.trim()==""){
		box.style.cssText="border:2px solid red;";
		document.getElementById("codeLabel1").innerHTML = "Field cannot be Empty";
		return false;
	}
	localStorage.setItem("gameCode",box.value);
	document.getElementById("gameCodeBox1").value="";
	window.location.href="spectateBoard.jsp";
}

function putCode(){
	var gameCode = localStorage.getItem("gameCode");
	document.getElementById("codeSpan").innerHTML = gameCode;
}


function openLeaderBoard(){
	document.getElementById("leaderBoardPage").style.cssText="display:flex";
}


function closeLeaderBoard(){
	document.getElementById("leaderBoardPage").style.cssText="display:none";
}
<file_sep>/firstChess/src/main/webapp/spectator.js
/**
 * 
 */


function spectatorBoardPos(){
	
	var gameCode = localStorage.getItem("gameCode");
	var xhttp = new XMLHttpRequest();
	var indx;
	var ar= ["00","01","02","03","04","05","06","07",
			 "10","11","12","13","14","15","16","17",
			 "20","21","22","23","24","25","26","27",
			 "30","31","32","33","34","35","36","37",
			 "40","41","42","43","44","45","46","47",
			 "50","51","52","53","54","55","56","57",
			 "60","61","62","63","64","65","66","67",
			 "70","71","72","73","74","75","76","77"];
		
	xhttp.onreadystatechange = function(){
		if(this.readyState = 4 && this.status == 200){
			var resp = this.responseText;
			var arr = resp.split(",");
			console.log(arr);
			for(let i=0;i<64;i++){
				
					if(arr[i]=="we1" || arr[i]=="we2"){
						document.getElementById(ar[i]).innerHTML = '\u2656';
					}else if(arr[i] == "wh"){
						document.getElementById(ar[i]).innerHTML = '\u2658';
					}else if(arr[i] == "wm"){
						document.getElementById(ar[i]).innerHTML = '\u2657';
					}else if(arr[i] == "wq"){
						document.getElementById(ar[i]).innerHTML = '\u2655';
					}else if(arr[i] == "wk"){
						document.getElementById(ar[i]).innerHTML = '\u2654';
					}else if(arr[i] == "wp"){
						document.getElementById(ar[i]).innerHTML = '\u2659';
					}else if(arr[i] == "be1" || arr[i] == "be2"){
						document.getElementById(ar[i]).innerHTML = '\u265C';
					}else if(arr[i] == "bh"){
						document.getElementById(ar[i]).innerHTML = '\u265E';
					}else if(arr[i] == "bm"){
						document.getElementById(ar[i]).innerHTML = '\u265D';
					}else if(arr[i] == "bq"){
						document.getElementById(ar[i]).innerHTML = '\u265B';
					}else if(arr[i] == "bk"){
						document.getElementById(ar[i]).innerHTML = '\u265A';
					}else if(arr[i] == "bp"){
						document.getElementById(ar[i]).innerHTML = '\u265F';
					}
				
			}
	
		}
	};
	xhttp.open("POST","chessLoginServlet?action="+"getSpectatorBoardPos"+"&gameCode="+gameCode,false);
	xhttp.send();
}
