// JavaScript Document

// ============================================================================================================= //
// ================================ Função para as máscaras de entrada ========================================= //
// ============================================================================================================= //
function txtBoxFormat(strField, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

    if(window.event) { // Internet Explorer
      nTecla = evtKeyPress.keyCode; }
    else if(evtKeyPress.which) { // Nestcape / firefox
      nTecla = evtKeyPress.which;
    }
    //se for backspace não faz nada
    if (nTecla != 8){
    sValue = document.getElementById(strField).value;
    // alert(sValue);

    // 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) == " ") || (sMask.charAt(i) == ":"))

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

      i++;
    }

    document.getElementById(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;
    }
    }//fim do if que verifica se é backspace
}


// ============================================================================================================= //
// =================================== Função para validar o e-mail ============================================ //
// ============================================================================================================= //
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("E-mail incorreto")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("E-mail incorreto")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("E-mail incorreto")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("E-mail incorreto")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("E-mail incorreto")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("E-mail incorreto")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("E-mail incorreto")
		    return false
		 }

 		 return true					
	}

// ============================================================================================================= //
// ============================ Função para validar formulário de contato ====================================== //
// ============================================================================================================= //

function validaForm(frm){
	
	if((frm.nome == null) || (frm.nome.value == "")) {
		alert('Preencha o campo Nome.');
		frm.nome.focus();
		return false;
	}

	if((frm.email == null) || (frm.email.value == "")) {
		alert('Preencha o campo E-mail');
		frm.email.focus();
		return false;
	}

	if (echeck(frm.email.value) == false){
		frm.email.value=""
		frm.email.focus()
		return false
	}

	if((frm.conteudo == null) || (frm.conteudo.value == "")) {
		alert('Preencha o campo Mensagem');
		frm.conteudo.focus();
		return false;
	}

	frm.submit();
}

// ============================================================================================================= //
// ============================== Função para mostrar/esconder uma DIV ========================================= //
// ============================================================================================================= //

function toggle(id) {
	if (document.getElementById){
		var theitem = document.getElementById(id);
		if (theitem.style.display == 'block') {
			theitem.style.display = 'none';
		}
		else {
			theitem.style.display = 'block';
		}
	}
}

