function addFav(url, title){	
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}


function checkMail(email){
  er = /^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/;
  
  if(er.exec(email))
    {
      return true;
    } else {
      return false;
    }
}

    
function campoData(campo){
	var valor = campo.value.length;	
	if(valor == 2 || valor == 5)
		campo.value = campo.value+"/";
}

function campoCep(campo){
	var valor = campo.value.length;	
	if(valor == 5)
		campo.value = campo.value+"-";
}
function campoHora(campo){
	var valor = campo.value.length;	
	if(valor == 2 || valor == 5)
		campo.value = campo.value+":";
}
function campoCpf(campo){	
	var valor = campo.value.length;	
	if(valor == 3 || valor == 7)
		campo.value = campo.value+".";
	if(valor == 11)
		campo.value = campo.value+"-";		
}

function campoMoney(cur,len)
{
   n='__0123456789';
   d=cur.value;
   l=d.length;
   r='';
   if (l > 0)
   {
    z=d.substr(0,l-1);
    s='';
    a=2;
    for (i=0; i < l; i++)
    {
        c=d.charAt(i);
        if (n.indexOf(c) > a)
        {
            a=1;
            s+=c;
        };
    };
    l=s.length;
    t=len-1;
    if (l > t)
    {
        l=t;
        s=s.substr(0,t);
    };
    if (l > 2)
    {
        r=s.substr(0,l-2)+','+s.substr(l-2,2);
    }
    else
    {
        if (l == 2)
        {
            r='0,'+s;
        }
        else
        {
            if (l == 1)
            {
                r='0,0'+s;
            };
        };
    };
    if (r == '')
    {
        r='0,00';
    }
    else
    {
        l=r.length;
        if (l > 6)
        {
            j=l%3;
            w=r.substr(0,j);
            wa=r.substr(j,l-j-6);
            wb=r.substr(l-6,6);
            if (j > 0)
            {
                w+='.';
            };
            k=(l-j)/3-2;
            for (i=0; i < k; i++)
            {
                w+=wa.substr(i*3,3)+'.';
            };
            r=w+wb;
        };
    };
   };
   if (r.length <= len)
   {
    cur.value=r;
   }
   else
   {
    cur.value=z;
   };
   return 'ok';
};

function ajaxFunction()
{
var ajax;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  ajax=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  ajax=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
  return ajax;
}

function horario(){
	var Elem = document.getElementById("relogio");
	var hoje = new Date();
	var horas = hoje.getHours();
	if (horas < 10){
	horas = "0"+horas;
	}
	var minutos = hoje.getMinutes();
	if (minutos < 10){
	minutos = "0"+minutos;
	}
	var segundos = hoje.getSeconds();
	if (segundos < 10){
	segundos = "0"+segundos;
	}
	Elem.innerHTML = horas+":"+minutos+":"+segundos;
}
function buscaGoogle(){
	var busca = document.getElementById('buscarTopo').value;
	window.open('http://www.google.com/search?sitesearch=www.portaldograndeabc.com&as_q='+busca);
}

function eventKey(e){
	if(window.event){
		tecla = e.keyCode;
	}else if(e.which){
		tecla = e.which;
	}
	return tecla;
}

function enterBusca(e){
	var tecla = eventKey(e);
	if(tecla == 13){
		buscaGoogle();
	}
}

function validaLogin(){
	var login = document.getElementById('loginUsuario').value;
	var senha = document.getElementById('loginSenha').value;
	if(login == '' || senha == ''){
		alert('Favor preencher todos os campos antes de prosseguir.');
		return false;
	}
}

function msnCall()
{
	var w=window.open('#','MSN','width=550,height=350, top=100');
	w.document.write('<iframe src="http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=52c30b1973e5f378@apps.messenger.live.com&mkt=pt-br" width="500" height="300" style="border: solid 1px black; width: 500px; height: 300px;" frameborder="0"></iframe>');
	w.document.close();
}

function nextBackNews(cmd){
	var valor = 0;
	if(cmd == "back"){		
		if(noticiaAtual == 1){
			valor = 6;
		}else{
			valor = noticiaAtual - 1;
		}
	}
	if(cmd == "next"){
		if(noticiaAtual == 6){
			valor = 1;
		}else{
			valor = noticiaAtual + 1;
		}
	}
	mudaNoticia(valor);
}

function passaNoticias(){
	var browserName=navigator.appName;
	if ( browserName=="Netscape" ){
		var tempoPassar = 3000;
	} else {
		var tempoPassar = 800;
	}
	tempoNews++;
	if(tempoNews != tempoPassar){
		tempoNews++;
	}else{		
		var valor = 0;
		if(noticiaAtual == 6){
			valor = 1;
		}else{
			valor = noticiaAtual + 1;
		}
		mudaNoticia(valor);
	}
}

function validarCpf( cpf )
{  
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(cpf)){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
	}
	
	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");
	
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("CPF inválido. Tente novamente.");
	  return false;
	}
	
	soma = 0;
	for(i = 0; i < 9; i++)
		 soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
	 resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
	 resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
	}
	return true;
}

function remove ( str, sub )
{
	i = str.indexOf(sub);
	r = "";
	if (i == -1) return str;
	r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
	return r;
}
 
function checkTodos ( chk )
{
	var INPUTs = document.getElementsByTagName('input');
	for ( i = 0; i < INPUTs.length; i++ ) {
		INPUTs.item(i).checked = chk.checked;
	}	
}

function deletarTodos ()
{
	document.getElementById('form_tipo').value = 'del';
	if ( formTodos() ) {
		document.getElementById('formLista').submit();
	}
}

function statusTodos ( status )
{
	var cmd = '';
	switch ( status ) {
		case 0:
			cmd = 'inativar';
			break;
		case 1:
			cmd = 'ativar';
			break;
	}
	document.getElementById('form_tipo').value = cmd;
	if ( formTodos() ) {
		document.getElementById('formLista').submit();
	}
}

function formTodos ()
{
	var ok = 0;
	var INPUTs = document.getElementsByTagName('input');
	for ( i = 0; i < INPUTs.length; i++ ) {
		if ( INPUTs.item(i).checked == true ){
			ok++;
		}
	}
	if ( ok > 0 ) {
		document.getElementById('form_cmd').value = 'todos';
		return true;
	} else {
		alert('Nenhum registro selecionado.');
		return false;
	}
}

String.prototype.trim = function() {
  return (this.replace(/^\s+/, '').replace(/\s+$/, ''));
}

function checkData(valor) {    
	var date=valor;
	var ardt=new Array;
	var ExpReg=new RegExp("([1-9]|[12][0-9]|3[01])/([1-9]|1[012])/[12][0-9]{3}");
	ardt=date.split("/");
	erro=false;
	if ( date.search(ExpReg)==-1){
		erro = true;
		}
	else if (((ardt[1]==4)||(ardt[1]==6)||(ardt[1]==9)||(ardt[1]==11))&&(ardt[0]>30))
		erro = true;
	else if ( ardt[1]==2) {
		if ((ardt[0]>28)&&((ardt[2]%4)!=0))
			erro = true;
		if ((ardt[0]>29)&&((ardt[2]%4)==0))
			erro = true;
	}
	if (erro) {
		return false;
	}
	return true;

}
           
function checkHora(hora){
	hrs = (hora.substring(0,2)); 
	min = (hora.substring(3,5)); 
	seg = (hora.substring(6,8));
	
	// verifica data e hora 
	if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
	  return false;
	} 
	
	if (hora == "") { 
	  return false;
	}
	return true;
}