var Maia = {
	focus : function(id){
		if(id!= null && id.trim().length>0){
			id=this.cleanId(id);
    		this.privateFocus(id, null);
		}
    },
    focusContainer : function(id){  	
    	if(id!= null && id.trim().length>0){
    		id=this.cleanId(id);
    		this.privateFocus(null, id);
    	}
    },
    privateFocus: function(id, context){
    	var selector=":input:visible:enabled";
		setTimeout(function(){
				if(id){
					var jq=$(PrimeFaces.escapeClientId(id)); if(jq.is(selector)){jq.focus();} else{jq.find(selector).eq(0).focus();}
				}else{
					if(context){$(PrimeFaces.escapeClientId(context)).find(selector).eq(0).focus();}else{$(selector).eq(0).focus();}
				}
		},250);
    },
    cleanId: function(id){
    	var c = id.substring(0,1);
    	if(c==":"){id = id.substring(1,id.length);}
		id = id.split("\\:").join(":");
		return id;
    },
	doGetCaretPosition: function(oField) {
        var iCaretPos = 0;
        if (document.selection) // IE Support
            {
            oField.focus();
            var oSel = document.selection.createRange();
            // Move selection start to 0 position
            oSel.moveStart ('character', -oField.value.length);
            // The caret position is selection length
            iCaretPos = oSel.text.length;
            }
        else
            if (oField.selectionStart || oField.selectionStart == '0') // Firefox support
                iCaretPos = oField.selectionStart;
        return (iCaretPos);
    },
    doSetCaretPosition: function (oField, iCaretPos)
        {
        if (document.selection) // IE Support
            {
            oField.focus();
            var oSel = document.selection.createRange();
            oSel.moveStart('character', -oField.value.length);
            oSel.moveStart('character', iCaretPos);
            oSel.moveEnd('character', 0);
            oSel.select();
            }
        else
            if (oField.selectionStart || oField.selectionStart == '0') // Firefox support
                {
                oField.selectionStart = iCaretPos;
                oField.selectionEnd = iCaretPos;
                oField.focus();
                }
    },
    forceupper: function(event, element){
    	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(k==8 || k==16 || k==39 || k==37 || e.shiftKey){
        	return true;
		}
        
    	$(element).addClass("m-upper-case");
	    var x = this.doGetCaretPosition(element);
	    element.value=element.value.toUpperCase();
	    this.doSetCaretPosition(element,x);
    },
    keyDownUpperLogin: function(event, element){
    	var e = event || window.event;    	
    	var k = null;
        if(e.which) k = e.which;
        else if(e.keyCode) k = e.keyCode;
        
        if (k == 9) {
	    	var element = document.activeElement;
	    	if($(element).hasClass("m-upper-case")){
	    		element.value=element.value.toUpperCase();
	    	}
	    	return true;	
		}
    },
    validateip: function(event, element)  {    	
    	var k = null;
    	var e = event || window.event; 
    	if(e.which) k = e.which;
        else if(e.keyCode) k = e.keyCode;
    	
    	if(k!=9 && k!=13){
    		//return false;
    	}

        //See if x looks like an IP address using our "almost IP regex".
        var regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
        var match = regex.exec(element.value);
        if (!match) {
        	e.preventDefault();
        	element.value = null;
    		element.focus();
    		$(element).addClass("ui-state-error");
        	return false;
        }
        // Additional code to check that the octets aren't greater than 255:
        for (var i = 1; i <= 4; ++i) {
            if (parseInt(match[i]) > 255){
            	e.preventDefault();
            	element.value = null;
        		element.focus();  
        		$(element).addClass("ui-state-error");
                return false;
            }
        }
        $(element).removeClass("ui-state-error");
        return true;
    },
    refreshusertasks: function(){
    	parent.window.inboxTransactions([{name:'tabDiary',value:0}]);return true;
    },
    loadPageUtil: function(){
    	//var context = parent.window.Maiaworkspace.getContext();
    	parent.window.Maiaworkspace.loadPage('/pages/initialtab.xhtml','Opci\u00f3n',null);
    	
    }
	
};