var arrInput = new Array(0);
  var arrInputValue = new Array(0);

function readcurrentscores()
{
	  	document.data.user.value = "Anonymous";
		document.data.str.value = puzzle_orig;
	  	document.data.time.value = 0;
	  	document.data.submit();
	
}

function getInfo(timedif)
{
	if (result == 100) {
		document.data.user.value = username;
		document.data.str.value = puzzle_orig;
	  	document.data.time.value= timedif;
	  	document.data.submit();

	}
}
function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
  result = check(kmax);  		

  if (result == 100 ) {
  		if ( timerRunning) {
  			document.getElementById('resultaat').innerHTML= "You have solved the puzzle!";
  			StopTheClock()
       		stoptime = new Date();
       		timedif = Math.floor((stoptime.getTime() - starttime.getTime())/1000);
       		timestring = calctimestring(timedif);
  			document.getElementById('resultaat').innerHTML="You have solved the puzzle in " + timestring; 
			document.getElementById("timer").innerHTML = "Your time: " + timestring;
			username = prompt("You did a great job .... tell me your name!");
			getInfo(timedif);
		}
		else {
			 document.getElementById('resultaat').innerHTML= "You have already solved the puzzle.";

		}
	}
  	else {
  		if (result == 1) {
	  		document.getElementById('resultaat').innerHTML="<span style='color:#FF0000; font-family:Arial;'>You've got some inconsistency ...</span>";
  		}
  		else  {
  			document.getElementById('resultaat').innerHTML="<span style='color:#000000; font-family:Arial;'>You have not yet solved the puzzle ...</span>";
  		}
  	}
}  
function calctimestring(inputtime){
       hours = 0;
       minutes = 0;
       seconds = inputtime;
       while (seconds > 59) {
          seconds = seconds - 60;
          minutes = minutes + 1;
       }
       while (minutes > 59) {
          minutes = minutes - 60;
          hours = hours + 1;
       }
       resultstring = " ";
      hourstring = ""+hours;
      minutestring = ""+minutes;
      secondstring = ""+seconds;
      if (hours > 0) {
         resultstring = resultstring + hourstring + ":";
      }
       if (minutes <= 9 && hours > 0) {
         resultstring = resultstring + "0" + minutestring + ":";
       }
       else {
         resultstring = resultstring + minutestring + ":";
       }
       if (seconds <= 9) {
         resultstring = resultstring + "0" + secondstring;
       }
       else {
         resultstring = resultstring + secondstring;
       }
      return resultstring;
   }
function addInput(celvalue,type, disabled, colorstring) {
  arrInput.push(arrInput.length);
  arrInputValue.push(celvalue);
  display(type,disabled, colorstring);
}

function display(type, disabled, colorstring) {
  for (intI=arrInput.length - 1;intI<arrInput.length;intI++) {
    document.getElementById('parah').innerHTML+=createInput(arrInput[intI], arrInputValue[intI],type,disabled, colorstring);
  }
}


function createInput(id,value,type, disabled, colorstring) {
	if (disabled == "disabled") {fontweight = "bold"; fontsize = 36;} else {fontweight = "italic"; fontsize = 28;}
	return "<input " + disabled + " size=2  type='" + type + "' style='color: #000000; font-family: Verdana; text-align:center;  vertical-align:middle; font-weight:" 
	+ fontweight + "; font-size: " + fontsize + "px; width:60px; height:60px; background-color:" 
	+ colorstring + "'  id='inpcel" + id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'>";
}

function deleteInput() {
  if (arrInput.length > 0) { 
     arrInput.pop(); 
     arrInputValue.pop();
  }
  display(); 
}
function check(kmax) {
	solved = "true";

	error = 0;

	
	cel = new Array(8);
	counter = new Array(10);
	

	for(i=1;i<=kmax;i++){ 
		
		cel[i] = new Array(8);
		
		for(j=1;j<=kmax;j++){ 
		
			k = kmax * (i-1) + j - 1;
		
			cel[i][j] = arrInputValue[k];
		
		}
	}
	
	for (i=0;i<=kmax+1;i++) {counter[i] = 0;}

	for(i=1;i<=kmax;i++){ 
		
		for(j=1;j<=kmax;j++){ 
			
			if ((i !== 1 && i !== kmax) || (j !== 1 && j !== kmax)) {
				if (cel[i][j] =="0" || cel[i][j]  =="1" || cel[i][j] =="2" || cel[i][j] =="3" || cel[i][j] =="4" || cel[i][j] =="5" || cel[i][j] =="6" || cel[i][j] =="7" || cel[i][j] =="8" || cel[i][j] =="9") {
					for(k=1;k<=kmax;k++){
						if((k !== 1 && k !== kmax) || (i !== 1 && i !== kmax)){
							if ((k !== j) && (cel[i][j] == cel[i][k].substring(0,1) || cel[i][j] == cel[i][k].substring(1,2) || cel[i][j] == cel[i][k].substring(2,3))) {
								solved="false";
								error=1;
							}
						}
						if((k !== 1 && k !== kmax) || (j !== 1 && j !== kmax)){
							if ((k !== i) && (cel[i][j] == cel[k][j].substring(0,1) || cel[i][j] == cel[k][j].substring(1,2) || cel[i][j] == cel[k][j].substring(2,3))) {
								solved="false";
								error=1;
							}
						}
					}
					//      nu diagonaal
					for (k = 1; k<= kmax; k++) {
						m = i - j + k;
						if (m >= 1 && m <= kmax && ((k !== 1 && k !== kmax) || (m !== 1 && m !== kmax))) {
							if ((m !== i) && (cel[i][j] == cel[m][k].substring(0,1) || cel[i][j] == cel[m][k].substring(1,2) || cel[i][j] == cel[m][k].substring(2,3))) {
								error=1;
								solved="false";
							}
						}
						m = i + j - k;
						if (m >= 1 && m <= kmax && ((k !== 1 && k !== kmax) || (m !== 1 && m !== kmax))) {
							if ((m !== i) && (cel[i][j] == cel[m][k].substring(0,1) || cel[i][j] == cel[m][k].substring(1,2) || cel[i][j] == cel[m][k].substring(2,3))) {
								error=1;
								solved="false";
							}
						}
					}
					if (cel[i][j] =="0") {
						counter[0]++;
					}
					if (cel[i][j] =="1") {
						counter[1]++;
					}
					if (cel[i][j] =="2") {
						counter[2]++;
					}
					if (cel[i][j] =="3") {
						counter[3]++;
					}
					if (cel[i][j] =="4") {
						counter[4]++;
					}
					if (cel[i][j] =="5") {
						counter[5]++;
					}
					if (cel[i][j] =="6") {
						counter[6]++;
					}
					if (cel[i][j] =="7") {
						counter[7]++;
					}
					if (cel[i][j] =="8") {
						counter[8]++;
					}
					if (cel[i][j] =="9") {
						counter[9]++;
					}
				}
				else {
					solved="false";
				}
			} //if niet een hoek
		} //for j
	} //for i
	
	document.getElementById('progressbars').innerHTML = "";
	
	for(i=0;i<=kmax+1;i++){
		if (counter[i] !== kmax-2) {solved="false";}
		
		document.getElementById('progressbars').innerHTML+= progressbar(i, counter[i],kmax-2);
		
	}

	if (solved == "true") { return 100 ; } else {return error;}
}


function progressbar(i,counter,maxnumber) {
 
 if ( counter == null ) {    return null;}
  

 var html =  "<table width='240px' cellspacing='1' cellpadding='0' border='0'>";

 html += "<tr><td>" + i + "</td>";

  

 for( var ix = 0; ix < maxnumber ; ix++ )

 {
 
 if(counter>maxnumber) 
 
          html += "<td style='background-color:#FF0000;' nowrap='nowrap'>&nbsp;</td>";	
 
 else {

     if( ix < counter )

         html += "<td style='background-color:#00FF00;' nowrap='nowrap'>&nbsp;</td>";

     else

         html += "<td style='background-color:#3333FF;' nowrap='nowrap'>&nbsp;</td>"; 
     }

 }

  

 html += "<td>&nbsp;&nbsp;&nbsp;" + counter + "</td></tr>";

 html += "</table>";

 return html;


}



