// Função para chamar ajax para recuperar senha do usuario
function recuperar_senha(tipo){
if(tipo) {
var usuario = document.getElementById("cliente_email").value;
var tipo = tipo;
recuperar_senha_envia(usuario,tipo);
} else {
var usuario = document.getElementById("login_sistema").value;
var tipo = "operador";
recuperar_senha_envia(usuario,tipo);
}

}
// Função para exibir input de alterar senha do dominio
function exibir_input_cliente() {
var codigo_cliente = document.getElementById("codigo_cliente").value;
document.getElementById("div_senha").innerHTML = '<input name="cliente_senha" type="password" class="input" id="cliente_senha" size="30" onblur="alterar_senha_cliente(this.value,'+codigo_cliente+')" /> Digite no mínimo 8 caracteres.';
document.getElementById("cliente_senha").focus();
}
// Função para exibir input de alterar senha do cliente
function exibir_input() {
var dominio = document.getElementById("codigo_dominio").value;
document.getElementById("div_senha").innerHTML = '<input name="senha_cpanel" type="password" class="input" id="senha_cpanel" size="30" onblur="alterar_senha(this.value,'+dominio+')" /> Digite no mínimo 8 caracteres.';
document.getElementById("senha_cpanel").focus();
}
// Abrir/Fechar Assistente
function assistente(opcao_assistente){
	var div_assistente = document.getElementById( 'Assistente' );
	var verifica_assistente = opcao_assistente.substring(0,6);
	if(verifica_assistente == "abrir"){
		div_assistente.style.display = "block";
	}else{
		div_assistente.style.display = "none";
	}
}
// Função abrir e fechar divs
function abrir_div(div) {
document.getElementById( div ).style.display='block';
document.getElementById( 'dominio' ).disabled = false;
document.getElementById( 'extensao' ).disabled = false;
}
function fechar_div(div) {
document.getElementById( div ).style.display='none';
document.getElementById( 'whois' ).style.display = "none";
document.getElementById('dominio').value = '';
document.getElementById('extensao').value = '';
document.getElementById('sub_dominio').value = '';
document.getElementById('subdominio_extensao').value = '';
document.getElementById('hospedar_dominio').value = '';
document.getElementById('hospedar_extensao').value = '';
document.getElementById('usuario_cpanel').value = '';

}
// Função para mascarar campos
function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { // Nestcape
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

    sValue = objeto.value;

    // 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( " ", "" );
    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) == "/") || (sMask.charAt(i) == ":"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

    objeto.value = sCod;

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla > 47) && (nTecla < 58)); }
      else { // qualquer caracter...
        return true;
      }
    }
    else {
      return true;
    }
  }
// Função para verificar se o email principal, contém o domínio
function verifica_Email_Dominio(){
	
	var regitrar = document.getElementById("dominio").value+document.getElementById("extensao").value;
	var subdominio = document.getElementById("sub_dominio").value+document.getElementById("subdominio_extensao").value;
	var transferir = document.getElementById("hospedar_dominio").value+document.getElementById("hospedar_extensao").value;
	
	if(regitrar!="") {
		var dominio_formatado = regitrar;
	} else if(subdominio!="") {
		var dominio_formatado = subdominio;
	} else {
		var dominio_formatado = transferir;
	}

  if (document.getElementById('email1').value.indexOf("@"+dominio_formatado) > 2) {
    alert('O e-mail principal não pode conter o domínio a ser hospedado!\n\nPor favor informe outro e-mail.');
	document.getElementById('email1').value = '';
    return false;
  }
  return true;
}
// Função para validar CPF
function validarCPF(){
   var cpf = document.getElementById("cpf").value;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(cpf == ""){
	 return true;
   }
   if(!filtro.test(cpf)){
     window.alert("Por favor digite um CPF válido!");
	 document.getElementById('cpf').value = '';
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("Por favor digite um CPF válido!");
	  document.getElementById('cpf').value = '';
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 window.alert("Por favor digite um CPF válido!");
	 document.getElementById('cpf').value = '';
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     window.alert("Por favor digite um CPF válido!");
	 document.getElementById('cpf').value = '';
	 return false;
   }
   return true;
 }
 
function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }
// Função para validar CNPJ
function validaCNPJ() {
        CNPJ = document.getElementById("cnpj").value;
        erro = new String;
		if (CNPJ == "") {
		return true;
		}
        if (CNPJ.length < 18 || CNPJ == "000.000.000/0000-00" || CNPJ == "00.000.000/0000-000") 
		erro += "Por favor digite um CNPJ válido! \n\n";
        //substituir os caracteres que nao sao numeros
        if(document.layers && parseInt(navigator.appVersion) == 4){
            x = CNPJ.substring(0,2);
            x += CNPJ.substring(3,6);
            x += CNPJ.substring(7,10);
            x += CNPJ.substring(11,15);
            x += CNPJ.substring(16,18);
            CNPJ = x;    
        } else {
            CNPJ = CNPJ.replace(".","");
            CNPJ = CNPJ.replace(".","");
            CNPJ = CNPJ.replace("-","");
            CNPJ = CNPJ.replace("/","");
        }
        var nonNumbers = /\D/;
        if (nonNumbers.test(CNPJ)) erro += "Por favor digite um CNPJ válido! \n\n"; 
        var a = [];
        var b = new Number;
        var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
        for (i=0; i<12; i++){
            a[i] = CNPJ.charAt(i);
            b += a[i] * c[i+1];
        }
        if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
        b = 0;
        for (y=0; y<13; y++) {
            b += (a[y] * c[y]); 
        }
        if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
        if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
            erro +="Por favor digite um CNPJ válido!";
		}
        if (erro.length > 0){
            alert(erro);
			document.getElementById('cnpj').value = '';
            return false;
        } else {

        }
        return true;
    }
// Função para mudar cor da tr, seleciona-la e selecionar os checkboxes
function mudar_cor(linha)
{
    var chk = linha.getElementsByTagName("input");

    chk[0].checked = !chk[0].checked;

    if(chk[0].checked)
    {
        linha.style.backgroundColor = "#F8FAFC";
    }
    else
    {
        linha.style.backgroundColor = "";
    }
}
function mudar_cor_chk(obj,linha)
{
    var tab = document.getElementById("tab");
    
    obj.checked = !obj.checked;
    
    if(obj.checked)
    {
        tab.rows[linha].style.backgroundColor = "#F8FAFC";
    }
    else
    {
        tab.rows[linha].backgroundColor = "";
    }
}
function mostraDiv(sID)
{
    var chks = document.getElementById("frm").getElementsByTagName("input");
    var sDiv = document.getElementById(sID);
    var exibir = false;
    
    for(i = 0; i < chks.length; i++)
    {
        if((chks[i].type == "checkbox") && (chks[i].checked))
        {
            exibir = true;
            break;
        }
    }
    
    if(exibir)
    {
        sDiv.style.display = "block";
    }
    else
    {
        sDiv.style.display = "none";
    }
}
// Função checar select para busca
function CeckSelect(obj_form) {
var type_sel = obj_form.cat.options[obj_form.cat.selectedIndex].value;
if (type_sel == "clientes" || type_sel == "dominios") {
  return false;
} else {
  return true;
}
}
// Função par mudar o tipo de pessoa
function muda_tipo_pessoa(tipo) {
	
if(tipo == "fisica") {
	
document.getElementById("razao_social").disabled = true;
document.getElementById("cnpj").disabled = true;
document.getElementById("cpf").disabled = false;
document.getElementById("rg").disabled = false;

} else {

document.getElementById("razao_social").disabled = false;
document.getElementById("cnpj").disabled = false;
document.getElementById("cpf").disabled = true;
document.getElementById("rg").disabled = true;

}
}
//Função criar login a partir do dominio
function criar_usuario() {
    var dominio = document.cadastro_dominio.dominio.value;
    var usuario_cpanel = dominio.substring(0,8);
    var usuario_cpanel = usuario_cpanel.toLowerCase();
    document.cadastro_dominio.usuario_cpanel.value = replaceString(".","",usuario_cpanel);
}
function criar_usuario_assinatura() {
    var dominio = document.getElementById("dominio").value;
	var extensao = document.getElementById("extensao").value;
	var sub_dominio = document.getElementById("sub_dominio").value;
	var subdominio_extensao = document.getElementById("subdominio_extensao").value;
	var hospedar_dominio = document.getElementById("hospedar_dominio").value;
	var hospedar_extensao = document.getElementById("hospedar_extensao").value;
	
	if(dominio!="") {
		var dominio_formatado = dominio+extensao;
	} else if(sub_dominio!="") {
		var dominio_formatado = sub_dominio+subdominio_extensao;
	} else {
		var dominio_formatado = hospedar_dominio+hospedar_extensao;
	}

    var usuario_cpanel = dominio_formatado.substring(0,8);
    var usuario_cpanel = usuario_cpanel.toLowerCase();
    document.getElementById("usuario_cpanel").value = replaceString(".","",usuario_cpanel);
}

function replaceString(oldS,newS,fullS) {
    // Replaces oldS with newS in the string fullS
    for (var i=0; i<fullS.length; i++) {
        if (fullS.substring(i,i+oldS.length) == oldS) {
            fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)
        }
    }
    return fullS
}
// Função para habilitar/desabilitar botao de submeter o formulario
function habilitar(valor){
if(valor) {
document.getElementById('enviar').disabled=false;
} else {
document.getElementById('enviar').disabled=true;
}
}
function desabilitar(){
document.getElementById('enviar').disabled=true;
}
// Função para mostrar o nível de segurança da senha
function Security(val, tamanho) {
 	
 	document.getElementById('d_baixa').style.background = 'white';
 	document.getElementById('d_media').style.background = 'white';
 	document.getElementById('d_alta').style.background = 'white';
 if(val == "")
 {
	document.getElementById('d_baixa').style.background = 'white';
 	document.getElementById('d_media').style.background = 'white';
 	document.getElementById('d_alta').style.background = 'white';
  
   return;   
 }
 if(val.length < tamanho )
 {
	document.getElementById('d_baixa').style.background = 'white';
 	document.getElementById('d_media').style.background = 'white';
 	document.getElementById('d_alta').style.background = 'white';
   
   return;   
 }
 
 
	if( val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) != -1 
		||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[A-Z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) 
		||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1 && val.search(/[0-9]/) 
		||val.length >= tamanho  && val.search(/[@!#$%&*+=?|-]/) != -1 && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) )
		{
  			document.getElementById('d_baixa').style.background = 'green';
 				document.getElementById('d_media').style.background = 'green';
 				document.getElementById('d_alta').style.background = 'green';
  		
  	} else{
  		if( val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[A-Z]/) != -1 
  			||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[0-9]/) != -1 
  			||val.length >= tamanho && val.search(/[a-z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1
				||val.length >= tamanho && val.search(/[A-Z]/) != -1 && val.search(/[0-9]/) != -1
				||val.length >= tamanho && val.search(/[A-Z]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1
				||val.length >= tamanho && val.search(/[0-9]/) != -1 && val.search(/[@!#$%&*+=?|-]/) != -1){
  			
  				document.getElementById('d_baixa').style.background = 'yellow';
 				document.getElementById('d_media').style.background = 'yellow';
 				document.getElementById('d_alta').style.background = 'white';
  				
  			} 
  			else {
  				if(val.length >= tamanho)
  				{
  					document.getElementById('d_baixa').style.background = 'red';
 					document.getElementById('d_media').style.background = 'white';
 					document.getElementById('d_alta').style.background = 'white';
  					
  				}
  		}
  	}
  }
// Função para abrir janela popup
function abrir_popup( pagina,altura,largura ){
window.open(pagina, "" , "height="+altura+",width="+largura+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=NO");
}
function borda_input( campo,top,left,right,bottom ){

document.getElementById( campo ).style.borderTop = top;
document.getElementById( campo ).style.borderLeft = left;
document.getElementById( campo ).style.borderRight = right;
document.getElementById( campo ).style.borderBottom = bottom;

}
// Função para buscar pelo domínio do cliente
function buscar_dominio_servico_cliente( chave ){
partes = chave.split(",");
if(partes['1'] == "h") {
window.location = "index.php?pagina=Busca&chave="+partes['0']+"&cat=dominio";
} else if(partes['1'] == "d") {
window.location = "index.php?pagina=Busca&chave="+partes['0']+"&cat=dominio2";
} else {
window.location = "index.php?pagina=Busca&chave="+partes['0']+"&cat=servico_codigo";
}
}