function doFirst(){
	
		for(var i=1;i<=4;i++){score[i]=0;}
		document.getElementById("sr1").value = "";
		document.getElementById("sr2").value = "";
		document.getElementById("sr3").value = "";
		document.getElementById("sr4").value = "";
		
	
		show();
		var gen = document.getElementById("gen");
		var bt1 = document.getElementById("bt1");
		var bt2 = document.getElementById("bt2");
		var bt3 = document.getElementById("bt3");
		var bt4 = document.getElementById("bt4");
		
		var bar = document.getElementById("selectedSec");
			
		
			
			if(police != 4){
				genarate();
			
			}
			gen.addEventListener("click",genarate,false);
			
			bt1.addEventListener("click",selectOne,false);
			
			bt2.addEventListener("click",selectTwo,false);
			
			bt3.addEventListener("click",selectThree,false);
			
			bt4.addEventListener("click",selectFour,false);
			
				
				
		
	
		
		score1=0;
		score2 =0;
		score3=0;
		score4=0;
		
		
}
var police=0;
var score1=0,score2=0,score3=0,score4=0;
var sc = document.getElementById("score");



function getRandom(low,high){
		return Math.floor( Math.random() * ( 1 + high - low ) ) + low;
}

var data = new Array();
var score = new Array();
var turnChecker =0;
function show(){
		
		var took = new Array();
		turnChecker = 0;
		for(var i=1;i<=4;i++)
		{
			took[i]=0;
			
			
		}
		var x;
		for(var j=1;j<=4;j++){
			while(took[x]!=0){
				x = getRandom(1,4);
			}
			took[x]=1;
			data[j]=x;
		}
		document.getElementById("bt1").value = "";
		document.getElementById("bt2").value = "";
		document.getElementById("bt3").value = "";
		if(data[4]==1){
			document.getElementById("bt4").value = "000";
		}else if(data[4]==2){
			document.getElementById("bt4").value = "700";
		}else if(data[4]==3){
			document.getElementById("bt4").value = "800";
		}else if(data[4]==4){
			document.getElementById("bt4").value = "900";
		}
		
		police =0;
		document.getElementById("bt1").style.backgroundImage = "url('button.jpeg')";
		document.getElementById("bt2").style.backgroundImage = "url('button.jpeg')";
		document.getElementById("bt3").style.backgroundImage = "url('button.jpeg')";
		document.getElementById("bt4").style.backgroundImage = "url('button.jpeg')";
		for(var a=1;a<=4;a++)
		{
			if(data[a]==2){
				police = a;
				
				document.getElementById("bt"+police).value = "700";
				document.getElementById("bt"+a).style.backgroundColor = "magenta";
				document.getElementById("bt"+a).style.backgroundImage = "url('buttonG.jpeg')";
				break;}
		}
		
}

function selectOne(){
	var check = document.getElementById("bt1").value;
	var checkYou = document.getElementById("bt4").value;
	if(checkYou=="700"){
	if(check!=""){
			alert("Invalid Click");
	}else{
	
		for(var a=1;a<=4;a++)
		{
			if(data[a]==1){
				
				document.getElementById("bt"+a).value = "000";
			}
			if(data[a]==3){
				
				document.getElementById("bt"+a).value = "800";
			}
			if(data[a]==4){
				
				document.getElementById("bt"+a).value = "900";
			}
		}
		
		
		if(data[1]==1){
				score[1] += 0;
				score[4] += 700;
				if(data[2]==3){
					score[2] += 800;
						
				}else if(data[2]==4){
					score[2] += 900;
						
				}
				if(data[3]==3){
					score[3] += 800;
					
				}else if(data[3]==4){
					score[3] += 900;
					
				}
				document.getElementById("bt1").style.backgroundColor = "black";
				document.getElementById("bt1").style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
		}else{
					
					score[4] += 0;
				if(data[2]==3){
					score[2] += 800;
						
				}else if(data[2]==4){
					score[2] += 900;
						
				}else if(data[2]==1){
					score[2]+=700;
					document.getElementById("bt2").style.backgroundColor = "magenta";
					document.getElementById("bt2").style.backgroundImage = "url('buttonG.jpeg')";
				}
				if(data[3]==3){
					score[3] += 800;
					
				}else if(data[3]==4){
					score[3] += 900;
					
				}
				else if(data[3]==1){
					score[3]+=700;
					document.getElementById("bt3").style.backgroundColor = "magenta";
					document.getElementById("bt3").style.backgroundImage = "url('buttonG.jpeg')";
				}
				if(data[1]==3){
					score[1]+=800;
				}else if(data[1]==4){
					score[1]+=900;
				}
				alert("Foolish Police :(");
				document.getElementById("bt4").style.backgroundColor = "black";
				document.getElementById("bt4").style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
				
		}
		WhoWin();
		turnChecker += 1;
		
	}
}
else{
		alert("You are not Police!!");
}
}

function selectTwo(){
var check = document.getElementById("bt2").value;
var checkYou = document.getElementById("bt4").value;
	if(checkYou=="700"){
	if(check!=""){
			alert("Invalid Click");
	}else{	
		for(var a=1;a<=4;a++)
		{
			if(data[a]==1){
				
				document.getElementById("bt"+a).value = "000";
			}
			if(data[a]==3){
				
				document.getElementById("bt"+a).value = "800";
			}
			if(data[a]==4){
				
				document.getElementById("bt"+a).value = "900";
			}
		}
		
		if(data[2]==1){
				score[2] += 0;
				score[4] += 700;
				if(data[1]==3){
					score[1] += 800;
						
				}else if(data[1]==4){
					score[1] += 900;
						
				}
				if(data[3]==3){
					score[3] += 800;
					
				}else if(data[3]==4){
					score[3] += 900;
					
				}
				document.getElementById("bt2").style.backgroundColor = "black";
				document.getElementById("bt2").style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
		}else{
					
					score[4] += 0;
					
				if(data[1]==3){
					score[1] += 800;
						
				}else if(data[1]==4){
					score[1] += 900;
						
				}else if(data[1]==1){
					score[1] +=700;
					document.getElementById("bt1").style.backgroundColor = "magenta";
					document.getElementById("bt1").style.backgroundImage = "url('buttonG.jpeg')";
				}
				if(data[3]==3){
					score[3] += 800;
					
				}else if(data[3]==4){
					score[3] += 900;
					
				}
				else if(data[3]==1){
					score[3] +=700;
					document.getElementById("bt3").style.backgroundColor = "magenta";
					document.getElementById("bt3").style.backgroundImage = "url('buttonG.jpeg')";
				}
				if(data[2]==3){
					score[2] +=800;
				}else if(data[2]==4){
					score[2] +=900;
				}
				alert("Foolish Police :(");
				document.getElementById("bt4").style.backgroundColor = "black";
				document.getElementById("bt4").style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
				
		}
		WhoWin();
		turnChecker += 1;
	}
}
else{
		alert("You are not the Police!!");
}
}
function selectThree(){
var check = document.getElementById("bt3").value;
var checkYou = document.getElementById("bt4").value;
	if(checkYou=="700"){
	if((check!="")){
			alert("Invalid Click");
	}else{	
		for(var a=1;a<=4;a++)
		{
			if(data[a]==1){
				
				document.getElementById("bt"+a).value = "000";
			}
			if(data[a]==3){
				
				document.getElementById("bt"+a).value = "800";
			}
			if(data[a]==4){
				
				document.getElementById("bt"+a).value = "900";
			}
		}
		
		if(data[3]==1){
				score[3] += 0;
				score[4] += 700;
				if(data[2]==3){
					score[2] += 800;
						
				}else if(data[2]==4){
					score[2] += 900;
						
				}
				if(data[1]==3){
					score[1] += 800;
					
				}else if(data[1]==4){
					score[1] += 900;
					
				}
				document.getElementById("bt3").style.backgroundColor = "black";
				document.getElementById("bt3").style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
		}else{
					
					score[4] += 0;
				if(data[2]==3){
					score[2] += 800;
						
				}else if(data[2]==4){
					score[2] += 900;
						
				}else if(data[2]==1){
					score[2] +=700;
					document.getElementById("bt2").style.backgroundColor = "magenta";
					document.getElementById("bt2").style.backgroundImage = "url('buttonG.jpeg')";
				}
				if(data[1]==3){
					score[1] += 800;
					
				}else if(data[1]==4){
					score[1] += 900;
					
				}
				else if(data[1]==1){
					score[1] +=700;
					document.getElementById("bt1").style.backgroundColor = "magenta";
					document.getElementById("bt1").style.backgroundImage = "url('buttonG.jpeg')";
				}
				if(data[3]==3){
					score[3] +=800;
				}else if(data[3]==4){
					score[3] +=900;
				}
				alert("Foolish Police :(");
				document.getElementById("bt4").style.backgroundColor = "black";
				document.getElementById("bt4").style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
				
		}
		WhoWin();
		turnChecker += 1;
	}	
}
else{
		alert("You are not the Police!!");
}
}

function selectFour(){
		alert("Invalid Click");
}

function CPU(x,y,z,p){
		if(data[x]==1){
			score[x] += 0;
			score[p] +=700;
			if(data[y]==3){
				score[y] +=800;
			}else if(data[y]==4){
				score[y] += 900;
			}
			if(data[z]==3){
				score[z] += 800;
			}else if(data[z]==4){score[z] += 900;}
				document.getElementById("bt"+x).style.backgroundColor = "black";
				document.getElementById("bt"+p).style.backgroundColor = "magenta";
				document.getElementById("bt"+x).style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("bt"+p).style.backgroundImage = "url('buttonG.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
		}else {
			score[p] +=0;
			if(data[y]==1){score[y] +=700;document.getElementById("bt"+y).style.backgroundColor = "magenta";
				document.getElementById("bt"+y).style.backgroundImage = "url('buttonG.jpeg')";}
			else if(data[y]==3){score[y] += 800;}
			else if(data[y]==4){score[y] += 900;}
			if(data[z]==1){score[z] +=700;document.getElementById("bt"+z).style.backgroundColor = "magenta";
				document.getElementById("bt"+z).style.backgroundImage = "url('buttonG.jpeg')";}
			else if(data[z]==3){score[z] += 800;}
			else if(data[z]==4){score[z] +=900;}
			if(data[x]==3){score[x] +=800;}
			else if(data[x]==4){score[x] += 900;}
				document.getElementById("bt"+p).style.backgroundColor = "black";
				document.getElementById("bt"+p).style.backgroundImage = "url('buttonR.jpeg')";
				document.getElementById("sr1").value = score[1];
				document.getElementById("sr2").value = score[2];
				document.getElementById("sr3").value = score[3];
				document.getElementById("sr4").value = score[4];
		}
		
		
		
}

var rnd=0;
function showOthers(){
	
	for(var a=1;a<=4;a++)
		{
			if(data[a]==1){
				
				document.getElementById("bt"+a).value = "000";
			}
			if(data[a]==3){
				
				document.getElementById("bt"+a).value = "800";
			}
			if(data[a]==4){
				
				document.getElementById("bt"+a).value = "900";
			}
		}
		
	
}
function anotherPolice(){
		if(police == 1){
			rnd = getRandom(1,3);
				if(rnd == 1){CPU(2,3,4,1);}
				else if(rnd == 2){CPU(3,2,4,1);}
				else if(rnd == 3){CPU(4,2,3,1);}
		
				showOthers();
				
		}else if(police ==2){
			rnd = getRandom(1,3);
			if(rnd == 1){CPU(1,3,4,2);}
				else if(rnd == 2){CPU(3,1,4,2);}
				else if(rnd == 3){CPU(4,1,3,2);}
				
				showOthers();
		
		}else if(police == 3){
			rnd = getRandom(1,3);
			if(rnd == 1){CPU(1,2,4,3);}
				else if(rnd == 2){CPU(2,1,4,3);}
				else if(rnd == 3){CPU(4,1,2,3);}
			
				showOthers();
		
		}
}


var genText = document.getElementById("gen");
var ck;

function genarate(){
		
	
		if((document.getElementById("bt4").value=="700")&&
		(document.getElementById("bt1").value=="")&&
		(document.getElementById("bt2").value=="")&&
		(document.getElementById("bt3").value=="")){
				alert("You can not skip your turn !");
		}else{
					show();
		anotherPolice();
		WhoWin();
			}

}
function WhoWin(){
		if(document.getElementById("gen").value = "Play Again"){document.getElementById("gen").value = "Generate";}
		//else{
		if(score[1]>=10000){
			alert("Congratulation North! you win :)");
			
			document.getElementById("gen").value = "Play Again";
			ck = confirm("Do you wanna Play Again?");if(ck){newGame();}else{window.location.assign("initial.html");}
			}
		else if(score[2]>=10000){
			alert("Congratulation West! you win :)");
			
			document.getElementById("gen").value = "Play Again";
			ck = confirm("Do you wanna Play Again?");
			if(ck){newGame();}else{window.location.assign("initial.html");}
			}
		else if(score[3]>=10000){
			alert("Congratulation East! you win :)");
			
			document.getElementById("gen").value = "Play Again";
			ck = confirm("Do you wanna Play Again?");
			if(ck){newGame();}else{window.location.assign("initial.html");}
			}
		else if(score[4]>=10000){
			alert("Congratulation ! you win :)");
			
			document.getElementById("gen").value = "Play Again";
			ck = confirm("Do you wanna Play Again?");
			if(ck){newGame();}else{window.location.assign("initial.html");}
			}
			
		//}
}
function newGame(){
		for(var b = 1; b<=4; b++){score[b]=0;}
}

window.addEventListener("load",doFirst,false);
