/*************************************************************
*  Lotto.js Language ef - spanish
*  
*  LotoEditor()
**************************************************************/
var lte_aMethods = [1, 44, 7, 28, 84, 210, 462];
var lte_aMaxNumb = [6,  5, 7,  8,  9,  10,  11];
var currentBlock;
var currentEditor;

function LotoEditor()
{
	this.version = "1.0.1.0";
	this.method = 0;
	this.click = lte_click;
	this.command = lte_command;
	this.setMethod = lte_setMethod;
	this.save = lte_save;
	this.blocks = [];
	for (var i = 0; i < 8; i++) this.blocks[this.blocks.length] = new Block(this, i);
	this.getValidBlocks = lte_getValidBlocks;
	this.onchange = null;
	this.getFactor = lte_getFactor;;
	this.clear = lte_clear;
	this.random = lte_random;
	this.setNumbers = lte_setNumbers 
}

function lte_getFactor()
{
	return lte_aMethods[this.method];
}

function lte_command(index, n)
{
	var block = this.blocks[index];
	
	if (!n) n = 0;
	if (!block.disabled) 
	{
		block.command(n);
		block.enable(true);
	}	
}

function lte_random()
{
	currentEditor = this;
	__randomizeAll(Math.max(Math.round(Math.random() * 7), 3));
}

function __randomizeAll(iTimes)
{
	var loop = currentEditor.method ? 1 : currentEditor.blocks.length;
	for (var i = 0; i < loop; i++)
	{ 
		var block = currentEditor.blocks[i];
		block.enable(true);
		block.clear();
		var j = 0;
		while (block.len < lte_aMaxNumb[currentEditor.method])
		{
			var num = Math.round(Math.random() * 49);
			if (num) block.click(num);
			if (++j > 100) exit;
		}
	}
	if (--iTimes) 
	{
		window.setTimeout( "__randomizeAll(" + iTimes + ")", 100);
	}
}

function lte_setNumbers(text)
{
	this.setMethod(0);
	currentEditor = this;
	currentEditor.sNewText = text;
	var a = currentEditor.sNewText.split(",");
	if (a.length)
	{
		var b = a[0].split(" ");
		var method = 0;
		if (b.length != 6) method = b.length == 5 ? 1 : b.length - 5;
		this.setMethod(method);
		window.setTimeout( "_setNumbers(" + 0 + "," + method + ")", 100);
	}	
}

function _setNumbers(iIndex, method)
{
	var a = currentEditor.sNewText.split(",");
	if (iIndex < a.length)
	{
		var block = currentEditor.blocks[iIndex];
		block.clear();
		var b = a[iIndex].split(" ");
		for (var i = 0; i < b.length; i++) block.click(b[i]);
		block.enable(true);
		if (!method) window.setTimeout( "_setNumbers(" + ++iIndex + "," + method + ")", 100);
	}
}

function lte_save()
{
	var text = "";
	for (var i = 0; i < this.blocks.length; i++)
	{
		if (this.blocks[i].valid) 
		{
			text += this.blocks[i].numbers + " ";
		}
		else
		{
			break;
		}	
	}
	document.getElementById("_led_numbers").value = text.substr(0, text.length - 1);
}

function lte_getValidBlocks()
{
	if (this.method) return this.blocks[0].valid ? 1 : 0;
	for (var i = 0; i < this.blocks.length; i++)
	{
		if (!this.blocks[i].valid) return i;
	}
	return this.blocks.length;
}

function lte_click(index, n)
{
	var block = this.blocks[index];
	if (!block.disabled)
	{
		block.click(n);
		block.enable(true);
	}	
}

function lte_setMethod(method)
{
	this.clear();
	var o;

	//alert("XXXXXXXX: " + document.getElementById("tblMethodsPiano").innerHTML);
	with (document.getElementById("tblMethodsPiano").rows.item(0))
	{
		for (var i = 0; i < cells.length; i++) cells.item(i).className = "";
		if (method) o = cells.item(method - 1);
	}
	
	if (this.lastMethodTd) this.lastMethodTd.className = ""; 
	if (method == 0 || this.method == method)
	{
		this.method = 0;
		this.lastMethodTd = null;
	}
	else
	{
		this.method	= method;
		if (o) o.className = "slectedCell";
		this.lastMethodTd = o;
	}
	//document.getElementById("numOfBalls").innerHTML = lte_aMaxNumb[this.method];
	this.blocks[0].enable(true);
}

function lte_clear()
{
	for (var i = 0; i < 8; i++) this.blocks[i].clear();
	this.blocks[0].enable(true);
}
//lotoForm02

/*************************************************************
*  Lotto.js Language ef - spanish
*  
*  Block()
**************************************************************/

function Block(owner, i)
{
	this.owner = owner
	this.index = i;
	this.selectCell = lteb_selectCell
	this.click = lteb_click;
	this.disabled = true;
	this.enable = lteb_enableBlock;
	this.command = lteb_command;
	this.enable(!this.index);
  this.clear = lteb_clear;
  this.add = lteb_add;
  this.remove = lteb_remove;
  this.random = lteb_random;
  this.numbers = "";
  this.len = 0;
  this.valid = false;
  
}

function lteb_clear()
{
	var table = document.getElementById("blook" + this.index);
	for (var i = 0; i < table.rows.length; i++)
	{
		var cells = table.rows.item(i).cells;
		for (var j = 0; j < cells.length; j++)
		{
			if (i || j) cells.item(j).className = "";
		}	
	}
	this.len = 0;
  this.numbers = "";
  this.valid = false;
}

function lteb_add(n)
{
	//alert("," + this.numbers + ",   |" + "," + n + ",")
	if (("," + this.numbers + ", ").indexOf("," + n + ",") == -1)
	{
		var a = [];
		if (this.numbers) a = this.numbers.split(",");

		if (a.length < lte_aMaxNumb[this.owner.method])
		{
			a[a.length] = n;
			this.len = a.length;
			this.numbers = a.join(",");
			return true;
		}	
	}	
}

function lteb_remove(n)
{
	re = new RegExp("," + n + ",");
	this.numbers = ("," + this.numbers + ",").replace(re, ",");
	this.numbers = this.numbers.substr(1, this.numbers.length - 2);
	var a = [];
	if (this.numbers) a = this.numbers.split(",");
	this.len = a.length;
}

function lteb_click(n)
{
	if (!this.disabled)
	{
		var row = n % 10;
		var cell = Math.floor(n / 10);
		this.selectCell(row, cell);
		this.valid = (this.len == lte_aMaxNumb[this.owner.method]);
	}	
}

function lteb_random()
{
	currentBlock = this;
	runRandomStack(Math.max(Math.round(Math.random() * 13), 5));
}

function runRandomStack(iTimes)
{
	currentBlock.clear();
	while (currentBlock.len < lte_aMaxNumb[currentBlock.owner.method])
	{
		var num = Math.round(Math.random() * 49);
		if (num) currentBlock.click(num);
	}
	if (--iTimes) 
	{
		window.setTimeout( "runRandomStack(" + iTimes + ")", 100);
	}
}


function lteb_command(n)
{
	if (!this.disabled)
	{
		this.clear();
		if (n == 0) this.random();
	}	
}

function lteb_selectCell(row, cell)
{
	var table = document.getElementById("blook" + this.index);
	var td = table.rows.item(row).cells.item(cell);
	//var selected = td.className != "";
	var selected =  (("," + this.numbers + ", ").indexOf("," + td.innerHTML + ",") > -1)
	
	if (selected) 
	{
		td.className = "";
		this.remove(td.innerHTML);
	}	
	else
	{
		if (this.add(td.innerHTML))	td.className = "slectedCell";
	}
	window.status = this.len + " numbers selected";
}

function lteb_enableBlock(isEnabled)
{
	var sleepClass = this.owner.method ? "tblBlockSleep" : "tblBlockDim";
	var table = document.getElementById("blook" + this.index);
	btn = document.getElementById("btnBlock" + this.index);  
	btn.disabled = !isEnabled;
	//btn.value = this.len ? "Borrar" : "Autom" + String.fromCharCode(225) + "tico";
	//btn.value = this.len ? "Borrar" : "Llenar";
	btn.value = this.len ? btn.src ="images/borrar.gif" :btn.src ="images/automatico.gif" ;
	btn.mode = this.len; 
	this.disabled = !isEnabled;
	table.className =  isEnabled ? "tblBlock" : sleepClass;

	var btn = document.getElementById("btnBlock" + this.index);
	btn.style.visibility = isEnabled ? "" : "hidden";
	//alert(this.index+ " "+isEnabled);
	if (this.index>3&&isEnabled)segundaFila.style.display="block";
	
	if (this.index < 7 && this.owner.blocks[this.index + 1])
	{
		this.owner.blocks[this.index + 1].enable(isEnabled ? (this.owner.method ? false : this.valid) : false, sleepClass);
	}
	if (this.owner.onchange) this.owner.onchange(this.owner);
}


function Quicksort(vec, loBound, hiBound)
/**************************************************************
	This function adapted from the algorithm given in:
		Data Abstractions & Structures Using C++, by
		Mark Headington and David Riley, pg. 586.

	Quicksort is the fastest array sorting routine for
	unordered arrays.  Its big O is n log n.
 **************************************************************/
{

	var pivot, loSwap, hiSwap, temp;

	// Two items to sort
	if (hiBound - loBound == 1)
	{
		if (vec[loBound] > vec[hiBound])
		{
			temp = vec[loBound];
			vec[loBound] = vec[hiBound];
			vec[hiBound] = temp;
		}
		return;
	}

	// Three or more items to sort
	pivot = vec[parseInt((loBound + hiBound) / 2)];
	vec[parseInt((loBound + hiBound) / 2)] = vec[loBound];
	vec[loBound] = pivot;
	loSwap = loBound + 1;
	hiSwap = hiBound;

	do {
		// Find the right loSwap
		while (loSwap <= hiSwap && vec[loSwap] <= pivot)
			loSwap++;

		// Find the right hiSwap
		while (vec[hiSwap] > pivot)
			hiSwap--;

		// Swap values if loSwap is less than hiSwap
		if (loSwap < hiSwap)
		{
			temp = vec[loSwap];
			vec[loSwap] = vec[hiSwap];
			vec[hiSwap] = temp;
		}
	} while (loSwap < hiSwap);

	vec[loBound] = vec[hiSwap];
	vec[hiSwap] = pivot;


	// Recursively call function...  the beauty of quicksort

	// 2 or more items in first section		
	if (loBound < hiSwap - 1)
		Quicksort(vec, loBound, hiSwap - 1);


	// 2 or more items in second section
	if (hiSwap + 1 < hiBound)
		Quicksort(vec, hiSwap + 1, hiBound);
}

function SortNumbers(blocksString) 
{
	alert(blocksString);
	
	var newNumbers = "";	
	var blocksArray = blocksString.split(' ');

	for (i=0; i < blocksArray.length; i++) {
		vec = blocksArray[i].split(',');
		Quicksort(vec,0,vec.length);
		var vecString = "" + vec[0];
		for (j=1; j<vec.length; j++) {
			vecString = vecString + "," + vec[j];
		}
		newNumbers = newNumbers + vecString + " ";
	}
	
	newNumbers = newNumbers.substr(0,newNumbers.length-1);	
  	
  	alert(newNumbers);
  	return newNumbers;
}

