// JavaScript Document
function formatarCampos(objForm, strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	
	if(window.event) { // Internet Explorer
		nTecla = evtKeyPress.keyCode; 
		//alert("ie");		
	}
	else if(evtKeyPress.which) { // Nestcape
		nTecla = evtKeyPress.which;
		//alert("netscape");
	}

	sValue = objForm[strField].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( ":", "" );
	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) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		bolMask = bolMask || (sMask.charAt(i) == ":")

		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++; 
		}
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	objForm[strField].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;
		} 
	}
	else {
		return true;
	}
} 

	function validarEmail(mail){
		var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
		if(typeof(mail) == "string"){
			if(er.test(mail)){ return true; }
		}else if(typeof(mail) == "object"){
			if(er.test(mail.value)){ 
						return true; 
					}
		}else{
			return false;
		}
	}


	function validarData(campo){
		var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
		var msgErro = 'Formato inválido de data.';
		if ((campo.value.match(expReg)) && (campo.value!='')){
			var dia = campo.value.substring(0,2);
			
			var mes = campo.value.substring(3,5);
			var ano = campo.value.substring(6,10);
			var teste =  dia.substring(0,1)
			
			
			if(teste == 0){
				dia = dia.substring(1,1);	
			}
			
			if((mes==4 || mes==6 || mes==9 || mes==11) && (dia > 30)){
				alert("Dia incorreto !!! O mês especificado contém no máximo 30 dias.");
				return false;
			} else{
				if(ano%4!=0 && mes==2 && dia>28){
				alert("Data incorreta!! O mês especificado contém no máximo 28 dias.");
				return false;
				} else{
					if(ano%4==0 && mes==2 && dia>29){
					alert("Data incorreta!! O mês especificado contém no máximo 29 dias.");
					return false;
					} else
						{
							//alert ("Data correta!");
							return true;
						}
				}
			}} else {
		alert(msgErro);
		campo.focus();
		return false;
		}
	}
	
	  
  function somente_numero(campo){
	  var digits="0123456789"
	  var campo_temp
	  for (var i=0;i<campo.value.length;i++){
		campo_temp=campo.value.substring(i,i+1)    
		if (digits.indexOf(campo_temp)==-1){
			  campo.value = campo.value.substring(0,i);
			  break;
		 }
	  }
  }
function MascaraMoeda(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(window.event) { // Internet Explorer
		whichCode = e.keyCode; 
		//alert("ie");		
	}
	else if(evtKeyPress.which) { // Nestcape
		whichCode = e.which;
		//alert("netscape");
	}	
    if (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;
}

