<?xml version="1.0" encoding="UTF-8"?>
<dynaForm type="xmlmenu">
  <MNU_ADD type="link" value="" link="javascript:newRol();" onclick="" colAlign="left" colWidth="35">
    <en><![CDATA[New]]></en>
    <es>Nuevo</es>
    <pt>Novo</pt>
  </MNU_ADD>
  <PAGED_TABLE_ID type="private"/>
  <PAGED_TABLE_FAST_SEARCH type="FastSearch" label="@G::LoadTranslation(ID_SEARCH)"/>
  <SEARCH_FILTER_FORM type="private"/>
  <CONFIRM type="private"/>
  <JS type="javascript"><![CDATA[
PROCESS_REQUEST_FILE = '../roles/roles_Ajax';
PROCESS_REQUEST_FILE_USER = '../roles/roles_AddUser';

function newRol() {
	popupWindow('', '../roles/roles_Ajax?request=newRole', 350, 265);
}


function saveNewRole()
{
	code = $('form[ROL_CODE]').value;
	name = $('form[ROL_NAME]').value;
	if(code == '') {
		new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG1});
		return false;
	} else if(name == '') {
		new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG3});
		return false;
	}

	var uri = 'request=verifyNewRole&code='+code;
	var ajax = AJAX();
	ajax.open("POST", PROCESS_REQUEST_FILE, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4) {

			if(eval(ajax.responseText.trim())){
		  		try{
					parent = $('form[ROL_PARENT]').value;
				}catch(e){}
				code = $('form[ROL_CODE]').value;
				status = $('form[ROL_STATUS]').value;
			    var uri = 'request=saveNewRole&code='+code+'&parent='+parent+'&status='+status+'&name='+name;

			  	var oRPC = new leimnud.module.rpc.xmlhttp({
			  		url : PROCESS_REQUEST_FILE,
			  		args: uri
			  	});

			  	oRPC.callback = function(rpc){
				  	var oRPC = new leimnud.module.rpc.xmlhttp({
					    url   : PROCESS_REQUEST_FILE,
					    async : false,
					    method: 'POST',
					    args  : 'request=show&r='+Math.random()
				  	});
					oRPC.make();
					$('publisherContent[0]').innerHTML = oRPC.xmlhttp.responseText;
				window.location = 'roles_List';
				}.extend(this);
				oRPC.make();
				currentPopupWindow.remove();
		  	}

		  	if(!eval(ajax.responseText.trim())){
			 	new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG2});
				return false;
		  	}
		}
	}
	ajax.send(uri);

}

function editRole(ROL_UID)
{
	if(ROL_UID!='00000000000000000000000000000002')
	 {
    	var uri = 'request=editRole&ROL_UID='+ROL_UID;
    	popupWindow('', '../roles/roles_Ajax?'+uri, 350, 265);
   }
   else
   {
   		new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG});
   }
}

function updateRole(ROL_UID) {

  code = $('form[ROL_CODE]').value;
	name = $('form[ROL_NAME]').value;
	if(code == '') {
		new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG1});
		return false;
	} else if(name == '') {
		new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG3});
		return false;
	}
	if(ajax_function('roles_Ajax','verifyNewRole','request=updateDataRole&code='+code,'GET') =='false'){
		new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_MSG2});
		return false;
		}

  	try{
	parent = $('form[ROL_PARENT]').value;
	}catch(e){}

	status = $('form[ROL_STATUS]').value;

    var uri = 'request=updateRole&code='+code+'&parent='+parent+'&status='+status+'&rol_uid='+ROL_UID+'&name='+name;

  	var oRPC = new leimnud.module.rpc.xmlhttp({
  		url : PROCESS_REQUEST_FILE,
  		args: uri
  	});

  	oRPC.callback = function(rpc){
	  	var oRPC = new leimnud.module.rpc.xmlhttp({
		    url   : PROCESS_REQUEST_FILE,
		    async : false,
		    method: 'POST',
		    args  : 'request=show'
	  	});
		oRPC.make();
		$('publisherContent[0]').innerHTML = oRPC.xmlhttp.responseText;
		currentPopupWindow.remove();
		window.location = 'roles_List';
	}.extend(this);
	oRPC.make();
};

function deleteRole(ROL_UID) {
	 		var uri = 'request=canDeleteRole&ROL_UID='+ROL_UID;
			var ajax = AJAX();
			ajax.open("POST", PROCESS_REQUEST_FILE, true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {

					if(eval(ajax.responseText.trim())){
					  new leimnud.module.app.confirm().make({
					  	label:G_STRINGS.ID_REMOVE_ROLE,
					  	action:function() {
					  		var uri = 'request=deleteRole&ROL_UID='+ROL_UID;

						  	var oRPC = new leimnud.module.rpc.xmlhttp({
						  		url : PROCESS_REQUEST_FILE,
						  		args: uri
						  	});

						  	oRPC.callback = function(rpc){
							  	var oRPC = new leimnud.module.rpc.xmlhttp({
								    url   : PROCESS_REQUEST_FILE,
								    async : false,
								    method: 'POST',
								    args  : 'request=show'
							  	});
								oRPC.make();
								$('publisherContent[0]').innerHTML = oRPC.xmlhttp.responseText;
							  window.location = 'roles_List';
							}.extend(this);
							oRPC.make();

					  	}.extend(this)
					  });
					}
					else
					{
						new leimnud.module.app.alert().make({label: G_STRINGS.ID_ROLES_CAN_NOT_DELETE});
						return false;
					}
			 	}
			}
			ajax.send(uri);
};


function usersIntoRole(ROL_UID)
{
	var uri = 'request=usersIntoRole&ROL_UID='+ROL_UID;
	myRoleWindow = popupWindow('', '../roles/roles_Ajax?'+uri, 500, 450);
}

function deleteUserRole(ROL_UID, USR_UID){

	new leimnud.module.app.confirm().make({
    	label:G_STRINGS.ID_MSG_CONFIRM,
    	action:function(){
	        var oRPC = new leimnud.module.rpc.xmlhttp({
				url   : PROCESS_REQUEST_FILE,
				async : false,
				method: 'POST',
				args  : 'request=deleteUserRole&ROL_UID=' + ROL_UID + '&USR_UID=' + USR_UID
	        });
	        oRPC.make();
	        currentPopupWindow.clearContent();
			currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
		}.extend(this)
    });
};

function showUsers(ROL_UID)
{
oPanel = new leimnud.module.panel();
    oPanel.options = {
        size  :{w:400,h:512},
        position:{x:0,y:0,center:true},
        title : 'Roles',
        theme :"processmaker",
        statusBar:false,
        control :{resize:false,roll:false,drag:true},
        fx  :{modal:true,opacity:true,blinkToFront:false,fadeIn:false,drag:true}
      };
      oPanel.events = {
        remove: function() {
          delete(oPanel);
          resetChecks();
        }.extend(this)
      };
    oPanel.make();
    oPanel.loader.show();
    currentPopupWindow = oPanel;


	var oRPC = new leimnud.module.rpc.xmlhttp({
		url   : PROCESS_REQUEST_FILE_USER,
		async : false,
		method: 'POST',
		args  : 'request=showUsers&ROL_UID=' + ROL_UID
    });
    oRPC.make();
    currentPopupWindow.clearContent();
	currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
}
var checks_selected_Rolid = new Array();
function showUsersLoad( ROL_UID ){
     currentPopupWindow.remove();
    oPanel = new leimnud.module.panel();
    oPanel.options = {
        size  :{w:500,h:512},
        position:{x:0,y:0,center:true},
        title : 'Roles',
        theme :"processmaker",
        statusBar:false,
        control :{resize:false,roll:false,drag:true},
        fx  :{modal:true,opacity:true,blinkToFront:false,fadeIn:false,drag:true}
      };
      oPanel.events = {
        remove: function() {
          delete(oPanel);
          resetChecks();
        }.extend(this)
      };
    oPanel.make();
    oPanel.loader.show();
    currentPopupWindow = oPanel;
    var oRPC = new leimnud.module.rpc.xmlhttp({
        url : '../roles/roles_AddUser?ROL_UID='+ROL_UID,
        args: ''
      });

    //currentPopupWindow.clearContent();
	  //currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
      checks_selected_Rolid.push(ROL_UID);
      oRPC.callback = function(rpc) {
        oPanel.loader.hide();
        var scs=rpc.xmlhttp.responseText.extractScript();
        oPanel.addContent(rpc.xmlhttp.responseText);
        scs.evalScript();
        var inputs = document.getElementsByTagName("input");
        for(i=0; i<inputs.length; i++){
          if( inputs[i].type == "checkbox" ){
            try{
              inputs[i].onclick = function(){
                 if(this.checked){
                 checks_selected_ids.push(this.value);
                } else {
                checks_selected_ids.deleteByValue(this.value);
                }
             };
           }catch(e){alert(e)}
         }
       }
      }.extend(this);
    oRPC.make();


  }
function assignUserToRole(ROL_UID, USR_UID)
{
		var oRPC = new leimnud.module.rpc.xmlhttp({
		url   : PROCESS_REQUEST_FILE,
		async : false,
		method: 'POST',
		args  : 'request=assignUserToRole&ROL_UID=' + ROL_UID + '&USR_UID=' + USR_UID
    });
    oRPC.make();
    currentPopupWindow.clearContent();
	currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
}

function viewPermitions(ROL_UID)
{
	var uri = 'request=viewPermitions&ROL_UID='+ROL_UID;
	popupWindow('', '../roles/roles_Ajax?'+uri, 500, 450);
}

function showPermissions(ROL_UID)
{
	var oRPC = new leimnud.module.rpc.xmlhttp({
		url   : PROCESS_REQUEST_FILE,
		async : false,
		method: 'POST',
		args  : 'request=showPermissions&ROL_UID=' + ROL_UID
    });
    oRPC.make();
    currentPopupWindow.clearContent();
	currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
}

function assignPermissionToRole(ROL_UID, PER_UID)
{

	var oRPC = new leimnud.module.rpc.xmlhttp({
		url   : PROCESS_REQUEST_FILE,
		async : false,
		method: 'POST',
		args  : 'request=assignPermissionToRole&ROL_UID=' + ROL_UID + '&PER_UID=' + PER_UID
    });
    oRPC.make();
    currentPopupWindow.clearContent();
	currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
}

function deletePermissionRole(ROL_UID, PER_UID){

	new leimnud.module.app.confirm().make({
    	label:G_STRINGS.ID_MSG_CONFIRM,
    	action:function(){
	        var oRPC = new leimnud.module.rpc.xmlhttp({
				url   : PROCESS_REQUEST_FILE,
				async : false,
				method: 'POST',
				args  : 'request=deletePermissionRole&ROL_UID=' + ROL_UID + '&PER_UID=' + PER_UID
	        });
	        oRPC.make();
	        currentPopupWindow.clearContent();
			currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
		}.extend(this)
    });
};

function backPermissions(ROL_UID)
{
	var oRPC = new leimnud.module.rpc.xmlhttp({
		url   : PROCESS_REQUEST_FILE,
		async : false,
		method: 'POST',
		args  : 'request=viewPermitions&ROL_UID=' + ROL_UID
    });
    oRPC.make();
    currentPopupWindow.clearContent();
	currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
}

function backUsers(ROL_UID)
{
	var oRPC = new leimnud.module.rpc.xmlhttp({
		url   : PROCESS_REQUEST_FILE,
		async : false,
		method: 'POST',
		args  : 'request=usersIntoRole&ROL_UID=' + ROL_UID
    });
    oRPC.make();
    currentPopupWindow.clearContent();
	currentPopupWindow.addContent(oRPC.xmlhttp.responseText);
}



function AJAX()
{
	try	{
		xmlhttp = new XMLHttpRequest();
	}
	catch(generic_error) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (microsoft_old_error) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (microsoft_error) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}

function $(id){
	return document.getElementById(id);
}
function saveUsers(){

  if(checks_selected_Rolid.length>1){
    rolUid = checks_selected_Rolid[1];
  }else{
    rolUid = checks_selected_Rolid;
  }

  if( checks_selected_ids.length == 0 ){
    new leimnud.module.app.alert().make({label: G_STRINGS.ID_MSG_GROUPS_ADDCONFIRM});
    return 0;
  }
    var oRPC = new leimnud.module.rpc.xmlhttp({
   		url   : PROCESS_REQUEST_FILE,
      async : false,
      method: 'POST',
      args  : 'request=assignUserToRole&ROL_UID=' + rolUid + '&aUsers=' + checks_selected_ids
    });
    resetChecks();
    oRPC.make();
    currentPopupWindow.remove();

    usersIntoRole(rolUid);
}

function resetChecks(){
  checks_selected_ids.length = 0;
}
]]></JS>
</dynaForm>
