var validar=function(evt)
{
   evt = evt || window.event;
   var cd= evt.which || evt.keyCode;
   //alert (cd);
   if ((!(cd > 45 && cd < 58) && !(cd > 95 && cd < 107) && cd != 9 && cd != 8 && cd != 37 && cd != 39) || (evt.ctrlKey == 1)) evt.preventDefault();
};  

function esconder_msj() {
	
	$('#msj_flash').show('slow');
	
	window.setTimeout(function() {
		 $('#msj_flash').hide('slow');
		}, 3500);
}

function actualizarOctavos()
{
	var arr = document.getElementById('tabla_grupo').getElementsByTagName("tr");
	$('#idprimero').val(arr[1].cells[9].innerHTML);
	$('#idsegundo').val(arr[2].cells[9].innerHTML);
	
	var img1 = document.createElement("img");
	img1.src=arr[1].cells[11].innerHTML;
	img1.className="imgbandera";
	img1.width="25";
	img1.height="25";

	$('#LocalOctavos1').empty();
	$('#LocalOctavos1').append(img1);

	var img2 = document.createElement("img");
	img2.src=arr[2].cells[11].innerHTML;
	img2.className="imgbandera";
	img2.width="25";
	img2.height="25";

	$('#VisitanteOctavos2').empty();
	$('#VisitanteOctavos2').append(img2);
}

/***********************************************
* Dynamic Countdown script- © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
/*
function cdtime(container, targetdate){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.currentTime=new Date()
this.targetdate=new Date(targetdate)
this.timesup=false
this.updateTime()
}

cdtime.prototype.updateTime=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

cdtime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}

cdtime.prototype.showresults=function(){
var thisobj=this


var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////

//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left

//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc


function formatresults(){
if (this.timesup==false){//if target date/time not yet met
	var displaystring="<span class='lcdstyle'><strong>"+arguments[0]+"</strong> / "+arguments[1]+" / "+arguments[2]+" / "+arguments[3]+"</span>"
}
else{ //else if target date/time met
var displaystring="Future date is here!"
}
return displaystring
}


function formatresults2(){
if (this.timesup==false){ //if target date/time not yet met
var displaystring="<span class='lcdstyle'>"+arguments[0]+" <sup>days</sup> "+arguments[1]+" <sup>hours</sup> "+arguments[2]+" <sup>minutes</sup> "+arguments[3]+" <sup>seconds</sup></span>"
}
else{ //else if target date/time met
var displaystring=""; //Don't display any text
alert("Christmas is here!"); //Instead, perform a custom alert
}
return displaystring
}
*/

/* Funciones inArray y convertir_a_conjunto: utilidades de arreglos */
Array.prototype.inArray = function (value)
{
	// Returns true if the passed value is found in the
	// array. Returns false if it is not.
	var i;
	for (i=0; i < this.length; i++) 
	{
		if (this[i] == value) 
		{
		return true;
		}
	}
	return false;
}

function convertir_a_conjunto(arr)
{
	conj = new Array();
	for(i=0;i<arr.length;i++)
	{
		atroden = false;
		for(j=0;j<conj.length;j++)
		{
			
			if (arr[i] == conj[j]) {
				atroden = true;
			}
		}
		
		if (atroden == false) conj.push(arr[i]);
	}	
	
	return conj;
}

/* Funciones sobre(src), sobrefinal(src), fuerafinal(src) y fuera(src) : cambian el estilo de cada llave al pasar el mouse sobre ella */
function sobre (src) {
	src.className = 'resaltarllave';
}

function fuera (src) {
	src.className = '';
}

function sobrefinal (src) {
	src.className = 'resaltarllavefinal';
}

function fuerafinal (src) {
	src.className = '';
}

/* Funcion refrescar(): usada para refrescar la página al hacer submit de datos en detalle_llave.ctp */
function refrescar()
{
	window.location.reload(true);
}

/* Funcion refrescar(): usada para confirmar cambios, por ejemplo en detalle_llave.ctp */
function preguntarCambios()
{	
	if(parseInt($('#update_flag').val()) == 1)
	{
		if(confirm('Confirma que desea salir sin guardar?'))
		{
			Shadowbox.close();
		}
	} else {
		Shadowbox.close();
	}
}

/* Funcion llenarpodio(): Llena el podio dinamicamente. */
function llenarpodio()
{
	var campeon = 'Ganador de la final';
	var subcampeon = 'Perdedor de la final';
	var tercero = 'Ganador tercer puesto';
	
	if ($('#equipo_ganador_64').val() != '' && $('#equipoLocal64').val() != '' && $('#equipoVisitante64').val() != '')
	{
		var localfinal = $('#equipoLocal64').val();
		var visitantefinal = $('#equipoVisitante64').val();
		if (parseInt(localfinal) == parseInt($('#equipo_ganador_64').val()))
		{
			campeon = $('#labelLocal64').html() + '<br />' + $('#nombreEquipoLocal64').html();
			subcampeon = $('#labelVisitante64').html() + '<br /> ' + $('#nombreEquipoVisitante64').html();
		} else {
			campeon = $('#labelVisitante64').html() + '<br />' + $('#nombreEquipoVisitante64').html();
			subcampeon = $('#labelLocal64').html() + '<br />' + $('#nombreEquipoLocal64').html();
		}
	
	}
	
	if ($('#equipo_ganador_63').val() != '' && $('#equipoLocal63').val() != '' && $('#equipoVisitante63').val() != '')
	{
		var localtercero = $('#equipoLocal63').val();
		var visitantetercero = $('#equipoVisitante63').val();
		if (parseInt(localtercero) == parseInt($('#equipo_ganador_63').val()))
		{
			tercero = $('#labelLocal63').html() + '<br />' + $('#nombreEquipoLocal63').html();
			
		} else {
			tercero = $('#labelVisitante63').html() + '<br />' +  $('#nombreEquipoVisitante63').html();
			
		}
	
	}
	$('#primerptorneo').html(campeon);
	$('#segundoptorneo').html(subcampeon);
	$('#tercerptorneo').html(tercero);
}

/* Funciones reposicionarClose() y reposicionarCloseGrupos(): reposicionan la cruz de shadowbox a la esquina superior derecha.
 * La de grupos, ademas, llama a la función preguntarCambios() antes de cerrar. 												*/
function reposicionarClose()
{
	if(document.getElementById("sb-nav") != null)
	{
		document.getElementById("sb-nav").innerHTML = "";
	}

	if(document.getElementById("sb-title") != null)
	{
		document.getElementById("sb-title").innerHTML = '<div id="sb-info-inner" style="margin-top: 0px;"><div id="sb-counter"></div><div id="sb-nav"><a onclick="Shadowbox.close()" title="Close" id="sb-nav-close"></a><a onclick="Shadowbox.next()" title="Next" id="sb-nav-next" style="display: none;"></a><a onclick="Shadowbox.play()" title="Play" id="sb-nav-play" style="display: none;"></a><a onclick="Shadowbox.pause()" title="Pause" id="sb-nav-pause" style="display: none;"></a><a onclick="Shadowbox.previous()" title="Previous" id="sb-nav-previous" style="display: none;"></a></div></div><div id="sb-title-inner" style="margin-top: 26px;"></div>';
	}
}

function reposicionarCloseGrupos()
{

	if(document.getElementById("sb-nav") != null)
	{
		document.getElementById("sb-nav").innerHTML = "";
	}

	if(document.getElementById("sb-title") != null)
	{
		document.getElementById("sb-title").innerHTML = '<div id="sb-info-inner" style="margin-top: 0px;"><div id="sb-counter"></div><div id="sb-nav"><a onclick="preguntarCambios()" title="Close" id="sb-nav-close"></a><a onclick="Shadowbox.next()" title="Next" id="sb-nav-next" style="display: none;"></a><a onclick="Shadowbox.play()" title="Play" id="sb-nav-play" style="display: none;"></a><a onclick="Shadowbox.pause()" title="Pause" id="sb-nav-pause" style="display: none;"></a><a onclick="Shadowbox.previous()" title="Previous" id="sb-nav-previous" style="display: none;"></a></div></div><div id="sb-title-inner" style="margin-top: 26px;"></div>';
	}
}

/* Funcion abrirVentanaDetalle(pag, e, id): Abre la ventana de detalles de la llave al hacer click sobre ella.  */ 
function abrirVentanaDetalle(pag,e,id)
{
	// Refrescar al cerrar ventana: ponerle el refresh al hacer submit en el form
	var event = e || window.event;
  	var target = event.target || event.srcElement;
	
	
	if (target.className != null)
	{
		if (target.className == 'input_resultado' || target.id.substring(0,15) == 'imagenDesempate') return;
	} 

	var eqlocal = 'null';
	var eqvisitante = 'null';
	var gollocal = 'null';
	var golvisitante = 'null';
	
	if (parseInt($('#equipoLocal'+id).val()) >= 0) eqlocal = $('#equipoLocal'+id).val();  
	if (parseInt($('#equipoVisitante'+id).val()) >= 0) eqvisitante = $('#equipoVisitante'+id).val();  
	if (parseInt($('#llaveLocal'+id).val()) >= 0) gollocal = $('#llaveLocal'+id).val();  
	if (parseInt($('#llaveVisitante'+id).val()) >= 0) golvisitante = $('#llaveVisitante'+id).val();  
	
	hab = '';
	
	if (id >= 57) 
	{ 
		hab = $('#habilitado_'+id).val();
		//alert(hab);
	}
	
	p1 = pag+id+'/'+eqlocal+'/'+eqvisitante+'/'+gollocal+'/'+golvisitante+'/'+hab;
	
	Shadowbox.open({content: p1,
	        player:     "iframe",
     	   	title:      "Detalles del partido",
     	   	height: 500,
     	   	width: 900
         });
}

/* Funcion esconderFlashHome(): muestra y esconde el mensaje flash en el home. */
function esconderFlashHome() {
	$('#flashMessage').show('slow');
	window.setTimeout(function() {
		 $('#flashMessage').hide('slow');
		}, 3500);
}
