/*
property: 
rel_aux

valorg_aux

textbox_aux

caracteres_lista

extensiones

ext_valida

mensaje_conf_1

mensaje_conf_2

url_destino

*/

rel_aux = new Array()
valorg_aux = new Array()
var textbox_aux = "";
var caracteres_lista = "0";
var extensiones = ".gif;.jpg;.png;.pdf;.zip;.doc;.xls;.rtf";
var ext_valida = false;
var mensaje_conf_1 = "confirmacion 1";
var mensaje_conf_2 = "confirmacion 2";
var url_destino = null;

/*
Function:  menu_item
*Utilidad:* establece los valores de los inputs de navegacion del framework 
del portal, que son: obj_x,par_x,modo_x,accion_x,pag_x
	
*Parametros:* 
- form_x: establece el formulario utilizado en la pagina 
- obj_x: input de navegacion del framework
- par_x: input de navegacion del framework
- modo_x: input de navegacion del framework
- accion_x: input de navegacion del framework
- pag_x: input de navegacion del framework

*Valor(es) retonardo(s):* no aplica.
*/

function menu_item(form_x,obj_x,par_x,modo_x,accion_x,pag_x){
	if (typeof(form_x)=="object")
		f = form_x
	else
		if (form_x=="" || form_x == null)
			f = document.forms[0]
		else
			f = document.forms[form_x]
		// end if
	// end if
	f.cfg_objeto_aux.value = obj_x
	f.cfg_parametro_aux.value = par_x
	f.cfg_modo_aux.value = modo_x
	f.cfg_accion_aux.value = accion_x
	f.cfg_pagina_aux.value = pag_x
	//f.submit()
}// end function

/*
Function:  get_form
*Utilidad:* se encarga de poner a disposicion el formulario de la pagina
	
*Parametros:* 
- form_x: establece el nombre formulario utilizado en la pagina. La funcion valida si es tipo
objeto o es el nombre dentro del DOM o si esta vacio le asiga el formulario pore defecto
que este en la pagina  

*Valor(es) el objeto "form" de la pagina.
*/

function get_form(form_x){
	if (typeof(form_x)=="object")
		if (form_x.type=="form"){
			return form_x
		}else{
			return form_x.form
		}// end if
	else
		if (form_x=="" || form_x == null)
			return document.forms[0]
		else
			return document.forms[form_x]
		//end if
	//end if
}// end function

function eval_pagina(control_x,pagina_y){
	switch(parseInt(control_x.value,10)){
	case -4:
		control_x.value = pagina_y - 1
		if(control_x.value==0){
			control_x.value = 1
		}// end if
		break
	case -2:
		control_x.value = pagina_y + 1
		break
	}// end switch	
}// end if

function menu(form_x,para_x,pre_x,post_x,volver_x){
	if (para_x=="" || para_x == null)
		return false
	// end if
	f = get_form(form_x)
	if (pre_x == null)
		pre_x = ""
	if (post_x == null)
		post_x = ""
	var_x = para_x.split(";")
	pagina_x = parseInt(f.cfg_pagina_aux.value,10)
	pagina_z = parseInt(f.cfg_pag_form_aux.value,10)
	for (i=0;i<var_x.length;i++){
		var2_x = var_x[i].split(":")
		if (var2_x[0]!= null && var2_x[0]!= "" && var2_x[1]!= "-1"){
			cad_x = "f."+pre_x+var2_x[0]+post_x+".value='"+var2_x[1]+"'"	
			eval(cad_x)
		}// end if
	}// next
	eval_pagina(f.cfg_pagina_aux,pagina_x)
	eval_pagina(f.cfg_pag_form_aux,pagina_z)
	if(f.cfg_sw_aux.value==f.cfg_sw2_aux.value){
		if (f.cfg_sw_aux.value!="1"){
			f.cfg_sw_aux.value = "1"
		}else{
			f.cfg_sw_aux.value = "0"
		}// end if
	}// end if
	return true
}// end function

function menu_aux(para_x,form_x,aux_x,obj_x,text,modo){
	var m;
	var c;
	var obj = obj_x;
	var campo_texto = (text==null)?(obj):(text);		
	x = get_form(form_x)
	if(aux_x=="1"){
		m = validar_form(x)
	}else if(aux_x=="2"){
		processData()
		m = validar_form(x)
	}else if(aux_x=="3"){
		m = validar_form(x)
		if(m == true){
			m = confirmaciones();
		}
	}else if(aux_x=="4"){
		var campo = x.elements[obj].value
		var cad = new String(campo);
		//hora = cad.substr(0,2);
		if(!this.obligatorio(campo) || campo == ' ' || cad.substr(0,2) == '  '){
			alert('El campo '+campo_texto+' es obligatorio');
			m = false;
		}else{
			m = validar_form(x);
		}//end if
	}else if(aux_x=="5"){
		m = validar_form(x)
		if(m)
			m = validacion_especial(obj,text,modo);
	}else if(aux_x=="6"){
		m = validacion_especial(obj,text,modo);
		if(m)
			m = validar_form(x)
	}else{
		m = true;
	}//end if
	if(m == true){
		menu(form_x,para_x,'cfg_','_aux')
		f.submit()
	}//end if
}// end function

function validacion_especial(campo,msj_x,modo){
	x = get_form('formulario')
	var campo_x = x.elements[campo].value
	if(modo == "1"){
		m = val_rangos(campo,campo_x)
		if(m==false){
			alert(aux["mostrar"])
			aux["mostrar"] = "";
			return false;
		}
	}
	return true;
}//enn function

function obligatorio(valor_x){	re = /.+/	var matchArray = re.exec(valor_x)	//alert (matchArray)
	if (matchArray){		return true	}// end if
	return false}// end function

function boton_aux(para_x,form_x,valid_x,volver_x){
	//validar_reg() ||
	if (valid_x && (!val.ejecutar(get_form(form_x))))
		return false
	return menu(form_x,para_x,'cfg_','_aux',volver_x)
}// end function

function alert_m(m){
	if (m!=null && m!=""){
		return (true);
	}//end if
	return (false);
}// end if

function abrir_vinculo(enlace_x,target,prop_x){
	url_destino = enlace_x;
	if (target!="_self" && target != "" && target != null) {
  			if(prop_x==""){
				window.open(enlace_x,target,prop_x)
			}else{
				window.open(enlace_x,target)			
			}//end if
	}else{
		document.location.href = enlace_x
	}// end if
}// end function

// funciones de controles
function seleccionar_lista(control_x,nombre_x){
	n_ele = control_x.length
	sel = ""
	for(i=0;i<n_ele;i++){
		if(control_x.options[i].selected){
			sel += ((sel!="")?",":"")+control_x.options[i].text
		}// end if
	}// next
	control_x.form.elements[nombre_x].value = sel
}// end if

function seleccionar_todo(control_x,nombre_x){
	f = control_x.form
	n_ele = f.elements.length
	sel = ""
	valor_x = control_x.checked
	for(i=0;i<n_ele;i++){
		ele_x = f.elements[i]
		tipo_x = ele_x.type
		nombre = ele_x.name
		if (tipo_x=="checkbox" && nombre.substr(0,nombre_x.length) == nombre_x ){
			ele_x.checked = valor_x
		}// end if
	}// next
	seleccionar_set(control_x,nombre_x)
}// end if

function seleccionar_set(control_x,nombre_x){
	f = control_x.form
	n_ele = f.elements.length
	sel = ""
	for(i=0;i<n_ele;i++){
		ele_x = f.elements[i]
		tipo_x = ele_x.type
		nombre = ele_x.name
		if (tipo_x=="checkbox" && nombre.substr(0,nombre_x.length) == nombre_x ){
			if(ele_x.checked){
				sel += ((sel!="")?";":"")+ele_x.value
			}// end if
		}// end if
	}// next
	f.elements[nombre_x].value = sel
}// end function

function validar_reg(control_x){
	f = get_form(control_x)
	ele_x = f.elements["cfg_reg_aux"]
	if(ele_x.value != null && ele_x.value != ""){
		return (true)
	}else{
		return (false)
	}// end if
}// end if
// fin funciones de controles

function cambiar_item(control_x,direccion){
	if (control_x.length <=1){
		return false
	}// end if
	indice = control_x.selectedIndex
	f = control_x.form
	valor_x = control_x.value 
	texto_x = control_x.options[control_x.selectedIndex].text
	if(direccion == 's'){
		if (indice >0){
			dir = -1
		}else{
			return false
		}// end if
	}else{
		if ((indice+1)<(control_x.length)){
			dir = 1
		}else{
			return false
		}// end if
	}// end if
	control_x.options[control_x.selectedIndex].value = control_x.options[control_x.selectedIndex+dir].value
	control_x.options[control_x.selectedIndex].text = control_x.options[control_x.selectedIndex+dir].text
	control_x.options[control_x.selectedIndex+dir].value = valor_x
	control_x.options[control_x.selectedIndex+dir].text = texto_x
	control_x.selectedIndex = indice + dir
}// end function

function copiar_item (control_x,control_y,tipo_x){
	if (control_x.length <1){
		return false
	}// end if
	indice = control_x.selectedIndex
	l = control_x.length
	if (indice<0){
		return false
	}// end if
	f = control_x.form
	var opcion = document.createElement("OPTION")
	valor_x = control_x.value
	texto_x = control_x.options[control_x.selectedIndex].text
	opcion.value = valor_x
	opcion.text = texto_x
	control_y.options.add(opcion)
	if (tipo_x=="u"){
		control_x.options[control_x.selectedIndex] = null
		if ((indice+1)==l){
			control_x.selectedIndex = indice -1
		}else{
			control_x.selectedIndex = indice
		}// end if
	}// end if
}// end function

function eliminar_item (control_x,todo){
	if (control_x.length <1){
		return false
	}// end if
	l = control_x.length
	indice = control_x.selectedIndex
	control_x.options[indice] = null
	if ((indice+1)==l){
		control_x.selectedIndex = indice -1
	}else{
		control_x.selectedIndex = indice
	}// end if
	
}// end function

function sel_item(control_x,control_y){

	l = control_x.length
	aux = ""
	for(i=0;i<l;i++){
		aux += ((aux!="")?";":"")+control_x.options[i].value
	}// next
	control_y.value = aux
}// end function

function ajustar_grid(id_x){
	if(ele_x = document.getElementById(id_x)){
		if (navigator.appName.substring(0,4)=="Micr"){
			//alert(ele_x.id+' '+ele_x.offsetWidth+' '+ele_x.offsetHeight)
			if(ele_x.offsetWidth>466){
				ele_x.style.overflowX="auto"
				ele_x.style.height = ele_x.offsetHeight+20
			}// end if
		}else{
			ele_x.style.overflow="auto"
		}// end if
	}// end if
}// end function

function actualizar_list(f,lista_org,par_ini){
	if (par_ini == null){
		//alert ("es nulo")
		par_ini = 0
	}// end if
	var m_lista = new Array()
	var campo_nombre = new Array()
	var campo_pos = new Array()
	var campo_padre = new Array()
	var padre_ant = new Array()
	k = 0
	n_elementos = f.elements.length
	for (i=0;i<=n_elementos-1;i++){
		e = f.elements[i]	
		relacion_aux = rel_aux[e.name]
		if(e.type == "select-one" && relacion_aux !="" && relacion_aux != null){
			k = k + 1
			m_lista[k] = e
		}// end if
	}// next
	padre_aux = lista_org.name
	salir = "n"
	n_lista = k
	k = 0
	m = 0
	while (salir == "n" && n_lista > 0){
		salir = "s"
		band = 0
		for (i=1;i<=n_lista;i++){
			relacion_aux = rel_aux[m_lista[i].name]
			aux = relacion_aux.split(":")
			if(aux[0] == padre_aux ){			
				salir = "n"
				k = k + 1
				campo_nombre[k] = m_lista[i]
				campo_pos[k] = aux[1]
				campo_padre[k] = padre_aux
				if (band==0){
					m++
					band++
					padre_ant[m] = m_lista[i].name
				}// end if
			}// end if
		}// next
		if (k>0 && m>0){
			padre_aux = padre_ant[m]
		}// end if
	}// end while
	for (m=1;m<=k;m++){
		llenar_lista(f,campo_nombre[m],campo_pos[m],campo_padre[m],par_ini)
	}// next
}// end function

function llenar_lista(f,lista_x,pos_x,valor_padre,valor_y){
	nombre_campo = lista_x.name
	lista_x.length = 0
	encontrado_x = 0
	eval("matriz="+nombre_campo+"_xyz")
	var opcion = document.createElement("OPTION")
	opcion.value = '';
	opcion.text = 'Seleccionar';
	opcion.title = '';
	lista_x.options.add(opcion)
	for (i=1;i<matriz.length;i++){
		campo_aux = matriz[i].split(":")
			if (campo_aux[pos_x-1] == f.elements[valor_padre].value){			
				if(textbox_aux=="" || textbox_aux==null){
						var opcion = document.createElement("OPTION")
						valor_x = campo_aux[0]
						texto_x = campo_aux[1]
						opcion.value = valor_x;
						
						if(caracteres_lista == '0'){
							opcion.text = texto_x;
						}else{
						var cad = new String(texto_x);
						//caracteres_lista
							var mi_texto = cad.substr(0,50);
							opcion.text = mi_texto
						}//end if
						opcion.title = texto_x;
						lista_x.options.add(opcion)
				}else{
					f.elements[textbox_aux].value = campo_aux[0];
					f.elements[textbox_aux+'_aux'].value = campo_aux[0];				
				}
			}else{
				/*var opcion = document.createElement("OPTION");
				opcion.value = '';
				opcion.text = 'Ninguno x';
				lista_x.options.add(opcion)
				i = matriz.length;*/
			}// end if
	}// next
	valorg_x = valorg_aux[lista_x.name]
	if (valor_y == 1 && valorg_x !="" && valorg_x != null){
		lista_x.value = valorg_x
	}// end if
}// end function
function ini_list(){
	n_form = document.forms.length
	for (ifomr=0;ifomr<n_form;ifomr++){
		f = document.forms[ifomr]
		n_ele = f.elements.length
		for (iele=0;iele<n_ele;iele++){
			control_s = f.elements[iele]
			if("".concat(control_s.onchange).indexOf("actualizar_list")>=0){
				actualizar_list(f,control_s,1)
			}// end if 
		}// next
	}// next
}// end function

function ordenar_set(nombre_x,form_x){
	var formulario = document.forms[form_x]
	n_elementos = formulario.elements.length
	valor_x = ""
	formulario.elements[nombre_x].value = "" 
	for (i=0;i<=n_elementos-1;i++){
		tipo = formulario.elements[i].type
		nombre_elem = formulario.elements[i].name 
		if (tipo == "checkbox" && formulario.elements[i].checked && nombre_elem.substring(0,nombre_x.length)==nombre_x){
			valor_x += formulario.elements[i].value+","
		}// end if
	}// next
	valor_x = valor_x.substring(0,valor_x.length-1) 
	formulario.elements[nombre_x].value = valor_x
}// end if

function crear_hora(valor,objeto,obj_x){	
	var hora;
	var minutos="";
	var cero = "0";
	var linea = new String(objeto);
	var lista = linea.split("_");
	var prueba = 'time_'+lista[1];
	f = get_form("formulario")
	ele_x = f.elements[objeto];
	ele_x2 = f.elements[prueba];
	var tiempo_h = new String(ele_x2.value);
	hora = tiempo_h.substr(0,2);
	var tiempo_m = new String(ele_x2.value);
	minutos = tiempo_m.substr((tiempo_m.length-2),2);
	if(obj_x == 2){
		if(hora == ""){
			alert ('Debe Seleccionar Primero la Hora')
			formato = f.elements[objeto];
			formato.value = "am";
			return false;
		}//end if
		if(ele_x.value == 2){
			if(hora<12){
				if(hora<10){
					h = new String(hora);
					h2 = h.substr((h.length-1),1);	
					hora = parseInt(h2) + 12;			
				}else{
					hora = parseInt(hora)+12;
				}//end if
				if(hora == "12"){
					hora = "12";
				}// end if				
			}// end if
			ele_x2.value =  hora+':'+minutos
		}else{
			if(hora>12){
				hora = parseInt(hora)-12
			}// end if
			if(hora==12){
				hora = "00";
			}// end if
			if(hora>=10 || ele_x.value != 2)
				cero = ""
			ele_x2.value =  cero+hora+':'+minutos
		}// end if
		return false
	}// end if
	if(obj_x == 1){
		minutos = ele_x.value
		if(hora == ""){
			alert ('Debe Seleccionar Primero una Hora')
			formato = f.elements[objeto];
			formato.value = "";
			return false;
		}//end if
		ele_x2.value = hora+':'+minutos
	}else{
		hora = ele_x.value
		if(minutos == "")
			minutos = "00";
 		ele_x2.value = hora+':'+minutos
	}// end if
}//end function

function crear_fecha(valor,objeto,obj_x){
	f = get_form("formulario")
	var nom_obj = f.elements[objeto].name
	var nombre = new String(nom_obj);
	//n = nombre.substr((nombre.length-5),5)
	var n = nombre.split("_");
	var nombre2 = new String(n);
	n2 = nombre.substr((nombre.length-5),1)
	var d = "cal_dias_"+n[2]+"_aux";	
	var m = "cal_meses_"+n[2]+"_aux";
	var a = "cal_annios_"+n[2]+"_aux";
	var cal = "cal_fecha_"+n[2];

	var dia = f.elements[d].value;
	var mes = f.elements[m].value;
	var annio = f.elements[a].value;

	/*if(dia >= "28"){
		bi = retornar_dias(mes,annio)
		dia = bi;
		f.elements[d].value = bi;
	}*/

	if(dia >= "28" && mes == '02'){
		bi = retornar_dias(mes,annio)
		if(dia=='28' && bi == '29'){	
			f.elements[d].value = dia;
		}else{
			dia = bi;		
			f.elements[d].value = bi;	
		}		
	}

	if(dia > 30){
		if(mes == '04'){
			f.elements[d].value = dia = retornar_dias(mes,annio);
		}
		if(mes == '06'){
			f.elements[d].value = dia = retornar_dias(mes,annio);
		}
		if(mes == '09'){
			f.elements[d].value = dia = retornar_dias(mes,annio);
		}
		if(mes == '11'){
			f.elements[d].value = dia = retornar_dias(mes,annio);
		}
	}
	
	
	var fec_cal = f.elements[cal];
	//alert(fec_cal)
	//var f.elements[cal].value = annio+"-"+mes+"-"+dia;
	fec_cal.value = annio+"-"+mes+"-"+dia;
	//alert(annio+"-"+mes+"-"+dia);
}//end function

function retornar_dias(mes_x,annio_x){
	switch(mes_x){
		//''''''''''''''''''''''''''''''
		case "01":
		case "03":
		case "05":
		case "07":
		case "08":
		case "10":
		case "12":
			dia = 31;
			return dia;
		break;
		//''''''''''''''''''''''''''''''
		case "04":
		case "06":
		case "09":
		case "11":
			dia = 30;
			return dia;
		break;
		//'''''''''''''''''''''''''''''''
		case "02":
			bi = bisiesto(annio_x);
			return bi;
	}//end switch
}//end fucntion

function bisiesto(annio_x){
	var dias = 0;
	if(((annio_x%4)==0) || (annio_x%400==0)){
		dias = 29;
	}else{
		dias = 28;
	}//end if
	return dias
}//end function

function obtener_ruta(nombre_x,text_x){
	var var_x;
	var_x = extensiones.split(";")
	f = get_form("formulario")
	var text = f.elements[text_x];
	ele_x = f.elements[nombre_x];
	valor_x = text.value
	var cadena = new String(valor_x);
	var lista = cadena.substr((cadena.length-4),4);
	for (i=0;i<var_x.length;i++){
		if(var_x[i] == lista || ext_valida ==true){
			ext_valida = true;
		}else{
			ext_valida = false;
		}//end if
	}// next
	if(ext_valida==true){
		ele_x.value = valor_x
		ext_valida = false;
	}else{
		alert('Tipo de Archivo no permitido')
		formulario.text.value = ''
		ele_x.value = ""
		return false;
	}//end if
}//end function

function confirmar(valor_x,cual){
	var cadena = valor_x
	var inte = "¿Desea Eliminar el Registro?";
	if (confirm(inte)){
			if (confirm("¿Está Seguro que Desea Eliminar el Registro?")){
				menu_aux(cadena);
			}else{
				return false;
			}//end if
	}else{
		return false;
	}//end if
}//end function

function ir_a(cadena,pag){
	var cad = "";
	cad = cadena+"pagina:"+pag
	menu_aux(cad);
}//end function

function iniciar_editor(modo){
	if(gecko_ok == 1){
		document.getElementById('edit').contentWindow.document.designMode = "on";
		try{
			document.getElementById('edit').contentWindow.document.execCommand("undo", false, null);
		}catch(e){
			gecko_ok = -1;
		}//end try
	}//end if
	if(modo==1)
		cargar_menu();
}//end function

function abrir_pop_up(URL,destino,prop_x) {
	window.open(URL,destino,prop_x);
}//end function

function confirmaciones(){
	if (confirm(mensaje_conf_1)){
		if(mensaje_conf_2 != "confirmacion 2"){
			if(confirm(mensaje_conf_2)){
				return true;
			}else{
				return false;
			}//end if
		}else{
			return true;		
		}//end if
	}else{
		return false;
	}//end if
}//end fucntion

function validar_rango_fechas(fecha1,fecha2){	
	alert(fecha1);
	alert(fecha2);
	if(fecha1>fecha2){
	alert("error");	
	}else{
	alert("no hay error");
	}//end if	
}//end fucntion

function enviar_foco(ev,obj_n){				
	if(ev==13){
		f = get_form("formulario");
		var objeto_x = f.elements[obj_n]
		objeto_x.focus();
	}
}

function enviar_formulario_enter(ev,mm_aux,modo){
	if(ev==13){
		if(modo==''){
			n = 1;
		}else{
			n = modo;		
		}
		f = window.document.formulario;
		m = menu_aux(mm_aux,'',n);
		if(m){	
			f.submit();
		}else{
			return false;		
		}
	}
}

function asignar_telefono(obj_n,ind_x,modo){
	f = get_form("formulario");
	var obj_w = f.elements['cod_tlf_'+ind_x+'_aux'];
	var obj_x = f.elements[obj_n];
	var obj_y = f.elements['tlf_'+ind_x];
	//alert()
	if(obj_x.value!=''){
		modo = 0;	
	}
	if((isNaN(obj_x.value) || (obj_x.value.length) < 7) && modo != 1){
		alert('Debe ingresar un número telefónico válido');
		obj_y.value = '';
		obj_x.focus();
	}else{
		if(obj_w.value == ''){
			alert('Debe elegir un codigo de área ó operadora');
			obj_y.value = '';
			obj_w.focus();
		}else{
			obj_y.value = obj_w.value+obj_x.value;
		}//end if
	}//end if
}//end function
