jQuery(document).ready(function(){

	var props={
			type:"ok",
			delay:800,
			goTop:false,
			block:false,
			closeLink:true
			
	};
	
	$("#acciones_avisos").rup_feedback(props);
	//$("#massiveActionActiveStatus").val(true);
	$("#massiveActionActiveStatus").attr('checked',true);
	
	$("#massiveActionType").rup_combo({
		blank : "0",
		width: "350",
		source : [
			{
				i18nCaption: "changeAppAccess",
				value:"changeAppAccess",
				style:""
			},
			{
				i18nCaption: "changeEntityStatus",
				value:"changeEntityStatus",
				style:""
			},
			{
				i18nCaption: "changeAsociarEntidadesAGrupo",
				value:"changeAsociarEntidadesAGrupo",
				style:""
			}
		],
		change : function(){
			if($("#massiveActionType").val() == "changeAppAccess"){
				$("#massiveActionAppAccess").show();
				$("#massiveActionEntityStatus").hide();
				$("#massiveAsociarEntidadesAGrupo").hide();
			} else if($("#massiveActionType").val() == "changeEntityStatus"){
				$("#massiveActionAppAccess").hide();
				$("#massiveActionEntityStatus").show();
				$("#massiveAsociarEntidadesAGrupo").hide();
			} else if($("#massiveActionType").val() == "changeAsociarEntidadesAGrupo"){
				$("#massiveActionAppAccess").hide();
				$("#massiveActionEntityStatus").hide();
				$("#massiveAsociarEntidadesAGrupo").show();
				$("#group_tree").reload();
			} else {
				$("#massiveActionAppAccess").hide();
				$("#massiveActionEntityStatus").hide();
				$("#massiveAsociarEntidadesAGrupo").hide();
			}
		}
	});
	
	$("#massiveActionAccept").click(function(){
		if($("#massiveActionType").val() == "changeAppAccess"){
			changeAppAccess();
		} else if($("#massiveActionType").val() == "changeEntityStatus"){
			changeEntityStatus();
		}else if ($("#massiveActionType").val() == "changeAsociarEntidadesAGrupo"){
			y42bAsociarEntidadesAGrupo();
		}
		//closeMassiveActionDialog();
	});
	
	$("#massiveActionClean").click(function(){
		$("#massiveActionType").rup_combo("select", "0");
		$("#massiveActionAppAccess").hide();
		$("#massiveActionEntityStatus").hide();
	});
	
	
});

$("#massiveActionCancelar").click(function(){
	//parent jsp element
	$("#searchMassiveActionDialog").dialog("close");
	
});

function changeAppAccess() {
	var massiveActionKontsumoGest = "";
	var massiveActionFormativa = "";
	var massiveActionKontsumoNet = ""; 
	var massiveActionConsultaWeb = "";
	if ($('input[id=massiveActionKontsumoGest]').is(':checked')){
		massiveActionKontsumoGest="S";
	}else{
		massiveActionKontsumoGest="N";
	}
	if ($('input[id=massiveActionFormativa]').is(':checked')){
		massiveActionFormativa="S";
	}else{
		massiveActionFormativa="N";
	}
	if ($('input[id=massiveActionKontsumoNet]').is(':checked')){
		massiveActionKontsumoNet="S";
	}else{
		massiveActionKontsumoNet="N";
	}
	if ($('input[id=massiveActionConsultaWeb]').is(':checked')){
		massiveActionConsultaWeb="S";
	}else{
		massiveActionConsultaWeb="N";
	}
	//alert("checked: " + massiveActionKontsumoGest);
	//alert("checked: " + massiveActionFormativa);
	//alert("checked: " + massiveActionKontsumoNet);
	//alert("checked: " + massiveActionConsultaWeb);
	
	/*console.debug("checked: " + massiveActionKontsumoGest);
	console.debug("checked: " + massiveActionFormativa);
	console.debug("checked: " + massiveActionKontsumoNet);
	console.debug("checked: " + massiveActionConsultaWeb);
	*/
	
	var url = "../envio/changeAppAccess?envioId="+$("#idEnvio").val();
	url = url + "&kontsumoGest=" + massiveActionKontsumoGest;
	url = url + "&ofertaFormativa=" + massiveActionFormativa;
	url = url + "&kontsumoNet=" + massiveActionKontsumoNet;
	url = url + "&consultaWeb=" + massiveActionConsultaWeb;
	//alert("url: " + url);
	//console.debug("url: " + url);
	//return false;
	
	
	
	
	$.ajax({
		type : 'GET',
		url : url,
		error : function(jqXHR, textStatus, errorThrown) {
			$("#acciones_avisos").rup_feedback("option", "delay",null);
			$("#acciones_avisos").rup_feedback("set",jqXHR.responseText, "error");
		},
		success : function(data, textStatus, jqXHR) {
			$("#acciones_avisos").rup_feedback("option", "delay",1500);
			$("#acciones_avisos").rup_feedback("set", $.rup.i18n.app.msg.accionMasivaOK, "ok");
			setTimeout(function() {
				cleanSearch();
				$("#searchMassiveActionDialog").dialog("close");
				return;
			}, 2000);
		}
	});
}

function changeEntityStatus() {
	var status="";
	var url="";
	if ($('input[id=massiveActionActiveStatus]').is(':checked')){
		status="S";
	}else{
		status="N";
	}
	
	url="../envio/changeEntityStatus?envioId="+$("#idEnvio").val();
	url=url + "&active="+status;

	//alert(url);
	
	$.ajax({
		type : 'GET',
		url : url,
		error : function(jqXHR, textStatus, errorThrown) {
			$("#acciones_avisos").rup_feedback("option", "delay",null);
			$("#acciones_avisos").rup_feedback("set",jqXHR.responseText, "error");
		},
		success : function(data, textStatus, jqXHR) {
			$("#acciones_avisos").rup_feedback("option", "delay",1500);
			$("#acciones_avisos").rup_feedback("set", $.rup.i18n.app.msg.accionMasivaOK, "ok");
			
			setTimeout(function() {
				cleanSearch();
				$("#searchMassiveActionDialog").dialog("close");
				return;
			}, 2000);
			
		}
	});
}


$("#group_tree").dynatree({
	initAjax: {
		url: "../entidad/groupTree"
		, data: {entidadId: "", grupos: ""} // Optional arguments to append to the url

	},
	activeVisible: true, // Make sure, active nodes are visible (expanded).
	checkbox: true, // Show checkboxes.
	selectMode: 2, // 1:single, 2:multi, 3:multi-hier
	onClick: function(node, event) {
		// We should not toggle, if target was "checkbox", because this
		// would result in double-toggle (i.e. no toggle)
		if( node.getEventTargetType(event) == "title" ) {
			node.toggleSelect();
		}
	},
	onSelect: function(select, node) {
		// Display list of selected nodes
		var selNodes = node.tree.getSelectedNodes();
		// convert to key array
		var keyList = $.map(selNodes, function(node){
			return node.data.key;
		});
		
		$("#y42bMasivos_listaGrupos").val(keyList);
		//saveNodeGrupos(select, node);
		
	},
	onPostInit: function(isReloading, isError) {
    	$("#group_tree").dynatree("getRoot").visit(function(node){
	   		 node.expand(false);
    	});
	},
	// The following options are only required, if we have more than one tree on one page:
	cookieId: "group_list_dynatree",
	idPrefix: "group_list_dynatree"
});



//botones contraer y expandir arbol 
$("#y42bgroup_tree_expandir").click(function () {
	$("#group_tree").dynatree("getRoot").visit(function(node){
		 node.expand(true);
	});
	return false;
}); 

$("#y42bgroup_tree_contraer").click(function () {
	$("#group_tree").dynatree("getRoot").visit(function(node){
		 node.expand(false);
	});
	return false;
}); 



function validarCampos(){
	var txtFeedback = "";
	var validoCampo1 = false;

	if ( $("#y42bMasivos_listaGrupos").val()==""){
		txtFeedback += "<li>"+ $.rup.i18n.app.msg.requiredGrupo +"</li>\n";
		validoCampo1 = false;
	}else{
		validoCampo1 = true;
	}

	
	if (validoCampo1){
		return true;
	}else{
		$("#acciones_avisos").rup_feedback("option", "delay",null);
		$("#acciones_avisos").rup_feedback("set","<ul>" + txtFeedback + "</ul>", "warning");

		return false;
	}
}


function y42bAsociarEntidadesAGrupo(){
		
	if (validarCampos()){
		
		var listaGrupos = $("#y42bMasivos_listaGrupos").val();
		
		url="../envio/changeAsociarEntidadesAGrupo?envioId="+$("#idEnvio").val();
		url=url + "&listaGrupos="+listaGrupos;
				
		$.ajax({
			type : 'GET',
			url : url,
			error : function(jqXHR, textStatus, errorThrown) {
				$("#acciones_avisos").rup_feedback("option", "delay",null);
				$("#acciones_avisos").rup_feedback("set",jqXHR.responseText, "error");
			},
			success : function(data, textStatus, jqXHR) {
				$("#acciones_avisos").rup_feedback("option", "delay",1500);
				$("#acciones_avisos").rup_feedback("set", $.rup.i18n.app.msg.accionMasivaOK, "ok");
				setTimeout(function() {
					cleanSearch();
					$("#searchMassiveActionDialog").dialog("close");
					return;
				}, 2000);
				
			}
		});
		
	}
	
	
	
	

}

