function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.value.length)
		if (r.text == '') return o.value.length
		return o.value.lastIndexOf(r.text)
	} else {
		return o.selectionStart;
	}
}



/***********************************************************

	VALIDADORES DE CEP

***********************************************************/

function focusCEP (campo, evento) {
	if ((campo.value == "") || (campo.value == "-")){
		campo.value = "_____-___";
	}
	if(campo.createTextRange) {
		var range = campo.createTextRange();
        range.move("character", 0);
        range.select();
    } else {
	   campo.setSelectionRange(0, 0);
    }

}

function blurCEP () {
	if (form1.cep.value == "_____-___") {
		form1.cep.value = "";
	}
}

function keyPressCEP(campo, evento) {
	campo.style.color='#0000ff';
	var pos = getSelectionStart(campo);
	var pos_orig = pos - 1;
	if(window.event) {
	    var tecla = evento.keyCode;
	} else {
		tecla = evento.which;
	}

	var parada = true;
	var mascara = "_____-___";
	var retorno = 0;

	var ss = campo.value;
	var s = "";
	var ch = "";


	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch == "_") || ((ch >= "0") && (ch <= "9"))) {
			 s += ss.charAt(i);
		} else {
			if (pos > i) {
				pos--;
			}
		}
	}
	campo.value = s;

	var c = String.fromCharCode(tecla);

	// BACKSPACE
	if (tecla == 8) {
		if (pos > 0) {
			campo.value = s.substring (0, pos - 1);
			campo.value += "_";
			campo.value += s.substring (pos);
			pos--;
		}
	}
	if (tecla == 9) {
		parada = false;
	}
	if (tecla == 35)
		pos = campo.value.length;
	if (tecla == 36)
		pos = 0;
	if (tecla == 37)
		pos--;
	if (tecla == 39)
		pos++;
	// DELETE
	if (tecla == 46 ) {
		if (pos < campo.value.length) {
			campo.value = s.substring (0, pos);
			campo.value += "_";
			campo.value += s.substring (pos + 1);
			pos++;
		}
	}
	if (((tecla >= 48) && (tecla <= 57)) || ((tecla >= 96) && (tecla <= 105))) {
		if (pos < campo.value.length) {
			if (tecla >= 96) tecla -= 48;
			campo.value = s.substring (0, pos);
			campo.value +=  String.fromCharCode(tecla);
			campo.value += s.substring (pos + 1);
			pos++;
		}
	}

	if (pos < 0)  pos = 0;
	if (pos > campo.value.length) {pos = campo.value.length; }

	s = "";
	ss = campo.value;
	var j = 0;

	for (var i = 0; i < mascara.length; i++) {
		ch = mascara.charAt(i);
		if (ch == "_") {
			s += ss.charAt(j);
			j++;
		} else {
			s += ch;
			if (pos > i) pos++;
		}
	}
	campo.value = s;

	if(campo.createTextRange) {
		var range = campo.createTextRange();
        range.move("character", pos);
        range.select();
    } else {
	   campo.setSelectionRange(pos, pos);
    }
	if (parada) {
		return false;
	} else {
		return true;
	}
}




/***********************************************************

	VALIDADORES DE CNPJ

***********************************************************/

function focusCPNJ (campo, evento) {
	var ss = campo.value;
	var s = "";
	var ch = "";


	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch == "_") || ((ch >= "0") && (ch <= "9"))) {
			 s += ss.charAt(i);
		}
	}
	while (s.length < 14) {
		s += "_";
	}
	campo.value = s;
	if (campo.createTextRange) {
		var range = campo.createTextRange();
        range.move("character", 0);
        range.select();
    } else {
	   campo.setSelectionRange(0, 0);
    }
}

function blurCNPJ (campo) {
	if (campo.value == "______________") {
		campo.value = "";
	} else {
		if (!valida_cnpj(campo.value)) {
			alert("CNPJ Inválido");
			campo.focus();
			campo.select();
		} else {
			campo.value = campo.value.substring(0,2) + "." +
				campo.value.substring(2,5) + "." +
				campo.value.substring(5,8) + "/" +
				campo.value.substring(8,12) + "-" +
				campo.value.substring(12,14);
		}
	}
}

function keyPressCNPJ(campo, evento) {
	campo.style.color='#0000ff';
	var pos = getSelectionStart(campo);
	var pos_orig = pos - 1;
	if(window.event) {
	    var tecla = evento.keyCode;
	} else {
		tecla = evento.which;
	}

	var parada = true;
	var mascara = "______________";
	var retorno = 0;

	var ss = campo.value;
	var s = "";
	var ch = "";


	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch == "_") || ((ch >= "0") && (ch <= "9"))) {
			 s += ss.charAt(i);
		} else {
			if (pos > i) {
				pos--;
			}
		}
	}
	campo.value = s;

	var c = String.fromCharCode(tecla);


	if (tecla == 8) { // BACKSPACE
		if (pos > 0) {
			campo.value = s.substring (0, pos - 1);
			campo.value += "_";
			campo.value += s.substring (pos);
			pos--;
		}
	}
	if (tecla == 9) {  // TAB
		parada = false;
	}
	if (tecla == 35) // END
		pos = campo.value.length;
	if (tecla == 36) // HOME
		pos = 0;
	if (tecla == 37) // SETA ESQUERDA
		pos--;
	if (tecla == 39) // SETA DIREITA
		pos++;
	// DELETE
	if (tecla == 46 ) {
		if (pos < campo.value.length) {
			campo.value = s.substring (0, pos);
			campo.value += "_";
			campo.value += s.substring (pos + 1);
			pos++;
		}
	}
	if (((tecla >= 48) && (tecla <= 57)) || ((tecla >= 96) && (tecla <= 105))) { // DIGITO
		if (pos < campo.value.length) {
			if (tecla >= 96) tecla -= 48;
			campo.value = s.substring (0, pos);
			campo.value +=  String.fromCharCode(tecla);
			campo.value += s.substring (pos + 1);
			pos++;
		}
	}

	if (pos < 0)  pos = 0;
	if (pos > campo.value.length) {pos = campo.value.length; }

	s = "";
	ss = campo.value;
	var j = 0;

	for (var i = 0; i < mascara.length; i++) {
		ch = mascara.charAt(i);
		if (ch == "_") {
			s += ss.charAt(j);
			j++;
		} else {
			s += ch;
			if (pos > i) pos++;
		}
	}
	campo.value = s;

	if(campo.createTextRange) {
		var range = campo.createTextRange();
	    range.move("character", pos);
	    range.select();
	} else {
	   campo.setSelectionRange(pos, pos);
	}
	if (parada) {
		return false;
	} else {
		return true;
	}
}

function valida_cnpj(cnpj) {

	var ss = cnpj;
	var s = "";
	var ch = "";


	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch == "_") || ((ch >= "0") && (ch <= "9"))) {
			s += ss.charAt(i);
		}
	}
	cnpj = s;

	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;

	digitos_iguais = 1;
	if (cnpj.length < 14 && cnpj.length < 15) {
	    return false;
	}
	for (i = 0; i < cnpj.length - 1; i++) {
	    if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
		    digitos_iguais = 0;
	        break;
	    }
	}
	if (!digitos_iguais) {
	    tamanho = cnpj.length - 2
	    numeros = cnpj.substring(0,tamanho);
	    digitos = cnpj.substring(tamanho);
	    soma = 0;
	    pos = tamanho - 7;
	    for (i = tamanho; i >= 1; i--) {
			soma += numeros.charAt(tamanho - i) * pos--;
	        if (pos < 2) {
		        pos = 9;
		    }
	    }
	    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	    if (resultado != digitos.charAt(0)) {
		    return false;
	    }
		tamanho = tamanho + 1;
	    numeros = cnpj.substring(0,tamanho);
	    soma = 0;
	    pos = tamanho - 7;
	    for (i = tamanho; i >= 1; i--) {
		    soma += numeros.charAt(tamanho - i) * pos--;
		    if (pos < 2) {
		        pos = 9;
		    }
	    }
	    resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	    if (resultado != digitos.charAt(1)) {
	          return false;
	    }
	    return true;
	} else {
	    return false;
	}
}


/***********************************************************

	VALIDADORES DE FATURAMENTO

***********************************************************/

function focusFaturamento (campo, evento) {
	var ss = campo.value;
	var s = "";
	var ch = "";


	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch >= "0") && (ch <= "9")) {
			 s += ss.charAt(i);
		}
	}

	campo.value = s;
	if (campo.createTextRange) {
		var range = campo.createTextRange();
        range.move("character", 0);
        range.select();
    } else {
	   campo.setSelectionRange(0, 0);
    }
}

function blurFaturamento (campo) {
	if (campo.value == "_______________") {
		campo.value = "";
	} else {
		var s = "";

		var ss = campo.value;
		for (var i = 0; i < ss.length; i++) {
			ch = ss.charAt(i);
			if ((ch >= "0") && (ch <= "9")) {
				 s += ss.charAt(i);
			}
		}
		var len = s.length;
		var pos = len - 2;
		ss = "," + s.substring(pos,len);
		var ss0 = s;
		var casa = 0;
		var ultpos = pos;
		for (i = pos - 1; i >= 0; i--) {
			if (i >= 0) {
				if (casa == 3) {
					ss =  "." + ss;
					casa = 0;
				}
				casa++;
			}
			ss = ss0.substring(i,i + 1) + ss;
		}
		campo.value = ss;
	}
}

function blurNumerico (campo) {
	var s = "";

	var ss = campo.value;
	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch >= "0") && (ch <= "9")) {
			 s += ss.charAt(i);
		}
	}
	if (s == "") {
		s = "0";
	}
	campo.value = s;
}

/***********************************************************

	VALIDADORES DE E-MAIL

***********************************************************/

function checkMail(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 checkData(valor){
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	if (valor.match(expReg)) {
		var dia = valor.substring(0,2);
		var mes = valor.substring(3,5);
		var ano = valor.substring(6,10);
		if(mes==4 || mes==6 || mes==9 || mes==11 && dia > 30) {
			return false;
		} else {
			if(ano%4!=0 && mes==2 && dia>28){
				return false;
			} else {
				if(ano%4==0 && mes==2 && dia>29){
					return false;
				} else {
					return true;
				}
			}
		}
	} else {
	   return false;
	}
}


function blurEmail(campo) {
	if (campo.value) {
		if (!checkMail(campo.value)) {
			alert("E-mail inválido");
			campo.focus();
			campo.select();
		}
	}
}

/***********************************************************

	VALIDADORES DE TELEFONE

***********************************************************/

function formata_numero(campo, teclaPress) {

    if (window.event) {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var ch = "";
	var s = "";

	for (var i = 0; i < campo.value.length; i++) {
		if ((campo.value.charAt(i) >= "0") && (campo.value.charAt(i) <= "9")) {
			s += campo.value.charAt(i);
		}
	}
	campo.value = s
}

function formata_cnpj(campo, teclaPress) {

    if (window.event) {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var ch = "";
	var s = "";

	for (var i = 0; i < campo.value.length; i++) {
		if ((campo.value.charAt(i) >= "0") && (campo.value.charAt(i) <= "9")) {
			s += campo.value.charAt(i);
		}
	}

   tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 && tecla != 16 ) {
        if (tam <= 2) {
        	campo.value = s;
        }
		if (tam > 2)
            campo.value = s.substr(0,2) + '.' + s.substr(2,tam);
        if (tam > 5)
            campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam);
        if (tam > 8)
            campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam);
        if (tam > 12)
            campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4) + '-' + s.substr(12,tam);
    }
}

function formata_cpf(campo, teclaPress) {

    if (window.event) {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var ch = "";
	var s = "";

	for (var i = 0; i < campo.value.length; i++) {
		if ((campo.value.charAt(i) >= "0") && (campo.value.charAt(i) <= "9")) {
			s += campo.value.charAt(i);
		}
	}

   tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 && tecla != 16 ) {
        if (tam <= 2) {
        	campo.value = s;
        }
		if (tam > 3)
            campo.value = s.substr(0,3) + '.' + s.substr(3,tam);
        if (tam > 6)
            campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam);
        if (tam > 9)
            campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam);
    }
}

function formata_tel(campo, teclaPress) {

    if (window.event)
    {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var ch = "";
	var s = "";

	for (var i = 0; i < campo.value.length; i++) {
		if ((campo.value.charAt(i) >= "0") && (campo.value.charAt(i) <= "9")) {
			s += campo.value.charAt(i);
		}
	}

   tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 ) {
        if (tam <= 2) {
        	campo.value = s;
        }
		if (tam > 2 && tam < 7)
            campo.value = '(' + s.substr(0,2) + ')' + s.substr(2,tam);
        if (tam >= 7)
            campo.value = '(' + s.substr(0,2) + ')' + s.substr(2,4) + '-' + s.substr(6,tam-6);
    }
}

function formata_cep(campo, teclaPress) {

    if (window.event) {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var ch = "";
	var s = "";

	for (var i = 0; i < campo.value.length; i++) {
		if ((campo.value.charAt(i) >= "0") && (campo.value.charAt(i) <= "9")) {
			s += campo.value.charAt(i);
		}
	}

    tam = s.length + 1;
    if ((tecla != 8) && (tecla != 9)) {
		if (tam > 5) {
	        campo.value = s.substr(0,5) + '-' + s.substr(5,tam);
	    } else {
		    campo.value = s;
	    }
    }
}

function formata_data(campo, teclaPress) {

    if (window.event) {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var ch = "";
	var s = "";

	for (var i = 0; i < campo.value.length; i++) {
		if ((campo.value.charAt(i) >= "0") && (campo.value.charAt(i) <= "9")) {
			s += campo.value.charAt(i);
		}
	}

    tam = s.length + 1;
    if ((tecla != 8) && (tecla != 9)) {
		campo.value = s;
		if (tam > 2) {
	        campo.value = s.substr(0,2) + '/' + s.substr(2,tam);
	    }
		if (tam > 4) {
	        campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam);
	    }
    }
}

function numero(e)
{
    navegador = /msie/i.test(navigator.userAgent);
    if (navegador)
    var  tecla = event.keyCode;
    else
    var tecla = e.which;

    if(tecla > 47 && tecla < 58)
        return true;
    else
    {
        if (tecla != 8)
            return false;
        else
            return true;
    }
}

function focusTelefone (campo, evento) {
	if (campo.value == "") {
		campo.value = "__-____-____";
	}
	if(campo.createTextRange) {
		var range = campo.createTextRange();
        range.move("character", 0);
        range.select();
    } else {
	   campo.setSelectionRange(0, 0);
    }

}

function blurTelefone (campo) {
	if (campo.value == "(__) ____-____") {
		campo.value = "";
	}
}

function keyPressTelefone(campo, evento) {
	campo.style.color='#0000ff';
	var pos = getSelectionStart(campo);
	var pos_orig = pos - 1;
	if(window.event) {
	var tecla = evento.keyCode;
	} else {
		tecla = evento.which;
	}

	var parada = true;
	var mascara = "(__) ____-____";
	var retorno = 0;

	var ss = campo.value;
	var s = "";
	var ch = "";


	for (var i = 0; i < ss.length; i++) {
		ch = ss.charAt(i);
		if ((ch == "_") || ((ch >= "0") && (ch <= "9"))) {
			s += ss.charAt(i);
		} else {
			if (pos > i) {
				pos--;
			}
		}
	}
	campo.value = s;

	var c = String.fromCharCode(tecla);

	// BACKSPACE
	if (tecla == 8) {
		if (pos > 0) {
			campo.value = s.substring (0, pos - 1);
			campo.value += "_";
			campo.value += s.substring (pos);
			pos--;
		}
	}
	if (tecla == 9) {
		parada = false;
	}
	if (tecla == 35)
		pos = campo.value.length;
	if (tecla == 36)
		pos = 0;
	if (tecla == 37)
		pos--;
	if (tecla == 39)
		pos++;
	// DELETE
	if (tecla == 46 ) {
		if (pos < campo.value.length) {
			campo.value = s.substring (0, pos);
			campo.value += "_";
			campo.value += s.substring (pos + 1);
			pos++;
		}
	}
	if (((tecla >= 48) && (tecla <= 57)) || ((tecla >= 96) && (tecla <= 105))) {
		if (pos < campo.value.length) {
			if (tecla >= 96) tecla -= 48;
			campo.value = s.substring (0, pos);
			campo.value +=  String.fromCharCode(tecla);
			campo.value += s.substring (pos + 1);
			pos++;
		}
	}

	if (pos < 0)  pos = 0;
	if (pos > campo.value.length) {pos = campo.value.length; }

	s = "";
	ss = campo.value;
	var j = 0;

	for (var i = 0; i < mascara.length; i++) {
		ch = mascara.charAt(i);
		if (ch == "_") {
			s += ss.charAt(j);
			j++;
		} else {
			s += ch;
			if (pos > i) pos++;
		}
	}
	campo.value = s;

	if (campo.createTextRange) {
		var range = campo.createTextRange();
		range.move("character", pos);
		range.select();
	} else {
		campo.setSelectionRange(pos, pos);
	}
	if (parada) {
		return false;
	} else {
		return true;
	}
}