// LIBRERIA JAVASCRIPT PARA LA VALIDACION DE CAMPOS DE FORMULARIOS

function validarTelefono(x) {
	var checkOK = "0123456789+-()" 
	var checkStr = x.value; 
	var allValid = true; 
	var allNum = "" 
	for (i = 0; i < checkStr.length; i++) { 
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j)) 
			break; 
			if (j == checkOK.length) { 
				allValid = false; 
				break; 
			} 
			allNum += ch; 
	}
	if (!allValid) { 
		x.focus(); 
		return false; 
	}
	return true;
}
function validarEmail(x) {
	if ((x.value.indexOf ('@', 0) == -1)
		||(x.value.length < 5) || (x.value.indexOf('.',0) == -1)) { 
		x.focus(); 
		return false; 
	} 
	return true; 
} 
function escribirMsg(idCampo, texto){
   if (document.getElementById(idCampo)){
	  document.getElementById(idCampo).innerHTML=texto;
    }
}
function campoVacio(campo1){
   return (campo1==null || campo1.value == '' || campo1.lenght == 0);
}

function bisiesto(x){
   return ( (x % 4 == 0 && x % 100 != 0)  || x % 400 == 0)
}

function bisiesto2(x)
{
	if(x % 4 == 0)
 	{
    		if ( x % 100 == 0)
    		{
			return false;
    		}
    		else
    		{
			return true;
    		}
  	}
  	if (x % 400 == 0)
  	{
    		return true;
  	}
  	else
  	{
    		return false;
  	}	
}

function menorActual ( d, m, y)
{
	var dd, mm, yy;
	var Actual = new Date();
	yy = Actual.getYear();
	mm = Actual.getMonth();
	dd = Actual.getDate();

	if ( y < yy)
	{
		return true;
	}
	if ( y == yy )
	{
		if ( m < mm )
		{
			return true;

		}
		if ( m == mm ) 
		{
			if ( (d < dd) || (d == dd) )
			{
				return true;
			}
			else
			{
				return false;	
			}
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}
function validarFecha ( d, m, y)
{
	
	febrero = 28;
	if (bisiesto(y))
	{
		febrero = 29;
	}
	
	if (m=="1")
	{
		if ((d>febrero))
		{		
			return false;
		}
		else
		{
			return true;
		}
	}
	else if (((m=="3") || (m=="5") || (m=="8") || (m=="10")))
	{
		if ((d>30))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
function validaAlfanumerico (valor) {
   var valido = /^([0-9]|[a-z]|[A-Z]|\s|\.|-)+$/
   return valido.test(valor);
}

function rellenarCeros(x){
	if( (0 < x) && (x < 100))
	{
		return ("000" + x);
	} 
	else if ( (9 < x) && (x < 100) )
	{
		return ("00" + x);
	}
	else if ( (99 < x) && (x < 1000))
	{
		return ("0" + x);
	}
	else
	{
		return x;
	}
}
/********************************************************/
/* Autor: ABDOMIN										*/
/* Fecha: 13-06-06										*/
/* Descripción: Validar un número con decimales			*/
/********************************************************/

//Sólo se permite el caracter ',' para los decimales
function valida_numero_decimal(Dato){

	for (var i=0; i < Dato.length; i++){
 		if (compruebaCaracter(Dato.charAt(i))){ 
		  		return false;
		}
	}
	if(!valida_comas_decimales(Dato)){
		return false;
	}else{
		return true;
	}
}
function compruebaCaracter(val) {
		var digits = "abcdefghijklmnñ?opqrstuvwxyzABCDEFGHIJKLMNÑ¿OPQRSTUVWXYZ!#$%&'()*+-/@[\]^_`{|}~.<>";
		for (var i=0; i < val.length; i++) {if (digits.indexOf(val.charAt(i)) == -1) { return false; }}
		return true;
}

function compruebaNumero(val) {
		var digits = "1234567890";
		for (var i=0; i < val.length; i++) {if (digits.indexOf(val.charAt(i)) == -1) { return false; }}
		return true;
}
function valida_comas_decimales(Dato){
    var numero_de_comas = 0;
    for(var i=0; i < Dato.length; i++){
		if(Dato.charAt(i)==','){
			numero_de_comas++;
		}
	}
	if(numero_de_comas==0) {
		return true;
		
	}else if(numero_de_comas==1){
	    if(Dato.length==1){
			return false;
		}else{
			if(Dato.substr(0,1) == ',' || Dato.substr(Dato.length-1,1) == ','){
				return false;
			}else{
				return true;
			}
		}
	}else if(numero_de_comas>1){
		return false;
	}
}
/*****************************************************************/
/* Autor: ABDOMIN										         */
/* Fecha: 13-06-06										         */
/* Descripción: Validar el tamaño de un documento adjunto        */
/* Recibe: Tamaño máximo que permite y la ruta completa.		 */
/* Devuelve: boolean  							         		 */
/* Ej: validaTamanioDocumento(50,formulario.nmDocumento.value))  */
/*****************************************************************/
function validaTamanioDocumento(tamMax,rutaDoc){
  	var posSeparator=0;
  	var sw=0;
  	for (var indiceDoc=rutaDoc.length; indiceDoc>0 && sw!=1; indiceDoc--){
  		if (rutaDoc.substring(indiceDoc,indiceDoc-1) == '\\')
  		{
  			posSeparador = indiceDoc;
  			sw=1;
  		}
  	}
  	if ( (rutaDoc.substring(posSeparador,rutaDoc.length)).length >= tamMax ){
  		return true;
  	}else{
  		return false;
  	}
 }
/*****************************************************************/
/* Autor: ABDOMIN										         */
/* Fecha: 29-06-06										         */
/* Descripción: Validar los campo textos de un formulario		 */
/*			    Obligatorio.                                     */
/*****************************************************************/
function mandatoryForm (pFormulario){
	var sw=0;
	for(i = 0; i < pFormulario.length;i++){
		if (pFormulario[i].type == 'text'){
				if(campoVacio(pFormulario[i])){
					pFormulario[i].className = "cajaobligatorioXX";
    				sw=1;
    			}else{
    				pFormulario[i].className = "cajaXX";
    			}
    	}
    }
    if(sw==1){return false;}
    else return true;
} 
/*****************************************************************/
/* Autor: ABDOMIN										         */
/* Fecha: 29-06-06										         */
/* Descripción: No permite introducir el caracter 'intro'		 */
/*****************************************************************/
function validarKey(pCampo){
	
		var bCancelarEntrada = false;
		var iKeyCode_Tabulador = 13;
				
		if(window.event){
			var cKeyCodeRecibido = window.event.keyCode; 	// Caracter recibido IE:
		} else if(E.which){
			var cKeyCodeRecibido = E.which; 	// Caracter recibido NS:
		}
		if(cKeyCodeRecibido == iKeyCode_Tabulador){
			
			bCancelarEntrada = true;
			if(window.event){
				window.event.returnValue = false;
			}else if(E.which){
				window.addEventListener("keypress", stopit, false);
				setTimeout("window.removeEventListener('keypress', stopit, false)",1); //Solo es para que se ejecute con un poco de retardo, si se ejecuta automáticamente después no hace nada el addEventListener
			}
		}
		return bCancelarEntrada;	// devuelve true si ha cancelado la entrada, es decir, se ha alcanzado el maximo.	
}

/*****************************************************************/
/* Autor: ABDOMIN										         */
/* Fecha: 26-03-07										         */
/* Descripción: Activa todos los campos de un formulario		 */
/*****************************************************************/
function activaCamposFormulario(pFormulario, op){
	switch (op) {
		case 'TEXT':
			var inputs = pFormulario.getElementsByTagName('Input');
			if (inputs){
				for(var i = 0; i < inputs.length;i++)
					inputs[i].disabled=false; 
			}	
		case 'SELECT':		
			var combos = pFormulario.getElementsByTagName('Select');
			if (combos){
				for(var i = 0; i < combos.length;i++)
					combos[i].disabled=false; 
			}	
			break;
		case 'TEXTAREA':		
			var textArea = pFormulario.getElementsByTagName('TextArea');
			if (textArea){
				for(var i = 0; i < textArea.length;i++)
					textArea[i].disabled=false; 
			}	
			break;	
		default:
			break;
	}	
}

/*****************************************************************/
/* Autor: ABDOMIN										         */
/* Fecha: 26-03-07									         */
/* Descripción: Desactiva todos los campos de un formulario		 */
/*****************************************************************/
function desactivaCamposFormulario(pFormulario, op){
	switch (op) {
		case 'TEXT':
			var inputs = pFormulario.getElementsByTagName('Input');
			if (inputs){
				for(var i = 0; i < inputs.length;i++)
					inputs[i].disabled=true; 
			}	
		case 'SELECT':		
			var combos = pFormulario.getElementsByTagName('Select');
			if (combos){
				for(var i = 0; i < combos.length;i++)
					combos[i].disabled=true; 
			}	
			break;
		case 'TEXTAREA':		
			var textArea = pFormulario.getElementsByTagName('TextArea');
			if (textArea){
				for(var i = 0; i < textArea.length;i++)
					textArea[i].disabled=true; 
			}	
			break;	
		default:
			break;
	}	
}