$(document).ready(function() {
	//Desabilitar la ayuda en internet explorer 
	document.onhelp = function() {
        event.cancelBubble = true;
        event.returnValue = false;
        event.preventDefault();
    };
    
    //Desabilitar el boton derecho
    document.oncontextmenu = function(){
        //return false;
    };

    //Desabilitar el boton derecho
    document.onkeydown = function(){
    	
    };
    
    $(document).bind('keypress', function(event) {
    	var e = event || window.event;    	
    	var k = null;
        if(e.which) k = e.which;
        else if(e.keyCode) k = e.keyCode;
        
      //Control para permitir ctrl+c, ctrl+v, ctrl+x
        if(e.ctrlKey==true && k==99)
			return true;
		if(e.ctrlKey==true && k==118)
			return true;
		if(e.ctrlKey==true && k==120)
			return true;
		
        if((e.ctrlKey) && (k===81 || k===113 || k===115 || k ===116)){
        	e.preventDefault();
            return false;
        }
    });
    
   
    $(document).bind('keydown', function(event) {    	
    	var e = event || window.event;    	
    	var k = null;
        if(e.which) k = e.which;
        else if(e.keyCode) k = e.keyCode;
        
      //Control para permitir altgr
        if (event.ctrlKey && event.altKey) {
        	return true;
        }
        
        //Control para permitir ctrl+c, ctrl+v, ctrl+x
        if(e.ctrlKey==true && k==99)
			return true;
		if(e.ctrlKey==true && k==118)
			return true;
		if(e.ctrlKey==true && k==120)
			return true;
		
        //Control para manejar el foco dentro de la misma pantalla, tab, lov, dialogo
        if(k == 9 && e.shiftKey){
        	return true;
        }else if (k == 9) {
        	var actElement = document.activeElement;
        	// Uppercase de la ultima letra al dar TAB
        	if($(actElement).hasClass("m-upper-case")){
        		try{
        			actElement.value=actElement.value.toUpperCase();
        		}catch(Err){console.log(Err);}
        	}
        	// Retorno del foco al primer elemento en un Dialogo
        	var elementsDialog = $(getActiveFrame()).contents().find("div.ui-dialog.ui-overlay-visible").find(":input:enabled:visible:not(input[type=hidden]), button:enabled");
			var dialogLastElem = elementsDialog.last();
			if(dialogLastElem.length>0 && dialogLastElem[0].id==actElement.id){
				var element = elementsDialog.first();
				element.select();
				element.focus();
				return false;
			}else{
				var elementsInnerTabs = $(getActiveFrame()).contents().find(".m-tab-inner").find("div.ui-tabs-panels>div[aria-hidden='false']").find(".m-filters, .m-controls, .m-data-content").find(":input:enabled:visible:not(input[type=hidden]), button:enabled");
				var innerTabLastElem = elementsInnerTabs.last();
				if(innerTabLastElem.length>0 && innerTabLastElem[0].id==actElement.id){
					var element = elementsInnerTabs.first();
					element.select();
					element.focus();
					return false;
				}else{
					//Retorno del foco al primer elemento en el tab activo
					var elementsActiveTab = $(getActiveFrame()).contents().find(".m-filters, .m-controls, .m-data-content").find(":input:enabled:visible:not(input[type=hidden]), button:enabled");
		        	var tabLastElem = elementsActiveTab.last();
		        	if(tabLastElem.length>0 && tabLastElem[0].id==actElement.id){
						var element = elementsActiveTab.first();
						element.select();
						element.focus();
						return false;
					}else{
						// Retorno del foco al primer elemento en un LOV
						var actFrame = $(getActiveFrame());
						var framesLov = $(getActiveFrame()).contents().find("div.ui-dialog.ui-overlay-visible").find("iframe");
						if(framesLov.length>0 && framesLov[0]){
							var elementsFrame = $(framesLov[0]).contents().find(".m-filters, .m-controls, .m-data-content").find(":input:enabled:visible:not(input[type=hidden]), button:enabled");
							var frameLastElem = elementsFrame.last();
							if(frameLastElem.length>0 && frameLastElem[0].id==actElement.id){
								var element = elementsFrame.first();
								element.select();
								element.focus();
								return false;
							}
						}
					}
				}
			}   	
        	return true;
		}
        if(e.altKey && (k == 37 || k == 39)){
        	e.preventDefault();
        	return false;
        }
        if(e.altKey && (k == 115)){
        	e.preventDefault();
        	return false;
        }        
        if(e.ctrlKey && k == 84){
        	e.preventDefault();
        	return false;
        }
        if (e.ctrlKey){
    		if( k == 65 || k == 66 || k == 68 || k == 69 || k == 70 ||
	            k == 71 || k == 72 || k == 73 || k == 74 || k == 75 ||
	            k == 76 || k == 78 || k == 79 || k == 80 || k == 81 ||
	            k == 82 || k == 83 || k == 84 || k == 85 || k == 87 ||
	            k == 107 || k == 109 || k == 115 || k == 116){
    			e.preventDefault();    			
	            return false;
	        }
        }
    	//F1
        if(k === 112) {
        	helpController();
           return false;
        }
        //F2
        if(k === 113) {
        	showHideTransactionInputs();
           return false;
        }
        //F3
        if(k === 114) {
           return false;
        }
        //F4
        if(k === 115) {
        	reloadTab();
           return false;
        }
        //F5
        if(k === 116) {
        	queryController();
        	return false;
        }
        //F6
        if(k === 117) {
           return false;
        }
        //F7
        if(k === 118) {
           return false;
        }
        //F8
        if(k === 119) {
        	previousController();
        	return false;
        }
        //F9
        if(k === 120) {
        	nextController();
        	return false;
        }
        //F10
        if(k === 121) {
        	saveController();
        	return false;
        }
        //F11
        if(k === 122) {
           return false;
        }
        //F12
        if(k === 123) {
           return false;
        }
    });
    
   

    
});

function getActiveFrame(){
	var tabId =  getActiveTab();
	if(tabId==null){
		return null;
	}
	var iframe = document.getElementById('tab'+tabId);
	if(iframe==null){
		iframe = parent.document.getElementById('tab'+tabId);
	}
	if(iframe==null){
		iframe = parent.parent.document.getElementById('tab'+tabId);
	}
	return iframe;
}
function getActiveTab(){
	var tabId = null;
	try{
		tabId =  Maiaworkspace.tabid;
    }catch(Err){
    	try{
    		tabId =  parent.window.Maiaworkspace.tabid;
    	}catch(Err){
    		try{
        		tabId =  parent.parent.window.Maiaworkspace.tabid;
        	}catch(Err){}
    	}
    }			        	
	return tabId;
	
}
function queryController(){
	if (typeof queryFunc == 'function') {
		queryFunc(); 
	}else{
		try{ getActiveFrame().contentWindow.queryFunc(); }catch(Err){}
	}
}
function previousController(){
	if (typeof previousFunc == 'function') {
		previousFunc(); 
	}else{
		try{ getActiveFrame().contentWindow.previousFunc(); }catch(Err){}
	}
}
function nextController(){
	if (typeof nextFunc == 'function') {
		nextFunc(); 
	}else{
		try{ getActiveFrame().contentWindow.nextFunc(); }catch(Err){}
	}
}
function saveController(){
	if(typeof saveFuncChangePassword == 'function'){
		saveFuncChangePassword();
	}
	else if (typeof saveFunc == 'function') {
		saveFunc();
	}else{
		try{ getActiveFrame().contentWindow.saveFunc(); }catch(Err){}
	}
}
function reloadTab(){
	try{ getActiveFrame().src = getActiveFrame().src; }catch(Err){}
}
function showHideTransactionInputs(){
	var panelMenu = document.getElementById('transactionMenuPanel');
	if(panelMenu==null){
		panelMenu = parent.document.getElementById('transactionMenuPanel');
	}
	var module = document.getElementById('menuJsonForm:tmodule');
	if(module==null){
		module = parent.document.getElementById('menuJsonForm:tmodule');
	}
	$(panelMenu).toggle();
	$(module).focus();
}
function helpController(){
	if (typeof helpFunc == 'function') {
		helpFunc(); 
	}else{
		try{ parent.window.helpFunc();}catch(Err){}
	}
}
function block(){
	$(getActiveFrame()).contents().find(".m-filters, .m-controls, .m-data-content, .m-accordion-detail").find(".m-lov-button").prop("disabled", true).addClass('ui-state-disabled');
	$(getActiveFrame()).contents().find(".m-filters, .m-controls, .m-data-content, .m-accordion-detail").find(".ui-datepicker-trigger").prop("disabled", true).addClass('ui-state-disabled');
	$(getActiveFrame()).contents().find(".m-filters, .m-controls, .m-data-content, .m-accordion-detail").find(".ui-chkbox-box").prop("disabled", true).addClass('ui-state-disabled');
}




       
