curr=0;
score=0;
coins_used=0;
level=1;
var a;

function next()
{   
   enable();
   document.getElementById("clr").disabled=false;
   level++;
   if(level>11)    a=Math.floor((Math.random()*10000)+100);
   else            a=Math.floor((Math.random()*1000)+100);
   if(level>=20)
   {
    document.getElementById("skip").innerHTML="Finish";
	document.getElementById("next").innerHTML="Finish";
	disable();
   }
   if(level>20)
   {
   if(score>180)    
   alert("Congratulations, You have performed excellent.\n\nSCORE : "+score); 
   else if(score>120) 
   alert("Congratulations, You have performed Well. You can try for even higher score.\n\nSCORE : "+score); 
   else if(score>80) 
   alert("Your performance is Average. You can still improve.\n\nSCORE : "+score); 
   else if(score<80) 
   alert("Your Performance is Poor. You should improve it. \n\n SCORE : "+score); 
   //level=1;
   //start();
   }
   else
   {      
   document.getElementById("correct").style.display="none"; 
   document.getElementById("next").style.display="none";   
   document.getElementById("wrong").style.display="none";
   document.getElementById("clr").style.display="block";   
   document.getElementById("skip").style.display="inline";     

   document.getElementById("q").innerHTML="Question : "+level+"/20";// || Money  to make :"+a;
   coins_used=0; 
   curr=0;
   var input = $('#vMoney');
   input.val("Money: " + curr);
   var input = $('#vCoins');
   input.val("Coins used: " + coins_used);
   }
   a=Math.floor((Math.random()*1000)+100);
   document.getElementById("lbl1").innerHTML="Money  to make : "+a;
   
 }
 function disable()
 {
document.getElementById("n1").disabled = true;
document.getElementById("n2").disabled = true;
document.getElementById("n5").disabled = true;
document.getElementById("n10").disabled = true;
document.getElementById("n20").disabled = true;
document.getElementById("n50").disabled = true;
document.getElementById("n100").disabled = true;
document.getElementById("n500").disabled = true;
document.getElementById("n1000").disabled = true;

}
function enable()
 {
   document.getElementById("n1").disabled = false;
   document.getElementById("n2").disabled = false;
   document.getElementById("n5").disabled = false;
   document.getElementById("n10").disabled = false;
   document.getElementById("n20").disabled = false;
   document.getElementById("n50").disabled = false;
   document.getElementById("n100").disabled = false;
   document.getElementById("n500").disabled = false;
   document.getElementById("n1000").disabled = false;
}

function clearval()
{ 
   enable();
   curr=0;
   coins_used=0;
     
   document.getElementById("wrong").style.display="none";  
   var input = $('#vMoney');
   input.val("Money: " + curr);
   var input = $('#vCoins');
   input.val("Coins used: " + coins_used);  
	var input = document.getElementById("score");
    input.innerHTML = "Score : "+score;   
}

function add(val,trgt)
{
 if(document.getElementById("n1").disabled == true)
  return false;
  
   curr=Number(curr)+Number(val);
   coins_used=Number(coins_used)+1;
   
   if(curr>trgt)
   {
   //alert("Sorry, You lost the game");    
   document.getElementById("clr").style.display="block";
   document.getElementById("wrong").style.display="block";
   score=Number(score)-1; 
	var input = document.getElementById("score");
    input.innerHTML = "Score : "+score;
   //coins_used=0; 
   //curr=0;
   disable();
   }
   else if(curr==trgt)
   {
    score=Number(score)+10;
	var input = document.getElementById("score");
    input.innerHTML = "Score : "+score;
   document.getElementById("skip").style.display="none";
    document.getElementById("correct").style.display="block";
   document.getElementById("next").style.display="inline";
   document.getElementById("clr").disabled=true;
   disable();
   }
   else
   {
   //coins_used=Number(coins_used)+1;   
   }
 }
 
 function exit()
 {
  enable();
  var r=confirm("Do you really want to exit?");
  if (r==true)
  window.location="index.html";
 }
 function restart()
 {
  if(confirm('Are you sure you want to restart?'))
   start();  
 }
 
 function start()
 {
  level=1;
   enable();
   curr=0;
   coins_used=0;
   score=0;   
   a=Math.floor((Math.random()*1000)+100);
   document.getElementById("lbl1").innerHTML="Money  to make : "+a;
   document.getElementById("q").innerHTML = "Question : "+level+"/20";
   var input = $('#vMoney');
   
   input.val("Score: " + curr);
   input.val("Money: " + curr);
   var input = $('#vCoins');
   input.val("Coins used: " + coins_used);
   var input = document.getElementById("score");
    input.innerHTML = "Score : "+score;
 }
