//////////////////////////////////////////
// funcion que valida fecha de nacimiento
//////////////////////////////////////////
function IsNumeric(valor) 
{ 
var log=valor.length; var sw="S"; 
for (x=0; x<log; x++) 
{ v1=valor.substr(x,1); 
v2 = parseInt(v1); 
//Compruebo si es un valor numérico 
if (isNaN(v2)) { sw= "N";} 
} 
if (sw=="S") {return true;} else {return false; } 
} 

var primerslap=false; 
var segundoslap=false; 
function formateafecha(fecha) 
{ 
var long = fecha.length; 
var dia; 
var mes; 
var ano; 

if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
else { fecha=""; primerslap=false;} 
} 
else 
{ dia=fecha.substr(0,1); 
if (IsNumeric(dia)==false) 
{fecha="";} 
if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
} 
if ((long>=5) && (segundoslap==false)) 
{ mes=fecha.substr(3,2); 
if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
else { fecha=fecha.substr(0,3);; segundoslap=false;} 
} 
else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
if (long>=7) 
{ ano=fecha.substr(6,4); 
if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } } 
} 

if (long>=10) 
{ 
fecha=fecha.substr(0,10); 
dia=fecha.substr(0,2); 
mes=fecha.substr(3,2); 
ano=fecha.substr(6,4); 
// Año no viciesto y es febrero y el dia es mayor a 28 
if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
} 
return (fecha); 
} 
////////////////////////////////////////////////
// valida fecha en conjunto
////////////////////////////////////////////////
function esDigito(sChr){
var sCod = sChr.charCodeAt(0);
return ((sCod > 47) && (sCod < 58));
}

function valSep(oTxt){
var bOk = false;
bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));
bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
return bOk;
}

function finMes(oTxt){
var nMes = parseInt(oTxt.value.substr(3, 2), 10);
var nRes = 0;
switch (nMes){
case 1: nRes = 31; break;
case 2: nRes = 29; break;
case 3: nRes = 31; break;
case 4: nRes = 30; break;
case 5: nRes = 31; break;
case 6: nRes = 30; break;
case 7: nRes = 31; break;
case 8: nRes = 31; break;
case 9: nRes = 30; break;
case 10: nRes = 31; break;
case 11: nRes = 30; break;
case 12: nRes = 31; break;
}
return nRes;
}

function valDia(oTxt){
var bOk = false;
var nDia = parseInt(oTxt.value.substr(0, 2), 10);
bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
return bOk;
}

function valMes(oTxt){
var bOk = false;
var nMes = parseInt(oTxt.value.substr(3, 2), 10);
bOk = bOk || ((nMes >= 1) && (nMes <= 12));
return bOk;
}

function valAno(oTxt){
var bOk = true;
var nAno = oTxt.value.substr(6);
bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
if (bOk){
for (var i = 0; i < nAno.length; i++){
bOk = bOk && esDigito(nAno.charAt(i));
}
}
return bOk;
}

function valFecha(oTxt)
{
	var bOk = true;
	if (oTxt.value != "")
	{
		bOk = bOk && (valAno(oTxt));
		bOk = bOk && (valMes(oTxt));
		bOk = bOk && (valDia(oTxt));
		bOk = bOk && (valSep(oTxt));
		if (!bOk)
		{
		alert("Fecha inválida");
		oTxt.value = "";
		}
	}
	else
	{
		alert("El Campo FECHA DE NACIMIENTO no debe ser Vacia!");
		return -1;
	}
}
//////////////////////////////////////////
//	Funcion para validar solo texto
//////////////////////////////////////////
function valida_texto ( texto, valor )
{
	var texto = document.getElementById(texto);
	
	var textoRegxp = /^([a-zA-Z ñÑáéíóúÁÉÍÓÚ.,]+)$/;

	if(texto.value == "" && valor == 1)
	{
		alert("El Campo "+ texto.id.toUpperCase() +" no debe ser vacio..!");	
		return -1;
	}
	else
	{
		if(texto.value != "")
		{
			if ( textoRegxp.test(texto.value) == false )
			{	
				alert("ERROR! El campo "+ texto.id.toUpperCase() +" solo debe contener texto") ;
				//texto.focus() ;
				return -1 ;
			}
		}
	}
	return 1 ;
}

/////////////////////////////////////////
//	Funcion para validar solo números
/////////////////////////////////////////
function valida_numero ( numero, valor )
{	
	var numero = document.getElementById(numero);

	var numeroRegxp = /^([0-9]+)$/;

	if(numero.value == "" && valor == 1)
	{
		alert("El Campo "+ numero.id.toUpperCase() +" no debe ser vacio..!");	
		return -1;
	}
	else
	{
		if(numero.value != "")
		{
			if ( numeroRegxp.test(numero.value) == false )
			{
				alert ("ERROR: El campo "+ numero.id.toUpperCase() +" solo debe contener números") ;
				//document.hoja1.elements[control].focus() ;
				return -1 ;
			}
		}
	}

	return 1 ;
}

/////////////////////////////////////////
//	Funcion para validar solo números
/////////////////////////////////////////
function valida_porcentaje ( numero )
{
	if ( numero.value == "" )
	{
		return true ;
	}
	
	var numeroRegxp = /^[0-9]+(.[0-9]+)*$/;
	
	if ( numeroRegxp.test(numero.value) == false )
	{
		var control = numero.name ;
		alert ("ERROR: El campo solo debe contener números") ;
		document.hoja1.elements[control].focus() ;
		return false ;
	}

	return true ;
}

/////////////////////////////////////////
//	Funcion para validar alfanumericos
/////////////////////////////////////////
function valida_alfanumerico ( cadena, valor )
{
	var cadena = document.getElementById(cadena);
	
	var cadenaRegxp = /^[a-z0-9A-Z ñÑáéíóúÁÉÍÓÚ.,]+$/  /*/^[0-9]+([a-zA-Z ]+)*$/;*/
	
	if(cadena.value == "" && valor == 1)
	{
		alert("El Campo "+ cadena.id.toUpperCase() +" no debe ser vacio..!");	
		return -1;
	}
	else
	{
		if(cadena.value != "")
		{
			if ( cadenaRegxp.test(cadena.value) == false )
			{	
				alert("ERROR: El campo "+ cadena.id.toUpperCase() +" solo debe contener alfanumericos") ;		
				//texto.focus() ;
				return -1 ;
			}
		}
	}
	
	return 1;
}

/////////////////////////////////////////
//	Funcion para validar email
/////////////////////////////////////////
function valida_email ( email )
{
	if ( email.value == "" )
	{
		return true ;
	}

	var emailRegxp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/ ;
	
	if ( emailRegxp.test(email.value) == false )
	{
		alert ("ERROR: El email no esta escrito correctamente") ;
		//document.hoja1.elements[email.name].focus() ;
		return false ;
	}

	return true ;
}

/////////////////////////////////////////
//	Funcion para validar web
/////////////////////////////////////////
function valida_web ( web )
{
	if ( web.value == "" )
	{
		return true ;
	}

	var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;

	if ( urlRegxp.test(web.value) == false )
	{
		alert ("ERROR: La dirección web no esta escrita correctamente") ;
		document.hoja1.elements[web.name].focus() ;
		return false ;
	}
	return true ;
}
///////////////////////////////////////////////
// valida fechas de mes y año 
///////////////////////////////////////////////
var segundoslap=false; 
function valida_mes_anio(fechaid) 
{
	var fecha = document.getElementById(fechaid).value;
	
	var long = fecha.length; 
	//var dia; 
	var mes; 
	var ano; 

	if ((long >= 2) && (segundoslap == false)) 
	{ 
		mes = fecha.substr(0,2); 
		
		if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) 
		{ 
			document.getElementById(fechaid).value = fecha.substr(0,2)+"/"+fecha.substr(3,4); 
			segundoslap=true; 
		} 
		else 
		{ 
			document.getElementById(fechaid).value = "";
			segundoslap=false;
		} 
	} 
	else 
	{ 
		if ((long<=2) && (segundoslap=true)) 
		{ 
			document.getElementById(fechaid).value = fecha.substr(0,1); 
			segundoslap=false; 
		} 
	} 
	
	if (long >= 3) 
	{ 
		ano = fecha.substr(3,4); 
		if (IsNumeric(ano) == false) 
		{ 
			document.getElementById(fechaid).value = fecha.substr(0,2); 
		} 
		else 
		{ 
			if (long == 7) 
			{ 
				
				if ((ano==0) || (ano<1900) || (ano>2100)) 
				{ 
					document.getElementById(fechaid).value = fecha.substr(0,2); 
				} 
			} 
		} 
	} 

	if (long >= 7) 
	{ 
		document.getElementById(fechaid).value = fecha.substr(0,7); 
		//dia=fecha.substr(0,2); 
	} 
	return (fecha); 
} 
//////////////////////////////////////////////////////
// funcion que valida si no esta vacia la fecha de 
// ingreso de tiempo de trabajo
//////////////////////////////////////////////////////
function valida_fecha_trabajo(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}
/*******************************************************
///////////////////////////////////////////////////////
// validacion del formulario completo antes de guardar
///////////////////////////////////////////////////////
*******************************************************/
function validar_formulario()
{
	var sw1 = 1;
	var sw3 = 1;
	
	///////////////////////////////////////////
	// valida datos personales del postulante
	///////////////////////////////////////////
	if(valida_texto('apellido_paterno',1) == 1 && valida_texto('apellido_materno',1) == 1 && valida_texto('nombres',1) == 1)
	{
		sw1 = 0;
	}	
	else
	{
		sw1 = 1;
		return false;
	}	
	
	if(document.getElementById('fecha_nacimiento').value == "")
	{
		alert('La fecha de Nacimiento no debe ser Vacia');
		sw1 = 1;
		return false;
	}
	
	if(document.getElementById('cedula_identidad').value == "")
	{
		alert('La cedula de identidad no debe ser Vacia');
		sw1 = 1;
		return false;
	}
	
	if(document.getElementById('login').value == "")
	{
		alert('el campo LOGIN no debe de ser vacio');
		sw = 1;
		return false;
	 }
	 
	if(document.getElementById('contrasenia').value == "")
	{
		alert("el campo CONTRASE"+'\u00d1'+"A no debe de ser vacio");
		sw = 1;
		return false;
	 }
	 
	if(document.getElementById('rcontrasenia').value == "")
	{
		alert("el campo CONFIRMAR CONTRASE"+'\u00d1'+"A no debe de ser vacio");
		sw = 1;
		return false;
	 }
	
	if(valida_texto('zona_direccion',1) == 1 && valida_alfanumerico('manzanocallenumero',1) == 1 )
	{
		sw1 = 0;
	}	
	else
	{
		sw1 = 1;
		return false;
	}	

	if(document.getElementById('telefono').value == "")
	{
		alert('el campo telefono no debe ser Vacia');
		sw1 = 1;
		return false;
	}
	
	/////////////////////////////////
	// valida datos colegio egreso
	////////////////////////////////
	if(valida_texto('nombre_colegio',1) == 1 && valida_texto('departamento',1) == 1 )
	{
		sw3 = 0;
	}	
	else
	{
		sw3 = 1;
		return false;
	}	
	
	if(document.getElementById('anio_egreso').value == "")
	{
		alert("el campo a"+'\u00f1'+"o de egreso no debe ser Vacia");
		sw3 = 1;
		return false;
	}
	//////////////////////////////////////////
	// verifica si hubo algun error en alguno
	//////////////////////////////////////////
	if(sw1 == 0 && sw3 == 0 )
	{
		return true;
	}
	else
	{
		return false;
	}
}

///////////////////////////////////////////////////////////////////////////
// funciones para validar fechas de trabajos
///////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
// funcion que valida si no esta vacia la fecha de 
// ingreso de tiempo de trabajo
//////////////////////////////////////////////////////
function valida_fecha_trabajo11(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}

function valida_fecha_trabajo12(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}

function valida_fecha_trabajo21(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}

function valida_fecha_trabajo22(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}

function valida_fecha_trabajo31(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}

function valida_fecha_trabajo32(fecha,valor)
{
	fecha = document.getElementById(fecha).value;
	
	var longitud = fecha.length;
	if(fecha == "" && valor == 1)
	{
		alert("El Campo de Fecha no debe ser vacio..!");	
		return valor;
	}
	
	if(fecha != "")
	{
		if ( longitud != 7 || fecha.substr(2,1) != "/")
		{
			alert ("ERROR: El campo de "+fecha+" no esta en Formato (mm/aaaa)") ;
			return valor ;
		}	
	}
	
	return 1 ;	
}

function validar2(e){
tecla_codigo = (document.all) ? e.keyCode : e.which;
if(tecla_codigo==8)return true;
patron =/[0-9]/;
tecla_valor = String.fromCharCode(tecla_codigo);
return patron.test(tecla_valor);

}

function checkEmail(whatYouTyped) 
{
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) 
	{
		document.getElementById("guardar_servicio").disabled = "";
		
	} 
	else 
	{
		alert("Ingrese una direccion valida");
		document.getElementById("guardar_servicio").disabled = "disabled";
		document.envio.email.focus();
		//donde envio es el nombre del formulario 
		//y email es el nombre del campo
	}
}

function validaemail(whatYouTyped)
{
	var fieldset = whatYouTyped.parentNode;
	var txt = whatYouTyped.value;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) 
	{
		//document.getElementById("guardar_servicio").disabled = "";
		
	} 
	else 
	{
		alert("Ingrese un correo electronico valido");
		//document.getElementById("guardar_servicio").disabled = "disabled";
		document.envio.email.focus();
		//donde form1 es el nombre del formulario 
		//y correo_electronico es el nombre del campo
	}  
  

 }
///////////////////////////////////////////////////////////////////////////1