// JavaScript Document
jQuery(function($){
   $("#cep").mask("99999-999",{placeholder:" "});
   $("#telefone").mask("(99) 9999-9999",{placeholder:" "});
});

function validaMail(email) {
	
	invalidCharsList = " /:,;~#";

	if ( email.indexOf('@',0)==-1 ||email.indexOf('@',0)== 0 || email.indexOf('.',0)==-1) {
		return (false);
	}

	for (i = 0; i < invalidCharsList.length; i++) {
		errorChar = invalidCharsList.charAt(i);
		if (email.indexOf(errorChar,0) != -1) {		
			return (false);
		}
	}   
}
function selecionaMaterialEco(val){
	if(val == 2){
		$('.cor-eco').show();
		$('.select-cor').show();
	} else {
		$('.select-cor').slideUp('',function(){
			$('.cor-eco').hide();
			$('#cor_eco').val(0);
			
			$('img.atual').each(function(){
				$(this).removeClass('atual');
			});
			$('.valor-cor').html('Selecione');
		});
	}
}
function selecionaModeloNovo(val){
	
	switch(val){
		case 'outro':
			$('.cor').hide();
			$('#material').val(0);
			$('.material').hide();
			$('.material_eco').hide();
			$('.material_baby').hide();
			$('.material_polo').hide();
		break;
		
		case '1': //Camisetas
		case '6': //Camisetas 3D
			$('.material').show();
			$('.cor').show();
			$('.material_eco').hide();
			$('.material_baby').hide();
			$('.material_polo').hide();
		break;
		
		case '2': //Polo
			$('.material_polo').show();
			$('.cor').show();
			$('.material').hide();
			$('.material_eco').hide();
			$('.material_baby').hide();
		break;
			
		case '3': //Baby look
			$('.material_baby').show();
			$('.cor').show();
			$('.material').hide();
			$('.material_eco').hide();
			$('.material_polo').hide();
		break;
		
		case '4': //Ecobags
			$('.material_eco').show();
			$('#cor').val(0);
			$('.cor').hide();
			$('.material').hide();
			$('.material_baby').hide();
			$('.material_polo').hide();
		break;

		case '12': //Moda corporativa
			$('.material_eco').hide();
			$('#cor').val(0);
			$('.cor').hide();
			$('.material').hide();
			$('.material_baby').hide();
			$('.material_polo').hide();
		break;

		default:
			$('.cor').hide();
			$('.material').hide();
			$('.material_eco').hide();
			$('.material_baby').hide();
			$('.material_polo').hide();
		break;
	}
}

$(document).ready(function(){
	
	/* abre modelos ecobag */
	$('#select-modelo, .valor-modelo').live('click',function(){
		$('.modelos').slideToggle();
	});
	
	/* select modelo */
	$('.modelos > div img').click(function(){
		
		$('img.atual-m').each(function(){
			$(this).removeClass('atual-m');
		});
		
		$(this).addClass('atual-m');
		$('#modelo_eco').val($(this).attr('longdesc'));
		$('.valor-modelo').html($(this).attr('longdesc'));
		$('.modelos').slideToggle();

	});
	
	/* abre cores ecobag - se material = lona leve */
	$('#select-cor, .valor-cor').live('click',function(){
		$('.cores').slideToggle();
	});
	
	/* select cor */
	$('.cores > div img').click(function(){
		
		$('img.atual').each(function(){
			$(this).removeClass('atual');
		});
		
		$(this).addClass('atual');
		$('#cor_eco').val($(this).attr('longdesc'));
		$('.valor-cor').html($(this).attr('longdesc'));
		$('.cores').slideToggle();
		
	});
	
	$('#form-orcamentos-novo').submit(function(){
		
		var code			= $("#code").val();

		var quantidade 		= $("#quantidade").val();
		var largura			= $("#largura").val();
		var altura			= $("#altura").val();
		var material		= $("#material").val();
		var material_polo	= $("#material_polo").val();
		var material_baby	= $("#material_baby").val();
		var material_eco	= $("#material_eco").val();
		var cor				= $("#cor").val();

		var pessoa_contato 	= $("#pessoa_contato").val();
		var endereco 		= $("#endereco").val();
		var numero			= $("#numero").val();
		var cidade 			= $("#cidade").val();
		var uf 				= $("#estado").val();
		var cep 			= $("#cep").val();
		var telefone 		= $("#telefone").val();
		var email  			= $("#email").val();

		var msg 			= "";
		var erro			= false;
		
		if(code != 12 && code != 'outro'){
			if(quantidade == "" || quantidade <= 99) {
				msg += ' - A quantidade deve ser superior a 100 peças <br \/>';
				erro = true;
			}
		}
		
		if(code == 4){
			if(largura == '' || altura == ''){
				msg += ' - Preencha as dimensões da ecobag corretamente <br \/>';
				erro = true;
			}

			if($('.modelos img.atual-m').size() == 0){
				msg += ' - Selecione o modelo da ecobag <br \/>';
				erro = true;
			}
		}

		if(code != 12 && code != "outro"){
			switch(code){
				
				case '1':
					if(material == 0){
						msg += ' - Selecione o material <br \/>';
						erro = true;
					}
				break;
				case '2':
					if(material_polo == 0){
						msg += ' - Selecione o material <br \/>';
						erro = true;
					}
				break;
				case '3':
					if(material_baby == 0){
						msg += ' - Selecione o material <br \/>';
						erro = true;
					}
				break;
				case '4':
					if(material_eco == 0){
						msg += ' - Selecione o material <br \/>';
						erro = true;
					}
				break;
				
			}
		}
		
		if(code == 4 && material_eco == 2){
			if($('.cores img.atual').size() == 0){
				msg += ' - Selecione a cor da ecobag <br \/>';
				erro = true;
			}
		}
		
		if(code == 1 || code == 2 || code == 3 || code == 6){
			if(cor == 0){
				msg += ' - Selecione a cor <br \/>';
				erro = true;
			}
		}
		
		if(pessoa_contato == ""){
			msg += ' - Preencha o nome da pessoa para contato <br \/>';
			erro = true;
		}

		if(endereco == ""){
			msg += ' - Preencha o endereço <br \/>';
			erro = true;
		}

		if(numero == ""){
			msg += ' - Preencha o número correspondente ao endereço <br \/>';
			erro = true;
		}

		if(cidade == ""){
			msg += ' - Preencha a cidade <br \/>';
			erro = true;
		}
	
		if(uf == ""){
			msg += ' - Preencha o estado <br \/>';
			erro = true;
		}
		
		if(cep == ""){
			msg += ' - Preencha o CEP <br \/>';
			erro = true;
		}
		
		if(telefone == 0){
			msg += ' - Preencha o telefone <br \/>';
			erro = true;
		}

		if(validaMail(email) == false){
			msg += ' - Preencha o e-mail corretamente <br \/>';
			erro = true;
		}
	
		if(erro == true){
			$('.retorno-orcamento').html(msg);
			return false;
		} else {
			$('.retorno-orcamento').html('');
			return true;	
		}
		
	});
});
