String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if (((evt.keyCode == 13) && (node.type=="text")) ) {return false;} //Enter

}

function preventEvent(e, number) {
	var evt = e || window.event;
	return (evt.keyCode == number); 
}

var textAreaMaxLenght = 500;
function countTextArea(obj, counter, length) {
	  
	if ( !length ) {
		length = textAreaMaxLenght;
	}
	
	  if ( obj.value.length >= length ) {
	  	obj.value = obj.value.substring(0, length);
	  }
	  
	  var leftObj = document.getElementById(counter);
	  leftObj.innerHTML = length - obj.value.length;
	  
}
document.onkeypress = stopRKey;

// Prevents double action 
var waitDialogShown = false;
var useTimerBeforeShowWaitDialog = true;
var waitDialogTimeout = 50;
var waitDialogTimer;

function showWaitDialog() {
    //avoid attempt to show it if it is already shown
    if (!waitDialogShown) {
        Richfaces.showModalPanel('wait-dialog-invisible');
        waitDialogShown = true;
    }
}

function onRequestStart() {
    if (useTimerBeforeShowWaitDialog) {
        waitDialogTimer = setTimeout("showWaitDialog();", waitDialogTimeout);
    } else {
        showWaitDialog();
    }
}
function onRequestEnd() {
    if (waitDialogShown) {
        Richfaces.hideModalPanel('wait-dialog-invisible');
        waitDialogShown = false;
    } else if (useTimerBeforeShowWaitDialog && waitDialogTimer) {
        clearTimeout(waitDialogTimer);
    }
}

function addFieldToForms() {
	for ( var i = 0 ; i < document.forms.length; i++ ) {
		document.forms[i].appendChild(input);
	}
}

// Verificar uma forma de alterar a URL sem fazer refresh na pagina
function setHashKey(hash) {
	//parent.location.search = hash;	
}

function stripAlphaChars(pstrSource) {
	
	var m_strOut = new String(pstrSource); 

	m_strOut = m_strOut.replace(/[^0-9]/g, ''); 

    return m_strOut; 
}

function autoCompleteKeyUp(event) {
	if ( event.keyCode == 9 || event.keyCode == 13 ) return true;
	return false;
}

function restrictNonAlpha(e) {

	   var tecla;  
	   if (e.keyCode) { // IE  
	     tecla = e.keyCode;  
	   }  
	   else if (e.which) { // Firefox  
	     tecla = e.which;  
	   }  
	   else { // Sei lá! :P  
	     return false;  
	   }
	   var allowed = [35, 36, 37, 39, 46, 8];
	   
	   for ( var i = 0 ; i < allowed.length ; i++ ) {
		   if ( allowed [i] == tecla) {
			   return true;
		   }
	   }
	   
	   return !(tecla < 48 || tecla > 57);  
	
   // 
   //	return /\w/.test(String.fromCharCode(charCode));
}

function setfocus( field ){
	field.focus();
	
}

var NowDate = new Date();
//document.write('<META name="Expires" content="' + NowDate.toGMTString() + '">');

function dealWithValuePesquisa(object) {
	if ( object != null ) {
		object.value=stripAlphaChars(object.value);
	}
}

/**
*
*  Javascript string pad
*  http://www.webtoolkit.info/
*
**/

var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;
 
function pad(str, len, pad, dir) {
 
	if ( str == '') {
		return str;
	}

	if (typeof(len) == "undefined") { var len = 0; }
	if (typeof(pad) == "undefined") { var pad = ' '; }
	if (typeof(dir) == "undefined") { var dir = STR_PAD_LEFT; }
	
	if (len + 1 >= str.length) {
 
		switch (dir){
 
			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
			break;
 
			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
			break;
 
			default:
				str = str + Array(len + 1 - str.length).join(pad);
			break;
 
		} // switch
 
	}
 
	return str;
 
}

function validaCampoBiometria(nomeBeneficiario) {
	if (trim(nomeBeneficiario).length < 3 || trim(nomeBeneficiario) == '') {
		return false;
	}
	return true;
}
function trim(str){return str.replace(/^\s+|\s+$/g,"");}

function switchVisibility(id) {
	var obj = document.getElementById(id);
	alert(obj);
	obj.style.display = ( obj.style.display ) == 'hidden' ? 'block' : 'hidden';
	
}

function isIE9() {
    return parseInt(jQuery.browser.version, 10) == 9;
}


