/******************************
*LISTA DE SCRIPT'S DE ORIADM
*******************************/

/*************Mostrar u ocultar capas***************/
function ShowHide(id) {
    if(document.getElementById(id).style["display"] != "none"){
        document.getElementById(id).style["display"] = "none";
    } else {
        document.getElementById(id).style["display"] = "";
    }
}

/*************************************/
//verifica que se ingrese solo numeros
var nav4 = window.Event ? true : false;
function acceptNum(evt){
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57));
}
//VERIFICA QUE SE INGRESE UN EMAIL VALIDO
function validarEmail(direccion) {
       if (direccion.indexOf("@") != -1)
          return true;
       else {
          alert('Debes escribir una dirección de e-mail válida');
          return false;
       }
}
//trim completo
function Trim( str ) {
	var resultStr = "";
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	return resultStr;
}
//trim right
function TrimRight( str ) {
	var resultStr = "";
	var i = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)
	return null;
	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0)
	resultStr = "";
	else {
	// Loop through string starting at the end as long as there
	// are spaces.
	i = str.length - 1;
	while ((i >= 0) && (str.charAt(i) == " "))
	i--;
	
	// When the loop is done, we're sitting at the last non-space char,
	// so return that char plus all previous chars of the string.
	resultStr = str.substring(0, i + 1);
	}
	
	return resultStr;
}
//trim left
function TrimLeft( str ) {
	var resultStr = "";
	var i = len = 0;
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null)
	return null;
	// Make sure the argument is a string
	str += "";
	if (str.length == 0)
	resultStr = "";
	else {
	// Loop through string starting at the beginning as long as there
	// are spaces.
	// len = str.length - 1;
	len = str.length;
	
	while ((i <= len) && (str.charAt(i) == " "))
	i++;
	// When the loop is done, we're sitting at the first non-space char,
	// so return that char plus the remaining chars of the string.
	resultStr = str.substring(i, len);
	}
	return resultStr;
}
//VALIDA REGISTRO
function ValidaRegistro(){
	if(Trim(document.FrmMain.paterno.value).length == 0 ) {
		alert("Debe ingresar el apellido paterno");
		return;
	}
	if(Trim(document.FrmMain.materno.value).length == 0 ) {
		alert("Debe ingresar el apellido materno");
		return;
	}
	if(Trim(document.FrmMain.nombre.value).length == 0 ) {
		alert("Debe ingresar el nombre");
		return;
	}
	if(document.FrmMain.pais.value == "0" ) {
		alert("Debe ingresar el pais de nacimiento");
		return;
	}
	if(document.FrmMain.dia.value == "0" ) {
		alert("Debe ingresar el dia de nacimiento");
		return;
	}
	if(document.FrmMain.mes.value == "0" ) {
		alert("Debe ingresar el mes de nacimiento");
		return;
	}
	if(document.FrmMain.ano.value == "0" ) {
		alert("Debe ingresar el año de nacimiento");
		return;
	}
	if(document.FrmMain.calle.value == "0" ) {
		alert("Debe seleccionar el tipo de calle");
		return;
	}
	if(Trim(document.FrmMain.direccion.value).length == 0 ) {
		alert("Debe ingresar su direccion");
		return;
	}
	/*
	if(document.FrmMain.distrito.value == "0" ) {
		alert("Debe selecionar un distrito");
		return;
	}
	*/
	if(document.FrmMain.phone.value.length < 7 ) {
		alert("Debe ingresar su telefono correctamente");
		return;
	}
	//login
	if(document.FrmMain.email.value.length == 0 ) {
		alert("Debe ingresar su email ");
		return;
	}else{
		if (!(validarEmail(document.FrmMain.email.value))){
			//alert("no contiene @ ")
			return;
		}
	}
	if(document.FrmMain.email2.value.length == 0 ) {
		alert("Debe ingresar verificación de email");
		return;
	}else{
		if (!(validarEmail(document.FrmMain.email2.value))){
			//alert("no contiene @ ")
			return;
		}
	}
	//verificacion igualdad
	if (!( Trim(document.FrmMain.email.value) == Trim(document.FrmMain.email2.value) )){
			alert("Los correos ingresados no son iguales");
			return;
	}
	//contraseña
	if(Trim(document.FrmMain.password.value).length < 6 ) {
		alert("Debe ingresar un password mayor a 6 caracteres");
		return;
	}
	if (!( Trim(document.FrmMain.password.value) == Trim(document.FrmMain.password2.value) )){
			alert("las contraseñas ingresadas no son iguales");
			return;
	}
	/*if (!(validarEmail(document.FrmMain.email3.value))){
			//alert("no contiene @ ")
			return;
	}*/
	//setear valores
	document.FrmMain.name_com.value=document.FrmMain.paterno.value+", "+document.FrmMain.materno.value+", "+document.FrmMain.nombre.value;
	document.FrmMain.fecha.value=document.FrmMain.ano.value+"/"+document.FrmMain.mes.value+"/"+document.FrmMain.dia.value;
	//hacer el envio
	document.FrmMain.submit();
}
//VALIDA PARAMETROS PARA EXPORTACION
function Parametros(){
	i=	document.FrmMain.institucion.options[document.FrmMain.institucion.selectedIndex].value + ":" + 
		document.FrmMain.institucion.options[document.FrmMain.institucion.selectedIndex].text;
	t=	document.FrmMain.postulante.options[document.FrmMain.postulante.selectedIndex].value + ":" + 
		document.FrmMain.postulante.options[document.FrmMain.postulante.selectedIndex].text;
	e=	document.FrmMain.estado.options[document.FrmMain.estado.selectedIndex].value + ":" + 
		document.FrmMain.estado.options[document.FrmMain.estado.selectedIndex].text;
	n=	document.FrmMain.nivel.options[document.FrmMain.nivel.selectedIndex].value + ":" + 
		document.FrmMain.nivel.options[document.FrmMain.nivel.selectedIndex].text;
	s=	document.FrmMain.periodo.options[document.FrmMain.periodo.selectedIndex].value + ":" + 
		document.FrmMain.periodo.options[document.FrmMain.periodo.selectedIndex].text;
	o=	document.FrmMain.orden.value;
	p=	document.FrmMain.n_year.value;
	Hoy		= new Date(); 
	Secs	= Hoy.getTime();
	URL="app/ori/oriadm/pages/ori_export_es.jsp?i="+i+"&t="+t+"&e="+e+"&n="+n+"&s="+s+"&o="+o+"&p="+p+'&now=' + Secs;;
	//alert(URL);
	window.open(URL);
	return false;
	
}
//VALIDA FORMULARIO
function ValidaFormulario(){
	if( document.getElementById("paterno") ) {
		if(Trim(document.FrmMain.paterno.value).length == 0 ) {
			alert("Debe ingresar el apellido paterno");
			return;
		}
	}	
	if( document.getElementById("materno") ) {
		if(Trim(document.FrmMain.materno.value).length == 0 ) {
			alert("Debe ingresar el apellido materno");
			return;
		}
	}
	if( document.getElementById("nombre") ) {
		if(Trim(document.FrmMain.nombre.value).length == 0 ) {
			alert("Debe ingresar el nombre");
			return;
		}
	}
	if( document.getElementById("pais") ) {
		if(document.FrmMain.pais.value == "0" ) {
			alert("Debe ingresar el pais de nacimiento");
			return;
		}
	}
	if( document.getElementById("dia") ) {
		if(document.FrmMain.dia.value == "0" ) {
			alert("Debe ingresar el dia de nacimiento");
			return;
		}
	}
	if( document.getElementById("mes") ) {
		if(document.FrmMain.mes.value == "0" ) {
			alert("Debe ingresar el mes de nacimiento");
			return;
		}
	}
	if( document.getElementById("ano") ) {
		if(document.FrmMain.ano.value == "0" ) {
			alert("Debe ingresar el año de nacimiento");
			return;
		}
	}
	if( document.getElementById("calle") ) {
		if(document.FrmMain.calle.value == "0" ) {
			alert("Debe ingresar el tipo de calle");
			return;
		}
	}
	if( document.getElementById("direccion") ) {
		if(Trim(document.FrmMain.direccion.value).length == 0 ) {
			alert("Debe ingresar su direccion");
			return;
		}
	}
	if( document.getElementById("distrito") ) {
		if(document.FrmMain.distrito.value == "0" ) {
			alert("Debe ingresar el distrito");
			return;
		}
	}
	if( document.getElementById("phone") ) {
		if(document.FrmMain.phone.value.length < 7 ) {
			alert("Debe ingresar su telefono");
			return;
		}
	}
	//login
	if( document.getElementById("email") ) {
		if(document.FrmMain.email.value.length == 0 ) {
			alert("Debe ingresar su email ");
			return;
		}else{
			if (!(validarEmail(document.FrmMain.email.value))){
				//alert("no contiene @ ")
				return;
			}
		}
	}
	//validacion carrera
	if( document.getElementById("carrera") ) {
		if(document.FrmMain.carrera.value == "0" ) {
			alert("Debe elegir la carrera");
			return;
		}
	}
	//validacion de razon
	if( document.getElementById("razon") ) {
		if(document.FrmMain.razon.value.length == 0 ) {
			alert("Debe ingresar la razon");
			return;
		}
	}
	//validacion argumento
	if( document.getElementById("argumento") ) {
		if(document.FrmMain.argumento.value.length == 0 ) {
			alert("Debe ingresar el argumento");
			return;
		}
	}
	//formar cadena de deporte
	var cad_deporte="";
	if( document.getElementById("deporte") ) {
		for (i=0;i<document.FrmMain.deporte.length;i++){
			if (document.FrmMain.deporte[i].checked){
			  cad_deporte = cad_deporte+document.FrmMain.deporte[i].value + "*";
			  //break;
			}
		}
	}
	//alert("deportes: "+cad_deporte);
	if( document.getElementById("dep_cadena") ) {document.FrmMain.dep_cadena.value = cad_deporte;}
	//formar cadena de arte
	var cad_arte="";
	if( document.getElementById("arte") ) {
		for (i=0;i<document.FrmMain.arte.length;i++){
			if (document.FrmMain.arte[i].checked){
			  cad_arte = cad_arte+document.FrmMain.arte[i].value + "*";
			  //break;
			}
		}
	}
	//alert("arte: "+cad_arte);
	if( document.getElementById("arte_cadena") ) {	document.FrmMain.arte_cadena.value = cad_arte;}
	//document.FrmMain.arte.length
	
	
	//limpiar cadenas
	if( document.getElementById("paterno") ) document.FrmMain.paterno.value = Limpiar(document.FrmMain.paterno.value);
	if( document.getElementById("materno") ) document.FrmMain.materno.value = Limpiar(document.FrmMain.materno.value);
	if( document.getElementById("nombre") ) document.FrmMain.nombre.value = Limpiar(document.FrmMain.nombre.value);
	if( document.getElementById("dni") ) document.FrmMain.dni.value = Limpiar(document.FrmMain.dni.value);
	if( document.getElementById("direccion") ) document.FrmMain.direccion.value = Limpiar(document.FrmMain.direccion.value);
	if( document.getElementById("email") ) document.FrmMain.email.value = Limpiar(document.FrmMain.email.value);
	if( document.getElementById("email3") ) document.FrmMain.email3.value = Limpiar(document.FrmMain.email3.value);
	if( document.getElementById("voluntariado") ) document.FrmMain.voluntariado.value = Limpiar(document.FrmMain.voluntariado.value);
	if( document.getElementById("otros") ) document.FrmMain.otros.value = Limpiar(document.FrmMain.otros.value);
	if( document.getElementById("distincion") ) document.FrmMain.distincion.value = Limpiar(document.FrmMain.distincion.value);
	if( document.getElementById("estudios") ) document.FrmMain.estudios.value = Limpiar(document.FrmMain.estudios.value);
	if( document.getElementById("padre1") ) document.FrmMain.padre1.value = Limpiar(document.FrmMain.padre1.value);
	if( document.getElementById("graduado1") ) document.FrmMain.graduado1.value  = Limpiar(document.FrmMain.graduado1.value);
	if( document.getElementById("padre2") ) document.FrmMain.padre2.value  = Limpiar(document.FrmMain.padre2.value);
	if( document.getElementById("padre_dni") ) document.FrmMain.padre_dni.value  = Limpiar(document.FrmMain.padre_dni.value);
	if( document.getElementById("madre1") ) document.FrmMain.madre1.value = Limpiar(document.FrmMain.madre1.value);
	if( document.getElementById("graduado2") ) document.FrmMain.graduado2.value = Limpiar(document.FrmMain.graduado2.value);
	if( document.getElementById("madre2") ) document.FrmMain.madre2.value = Limpiar(document.FrmMain.madre2.value);
	if( document.getElementById("madre_dni") ) document.FrmMain.madre_dni.value = Limpiar(document.FrmMain.madre_dni.value);
	if( document.getElementById("razon") ) document.FrmMain.razon.value = Limpiar(document.FrmMain.razon.value);
	if( document.getElementById("argumento") ) document.FrmMain.argumento.value = Limpiar(document.FrmMain.argumento.value);
	
	//cantadocument.FrmMain.argumento.value.length
	/*Trim(document.FrmMain.paterno.value).length == 0
	if(document.FrmMain.email2.value.length == 0 ) {
		alert("Debe ingresar verificación de email");
		return;
	}else{
		if (!(validarEmail(document.FrmMain.email2.value))){
			//alert("no contiene @ ")
			return;
		}
	}
	//verificacion igualdad
	if (!( Trim(document.FrmMain.email.value) == Trim(document.FrmMain.email2.value) )){
			alert("Los correos ingresados no son iguales");
			return;
	}*/
	/*
	//contraseña
	if(Trim(document.FrmMain.password.value).length < 6 ) {
		alert("Debe ingresar el password");
		return;
	}
	if (!( Trim(document.FrmMain.password.value) == Trim(document.FrmMain.password2.value) )){
			alert("las contraseñas ingresadas no son iguales");
			return;
	}*/
	/*if (!(validarEmail(document.FrmMain.email3.value))){
			//alert("no contiene @ ")
			return;
	}*/
	//setear colegio
	if( document.getElementById("cole") ) document.FrmMain.cole.value = document.FrmMain.colegio.value;
	if( document.getElementById("cole1") ) document.FrmMain.cole1.value = document.FrmMain.colegio1.value;
	if( document.getElementById("cole2") ) document.FrmMain.cole2.value = document.FrmMain.colegio2.value;
	if( document.getElementById("cole3") ) document.FrmMain.cole3.value = document.FrmMain.colegio3.value;
	//alert(document.FrmMain.colegio.value);
	//document.FrmMain.name_com.value=document.FrmMain.paterno.value+", "+document.FrmMain.materno.value+", "+document.FrmMain.nombre.value;
	//document.FrmMain.fecha.value=document.FrmMain.dia.value+"/"+document.FrmMain.mes.value+"/"+document.FrmMain.ano.value;
	//hacer el envio
	document.FrmMain.submit();
}

//contador de caracteres
function textCounter(field, countfield, maxlimit) {
	//alert(field.value.length);
	if (field.value.length > maxlimit) {// if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	}else{
	//countfield.value = maxlimit - field.value.length;
	countfield.value = field.value.length;
	}
}
//function Limpiar
function Limpiar(cadena){
	var cant =cadena.length;
	var caracter="";
	for (i=0;i<= cant-1;i++){
		caracter=cadena.charAt(i);
		if(caracter == "'"){
			//alert("es igual");
			cadena = cadena.substring(0,i)+" "+cadena.substring(i+1,cant-1);
			//alert(cadena);
		}	
	}
	return cadena;
}

//MAXIMO CARATERES EN UN TXTAREA
function max(txarea)
{
    total = 500;
    tam = txarea.value.length;
    str="";
    str=str+tam;
    //Digitado.innerHTML = str;
   // Restante.innerHTML = total - str;

    if (tam > total){
        aux = txarea.value;
        txarea.value = aux.substring(0,total);
        //Digitado.innerHTML = total
        //Restante.innerHTML = 0
    }
} 
//FUNCION VALIDA PASSWORD
function ValidaPsw(){
	if(Trim(document.FrmMain.anterior.value).length == 0 ) {
		alert("Debe ingresar la anterior contraseña");
		return;
	}
	if(Trim(document.FrmMain.nueva.value).length < 6 ) {
		alert("Debe ingresar una nueva contraseña mayor a 6 caracteres");
		return;
	}
	if(Trim(document.FrmMain.rep_nueva.value).length < 6 ) {
		alert("Debe ingresar una repeticion de contraseña mayor a 6 caracteres");
		return;
	}
	if (!( Trim(document.FrmMain.nueva.value) == Trim(document.FrmMain.rep_nueva.value) )){
			alert("Las contraseñas ingresadas no son iguales");
			return;
	}
	document.FrmMain.submit();
}

//FUNCION PRINCIPAL CAMBIAR NIVEL
function cambiar(){
//var index=document.forms.formulario.nivel.selectedIndex;
var index=document.forms.FrmMain.nivel.selectedIndex;
FrmMain.periodo.length=0;

if(index==0) trimestre0();
if(index==1) trimestre1();
if(index==2) trimestre2();

}

function trimestre0(){
opcion0=new Option("Se debe elegir primero un nivel","00","defauldSelected");

document.forms.FrmMain.periodo.options[0]=opcion0;

}

function trimestre1(){
opcion0=new Option("Elegir Semestre","00","defauldSelected");
opcion1=new Option("Marzo-Julio","01");
opcion2=new Option("Agosto-Diciembre","02");

document.forms.FrmMain.periodo.options[0]=opcion0;
document.forms.FrmMain.periodo.options[1]=opcion1;
document.forms.FrmMain.periodo.options[2]=opcion2;
}

function trimestre2(){
opcion0=new Option("Elegir Semestre","00","defauldSelected");
opcion1=new Option("Enero-Abril","03");
opcion2=new Option("Mayo-Agosto","04");
opcion3=new Option("Setiembre-Diciembre","05");

document.forms.FrmMain.periodo.options[0]=opcion0;
document.forms.FrmMain.periodo.options[1]=opcion1;
document.forms.FrmMain.periodo.options[2]=opcion2;
document.forms.FrmMain.periodo.options[3]=opcion3;
}