function AbrirPopUp(url, ancho, alto){

	var n = window.open(url,'','width=' + ancho + ',height=' + alto + ', top=' + ((screen.height - alto) / 2) + ', left=' + ((screen.width - ancho) / 2) + ', scrollbars=yes, toolbar=no, menubar=no, resizable=yes');
	
	return false;
	
}

/* AJAX */
function GetAjax() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

/* OBTENEMOS EL TIME STAMP */
function DimeTime(){
	var d = new Date();
	return d.getTime();
}

/* FUNCION PRINCIAL AJAX */
function Ajax(get){
	var oAjax = GetAjax();
	var url = "ajax.php?" + get + "&nocache=" + DimeTime();
    oAjax.open("GET", url, false);
    oAjax.send(null);

    if((oAjax.readyState == 4) && (oAjax.status == 200)){
        var r = unescape(oAjax.responseText);
		return r;
		
    } 
	else {
		
		/* PROCESOS ALTERNOS */
		
    }
}

function ObtenerDatosCbo(id, accion, cbos){
	
	var get = "accion=" + accion + "&id=" + id;
	
	datos = Ajax(get);

	var dc = cbos.split(',');
	
	
	for(var e = 0; e < dc.length; e++){

		LimpiarCbo(dc[e]);

	}

	if(datos != "NOK"){
		
		cbo_resultado = dc[0];
		
		var l = datos.split('|');
		
		if(l.length > 0){
			
			l.pop();
			
			for(var i = 0; i < l.length; i++){
				
				var d = l[i].split('[:?]');
				
				AgregarDatosCbo(cbo_resultado, unescape(d[1].replace(/\+/g, ' ', ' ')), unescape(d[0].replace(/\+/g, ' ')));
				
			}
		
		}
	}
	else{
		
	}
}

function LimpiarCbo(cbo){
	
	for( q = document.getElementById(cbo).length; q >= 0; q--){
		
		document.getElementById(cbo).options[q]=null;
		
	}	
	AgregarDatosCbo(cbo, '...Seleccione', '0');
}

function AgregarDatosCbo(cbo, valor, id){
	
	var op = new Option(valor, id, false, false);
	document.getElementById(cbo).options[(document.getElementById(cbo).length)] = op;	
	
}

// Reemplaza el document.getElementById() por de
function dge() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

// Valida el formato de la cuenta de correo
function ValidarEmail(email) {
	if((email.length > 0) && (email != "")){
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
			return (true)
		}else{
			return (false);
		}
	}else{
		return false;	
	}
}

function ValidarUrl(url) {
	if((url.length > 0) && (url != "")){
		if (/^\w+([\.-]?\w+)*.\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(url)){
			return (true)
		}else{
			return (false);
		}
	}else{
		return false;	
	}
}

function ValidarTel(telefono) {
	if((telefono.length > 0) && (telefono != "")){
		if (/^[+]?\d+[-]?\d+$/.test(telefono)){
			return (true)
		}else{
			return (false);
		}
	}else{
		return false;	
	}
}

// Valida el formato de la cuenta de correo
function ValidarFecha(email) {
	if((email.length > 0) && (email != "")){
		if (/^\d{2}\/\d{2}\/\d{4}$/.test(email)){
			return (true)
		}else{
			return (false);
		}
	}else{
		return false;	
	}
}

function ValidarRegistroUsuario(usuario, id){
	
	var get = "accion=vnu&nu=" + usuario + "&id=" + id;
	
	var datos = Ajax(get);
	if(datos == "OK"){
		
		return true;		
		
	}
	else{
		
		return false;
		
	}
	
}

function ValidarRegistroEmail(email, id){
	
	var get = "accion=veu&eu=" + email + "&id=" + id;

	datos = Ajax(get);
	if(datos == "OK"){
		
		return true;		
		
	}
	else{
		
		return false;
		
	}
	
}

function ValidadCodigoSeguridad(codigo){
	
	var get = "accion=captcha&codigo=" + codigo;

	datos = Ajax(get);
	
	if(datos == "OK"){
		
		return true;		
		
	}
	else{
		
		return false;
		
	}
	
}

function ValidarListaCheck(campo){
	
	var estado = false;
	
	for(var i = 1; document.getElementById(campo + i) != null; i++){
		
		if(document.getElementById(campo + i).checked == true){
			
			estado = true;
			
			break;
			
		}
	
	}	
	
	return estado;
	
}

function ValidarCheck(campo){
	
	var estado = false;
	
	if(document.getElementById(campo).checked == true){
		
		estado = true;
		
	}
	
	return estado;
	
}

function ValidarEnvio(){
	var m = "";	
	
	
	if(typeof(tinyMCE) != "undefined"){
		
		if (tinyMCE) tinyMCE.triggerSave(); 
	
	}
	if(ac.length > 0){

		for(var i = 0; i < ac.length; i++){
			
			var datos_v = ac[i].split('|');
			
			var campo = datos_v[0];
			var tipo_validacion = datos_v[1];
			var mensaje_error = datos_v[2];
			var a1 = (datos_v[3] != null) ? datos_v[3] : "";
			var a2 = (datos_v[4] != null) ? datos_v[4] : "";
			var a3 = (datos_v[5] != null) ? datos_v[5] : "";
			var a4 = (datos_v[6] != null) ? datos_v[6] : "";
			var a5 = (datos_v[7] != null) ? datos_v[7] : "";
			var a6 = (datos_v[8] != null) ? datos_v[8] : "";
			var a7 = (datos_v[9] != null) ? datos_v[9] : "";
			var a8 = (datos_v[10] != null) ? datos_v[10] : "";
			var a9 = (datos_v[11] != null) ? datos_v[11] : "";
			var a10 = (datos_v[12] != null) ? datos_v[12] : "";
			
			m += (!ValidarCampo(campo, tipo_validacion, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)) ? (mensaje_error + "\n") : "";
		
		}
		
	}
	
	if(m == ""){
	
		return true;
	
	}
	
	alert(m);
	return false;
}

// Valida los campos
function ValidarCampo(campo, tipo, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 ){
	
	if(document.getElementById(campo) != null){
		
		var e = dge(campo).value;
		
	}
	else{
		
		e = null;
		
	}

	var r = false;
	
	switch(tipo){
		case 'text': //Valida string
			r = ((e.length > 0) && (e != "")) ? true : false;
		break;
		case 'textc': //Valida string con limite de caracteres
			r = ((e.length > 0) && (e.length == a1)) ? true : false;
		break;
		case 'int': //Valida numeros enteros
			r = (e > 0) ? true : false;
		break;
		case 'intc': //Valida numeros enteros con limite de caracteres
			r = ((e > 0) && (e.length == a1)) ? true : false;
		break;
		case 'txtchk': //Valida campo si exite un check
			r = (((e != "") && (document.getElementById(a1).checked == true)) || (document.getElementById(a1).checked == false)) ? true : false;
		break;
		case 'txtitxt': //Valida campo si exite un check
			r = (((e != "") && (e == document.getElementById(a2).value) && (document.getElementById(a1).checked == true)) || (document.getElementById(a1).checked == false)) ? true : false;
		break;
		case 'email': //Valida formato email
			if(a1 == 2){

				r = ((e == "") || (ValidarEmail(e))) ? true : false;
				
			}
			else{
				
				r = (ValidarEmail(e)) ? true : false;
			
			}
			
		break;
		case 'fecha': //Valida formato email
			r = (ValidarFecha(e)) ? true : false;
		break;
		case 'checklist': //Valida lista de checks
			r = (ValidarListaCheck(campo)) ? true : false;
		break;
		case 'check': //Valida lista de checks
			r = (ValidarCheck(campo)) ? true : false;
		break;
		case 'alfanumerico': //Valida lista de checks
			r = ValidarAlfaNumericos(e);
		break;
		case 'alfanumericocant': //Valida lista de checks
			r = ValidarAlfaNumericosCantidad(e, a1);
		break;
		case 'captcha': //Valida lista de checks
			r = (ValidadCodigoSeguridad(e)) ? true : false;
		break;		
		
	}
	
	return r;	
	
}

function ValidarAlfaNumericos(valor){
	
	if((valor != "") && (valor.length > 0)){
		
		if(/^[a-zA-Z0-9\-\_]+$/.test(valor)){
			
			return true;
		
		}
		
	}
	
	return false;
	
}

function ValidarAlfaNumericosCantidad(valor, cantidad){
	
	if((valor != "") && (valor.length > 0) && (valor.length < cantidad)){
		
		if(/^[a-zA-Z0-9\-\_]+$/.test(valor)){
			
			return true;
		
		}
		
	}
	
	return false;
	
}

function EliminarPrensaImg(id){
	
	var get = "accion=eip&id=" + id;
	
	var datos = Ajax(get);
	
	if(datos == "OK"){
		
		return true;		
		
	}
	else{
		
		return false;
		
	}
	
}

function ActualizarEpigrafe(id , seccion , tipo){
	
	if(document.getElementById('epigrafe_' + id) != ""){

		var txt = document.getElementById('epigrafe_' + id).value;
		
		var get = "accion=eea&id=" + id + "&sec=" + seccion + "&tipo=" + tipo + "&valor=" + txt;
		
		var datos = Ajax(get);

		if(datos == "OK"){
			
			if(document.getElementById('athickbox_' + id) != null){
				
				document.getElementById('athickbox_' + id).title = txt;
			
			}
			
			alert("Dato Actualizado Correctamente");
			return true;		
			
		}
		else{
			
			return false;
			
		}
	
	}
}

function EliminarDatoAdjunto(id , seccion , tipo, id_master){
	var estado = window.confirm('¿Realmente desea eliminar esta archivo?');
	
	if(estado == true){
		var get = "accion=eia&id=" + id + "&sec=" + seccion + "&tipo=" + tipo;
	
		var datos = Ajax(get);

		if(datos == "OK"){
			
			location.href = "index.php?sec=" + seccion + "&op=adjuntar_" + tipo + "&id=" + id_master;
			
			return true;	
	
		}
		else{
	
			return false;
	
		}
	}
	else{
		
		return false;
	
	}

}


function CheckListTodos(campo, estado){
	
	for(var i = 1; document.getElementById(campo + i) != null; i++){

		document.getElementById(campo + i).checked = (estado == 1) ? true : false;
	
	}	
	
	document.getElementById('des_todos').style.display = (estado == 1) ? "block" : "none";
	document.getElementById('sel_todos').style.display = (estado == 1) ? "none" : "block";
	
	return true;
	
}

function CheckListTodosMooTools(container,obj, tipo){

	FancyForm.allnone(container,obj.checked);
	
	document.getElementById('des_todos_' + container).style.display = (obj.checked == 1) ? "block" : "none";
	document.getElementById('sel_todos_' + container).style.display = (obj.checked == 1) ? "none" : "block";
	ObtenerContenidosTamanios(tipo);
	return false;
	
}



var listado_menu = new Array();
function CargarMenu(sec, subsec){
	
	var menu = sec
	menu += ((subsec != null) && (subsec != "")) ? ("_" + subsec) : "";
	menu = (document.getElementById('menu_' + menu) != null) ? menu : sec;
	
	if(listado_menu.length > 0 && menu != ""){
		
		for(var i = 0; i < listado_menu.length; i++){
			
			var nombre_menu = listado_menu[i];
			
			if(document.getElementById('menu_' + nombre_menu) != null){

				if(menu == nombre_menu){
					
					document.getElementById('menu_' + nombre_menu).style.display = (document.getElementById('menu_' + nombre_menu).style.display == "none") ? "block": "none";
					
				}
				else{
					
					document.getElementById('menu_' + nombre_menu).style.display = "none";
					
				}	
				
			}
			
		}
		
	}

}

function EfectoDisplay(campo){
	
	document.getElementById(campo).style.display = (document.getElementById(campo).style.display == 'none') ? "block" : "none";
	
}

function ObtenerAliasUsuarios(username, seleccionado){

	var get = "accion=lau&username=" + username;
	
	datos = Ajax(get);
		
	LimpiarCbo("alias");

	if(datos != "NOK"){

		var l = datos.split('|');

		if(l.length > 0){

			for(var i = 0; i < l.length; i++){
				
				var d = l[i].split('[:?]');
				
				if(d[1] != null){
				
					AgregarDatosCbo("alias", unescape(d[1].replace(/\+/g, ' ', ' ')), unescape(d[1].replace(/\+/g, ' ')));
				
				}
				
			}
			
			if(seleccionado != null){
				
				if(seleccionado != ""){
					
					document.getElementById('alias').value = seleccionado;
				
				}

			}
		
		}
	}
	else{
	
		return false;
		
	}
	
}


function ObtenerUsuariosManagers(id, seleccionado){

	var get = "accion=lurm&id=" + id;
	
	datos = Ajax(get);
		
	LimpiarCbo("usuario");
	LimpiarCbo("alias");

	if(datos != "NOK"){

		var l = datos.split('|');

		if(l.length > 0){

			for(var i = 0; i < l.length; i++){
				
				var d = l[i].split('[:?]');
				
				if(d[1] != null){
				
					AgregarDatosCbo("usuario", unescape(d[1].replace(/\+/g, ' ', ' ')), unescape(d[1].replace(/\+/g, ' ')));
				
				}
				
			}
			
			if(seleccionado != null){
				
				if(seleccionado != ""){
					
					document.getElementById('usuario').value = seleccionado;
				
				}

			}
		
		}
	}
	else{
	
		return false;
		
	}
	
}

