function validateFormCustomer(form) {
  var name                = document.getElementById("name").value;
  var senha               = document.getElementById("senha").value;
  var conf_senha          = document.getElementById("conf_senha").value;
  var email               = document.getElementById("email").value;
  var gender              = document.getElementById("gender").value;
  var cpf                 = document.getElementById("txtCpf").value;
  var txtRg               = document.getElementById("txtRg").value;
  var txtDispatcherAgency = document.getElementById("txtDispatcherAgency").value;
  var txtDataNascimento   = document.getElementById("txtDataNascimento").value;
  var txtAddress          = document.getElementById("txtAddress").value;
  var txtAddressNumber    = document.getElementById("txtAddressNumber").value;
  var txtDistrict         = document.getElementById("txtDistrict").value;
  var txtCity             = document.getElementById("txtCity").value;
  var txtPhoneAreaCode    = document.getElementById("txtPhoneAreaCode").value;
  var txtPhoneNum         = document.getElementById("txtPhoneNum").value;
  var txtPhoneCelularAreaCode = document.getElementById("txtPhoneCelularAreaCode").value;
  var txtPhoneCelularNum  = document.getElementById("txtPhoneCelularNum").value;        
  var drpPartnerSource  = document.getElementById("drpPartnerSource").value;        
 
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  cpf = cpf.replace('/','');
  cpf = cpf.replace('.','');
  cpf = cpf.replace('.','');
  cpf = cpf.replace('-','');
  if(email == "") {
    inlineMsg('email','<strong>Erro</strong><br />Digite seu e-mail.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />Digite um email valido.',2);
    return false;
  }
  if(senha == "") {
    inlineMsg('senha','Digite a senha.',2);
    return false;
  }
  if(conf_senha == "") {
    inlineMsg('conf_senha','Digite a confirmacao da senha.',2);
    return false;
  }  
  if(senha != conf_senha) {
    inlineMsg('conf_senha','<strong>Erro</strong><br />Voce digitou senhas diferentes!',2);
    return false;
  }  
  if(!name.match(nameRegex)) {
    inlineMsg('name','Digite um nome valido.',2);
    return false;
  }
  if(cpf == "") {
    inlineMsg('txtCpf','Digite seu CPF.',2);
    return false;
  }
  if(valida_cpf(cpf) == false) {
    inlineMsg('txtCpf','CPF Invalido!',2);
    return false;
  }  
  if(txtRg == "") {
    inlineMsg('txtRg','Digite seu RG.',2);
    return false;
  }
  if(txtRg.length < 6) {
    inlineMsg('txtRg','RG Invalido!',2);
    return false;
  }   
  if(txtDispatcherAgency == "") {
    inlineMsg('txtDispatcherAgency','Digite o Orgao Emissor.',2);
    return false;
  }
  if(gender == "" || gender == "Selecione") {
    inlineMsg('gender','<strong>Erro</strong><br />Selecione seu sexo.',2);
    return false;
  }  
  if(txtDataNascimento == "") {
    inlineMsg('txtDataNascimento','Digite sua data de nascimento.',2);
    return false;
  }
  
  validateCep(form); 
  
  if(txtAddress == "") {
    inlineMsg('txtZip','Digite um cep valido.',2);
    return false;
  }
  if(txtAddressNumber == "") {
    inlineMsg('txtAddressNumber','Digite o numero do logradouro.',2);
    return false;
  }
  if(txtDistrict == "") {
    inlineMsg('txtZip','Digite um cep valido.',2);
    return false;
  }
  if(txtCity == "") {
    inlineMsg('txtZip','Digite um cep valido.',2);
    return false;
  }        
  if(txtPhoneAreaCode == "") {
    inlineMsg('txtPhoneAreaCode','Digite o seu telefone residencial.',2);
    return false;
  }
  if(txtPhoneNum == "") {
    inlineMsg('txtPhoneNum','Digite o seu telefone residencial.',2);
    return false;
  }
  if(txtPhoneCelularAreaCode == "") {
    inlineMsg('txtPhoneCelularAreaCode','Digite o seu telefone celular.',2);
    return false;
  }
  if(txtPhoneCelularNum == "") {
    inlineMsg('txtPhoneCelularNum','Digite o seu telefone celular.',2);
    return false;
  } 
  if(drpPartnerSource == "" || drpPartnerSource == "Selecione") {
    inlineMsg('gender','<strong>Erro</strong><br />Selecione de que forma voce conheceu o Aldeia Bonsai.',2);
    return false;
  }    
  
  return true;
}

function validaNewsletter()
{
  var name  = document.getElementById("txtName").value;
  var email = document.getElementById("txtEmail").value;
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

  if(email == "") {
    //inlineMsg('email','<strong>Erro</strong><br />Digite seu e-mail.',2);
    alert("Digite seu e-mail.");
    return false;
  }
  if(!email.match(emailRegex)) {
    //inlineMsg('email','<strong>Erro</strong><br />Digite um email valido.',2);
    alert("Digite um email valido.");
    return false;
  }
  if(!name.match(nameRegex)) {
    //inlineMsg('name','Digite um nome valido.',2);
    alert("Digite um nome valido.");
    return false;
  }

  return true;    
}

function validaRecoveryPassword()
{
  var email = document.getElementById("email").value;
  
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

  if(email == "") {
    inlineMsg('email','<strong>Erro</strong><br />Digite seu e-mail.',2);
    //alert("Digite seu e-mail.");
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />Digite um email valido.',2);
    //alert("Digite um email valido.");
    return false;
  }

  return true;    
}

function validateCep(form) {
  var txtZip              = document.getElementById("txtZip").value;
  var txtZipComplement    = document.getElementById("txtZipComplement").value;

  if(txtZip == "") {
    inlineMsg('txtZip','Campo CEP e obrigatorio.',2);
    return false;
  }
  if(txtZipComplement == "") {
    inlineMsg('txtZipComplement','Campo CEP e obrigatorio.',2);
    return false;
  }
  
  return true;
}

function validateClient(valid){
  if (valid == "true") {
    inlineMsg('email','O email ja esta sendo utilizado em outro cadastro, digite outro email.',2);
    return false;
  }
  return true;
}

function validatePreLogin(){
  var email       = document.getElementById("email").value;
  var rbtNewUser  = document.getElementById("rbtNewUser").checked;
  var rbtSiteUser = document.getElementById("rbtSiteUser").checked;
  var txtZip      = document.getElementById("txtZip").value;
  var txtPassword = document.getElementById("txtPassword").value;
  
  if (email == "") {
    inlineMsg('email','Digite um email valido!',2);
    return false;
  }
  
  if (rbtNewUser == true) {
    if (txtZip == ""){
        inlineMsg('txtZip','Digite um cep valido!',2);
        return false;
    }
    else{
        if (doExistClient() == false){
            return false;
        }
    }
  }  
  
  if (rbtSiteUser == true) {
    if (txtPassword == ""){
        inlineMsg('txtPassword','Email ou senha invalidos!',2);
        return false;
    }
  }    
  
  return true;
}

function validatePreLoginUser(){
  var uName        = document.getElementById("uName").value;
  var txtPassword  = document.getElementById("txtPassword").value;
  
  if (uName == "") {
    inlineMsg('uName','Digite seu nome!',2);
    return false;
  }
  
  if (txtPassword == "") {
    inlineMsg('txtPassword','Usuario ou senha invalidos!',2);
    return false;
  }    
  
  return true;
}

function validateSendMessage(){
  var uName       = document.getElementById("uName").value;
  var EMail       = document.getElementById("EMail").value;
  var oTelefone   = document.getElementById("oTelefone").value;
  var DeptID      = document.getElementById("DeptID").value;
  var oMensagem   = document.getElementById("oMensagem").value;
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

  if(!uName.match(nameRegex)) {
    inlineMsg('uName','Digite um nome valido.',2);
    return false;
  }
  if(EMail == "") {
    inlineMsg('EMail','<strong>Erro</strong><br />Digite seu e-mail.',2);
    return false;
  }
  if(!EMail.match(emailRegex)) {
    inlineMsg('EMail','<strong>Erro</strong><br />Digite um email valido.',2);
    return false;
  }
  if (oTelefone == "") {
    inlineMsg('oTelefone','Digite o seu telefone!',2);
    return false;
  }
  if (DeptID == "" || DeptID == "Selecione um assunto") {
    inlineMsg('DeptID','Selecione um assunto!',2);
    return false;
  }
  if (oMensagem == "") {
    inlineMsg('oMensagem','Digite a sua mensagem!',2);
    return false;
  }
  
  return true;
}

function validateLogin(valid){
  if (valid == ""){
    inlineMsg('email','Email ou senha invalidos!',2);
    return false;
  }    
  
  return true;
}

function validateLoginUser(valid){
  if (valid == ""){
    inlineMsg('uName','Usuario ou senha invalidos!',2);
    return false;
  }    
  
  return true;
}

function getElementsCustomers(){
  var delimiter = "£";
  var elements  = "";
  elements += "CLI_ST_NOME=" + document.getElementById("name").value + delimiter;
  elements += "CLI_ST_SENHA=" + document.getElementById("senha").value + delimiter;
  elements += "CLI_ST_EMAIL=" + document.getElementById("email").value + delimiter;
  elements += "CLI_ST_NEWSLETTER=" + document.getElementById("chkRcvEmail").checked + delimiter;
  elements += "CLI_ST_SEXO=" + document.getElementById("gender").value + delimiter;
  elements += "CLI_ST_CPF=" + document.getElementById("txtCpf").value + delimiter;
  elements += "CLI_ST_RG=" + document.getElementById("txtRg").value + delimiter;
  elements += "CLI_ST_ORGAOEMISSOR=" + document.getElementById("txtDispatcherAgency").value + delimiter;
  elements += "CLI_DT_NASCIMENTO=" + document.getElementById("txtDataNascimento").value + delimiter;
  elements += "CLI_ST_CEP=" + document.getElementById("txtZip").value + document.getElementById("txtZipComplement").value + delimiter;
  elements += "CLI_ST_ENDERECO=" + document.getElementById("txtAddress").value + delimiter;
  elements += "CLI_ST_NUMERO=" + document.getElementById("txtAddressNumber").value + delimiter;
  elements += "CLI_ST_COMPLEMENTO=" + document.getElementById("txtComplement").value + delimiter;
  elements += "CLI_ST_BAIRRO=" + document.getElementById("txtDistrict").value + delimiter;
  elements += "CLI_ST_CIDADE=" + document.getElementById("txtCity").value + delimiter;
  elements += "CLI_ST_ESTADO=" + document.getElementById("dropState").value + delimiter;
  elements += "CLI_ST_TELEFONE1=" + "(" + document.getElementById("txtPhoneAreaCode").value + ")" + document.getElementById("txtPhoneNum").value + delimiter;
  elements += "CLI_ST_TELEFONE2=" + "(" + document.getElementById("txtPhoneCelularAreaCode").value + ")" + document.getElementById("txtPhoneCelularNum").value + delimiter;
  elements += "CLI_ST_TELEFONE3=" + "(" + document.getElementById("txtPhoneComercialCode").value + ")" + document.getElementById("txtPhoneComercialNum").value + delimiter;
  elements += "CLI_ST_PARTNERSOURCE=" + document.getElementById("drpPartnerSource").value + delimiter;
  elements += "CLI_ST_PARTNEROTHERS=" + document.getElementById("tbxCustomPartnerSource").value + delimiter;
  
  return elements;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

function valida_cpf(cpf)
{
      var numeros, digitos, soma, i, resultado, digitos_iguais;
      digitos_iguais = 1;
      if (cpf.length < 11)
            return false;
      for (i = 0; i < cpf.length - 1; i++)
            if (cpf.charAt(i) != cpf.charAt(i + 1))
                  {
                  digitos_iguais = 0;
                  break;
                  }
      if (!digitos_iguais)
            {
            numeros = cpf.substring(0,9);
            digitos = cpf.substring(9);
            soma = 0;
            for (i = 10; i > 1; i--)
                  soma += numeros.charAt(10 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            numeros = cpf.substring(0,10);
            soma = 0;
            for (i = 11; i > 1; i--)
                  soma += numeros.charAt(11 - i) * i;
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
      else
            return false;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
}
