
// Activate cloak


var wNumbers =  document.valores.valores_qtde.value; // number of numbers used in wheel
var limite = document.form1.rangeend.value;
var wSize = 12 // number of combinations produced by wheel
var Boxes = new Array(wNumbers)
var NextBox = 0 // Position of next insert in array
var ray = new Array(wSize)
	
	
  for (i=0;i<wNumbers;i++) {Boxes[i] = -1}
  for (i=0;i<wSize;i++) {ray[i] = ""}
  function ResetBoxes() {
     for (i=0;i<wNumbers;i++){Boxes[i] = -1}
     NextBox = 0
  }
  function ShowBoxes() {
		for (i=0; i < wNumbers;++i) {
			n = Boxes[i]
			if (n < 0) {
				r = ""
			} else {
				r = ( n < 10) ? "0"+n:n
			}
			document.form1["N"+i].value = ""+r
		}
		document.form1.NrsUsed.value = NextBox
	}
	function UnCheck(n) {
		for (i=0; i < NextBox;i++) {
			if (Boxes[i] == n) {
				for (j=i+1;j < NextBox;j++) {
					Boxes[j-1] = Boxes[j]
				}
				NextBox -= 1
              Boxes[NextBox] = -1
				break;
			}
		}
		ShowBoxes()
	}
	function Checker(nBox) {
    t = nBox.value.charAt(0)=="0" ? nBox.value.charAt(1):nBox.value
    n = parseInt(t)
    if (nBox.checked) {
			if (NextBox < wNumbers) {
				Boxes[NextBox] = n
			 	document.form1["N"+NextBox].value = nBox.value
			 	NextBox += 1
    		document.form1.NrsUsed.value = NextBox
			} else {
        alert("Você já tem "+wNumbers+" números selecionados Click em Combinar.")
				nBox.checked = false
			}
		} else {
			UnCheck(n)
		}
	}  // Checker(nBox)
  function QuickSort(L,R) {
    i = L
    j = R
    x = ray[Math.round((L+R)/2)]
    while (true) {
      while (ray[i] < x) {
        ++i
      }
      while (x < ray[j]) {
        --j
      }
      if (i <= j) {
        t = ray[i]
        ray[i] = ray[j]
        ray[j] = t
        ++i
        --j
      }  
      if (i > j) {
        break;
      }
    }
    if (L < j) {QuickSort(L,j)}
    if (i < R) {QuickSort(i,R)}
  }
	function ReCheck(BoxNr) { // Called when number in table is directly changed
    var n = document.form1["N"+BoxNr].value
    var i
		if (n == "") { //box was deleted
			if (Boxes[BoxNr] >= 0) {//it had a good value
				n = Boxes[BoxNr]
				document.form1["P"+n].checked = false
				UnCheck(n)
			} // ignore if it was already blank
		} else { //box must have been changed
			n = eval("1"+n)-100
			if (n > (limite) || n < 0) { // have invalid value
				alert(" Número Inválido! Dezena está fora da faixa permitida. ")
		    ShowBoxes()
		    return
			}
      if (document.form1["P"+n].checked) {
        alert("O número "+document.form1["P"+n].value+" já foi escolhido!")
        ShowBoxes()
      } else { // New Number was not already used
        if (Boxes[BoxNr] >= 0) { // Had a different number in box
    	   	document.form1["P"+Boxes[BoxNr]].checked = false
	      } else { // Box had no entry
        	while ( BoxNr > 0 && Boxes[BoxNr-1]==-1)
            {--BoxNr}
		   		NextBox = NextBox + 1
        }
  			document.form1["P"+n].checked = true
        Boxes[BoxNr] = n
		    ShowBoxes()
      }
    }
  }

 
  // Deactivate cloak -->





