// EyeDea
//----------------------------------------
//popup
//----------------------------------------
function abreJanela(largura,altura,url) {
 direita = (screen.width - largura)/2
 topo = (screen.height - altura)/2
 window.open(url,'','width=' + largura + ',height=' + altura + ',top=' + topo + ', left=' + direita + 'toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=no')
}
//----------------------------------------
//limpa string : permite apenas numeros
//----------------------------------------
function limpa_stringNumero(S) // Deixa so os digitos no numero
{
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++)
	{
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0)
		{
			temp=temp+digito
		}
	}
	return temp
}
function limpa_stringNT(S) // Deixa so os digitos no numero
{
	var Digitos = "0123456789-";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++)
	{
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0)
		{
			temp=temp+digito
		}
	}
	return temp
}
function validar_num(campo)
{
	nome=campo.name
	num = limpa_stringNT(campo.value);
	campo.value = num;
}
//----------------------------------------
//verifica EMAIL
//----------------------------------------
function Validar_Email(email)
{
	if(email.length < 6) {
		return false;
	}
	var x = 0;
	for (var c=0;c<email.length;c++) {
		if (email.substring(c,c+1) == '@') {
			x = c;
		}
	}
	var y = 0;
	if (x > 0) {
		for (c=x;c<email.length;c++) {
			if (email.substring(c,c+1)=='.') {
				y = c;
				var valida = 1;
			}
		}
		if (y > 0) {
			var dominio = '';
			for (c=x;c<y;c++) {
				dominio = dominio + email.substring(1,c);
			}
		}
	}
	else {
		return false;
	}					
	if (y <= x+2){
		return false;
	}				
	if (valida == 1){
		return true;
	}
}
//----------------------------------------
//Valida o cpf formatado com os pontos e traços
//----------------------------------------
function valida_cpf(CPF)
{	
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 10;
	controle_2 = 11;
	lsucesso = 1;
	
	if ((CPF.length != 14) || (CPF.substring(11, 12) != "-"))
	return false
	else
	{
	CPF = CPF.substring(0,3) + CPF.substring(4,7) + CPF.substring(8,14);
	for (i=0 ; i < 9 ; i++)
	{
		dig_1 = dig_1 + parseInt(CPF.substring(i, i+1) * controle_1);
		controle_1 = controle_1 - 1;
	}
	
	resto = dig_1 % 11;
	dig_1 = 11 - resto;
	
	if ((resto == 0) || (resto == 1)) dig_1 = 0;
	for ( i=0 ; i < 9 ; i++)
	{
		dig_2 = dig_2 + parseInt(CPF.substring(i, i + 1) * controle_2);
		controle_2 = controle_2 - 1;
	}
	dig_2 = dig_2 + 2 * dig_1;
	resto = dig_2 % 11;
	dig_2 = 11 - resto;
	
	if ((resto == 0) || (resto == 1)) dig_2 = 0;
	dig_ver = (dig_1 * 10) + dig_2;
	
	if (dig_ver != parseFloat(CPF.substring(CPF.length-2,CPF.length))) return false;
	}
	return true;
}
//----------------------------------------
//conta caracteres em campo input
//----------------------------------------
function f_ContCaracteres(objeto,nomeCampo,tamanhoCampo){

	var limite = tamanhoCampo;
	var erro = "Você ultrapassou o limite de ("+limite+") caracteres.";
	alert('passou');
	tamanho = objeto.value.length;
	if (tamanho>limite) {
		objeto.value = objeto.value.substring(0,limite);
		alert(erro);
	} 
	else 
	{
		document.getElementById(nomeCampo).innerHTML = (limite-objeto.value.length);
	}
}
//----------------------------------------
//Formatação para qualquer mascara
//----------------------------------------
function formatar(src, mask) 
{
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida) 
	{
		src.value += texto.substring(0,1);
	}
}

/* 

Exemplos:
CEP
OnKeyPress="formatar(this, '#####-###')"
CPF
OnKeyPress="formatar(this, '###.###.###-##')"
DATA
OnKeyPress="formatar(this, '##/##/####')"
*/
//----------------------------------------
//formata telefone (31)2211-1122
//----------------------------------------
var n;
var p;
var p1;
function ValidatePhone(){
p=p1.value
if(p.length==2){
	//d10=p.indexOf('(')
	pp=p;
	d4=p.indexOf('(')
	d5=p.indexOf(')')
	if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+")";
	}
	//pp="("+pp+")";
	document.form1.telefone.value="";
	document.form1.telefone.value=pp;
}
if(p.length>2){
	d1=p.indexOf('(')
	d2=p.indexOf(')')
	if (d2==-1){
		l30=p.length;
		p30=p.substring(0,4);
		//alert(p30);
		p30=p30+")"
		p31=p.substring(4,l30);
		pp=p30+p31;
		//alert(p31);
		document.form1.telefone.value="";
		document.form1.telefone.value=pp;
	}
	}
if(p.length>5){
	p11=p.substring(d1+1,d2);
	if(p11.length>3){
	p12=p11;
	l12=p12.length;
	l15=p.length
	//l12=l12-3
	p13=p11.substring(0,3);
	p14=p11.substring(3,l12);
	p15=p.substring(d2+1,l15);
	document.form1.telefone.value="";
	pp="("+p13+")"+p14+p15;
	document.form1.telefone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
	l16=p.length;
	p16=p.substring(d2+1,l16);
	l17=p16.length;
	if(l17>3&&p16.indexOf('-')==-1){
		p17=p.substring(d2+1,d2+5);
		p18=p.substring(d2+5,l16);
		p19=p.substring(0,d2+1);
		//alert(p19);
	pp=p19+p17+"-"+p18;
	document.form1.telefone.value="";
	document.form1.telefone.value=pp;
	//obj1.value="";
	//obj1.value=pp;
	}
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
	n=m.name;
	//p1=document.forms[0].elements[n]
	p1=m
	ValidatePhone()
}
function testphone(obj1){
	p=obj1.value
	//alert(p)
	p=p.replace("(","")
	p=p.replace(")","")
	p=p.replace("-","")
	p=p.replace("-","")
	//alert(isNaN(p))
	if (isNaN(p)==true){
		alert("Verifique o campo telefone.");
		return false;
	}
}

//----------------------------------------
//fecha janela
//----------------------------------------
function fecharJanela(){
	window.close();
}

//----------------------------------------
//mascara moeda
//----------------------------------------
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 (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;
}
