/*********************************************************************
 * author: Leonardo Matias                                           *
 * mail  : valgallom@yahoo.com.br                                    *
 * msn   : valgallom@hotmail.com                                     *
 *********************************************************************/

function InCompany(baseUrl) {
    this.baseUrl = baseUrl;

    this.validaForm = function() {
        var form = document.getElementById('form');
        var field;
        var index = null;
        var fieldNull = new Array();

        var listField = new Array(new Array('cursoid','Curso',null,false),
                                  new Array('periodoDe','Período de','date',true),
                                  new Array('periodoAte','Período até','date',true),
                                  new Array('qtd_pessoas','Quant. Pessoas','int',true),
                                  new Array('nome','Empresa',null,true),
                                  new Array('responsavel','Responsável',null,true),
                                  new Array('endereco','Endereço',null,true),
                                  new Array('cidade','Cidade',null,true),
                                  new Array('estado','Estado',null,true),
                                  new Array('cep','CEP','cep',true),
                                  new Array('telefone','Telefone','fone',true),
                                  new Array('fax','Fax','fone',false),
                                  new Array('email','E-mail','email',true),
                                  new Array('cnpj','CNPJ','cnpj',true),
                                  new Array('pagto','Forma de Pagamento',null,true),
                                  new Array('obs_curso','Caso não encontre o seu curso, descreva-o',null,false)
                                 );
        
        for(index in listField) {
            field = document.getElementById(listField[index][0]);
            
            field.value = trim(field.value);
            if(listField[index][3] || field.value!=''){
                if (listField[index][2]!=null && field.value!='') {
                    switch (listField[index][2]) {
                        case 'date':
                                if(!validateDate(field.value,'d/m/a')) {
                                    fieldNull.push(new Array(listField[index][1]));
                                }
                                break;
                        case 'fone':
                                if(!validateTelefone(field.value)) {
                                    fieldNull.push(new Array(listField[index][1]));
                                }
                                break;
                        case 'email':
                                if(!validateEmail(field.value)) {
                                    fieldNull.push(new Array(listField[index][1]));
                                }
                                break;
                        case 'cnpj':
                                if(!validateCNPJ(field.value)) {
                                    fieldNull.push(new Array(listField[index][1]));
                                }
                                break;
                        case 'int':
                                if(!validateInteger(field.value)) {
                                    fieldNull.push(new Array(listField[index][1]));
                                }
                                break;
                        case 'cep':
                                if(!validateCep(field.value)) {
                                    fieldNull.push(new Array(listField[index][1]));
                                }
                                break;
                    }
                } else if (field.value==''){
                    fieldNull.push(new Array(listField[index][1]));
                }
            }
        }
        
        if(fieldNull.length>0){
            alert('Os campos '+fieldNull.join(', ')+' são de preechimento obrigatório ou inválidos!');
            return false;
        }

        field = document.getElementById('cursoid');
        if(field.value=="0") {
            field = document.getElementById('obs_curso');
            if(field.value=="") {
                alert('Erro: Não foi informado Curso desejado!');
                return false;
            }
        }

       form.submit();

    }

    // Busca os periodo do curso selecionado
    this.ajaxInstrutor = function(obj) {
        this.objAjax = new AjaxObject("POST",this.baseUrl+'/ajax/ajaxInstrutor.php');
        this.objAjax.addRequestValue("cursoid", obj.value);
        this.objAjax.setCallBackFunction(this.callBackInstrutor, new Array());
        this.objAjax.load();
    }

    this.callBackInstrutor = function (reponse,layersName) {
        if(reponse) {
            var tdInstrutor = document.getElementById('tdInstrutor');
            tdInstrutor.innerHTML = reponse;
        }
    }

}
