/*
Validacion Formularios 
Nivel ADMINISTRADOR
Luis Matias Suarez
06/02/09
*/

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 los campos
function ValidarCampo(campo, tipo){
	var e = dge(campo).value;
	var r = false;
	switch(tipo){
		case 'text': //Valida string
			r = ((e.length > 0) && (e != "")) ? true : false;
		break;
		case 'int': //Valida numeros enteros
			r = (e > 0) ? true : false;
		break;
		case 'email': //Valida formato email
			r = (ValidarEmail(e)) ? true : false;
		break;
		case 'fecha': //Valida formato email
			r = (ValidarFecha(e)) ? true : false;
		break;
	}
	return r;	
}

// Valida el formato de la cuenta de correo
function ValidarEmail(email) {
	if((email.length > 0) && (email != "")){
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
			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 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];
			
			m += (!ValidarCampo(campo, tipo_validacion)) ? (mensaje_error + "\n") : "";
		
		}
		
	}
	
	if(m == ""){
	
		return true;
	
	}
	
	alert(m);
	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;
	
	}
}
