function Janela(pagina, nomedajanela, w, h) {
   var winl = ((screen.width - w) / 2) - 5;
   var wint = ((screen.height - h) / 2) - 25;
   winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',status=no,scrollbars=yes,resizable=yes,menubar=no'
   win = window.open(pagina, nomedajanela, winprops)
   if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

function Foto(foto) {
   var winl = ((screen.width - 400) / 2);
   var wint = ((screen.height - 300) / 2);
   winprops = 'height=300,width=400,top='+wint+',left='+winl+',status=no,scrollbars=no,resizable=no,menubar=no'
   win = window.open('fotos.php?Foto='+foto, 'AmpliarFotos', winprops)
   if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}

function verifyCompatibleBrowser(){
        this.ver=navigator.appVersion
        this.dom=document.getElementById?1:0
        this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
        this.ie4=(document.all && !this.dom)?1:0;
        this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
        this.ns4=(document.layers && !this.dom)?1:0;
        this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
        return this
}
function ConstructObject(obj,nest){
    nest=(!nest) ? '':'document.'+nest+'.'
        this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
          this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
        this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight
        this.newsScroll=newsScroll;
        this.moveIt=b_moveIt; this.x; this.y;
    this.obj = obj + "Object"
    eval(this.obj + "=this")
    return this
}
function b_moveIt(x,y){
        this.x=x;this.y=y
        this.css.left=this.x
        this.css.top=this.y
}
function newsScroll(speed){
        if(this.y>-this.scrollHeight){
                this.moveIt(0,this.y-pr_step)
                setTimeout(this.obj+".newsScroll("+speed+")",speed)
        }else if(loop) {
                this.moveIt(0,lstart)
                eval(this.obj+".newsScroll("+speed+")")
          }
}
function InitialiseAutoScrollArea(){
        objContainer=new ConstructObject('divASContainer')
        objContent=new ConstructObject('divASContent','divASContainer')
        objContent.moveIt(0,lstart)
        objContainer.css.visibility='visible'
        objContent.newsScroll(speed)
}

function zeros(campo){
        var s
        s = campo.value
        t = 10 - s.length
        for(x = 0 ;  x < t ; x++){
                s = 0 + s
        }
        campo.value = s
}

function FormataCampo(Campo,teclapres,mascara){
        //pegando o tamanho do texto da caixa de texto com delay de -1 no event
        //ou seja o caractere que foi digitado não será contado.
        strtext = Campo.value
        tamtext = strtext.length
        //pegando o tamanho da mascara
        tammask = mascara.length
        //criando um array para guardar cada caractere da máscara
        arrmask = new Array(tammask)
        //jogando os caracteres para o vetor
        for (var i = 0 ; i < tammask; i++){
                arrmask[i] = mascara.slice(i,i+1)
        }
        //alert (teclapres.keyCode)
        //começando o trabalho sujo
        if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))){
                if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){
                        Organiza_Casa(Campo,arrmask[tamtext],teclapres.keyCode,strtext)
                }
                else{
                        Detona_Event(Campo,strtext)
                }
        }
        else{//Aqui funcionaria a mascara para números mas eu ainda não implementei
                if ((arrmask[tamtext] == "A"))        {
                        charupper = event.valueOf()
                        //charupper = charupper.toUpperCase()
                        Detona_Event(Campo,strtext)
                        masktext = strtext + charupper
                        Campo.value = masktext
                }
        }
}
function Organiza_Casa(Campo,arrpos,teclapres_key,strtext){
        if (((arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres_key == 8)){
                separador = arrpos
                masktext = strtext + separador
                Campo.value = masktext
        }
}
function Detona_Event(Campo,strtext){
        event.returnValue = false
        if (strtext != "") {
                Campo.value = strtext
        }
}


function ValidaData(TheData, NomeData) {
   Tam = TheData.length;
   if (Tam != 10) {
      alert ('Digite a '+NomeData+' no formato dd/mm/aaaa, por favor.');
      return false;
   }

   //--- Separa do Conteúdos do campo ---
   AnoT = TheData.substring(6,10);
   MesT = TheData.substring(3,5);
   DiaT = TheData.substring(0,2);

   //--- Validação para Números ---
   if ( !isFinite(AnoT) || !isFinite(MesT) || !isFinite(DiaT) ) {
      alert ('Digite a '+NomeData+' no formato dd/mm/aaaa, por favor.');
      return false;
   }

   //--- Validação para as Barras ---
   Barra1T = TheData.substring(2,3)
   Barra2T = TheData.substring(5,6)
   if (Barra1T != "/" || Barra2T != "/") {
      alert ('A '+NomeData+' está com os separadores inválidos, utilize \"/\" por valor.');
      return false;
   }

   //--- Faz a Verificação se o Mês é Válido ---

   //--- Faz a Verificação se o Dia é Válido ---
   if(DiaT>31 || DiaT<1) {
     alert('Digite um dia válido para a '+NomeData+', por favor.');
     return false
   }

   if(MesT > 12 || MesT < 1) {
     alert('Digite um mês válido para a '+NomeData+', por favor.');
     return false
   }
}


function validaEMAIL(objeto) {
        var email = objeto.value;
        var s = new String(email);
        var retorno = true;
        // Validar se o e-mail inicia com o @
        if (email.substr(0,1) == "@")
                retorno = false;
        // { } ( ) < > [ ] | \ /
        if ((s.indexOf("{")>=0) || (s.indexOf("}")>=0) || (s.indexOf("(")>=0) || (s.indexOf(")")>=0) || (s.indexOf("<")>=0) || (s.indexOf(">")>=0) || (s.indexOf("[")>=0) || (s.indexOf("]")>=0) || (s.indexOf("|")>=0) || (s.indexOf("\"")>=0) || (s.indexOf("/")>=0))
                retorno = false;
        // & * $ % ? ! ^ ~ ` ' "
        if ((s.indexOf("&")>=0) || (s.indexOf("*")>=0) || (s.indexOf("$")>=0) || (s.indexOf("%")>=0) || (s.indexOf("?")>=0) || (s.indexOf("!")>=0) || (s.indexOf("^")>=0) || (s.indexOf("~")>=0) || (s.indexOf("`")>=0) || (s.indexOf("'")>=0) )
                retorno = false;
        // , ; : = #
        if ((s.indexOf(",")>=0) || (s.indexOf(";")>=0) || (s.indexOf(":")>=0) || (s.indexOf("=")>=0) || (s.indexOf("#")>=0) )
                retorno = false;
        // procura se existe apenas um @
        if ( (s.indexOf("@") < 0) || (s.indexOf("@") != s.lastIndexOf("@")) )
                retorno = false;
        // verifica se tem pelo menos um ponto após o @
        if (s.lastIndexOf(".") < s.indexOf("@"))
                retorno = false;
        // áéíóúâêîôûäëïöüçàèìòùãõ
        if ((s.indexOf("á")>=0) || (s.indexOf("é")>=0) || (s.indexOf("í")>=0) || (s.indexOf("ó")>=0) || (s.indexOf("ú")>=0) ||
            (s.indexOf("â")>=0) || (s.indexOf("ê")>=0) || (s.indexOf("î")>=0) || (s.indexOf("ô")>=0) || (s.indexOf("û")>=0) ||
            (s.indexOf("ä")>=0) || (s.indexOf("ë")>=0) || (s.indexOf("ï")>=0) || (s.indexOf("ö")>=0) || (s.indexOf("ü")>=0) ||
            (s.indexOf("ç")>=0) || (s.indexOf("à")>=0) || (s.indexOf("è")>=0) || (s.indexOf("ì")>=0) || (s.indexOf("ò")>=0) ||
            (s.indexOf("ù")>=0) || (s.indexOf("ã")>=0) || (s.indexOf("õ")>=0))
                retorno = false;
        // ÁÉÍÓÚÂÊÎÔÛÄËÏÖÜÇÀÈÌÒÙãõ
        if ((s.indexOf("Á")>=0) || (s.indexOf("É")>=0) || (s.indexOf("Í")>=0) || (s.indexOf("Ó")>=0) || (s.indexOf("Ú")>=0) ||
            (s.indexOf("Â")>=0) || (s.indexOf("Ê")>=0) || (s.indexOf("Î")>=0) || (s.indexOf("Ô")>=0) || (s.indexOf("Û")>=0) ||
            (s.indexOf("Ä")>=0) || (s.indexOf("Ë")>=0) || (s.indexOf("Ï")>=0) || (s.indexOf("Ö")>=0) || (s.indexOf("Ü")>=0) ||
            (s.indexOf("Ç")>=0) || (s.indexOf("À")>=0) || (s.indexOf("È")>=0) || (s.indexOf("Ì")>=0) || (s.indexOf("Ò")>=0) ||
            (s.indexOf("Ù")>=0) || (s.indexOf("Ã")>=0) || (s.indexOf("Õ")>=0))
                retorno = false;
        return retorno;
}

function ValidaAreaRestrita(theform) {
  if(theform.Usuario.value=='') {
        alert('Informe o usuário, por favor.');
        theform.Usuario.focus();
        return false;
  }
  if(theform.Senha.value=='') {
        alert('Informe a senha, por favor.');
        theform.Senha.focus();
        return false;
  }
  Janela("branco.php", "AreaRestrita", 770, 500);
  theform.userid.value=theform.Usuario.value;
  theform.password.value=theform.Senha.value;
  theform.Usuario.value='';
  theform.Senha.value='';
  return true;
}

function ValidaVisitaTecnica(theform) {
  if(theform.Nome.value=='') {
        alert('Informe o seu nome, por favor.');
        theform.Nome.focus();
        return false;
  }
  if(theform.Telefone.value=='') {
        alert('Informe o telefone, por favor.');
        theform.Telefone.focus();
        return false;
  }
  if(theform.Endereco.value=='') {
        alert('Informe o endereço, por favor.');
        theform.Endereco.focus();
        return false;
  }
  if(theform.Bairro.value=='') {
        alert('Informe o bairro, por favor.');
        theform.Bairro.focus();
        return false;
  }
  if(theform.Cidade.value=='') {
        alert('Informe a cidade, por favor.');
        theform.Cidade.focus();
        return false;
  }
  if(theform.Estado.value=='') {
        alert('Informe o estado, por favor.');
        theform.Estado.focus();
        return false;
  }
  if(theform.Email.value=='') {
        alert('Informe o seu e-mail, por favor.');
        theform.Email.focus();
        return false;
  }
  if(!validaEMAIL(theform.Email)) {
        alert('E-mail inválido. Digite um e-mail válido, por favor.');
        theform.Email.focus();
        theform.Email.select();
        return false;
  }
  if(theform.Data.value=='') {
        alert('Informe a data de preferência, por favor.');
        theform.Data.focus();
        return false;
  }
  if(ValidaData(theform.Data.value, 'data de preferência') == false ){
        theform.Data.focus();
        theform.Data.select();
        return false;
  }
  if(theform.Hora.value=='') {
        alert('Informe a hora de preferência, por favor.');
        theform.Hora.focus();
        return false;
  }
  if(theform.Hora.value.length<5) {
        alert('Digite a hora de preferência no formato hh:mm, por favor');
        theform.Hora.focus();
        theform.Hora.select();
        return false;
  }
  return true;
}

function ValidaContato(theform) {
  if(theform.Nome.value=='') {
        alert('Informe o seu nome, por favor.');
        theform.Nome.focus();
        return false;
  }
  if(theform.Email.value=='') {
        alert('Informe o seu e-mail, por favor.');
        theform.Email.focus();
        return false;
  }
  if(!validaEMAIL(theform.Email)) {
        alert('E-mail inválido. Digite um e-mail válido, por favor.');
        theform.Email.focus();
        theform.Email.select();
        return false;
  }
  if(theform.Telefone.value=='') {
        alert('Informe o telefone, por favor.');
        theform.Telefone.focus();
        return false;
  }
  if(theform.Mensagem.value=='') {
        alert('Digite a sua mensagem, por favor.');
        theform.Mensagem.focus();
        return false;
  }
  return true;
}
