/*$(document).ready(function() {
    document.onkeypress = bloquearCopia;
    document.onkeydown = bloquearCopia;
    document.oncontextmenu = mensagem;
    $('body').attr("onselectstart", "return false");
});*/


function mensagem(){
    alert('Conteudo bloqueado!');
    return false;
}
function bloquearCopia(Event){
    var Event = Event ? Event : window.event;
    var tecla = (Event.keyCode) ? Event.keyCode : Event.which;
    if(tecla == 17){
        mensagem();
    }
}

function lembrar(){												  
    $('#lembrarSenha').animate({
        height: 'toggle'
    }, 'slow');
}

function validaEmail(email)
{
    if(email == "")
    {
        alert('Você precisa digitar um endereço de e-mail! ');
        return false;
    }

    if(email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
    {
        alert('Você precisa digitar um endereço de e-mail valido! ');
        return false;
    }

    return true;
}

//------------------------//------------------------------------//----------------


/*
 * Função para para por determinado tempo
 * @param milliseconds milisegundos
 */
function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
            break;
        }
    }
}

//-----------------------------------------------------------------------------//
// VALIDA E-MAIL
//-----------------------------------------------------------------------------//

function validaEmail(email)
{
    if(email == "")
    {
        alert('Você precisa digitar um endereço de e-mail! ');
        return false;
    }

    if(email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
    {
        alert('Você precisa digitar um endereço de e-mail valido! ');
        return false;
    }

    return true;
}

function formataMoeda(campo,evt) {
   
    //para evitar caracteres alfas.
    if(((evt.keyCode < 96) || (evt.keyCode > 105)) && ((evt.keyCode < 48) || (evt.keyCode > 57)) ){
        campo.value = campo.value.replace(String.fromCharCode(evt.keyCode).toLowerCase(),"");
    }
    str = campo.value;

    while(str.search(",") != -1)
        str = str.replace(",","");
   
    var i = 0;

    while(i< str.length){
        if(str.substr(i,1) == ".")
            str = str.replace(".","");
        i++;
    }

    part1 = str.substr(0,str.length - 2);
    while(part1.search(" ") != -1)
        part1 = part1.replace(" ","");

    part2 = str.substr(str.length - 2,2);
    res = "";
    i = part1.length;
    sob = i % 3;
    if((sob != 0) && (i > 2))
        res = part1.substr(0,sob) + ".";
    else
        res = part1.substr(0,sob);
    j = 1;
    part1 = part1.substr(sob);
    i = 0;
    while(i < part1.length){
        if(j == 3){
            if(i + 1 == part1.length)
                res = res + part1.substr(i-2,3);
            else res = res + part1.substr(i-2,3) + ".";
        }
        i++;
        j = j<3?j+1:1;
    }
    campo.value = res + "," + part2;
	   
    if (campo.value == ',')
        campo.value = '';

}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // %        nota 1: Para 1000.55 retorna com precisão 1 no FF/Opera é 1,000.5, mas no IE é 1,000.6
    // *     exemplo 1: number_format(1234.56);
    // *     retorno 1: '1,235'
    // *     exemplo 2: number_format(1234.56, 2, ',', ' ');
    // *     retorno 2: '1 234,56'
    // *     exemplo 3: number_format(1234.5678, 2, '.', '');
    // *     retorno 3: '1234.57'
    // *     exemplo 4: number_format(67, 2, ',', '.');
    // *     retorno 4: '67,00'
    // *     exemplo 5: number_format(1000);
    // *     retorno 5: '1,000'
    // *     exemplo 6: number_format(67.311, 2);
    // *     retorno 6: '67.31'

    decimals = decimals == '' ? 2 : decimals;

    var n = number, prec = decimals;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
    var dec = (typeof dec_point == "undefined") ? '.' : dec_point;

    var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = Math.abs(n).toFixed(prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
        _[0].slice(i).replace(/(\d{3})/g, sep+'$1');

        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    return s;
}

//---------------------------------------------------------------//
// MASK
//---------------------------------------------------------------//

function MASK(form) {
	
    $(form + ' input[mask=telefone]').each( function() {
		
        $(this).mask("(99) 9999-9999");
	
    });

    $(form + ' input[mask=cep]').each( function() {
		
        $(this).mask("99999-999");
	
    });

    $(form + ' input[mask=cpf]').each( function() {
		
        $(this).mask("999.999.999-99");
	
    });

    $(form + ' input[mask=cnpj]').each( function() {
		
        $(this).mask("99.999.999/9999-99");
	
    });

    $(form + ' input[mask=data]').each( function() {
		
        $(this).mask("99/99/9999");
	
    });

    $(form + ' input[mask=ano_modelo]').each( function() {
		
        $(this).mask("9999/9999");
	
    });

    $(form + ' input[mask=moeda]').each( function() {
		
        $(this).keyup( function(event) {
			
            formataMoeda(this,event);
								
        });

    });
	
    $(form + ' input[mask=dia_mes]').each( function() {
		
        $(this).mask("99/99");
	
    });
	
    $(form + ' input[mask=time]').each( function() {
		
        $(this).mask("99:99:99");
	
    });
    $(form + ' input[mask=hora_minuto]').each( function() {
		
        $(this).mask("99:99");
	
    });
	
    $(form + ' input[mask=placa]').each( function() {
		
        $(this).mask("aaa-9999");
	
    });
	
    $(form + ' input[mask=numero]').each( function() {
		
        $(this).keyup( function(event) {
								
            if(((event.keyCode < 96) || (event.keyCode > 105)) && ((event.keyCode < 48) || (event.keyCode > 57)) ){
                $(this).val( $(this).val().replace(String.fromCharCode(event.keyCode).toLowerCase(),"") );
            }

            //verifica se não é numero
            if (isNaN($(this).val()))
                $(this).val("");

		
        });

    });

}

//--------------------------------------//
//      Validações  
//--------------------------------------//


var Validacao = {
    'AgendaServico' : function(form)
    {
        if ( $('#data','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo data! ');
            $('#data','#'+form).focus();
            return false;
        }
        
        if ( $('#placa','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo placa! ');
            $('#placa','#'+form).focus();
            return false;
        }
        
        if ( $('#km','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo km! ');
            $('#km','#'+form).focus();
            return false;
        }
        
        if ( $('#nome-razao','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome / razão Social! ');
            $('#nome-razao','#'+form).focus();
            return false;
        }
        
        if ( $('#cpf-cnpj','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo CPF / CNPJ! ');
            $('#cpf-cnpj','#'+form).focus();
            return false;
        }

        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }

        if ( $('#celular','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo celular! ');
            $('#celular','#'+form).focus();
            return false;
        } 
        
        if ( $('#pessoa-contato','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo pessoa de contato! ');
            $('#pessoa-contato','#'+form).focus();
            return false;
        }  
        
        if ( $('#servicos','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo serviços desejados! ');
            $('#servicos','#'+form).focus();
            return false;
        }       
        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;      
    },
    
    'Locadora' : function(form)
    {
        if ( $('#razaosocial','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo Razão Social! ');
            $('#razaosocial','#'+form).focus();
            return false;
        }
        if ( $('#cpf-cnpj','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo cpf-cnpj! ');
            $('#cpf-cnpj','#'+form).focus();
            return false;
        }

        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }

        if ( $('#telefone','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo telefone! ');
            $('#telefone','#'+form).focus();
            return false;
        }       
        if ( $('#data_retirada','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo data retirada! ');
            $('#data_retirada','#'+form).focus();
            return false;
        }       
        if ( $('#hora_retirada','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo hora da retirada! ');
            $('#hora_retirada','#'+form).focus();
            return false;
        }       
        if ( $('#data_entrega','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo data de entrega! ');
            $('#data_entrega','#'+form).focus();
            return false;
        }       
        if ( $('#hora_entrega','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo hora de entrega! ');
            $('#hora_entrega','#'+form).focus();
            return false;
        } 
        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;      
    },
    
    'Cadastro': function(form)
    {

        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }

        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }

        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;
    },
    
    'Contato': function(form)
    {

        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }

        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }

        if ( $('#telefone','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo telefone! ');
            $('#telefone','#'+form).focus();
            return false;
        }

        if ( $('#assunto','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo assunto! ');
            $('#assunto','#'+form).focus();
            return false;
        }

        if ( $('#mensagem','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo mensagem! ');
            $('#mensagem','#'+form).focus();
            return false;
        }
        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;
    },
    
    'Reserva': function(form)
    {

        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }

        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }

        if ( $('#telefone','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo telefone! ');
            $('#telefone','#'+form).focus();
            return false;
        }

        if ( $('#sede','#'+form).val() == "" || $('#sede','#'+form).val() == 0)
        {
            alert('Você precisa selecionar uma sede! ');
            return false;
        }

        if ( $('#uf','#'+form).val() == "" || $('#uf','#'+form).val() == 0)
        {
            alert('Você precisa selecionar um estado! ');
            return false;
        }

        if ( $('#id_cidade','#'+form).val() == "" || $('#id_cidade','#'+form).val() == 0)
        {
            alert('Você precisa selecionar uma cidade! ');
            return false;
        }

        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;
    },
    
    'Login': function(form)
    {

        if ( $('#codigo-matricula','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo Código de matrícula! ');
            $('#codigo-matricula','#'+form).focus();
            return false;
        }

        if ( $('#senha','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo senha! ');
            $('#senha','#'+form).focus();
            return false;
        }

        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            if(retorno == "OK") {
                document.location.href = "area-restrita.php";
            } else {
                alert(retorno);
                document.getElementById(form).reset();
            }
            return false;
        });
		
        return false;
    },
    
    'Lembrar': function(form)
    {
        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;
    }
}
