/**
 * Valida un numero de telefono
 */
function esTelefono(cad) {
    var miRegExp = new RegExp('^[0-9\-\.\)\(]+$');
    return miRegExp.test(cad);
}//fin esTelefono()

/**
 * Valida un email es correcto
 */
function esEmail(cad){
    var miRegExp = new RegExp('^.+@.+\..{2,4}$');
	return miRegExp.test(cad);
}//fin esEmail()

function esNumerico(cad){
    var miRegExp = new RegExp('^[0-9]*[,|.]{0,1}[0-9]+$');
    return miRegExp.test(cad);
}//fin esNumerico()

/**
 * Valida un DNI
 */
function esDNI(cad){
    var miRegExp = new RegExp('^[0-9]{5,8}[a-z]{0,1}$', 'i');
    return miRegExp.test(cad);
}//fin esDNI()

function esDNISociedad(cad){
    var miRegExp = new RegExp('^[a-z]{0,1}[0-9]{5,8}[a-z]{0,1}$', 'i');
    return miRegExp.test(cad);
}//fin esDNI()

/**
 * Valida una URL
 */
function esURL(cad){
    var miRegExp = new RegExp('^(http|ftp|https):\/{2}.+$', 'i');
    return miRegExp.test(cad);
}//fin esURL()

/**
 * Valida una cadena alfanumerica (no admite espacios)
 */
function esAlfanumerico (cad, min, max) {
    var miRegExp = new RegExp('^w{' + min + ',' + max + '}$', 'i');
    return miRegExp.test(Trim(cad));
}//fin esAlfanumerico()

/**
 * Valida una clave de usuario (letras y numeros NO simbolos)
 */
function esClave(cad, min, max) {
    var miRegExp = new RegExp('^[a-z0-9]{' + min + ',' + max + '}$', 'i');
    return miRegExp.test(cad);
}//fin esClave()

/**
 * Valida si una fecha está en el formato dd/mm/aaaa o dd-mm-aaaa
 * NOTA: aun no verifica los años bisiestos
 */
function esFecha(fecha) {

    var posDia = 0; posMes = 1; posAno = 2;
    var res = false;
    var meses = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    var ANO_MIN = 1900; ANO_MAX = 2100;
    fecha = Trim(fecha);
    if (fecha != '') {
        if (fecha.indexOf('/')) {
            fecha = fecha.split('/');
        }
        else if (fecha.indexOf('-')) {
            fecha = fecha.split('-');
        }//fin if

        if (fecha.length == 3 &&  typeof(fecha) == 'object') {
            dia = forceInt(fecha[posDia], 10);
            mes = forceInt(fecha[posMes], 10);
            ano = forceInt(fecha[posAno], 10);
            if (esBisiesto(ano)) {
            	meses[1] = 29;//Si es bisiesto Febrero tiene 29 dias
            }

            if (isNaN(dia) || isNaN(mes) || isNaN(ano)) {
            	res = false;
            }
        	else if (ano < ANO_MIN || ano > ANO_MAX) {
        		res = false;
        	}
        	else if (mes >= 1 && mes <= 12) {
                if (meses[mes-1] >= dia && dia >= 1) {
                    res = true;
                }//fin if
            }//fin if
        }//fin if
    }//fin if
    return res;
}//fin esFecha()

/**
 * Valida si una hora está en el formato hh:mm ó hh:mm:ss
 */
function esHora(hora) {
    var res = false;
    var posHora = 0; posMin = 1; posSeg = 2;
    hora = Trim(hora);
    if (hora != '') {
        if (hora.indexOf(':')) {
            hora = hora.split(':');
            if (hora.length == 2 || hora.length == 3) {
                horas = forceInt(hora[posHora], 10);
                mins  = forceInt(hora[posMin],  10);
                if ((horas >= 0 && horas <= 23) && (mins >= 0 && mins <= 59 && hora[posMin].length == 2)) {
                    if (hora.length == 3) {
                        segs = forceInt(hora[posSeg], 10);
                        if (segs >= 0 && segs <= 59 && hora[posSeg].length == 2) {
                            res = true;
                        }//fin if
                    }
                    else {
                        res = true;
                    }//fin if
                }//fin if
            }//fin if
        }//fin if
    }//fin if
    return res;
}//fin esHora()



/*
 * Elimina los espacios en blanco por la izqda.
 */
function LTrim(str) {
var whitespace = new String(" \t\n\r");
var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;

		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}//fin if
	return s;
}//fin LTrim()

/*
 * Elimina los espacios en blanco por la dcha.
 */
function RTrim(str) {
var whitespace = new String(" \t\n\r");
var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
  		var i = s.length - 1;       // Get length of string
  		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
   			i--;
 		s = s.substring(0, i+1);
	}//fin if
	return s;
}//fin RTrim()

/*
 * Elimina los espacios en blanco por ambos lados
 */
function Trim(str){
	return RTrim(LTrim(str));
}//fin Trim()

/* Función que comprueba si es un numero entero */

function EsEntero(Chain){
	var Template = new RegExp('^[0-9]+$'); //Formato de numero entero sin signo
	return Template.test(Chain); //Compara "Chain" con el formato "Template"
}

function esBisiesto(ano) {
	return (ano % 4 == 0) && (ano % 100 != 0);
}

function forceInt(num, radix) {
	if (EsEntero(num)) {
		return parseInt(num, radix);
	}
	else {
		return NaN;
	}//fin if
}//fin forceInt()


/*
 * Recibe una fecha en formato dd-mm-yyyy o dd/mm/yyyy y la
 * convierte a un Date de javascript.. (parte d una fecha valida)
 */
function strToDate(str) {
	var posDia = 0; posMes = 1; posAno = 2;
	var dia, mes, ano;
	var arrFecha;
	var strFecha;
    strFecha = Trim(str);
    if (strFecha != '') {
        if (strFecha.indexOf('/')) {
            arrFecha = strFecha.split('/');
        }
        else if (fecha.indexOf('-')) {
            arrFecha = strFecha.split('-');
        }//fin if

        if (arrFecha.length == 3 &&  typeof(arrFecha) == 'object') {
            dia = forceInt(arrFecha[posDia], 10);
            mes = forceInt(arrFecha[posMes], 10);
            ano = forceInt(arrFecha[posAno], 10);
			return new Date(ano,mes,dia,0,0,0,0);
		}
	}
	return false;
}