//ARCHIVO DE FUNCIONES JS

//INFORMA SI HAY ERRORES DE JAVASCRIPT
//window.onerror=function(strDesc,strUrl,intLinea)
//{
//	var strError = "SIMR\nHa Ocurrido un error de JavaScript \n" + "\nDescripción: " + strDesc + "\nUrl: " + strUrl + "\nLinea: "+ intLinea;
//	alert(strError);
//}

function Fijar_Fecha(url, campo){
//***********************************************************************
//* DESCRIPCION: funcion que valida si el campo esta habilitado para
//* mostrar el calendario
//* CONDICIONES: la url de destino y el campo a validar
//* RETORNA: abre una ventana con el calendario
//* CAMBIOS: ninguno.
//***********************************************************************
	if(campo.disabled)
		alert('Este campo no se puede modificar');
	else
		Abrir_Calendario(url);
}


function Abrir_Calendario(url){
//***********************************************************************
//* DESCRIPCION: funcion similar a la funcion que Fijar_Fecha, con la
//* diferencia que no se valida si el campo donde ira el valor
//* CONDICIONES: la url de destino
//* RETORNA: abre una ventana con el calendario
//* CAMBIOS: ninguno.
//***********************************************************************

	window.open(url, 'cal', 'status=1,width=225,height=225,left=270,top=180');
}


function ActualizarSubnivel() {
//***********************************************************************
//* DESCRIPCION: ESTA FUNCION ACTUALIZA LOS SUBNIVELES DE ACUERDO AL NIVEL SELECCIONADO
//* CONDICIONES:
//* RETORNA:
//* CAMBIOS: ninguno.
//***********************************************************************
	var Nivel = eval("form2.area");
	var Subnivel = eval("form2.subarea");

	//BORRA OPTIONS ACTUALES
	while(Subnivel.options.length!=0)
		Subnivel.options[0]=null;

	Subnivel.options[Subnivel.options.length]=new Option('Seleccione...','0');
	//RECORRE EL ARREGLO DE Aplicaciones
	for (i=0;i<arrSubniveles.length;i++)
		if (Nivel.value == arrSubniveles[i][2])
			Subnivel.options[Subnivel.options.length]=new Option(arrSubniveles[i][1], arrSubniveles[i][0]);


}

function ValidarBusquedaAvanzada() {
//***********************************************************************
//* DESCRIPCION: VALIDA LOS CAMPOS DE LA FORMA Y LA ENVIA
//* CONDICIONES:
//* RETORNA:
//* CAMBIOS: ninguno.
//***********************************************************************

form2.submit()
}
function ValidarBusquedaRapida() {
//***********************************************************************
//* DESCRIPCION: VALIDA LOS CAMPOS DE LA FORMA Y LA ENVIA
//* CONDICIONES:
//* RETORNA:
//* CAMBIOS: ninguno.
//***********************************************************************
	var str_error = "";
	if (!cadena_valida(form1.palabra.value))
		str_error += "Por favor ponga cualquier criterio de busqueda en la casilla."
	//if (!cadena_valida(form1.email.value))
		//str_error += "\n ·  Debe colocar su Email"
	if (str_error != "")
		confirm('Se encontraron los siguientes errores en el formulario\n'+str_error)
	else
		form1.submit()
}

function  ValidarRegistrese() {
//***********************************************************************
//* DESCRIPCION: VALIDA LOS CAMPOS DE LA FORMA Y LA ENVIA
//* CONDICIONES:
//* RETORNA:
//* CAMBIOS: ninguno.
//***********************************************************************
	var str_error = "";
	if (isEmail(form.mail.value) == false)
		str_error +=  "Favor ingresar una direccion de correo electronico valida."
	//if (!cadena_valida(form1.email.value))
		//str_error += "\n ·  Debe colocar su Email"
	if (str_error != "")
		confirm('Se encontraron los siguientes errores en el formulario\n'+str_error)
	else
		form.submit()
}

function  Validarformularioregistrese() {
//***********************************************************************
//* DESCRIPCION: VALIDA LOS CAMPOS DE LA FORMA Y LA ENVIA
//* CONDICIONES:
//* RETORNA:
//* CAMBIOS: ninguno.
//***********************************************************************
	var str_error = "";
	if (!cadena_valida(form2.nombre.value))
		str_error += "\n- Debe colocar su Nombre"
	if (!cadena_valida(form2.ciudad.value))
		str_error += "\n- Debe colocar su Ciudad de residencia"
	if (!cadena_valida(form2.telefono.value))
		str_error += "\n- Debe colocar su Telefono"
	if (!cadena_valida(form2.celular.value))
		str_error += "\n- Debe colocar su celular"
	if (!cadena_valida(form2.profesion.value))
		str_error += "\n- Debe colocar su profesion"
	if (!cadena_valida(form2.mail.value))
		str_error +=  "\n- Favor ingresar una direccion de correo electronico laboral."		
	else
		if (isEmail(form2.mail.value) == false){
			str_error +=  "\n- Favor ingresar una direccion de correo electronico laboral valida."
		}
	//if (cadena_valida(form2.mail2.value))
	//	str_error +=  "\n- Favor ingresar una direccion de correo electronico personal."
	//else
		if (isEmail(form2.mail2.value) == false){
			str_error +=  "\n- Favor ingresar una direccion de correo electronico personal valida."
		}
	if (str_error != "")
		confirm('Se encontraron los siguientes errores en el formulario\n'+str_error)
	else
		form2.submit()
}

function validar_contactenos() {
//***********************************************************************
//* DESCRIPCION: Valida los campos obligatorios en el formulario
//* CONDICIONES:
//* RETORNA: true or false
//* CAMBIOS: ninguno.
//***********************************************************************
	var str_error = "";
	if (!cadena_valida(form2.nombre.value))
		str_error += "\n- Por favor coloque su nombre."
	if (!cadena_valida(form2.apellido.value))
		str_error += "\n- Por favor coloque su apellido."
	if (!cadena_valida(form2.email.value))
		str_error += "\n-  Por favor coloque su Email"
	if (isEmail(form2.email.value) == false)
		str_error +=  "\n- Por favor ingresar una dirección de correo electrónico valida."
	if (form2.temainteres.value == -1)
		str_error +=  "\n- Por favor seleccione un tema de interés."
	if (str_error != "")
		confirm('Se encontraron los siguientes errores en el formulario\n'+str_error)
	else
		form2.submit()
}

function cadena_valida(s){
//***********************************************************************
//* DESCRIPCION: Valida que s no sea vacia
//* CONDICIONES:
//* RETORNA:
//* CAMBIOS: ninguno.
//***********************************************************************
	var i;
	sw = 0;
	for (i = 0; i < s.length; i++){
        	// Revisa que alguno de los caracteres al menos no sea un espacio
        	var c = s.charAt(i);
        	if (c != " ")
        		sw = 1
    	}
    	if (sw==0)
    		return false;
    	else
    		return true;
}
function isEmail(email) {
//***********************************************************************
//* DESCRIPCION: Valida que 'email' sea una dirección de correo valida
//* CONDICIONES:
//* RETORNA: true or false
//* CAMBIOS: ninguno.
//***********************************************************************

		// valid format "a@b.cd"
		invalidChars = " /;,:";
		if (email == "")
			return false;

		for (i=0; i< invalidChars.length; i++)
		{
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) > -1)
			{
				return false;
			}
		}

		atPos = email.indexOf("@",1)
		// there must be one "@" symbol
		if (atPos == -1)
			return false;

		// and only one "@" symbol
		if (email.indexOf("@", atPos+1) != -1)
			return false;

		// and at least one "." after the "@"
		periodPos = email.indexOf(".",atPos)
		if(periodPos == -1)
			return false;

		// and at least one character between "@" and "."
		if ( atPos +2 > periodPos)
			return false;

		if ( periodPos +3 > email.length)
			return false;

		return true;
}

function Abrir_Ventana(url, ancho, alto){
//***********************************************************************
//* DESCRIPCION: funcion que abre una ventana nueva
//* CONDICIONES: la url de destino, el ancho w y el alto h
//* RETORNA: abre una ventana nueva
//* CAMBIOS: ninguno.
//***********************************************************************

	var numero = Math.round((Math.random()*100));

	posL = (screen.width) ? (screen.width - ancho) / 2 : 0;
	posT = (screen.height) ? (screen.height - alto) / 2 : 0;

	a = window.open(url,numero,"height=" + alto + ",width="+ ancho + ",top=" + posT + ",left=" + posL + ",toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1");
}

