// funcion que comprueba que el campo ha sido rellenado
function esBlanco(campo) {
    if (campo.length == 0)
        return true;
    else
        return false;
}

/***************************************************************************************
 VALIDAR NUMERO DE TELEFONO (OMS)
 Segun los siguientes criterios:
	- con prefijo del país: un signo y 11 dígitos numéricos (+XXXXXXXXXXX).
	- sin prefijo del país: 9 dígitos numéricos (XXXXXXXXX).
***************************************************************************************/
function esTelefono(elemento) {

	var strAux = Trim(elemento.value);
	var tam = strAux.length;
	if ((tam != 9) && (tam != 12)) {
		return false;
	} 
	if ((tam == 9) && (!esNumeros(strAux))) {
		return false;
	}
	if (tam == 12) {
		var strAux2 = strAux.substring(1,tam);
		if ((strAux.substr(0,1) != "+") || (!esNumeros(strAux2))) {
			return false;
		}
	}
	return true;
}

/***************************************************************************************
 VALIDAR DIRECCION DE CORREO ELECTRONICO (OMS)
 Segun los siguientes criterios:
	- la cadena contiene una @.
	- la cadena contiene algo antes de la @.
	- la cadena contiene algo después de la @.
***************************************************************************************/
function esEmail(variable) {
	var strAux = Trim(variable);
	var posArr = strAux.indexOf("@");
	
	if ((posArr == -1) || (posArr == 0) || (posArr == (strAux.length-1))) {		
		return false;
	}
	else {
		return true;
	}
}

/***************************************************************************************
 VALIDA SI UNA CADENA DE CARACTERES ESTA FORMADA UNICAMENTE POR DIGITOS NUMERICOS
        - Entrada:  cadena de caracteres
	- Devuelve: true/false
***************************************************************************************/
function esNumeros(variable) {
	var patron = /^\d+$/;
	return patron.test(variable);
}

//******************************************************
//Función que devuelve una cadena 
//sin espacios por la derecha y la izquierda.
//Elemento:Es el objecto a validar(la caja de texto).
//******************************************************
function Trim(str) {
	var resultStr = '';
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);	
	return resultStr;
}
//***************************************************************
//Función que quita los espacios por la izquierda.
//str:la cadena a limpiar.
//***************************************************************
function TrimLeft(str) {
	var resultStr = '';
	var i =  0;
	var len = 0 ;
	if (str+'' == 'undefined' || str == null){return null;}
	str += '';
	if (str.length == 0){
		resultStr = '';
	}else{	
		len = str.length;					
  		while ((i <= len) && (str.charAt(i) == " ")){i++;}
  		resultStr = str.substring(i, len);
  	}		
  	return resultStr;
}
//**********************************************************
//Función que quita los espacios por la derecha.
//str:la cadena a limpiar.
//**********************************************************
function TrimRight(str) {
	var resultStr='';
	var i=0;
	if (str+'' == 'undefined' || str == null){return null;}
	str += '';
	if (str.length == 0){
		resultStr = '';
	}else{
  		i = str.length - 1;
  		while ((i >= 0) && (str.charAt(i) == ' ')){i--;}
	  	resultStr = str.substring(0, i + 1);
	}
	return resultStr;
}

// funcion que comprueba si el campo es numerico decimal (positivo o negativo)
// El caracter del decimal es el punto o la coma.
function esDecimal(campo) {
    if (esEntero(campo) || esBlanco(campo))
        return true;
    
    var posPunto = campo.indexOf(".");
    if (posPunto < 0)
        posPunto = campo.indexOf(",");
    if (posPunto < 0)
        return false;
    
    if (!esEntero(campo.substring(0, posPunto)))
        return false;
    
    if (!esEntero(campo.substring(posPunto + 1)))
        return false;
        
    if (campo.charAt(posPunto + 1) == '-') 
        return false;
        
    return true;
    
}


// funcion que comprueba si el campo es numerico entero (positivo o negativo)
function esEntero(campo) {
    var inLen = campo.length;
        
    for (var i=0; i < inLen; i++) {
        var ch = campo.substring(i, i + 1);
        if ((ch < "0") || ("9" < ch)) {
            if (i != 0) return false;
    		else 
                if (ch != "-") return false;
        }
    }
    return true;
}

function compara_fechas(fechaDesde, fechaHasta){
    var fecha1, fecha2
    	
	fecha1 = parseInt(fechaDesde.slice(6,10) + fechaDesde.slice(3,5) + fechaDesde.slice(0,2), 10);
	fecha2 = parseInt(fechaHasta.slice(6,10) + fechaHasta.slice(3,5) + fechaHasta.slice(0,2), 10);
	
	
	 if (fecha1 > fecha2)
	   return true;
	 else  
	   return false;

}
// funcion que comprueba si el campo es numerico decimal (positivo o negativo)
// El caracter del decimal es la coma y los miles pueen ir separadoa por punto.
function esDecimalFormateado(campo) {
    if (esEntero(campo) || esBlanco(campo))
        return true;
        
    var posComa = campo.indexOf(",");    
    
    if (!esEntero(campo.substring(0, posComa))) {
    	var posPunto = campo.indexOf(".");
    	
    	if (!esEntero(campo.substring(0, posPunto)))
        	return false;   
    	if (!esEntero(campo.substring(posPunto + 1, posComa-1)))
        	return false;   
    }
        
    if (!esEntero(campo.substring(posComa + 1)))
        return false;        
        
    if (campo.charAt(0) == '-')
    	return false;    
                 
    return true;
    
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function esFecha(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}
