///////////////////// LIBERA CAMPO DE PESQUISA
/**
 *
 * @access public
 * @return void
 **/
function liberaPesquisa(obj){
	if(obj.value=="Pesquisar..."){
		obj.value = "";
		obj.focus();
		obj.style.color = "#000";
	}else if(obj.value==""){
		obj.value = "Pesquisar...";
		obj.focus();
		obj.style.color = "#C9C9C9";
	}


}


///////////////////// ABRE ANUNCIO
function abre_anuncio(id,target){
	if(document.getElementById(target).innerHTML==""){
		ajxAnuncio = new Object(ajaxutil);
		ajxAnuncio.method = "GET";
		ajxAnuncio.target = document.getElementById(target)
		ajxAnuncio.msg = "<img src='../img/loading.gif' align='center' width='100px'>";
		ajxAnuncio.request("ajx/anuncio_ajx.php?anuncio_id="+id);
	}else{
		document.getElementById(target).innerHTML="";
	}
}

///////////////////// ABRE TELA LIGHT WINDOW
function abreTela(url,method,classe,width,oklabel){

	Dialog.alert({url: url, options: {method: 'get'}},
	{className: classe, width:width, okLabel: oklabel})
}

///////////////////// CADASTRO NEWSLETTER
function cadNewsletter(){

	email = document.getElementById("txtNewsleter").value;
	var femail = document.getElementById("txtNewsleter");

	if (email == "") {
		alert("Informe seu e-mail.");
		femail.focus();
		femail.select();
		return false;
	}else{
		prim = email.indexOf("@")
		if(prim < 2) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("@",prim + 1) != -1) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf(".") < 1) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf(" ") != -1) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("zipmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("hotmeil.com") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf(".@") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("@.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf(".com.br.") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("/") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("[") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("]") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("(") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf(")") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
		if(email.indexOf("..") > 0) {
			alert("O e-mail informado parece não estar correto.");
			femail.focus();
			femail.select();
			return false;
		}
	}

	ajxNews = new Object(ajaxutil);
	ajxNews.method = "GET";
	ajxNews.target = document.getElementById("spNews")
	ajxNews.msg = "<img src='img/loading.gif' align='center'>";
	ajxNews.request("ajx/newsletter_ajx.php?email="+email);
}

String.prototype.replaceAll = function(de, para){
    var str = this;
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}


function VerDig(evento,tipo,obj)
{
  if (evento.keyCode) tecla = evento.keyCode;
  else if (evento.which) tecla = evento.which; // Netscape 4.?
  else if (evento.charCode) tecla = evento.charCode; // Mozilla


	switch (tipo)
	{
		case 1: // tipo 1 = Números
				 if (tecla < 48 || tecla > 57)
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				 break;

		case 2:  // tipo 2 = Letras
				 if (tecla > 48 && tecla < 57)
				 	if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				 break;
		case 3:	// tipo 3 = currency

				 if ((tecla < 48 || tecla > 57) && (tecla != 44)){
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
					}
				break;
		case 4:	//tipo 4 = date
				if (tecla < 48 || tecla > 57)
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				else
				{
					if(obj.value.length == 2 || obj.value.length == 5) obj.value += "/";
				}
				break;
		case 5:	//tipo 5 = hora

				if (tecla < 48 || tecla > 57)
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				else
				{
					if(obj.value.length == 2) obj.value += ":";
				}
				break;
		case 6:	// tipo 6 = currency com ponto
				 if ((tecla < 48 || tecla > 57) && (tecla != 46))
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				 break;
		case 7:	//tipo 7 = telefone

				if (tecla < 48 || tecla > 57)
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				else
				{
					if(obj.value.length == 2){
						obj.value = "("+obj.value;
						obj.value += ") ";
					}
					if(obj.value.length == 9) obj.value += "-";
				}
				break;
			case 8:	//tipo 8 = cep

				if (tecla < 48 || tecla > 57)
					if (evento.keyCode){
						evento.returnValue = false;
						evento.cancel = true;
					}else{
						evento.preventDefault();
					}
				else
				{
					if(obj.value.length == 5) obj.value += "-";
				}
				break;

	}


}

/* Valida EMAIL */
function validaEmail(email){

		prim = email.indexOf("@")
		if(prim < 2) {
			return false;
		}
		if(email.indexOf("@",prim + 1) != -1) {
			return false;
		}
		if(email.indexOf(".") < 1) {
			return false;
		}
		if(email.indexOf(" ") != -1) {
			return false;
		}
		if(email.indexOf("zipmeil.com") > 0) {
			return false;
		}
		if(email.indexOf("hotmeil.com") > 0) {
			return false;
		}
		if(email.indexOf(".@") > 0) {
			return false;
		}
		if(email.indexOf("@.") > 0) {
			return false;
		}
		if(email.indexOf(".com.br.") > 0) {
			return false;
		}
		if(email.indexOf("/") > 0) {
			return false;
		}
		if(email.indexOf("[") > 0) {
			return false;
		}
		if(email.indexOf("]") > 0) {
			return false;
		}
		if(email.indexOf("(") > 0) {
			return false;
		}
		if(email.indexOf(")") > 0) {
			return false;
		}
		if(email.indexOf("..") > 0) {
			return false;
		}
}

/* Valida CNPJ */

function validaCNPJCPF(TIPnumber) {

	valor = TIPnumber;
    erro = new String;
	if (valor.length == 14) {
	  CNPJ = valor;
	  var nonNumbers = /\D/;
	  if (nonNumbers.test(CNPJ)) erro = "A verificacao de CNPJ suporta apenas numeros! ";
      var a = [];
      var b = new Number;
      var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
      var msg = "" ;
      for (i=0; i<12; i++)
		  {
          a[i] = CNPJ.charAt(i);
          b += a[i] * c[i+1];
		  }

      if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
      b = 0;
      for (y=0; y<13; y++)
	    {
        b += (a[y] * c[y]);
        }

      if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }

      if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
                       erro +="Dígito verificador do CNPJ com problema!";
      }

      if (erro.length > 0){
       	alert(erro);
        return false;
      }
      return true;
}
else
{
    cpf = valor;
    if (cpf.length < 11)
    {
    	erro = "Sao necessarios 11 digitos para verificacao do CPF! ";
    	msg = "CPF";
    }
	if (cpf.length > 11)
	 {
	 	erro = "Sao necessarios 14 digitos para verificacao do CNPJ! ";
	 	msg = "CNPJ";
	 }
    var nonNumbers = /\D/;
    if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! ";
    if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
         erro += "Numero de" + msg + "invalido!"
	}
    var a = [];
    var b = new Number;
    var c = 11;
    for (i=0; i<11; i++){
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
    b = 0;
    c = 11;
    for (y=0; y<10; y++) b += (a[y] * c--);
    if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
    if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
           erro +="Digito verificador do CPF com problema!";
    }
    if (erro.length > 0){
        alert(erro);
    return false;
    }
    return true;
  }
}

