
function adminBorrarPublicacion(id_publicacion)	{
	
	if (confirm('¿Borrar esta publicación?'))	{
		document.getElementById("form-eliminar-publicacion-" + id_publicacion).submit();
	}
}



function todos(nombre_array, tamano)	{

	for (i = 0; i < tamano ; i++)	{
		id_mbro = eval(nombre_array + '[' + i + ']' );
		//alert(id_mbro + "---");
		//alert(nombre_array + '_' + id_mbro);
		if (document.getElementById(nombre_array + '_' + id_mbro).checked == true)
			document.getElementById(nombre_array + '_' + id_mbro).checked = false;
		else
			document.getElementById(nombre_array + '_' + id_mbro).checked = true;	
	}
}


function adminBuscarUsuarios()	{
	if (document.getElementById("form-busqueda-miembros").texto_busqueda.value != "")	{	
		window.location.href='/admin/miembros/texto-busqueda/' + urlencode(document.getElementById("form-busqueda-miembros").texto_busqueda.value);
	}
}

function adminBuscarPublicaciones(tipo)	{
	var redirect = '/admin/publicaciones/tipo/' + tipo;

	if (document.getElementById("form-busqueda-publicaciones").texto_busqueda.value != "")	{
		redirect += '/texto-busqueda/' + urlencode(document.getElementById("form-busqueda-publicaciones").texto_busqueda.value); 	
	}
	
	if (document.getElementById("form-busqueda-publicaciones").subseccion.value > 0)	{
		redirect += '/subseccion/'+ document.getElementById("form-busqueda-publicaciones").subseccion.value; 	
	}
	
	window.location.href= redirect;
}

function adminBorrarPublicaciones()	{
	if (confirm('¿Aplicar cambios?'))	{
		document.getElementById("form-publicaciones").submit();
	}
}

function adminBorrarBoletines()	{
	if (confirm('¿Borrar estos boletines?'))	{
		document.getElementById("form-boletines").submit();
	}
}


function adminBorrarUsuarios()	{
	
	if (confirm('¿Aplicar cambios?'))	{
		document.getElementById("form-miembros").submit();
	}
}

function adminComentarios()	{
	if (confirm('¿Aplicar cambios?'))	{
		document.getElementById("form-comentarios").submit();
	}
}

//////////////

//
// Funciones para el envio del form de login
//

var handleSuccess_enviarFormLoginAdmin = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            for(var campo in messages) { 
             		for (var razon in messages[campo])	{
             			if (messages[campo][razon] !== undefined && messages[campo][razon] != '')	
             				mensajeError += "<p>&raquo; " + messages[campo][razon] + "</p>\n";
             				
             			 
             				clase = new String(document.getElementById(campo).className);
             				
             				//alert(clase + "Error");
							document.getElementById(campo).className = clase + "Error";
             		} 
             }
             if (mensajeError != '')	{
             	document.getElementById("mensaje-error").innerHTML = mensajeError;
             	document.getElementById("mensaje-error").className = "mensaje-error";
             }
             else	{
             	document.getElementById('form-login-admin').submit();
             }
            
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_enviarFormLoginAdmin = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
	}
};

var callback_enviarFormLoginAdmin =
{
  success:handleSuccess_enviarFormLoginAdmin,
  failure:handleFailure_enviarFormLoginAdmin
};

function enviarFormLoginAdmin()	{

	//Reset todos los estilos:
	
	document.getElementById("correo-electronico").className		= "form_campoBuscadorHome";
	document.getElementById("password").className 				= "form_campoBuscadorHome";
   
	// argument formId can be the id or name attribute value of the
	// HTML form, or an HTML form object.
	var formObject = document.getElementById('form-login-admin');
	YAHOO.util.Connect.setForm(formObject);
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	var request = YAHOO.util.Connect.asyncRequest('POST', '/validar/formulario', callback_enviarFormLoginAdmin);
}

///////////////////


var handleSuccess_enviarFormBoletin = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            for(var campo in messages) { 
             		for (var razon in messages[campo])	{
             			if (messages[campo][razon] !== undefined && messages[campo][razon] != '')	
             				mensajeError += "<p>&raquo; " + messages[campo][razon] + "</p>\n";
             				
             			 
             				clase = new String(document.getElementById(campo).className);
             				
             				//alert(clase + "Error");
							document.getElementById(campo).className = clase + "Error";
             		} 
             }
             if (mensajeError != '')	{
             	document.getElementById("mensaje-error").innerHTML = mensajeError;
             	document.getElementById("mensaje-error").className = "mensaje-error";
             }
             else	{
             	document.getElementById('form-boletin').submit();
             }
            
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_enviarFormBoletin = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
	}
};

var callback_enviarFormBoletin =
{
  success:handleSuccess_enviarFormBoletin,
  failure:handleFailure_enviarFormBoletin
};

function enviarFormBoletin()	{

	//Reset todos los estilos:
	
   
	// argument formId can be the id or name attribute value of the
	// HTML form, or an HTML form object.
	var formObject = document.getElementById('form-boletin');
	YAHOO.util.Connect.setForm(formObject);
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	var request = YAHOO.util.Connect.asyncRequest('POST', '/validar/formulario', callback_enviarFormBoletin);
}

////////////////////
var handleSuccess_boletines_lista_subsecciones = function(o){
	if(o.responseText !== undefined){
		document.getElementById("menu_subsecciones").innerHTML = o.responseText;
	}
};

var handleFailure_boletines_lista_subsecciones = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
	}
};

var callback_boletines_lista_subsecciones =
{
  success:handleSuccess_boletines_lista_subsecciones,
  failure:handleFailure_boletines_lista_subsecciones
};

function boletines_lista_subsecciones(id_tipo_publicacion)	{
	document.getElementById("menu_subsecciones").innerHTML	= '<div style=\'text-align:center\'><img src=\'/images/ajax-loader.gif\'></div>';
	document.getElementById('menu_publicaciones').innerHTML = '';
	var request = YAHOO.util.Connect.asyncRequest('GET', '/admin/listasubseccionesboletin/id_tipo_publicacion/' + id_tipo_publicacion, callback_boletines_lista_subsecciones);
}

var handleSuccess_boletines_lista_publicaciones = function(o){
	if(o.responseText !== undefined){
		document.getElementById("menu_publicaciones").innerHTML = o.responseText;
	}
};

var handleFailure_boletines_lista_publicaciones = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
	}
};

var callback_boletines_lista_publicaciones =
{
  success:handleSuccess_boletines_lista_publicaciones,
  failure:handleFailure_boletines_lista_publicaciones
};

function boletines_lista_publicaciones(id_tipo_publicacion, id_subseccion)	{
	document.getElementById("menu_publicaciones").innerHTML	= '<div style=\'text-align:center\'><img src=\'/images/ajax-loader.gif\'></div>';
	var request = YAHOO.util.Connect.asyncRequest('GET', '/admin/listapublicacionesboletin/id_tipo_publicacion/' + id_tipo_publicacion + '/id_subseccion/' + id_subseccion, callback_boletines_lista_publicaciones);
}

///////

var handleSuccess_PublicacionesBoletin = function(o){
	if(o.responseText !== undefined){
		document.getElementById("div_publicaciones_boletin").innerHTML = o.responseText;
	}
};

var handleFailure_PublicacionesBoletin = function(o){
	if(o.responseText !== undefined){
		div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
		div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
		div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
	}
};

var callback_PublicacionesBoletin =
{
  success:handleSuccess_PublicacionesBoletin,
  failure:handleFailure_PublicacionesBoletin
};

function agregareliminarPublicacionesBoletin(accion)	{

	if (accion == 'agregar')	{
		len = document.getElementById("publicaciones_agregar").length
		i = 0
		chosen = ""
		
		for (i = 0; i < len; i++) {
			if (document.getElementById("publicaciones_agregar")[i].selected) {
				chosen = chosen + document.getElementById("publicaciones_agregar")[i].value + ","
			}
		}
	}
	else if (accion == 'eliminar')	{
		var id_publicaciones		= document.getElementById("ids_publicaciones").value;
		var id_publicaciones_array	= id_publicaciones.split(",");
	
			len = id_publicaciones_array.length
			i = 0
			chosen = ""
			
			for (i = 0; i < len; i++) {
				if (id_publicaciones_array[i] > 0)	{ 
					//alert (id_publicaciones_array[i] + ' ---- ' + document.getElementById("eliminar_publicacion_" + id_publicaciones_array[i]).checked);
					if (document.getElementById("eliminar_publicacion_" + id_publicaciones_array[i]).checked) {
						chosen = chosen + id_publicaciones_array[i] + ","
					}
				}
			}
	}
	
	document.getElementById("div_publicaciones_boletin").innerHTML	= '<div style=\'text-align:center\'><img src=\'/images/ajax-loader.gif\'></div>';
	var request = YAHOO.util.Connect.asyncRequest('GET', '/admin/publicacionesboletin/'+ accion + '_id_publicaciones/' + chosen, callback_PublicacionesBoletin);
}

//////////////////
function previsualizarBoletin()	{
	var id_publicaciones		= document.getElementById("ids_publicaciones").value;
	var id_publicaciones_array	= id_publicaciones.split(",");
	tmp							= new Array();
	hay_error					= false;


	len = id_publicaciones_array.length
	i = 0
	param_str = ""
			
	for (i = 0; i < len; i++) {
		if (id_publicaciones_array[i] > 0)	{
			//alert (document.getElementById("orden_publicacion_" + id_publicaciones_array[i]).value);
			tmp[i] = document.getElementById("orden_publicacion_" + id_publicaciones_array[i]).value;
			
			//id_publicacion-posicion
			param_str = param_str + id_publicaciones_array[i] + '-' + tmp[i] + ',';
		} 
	}
	
	//alert(param_str);
	
	window.open('/admin/previsualizarboletin/titulo/' + urlencode(document.getElementById("titulo").value) + '/id_publicaciones/' + param_str + '/fecha_envio/' + urlencode(document.getElementById("fecha_envio").value));
	
}


function ordenarPublicacionesBoletin(accion)	{
	var id_publicaciones		= document.getElementById("ids_publicaciones").value;
	var id_publicaciones_array	= id_publicaciones.split(",");
	tmp							= new Array();
	hay_error					= false;
	var publicaciones			= new Array();

	len = id_publicaciones_array.length
	i = 0
	param_str = ""
			
	for (i = 0; i < len; i++) {
		if (id_publicaciones_array[i] > 0)	{
			//alert (document.getElementById("orden_publicacion_" + id_publicaciones_array[i]).value);
			tmp[i] 				= document.getElementById("orden_publicacion_" + id_publicaciones_array[i]).value;
			titulo_publicacion	= document.getElementById("titulo_publicacion_" + id_publicaciones_array[i]).value;
			
			var publicacion = {id: id_publicaciones_array[i], orden: tmp[i], titulo: titulo_publicacion};
			
			publicaciones[i] = publicacion;
			
			//id_publicacion-posicion
			//param_str = param_str + id_publicaciones_array[i] + '-' + tmp[i] + ',';
		} 
	}
	
	
	for(var i = 0, n = tmp.length; i < n; i++) {
      for(var x = i + 1 ; x < n; x++)
         if(tmp[x]==tmp[i]) {
			alert('La posición ' + tmp[x] + ' tiene mas de una publicación');
			hay_error	= true;
		}
	}
	
	
	var jsonStr = YAHOO.lang.JSON.stringify(publicaciones);
	
	//alert(jsonStr);
	
	document.getElementById("datos_publicaciones").value = jsonStr;
	
	
	if (!hay_error)	{
	
		var formObject = document.getElementById('form-dummy-boletines');
		YAHOO.util.Connect.setForm(formObject);
		document.getElementById("div_publicaciones_boletin").innerHTML	= '<div style=\'text-align:center\'><img src=\'/images/ajax-loader.gif\'></div>';
		var request = YAHOO.util.Connect.asyncRequest('POST', '/admin/publicacionesboletin/ordenar_id_publicaciones/1', callback_PublicacionesBoletin);
	
		
		//var request = YAHOO.util.Connect.asyncRequest('GET', '/admin/publicacionesboletin/ordenar_id_publicaciones/' + param_str, callback_PublicacionesBoletin);
	}
	
}




// {{{ urlencode
function urlencode( str ) {
    // URL-encodes string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/
    // +       version: 809.1713
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}// }}}
