﻿/*
* Recupera a instancia do objeto no client.
*/
function GetObject(controlName) {
    return document.getElementById(GetClientID(controlName));
}

/*
* Recupera o nome do controle gerado no Browser.
*/
function GetClientID(controlName) {
    return "ctl00_ContentPlaceHolder1_" + controlName;
}

// JScript File
function BrandsValidate() {
    
    var brand = GetObject("txtAccountBrand");
    if (brand.value == ""){
        alert("O nome da marca é obrigatório.");
        brand.focus();
        return false;
    }
    else {
        if (brand.value.length < 2){
            alert("O nome da marca deve ter no mínimo 2 caracteres.");
            brand.focus();
            return false;
        }
    
    }

    return true;
}
 
/*
* Valida o cadastro de um novo endereço/
*/
function AddressValidate() {
    var name = GetObject("txtAddressName");
    var postalCode = GetObject("txtAddressPostalCode");
    var city = GetObject("DropDownList_City");
    var line1 = GetObject("txtAddressLine1");
    var line2 = GetObject("txtAddressLine2");
    var line3 = GetObject("txtAddressLine3");
    //var site = GetObject("txtAddressSite");
    var state = GetObject("DropDownList_State");
    var country = GetObject("DropDownList_Country");
    
    var message = "";
    
    if (name.value == "" || name.value.length < 3) {
        message = "O campo nome do Endereço é obrigatório.\n\n";
           
    }
    
    if (line1.value == "") {
        message += "O campo Logradouro é obrigatório.\n\n";
        
    }
     
    if (line3.value == "") {
        message += "O campo Bairro é obrigatório.\n\n";
       
    }
    else {
        if (line3.value.length < 3){
            message += "O campo Bairro é inválido.\n\n";
            
        }
    }
    
    
    if (postalCode.value == "") {
        message += "O campo CEP é obrigatório.";
       
    }
    else {
        if (postalCode.value.length < 8) {
            message += "O campo CEP é inválido.";
            
        }    
    }
    
    if (message != ""){
    
        alert(message);
        return false;
    }
    return true;
}

/*
* Método que valida a exclusão de um item da grid.
*/
function DeleteValidate() {
    return confirm("Tem certeza que deseja excluir o registro selecionado?");
}

/*
* Método que valida o cadastro do contato.
*/
function ContactValidate() {

    var name            = GetObject("txtContactName");
    var email           = GetObject("txtContactEmail");
    var phone           = GetObject("txtContactPhone");
    var fax             = GetObject("txtContactFax");
    var mobile          = GetObject("txtContactMobile");
    
    var msg = "";
    
    if (name.value == "") {
        msg = "Nome do Contato é obrigatório.\n\n";
        
    }
    else {        
        if (name.value.length < 3) {
            msg += "Nome do Contato inválido.\n\n";
           
        }    
    }
    
    if (email.value == "") {
        msg += "O Email é obrigatório.\n\n";
    }
    if (email.value.length > 0) {
        if (!ValidateEmail(email.value)) {
            msg += "O Email é inválido.\n\n";
         
        }
    }
    
    if (phone.value == "") {
        msg += "Telefone do contato é obrigatório.";
        
    }
    else {
    
        if (phone.value.length < 8) {
            msg += "O Telefone é inválido";
           
        }
    }
    
    if (mobile.value != "" && mobile.value.length < 8) {
        alert += "O Celular é inválido.";
        
    }
    
    
    if (fax.value != "" && fax.value.length < 8) {
        alert += "O Fax é inválido.";
    }
    
    if (msg != ""){
        alert(msg);
        return false;
    
    }
    
    return true;
}

/*
* Valida dados referente a conta.
*/
function AccountValidate() {

    var accountDate = GetObject("txtAccountDate");
    var lastDate = GetObject("TextBox_LastDate");
    var cnpj = GetObject("txtAccountDocument");
    var responsabilityName = GetObject("TextBox_ResponsabilityName");
    var responsabilityFunction = GetObject("TextBox_ResponsabilityFunction");
    var email = GetObject("TextBox_Email");
    
    var simRepresenta = GetObject("CheckBoxSimRepresenta");
    var naoRepresenta = GetObject("CheckBoxNaoRepresenta");
    var simExporta = GetObject("CheckBoxSimExporta");
    var naoExporta = GetObject("CheckBoxNaoExporta");
    var emailPrincipal = GetObject("TextBoxEmailPrincipal");
    
    var msg = "";
    
    if (accountDate.value.length > 0) {
    
        if (!DateValidate(accountDate)) {
            msg = 'Data de Fundação inválida.\n\n';
            
        }
    
    }
   
    if (lastDate.value.length > 0) {
       if (!DateValidate(lastDate)) {
            msg += 'Data de Certificação inválida.\n\n';
           
        }
    }
    
    if (cnpj.value.length > 0){
        
        if (!CnpjValidate(cnpj.value)) {
            msg += "Cnpj inválido.\n\n";
            
        }
    }
    
    if (emailPrincipal.value == ""){
        msg += "O Email Principal é obrigatório.\n\n";
        emailPrincipal.style.borderColor = "red";
    }
    else {    
        if (!ValidateEmail(emailPrincipal.value)){
            msg += "O Email Principal é inválido.\n\n";            
        }
    }
    
    if (!simExporta.checked && !naoExporta.checked) {
        msg += "Por favor informe se a sua empresa Exporta ou não.\n\n";
         
    }
    
    if (!simRepresenta.checked && !naoRepresenta.checked) {
        msg += "Por favor informe se a sua empresa representa ou não produtos no exterior.\n\n";
          
    }
    
    if (responsabilityName.value == ""){
        msg += "O Nome do responsável é obrigatório.\n\n";
        responsabilityName.style.borderColor = "red";
    }
    
    if (responsabilityFunction.value == ""){
        msg += "O Cargo do responsável é obrigatório.\n\n";
       responsabilityFunction.style.borderColor = "red";
    }
    
    if (email.value == ""){
        msg += "O Email do responsável é obrigatório.\n\n";
        email.style.borderColor = "red";
    }
    else {
    
        if (!ValidateEmail(email.value)){
            msg += "O Email do responsável é inválido.\n\n";
            
        }
    }
    
    if (msg != ""){
        alert(msg);
        return false;
    
    }
    return true;
}


function ValidateEmail(email){
    var arr = email.split('@');
    
    parte1 = email.indexOf("@");
    parte2 = email.indexOf(".");
    parte3 = email.length;

    if (!(parte1 >= 2 && parte3 >= 9)) {
              return false;
    }
    
    
    if (arr.length > 2) {
        return false;
    }
    
    return true;
}

/*
* Valida o click nos checkbox da lista de estruturas industriais. 
*/
function ValidateIndustrialStructure(simId, naoId, scId, action) {
    
    var oSim = document.getElementById(simId);
    var oNao = document.getElementById(naoId);
    var oSC = document.getElementById(scId);
    
    if (action == "sim") {
        ValidateIndustrialStructureSim(oNao);
    } else {
    
        if (action == "nao") {
            ValidateIndustrialStructureNao(oSim,oSC);
        } else {
            
            ValidateIndustrialStructureSC(oNao);
        }
    }    

}

/*
* Valida o click no checkbox Sim da lista de estruturas industriais. Isso porque quando este item é selecionado
* O item "Não" deve ser desmarcado.
*/
function ValidateIndustrialStructureSim(oNao) {
    oNao.checked = false;
}

/*
* Valida o click no checkbox Não da lista de estruturas industriais. Isso porque quando este item é selecionado
* O item "Sim" e "SC" devem ser desmarcados.
*/
function ValidateIndustrialStructureNao(oSim, oSC) {    
    oSim.checked = false;
    oSC.checked = false;
}

/*
* Valida o click no checkbox SC da lista de estruturas industriais. Isso porque quando este item é selecionado
* O item "Não" deve ser desmarcado.
*/
function ValidateIndustrialStructureSC(oNao) {
    oNao.checked = false;
}

/*
*  Valida os checkbox SIM da seção de exporta produtos ou não
*/
function ValidateExportaSim(naoId) {

    document.getElementById(naoId).checked = false;
}


/*
*  Valida os checkbox NÃO da seção de exporta produtos ou não
*/
function ValidateExportaNao(simId) {

    document.getElementById(simId).checked = false;
}

/*
*  Valida os checkbox SIM da seção de Representa produtos ou não
*/
function ValidateRepresentaSim(naoId) {

    document.getElementById(naoId).checked = false;
}


/*
*  Valida os checkbox NÃO da seção de Representa produtos ou não
*/
function ValidateRepresentaNao(simId) {

    document.getElementById(simId).checked = false;
}

function mascaraCep(objeto){
	if (objeto.value.indexOf("-") == -1 && objeto.value.length > 5){ objeto.value = ""; }
	if (objeto.value.length == 5){
		objeto.value += "-";
	}
}

function WebSiteFormat(obj) {
    if (obj.value != "") {
      
        if(obj.value.indexOf("http://") == -1)
        {
           obj.value =  "http://" + obj.value;
        }
    }
}

function ToUpper(obj) {
    obj.value = obj.value.toUpperCase();
}

function FormatarCNAE(obj) {
    var value = obj.value.replace(".","").replace("-","").replace("-","");

    if (value.length != 7) {
        alert("Código CNAE é inválido");
        obj.focus();
    }
    else {    
        obj.value = value.substr(0,2) + "." + value.substr(2,2) + "-" + value.substr(4,1) + "-" + value.substr(5,2);
    }
}
