
//////////////////////////////////////////////////////////////////////////////////////////
////////				Uinet 1.0 [Update Information Internet ]				//////////
////////					Webroom Soluções Interativa							//////////
////////						A internet sob Medida							//////////
////////					http://www.webroom.com.br							//////////
////////					email:: webroom@webroom.com.br						//////////
//////////////////////////////////////////////////////////////////////////////////////////
////////				Criado por   : Flávio Theruo Kaminisse					//////////
////////					Data Criação : 28/09/2008							//////////
////////																		//////////
////////				- Compatível com MSIE, Firefox e Opera.					//////////
//////////////////////////////////////////////////////////////////////////////////////////

var $a;
//Mascara utilizada para letras
var mascaraAlpha = "a";
//Mascara utilizada para numeros
var mascaraNum = "n";
//Mascara utilizada para letras e numeros
var mascaraAlphaNum = "x";
//Variavel utilizada para pegar a posicao
//do cursor em um elemento de texto
var posGlobalCursor;

function WBR_Formata_OnImput(event,$b) {
	WBR_ValidaConteudo($b);
	WBR_FormataSeparador($b,WBR_GetInicioSelecao($b));
};

function WBR_ValidaConteudo($b) {
	var $c = "";
	var $d = $b.value;
	var $e = $b.getAttribute("mask");
	var $f = mascaraAlpha;
	var $g = mascaraNum;
	var $h = mascaraAlphaNum;
	var $i = $b.getAttribute("maskDisplay");
	for ( i = 0; i < $e.length; i++ ) {
		if ( $e.substring( i,( i + 1 ) ) == $f ) {
			while ( $d.length > 0 && ( !( ( $d.substring(0,1).charCodeAt(0) >= 65 && $d.substring(0,1).charCodeAt(0) <= 90 ) || ( $d.substring(0,1).charCodeAt(0) >= 97 && $d.substring(0,1).charCodeAt(0) <= 122 ) ) ) ) {
				$d=$d.substring(1);
			};
			if ( $d.length > 0 ) {
				$c += $d.substring(0,1);
				$d = $d.substring(1);
			}
			else {
				$c += $i;
			}
		}
		else if ( $e.substring(i,(i+1)) == $g ) {
			while ( $d.length > 0 && ( !( $d.substring(0,1).charCodeAt(0) >= 48 && $d.substring(0,1).charCodeAt(0) <= 57 ) ) ) {
				$d = $d.substring(1);
			};
			if ( $d.length > 0 ) {
				$c += $d.substring(0,1);
				$d = $d.substring(1);
			}
			else {
				$c+=$i;
			}
		}
		else if ( $e.substring(i,(i+1)) == $h ) {
			while ( $d.length > 0 && ( !( ( $d.substring(0,1).charCodeAt(0) >= 65 && $d.substring(0,1).charCodeAt(0) <= 90 ) || ( $d.substring(0,1).charCodeAt(0) >= 97 && $d.substring(0,1).charCodeAt(0) <= 122 ) || ( $d.substring(0,1).charCodeAt(0) >= 48 && $d.substring(0,1).charCodeAt(0) <= 57 ) ) ) ) {
				$d = $d.substring(1);
			};
			
			if ( $d.length > 0 ) {
				$c += $d.substring(0,1);
				$d = $d.substring(1);
			}
			else {
				$c += $i;
			}
		}
		else {
			$c += $e.substring(i,(i+1));
		}
	};
	$b.value = $c;	
};

function WBR_Valida_OnBlur($j,$b) {
	if ( $b == null ) {
		$b = $a;
	};
	WBR_FormataSeparador($b,$j);
};

function WBR_Formata_OnPaste($b) {
	var $d = window.clipboardData.getData("Text");
	var $j = WBR_MontaMascara($b,$d);
	$a=$b;
	window.setTimeout("WBR_Valida_OnBlur("+$j+")",10);
	WBR_StopEvent(event);
};

function WBR_Formata_OnCut($b) {
	var $k = WBR_GetInicioSelecao($b);
	var $l = WBR_GetSelectionEnd($b);
	if ( $k + $l == $b.value.length ) {
		window.clipboardData.setData("Text",$b.value);
		$b.value="";
		WBR_Formata_OnFocus($b,$msg);
	}
	else {
		var $i = $b.getAttribute("maskDisplay");
		window.clipboardData.setData("Text",$b.value.substring($k,($k+1)));
		WBR_UpdateChar($b,$k,$i);
		WBR_FormataSeparador($b,$k);
	};
	WBR_StopEvent(event);
};

function WBR_MontaMascara($b,$d) {
	var $c = "";
	if ( $d.length > 0 ) {
		var $e = $b.getAttribute("mask");
		var $f = mascaraAlpha;
		var $g = mascaraNum;
		var $h = mascaraAlphaNum;
		var $i = $b.getAttribute("maskDisplay");
		var $m = WBR_GetInicioSelecao($b);
		$c += $b.value.substring(0,$m);
		for ( i = $m; i < $e.length; i++ ) {
			if ( $e.substring(i,(i+1))==$f ) {
				while ( $d.length > 0 && ( !( ( $d.substring(0,1).charCodeAt(0) >= 65 && $d.substring(0,1).charCodeAt(0) <= 90 ) || ( $d.substring(0,1).charCodeAt(0) >= 97 && $d.substring(0,1).charCodeAt(0) <= 122 ) ) ) ) {
					$d = $d.substring(1);
				};
				if ( $d.length > 0 ) {
					$c += $d.substring(0,1);
					$d = $d.substring(1);
				}
				else {
					break;
				}
			}
			else if ( $e.substring( i,( i + 1 ) ) == $g) {
				while ( $d.length > 0 && ( !( $d.substring(0,1).charCodeAt(0) >= 48 && $d.substring(0,1).charCodeAt(0) <= 57 ) ) ) {
					$d = $d.substring(1);
				};
				if ( $d.length > 0 ) {
					$c += $d.substring(0,1);
					$d = $d.substring(1);
				}
				else {
					break;
				}
			}
			else if ( $e.substring( i,( i + 1 ) ) == $h ) {
				while ( $d.length > 0 && ( !( ( $d.substring(0,1).charCodeAt(0) >= 65 && $d.substring(0,1).charCodeAt(0) <= 90 ) || ( $d.substring(0,1).charCodeAt(0) >= 97 && $d.substring(0,1).charCodeAt(0) <= 122 ) || ( $d.substring(0,1).charCodeAt(0) >= 48 && $d.substring(0,1).charCodeAt(0) <= 57 ) ) ) ) {
					$d = $d.substring(1);
				};
				if ( $d.length > 0 ) {
					$c += $d.substring(0,1);
					$d = $d.substring(1);
				}
				else {
					break;
				}
			}
			else {
				$c += $e.substring(i,(i+1));
			}
		};
		$c += $b.value.substring(i,$e.length);
	};
	$b.value = $c;
	return i;
};

function WBR_PerdeFoco($b) {
	var $e = $b.getAttribute("mask");
	if ( $e != null && $b.value == WBR_GetDisplayMask($b,true) ) {
		$b.value="";
	}
	else {
		if ( $b.value != null && $b.value.length > 0 ) {
			var $n = $b.getAttribute("RegexPattern");
			var $o = $b.getAttribute("OnRegexMatch");
			var $p = $b.getAttribute("OnRegexNoMatch");
			if ( $n != null && $n.length > 0 ) {
				var re = new RegExp($n);
				if ( $b.value.match(re) ) {
					if ( $o != null && $o.length > 0) {
						eval($o);
					}
				}
				else {
					if ( $p != null && $p.length > 0 ) {
						eval($p);
					}
				}
			}
		}
	}
};

function WBR_Formata_OnKeyDown(event,$b) {
	var $e = $b.getAttribute("mask");
	if ( $e != null && $e.length > 0 ) {
		var k=WBR_GetKeyCode(event);
		if ( k == 8 || ( k >= 33 && k <= 40 ) || k == 46 ) {
			switch ( k ) {
				case 8  : WBR_KeyBackspace($b); break;
				case 33 : WBR_PushPosBegin($b); break;
				case 34 : WBR_PushPosEnd($b); break;
				case 35 : WBR_PushPosEnd($b); break;
				case 36 : WBR_PushPosBegin($b); break;
				case 37 : WBR_PushPosLeft($b); break;
				case 38 : WBR_PushPosLeft($b); break;
				case 39 : WBR_PushPosRight($b); break;
				case 40 : WBR_PushPosRight($b); break;
				case 46 : WBR_KeyDelete($b); break;
			};
			WBR_StopEvent(event);
		}
	}
};

function WBR_Formata_OnKeyPress(event,$b) {
	var $e = $b.getAttribute("mask");
	if ( $e != null && $e.length > 0 ) {
		var kc=WBR_GetKeyCode(event);
		var ss=WBR_GetInicioSelecao($b);
		if ( kc != 9 ) {
			if ( WBR_KeyIsValid($b,ss,kc) ) {
				var ks = String.fromCharCode(kc);
				WBR_UpdateChar($b,ss,ks);
				WBR_FormataSeparador($b,ss+1);
			}
			else {
				var $q = null;
				if ( $q != null && $q.length > 0) {
					eval($q);
				}
			};
			WBR_StopEvent(event);
		}
	}
};

function WBR_StopEvent(event) {
	if ( document.all ) {
		event.returnValue = false;
	}
	else if ( event.preventDefault ) {
		event.preventDefault();
	}
};

function WBR_StopEventPropagation(event) {
	event.cancelBubble = true;
	if ( event.stopPropagation ) {
		event.stopPropagation();
	}
};

function WBR_Formata_OnFocus($b,$msg) {
	var $e = $b.getAttribute("mask");
	//Como ao se formatar utiliza-se o evento OnFocus, perde-se a ajuda. Está foi
	//uma forma alternativa de exibir a ajuda.
	var $ajuda = $b.getAttribute("ajuda");
	if($ajuda !=null && $ajuda.length > 0){
	    var aux = "help_"+$b.id;
		document.getElementById(aux).innerHTML = $b.getAttribute("ajuda");
	}
	
	if ( $e != null && $e.length > 0 ) {
		var $r = WBR_GetInicioSelecao($b);
		var $j = WBR_GetValidPos($b,$r);
		if ( $j != -1 ) {
			if ( $b.value == null || $b.value.length == 0 ) {
				$b.value = WBR_GetDisplayMask($b,true);
			};
			WBR_FormataSeparador($b,$j);
		}
		else {
			$b.blur();
		}
	}
};

function WBR_Formata_OnClick(event,$b) {
	var $e = $b.getAttribute("mask");
	if ( $e != null && $e.length > 0 ) {
		var $s = WBR_GetInicioSelecao($b);
		WBR_FormataSeparador($b,$s);
	};
	WBR_StopEventPropagation(event);
};

function WBR_GetKeyCode(event){
	return ( event.keyCode ? event.keyCode : event.which ? event.which : event.charCode );
};

function WBR_KeyDelete($b) {
	var $k = WBR_GetInicioSelecao($b);
	var $l = WBR_GetSelectionEnd($b);
	if ( $k + $l == $b.value.length ) {
		$b.value = "";
		WBR_Formata_OnFocus($b);
	}
	else {
		var $i = $b.getAttribute("maskDisplay");
		WBR_UpdateChar($b,$k,$i);
		WBR_FormataSeparador($b,$k+1);
	}
};

function WBR_KeyBackspace($b) {
	var $k = WBR_GetInicioSelecao($b);
	var $l = WBR_GetSelectionEnd($b);
	if ( $k + $l == $b.value.length ) {
		$b.value = "";
		WBR_Formata_OnFocus($b);
	}
	else {
		var $i = $b.getAttribute("maskDisplay");
		WBR_UpdateChar($b,$k,$i);
		$j = WBR_GetValidPos($b,$k-1,true);
		WBR_FormataSeparador($b,$j);
	}
};

function WBR_PushPosLeft($b) {
	var k = WBR_GetInicioSelecao($b);
	if ( ( k -1 ) >= 0 ) {
		WBR_FormataSeparador($b,(k-1),true);
	}
};

function WBR_PushPosRight($b) {
	var k = WBR_GetInicioSelecao($b);
	if ( ( k + 1 ) < $b.value.length ) {
		WBR_FormataSeparador($b,(k+1));
	}
};

function WBR_PushPosBegin($b) {
	WBR_FormataSeparador($b,0);
};

function WBR_PushPosEnd($b) {
	WBR_FormataSeparador($b,$b.getAttribute("mask").length);
};

function WBR_UpdateChar($b,$j,ks) {
	var x = $b.value;
	var $t = x.substring(0,$j);
	var $u = x.substring($j+1,x.length);
	$b.value = $t + ks + $u;
};

function WBR_FormataSeparador($b,$j,$v) {
	if ( $j <= 0 ) {
		$j = 0;
	};
	if ( $j >= $b.value.length - 1 ) {
		$j = $b.value.length-1
	};
	$j = WBR_GetValidPos($b,$j,$v);
	if ( $j != -1 ) {
		if ( $b.createTextRange ) {
			var $w=$b.createTextRange();
			$w.moveStart("character",$j);
			$w.moveEnd('character',$j+1-$b.value.length);
			$w.select();
		}
		else if ( $b.setSelectionRange ) {
			$b.focus();
			$b.setSelectionRange($j,$j+1);
		}
	}
	else {
		$b.blur();
	}
};

function WBR_GetValidPos($b,$j,$v) {
	if ( $v == null ) {
		$v=false;
	};
	if ( WBR_PosIsValid($b,$j) ) {
		return $j;
	}
	else {
		var $e = $b.getAttribute("mask");
		if ( $v ) {
			while ( $j >= 0 ) {
				if ( WBR_PosIsValid($b,$j) ) {
					return $j;
				};
				$j--;
			};
			while ( $j < $e.length - 1 ) {
				if ( WBR_PosIsValid($b,$j) ) {
					return $j;
				};
				$j++;
			}
		}
		else {
			while ( $j < $e.length - 1 ) {
				if ( WBR_PosIsValid($b,$j) ) {
					return $j;
				};
				$j++;
			};
			while ( $j >= 0 ) {
				if ( WBR_PosIsValid($b,$j) ) {
					return $j;
				};
				$j--;
			}
		};
		return -1;
	}
};

function WBR_PosIsValid($b,$j) {
	var $e = $b.getAttribute("mask");
	var m = $e.split("");
	if ( m.length > $j ) {
		if ( m[$j] != null ) {
			var $f = mascaraAlpha;
			var $g = mascaraNum;
			var $h = mascaraAlphaNum;
			if ( m[$j] == $f || m[$j] == $g || m[$j] == $h ) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
};

function WBR_KeyIsValid($b,$j,$x) {
	var m = $b.getAttribute("mask").split("");
	if ( m.length > $j ) {
		var $y = m[$j];
		var $z = mascaraAlpha;
		var $A = mascaraNum;
		var $B = mascaraAlphaNum;
		if ( $y == $z ) {
			if ( ( $x >= 65 && $x <= 90 ) || ( $x >= 97 && $x <= 122 ) ) {
				return true;
			}
			else {
				return false;
			}
		}
		else if ( $y == $A ) {
			if ( $x >= 48 && $x <= 57 ) {
				return true;
			}
			else {
				return false;
			}
		}
		else if ( $y == $B ) {
			if ( ( $x >= 48 && $x <= 57 ) || ( $x >= 65 && $x <= 90 ) || ( $x >= 97 && $x <= 122 ) ) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
};

function WBR_GetInicioSelecao($b) {
	if ($b.createTextRange ) {
		$C = document.selection.createRange().duplicate();
		$C.moveEnd("character",$b.value.length);
		$j = $b.value.lastIndexOf($C.text);
		if ( $C.text == "" )
			$j = $b.value.length;
		return $j;
	}
	else {
		return $b.selectionStart;
	}
};

function WBR_GetSelectionEnd($b) {
	if ( $b.createTextRange ) {
		$C = document.selection.createRange().duplicate();
		$C.moveStart("character",-$b.value.length);
		$j = $C.text.length;
		return $j;
	}
	else {
		return $b.selectionEnd;
	}
};

function WBR_GetDisplayMask($b,$D) {
	var $e = $b.getAttribute("mask");
	if ( $D == true ) {
		var f = "";
		var $f = mascaraAlpha;
		var $g = mascaraNum;
		var $h = mascaraAlphaNum;
		var $i = $b.getAttribute("maskDisplay");
		var m = $e.split("");
		for ( mi = 0; mi < m.length; mi++ ) {
			if ( m[mi] == $f || m[mi] == $g || m[mi] == $h ) {
				f += $i;
			}
			else {
				f += m[mi];
			}
		};
		return f;
	}
	else {
		return $e;
	}
}

function formata_valor(campo_valor) {
	var valor = campo_valor.value;
	var nro_formatado = "";
	var count_virgula = 0;
	var count_ponto = 0;
	var coloquei_virgula = false;
	for ( i = valor.length - 1; i >= 0; i-- ) {
		if ( ( valor.charAt(i) >= "0" && valor.charAt(i) <= "9" ) || valor.charAt(i) == "-" ) {
			if ( valor.charAt(i) != "-" ) {
				if ( count_virgula == 2 && !coloquei_virgula ) {
					nro_formatado = "," + nro_formatado;
					coloquei_virgula = true;
				} //if
				if ( count_ponto != 0 && ( count_ponto % 3 ) == 0 )
					nro_formatado = "." + nro_formatado;
				if ( coloquei_virgula )
					count_ponto++;
				else
					count_virgula++;
			} //if ( valor.charAt(i) != "-" )
			nro_formatado = valor.charAt(i) + nro_formatado;
		} //if ( ( valor.charAt(i) >= "0" && valor.charAt(i) <= "9" ) || valor.charAt(i) == "-" )
	} //Fim do for
	campo_valor.value = nro_formatado;
}

function formata_campo(objeto, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen, bolMask, sCod, nTecla;
	
	if ( document.all ) { // Internet Explorer
		nTecla = evtKeyPress.keyCode;
	} //if
	else { // Nestcape
		nTecla = evtKeyPress.which;
	}
	
	sValue = objeto.value;
	
	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( ":", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;
	
	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;
	
	while ( i <= mskLen ) {
		bolMask = ((sMask.charAt(i) == ":") || (sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		
		if ( bolMask ) {
			sCod += sMask.charAt(i);
			mskLen++;
		} //if
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		} //Fim do else
		i++;
	} //Fim do while
	
	objeto.value = sCod;
	
	if ( nTecla != 8 ) { // backspace
		if ( sMask.charAt(i-1) == "9" ) { // apenas números...
			return ((nTecla > 47) && (nTecla < 58));
		} // números de 0 a 9
		else { // qualquer caracter...
			return true;
		} //Fim do else
	} //if
	else {
		return true;
	} //Fim do else
}

//Atribui a posição atual do cursor para a variável posGlobalCursor
//em cada alteração que ocorrer na textarea
function setPosCursor( campo ) {
	posGlobalCursor = getPosCursor( campo );
}

//Função que retorna a posição atual do cursor no elemento passado a ela
function getPosCursor( textElement ) {
	
	var sOldText = textElement.value;
	
	var objRange = document.selection.createRange();
	var sOldRange = objRange.text;
	
	var sWeirdString = '#%~';
	
	objRange.text = sOldRange + sWeirdString;
	objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));
	
	var sNewText = textElement.value;
	
	objRange.text = sOldRange;
	
	for ( i = 0; i <= sNewText.length; i++ ) {
		var sTemp = sNewText.substring(i, i + sWeirdString.length);
		if ( sTemp == sWeirdString ) {
			var cursorPos = ( i - sOldRange.length );
			return cursorPos;
		} //if
	} //Fim do for
}

function auto_tab(campo1, tamanho, campo2) {
	var tam_campo1 = document.getElementById(campo1).value.length;
	if ( tam_campo1 >= tamanho )
		document.getElementById(campo2).focus();
}

