
function AdsrtWiki(formName, fieldName, index) {
    var self = new AdsrtSourceCode(formName, fieldName, index);
    return self;
}

function AdsrtCreditCardNumber(formName, fieldName, index) {
    var self = new AdsrtUpper(formName, fieldName, index);
    return self;
}

function AdsrtRegexp(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtEMail(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Verifica se o e-mail informado:
     * 1) Tem um único "@"
     * 2) Tem pelo menos um "." após o "@".
     */
    self.validate = function() {
        var str = this.getValue();
        var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
        if (str.length == 0 || str.match(re)) {
            this.setErrorMessage("");
        } else {
            this.setErrorMessage("Invalid e-mail address!");
        }
    }
    return self;
}

function AdsrtDateTime(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    
    /**
     * Interpreta e valida o que foi digitado dentro do campo conforme as
     *  regras para data e hora.
     */
    self.validate = function() {
        datetime_validate(ctl,"HMS");
    }
    return self;
}

function AdsrtDateTimeHm(formName, fieldName, index) {
    var self = new AdsrtDateTime(formName, fieldName, index);
    return self;
}

function AdsrtCmc7(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Remover todos os caracteres, deixando apenas os números
     */
    self.toNumbers = function() {
        ctl.value=get_numbers( ctl.value );
        while (ctl.value.charAt(ctl.value.length-1) == " "){ 
            ctl.value = ctl.value.substr(0, ctl.value.length-1); 
        } 
    }
    return self;
}

function AdsrtOptionRadio(formName, fieldName, index) {
    var self = new AdsrtOpcao(formName, fieldName, index);
    return self;
}

function AdsrtTime(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    
    /**
     * Validar a hora digitada no format HH:MM
     */
    self.validarHora = function() {
        var ok = time_format(ctl.value,"HM");
        if (ok.charAt(0) == "*") {
            ctl.focus();
            alert(ok.substr(1));
        } else {
            ctl.value = ok;
        }
    }
    return self;
}

function AdsrtDouble(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    return self;
}

function AdsrtInteger(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    return self;
}

function AdsrtLower(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     */
    self.toLower = function() {
        ctl.value = ctl.value.toLowerCase();
        while (ctl.value.charAt(ctl.value.length-1) == " "){
            ctl.value = ctl.value.substr(0, ctl.value.length-1);
        }
    }
    return self;
}

function AdsrtSelectionBox(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtDate(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    return self;
}

function AdsrtValueDc(formName, fieldName, index) {
    var self = new AdsrtValue(formName, fieldName, index);
    return self;
}

function AdsrtNcm(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtPasswordHash(formName, fieldName, index) {
    var self = new AdsrtPassword(formName, fieldName, index);
    
    /**
     * Copia os dados digitados para o valor de forma a validar o preenchimento
     *  do campo
     * (depois roda o SHA1)
     */
    self.copy = function() {
        this.setValue(this.getZoneElement("inp").value);
    }
    
    /**
     * Quando formulário for submetido, o valor digitado deve ser substituído
     *  pelo SHA1 correspondente.
     * Por motivo de segurança, o campo da tela é limpado para não ser enviado
     *  no post.
     */
    self.preSubmit = function() {
        this.setValue(SHA1(this.getValue()));
    }
    return self;
}

function AdsrtSelectionCheck(formName, fieldName, index) {
    var self = new AdsrtSelectionBox(formName, fieldName, index);
    return self;
}

function AdsrtPhone(formName, fieldName, index) {
    var self = new AdsrtUpper(formName, fieldName, index);
    return self;
}

function AdsrtCpf(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Rotina de executa a validação do CPF informado.
     */
    self.validaCpf = function() {
        cpfValidate(ctl);
    }
    return self;
}

function AdsrtInscricaoMunicipal(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Rotina que faz o calculo da validação da Inscrição Municipal informada.
     * Caso identifique erro, uma exceção é disparada.
     */
    self.validarInscricao = function() {
        // Valida o código informado
        //--> 
        //if ( ads != null ) {
        //     ads.includeJava... ( "lib/documents.js" );
        //}
        //--> executa a rotina de calculo do código nas bibliotecas do ADS
    }
    return self;
}

function AdsrtOpcao(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtId(formName, fieldName, index) {
    var self = new AdsrtBigInteger(formName, fieldName, index);
    return self;
}

function AdsrtSourceCode(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtTextHtml2(formName, fieldName, index) {
    var self = new AdsrtTextHtml(formName, fieldName, index);
    return self;
}

function AdsrtRg(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Rotina que faz o calculo da validação da Carteira de Identidade
     *  informada.
     */
    self.validarRg = function() {
        rgValidate(ctl);
    }
    return self;
}

function AdsrtSexo(formName, fieldName, index) {
    var self = new AdsrtOpcao(formName, fieldName, index);
    return self;
}

function AdsrtPercent(formName, fieldName, index) {
    var self = new AdsrtDouble(formName, fieldName, index);
    return self;
}

function AdsrtCheck(formName, fieldName, index) {
    var self = new AdsrtFlag(formName, fieldName, index);
    return self;
}

function AdsrtCnpj(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Rotina de executa a validação do CNPJ informado.
     */
    self.validarCnpj = function(ctl) {
        cnpjValidate(ctl);
    }
    return self;
}

function AdsrtNumber(formName, fieldName, index) {
    var self = new AdsrtDouble(formName, fieldName, index);
    return self;
}

function AdsrtUrl(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtTextLong(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtDayTime(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtIcon(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtPassword(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Se o campo for um RETYPE, verifica se a senha digitada aqui é igual ao
     *  do nome base.
     */
    self.validate = function() {
        if (this.fieldName.substring(0,7)=="RETYPE_") {
            var name = this.fieldName.substring(7,99);
            var retype = this.formName + "-val-" +
                name + "-" + this.index;
            var elem = document.getElementById(retype);
            if (elem) {
                var d2 = getDomain(elem);
                var v1 = this.getValue();
                var v2 = d2.getValue();
                if (v1 != v2) {
                    this.setErrorMessage("As senhas digitadas devem ser iguais!");
                }
            }
        }
    }
    return self;
}

function AdsrtImageUrl(formName, fieldName, index) {
    var self = new AdsrtUrl(formName, fieldName, index);
    return self;
}

function AdsrtUpperAlphanumeric(formName, fieldName, index) {
    var self = new AdsrtUpper(formName, fieldName, index);
    
    /**
     * Passa o texto para maiúsculo e retira todos os espaços do final e
     *  acentos ou carateres especiais.
     */
    self.adjustValue = function() {
        ctl.value = ctl.value.toUpperCase();
        while (ctl.value.charAt(ctl.value.length-1) == " "){
            ctl.value = ctl.value.substr(0, ctl.value.length-1);
        }
        ctl.value = fisicalName(ctl.value);
    }
    return self;
}

function AdsrtAsc6(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtTextHtml(formName, fieldName, index) {
    var self = new AdsrtTextLong(formName, fieldName, index);
    return self;
}

function AdsrtReal(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    return self;
}

function AdsrtMes(formName, fieldName, index) {
    var self = new AdsrtOpcao(formName, fieldName, index);
    return self;
}

function AdsrtPrice(formName, fieldName, index) {
    var self = new AdsrtValue(formName, fieldName, index);
    return self;
}

function AdsrtProdutoArezzo(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtLogin(formName, fieldName, index) {
    var self = new AdsrtLower(formName, fieldName, index);
    return self;
}

function AdsrtInscricaoEstadual(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Rotina que faz o calculo da validação da Inscrição Estadual informada.
     * Caso identifique erro, uma exceção é disparada.
     */
    self.validarInscricao = function() {
        // Valida o código informado
        //--> 
        //if ( ads != null ) {
        //     ads.includeJava... ( "lib/documents.js" );
        //}
        //--> executa a rotina de calculo do código nas bibliotecas do ADS
    }
    return self;
}

function AdsrtHtmlarea(formName, fieldName, index) {
    var self = new AdsrtTextLong(formName, fieldName, index);
    return self;
}

function AdsrtCombo(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtWeekDays(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Calcula o valor do atributo conforme a informação dos check-boxes.
     */
    self.calcularAtributo = function() {
        var dia=ctl.name.substr(1,1);
        var f=document.getElementById("A_"+ctl.name.substr(3));
        
        if (ctl.checked) {
            f.value=trim(f.value)+dia;
        } else {
            var temp = "";
            for (var i=0; i<f.value.length; i++) {
                var c=f.value.charAt(i);
                if (c != dia) { temp=temp+c; }
            }
            f.value=trim(temp);
        }
    }
    return self;
}

function AdsrtBigInteger(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    return self;
}

function AdsrtGridQuantity(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtFileUpload(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtFlagYn(formName, fieldName, index) {
    var self = new AdsrtFlag(formName, fieldName, index);
    
    /**
     * Ação executada sempre que o usuário clica no check-box.
     * Atualiza o valor do domínio conforme a conveção.
     */
    self.click = function() {
        var chk = this.getZoneElement("inp");
        var val = this.getZoneElement("val");
        if (chk.checked) {
            val.value = "Y";
        } else {
            val.value = "N";
        }
    }
    return self;
}

function AdsrtValue(formName, fieldName, index) {
    var self = new AdsrtDouble(formName, fieldName, index);
    return self;
}

function AdsrtCep(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtText(formName, fieldName, index) {
    var self = new Domain(formName, fieldName, index);
    return self;
}

function AdsrtImageUrlList(formName, fieldName, index) {
    var self = new AdsrtImageUrl(formName, fieldName, index);
    return self;
}

function AdsrtUpper(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Passa o texto para maiúsculo e retira todos os espaços do final. 
     */
    self.toUppercase = function() {
        ctl.value=ctl.value.toUpperCase();
        while (ctl.value.charAt(ctl.value.length-1) == " "){ 
            ctl.value = ctl.value.substr(0, ctl.value.length-1); 
        } 
    }
    return self;
}

function AdsrtButton(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    return self;
}

function AdsrtFlag(formName, fieldName, index) {
    var self = new AdsrtText(formName, fieldName, index);
    
    /**
     * Ação executada sempre que o usuário clica no check-box.
     * Atualiza o valor do domínio conforme a conveção.
     */
    self.click = function() {
        var chk = this.getZoneElement("inp");
        var val = this.getZoneElement("val");
        if (chk.checked) {
            val.value = "S";
        } else {
            val.value = "N";
        }
    }
    return self;
}

