Promocao = {
    
    filter: {
        
        clearString: function(str){
            return str.replace(/(['-.,:\/])/g, "");
        }
    },
    
    validator: {
        
        cpf: function(value) {
        
            value = Promocao.filter.clearString(value);

            if (value.length != 11 || value == "00000000000" || value == "11111111111" ||	value == "22222222222" ||
            value == "33333333333" || value == "44444444444" ||	value == "55555555555" || value == "66666666666" ||
            value == "77777777777" || value == "88888888888" || value == "99999999999"){
                    return false;
            }

            var soma = 0;
            for (var i=0; i<9; i++) soma += parseInt(value.charAt(i)) * (10 - i);
            var resto = 11 - (soma % 11);
            if (resto == 10 || resto == 11) resto = 0;
            if (resto != parseInt(value.charAt(9))) return false;

            var soma = 0;
            for (var i=0; i<10; i++) soma += parseInt(value.charAt(i)) * (11 - i);
            var resto = 11 - (soma % 11);
            if (resto == 10 || resto == 11) resto = 0;
            if (resto != parseInt(value.charAt(10))) return false;

            return true;
        }
    },
    
    init: function(){
        
        $('#promocao_form').submit( Promocao.sendForm );
    },
    
    sendForm: function(){
        
        var formOK = true;
        $('#promocao_form input').each(function(i, e){
            
            switch( $(e).attr('name') ){
                case 'data_nascimento':
                    
                    if( $(e).val() == 'Data Nascimento' || $(e).val() == ''){
                        alert('Informe a Data de Nascimento'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
                case 'nome':
                    
                    if( $(e).val() == 'Nome' || $(e).val() == ''){
                        alert('Informe seu nome'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
                    
                case 'email':
                    if( $(e).val() == 'Email' || $(e).val() == ''){
                        alert('Informe seu email'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
                case 'cpf':
                    if( $(e).val() == 'CPF' || $(e).val() == '' || !Promocao.validator.cpf( $(e).val() ) ){
                        alert('Informe um CPF válido.'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
                case 'rg':
                    if( $(e).val() == 'RG' || $(e).val() == ''){
                        alert('Informe seu RG'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
               case 'endereco':
                    if( $(e).val() == 'Endereço' || $(e).val() == ''){
                        alert('Informe seu endereço'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
                    
               case 'resposta':
                    if( $(e).val() == 'Resposta' || $(e).val() == ''){
                        alert('Deixe sua resposta'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
                    
               case 'optin':
                    if( $(e).attr('checked') == false ){
                        alert('Por favor, leia e aceite o Regulamento desta promoção'); 
                        formOK = false;
                        $(e).focus();
                        return false;
                    }
                    break;
            }
        });
        
        if(!formOK ) return false;
        var dados = $('#promocao_form').serialize();
        Promocao.resetForm('promocao_form');
        
        $('#dialog_wait').dialog({

            title: 'Enviando dados...',
            resizable: false,
            draggable: false,
            modal: true
        });
        $('#dialog_wait').prev('div.ui-dialog-titlebar').children('a.ui-dialog-titlebar-close').css('display', 'none');
        $('#dialog_wait p').text( 'Aguarde, enviando cadastro...' );
                
        $.ajax({
            type	: "POST",
            url	: PATH+"promocoes/inserir-participante",
            global	: false,
            data	: dados,
            dataType: "html",
            height: 400,
            success	: function(content){
                
                $('#dialog_wait').dialog('close');
                $('#dialog_wait').dialog({

                    title: '',
                    modal: false,
                    resizable: false,
                    buttons:{
                        Fechar: function(){
                            $(this).dialog('close');
                        }
                    },
                    close: 'slide',
                    show: 'drop'
                });
                
                $('#dialog_wait p').text( content );
                $('#dialog_wait').prev('div.ui-dialog-titlebar').children('a.ui-dialog-titlebar-close').css('display', 'block');
            }
        });
        return false;
    },
    
    resetForm: function( idForm ){
        
        $(':input, #'+idForm).not(':button, :submit, :reset, :hidden').val('');
    },
    
    response: function( xhr ){
        
        try {
            
            alert( xhr );
        } catch ( e ) {
            
            alert( e.toString() );
        }

    },
    
    regulamento: function(){
        
        var regulamento = $('#box_regulamento');
        
        if( regulamento.css('display') == 'none' ){
            //regulamento.css({display:'block'});
            regulamento.animate({
                height:'toggle'
            }, 500, 'linear');
            $('#a_regulamento').text('Ocultar o Regulamento');
            $('#a_regulamento_bottom').css('display', 'block');
        }
        else{
            regulamento.animate({
                height:'toggle'
            }, 500, 'linear');
            $('#a_regulamento').text('Leia o Regulamento');
            $('#a_regulamento_bottom').css('display', 'none');
        }
    }
}

