﻿function validaNome(val) {
    var texto = /^[a-zA-ZãÃáÁàÀêÊéÉèÈíÍìÌôÔõÕóÓòÒúÚùÙûÛçÇ\s]+$/;
    return texto.test(val);
};
 
function validaEmail(val) {
    var texto = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    return texto.test(val);
};

function somenteNumero(e){
	var tecla=(window.event)?event.keyCode:e.which; 
    if((tecla>47 && tecla<58)) return true;
    else{
    	if (tecla==8 || tecla==0) return true;
	else  return false;
    }
}

function bloqueiaNumeros(e){
	var tecla=(window.event)?event.keyCode:e.which; 
	tecla = String.fromCharCode(tecla);
	if ((tecla >= "0") && (tecla <= "9")){ return false;}
	else {return true;}
}

function soLetras(e){
	var tecla=(window.event)?event.keyCode:e.which; 
     if((tecla > 65 && tecla < 90)||(tecla > 97 && tecla < 122))return true;
    else{
    	if (tecla==8 || tecla==0) return true;
	else  return false;
    }
}


function displayErrorMessage(msg) {
    alert(msg);
};


function validateForm() {
    var valido = true;

    var Fields = $([{ field: $("#name"), dataType: "text", name: "nome", defaultErrorMessage: "* Nome Inválido" },
	{ field: $("#mail"), dataType: "email", name: "mail", defaultErrorMessage: "* Email Inválido" },
	{ field: $(".radio"), dataType: "check", name: "checkbox", defaultErrorMessage: "* Necessário selecionar um segmento" }]);

    //verifica se há campos vazios
    if (Fields.filter(function(index) {
        if (this.field.val() == "") {
            this.field.prev().addClass('invalidInput');
            return true;
        } else {
            this.field.prev().removeClass('invalidInput');
            return false;
        }
    }).length > 0) {
        valido = false;
        displayErrorMessage("* Preencha todos os campos obrigatórios.");
    } else {
        //nenhum campo vazio, proceder com a verificacao de cada campo
        var validType = true;
        Fields.each(function() {
            if (this.name == "nome") {
                if (!validaNome(this.field.val())) {
                    this.field.prev().addClass('invalidInput');
                    displayErrorMessage(this.defaultErrorMessage);
                    validType = false;
                    return false;
                }
            } else if (this.dataType == "email") {
                if (!validaEmail(this.field.val())) {
                    this.field.prev().addClass('invalidInput');
                    displayErrorMessage(this.defaultErrorMessage);
                    validType = false;
                    return false;
                }
            } else if(this.dataType == "check"){
				if($(':checked').length == 0){
					this.field.next().addClass('invalidInput');
					displayErrorMessage(this.defaultErrorMessage);
					validType = false;
					return false;
				}
			}
        }); if (!validType) {
            valido = false;
        }
    }
    return valido;
};

function validateCotacao() {
    var valido = true;

    var Fields = $([
	{ field: $("#nome"), dataType: "text", name: "nome", defaultErrorMessage: "* Nome Inválido" },
	{ field: $("#email"), dataType: "email", name: "mail", defaultErrorMessage: "* Email Inválido" },
	{ field: $("#empresa"), dataType: "default", name: "empresa", defaultErrorMessage: "* Nome da Empresa Inválida" },
	{ field: $("#cnpj"), dataType: "cnpj", name: "cnpj", defaultErrorMessage: "* CNPJ Inválido" },
	{ field: $("#quantidade"), dataType: "number", name: "quantidade", defaultErrorMessage: "* Quantidade Inválida" }
	]);

    //verifica se há campos vazios
    if (Fields.filter(function(index) {
        if (this.field.val() == "") {
            this.field.prev().addClass("invalidInput");
            return true;
        } else {
            this.field.prev().removeClass('invalidInput');
            return false;
        }
    }).length > 0) {
        valido = false;
        displayErrorMessage("* Preencha todos os campos obrigatórios.");
		
    } else {
        //nenhum campo vazio, proceder com a verificacao de cada campo
        var validType = true;
        Fields.each(function() {
            if (this.dataType == "email") {
                if (!validaEmail(this.field.val())) {
                    this.field.prev().addClass('invalidInput');
                     displayErrorMessage(this.defaultErrorMessage);
                    validType = false;
                    return false;
                }
            }
        }); if (!validType) {
            valido = false;
        }
    }
    return valido;
};

function menuDropDown(){
	$("li.subNivel1Show").hover(
	  function () {
		$(this).find("a").css({'background':'#dfdfdf','color':'#334048'});
		$(this).find(".subNivel1").css({"display":"block", "width":$(this).css("width")});
		$("li.subNivel2Show").hover(
			function () {
				$(this).children().css({'background':'#f2f3f3'});
				$(this).find('.subNivel2').css({"display":"block", "margin-left":$(this).css("width")});
			}, 
			function () {
				$("li.subNivel2Show a").css({'background':'#dfdfdf'});
				$(this).find('.subNivel2').css("display","none");
			}
		);
	  },
	  function () {
	    $(".subNivel1").css("display","none");
		$(".subNivel1Show a").css({'background':'#334048','color':'#fff'});
	  }
	);
};


function changeMenuSegment(){
	$(".category li").click(function(){	
		$('.produtoSegmento').removeClass("on");
		if($(this).attr("class").split(' ').slice(-1) == "opcao1"){
			$("#opcao1").addClass("on");
		}else if($(this).attr("class").split(' ').slice(-1) == "opcao2"){
			$("#opcao2").addClass("on");
		}else if($(this).attr("class").split(' ').slice(-1) == "opcao3"){
			$("#opcao3").addClass("on");
		}
	});
}

function identifySegment(){
	if(location.search.substr(1).split('?') == "1"){
		$("#opcao1").addClass("on");
	}else if(location.search.substr(1).split('?') == "2"){
		$("#opcao2").addClass("on");
	}else if(location.search.substr(1).split('?') == "3"){
		$("#opcao3").addClass("on");
	}
}

$(document).ready(function(){
    $("#submitNews").click(function(e) {
        e.preventDefault();
        if (validateForm()) $("#sendNews").submit();
    });
	
	$("#submitCotacao").click(function(e) {
        e.preventDefault();
        if (validateCotacao()) $("#sendCotacao").submit();
    });
	
	$("#InternalQuemSomos a").click(function(){
		$("#InternalQuemSomos").css("display","none");
		$("#InternalVisao").css("display","block");
	});
	
	$("#InternalVisao a").click(function(){
		$("#InternalQuemSomos").css("display","block");
		$("#InternalVisao").css("display","none");
	});
	
	$("#btnVoltarNews").click(function(){
		$(".secondNews").css("display","none");
		$(".firstNews").css("display","block");
	});
	
	$("#btnAvancarNews").click(function(){
		$(".firstNews").css("display","none");
		$(".secondNews").css("display","block");
	});
	identifySegment();
	menuDropDown();
	changeMenuSegment();
});
