/**
 * Classe JavaScript de máscaras de validação.
 * 
 * @package LIB
 * @subpackage JAVASCRIPT
 */
function Masks() {
	
	/**
	 * Máscara para formatação de datas.
	 * 
	 * EXEMPLO DE USO:
	 * 		<input type="text" name="data" id="data" onKeyPress="return masks.data(this, event)" />
	 * 
	 * @param object $obj Objeto HTML input text que conterá a data.
	 * @param object $e Evento JavaScript que representa a digitação do valor no campo.
	 * 
	 * @return string
	 */
	this.data = function(obj, e) {
		var strCheck = '0123456789';
		var whichCode = window.Event ? e.which : e.keyCode;
		var key = String.fromCharCode(whichCode);
		var valueField = obj.value;
		
		//Liberando teclas Enter(13), BackSpace(8), barra(47) e demais teclas de controle do teclado(0).
		if((whichCode == '0') || (whichCode == '8') || (whichCode == '13') || (whichCode == '47')) { return true; }
		
		//Caracteres não permitidos ou capaciadade máxima do campo atiginda.
		if((strCheck.indexOf(key) == -1) || (valueField.length == '10')) { return false; }
		
	    //Inserindo as barras da data.
	    if(valueField.length == '2') { obj.value = valueField + '/'; }
	    if(valueField.length == '5') { obj.value = valueField + '/'; }
	    return true;
	    //Inserindo as barras da data.
	}
	
	/**
	 * Máscara para formatação de CEPs.
	 * 
	 * EXEMPLO DE USO:
	 * 		<input type="text" name="data" id="data" onKeyPress="return masks.cep(this, event)" />
	 * 
	 * @param object $obj Objeto HTML input text que conterá a data.
	 * @param object $e Evento JavaScript que representa a digitação do valor no campo.
	 * 
	 * @return string
	 */
	this.cep = function(obj, e) {
		var strCheck = '0123456789';
		var whichCode = window.Event ? e.which : e.keyCode;
		var key = String.fromCharCode(whichCode);
		var valueField = obj.value;
		
		//Liberando teclas Enter(13), BackSpace(8), hífen(45) e demais teclas de controle do teclado(0).
		if((whichCode == '0') || (whichCode == '8') || (whichCode == '13') || (whichCode == '45')) { return true; }
		
		//Caracteres não permitidos ou capaciadade máxima do campo atiginda.
		if((strCheck.indexOf(key) == -1) || (valueField.length == '9')) { return false; }
		
	    //Inserindo as barras da data.
	    if(valueField.length == '5') { obj.value = valueField + '-'; }
	    return true;
	    //Inserindo as barras da data.
	}
	
	/**
	 * Máscara para formatação para CPFs.
	 * 
	 * EXEMPLO DE USO:
	 * 		<input type="text" name="data" id="data" onKeyPress="return masks.cpf(this, event)" />
	 * 
	 * @param object $obj Objeto HTML input text que conterá a data.
	 * @param object $e Evento JavaScript que representa a digitação do valor no campo.
	 * 
	 * @return string
	 */
	this.cpf = function(obj, e) {
		var strCheck = '0123456789';
		var whichCode = window.Event ? e.which : e.keyCode;
		var key = String.fromCharCode(whichCode);
		var valueField = obj.value;
		
		//Liberando teclas Enter(13), BackSpace(8), hífen(45), ponto(46) e demais teclas de controle do teclado(0).
		if((whichCode == '0') || (whichCode == '8') || (whichCode == '13') || (whichCode == '45') || (whichCode == '46')) { return true; }
		
		//Caracteres não permitidos ou capaciadade máxima do campo atiginda.
		if((strCheck.indexOf(key) == -1) || (valueField.length == '14')) { return false; }
		
	    //Inserindo as barras da data.
	    if(valueField.length == '3') { obj.value = valueField + '.'; }
	    if(valueField.length == '7') { obj.value = valueField + '.'; }
	    if(valueField.length == '11') { obj.value = valueField + '-'; }
	    return true;
	    //Inserindo as barras da data.
	}
	
	/**
	 * Máscara para formatação para telefone.
	 * 
	 * EXEMPLO DE USO:
	 * 		<input type="text" name="data" id="data" onKeyPress="return masks.telefone(this, event)" />
	 * 
	 * @param object $obj Objeto HTML input text que conterá a data.
	 * @param object $e Evento JavaScript que representa a digitação do valor no campo.
	 * 
	 * @return string
	 */
	this.telefone = function(obj, e) {
		var strCheck = '0123456789';
		var whichCode = window.Event ? e.which : e.keyCode;
		var key = String.fromCharCode(whichCode);
		var valueField = obj.value;
		
		//Liberando teclas Enter(13), BackSpace(8), hífen(45), Parênteses(40 e 41), espaço(32) e demais teclas de controle do teclado(0).
		if((whichCode == '0') || (whichCode == '8') || (whichCode == '13') || (whichCode == '32') || (whichCode == '41') || (whichCode == '42') || (whichCode == '45')) { return true; }
		
		//Caracteres não permitidos ou capaciadade máxima do campo atiginda.
		if((strCheck.indexOf(key) == -1) || (valueField.length == '14')) { return false; }
		
	    //Inserindo as barras da data.
	    if(valueField.length == '0') { obj.value = valueField + '('; }
	    if(valueField.length == '3') { obj.value = valueField + ') '; }
	    if(valueField.length == '9') { obj.value = valueField + '-'; }
	    return true;
	    //Inserindo as barras da data.
	}
	
	/**
	 * Máscara para formatação de números.
	 * 
	 * EXEMPLO DE USO:
	 * 		<input type="text" name="data" id="data" onKeyPress="return masks.numeric(this, event)" />
	 * 
	 * @param object $obj Objeto HTML input text que conterá a data.
	 * @param object $e Evento JavaScript que representa a digitação do valor no campo.
	 * 
	 * @return string
	 */
	this.numeric = function(obj, e) {
		var strCheck = '0123456789';
		var whichCode = window.Event ? e.which : e.keyCode;
		var key = String.fromCharCode(whichCode);
		var valueField = obj.value;
		
		//Liberando teclas Enter(13), BackSpace(8) e demais teclas de controle do teclado(0).
		if((whichCode == '0') || (whichCode == '8') || (whichCode == '13')) { return true; }
		
		//Caracteres não permitidos.
		if((strCheck.indexOf(key) == -1)) { return false; }
	    return true;
	}
	
	/**
	 * Informa limites de digitação em campos do tipo textarea em tempo de digitação.
	 * 
	 * @param object $txtarea Objeto HTML que representa o elemento textarea.
	 * @param string $digitado ID do objeto html que receberá a quantidade de caracteres digitados.
	 * @param string restante ID do objeto html que a quantidades de caracteres restantes.
	 */
	this.max_txt_area = function(txtarea, digitado, restante) {
		total = 250; 
		tam = txtarea.value.length; 
		str = ''; 
		str = str + tam; 
		document.getElementById(digitado).innerHTML = str; 
		document.getElementById(restante).innerHTML = total - str; 
		if(tam > total) { 
			aux = txtarea.value; 
			txtarea.value = aux.substring(0, total); 
			digitado.innerHTML = total;
			restante.innerHTML = 0;
		} 
	}
	
	/**
	 * Máscara para formatação monetária.
	 * 
	 * Exemplo de utilização:
	 *     <input type="text" name="valor" value="" onKeyPress="return(masks.MaskMoney(this,'.',',',event))" />
	 * 
	 * @author Gabriel Fróes.
	 * @author Leonardo Castilhos Thibes.
	 * 
	 * @param object $objTextBox Objeto HTML que representa o textbox.
	 * @param string $SeparadorMilesimo Caracter separador de milésimos.
	 * @param string $SeparadorDecimal Caracter separador de decimais.
	 * @param event $e Evento JavaScript.
	 * 
	 * @return bool
	 */
	this.MaskMoney = function(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
	    var sep = 0;
	    var key = '';
	    var i = j = 0;
	    var len = len2 = 0;
	    var strCheck = '0123456789';
	    var aux = aux2 = '';
	    var whichCode = (window.Event) ? e.which : e.keyCode;
	    
	    if (whichCode == 0 || whichCode == 8 || whichCode == 13) { return true; }
	    key = String.fromCharCode(whichCode); // Valor para o código da Chave
	    if (strCheck.indexOf(key) == -1) return false; // Chave inválida
	    len = objTextBox.value.length;
	    for(i = 0; i < len; i++)
	        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
	    aux = '';
	    
	    for(; i < len; i++)
	        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
	    aux += key;
	    len = aux.length;
	    if (len == 0) objTextBox.value = '';
	    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
	    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
	    if (len > 2) {
	        aux2 = '';
	        for (j = 0, i = len - 3; i >= 0; i--) {
	            if (j == 3) {
	                aux2 += SeparadorMilesimo;
	                j = 0;
	            }
	            aux2 += aux.charAt(i);
	            j++;
	        }
	        objTextBox.value = '';
	        len2 = aux2.length;
	        for (i = len2 - 1; i >= 0; i--)
	        objTextBox.value += aux2.charAt(i);
	        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
	    }
	    return false;
	}
	
}

/** Instanciando a classe **/
var masks = new Masks();