/*
 
Funciones Javascript


*/

function Tabula(txtbox)
{
    if(window.event.keyCode==13) document.getElementById(txtbox).focus();
}

function CerrarTitulo(titulo) 
{
    var idx=1;
    do
    {
        if(idx!=titulo)
        {
            $find("Tit" + idx)._doClose();
        }
        idx++;
    } 
    while(idx<4);
}

function ValidoFecha(Campo,dspmsg)
{
    var Valor,ExpReg_cad;
    Valor=document.getElementById(Campo).value;
    var longitud=Valor.length;
    if (longitud > 0)
    {
        ExpReg_cad=/^(\d+)$/;
               /*Solo valores numericos*/ 
        if (ExpReg_cad.test(Valor))
        {
        switch (longitud)
         {
         case 5:
           ExpReg_cad=/^(\d)(\d{2})(\d{2})$/;
           Valor= Valor.replace(ExpReg_cad, "0$1/$2/20$3"); 
           break;
         case 6:
           ExpReg_cad=/^(\d{2})(\d{2})(\d{2})$/;
           Valor= Valor.replace(ExpReg_cad, "$1/$2/20$3"); 
           break;
         case 7:
           ExpReg_cad=/^(\d)(\d{2})(\d{4})$/;
           Valor= Valor.replace(ExpReg_cad, "0$1/$2/$3"); 
           break;
         case 8:
           ExpReg_cad=/^(\d{2})(\d{2})(\d{4})$/;
           Valor= Valor.replace(ExpReg_cad, "$1/$2/$3"); 
           break;
         }
        }
        else
        {
           ExpReg_cad=/[/-]/;
               /*Formato standard de Fecha d/m/y de cualquier manera*/ 
           if (ExpReg_cad.test(Valor))
           {
            var matriz=Valor.split(ExpReg_cad);
            if (matriz.length==3)
            {
             if (matriz[0].length == 1) matriz[0]="0"+matriz[0];
             if (matriz[1].length == 1) matriz[1]="0"+matriz[1];
             if (matriz[2].length == 2) matriz[2]="20"+matriz[2];
             Valor=matriz[0]+"/"+matriz[1]+"/"+matriz[2];
            }
           }
        }
         if (dspmsg)
         {
          ExpReg_cad=/^([0][1-9]|[12][0-9]|3[01])([/-])(0[1-9]|1[012])\2(19|20)(\d{2})$/;
          if (ExpReg_cad.test(Valor)==false)
          {
            alert ('Fecha no valida, verifique ...\nReingrese la fecha en formato dd/mm/yyyy ');
            Valor = ""; 
            document.getElementById(Campo).focus();
          }
         } 
        document.getElementById(Campo).value = Valor;
    }
}

function ValidoHora(Campo)
{
    var Valor,ExpReg_cad;
    Valor=document.getElementById(Campo).value;
    if (Valor.length > 0)
    {
        ExpReg_cad=/^(\d)$/;
        if (ExpReg_cad.test(Valor))
        { Valor= Valor.replace(ExpReg_cad, "0$1:00"); }
        else
        {
            ExpReg_cad=/^(\d{2})$/;
            if (ExpReg_cad.test(Valor))
            { Valor = Valor.replace(ExpReg_cad, "$1:00"); }
            else
            {
                ExpReg_cad=/^(\d)(\d{2})$/;
                if (ExpReg_cad.test(Valor))
                { Valor = Valor.replace(ExpReg_cad, "0$1:$2"); }
                else
                {
                    ExpReg_cad=/^(\d{2})(\d{2})$/;
                    if (ExpReg_cad.test(Valor))
                    { Valor = Valor.replace(ExpReg_cad, "$1:$2"); }
                    else
                    {
                        ExpReg_cad=/^(\d):(\d{2})$/;
                        if (ExpReg_cad.test(Valor))
                        { Valor = Valor.replace(ExpReg_cad, "0$1:$2"); }
                    }    
                }
            }
        }
        ExpReg_cad=/^(0\d|1\d|2[0-3]|24(?=:00)):([0-5]\d)$/;
        if (ExpReg_cad.test(Valor)==false)
        {
            alert ('Hora no valida, verifique ...\nReingrese la hora en formato HH:mm ');
            Valor = ""; 
            document.getElementById(Campo).focus();
        }
        document.getElementById(Campo).value = Valor;
    }
}

function EnterPorTab(e,obj)  
        { 
            tecla=(document.all) ? e.keyCode : e.which; 
            if(tecla!=13) return; 
            frm=obj.form; 
            for(i=0;i<frm.elements.length;i++)  
                if(frm.elements[i]==obj)  
                {  
                    if (i==frm.elements.length-1)  
                        i=-1; 
                    break  
                } 
    /*Para campos disabled*/ 
            if (frm.elements[i+1].disabled ==true )     
                EnterPorTab(e,frm.elements[i+1]); 
    /*Para campos readOnly */ 
            else if (frm.elements[i+1].readOnly ==true )     
                EnterPorTab(e,frm.elements[i+1]); 
            else frm.elements[i+1].focus(); 
            return false; 
}

function DragDrop(filas)
{
    // "DragSources" 1 por fila de tabla
    var i=0;
    for (i=1;i<=filas;i++)
    {
        var source1 = new Custom.UI.TxtDragSourceBehavior($get('ctl00_ContentPlaceHolder1_F'+ i), 'ctl00_ContentPlaceHolder1_F'+ i);
        source1.initialize();
    }
    // "DropTargets" 1 por fila de tabla
    var i=0;
    for (i=1;i<=filas;i++)
    {
        var source1 = new Custom.UI.TxtDropTargetBehavior($get('ctl00_ContentPlaceHolder1_F'+i));
        source1.initialize();
    }
}

