   
function valida_fecha(dia,mes,ano)
		{
		
    if ( dia.length == 0 ) return false;
    if ( mes.length == 0 ) return false;
    if ( ano.length == 0 ) return false;
    
    
		dia=parseInt(limpiaCeros(dia));
		mes=parseInt(limpiaCeros(mes));
		ano=parseInt(limpiaCeros(ano));
	
		fecha = new Date();
	
	 
		if (mes<1 || mes>12)
			return false;
	
			meses = new Array(13)
			meses[1] = 31;
			if ( (ano%4) ==0) meses[2]=29;
			else meses[2]=28;
			meses[3] = 31;
			meses[4] = 30;
			meses[5] = 31;
			meses[6] = 30;
			meses[7] = 31;
			meses[8] = 31;
			meses[9] = 30;
			meses[10] = 31;
			meses[11] = 30;
			meses[12] = 31;


		if (dia<1 || dia>meses[mes]) return false;	
			
		return true;
		}

function seleccionar(form_select,seleccion)
		{
		for(i=0;i<(form_select.length);i++)
				{
					if (form_select.options[i].value==seleccion)
						{
						form_select.options[i].selected=true;
						}
				}	
		}

function anadir(form_select,valor,texto,marcado,clase)
			{

			form_select.length++;
				form_select.options[form_select.length-1].text=texto;
				form_select.options[form_select.length-1].value=valor;
				form_select.options[form_select.length-1].selected=marcado;	
				form_select.options[form_select.length-1].className = clase;
			}


digito="0123456789";
puntoDecimal=".,";
caracterMayuscula="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
caracterMinuscula="abcdefghijklmnopqrstuvwxyz";
caracer = caracterMayuscula + caracterMinuscula;
alfaNumerico=digito+caracterMayuscula+caracterMinuscula;
signos="_-.";
arroba="@";
cadenaValida= alfaNumerico+signos+arroba;



function validarCadena(mascara,cadena)
	{
	
		if (cadena.length==0) return false;
	
	
		for(i=0;i<cadena.length;i++)
			{
			if (mascara.indexOf(cadena.charAt(i))==-1) return false;
			}
		return true;
	}

function esEmail(cadena)
	{
	
		if(!validarCadena(cadena,arroba))
			{error='cadena sin arroba';return false;}
		
		parte = cadena.split("@");
	
		aaaa=parte[0];
		
			
		ultimoPunto = parte[1].lastIndexOf(".");	
		if (ultimoPunto==-1)
			{error='El email no tiene dominio';return false;}		
		
		bbbb=parte[1].slice(0,ultimoPunto);
		
		if (bbbb.length<2)
			{error='bbbb no puede ser menor que 2';return false;}
		
		if (!validarCadena(alfaNumerico+signos,aaaa+bbbb))
				{error='cadena sin arroba';return false;}
		

		cc=parte[1].slice(ultimoPunto+1);


		if (cc.length<2 || cc.length>3)
			{error='cadena sin arroba';return false;}
		if (!validarCadena(caracterMayuscula+caracterMinuscula,cc))
			{error='cadena sin arroba';return false;}	

		return true;
	
	}


function roundOff(value, precision)
	{
		
        value = "" + value //convert value to string
        precision = parseInt(precision);

        var whole = "" + Math.round(value * Math.pow(10, precision));
        if ( precision == 0) return whole;
        var decPoint = whole.length - precision;

        if(decPoint != 0)
        {

                result = whole.substring(0, decPoint);

				result += ",";
                result += whole.substring(decPoint, whole.length);

				if (result==",0") result="0,00";
        }
        else
        {

                result = "0,"+whole;
        }

        return result;
		}
    
function limpiaCeros( cadena ) 
{
    temp="";
    numeros=0;
    for(i=0;i<cadena.length;i++)
    {
        if ( cadena.charAt(i)!="0" || numeros>0)
        {  
           temp+=cadena.charAt(i);
           numeros++;
        } 
    }
return temp;
}

function mascara(  mask , cadena , align ) {
    var i;
    if ( !align ) align="left";

    if ( align == "right" ) {
        cad = "";
        for(i=0;i<mask.length-cadena.length;i++) {
            cad+=mask.charAt(i);
        } 
        cad += cadena; 
    
    }
    
    if ( align == "left" ) {
        cad = cadena;
        
        for(i=cadena.length;i<mask.length;i++) {
            cad+=mask.charAt(i);
        } 
            
    }
    
    return cad;
}


    



    
    
/*-------------------------------------------------------------------
    Objeto popup
--------------------------------------------------------------------*/

     function JPopup(window) {
         this.url    = "";
         this.width  = 780;
         this.height = 500;
         this.returnedValue = "";
         this.window = window;
         this.Show = JPopupShow;
         this.hide   = "no";
         this.title  = "";
     }    
     
     function JPopupShow() {
         
         argumentos = Array( this.url );
         valor = this.window.showModalDialog( "http://www.deportesmatch.com/acceso_admin/funciones/popup.php?title="+this.title, argumentos ,  "dialogHeight:"+this.height+"px; dialogWidth:"+this.width+"px; dialogTop:150px; dialogLeft:150px;  edge:raised; scroll:yes; status:no; dialogHide="+this.hide+";");

         if ( valor == "undefined" ) valor = "";
         if ( !valor ) valor = "";
         return valor;
     }
    
/*-------------------------------------------------------------------
    Objeto Confirm
--------------------------------------------------------------------*/
     function JConfirm( texto ) {
        this.texto  = texto;
        this.width  = "400";
        this.height = "200";
        this.Show  = JConfirmShow;
     }

     function JConfirmShow() {
         argumentos = Array( this.texto );
         res = window.showModalDialog( "http://www.deportesmatch.com/acceso_admin/plantillas/jmensaje.php", argumentos ,  "dialogHeight:"+this.height+"px; dialogWidth:"+this.width+"px center:yes; edge:raised; scroll:no; status:no;");
         if ( res == "undefined" ) res = false;
         if ( !res ) res = false;
         return res;
     }
   
/*------------------------------------------------------------------
     Objeto Formulario 
-------------------------------------------------------------------*/
    function JForm( form ) {
        this.form = form;
        this.arrInputs = Array();
           
           
        this.addInput  = JFormAddInput;
        this.validar   = JFormValidar;
        this.submit    = JFormSubmit;
    }
     
    function JFormAddInput( input ) {
       
        this.arrInputs[ this.arrInputs.length ] = input;
 
    }
    
    function JFormSubmit() {

        if  (this.validar()) {
            this.form.submit();
        }
    }
    
    function JFormValidar() {
        
        for(iInp=0;iInp<this.arrInputs.length;iInp++) {
            
            
            //if ( iInp > 50 ) return;
            if ( !this.arrInputs[iInp].validar() ) return false;
        }   
        
        return true;
    }
    
    
    function numericCheckKey() {
        
        key = event.keyCode;
        keyNom = String.fromCharCode(key);
        
        validos = "0123456789.,";
        if ( key == 46 ) {
           event.keyCode = 44;
        }
        if ( validos.search(keyNom)==-1 ) {
            event.keyCode = null;
            return;
        }
        
    }
    
    
    
    
    
/*----------------------------------------------------------------
    Objeto Input
-----------------------------------------------------------------*/    
    function JInput( input , alias , tipo , notNull ) {
        this.input   = input;
        this.alias   = alias;
        this.tipo    = (tipo) ? tipo : "texto";
        this.notNull = (notNull) ? notNull : false;
        this.mensaje = "";
         
        this.validar = JInputValidar;
        if ( this.input.onchange == null ) {
            this.input.onchange = JInputModificado;
        }
       
        
        if ( this.tipo == "numerico" ) {
            this.input.onkeypress = numericCheckKey;
            this.input.value = this.input.value.replace(/\./gi,",");
        }
            
    }
    function mesaje() {
        
    }
    
    function JInputModificado() {
        this.className = "inputMod";
    }
    function JInputValidar() {
        
        if ( this.notNull ) {
              if ( this.input.value.length == 0 ) {
                   
                  if ( !this.mensaje ) {
                      alert("El campo " + this.alias + " no puede quedar vacio");
                  } else {
                      alert(this.mensaje);
                  }
                  if ( this.input.type != "textarea") {
                      this.input.focus();
                  }
                  return false;
              }
              if ( this.tipo == "numerico" && this.input.value == 0) {
              if ( !this.mensaje ) {
                      alert("El campo " + this.alias + " no puede ser 0");
                  } else {
                      alert(this.mensaje);
                  }
                  this.input.focus();
                  return false;
              }
           } 
    
        if ( this.tipo == "texto" ) {
            
        }

        if ( this.tipo == "numerico" ) {
            
            
            numcomas = 0;
            for(i=0;i<this.input.value.length;i++) {
                numcomas += (this.input.value.charAt(i) == ",") ? 1 : 0;
            }
            

            this.input.value = this.input.value.replace(/\./gi,"");
            this.input.value = this.input.value.replace(/,/gi,".");
            this.input.value = limpiaCeros( this.input.value );
            if ( this.input.value.length == 0 ) this.input.value = 0;
            if ( isNaN(this.input.value) ) {
                this.input.value = this.input.value.replace(/\./gi,",");
                if ( this.mensaje ) alert(this.mensaje);
                else alert("El campo " + this.alias + " debe contener valores númericos");
                this.input.focus();
                return false;
            }
        }
        
        if ( this.tipo == "email") {
            if ( !esEmail( this.input.value)) {
               if ( this.mensaje ) alert(this.mensaje);
               else alert("El campo "+ this.alias +" no contiene un formato de email válido");
               this.input.focus();
               return;
            } 
        }
        
        
           
        return true;  
    }

/*--------------------------------------------------------------------
    Objeto select
-----------------------------------------------------------------------*/
function JSelect(select,textoInicial,valorInicial) {
    this.select = select;
    this.textoInicial = ((textoInicial+"").length>0) ? textoInicial : "";
    this.valorInicial = ((valorInicial+"").length>0) ? valorInicial : "";
    // Funciones
    this.addValue = JSelectAddValue;
    this.ini      = JSelectIni;
    this.seleccionar = JSelectSeleccionar;
}    

function JSelectAddValue(codigo,valor,selected) {
    selected = (selected) ? true : false;
    anadir(this.select, codigo, valor, selected);
}
    
function JSelectIni() {
    this.select.length = 0;
    this.addValue(this.valorInicial,this.textoInicial,true);
}
function JSelectSeleccionar(value) {
    seleccionar(this.select,value);
}    
    
/*------------------------------------------------------------------------
     Objeto fecha
------------------------------------------------------------------------*/
    function JInputFecha( dia, mes , ano , alias , notNull ) {
        this.dia   = dia;
        this.mes   = mes;
        this.ano   = ano;
        this.alias = alias;
        this.notNull = (notNull) ? true : false;    
    
        this.validar = JInputFechaValidar;
    }
    
    
    function JInputFechaValidar() {

        if ( this.notNull ) {
            dia         = new JInput( this.dia );
            dia.alias   = this.alias + " dia ";
            dia.notNull = true;
            dia.tipo    = "numerico";
            
            
            mes = new JInput( this.mes );
            mes.alias   = this.alias  + " mes " ;
            mes.notNull = true;
            mes.tipo    = "numerico" ;

            ano = new JInput( this.ano );
            ano.alias   = this.alias   + " año ";
            ano.notNull = true;
            ano.tipo    = "numerico";           
            
            if ( !dia.validar() ) return false;
            if ( !mes.validar() ) return false;
            if ( !ano.validar() ) return false;

            if ( dia.input.value==0 || mes.input.value==0 || ano.input.value == 0 ) {
                dia.input.value = "";
                mes.input.value = "";
                ano.input.value = "";
                alert("El campo [" + this.alias + "] no puede quedar vacío");
                this.dia.focus();
                return false;
            }
        }
        
        
        
        if ( this.dia.value.length > 0 || this.mes.value.length > 0 || this.ano.value.length > 0)  {
            
            if ( !valida_fecha( this.dia.value , this.mes.value , this.ano.value )) {
                alert("El campo [" + this.alias + "] no contiene una fecha válida");
                this.dia.focus();
                return false;
            }
        }
       
        return true;
    }
    

    
/*------------------------------------------------------------------
       Objeto para obtener una fecha
-------------------------------------------------------------------*/
    function JDialogFecha( dia , mes ,ano ) {
        this.dia = dia;
        this.mes = mes;
        this.ano = ano;
        this.ruta;    
        this.width  = 210;
        this.height = 200;
        this.value = "";
        this.setFecha = JDialogFechaSetFecha;   
        this.Show     = JDialogFechaShow;
    }    
    
    function JDialogFechaSetFecha() {
        if ( !this.value ) return;
        if ( this.value.length == 0 ) return;
        
        tokens = this.value.split("-");
        if ( tokens.length != 3 ) return;
        
        dia = parseInt(tokens[0],10);
        mes = parseInt(tokens[1],10);
        ano = parseInt(tokens[2],10);
        
        this.dia.value = dia;
        this.mes.value = mes;
        this.ano.value = ano;            
    }
    
    function JDialogFechaShow() {
        valor = window.showModalDialog( "http://www.deportesmatch.com/acceso_admin/funciones/calendar.php", "" , "dialogHeight:"+this.height+"px; dialogWidth:"+this.width+"px center:yes; edge:raised; scroll:no; status:no;"); 

        if ( valor == "undefined" ) valor = "";
        this.value = valor;
        this.setFecha();
        return valor;        
    }
    
/*-----------------------------------------------------------
    Objeto para obtener una imagen
------------------------------------------------------------*/

   function JDialogImagen(tabla,campo) {
       this.rutaImg = "";
       this.tabla  = (tabla) ? tabla : "";
       this.campo  = (campo) ? campo : "";  
       this.width  = 780;
       this.height = 580;
       this.Show   = JDialogImagenShow;
       this.TextBox  = "";
       this.Imagen = "";
       this.UpLoad = JDialogImagenUpLoad;
       
   }
   
   function JDialogImagenShow() {
       var imagen = window.showModalDialog( "http://www.deportesmatch.com/acceso_admin/funciones/mrk_imagenes.php?tabla="+this.tabla+"&campo="+this.campo+"&current="+this.TextBox.value,  "" , "dialogHeight:"+this.height+"px; dialogWidth:"+this.width+"px center:yes; edge:raised; scroll:no; status:no;");
       
       if ( imagen ) {
           this.TextBox.value = imagen;
           this.Imagen.src = this.rutaImg+imagen;
       }
       
       return imagen;
   }
   
   function JDialogImagenUpLoad() {
       var imagen = window.showModalDialog( "http://www.deportesmatch.com/acceso_admin/funciones/subir.php?tabla="+this.tabla+"&campo="+this.campo,  "" , "dialogHeight:180px; dialogWidth:500px center:yes; edge:raised; scroll:no; status:no;");
       if ( imagen ) {
           this.TextBox.value = imagen;
           this.Imagen.src = this.rutaImg+imagen;
       }
       return imagen;
   }
   
/*------------------------------------------------------------------
       Objeto para seleccionar un color
-------------------------------------------------------------------*/
    function JDialogColor() {
        this.Show  = JDialogColorShow;
        this.width  = 300;
        this.height = 200;

    } 
    
    function JDialogColorShow() {
      
       var res = window.showModalDialog( "http://www.deportesmatch.com/acceso_admin/funciones/select_color.html",  "" , "dialogHeight:"+this.height+"px; dialogWidth:"+this.width+"px center:yes; edge:raised; scroll:no; status:no;");
       return res;
    }

/*------------------------------------------------------------------
    Objeto para controlar una imagen
-------------------------------------------------------------------*/
    function JImagen( input , TextBox , Imagen, rutaImg, tabla, campo ) {
        this.input   = input;
        this.TextBox = TextBox;
	    this.Imagen  = Imagen;
	    this.rutaImg = rutaImg;
	    this.tabla = tabla;
	    this.campo = campo;
        this.validar  = JImagenValidar;
        this.setImg   = JImagenSetImg;
        this.getImage = JImagenGetImage;
        
    }
    
    function JImagenValidar() {
        return this.input.validar();
    }
    
    function JImagenSetImg(valor) {
        this.Imagen.src = this.rutaImg + valor;
    }
    
    function JImagenGetImage(tipo) {
	
       Img = new JDialogImagen(this.tabla,this.campo);
	   Img.TextBox = this.TextBox;
	   Img.Imagen  = this.Imagen;
	   Img.rutaImg = this.rutaImg;
	   if ( tipo=="listado" ) Img.Show();
	   if ( tipo=="nueva")    Img.UpLoad();
    }
    
/**
 * Objeto Imagen de un listado
 */
    function JLstImagen(imagen) {
        this.imagen = imagen;
        
        this.imagen.style.cursor = "hand";
        this.imagen.alt = "Ver imagen";
        this.imagen.onclick = JLstVerImagen;   
    }
    
    function JLstVerImagen() {
        imagen = event.srcElement;
        pop = new JPopup(window);
        pop.url = imagen.src;
        pop.width = 300;
        pop.height = 300;
        pop.Show();
    }
    
/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
    