//**********************************************************
// TIME AND YEAR DISPLAY
//**********************************************************
function showYear() {
    var myYear = new Date;
    document.write(myYear.getFullYear());
}

function showDate() {
    var stampmonths = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Deciembre");
    var stampdays = new Array("Domingo","Lunes","Martes","Mi&eacute;rcoles","Jueves","Viernes","S&aacute;bado");
    var thedate = new Date();
    document.write(stampdays[thedate.getDay()] + ", " + thedate.getDate() + " de " + stampmonths[ thedate.getMonth()] + " de "  + thedate.getFullYear() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
}


//**********************************************************
// CENTERED POPUP WINDOW
//**********************************************************

function popup(mypage,myname,w,h,features) {
    if(screen.width){
        var winl = (screen.width-w)/2;
        var wint = (screen.height-h)/2;
    } else {
        winl = 0;wint =0;
    }
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += features;
    win = window.open(mypage,myname,settings);
    win.window.focus();
}

//**********************************************************
// FULL SCREEN POPUP WINDOW
//**********************************************************

function fullScreen() {
    window.open('main/sistema.jsp?cerrar=S','SETT','width='+screen.width*0.99+',height='+screen.height*0.93+',top=0,left=0,resizable=yes');
}

//**************************************************************************
// FUNCION QUE PERMITE CONTROLAR QUE SIEMPRE QUE SE GENERE SUBMIT, EL VALOR 
// TIPO ACCION SEA DIFERENTE DE NULL
//**************************************************************************
function generarSubmit(forma) {
    var tipoAccion; 
    tipoAccion=eval('document.' + forma + '.tipoAccion.value');
    if(tipoAccion!=''){
        return true;
    }
    return false;
}

//**************************************************************************
// FUNCION QUE PERMITE CONTROLAR QUE SIEMPRE QUE SE GENERE SUBMIT, EL VALOR 
// TIPO ACCION SEA DIFERENTE DE NULL
//**************************************************************************
function asignarAccion(forma,tipoAccion) {
    eval("document." + forma + ".tipoAccion.value='"+ tipoAccion +"'");
}

//****************************************************************
//PONER FORMATO DE FECHA
//***************************************************************
var formato = new Array(2,2,4)
function mascara(d,sep,pat,nums){
    if(d.valant != d.value){
        val = d.value
        largo = val.length
        val = val.split(sep)
        val2 = ''
        for(r=0;r<val.length;r++){
            val2 += val[r]	
        }
        if(nums){
            for(z=0;z<val2.length;z++){
                if(isNaN(val2.charAt(z))){
                    letra = new RegExp(val2.charAt(z),"g")
                    val2 = val2.replace(letra,"")
                }
            }
        }
        val = ''
        val3 = new Array()
        for(s=0; s<pat.length; s++){
            val3[s] = val2.substring(0,pat[s])
            val2 = val2.substr(pat[s])
        }
        for(q=0;q<val3.length; q++){
            if(q ==0){
                val = val3[q]
            }
            else{
                if(val3[q] != ""){
                    val += sep + val3[q]
                }
            }
        }
        d.value = val
        d.valant = val
    }
}

var formatoFecha = new Array(2,2,4,2,2)
function mascaraFecha(d,sep,pat){
    if(d.valant != d.value){
        val = d.value
        val2 = ''
        letra=''
        for(z=0;z<val.length;z++){
            if(!isNaN(val.charAt(z))){
                if(val.charAt(z)!=' '){
                    val2+=val.charAt(z)
                }	
            }
        }
        val = ''
        val3 = new Array()
        
        for(s=0; s<pat.length; s++){
            val3[s] = val2.substring(0,pat[s])
            val2 = val2.substr(pat[s])
        }
        for(q=0;q<val3.length; q++){
            if(q ==0){
                val = val3[q]
            }
            else if((q==1 || q==2) && val3[q]!=''){
                val += sep + val3[q]
            }else if(q==3 && val3[q]!=''){
                val += " "+ val3[q]
            }else if(q==4 && val3[4]!=''){
                val += ":"+ val3[q]		
                
            }
        }
        d.value = val
        d.valant = val
    }
    
}

//****************************************************************
// CONTROLAR LA EJECUCION 1 SOLO CLIC
//***************************************************************
var dobleClick = 0;

function controlTipoAccion(objeto, forma, accion) {
    dobleClick++;
    //alert('El proceso está en ejecución ' + dobleClick);
    if (dobleClick == 1) {
        objeto.disabled=true;
        eval('document.' + forma + '.tipoAccion.value = "' + accion + '"');
        eval('document.' + forma + '.submit();');
    } else {
        alert('El proceso está en ejecución');
    }
}

//****************************************************************
// FUNCION QUE VALIDA EL FORMATO DE UNA PLACA
//***************************************************************
function esPlaca(placaStr,nombreForma,nombreCampo){
    
    var pla0=/^[A-Z][A-Z][A-Z][0-9][0-9][0-9]$/;
    var pla1=/^[A-Z][A-Z][0-9][0-9][0-9][0-9]$/;
    var pla2=/^[A-Z][0-9][0-9][0-9][0-9][0-9]$/;
    var pla3=/^[A-Z][A-Z][A-Z][0-9][0-9]$/;
    var pla4=/^[A-Z][A-Z][0-9][0-9][0-9]$/;
    var pla5=/^[0-9][0-9][0-9][0-9][0-9]$/;
    var pla6=/^[0-9][0-9][0-9][0-9]$/;
    var pla7=/^[A-Z][A-Z][A-Z][0-9][0-9][A-Z]$/;
    var pla8=/^[0-9][0-9][0-9][A-Z]$/;
    var pla9=/^[0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
    var pla10=/^[0-9][0-9][0-9][A-Z][A-Z]$/;
    var pla11=/^[S][I][N]$/;
    
    placaStr=placaStr.toUpperCase();
    var m= placaStr.match(pla0);
    //alert(placaStr+"..."+m)
    if (m==null)
        m= placaStr.match(pla1);
    if (m==null)
        m= placaStr.match(pla2);
    if (m==null)
        m= placaStr.match(pla3);
    if (m==null)
        m= placaStr.match(pla4);
    if (m==null)
        m= placaStr.match(pla5);
    if (m==null)
        m= placaStr.match(pla6);
    if (m==null)
        m= placaStr.match(pla7);
    if (m==null)
        m= placaStr.match(pla8);
    if (m==null)
        m= placaStr.match(pla9);
    if (m==null)
        m= placaStr.match(pla10);
    if (m==null)
        m= placaStr.match(pla11);
    

    if ((m == null) && (placaStr != "")) {
        alert("Formato de placa no válido");
        eval("document." + nombreForma + "."+nombreCampo+".value = ''");
        eval("document." + nombreForma + "."+nombreCampo+".focus()");
        return false;
    }
    //eval("document." + nombreForma + ".codClaseVehiculo.focus()");

    return true;
}

//****************************************************************
// FUNCION QUE VALIDA EL FORMATO DE UNA PLACA POR LOS 3 FORMATOS
//***************************************************************
function esPlacaFormato(placaStr,nombreForma,nombreCampo){
    
    var pla0=/^[A-Z][A-Z][A-Z][0-9][0-9][0-9]$/;
    var pla1=/^[A-Z][A-Z][A-Z][0-9][0-9]$/;
    var pla2=/^[A-Z][A-Z][A-Z][0-9][0-9][A-Z]$/;
    var pla3=/^[S][I][N]$/;
    
    placaStr=placaStr.toUpperCase();
    var m= placaStr.match(pla0);
    //alert(placaStr+"..."+m)
    if (m==null)
        m= placaStr.match(pla1);
    if (m==null)
        m= placaStr.match(pla2);
    if (m==null)
        m= placaStr.match(pla3);

    if ((m == null) && (placaStr != "")) {
        alert("Formato de placa no válido");
        eval("document." + nombreForma + "."+nombreCampo+".value = ''");
        eval("document." + nombreForma + "."+nombreCampo+".focus()");
        return false;
    }
    //eval("document." + nombreForma + ".codClaseVehiculo.focus()");

    return true;
}
//****************************************************************
// FUNCION PARA CONTROLAR EL MAXIMO NUMERO DE CARACTERES QUE SE PUEDE
// DIGITAR EN UN CONTROL TEXTAREA
//***************************************************************

function ismaxlength(obj,maxlength){
    var mlength=parseInt(maxlength)
    if (obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
}


//*******************************************************************************************
// OBTENEMOS EL VALOR SELECCIONADO DE UN CONJUNTO DE VALORES RADIO BUTTON
//*****************************************************************************
function obtenerRadioButtonSeleccionado(control) {
    if (control.length > 1){
        for(i=0;i<control.length;i++){
            if(control[i].checked){ 
                return control[i].value;
            }
        }  
    }else{      
        if(control.checked){ 
            return control.value;
        }
    }
    return 0;
}

//**********************************************************
// MENU DE ACCESO DIRECTO
//**********************************************************
function menuAcceso(selObj,restore){
    if(selObj.options[selObj.selectedIndex].value!=0){
        eval("parent.parent.mainFrame.location.href='"+selObj.options[selObj.selectedIndex].value+"'");
        if (restore) selObj.selectedIndex=0;
    }
}

//****************************************************************
// FUNCION PARA CONTROLAR EL INGRESO DE LETRAS SOLO EN MAYUSCULAS
//***************************************************************

function upperCase(obj){
    obj.value=obj.value.toUpperCase()
}

function esNumero(valor){
    alert(valor);
    var i;
    var cadena="";
    var cadena1="";
    for (i = 0; i < valor.length; i++){   
        var c = valor.charCodeAt(i);
        if (c >= 48 && c <= 57){
            cadena+=valor.charAt(i);
        }
        cadena1=cadena;
    }
    return cadena1;
}

//****************************************************************
// FUNCION PARA CONTROLAR EL INGRESO DE NUMEROS
//***************************************************************
function entero(campo){
    var key=event.keyCode;//codigo de tecla.
    //solo se puede introducir números y puntos
    if (key < 48 || key > 57) {//si no es numero 
        window.event.keyCode=0;//anula la entrada de texto.
    }
}

//****************************************************************
// FUNCION PARA CONTROLAR CAMPOS VACIOS
//***************************************************************
function validarCampo(campo,nombreForma,nombreCampo,accion,valorLista,campoAux){
    if (nombreCampo=='numeroDocumento'){
        if (campo!='') {
            if (valorLista!=-1){
                if (campo!=campoAux){
                    eval('document.' + nombreForma + '.tipoAccion.value = "' + accion + '"');
                    eval('document.' + nombreForma + '.submit();');
                    return true;
                }else{
                    return false;
                }    
            }else{
                alert("Debe seleccionar el tipo de documento.");  
                eval("document." + nombreForma + ".tipoDocumento.focus()");
                return false;  
            }
        }
    }else{
        if (campo!='') {
            if (campo!=campoAux){
                eval('document.' + nombreForma + '.tipoAccion.value = "' + accion + '"');
                eval('document.' + nombreForma + '.submit();');
                return true;
            }else{
                return false;
            }
        }
    }
}

//***************************************************************************
// FUNCION PARA CONTROLAR EL INGRESO DE NUMEROS DECIMALES CON UNA PRECISION
// ESPECIFICA
//**************************************************************************
var borrarCampo="false";
function decimal(campo,precision) {
    punto=false;
    if(borrarCampo=="true"){
        campo.value='';
        borrarCampo="false";
    }
    
    val = campo.value
    var key=event.keyCode;//codigo de tecla.
    for(z=0;z<val.length;z++){
        if(val.charAt(z)=='.'){
            punto=true;
        }	
    }
    
    if ((key < 48 || key > 57) && key!=46) {//si no es numero 
        window.event.keyCode=0;//anula la entrada de texto.
    }else if(key!=46){
        if(punto==true){
            index=val.lastIndexOf('.')
            val2=val.substring(index,val.length)  
            
            if((val2.length)>precision){
                window.event.keyCode=0;
                return false;
            }
        }
    }
    
    // no deja introducir mas de una coma.
    if (key == 46) {
        if (punto==false) { 
            punto=true;
        } else {
            window.event.keyCode=0;
            return false;
        }
    }
}

function limpiarCampo() {
    borrarCampo="true";
}

//****************************************************************
// VALIDA QUE EL TEXTO NO TENGA CARACTERES ESPECIALES
//***************************************************************
function validarCaracteresEspeciales(texto, upperCase){
    valor=texto.value;
    if(upperCase=='true'){
        valor=valor.toUpperCase();
    }
    var cadena1="";
    var cadena="";
    var caracterEsp=""
    for (i = 0; i < valor.length; i++){   
        var c = valor.charCodeAt(i);        
        if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58) || c==32 || c==209 || c==164 || c==164 ){
            cadena+=valor.charAt(i);          
        }      
        cadena1=cadena;
    } 
    texto.value=cadena1;   
}

//***************************************************************************
// FUNCION PARA DESHABILITAR CAMPO
//**************************************************************************
function deshabilitar(nombreForma,campoDeshabilitar,campoValidacion) {
    var valorRadio;
    valorRadio=obtenerRadioButtonSeleccionado(eval("document." + nombreForma + "."+campoValidacion));
    if (valorRadio=='N'){
        eval("document." + nombreForma + "."+campoDeshabilitar+".disabled=true");
    }else{
        eval("document." + nombreForma + "."+campoDeshabilitar+".disabled=false");
    }

}

//***************************************************************************
// FUNCION PARA LIMPIAR CAMPOS DE SERVICIO GRUA
//**************************************************************************
function datosGrua(nombreForma,campoValidacion) {
    var valorRadio;
    valorRadio=obtenerRadioButtonSeleccionado(eval("document." + nombreForma + "."+campoValidacion));
    if (valorRadio=='N'){        
        eval("document." + nombreForma + ".codigoGrua.value='-1'");
        eval("document." + nombreForma + ".facturaGrua.value=''");
    }
}


//***************************************************************************
// FUNCION PARA VALIDAR PLACA Y NO HAGA SUBMIT
//**************************************************************************
function validar(boton,forma,accion,campo){
    if (esPlacaFormato(eval('document.'+forma+'.'+campo+'.value'),forma,campo)){
        controlTipoAccion(boton, forma, accion);
    }
}

//***************************************************************************
// FUNCION PARA IMPRIMIR ESPACIOS EN BLANCO
//***************************************************************************
function formatoEspaciosBlancos(cantidadBlancos){  
    for (i = 0; i < cantidadBlancos; i++){
        document.write("&nbsp;");
    }
}

function ocultarImagen(){
document.getElementById("imagenImprimir").innerHTML = ''; 
}

