//exibir um alerta
function alertar(string) {
  alert(string);
}

//contar e limitar o número de caracteres de um textarea
function Contador(obj, counter) { 
  obj = document.getElementById(obj);
    if (obj.value.length > 200)  {
    obj.value = obj.value.substring(0, 200); 
    }
  document.getElementById(counter).value = obj.value.length + '/200';
}




/*abrir nova janela*/
function OpenWindowA(url) {
  window.open(url,"_blank");
}

/*abrir nova janela com parâmetros*/
function OpenWindowB(url) {
  window.open(url,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=530, height=500");
}

/*retira os espaços do início e fim de uma string*/
function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}

/*verifica se é um e-mail válido*/
function ValidaEmail(email) {
  var padrao = '^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([a-z,A-Z]){2,3}([0-9,a-z,A-Z])?$';
  var reg = new RegExp(padrao);
  var result = reg.exec(email);
	if(result != null)
	  return true;
	else
	  return false;
}

/*validação dos campos do formulário*/
function ValidaForm(form) {
  var erros = Array();
  var foco = null;
  var nomeform = form;

    if (nomeform.texto) {
	  nomeform.texto.value = trim(nomeform.texto.value);
	    if(nomeform.texto.value == "") {
		  erros.push('Field required.');
		if (foco == null)
		  foco = nomeform.texto;
		}
	}

    if (nomeform.nome) {
	  nomeform.nome.value = trim(nomeform.nome.value);
	    if(nomeform.nome.value == "") {
		  erros.push('Name required.');
		if (foco == null)
		  foco = nomeform.nome;
		}
	}

	if (nomeform.email) {
	  nomeform.email.value = trim(nomeform.email.value);
	    if (!ValidaEmail(nomeform.email.value)) {
		  erros.push('Wrong E-Mail.');
		if (foco == null)
		  foco = nomeform.email;
		}	
	}
	

	if (nomeform.telefone) {
	  nomeform.telefone.value = trim(nomeform.telefone.value);
	    if (nomeform.telefone.value == "") {
		  erros.push('Phone Number required.');
		if (foco == null)
		  foco = nomeform.telefone;
		}
	}

	if (nomeform.cidade) {
	  nomeform.cidade.value = trim(nomeform.cidade.value);
	    if (nomeform.cidade.value == "") {
		  erros.push('City required.');
		if (foco == null)
		  foco = nomeform.cidade;
		}
	}

	if (nomeform.estado) {
	  nomeform.estado.value = trim(nomeform.estado.value);
	    if (nomeform.estado.value == "") {
		  erros.push('State required.');
		if (foco == null)
		  foco = nomeform.estado;
		}
	}

	if (nomeform.mensagem) {
	  nomeform.mensagem.value = trim(nomeform.mensagem.value);
	    if (nomeform.mensagem.value == "") {
		  erros.push('Message required.');
		if (foco == null)
		  foco = nomeform.mensagem;
		}
	}

	if (nomeform.descricao) {
	  nomeform.descricao.value = trim(nomeform.descricao.value);
	    if (nomeform.descricao.value == "") {
		  erros.push('O campo descrição é obrigatório.');
		if (foco == null)
		  foco = nomeform.descricao;
		}
	}

	numErros = erros.length;	
	if (numErros > 0) {
	  msg = 'Warning:';
	  for (i = 0; i < numErros; i++) {
	    msg += '\n » ' + erros[i];
	  }
	  alert(msg);
	  foco.focus();
	  return false;
	} else {
	  nomeform.submit();
	  return true;
	}
}

/*AJAX*/
var xmlhttp
var id
function carregar(arquivo,identificador)
{
id = identificador;

//exibe a mensagem carregando
var loading=document.getElementById(identificador)
    loading.innerHTML='<p class="carregando">Loading...</p>'

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  xmlhttp.onreadystatechange=xmlhttpChange
  xmlhttp.open("GET",arquivo,true)
  xmlhttp.send(null)
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open("GET",arquivo,true)
    xmlhttp.send()
    }
  }
}

function xmlhttpChange()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
  {
  var resultado;
  resultado = xmlhttp.responseText;
  resultado = resultado.replace(/\+/g,' '); //substitui o + por um espaço
  resultado = unescape(resultado); //desfaz a função urlencode */
  document.getElementById(id).innerHTML=resultado;
  }
  else
  {
  alert("Problem retrieving data:" + xmlhttp.statusText)
  }
  }
}
