// QUANDO O DOCUMENTO ESTIVER PRONTO
$(document).ready(function(){	
						   
   // CONFIGURA A VALIDACAO DO FORMULARIO
   $("#formLogin").validate({
	  rules: {
		 login: {required: true},
		 senha: {required: true}
	  },
	  messages: {
		 login: {required: "Informe o Login"},
		 senha: {required: "Informe a Senha"}
	  }
	  ,submitHandler:function(form) {
		 form.submit();
	  }
   });
   
   // CONFIGURA A VALIDACAO DO FORMULARIO
   $("#formMenu").validate({
	  rules: {
		 ordem: {required: true},
		 titulo: {required: true}
	  },
	  messages: {
		 ordem: {required: "Informe a Ordem"},
		 titulo: {required: "Informe o Titulo"}
	  }
	  ,submitHandler:function(form) {
		 type = $("#type").val();
		 form.submit();
		 alert("Cadastro efetuado com Sucesso");
		 if(type == "cadMenu") {
		 	document.location.href = "editar.php?choice=1";
		 } else if(type == "cadLnkArt") {
		 	document.location.href = "editar.php?choice=5";
		 } else if(type == "altLnkArt") {
		 	document.location.href = "editar.php?id=3";
		 } else {
			id = $("#id").val();
			document.location.href = "editar.php?id=" + id; 
		 }
	  }
   });   

   // CONFIGURA A VALIDACAO DO FORMULARIO
   $("#formCont").validate({
	  rules: {
		 nome: {required: true},
		 sobrenome: {required: true},
		 cpf: {cpf: true},
		 ddd_cel: {required: true, minlength: 2},
		 celular: {required: true, minlength: 8}
	  },
	  /*messages: {
		 nome: {required: "Informe o Nome"},
		 sobrenome: {required: "Informe o Sobrenome"},
		 cpf: { cnpj: "CPF inválido"},
		 ddd_cel: {required: "Informe o DDD", minlength: "Informe o DDD Completo"},
		 celular: {required: "Informe o Celular", minlength: "Informe o Celular Completo"}
	  }*/
	  showErrors: function(errorMap, errorList) {
		  var erros = this.numberOfInvalids();
		  if (erros > 0) {
		  	$("#msgErro").css({display: "block"});
		  }
	  },
	  submitHandler:function(form) {
		 type = $("#type").val();		  
		 form.submit();
		 alert("Cadastro efetuado com Sucesso");
		 /*if(type == "cadCont") {
		 	document.location.href = "editar.php?choice=2&novo=s";
		 } else if(type == "altCont") {
		 	id = $("#id").val();			 
			document.location.href = "editar.php?choice=2&id_contato=" + id; 
		 } else {
			document.location.href = "index.php"; 
		 }*/
	  }
   });
   
   // CONFIGURA A VALIDACAO DO FORMULARIO
   $("#formMsg").validate({
	  rules: {
		 remetente: {required: true}
	  },
	  messages: {
		 remetente: {required: "Informe o Remetente"}
	  }
	  ,submitHandler:function(form) {
		 form.submit();
		 alert("Cadastro efetuado com Sucesso");
		 document.location.href = "editar.php?choice=3"; 
	  }
   }); 
   
   // CONFIGURA A VALIDACAO DO FORMULARIO
   $("#formArt").validate({
	  rules: {
		 titulo: {required: true},
		 lnk: {required: true}
	  },
	  messages: {
		 titulo: {required: "Informe o Titulo"},
		 lnk: {required: "Informe o Link"}
	  }
	  ,submitHandler:function(form) {
		 form.submit();
		 alert("Cadastro efetuado com Sucesso");
		 document.location.href = "editar.php?choice=5"; 
	  }
   }); 
   
   // CONFIGURA A VALIDACAO DO FORMULARIO
   $("#formContato").validate({
	  rules: {
		 nome: {required: true},
		 sobrenome: {required: true},
		 email: {required: true, email: true},
		 assunto: {required: true},
		 mensagem: {required: true}
	  },
	  messages: {
		 nome: {required: "Informe o Nome"},
		 sobrenome: {required: "Informe o Sobrenome"},
		 email: {required: "Informe o E-mail", email: "Por favor informe um e-mail valido"},
		 assunto: {required: "Infome o Assunto"},
		 mensagem: {required: "Informe a Mensagem"}
	  }
	  ,submitHandler:function(form) {
		 form.submit();	 
	  }
   });    
   
   // Limpar erros
   $("#limpar").click(function(){
		$("label.error").css({display: "none"});
		$("#MsgCount").text("");
	});  
   
   // Iniciar contagem de palavras
   maxLen();
   maxLen2();    

});	

/* Função para enviar SMS */

function send(nome, ddd_cel, celular, authorization) {

	if(confirm("Confirma envio de SMS para " + nome + "?"))
	{
		$.post("enviarSMS.php",{nome: nome, ddd_cel: ddd_cel, celular: celular, authorization: authorization},
			function(data){
				alert(data);
			}					   
		);		
	}
	
}

/* Função para enviar vários SMS  */

function sendMult() {
	
	/* Pegando dados enviados da pagina */	

	if(confirm("Confirma envio de SMS para todos os contatos?"))
	{
		var mult = "S";

		$.post("enviarSMS.php",{mult: mult},
			function(data){
				alert(data);
			}					   
		);
	}
	
}

/* Função para enviar email */

function email(nome, email) {

	if(confirm("Confirma envio de email para " + nome + "?"))
	{
		$.post("enviarEmail.php",{nome: nome, email: email},
			function(data){
				alert(data);
			}					   
		);		
	}

}

function emailMult() {
	
	/* Pegando dados enviados da pagina */	

	if(confirm("Confirma envio de Email para todos os contatos?"))
	{
		var mult = "S";

		$.post("enviarEmail.php",{mult: mult},
			function(data){
				alert(data);
			}					   
		);
	}
	
}

/* Função para deletar dados */

function excluir(type,id, nome) {

	if(confirm("Confirma exclusao desse contato: " + nome + "?"))
	{
		$.post("excluir.php",{type: type, id: id},
			function(){
				if(type == "menu") {
					document.location.href = "editar.php";
				} else if(type == "artigo") {
					document.location.href = "editar.php?id=3";
				} else {
					document.location.href = "editar.php?choice=2";
				}
			}					   
		);		
	}
	
}

/* Função para criar Restringir digitar mais dados doque o campo permiti */

function maxLen() {
	$("#message").keyup(function(event) {
		var msg = $(this).val();
		var padrao = msg.substr(0, 128);
		var maxText = 128;
		var numChar = msg.length;
		$("#MsgCount").css("color","#999999"); 
		$("#MsgCount").text("Digitados ate agora: " + numChar);

		if(numChar == maxText) {
			
			padrao = msg;
			alert("Limite de caracteres excedido!\n So e permitido no maximo: " + maxText + " Caracteres");
			$("#MsgCount").text("Digitados ate agora: " + padrao.length);
			
		} else if(numChar > maxText) {	
		
			alert("Limite de caracteres excedido!\n So e permitido no maximo: " + maxText + " Caracteres");
			$("#MsgCount").text("Digitados ate agora: " + padrao.length);			
			$("#message").val(padrao);
			
		}
		
	})
	
}

/* Função para criar Restringir digitar mais dados doque o campo permiti */

function maxLen2() {
	$("#mensagem").keyup(function(event) {
		var msg = $(this).val();
		var padrao = msg.substr(0,500);
		var maxText = 500;
		var numChar = msg.length;
		$("#MsgCount").css("color","#999999"); 
		$("#MsgCount").text("Digitados ate agora: " + numChar);
		
		if(numChar == maxText) {
			
			alert("Limite de caracteres excedido!\n So e permitido no maximo: " + numChar + " Caracteres");
			$("#MsgCount").text("Digitados ate agora: " + padrao.length);		
			
		} else if(numChar > maxText) {	
		
			alert("Limite de caracteres excedido!\n So e permitido no maximo: " + numChar + " Caracteres");
			$("#MsgCount").text("Digitados ate agora: " + padrao.length);			
			$("#mensagem").val(padrao);
			
		}
		
	})
	
}

function somente_numero(campo)
{
	var digits="0123456789";
	var campo_temp;
	
	for (var i=0;i<campo.value.length;i++)
	{
		campo_temp=campo.value.substring(i,i+1)    
		if (digits.indexOf(campo_temp)==-1)
		{
			campo.value = campo.value.substring(0,i);
			alert("Digite apenas numeros!");
			break;
		}
	}
	
}

function SomenteNumero(e){
    var tecla=(window.event)?event.keyCode:e.which;
    if((tecla > 47 && tecla < 58)) return true;
    else{
    if (tecla != 8) return false;
    else return true;
    }
}

