function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function webServiceCep(id, metodo, url, retorno) {
	
	var divInfo = document.getElementById(id);
	var divInfo2 = document.getElementById(retorno);
	divInfo.style.visibility = "visible";
	divInfo.style.position = "relative";
	divInfo.innerHTML = "Processando..."; 
	
	
	 $(function(){  

			       $.ajax({
					       
				       type: metodo,
				       url: url,
					 success: function(msg){


						
				       		resposta=msg.split('|');
						
							resultado =  trim(resposta[0],' ');
							estado = trim(resposta[1],' ');
							cidade = trim(resposta[2],' ');
							bairro = trim(resposta[3],' ');
							tipo = trim(resposta[4],' ');
							endereco = trim(resposta[5],' ');
							resultadoTxt = trim(resposta[6],' ');
							
						if (resultado==1) {
						
						divInfo.innerHTML = ""; 
						 document.getElementById('cidade').value = cidade;
						 document.getElementById('estado').value = estado;
						 document.getElementById('rua').value = tipo+' '+endereco;
						 document.getElementById('bairro').value  = bairro;	
						 document.getElementById('numero').focus(); //Passa o focu para o campo de pesquisa.
						
						}else{
						divInfo.innerHTML = resultadoTxt; 
						}
						/*
						minhasopcoes = meuform.estado.options;
						for (var i = 0; i<minhasopcoes.length;i++){
							if (meuform.estado.options[i].value == estado){
								meuform.estado.selectedIndex = i;
								
							}
						}
						*/
			
							
				       }
			       
			       });
		       });
}

	function campoData(campo,time){
		

		$(function() {  
		    $(campo).datepicker({
		    	showButtonPanel: false,
				duration: '',
				dateFormat:'dd/mm/yy',  
				showTime: time,  
				constrainInput: false,  
				stepMinutes: 1,  
				stepHours: 1,  
				altTimeField: '',
				changeMonth: false,
				changeYear: false,
				dayNames: ['Domingo', 'Segunda', 'Terça', 'Quata', 'Quita', 'Sexta', 'S&aacute;bado'],
				dayNamesMin: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'S&aacute;b'],
				dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'S&aacute;b'],
				monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
				monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'],
		        time24h: true  
		     });
		    
		});
		
	}
function submeterLoginMain(){
    $("#formLoginMain").ajaxSubmit({
	
        target: '',
        url: '../mister/cliente/?acao=login', 
        type: 'post', 
        success: function(resposta) {
   			if(resposta!=1){
   				alert(resposta);
				location.href="login.php";
   			}else{
   				location.href="index.php";
   			}

        }
    		
    });
	
}
function submeterLogin(){
    $("#formLogin").ajaxSubmit({
	
        target: '',
        url: '../mister/cliente/?acao=login', 
        type: 'post', 
        success: function(resposta) {
   			if(resposta!=1){
   				alert(resposta);
   			}else{
   				location.href="carrinho.php";
   			}

        }
    		
    });
	
}


function submitCarrinho(){
    $("#carrinhoForm").ajaxSubmit({
	
        target: '',
        url: 'carrinho_finalizar_email.php', 
        type: 'post', 
        success: function(resposta) {
   			if(resposta!=1){
				alert(resposta);
   				location.href="carrinho.php";
   			}else{
   				alert(resposta);
				location.href="carrinho.php";
   			}

        }
    		
    });
	
}

function submitCarrinho2(){
    $("#carrinhoForm").ajaxSubmit({
	
        target: '',
        url: 'carrinho_finalizar_email2.php', 
        type: 'post', 
        success: function(resposta) {
   			if(resposta!=1){
				alert(resposta);
   				location.href="carrinho2.php";
   			}else{
   				alert(resposta);
				location.href="carrinho2.php";
   			}

        }
    		
    });
	
}


function submeterLembrar(){
    $("#formLembrar").ajaxSubmit({
	
        target: '',
        url: '../mister/cliente/?acao=lembar', 
        type: 'post', 
        success: function(resposta) {
   			if(resposta!=1){
   				alert(resposta);
				document.getElementById('formLembrar').reset();
   			}else{
   				alert(resposta);
				document.getElementById('formLembrar').reset();
   			}

        }
    		
    });
	
}

function submeterNews(){
	

			$("#newsletter_form").ajaxSubmit({
   			        target: '',
   			        url: '../mister/newsletter/?acao=set2', 
   			        type: 'post', 
   			        success: function(resposta) {
						//alert(resposta);
   			   			if(resposta==1){
   			   				alert('Cadastro efetuado com sucesso');
							$("#divNews").slideUp();
   			   			}else{
   			   				alert('Email já cadastrado, devido isto, apenas re-ativamos o recebimento');
							$("#divNews").slideUp();
   			   			}

   			        }
   			    		
   			    });
    
 
}

function submeterCliente(){
	

			$("#Cliente_form").ajaxSubmit({
   			        target: '',
   			        url: '../mister/cliente/?acao=set2', 
   			        type: 'post', 
   			        success: function(resposta) {
						//alert(resposta);
   			   			if(resposta==1){
   			   				alert('Cadastro efetuado com sucesso');
   			   				location.href="carrinho.php";
   			   			}else{
   			   				alert("Alguns erros foram encontrados no formulário de contato:"+resposta);
   			   			}

   			        }
   			    		
   			    });
    
 
}

function submeterClienteAlterar(){
	

    $("#Cliente_form").ajaxSubmit({
 	   		
	        target: '',
	        url: '../mister/cliente/?acao=update2', 
	        type: 'post', 
	        success: function(resposta) {
	   			if(resposta==1){
	   				alert('Seus dados foram alterados com sucesso');
	   				location.href="index.php";
				}else{
					if(resposta==0){
						alert('Nenhum dado foi alterado');
					}else{
						alert('Erro ao enviar a mensagem, verifique se todos os campos est&atilde;o preenchidos corretamente');
					}
				}

	        }
	    		
    });


}
					

	$(function() {
		
		$(".abre_busca").toggle(
			function() {
				$("#busca_avancada").slideDown();
			},
			function() {
				$("#busca_avancada").slideUp();
			}
		);
		
	});
	
function limpa(x){
	x.value = '';
	x.style.color = '#000000';
}



