function validar(input,tipo){
	
	var bAllOk = true;
	var errColor = '#FFBE00';
	var normalColor = '#FFFFFF';
	
	// valida si el string esta vacio 
	if(tipo == "string"){
		if ((input.value.length==0) || (input.value==null)) {
			input.style.backgroundColor = errColor;
			return false;
		}else{
			input.style.backgroundColor = normalColor;
			return true;
		}
	}
	
	// valida email
	if(tipo == "email"){
		var emailRegEx = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
		var str = input.value;
		if(str.match(emailRegEx)){
			//Email valido
			input.style.backgroundColor = normalColor;
			return true;
		}else{
			//Email invalido
			input.style.backgroundColor = errColor;
			return false;
		}
	}

	// valida numero entero
	if(tipo == "entero"){
		var i;
    	var error;
	    var sPalabra = input.value;
	    for (i = 0; i < sPalabra.length; i++){
	        var c = sPalabra.charAt(i);
    	    if (!isDigit(c)){
				error = false;
				break;
		    }else{
				error = true;
			}
		}
		
		if(error == true){
		    input.style.backgroundColor = normalColor;
			return true; 
		}else{
			input.style.backgroundColor = errColor;
			return false; 
		}
     }
     
     // valida numero decimal
     if(tipo == "decimal"){
	   var ValidChars = "0123456789.";
   	   var IsNumber=true;
	   var Char;
	
	   for (i = 0; i < input.value.length; i++){ 
	      Char = input.value.charAt(i); 
    	  if (ValidChars.indexOf(Char) == -1){
	         IsNumber = false;
	         break;
          }else{
          	 IsNumber = true;
          }
        }
        
        if(IsNumber == true){
		   input.style.backgroundColor = normalColor;
			return true; 
		}else{
			input.style.backgroundColor = errColor;
			return false; 
		}
    }
    
    // valida que el string sea solo letras 
    if(tipo == "caracteres"){
    	var i;
    	var error;
	    var sLower = input.value.toLowerCase();
    	for (i = 0; i < sLower.length; i++){
	        var c = sLower.charAt(i);
    	    if (!isLetter(c)){
				error = false;
				break;
		    }else{
				error = true;
			}
		}
		
		if(error == true){
		    input.style.backgroundColor = normalColor;
			return true; 
		}else{
			input.style.backgroundColor = errColor;
			return false; 
		}
	}
	
	
}

function compararIgualdad(input1,input2){

	var errColor = '#FFBE00';
	var normalColor = '#FFFFFF';
	
	if(input1.value != input2.value){
		input1.style.backgroundColor = errColor;
		input2.style.backgroundColor = errColor;
		return false;
	}else{
		input1.style.backgroundColor = normalColor;
		input2.style.backgroundColor = normalColor;
		return true;
	}
	
}
/* funciones para validar formularios */
// es letra

function isLetter (c)
{
	var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzáéíóúñü. ";
	var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ. ";
	
    return( ( uppercaseLetters.indexOf( c ) != -1 ) ||
            ( lowercaseLetters.indexOf( c ) != -1 ) )
}
// si es un digito
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// si es un digito o una letra
function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

// si es un numero
function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if( i != 0 ) {
            if (!isDigit(c)) return false;
        } else { 
            if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}
// fin funciones para validar formularios 

// validar checkbox
function validarCheckbox(checks,cant,tipo){
  
  var count = 0;
  for (var j=0; j<checks.length; j++){
     if (checks[j].checked){
     	count++;
     }
  }

  if(tipo == "mayor"){
	  if (count>cant && count>0){ 
	  	return true;
	  }else{
	  	return false;
	  }
  }else if(tipo == "menor"){
  	  if (count<cant && count>0){ 
	  	return true;
	  }else{
	  	return false;
	  }
  }else if(tipo == "igual"){
  	  if (count==cant && count>0){ 
	  	return true;
	  }else{
	  	return false;
	  }
  }
  
}
// validar radio button 
function validarRadio(radio_b){
	var radio = radio_b.length;
	for (var j=0; j<radio; j++){
		if(radio_b[j].checked){
			return true;
		}
	}
	return false;
	
}

//validar checkbox button 
function validarCheckbox(checkbox){
	for (i=0; i<checkbox.length; i++) {
		if (checkbox[i].checked == true) {
	    	 return true;
	    	 break;
	    }
	}
	return false;
}
