///////////////////// 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="";
	}
}

/////////////////////ENQUETE
function votar(){
	var opcao = document.getElementById("opcao").value;
	if (opcao==""){
		alert("Selecione uma opção!");
		return;
	}
	ajxEnquete = new Object(ajaxutil);
	ajxEnquete.method = "GET";
	ajxEnquete.target = document.getElementById("spResult")
	ajxEnquete.msg = "<img src='img/loading.gif' align='center'>";
	ajxEnquete.request("ajx/enquete_ajx.php?opcao="+opcao);		
}
function exibir_enquete(id,acao){
	ajxResult = new Object(ajaxutil);
	ajxResult.method = "GET";
	ajxResult.target = document.getElementById("spResult")
	ajxResult.msg = "<img src='img/loading.gif' align='center'>";
	ajxResult.request("ajx/enquete_ajx.php?id="+id+"&acao="+acao);		
}

//////////////////////VERSAO PARA IMPRESSAO
function versaoImpressao(id){
	window.open('template/impressao.php?id='+id,'Impressao','toolbar=no, scrollbars=yes, width=600, height=500');
}

///////////////////// ABRE TELA LIGHT WINDOW
function abreTela(url,method,classe,width,oklabel){
	document.getElementById('banner').style.visibility='hidden';
	//document.getElementById('projetos').style.visibility='hidden';
	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);
}


///////////////////////////////////
//Captura posicao do mouse
//////////////////////////////////
// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.
if (document.all) { // Internet Explorer
//    document.onmousemove = captureMousePosition;
    document.onmouseup = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
//    document.onmousemove = captureMousePosition;
    document.onmouseup = captureMousePosition;
}

// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page
yTop = 0; // Define a posicao TOP da tela

function captureMousePosition(e) {
if (document.all) {
		yTop = document.body.scrollTop;
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard 
		yTop= window.pageYOffset;
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}
//////////////////////////////////
function contadorcaracter(obj,tam,cont) {
        document.getElementById(cont).value = tam - obj.value.length;
        if (obj.value.length > tam) {
           alert ("Texto maior que "+tam+" caracteres, ele será cortado!!!");
			varTemp = obj.value;
			obj.value = varTemp.substring(0,tam);
			document.getElementById(cont).value = tam - obj.value.length;
        }
}


function trim(objStr) {
// trim:
// Elimina espacos vazios no inicio e no final da string
	return objStr.replace(/(^\s*)|(\s*$)/g, "");
}

function ValidaHora(obj)
{
	var hh = obj.value.substring(0,2);
	var mm = obj.value.substring(3,5);
	
	if (hh > 23) return 1
	if (mm > 59) return 1
	
	return 0
}

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;
  }
}


function go(pDestino, pDvDestino){
	ajxEnvia = new Object(ajaxutil);
	if (pDvDestino == null) {
		ajxEnvia.target = document.getElementById("dvTela")
	}
	else {
		ajxEnvia.target = document.getElementById(pDvDestino)
	}
	ajxEnvia.msg = "";
	ajxEnvia.request (pDestino);
	return false
}

// Codificação e decodificação de UTF-8 - CHAR ISO-8859-1
// url_encode version 1.0 
function url_encode(str) { 
    var hex_chars = "0123456789ABCDEF"; 
//    var noEncode = /^([a-zA-Z0-9\_\-\.<>\s\\\"\',\{\}\[\]=#;:!\(\)\/])$/; 
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
    var n, strCode, hex1, hex2, strEncode = ""; 
//	str = str.replace(/\n/gi,"\r\n")
    for(n = 0; n < str.length; n++) { 
        if (noEncode.test(str.charAt(n))) { 
            strEncode += str.charAt(n); 
        } else { 
            strCode = str.charCodeAt(n); 
            hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
            hex2 = hex_chars.charAt(strCode % 16); 
            strEncode += "%" + (hex1 + hex2); 
        } 
    } 
    return strEncode; 
} 

// url_decode version 1.0 
function url_decode(str) { 
    var n, strCode, strDecode = ""; 

    for (n = 0; n < str.length; n++) { 
        if (str.charAt(n) == "%") { 
            strCode = str.charAt(n + 1) + str.charAt(n + 2); 
            strDecode += String.fromCharCode(parseInt(strCode, 16)); 
            n += 2; 
        } else { 
            strDecode += str.charAt(n); 
        } 
    } 
    return strDecode; 
}

