function USER_verPerfil(id,extern)
{
	if(extern == true)
	{
		SYS_loadContents('includes/html/usuarios.php?lista=true',undefined,function(){SYS_toPageFromObject($("USERENAB_cuerpo_" + id)); USER_verPerfil(id);});
		return;
	}
	
	SYS_loadContents("includes/html/usuarios.php?id=" + id,"USER_contents_" + id,function(){USER_openContents(id)});
}

function USER_editPerfil(id,extern)
{
	if(extern == true)
	{
		SYS_loadContents('includes/html/usuarios.php?lista=true',undefined,function(){USER_editPerfil(id);});
		return;
	}
	
	SYS_loadContents("includes/html/usuarios.php?form_usuario=true&editar=" + id,"USER_contents_" + id,function(){USER_openContents(id)});
}

function USER_editMiPerfil(id)
{
	SYS_loadContentsPOP("includes/html/usuarios.php?form_usuario=true&editar=" + id);
}

function USER_permisos(perfil,id,extern)
{
	if(extern == true)
	{
		SYS_loadContents('includes/html/usuarios.php?lista=true',undefined,function(){USER_permisos(perfil,id);});
		return;
	}
	
	SYS_loadContents("includes/html/permisos.php?form_usuario=true&perfil_directo=" + perfil,"USER_contents_" + id,function(){USER_openContents(id)});
}

function USER_openContents(id)
{
	var row0 = $("USER_row_0_" + id);
	if(row0 != undefined)
	{
		if(row0.className == "USER_row_1") row0.className = "USER_row_1_2";
		if(row0.className == "USER_row_2") row0.className = "USER_row_2_2";
	}
	
	var row = $("USER_row_1_" + id);
	if(row.className == "USER_row_1") row.className = "USER_row_1_2";
	if(row.className == "USER_row_2") row.className = "USER_row_2_2";
	
	row = $("USER_row_2_" + id);
	if(row.className == "USER_row_1") row.className = "USER_row_1_2";
	if(row.className == "USER_row_2") row.className = "USER_row_2_2";
	
	var obj = $("USER_contents_" + id);
	if(obj.style.display == "none") Effect.toggle(obj,"blind");
}

function USER_closeContents(id)
{
	var row = $("USER_row_0_" + id);
	if(row != undefined)
	{
		if(row.className == "USER_row_1_2") row.className = "USER_row_1";
		if(row.className == "USER_row_2_2") row.className = "USER_row_2";
	}
	
	var row = $("USER_row_1_" + id);
	if(row.className == "USER_row_1_2") row.className = "USER_row_1";
	if(row.className == "USER_row_2_2") row.className = "USER_row_2";
	
	row = $("USER_row_2_" + id);
	if(row.className == "USER_row_1_2") row.className = "USER_row_1";
	if(row.className == "USER_row_2_2") row.className = "USER_row_2";
	
	var obj = $("USER_contents_" + id);
	Effect.toggle(obj,"blind");
}

function USER_cancel(id)
{
	if(id != 0) CONFIRM_do(function(){USER_closeContents(id);},"¿ Seguro que desea cancelar ?<br><br>( se perderán todos los datos no guardados )");
}

function USER_agregarGrupo(id)
{
	//obtengo la ID y el nombre del grupo a agregar
	var GRUPOS = $("USER_grupos_" + id);
	var select = $("USER_selector_grupos_" + id);
	var index = select.value;
	var label = select.options[select.selectedIndex].text;
	
	if(index == 0) 
	{
		FORM_addComboAll("USER_agregarGrupo",select,id);
		return;
	}
	
	//obtengo el objeto donde guardo los grupos
	var grupos = $("USER_grupos_dato_" + id);
	
	if(!grupos.value.include(index + ","))
	{
		//agrego el grupo al objeto
		grupos.value = grupos.value + index + ",";
		
		//agrego visualmente el grupo
		var GRUPO = document.createElement("table");
		var TBODY = document.createElement("tbody");
		var TR1 = document.createElement("tr");
		var TD1 = document.createElement("td");
		var TD2 = document.createElement("td");
		var CADENA = document.createTextNode(label);
		var ICONO = document.createElement("img");
		
		GRUPO.className = "USER_grupo";
		TD2.appendChild(CADENA);
		TD2.className = "USER_grupo_nombre";
		TD1.className = "USER_grupo_icono";
		
		ICONO.setAttribute("alt","Quitar usuario de este grupo");
		ICONO.setAttribute("title","Quitar usuario de este grupo");
		ICONO.setAttribute("src","img/icon_delete.gif");
		ICONO.className = "DISABLED_off";
		
		TD1.appendChild(ICONO);
	
		//click
		if (TD1.addEventListener)
		{
			TD1.addEventListener("click", function(){USER_quitarGrupo(GRUPO,id,index);}, false);
		}
		else if (TD1.attachEvent)
		{
			TD1.attachEvent('onclick', function(){USER_quitarGrupo(GRUPO,id,index);});
		}
		
		//mouseOver
		if (ICONO.addEventListener)
		{
			ICONO.addEventListener("mouseover", function(){ICONO.className = "DISABLED_on";}, false);
		}
		else if (TD1.attachEvent)
		{
			ICONO.attachEvent('onmouseover', function(){ICONO.className = "DISABLED_on";});
		}
		
		//mouseOut
		if (ICONO.addEventListener)
		{
			ICONO.addEventListener("mouseout", function(){ICONO.className = "DISABLED_off";}, false);
		}
		else if (TD1.attachEvent)
		{
			ICONO.attachEvent('onmouseout', function(){ICONO.className = "DISABLED_off";});
		}
		
		TR1.appendChild(TD1);
		TR1.appendChild(TD2);
		TBODY.appendChild(TR1);
		GRUPO.appendChild(TBODY);
		GRUPOS.appendChild(GRUPO);
		
		if($("USER_grupos_none_" + id).style.diplasy != "none") $("USER_grupos_none_" + id).style.display = "none";
	}
	else CONFIRM_do(null,"El usuario ya se encuentra en ese grupo.",true)
}

function USER_quitarGrupo(grupo,id,index)
{
	Element.remove(grupo);
	var grupos = $("USER_grupos_dato_" + id);
	
	grupos.value = grupos.value.gsub(index + ",","");
	
	if(grupos.value.empty()) $("USER_grupos_none_" + id).style.display = "";
}

function USER_delete(id,unalert)
{
	SYS_mostrarRefresher();
	new Ajax.Updater("dummy_container","includes/html/usuarios.php",{method:"post",postBody:"borrar=" + id,onSuccess:function(retorno){
		if(SYS_isError(retorno.responseText))
		{
			SYS_doError(retorno.responseText);
			return;
		}
		SYS_ocultarRefresher();
		SYS_loadContents("includes/html/usuarios.php?lista=true");
		if(unalert != true)CONFIRM_do(null,"El usuario fue eliminado correctamente.",true)
	}});
}

function USER_new()
{
	SYS_loadContentsPOP('includes/html/usuarios.php?form_usuario=true');
}

function USER_checkCampoRepetido(obj,idFORM,id)
{

	//editando
	if(id == undefined) id = 0;

	var FORM = $(idFORM);
	var icono = $(idFORM + "_status_" + obj.name);
	var campoCheck = null;
	
	//obtengo el elemento donde marco si está bien o mal	
	var x;
	//for(x in FORM.elements) if(FORM.elements[x].name == obj.name + "_check") campoCheck = FORM.elements[x];
	for(x=0;x<FORM.elements.length;x++) if(FORM.elements[x].name == obj.name + "_check") campoCheck = FORM.elements[x];
	
	//establezco que voy a checkear
	var tipo = obj.name;
	
	//seteo el icono
	icono.src = "img/icon_check_check.gif";
	if(icono.className != "") icono.className = "";
	
	new Ajax.Updater("dummy_container","includes/html/usuarios.php",{method:"post",postBody:"check_dato=" + obj.value + "&tipo=" + tipo + "&id=" + id,onSuccess:function(retorno){
	if(SYS_isError(retorno.responseText))
	{
		SYS_doError(retorno.responseText);
		icono.src = "img/icon_check_error.gif";
		icono.setAttribute("alt","Inválido");
		icono.setAttribute("title","Inválido");
		campoCheck.value = "";
		return;
	}
	
	if(retorno.responseText == "OK")
	{
		icono.src = "img/icon_check_ok.gif";
		icono.setAttribute("alt","Válido");
		icono.setAttribute("title","Válido");
		campoCheck.value = obj.value;
	}
	else
	{
		icono.src = "img/icon_check_error.gif";
		var y;
		var mensaje = "";
		for(y in _ERRORES_CAMPOS) if(_ERRORES_CAMPOS[y].nombre == obj.name) mensaje = _ERRORES_CAMPOS[y].texto;
		icono.setAttribute("alt",mensaje);
		icono.setAttribute("title",mensaje);
		campoCheck.value = "";
	}
	}});
}

function USER_enable(id,unalert)
{
	SYS_mostrarRefresher();
	new Ajax.Updater("dummy_container","includes/html/usuarios.php",{method:"post",postBody:"enable=" + id,onSuccess:function(retorno){
		if(SYS_isError(retorno.responseText))
		{
			SYS_doError(retorno.responseText);
			return;
		}
		SYS_ocultarRefresher();
		SYS_loadContents("includes/html/usuarios.php?lista=true");
		if(unalert != true)CONFIRM_do(null,"La información de acceso fue actualizada con éxito.",true)
	}});
}

/*TODO!
//cambiar todos los Ajax.updater por el Ajax Request para no tener que tirar el "dummy_container";
*/