function MarcarTodos(){
	for (i=0;i<document.forms[1].elements.length;i++){
		if(document.forms[1].elements[i].type == "checkbox");
		document.forms[1].elements[i].checked = 1;
	}
}

function DesMarcarTodos(){
	for (i=0;i<document.forms[1].elements.length;i++){
		if(document.forms[1].elements[i].type == "checkbox");
		document.forms[1].elements[i].checked = 0;
	}
}

function Agregar(codGrupo){
	if(confirm('Vas a agregar los videos seleccionados al grupo, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'AGREGAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function Quitar(codGrupo){
	if(confirm('Vas a quitar los videos seleccionados del grupo, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'QUITAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function Aprobar(codGrupo){
	if(confirm('Vas a aprobar los videos seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'APROBAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function Denegar(codGrupo){
	if(confirm('Vas a denegar los videos seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'DENEGAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function Eliminar(codGrupo){
	if(confirm('Vas a eliminar los videos seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'ELIMINAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function AprobarUsuario(codGrupo){
	if(confirm('Vas a aprobar a los usuarios seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'APROBAR';
		document.getElementById('codGrupo').value   = codGrupo;
		document.forms[1].submit();
	}
}

function DenegarUsuario(codGrupo){
	if(confirm('Vas a denegar los usuarios seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'DENEGAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function EliminarUsuario(codGrupo){
	if(confirm('Vas a eliminar los usuarios seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'ELIMINAR';
		document.getElementById('codGrupo').value     = codGrupo;
		document.forms[1].submit();
	}
}

function AprobarPost(){
	if(confirm('Vas a aprobar los post seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'APROBAR';
		document.forms[1].submit();
	}
}

function DenegarPost(){
	if(confirm('Vas a denegar los post seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'DENEGAR';
		document.forms[1].submit();
	}
}

function EliminarPost(){
	if(confirm('Vas a eliminar los post seleccionados, ¿quieres continuar?')){
		document.getElementById('accionSubmit').value = 'ELIMINAR';
		document.forms[1].submit();
	}
}

function EliminarGrupo(codGrupo){
	if(confirm('¡Atención!\n¿Estás seguro que quieres borrar el grupo y toda su información asociada?')){
	document.getElementById('eliminarGrupo').value = 'ELIMINAR';
	document.getElementById('codGrupo').value     = codGrupo;
	document.forms[1].submit();
	}
}


function mostrarComentarios(id){
	document.getElementById("respuesta"+id).style.display = "block";
	document.getElementById("comentarioMas"+id).style.display = "none";
	document.getElementById("comentarioMenos"+id).style.display = "block";
}
function ocultarComentarios(id){
	document.getElementById("respuesta"+id).style.display = "none";
	document.getElementById("comentarioMas"+id).style.display = "block";
	document.getElementById("comentarioMenos"+id).style.display = "none";
}

function Seleccion(){
	document.getElementById('accionSubmit').value = 'SELECCION';
	document.forms[1].submit();
}
//////////////////////////////////////////////////////////////////
//////////////// AJAX PARA SUSCRICIONES DE GRUPOS ////////////////
//////////////////////////////////////////////////////////////////

function xmlhttpPost(str,Url,tipo, codGrupo) {
	var xmlHttpReq = false;
	var self = this;
	var strURL = Url;
	// Mozilla/Safari
	var opt=tipo;
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlHttpReq.open('POST', strURL, true);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function(tipo) {
		if ((xmlHttpReq.readyState == 4) && (opt==1)) {
				updateSuscrito(xmlHttpReq.responseText);
				var tipo =xmlHttpReq.responseText;
				if(tipo==0){// GRUPO LIBRE. LE MOSTRAMOS LA INFORMACION DE USUARIO
					InfoGrupo(codGrupo, 0);
				}
				if(tipo==1){// EN ESPERA DE MODERACION
					InfoGrupo(codGrupo, 2);
				}
				
		}
		if ((xmlHttpReq.readyState == 4) && (opt==2)) {
				updateSuscrito(xmlHttpReq.responseText);
		}
		if ((xmlHttpReq.readyState == 4) && (opt==4)) {
				updateMarcado(xmlHttpReq.responseText);
		}
		if ((xmlHttpReq.readyState == 4) && (opt==5)) {
				updateInfoGrupo(xmlHttpReq.responseText);
		}
	}
	xmlHttpReq.send(str);
}

function updateSuscrito(str){

	if(str == 0){// YA ES MIEMBRO
		mostrarEnlacesMiembroGrupo();
	}
	if(str == 1){// EN ESPERA DE MODERACION
		mostrarEnlacesEsperaGrupo();
	}
	if(str == 3){// NO ES MIEMBRO
		mostrarEnlacesNoMiembroGrupo();
	}
	if(str == 4){// NO ES MIEMBRO
		mostrarEnlacesNoMiembroGrupo();
	}
	if(str == 5){// NO PUEDE SUSCRIBIRSE
		mostrarNada();
	}
}

function updateMarcado(str){
	document.getElementById("DIV-marcar-"+str).innerHTML = '';
}

function updateInfoGrupo(str){
	document.getElementById("DIV-infoGrupo").innerHTML = str;
}

function suscribirse(codGrupo, codUsuario){
	qstr = 'codGrupo=' + codGrupo + '&codUsuario=' + codUsuario + '&tipo=1';  // NOTE: no '?' before querystring
	xmlhttpPost(qstr,"/grupo/suscribirse.phtml",1, codGrupo);
}

function quitarSuscrito(codGrupo, codUsuario){
	if(confirm('Vas a dejar de ser miembro del grupo, ¿quieres continuar?')){
		qstr = 'codGrupo=' + codGrupo + '&codUsuario=' + codUsuario + '&tipo=2';  // NOTE: no '?' before querystring
		xmlhttpPost(qstr,"/grupo/suscribirse.phtml",2, codGrupo);
		InfoGrupo(codGrupo, 2);// MOSTRAMOS LA INFORMACION DEL GRUPO
	}
}

function dejarGrupo(codGrupo, codUsuario){
	if(confirm('Vas a dejar de ser miembro del grupo, ¿quieres continuar?')){
		qstr = 'codGrupo=' + codGrupo + '&codUsuario=' + codUsuario;  // NOTE: no '?' before querystring
		xmlhttpPost(qstr,"/grupo/dejarGrupo.phtml",3, codGrupo);
		window.location.href = '/usuario/misGrupos/miembro/';
	}
}

function marcarInapropiado(codGrupo, codUsuario){
	if(confirm('Vas a marcar este grupo como inapropiado, ¿quieres continuar?')){
		qstr = 'codGrupo=' + codGrupo + '&codUsuario=' + codUsuario;  // NOTE: no '?' before querystring
		xmlhttpPost(qstr,"/grupo/marcarInapropiado.phtml",4, codGrupo);
	}
}

function mostrarEnlacesMiembroGrupo(){
	document.getElementById("unirmeGrupo").style.display  = "none";
	document.getElementById("dejarGrupo").style.display   = "block";
	document.getElementById("esperaGrupo").style.display  = "none";
}

function mostrarEnlacesNoMiembroGrupo(){
	document.getElementById("unirmeGrupo").style.display  = "block";
	document.getElementById("dejarGrupo").style.display   = "none";
	document.getElementById("esperaGrupo").style.display  = "none";
}

function mostrarEnlacesEsperaGrupo(){
	document.getElementById("unirmeGrupo").style.display  = "none";
	document.getElementById("dejarGrupo").style.display   = "none";
	document.getElementById("esperaGrupo").style.display  = "block";
}

function mostrarNada(){
	document.getElementById("unirmeGrupo").style.display  = "none";
	document.getElementById("dejarGrupo").style.display   = "none";
	document.getElementById("esperaGrupo").style.display  = "none";
}

function InfoGrupo(codGrupo, tipoUsuario){
	qstr = 'codGrupo=' + codGrupo + '&tipoUsuario=' + tipoUsuario;  // NOTE: no '?' before querystring
	xmlhttpPost(qstr,"/grupo/infoGrupo.phtml",5, codGrupo);
}
