/* Funciones Genéricas para los tratamientos de formularios.
/* Todas las funciones tienen en cuenta que para las validaciones existe un elemento que admite código
/* HTML dentro y cuyo id es el nombre del campo a validar más '_msg'
*/

function limpiarCampo(campo){
	var c = document.getElementById(campo);
	c.value='';
}	

function validarCampo(IdCampo){
	var campo = document.getElementById(IdCampo);
	var msg = document.getElementById(IdCampo+'_msg');
	msg.innerHTML="";
	if (campo.value == '' || campo.value.length <= 0){
		msg.innerHTML=" Campo obligatorio";
		return false;
	}
	return true;
}

function validarPassword(pass,confirmacion){
	var campoPass = document.getElementById(pass);
	var campoConf = document.getElementById(confirmacion);
	var msg = document.getElementById(confirmacion+'_msg');
	msg.innerHTML="";
	if (campoPass.value != campoConf.value){
		msg.innerHTML="Contrase&ntilde;a y validaci&oacute;n no coinciden";
		return false;
	}
	return true;
}

function numbersonly(myfield, e, allowPoint)
{
var key;
var keychar;
var numbStr = "0123456789";
if (allowPoint)
	numbStr+='.';

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if (((numbStr).indexOf(keychar) > -1))
   return true;
return false;
}

function validarFecha(dia,mes,anio) 
{ 

	var elMes = parseInt(mes,10); 
	var elDia = parseInt(dia,10);
	var elAnio = parseInt(anio,10);

	if(elMes==0 || elMes>12 || elAnio<1900) 
		return false; 
	// MES FEBRERO 
	if(elMes == 2){ 
		if(esBisiesto(elAnio)){ 
			if(elDia>29 || elDia==0){ 
				return false; 
			} 
		}else{
			if(elDia>28 || elDia==0){ 
				return false; 
			} 		
		}
	}else{
		if (elMes==1 || elMes==3 || elMes==5 || elMes==7 || elMes==8 || elMes==10 || elMes==12){
			if (elDia==0 || elDia>31)
				return false;
		}else if(elMes==4 || elMes==6 || elMes==9 || elMes==11){
			if (elDia==0 || elDia>30)
				return false;
		}else{
			return false;
		}
	}  
	return true; 

} 
function esBisiesto(anio) 
{ 
	var BISIESTO; 
	if(parseInt(anio)%4==0){ 
	if(parseInt(anio)%100==0){ 
	if(parseInt(anio)%400==0){ 
	BISIESTO=true; 
	} 
	else{ 
	BISIESTO=false; 
	} 
	} 
	else{ 
	BISIESTO=true; 
	} 
	} 
	else 
	BISIESTO=false; 
	
	return BISIESTO; 
} 
function checkCronologiaFechas(fechaInic_s, fechaFin_s) {
	fInic = createDate(fechaInic_s, '-');
	fFin = createDate(fechaFin_s, '-');

	return (fInic <= fFin);
}
function createDate(date, separator) {
	var d = new Date();
	
	dArray = new Array();
	dArray = date.split(separator);
	
	
	d.setFullYear(dArray[2]);
	d.setMonth(dArray[1] - 1);
	d.setDate(dArray[0]);
	
	d.setHours(0);
	d.setMinutes(0);
	d.setSeconds(0);
	d.setMilliseconds(0);
	
	return d;
}

function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}
