// Retirar os espaços de um texto
function retirarEspaco (s) {
	while (s.indexOf(' ') > 0) s = s.replace(' ', '');
	return s;
} 

// Verificar se campo foi informado
function validarCampoTexto(objCampo, strNomeCampo) {
	if (retirarEspaco(objCampo.value) == ''){
		alert('Favor informar campo ' + strNomeCampo + '.');
		objCampo.focus();
		return false;
	}
	return true;
}

// Verificar a data informada é válida
function validarData(objdt) {

	if (retirarEspaco(objdt.value) != '') 
	{
		var arr = objdt.value.split('/');
		if (arr.length != 3) 
		{
			alert('Data inválida.' + String.fromCharCode(13, 10) + 'Deve estar no formato dd/mm/aaaa')
			objdt.focus();
			return false;
		}
		var dtaux = arr[2] + '/' + arr[1] + '/' + arr[0];
		if (isNaN(Date.parse(dtaux))) {
			alert('Data inválida.' + String.fromCharCode(13, 10) + 'Deve estar no formato dd/mm/aaaa')
			objdt.focus();
			return false;
		}
	
		var dt = new Date(dtaux);
		if ((parseFloat(dt.getDate()) != parseFloat(arr[0])) || (parseFloat(dt.getMonth()+1) != parseFloat(arr[1])) || (parseFloat(dt.getFullYear()) != parseFloat(arr[2]))) {
			alert('Data inválida.' + String.fromCharCode(13, 10) + 'Deve estar no formato dd/mm/aaaa')
			objdt.focus();
			return false;
		}
	}
	
	return true;
}

// Verificar se a hora informada é válida
function validarHora(objhr) {

	if (retirarEspaco(objhr.value) != '') 
	{
		var arr = objhr.value.split(':');
		if (arr.length != 2) 
		{
			alert('Hora inválida.' + String.fromCharCode(13, 10) + 'Deve estar no formato hh:mi')
			objhr.focus();
			return false;
		}
		if (parseInt(arr[0]) > 23 || parseInt(arr[1]) > 59) {
			alert('Hora inválida.' + String.fromCharCode(13, 10) + 'Deve estar no formato hh:mi')
			objhr.focus();
			return false;
		}
	}
	
	return true;
}

// Verificar se o CEP informado é válido.
function validarCep(objcep)
{
	var cep = retirarEspaco(objcep.value);

	if (cep == '') 
		return true;
		
	if (cep.length < 8) {
		erro=true;
		alert('CEP inválido.' + String.fromCharCode(13, 10) + 'Deve estar no formato 99999-999')
		objcep.focus()
		erro=false;
		return false
	}
	
	if (cep.length == 9) {
		if (cep.search(/\d{5}-\d{3}/) != 0) {
			erro=true;
			alert('CEP inválido.' + String.fromCharCode(13, 10) + 'Deve estar no formato 99999-999')
			objcep.focus()
			erro=false;
			return false
		}	
	}
	
	if (cep.length == 8) {
		if (cep.search(/\d{8}/) != 0) {
			erro=true;
			alert('CEP inválido.' + String.fromCharCode(13, 10) + 'Deve estar no formato 99999-999')
			objcep.focus()
			erro=false;
			return false
		}	
	}
	return true
}

// Verificar se o telefone informado está no formato correto
function validarTelefone(objfone)
{
	var fone = retirarEspaco(objfone.value);

	if (fone == '') 
		return true;
		
	if (fone.length < 8) {
		erro=true;
		alert('Telefone inválido.' + String.fromCharCode(13, 10) + 'Deve estar no formato 9999-9999')
		objfone.focus()
		erro=false;
		return false
	}
	
	if (cep.length == 9) {
		if (cep.search(/\d{5}-\d{3}/) != 0) {
			erro=true;
			alert('CEP inválido.' + String.fromCharCode(13, 10) + 'Deve estar no formato 99999-999')
			objcep.focus()
			erro=false;
			return false
		}	
	}
	
	if (cep.length == 8) {
		if (cep.search(/\d{8}/) != 0) {
			erro=true;
			alert('CEP inválido.' + String.fromCharCode(13, 10) + 'Deve estar no formato 99999-999')
			objcep.focus()
			erro=false;
			return false
		}	
	}
	return true
}

// Validar email
function validarEmail (f,fld) {
  var frm = f ;
  var e = f.elements[fld]
  if (e.value == null){
    alert("Favor informar campo de Email.");
    e.focus();
    return (false);}
  //tirando os espaços vazios no endereço
  for (x = 1; x < e.value.length; x ++) 
     { e.value= e.value.replace(' ', '')}
	
  var emailStr
  emailStr = e.value;
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
  var matchArray = emailStr.match(emailPat)
  if (matchArray==null){
    alert('O email informado é inválido.');
    e.value="";
    e.focus();
    return (false);}
	
  var user=matchArray[1]
  var domain=matchArray[2]
  if (user.match(userPat)==null){
    alert("O email informado é inválido.");
    e.value="";
    e.focus();
    return (false);}
	
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null){
  for (var i=1;i<=4;i++){
  if (IPArray[i]>255){
    alert('O email informado é inválido.');
    e.value="";
    e.focus();
    return (false);}
  }
  return true;}

  var domainArray=domain.match(domainPat)
  if (domainArray==null){
    alert('O email informado é inválido.');
    e.value="";
    e.focus();
    return (false);}
	
  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>3){
    alert('O email informado é inválido.');
    e.value="";
    e.focus();
    return (false);}
	
  if (domArr.length<2){
    var errStr="O email informado é inválido."
    alert(errStr);
    e.value="";
    e.focus();
    return (false);}
	
return true;
}
// Permitir digitar somente numeros
function permitirApenasNumeros(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
			{
				if (tecla != 8) // backspace
					event.keyCode = 0;
					//return false;
				else
					return true;
			}
}

// Funcao para alterar a imagem de um objeto img
function changeImg(e,obj){
	if(e=='over')
		obj.src = "Images/Menu/" + obj.id + "_on.gif";
	else
		obj.src = "Images/Menu/" + obj.id + ".gif";
}

// Funcao para definir a pagina a ser exibida em um IFRAME - Fundo Mkt
function conteudo_fundomkt(link){
	document.ifr_fundomkt.location.href = link;
}
//Funcao para definir a pagina a ser exibida em um IFRAME 
function altera_iframe(link){
	document.ifr_conteudo.location.href = link;
}

// Funcao para redirecionar a pagina de acordo com a mudanca de um ComboBox
function altera_combo(link){
	window.location.href= link;
}

// Exibir popup
function exibirPopup(pLink, pTarget, pLarguraPx, pAlturaPx){
	janela = window.open(pLink,pTarget,'tollbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,top=0,left=0,width=' + pLarguraPx + ',height=' + pAlturaPx);		
//	janela.resizeTo(pLarguraPx,pAlturaPx);
	janela.focus();
}

// Desativar a tecla enter para não dar submit
function desativarEnter(){
	if (event.keyCode == 13){
		event.keyCode = 0;
	}
}


