function validaForm(f) {
	avisos = "";
	for(i=0;i<f.length;i++) {
		id = f.elements[i].id;
		id = id.substr(0,4);
		if(id == "vTxt") fTexto(f.elements[i]);
		if(id == "vEm0") fEmail(f.elements[i],false);
		if(id == "vEm1") fEmail(f.elements[i],true);
		/*
		if(id == "vfSelect") fSelect(f.elements[i]);
		if(id == "vfEmail") fEmail(f.elements[i]);
		if(id == "vfNumero") fNumero(f.elements[i]);
		if(id == "vfMoeda") fMoeda(f.elements[i]);
		if(id == "vfData") fData(f.elements[i],1);
		if(id == "vfDataN") fData(f.elements[i],0);
		if(id == "vfCnpjCpf") fCnpjCpf(f.elements[i]);
		if(id == "vfUnico") fUnico(f.elements[i],i);
		if(id == "vfUnicoEmail") fUnicoEmail(f.elements[i],i);
		*/
	}
	if(avisos!="") {
		alert("Verifique o preenchimento do formulário\n\n"+avisos);
		return false;
	}
	return true;
}
// Trata campos texto obrigatórios ----/
function fTexto(campo) {
	if(campo.type=="select-one") {
		if(campo.selectedIndex==0) {
			cp = campo.title;
			avisos = avisos + " - O campo "+cp+" é obrigatório!\n";
		}
	} else {
		if(campo.value.length < 3) {
			if(campo.alt)
				cp = campo.alt;
			else
				cp = campo.title;
			avisos = avisos + " - O campo "+cp+" é obrigatório!\n";
		}
	}
}
// Trata o e-mail válido ----/
function fEmail(campo,obrigatorio) {
	if(obrigatorio || campo.value!="") {
		rexp = new RegExp("/^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/");
		if(!rexp.test(campo.value)) {
			avisos = avisos + " - Preencha o campo "+campo.alt+" com um e-mail válido!\n";
		}
	}
}
function rascunho() {
	window.document.fDados.in_ativo.selectedIndex=2;
	if(validaForm(window.document.fDados))
		window.document.fDados.submit();
}
function publicar() {
	window.document.fDados.in_ativo.selectedIndex=1;
	if(validaForm(window.document.fDados))
		window.document.fDados.submit();	
}
function abreRel(bt,id) {
	if(window.document.getElementById(id).style.display=="none") {
		window.document.getElementById(id).style.display="block";
		bt.value = "-";
	} else {
		window.document.getElementById(id).style.display="none";
		bt.value = "+";
	}
}
function abre(url) {
	window.open(url,'pop','width=1,height=1');
	void(0);
}
function carregaFlash(caminho,largura,altura) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+largura+'" height="'+altura+'">');
	document.write('<param name="movie" value="'+caminho+'">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<param name="menu" value="false">');
	document.write('<embed src="'+caminho+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+largura+'" height="'+altura+'" wmode="transparent"></embed>');
	document.write('</object>');
}
function buscaVideo(c,i) {
	if(i==0)
		if(c.value=="buscar vídeos") c.value="";
	if(i==1)
		if(c.value=="") c.value="buscar vídeos";
}
function comenteNom(c,i) {
	if(i==0)
		if(c.value=="seu nome") c.value="";
	if(i==1)
		if(c.value=="") c.value="seu nome";
}
function comenteEma(c,i) {
	if(i==0)
		if(c.value=="e-mail") c.value="";
	if(i==1)
		if(c.value=="") c.value="e-mail";
}
function comenteCom(c,i) {
	if(i==0)
		if(c.value=="mande sua pergunta ou sugestão de convidado") c.value="";
	if(i==1)
		if(c.value=="") c.value="mande sua pergunta ou sugestão de convidado";
}
function buscaVal(c,i) {
	if(i==0)
		if(c.value=="buscar convidado") c.value="";
	if(i==1)
		if(c.value=="") c.value="buscar convidado";
}
var hRatings = function(t) {window.document.getElementById("tvratings").innerHTML = t.responseText;}
var eRatings = function(t) {alert('Erro!');}
function ratings(nota,id) {
	var url		= "ajax_rating.php";
	new Ajax.Request(url, {method:'get', parameters:'nota='+nota+'&id='+id, onSuccess:hRatings, onFailure:eRatings});
}
var hRatingsfm = function(t) {window.document.getElementById("fmratings").innerHTML = t.responseText;}
var eRatingsfm = function(t) {alert('Erro!');}
function fmratings(nota,id) {
	var url		= "ajax_rating.php";
	new Ajax.Request(url, {method:'get', parameters:'nota='+nota+'&id='+id, onSuccess:hRatingsfm, onFailure:eRatingsfm});
}
var hComentarios = function(t) {window.document.getElementById("comTV").innerHTML = t.responseText;}
var eComentarios = function(t) {alert('Erro!');}
function comentarios(ini,video) {
	var url			= "ajax_comentarios.php";
	var comentario	= window.document.fComentario.comentario.value;
	var nome		= window.document.fComentario.nome.value;
	var email		= window.document.fComentario.email.value;
	new Ajax.Request(url, {method:'post', postBody:'video='+video+'&ini='+ini+'&nome='+nome+'&email='+email+'&comentario='+comentario, onSuccess:hComentarios, onFailure:eComentarios});
}	
function incComentario(video) {
	if(window.document.fComentario.nome.value==""||window.document.fComentario.email.value==""||window.document.fComentario.comentario.value==""||window.document.fComentario.nome.value=="seu nome"||window.document.fComentario.email.value=="email"||window.document.fComentario.comentario.value=="deixe seu comentário") {
		alert("Preencha seu nome, e-mail e comentário para postar");
	} else {
		comentarios(0,video);
		window.document.fComentario.reset();
	}
}
var hRelacionados = function(t) {window.document.getElementById("relTV").innerHTML = t.responseText;}
var eRelacionados = function(t) {alert('Erro!');}
function relacionados(tags,video,ini) {
	var url			= "ajax_tags.php";
	new Ajax.Request(url, {method:'get', parameters:'i='+video+'&tags='+tags+'&ini='+ini, onSuccess:hRelacionados, onFailure:eRelacionados});
}
function relacionados2(tags,video,ini) {
	window.document.getElementById("tvrelacionados2").className="tvrecentes2_on";
	window.document.getElementById("tvvotados2").className="tvvotados2_off";
	window.document.getElementById("tvvistos2").className="tvvistos2_off";
	var url			= "ajax_tags.php";
	new Ajax.Request(url, {method:'get', parameters:'i='+video+'&tags='+tags+'&ini='+ini, onSuccess:hRelacionados, onFailure:eRelacionados});
}
function maisvotados(ini) {
	window.document.getElementById("tvrelacionados2").className="tvrecentes2_off";
	window.document.getElementById("tvvotados2").className="tvvotados2_on";
	window.document.getElementById("tvvistos2").className="tvvistos2_off";	
	var url			= "ajax_votados.php";
	new Ajax.Request(url, {method:'get', parameters:'ini='+ini, onSuccess:hRelacionados, onFailure:eRelacionados});
}
function maisvistos(ini) {
	window.document.getElementById("tvrelacionados2").className="tvrecentes2_off";
	window.document.getElementById("tvvotados2").className="tvvotados2_off";
	window.document.getElementById("tvvistos2").className="tvvistos2_on";		
	var url			= "ajax_vistos.php";
	new Ajax.Request(url, {method:'get', parameters:'ini='+ini, onSuccess:hRelacionados, onFailure:eRelacionados});
}
var hLista = function(t) { window.document.getElementById("fmesq").innerHTML = t.responseText; }
var eLista = function(t) {alert('Erro!');}
function lista(tipo,reg,ini,chave,az,dt) {
	var url			= "ajax_busca.php";
	new Ajax.Request(url, {method:'get', parameters:'tipo='+tipo+'&reg='+reg+'&ini='+ini+'&chave='+chave+'&az='+az+'&dt='+dt, onSuccess:hLista, onFailure:eLista});
}


var hEnvia = function(t) {alert(t.responseText);}
var eEnvia = function(t) {alert('Erro!');}
function envia() {
	var url			= "ajax_comentarios.php";
	var comentario	= window.document.fComentario.comentario.value;
	var nome		= window.document.fComentario.nome.value;
	var email		= window.document.fComentario.email.value;
	new Ajax.Request(url, {method:'post', postBody:'nome='+nome+'&email='+email+'&comentario='+comentario, onSuccess:hEnvia, onFailure:eEnvia});
}
function envComentario() {
	if(window.document.fComentario.nome.value==""||window.document.fComentario.email.value==""||window.document.fComentario.comentario.value==""||window.document.fComentario.nome.value=="seu nome"||window.document.fComentario.email.value=="e-mail"||window.document.fComentario.comentario.value=="mande sua pergunta ou sugestão de convidado") {
		alert("Preencha seu nome, e-mail e mensagem para enviar");
	} else {
		envia();
		window.document.fComentario.reset();
	}
}

function trp_menu(id,st) {
	if(st==1) {
		window.document.getElementById(id).style.visibility = "visible";
		eval("tmp"+id+" = window.setTimeout('window.document.getElementById(\""+id+"\").style.visibility = \"hidden\";',200);");
		eval("window.clearTimeout(tmp"+id+");");
	}
	if(st==2) {
		eval("window.clearTimeout(tmp"+id+");");	
	}
	if(st==0) {
		eval("tmp"+id+" = window.setTimeout('window.document.getElementById(\""+id+"\").style.visibility = \"hidden\";',550);");
	}
	if(st==3) {
		eval("tmp"+id+" = window.setTimeout('window.document.getElementById(\""+id+"\").style.visibility = \"hidden\";',550);");
	}
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function soNumeros(e) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
function mskData(e,f) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if(tecla!=8) {
		if(f.value.length==2) f.value = f.value + "/";	
		if(f.value.length==5) f.value = f.value + "/";
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
function mskDataHM(e,f) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if(tecla!=8) {
		if(f.value.length==2) f.value = f.value + "/";	
		if(f.value.length==5) f.value = f.value + "/";
		if(f.value.length==10) f.value = f.value + " ";
		if(f.value.length==13) f.value = f.value + ":";
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
function mskTEL(e,f) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if(tecla!=8) {
		if(f.value.length==0) f.value = f.value + "(";
		if(f.value.length==3) f.value = f.value + ") ";
		if(f.value.length==9) f.value = f.value + "-";
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
function mskCEP(e,f) {
	if(window.event) { // IE
		tecla = e.keyCode
	} else if(e.which) { // Netscape/Firefox/Opera
		tecla = e.which
	}
	if(tecla!=8) {
		if(f.value.length==5) f.value = f.value + "-";
	}
	if((tecla < 48 && tecla != 8)||(tecla > 57)) {
		 	return false;
	}	
}
var hCad = function(t) {
	if(t.responseText=="ok")
	   	window.document.fCad.submit();
	else
		alert("Este e-mail já está cadastrado!");
}
var eCad = function(t) {return false;}
function cad(email) {
	var url			= "ajax_cadastro.php";
	new Ajax.Request(url, {method:'get', parameters:'email='+email, onSuccess:hCad, onFailure:eCad});
}

var hLogin = function(t) {
	//alert(t.responseText);
	window.document.getElementById("menu").innerHTML = t.responseText;
}
var eLogin = function(t) {return false;}
function monta_menu() {
	var url			= "/lib/ajax_logado.php";
	new Ajax.Request(url, {method:'get', parameters:'id='+readCookie("trp_nome"), onSuccess:hLogin, onFailure:eLogin});
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//VERIFICA CPF
function check_cpf(cpf_ver)
{
 var p="";
 var soma=0, i=0, num=0, d=0, d1=0, d2=0, zero;
 if (cpf_ver.length < 3)
    {
     alert("CPF INCORRETO !");
     return false;
    }
 for (i=0; i < cpf_ver.length; i++)
     {
      if ((digito(cpf_ver.substring(i,i+1))) != 1)
         {
         alert("CPF INCORRETO !");
         return false;
         }
     }
 zero = parseInt(cpf_ver, 10);
 if (zero == 0)
    {
     alert("CPF INCORRETO !");
     return false;
    }
 p = cpf_ver.substring(0,(cpf_ver.length - 2));

 for (i = (p.length - 1), d=2; i >= 0; i--, d++)
     {
      num = parseInt(p.charAt(i), 10);
      soma += num*d;
     }
 num = soma / 11;
 num = parseInt(num, 10);
 num = 11 - (soma - (num * 11));
 if (num >= 10) num = 0;
 d1 = num;
 p = cpf_ver.substring(0,(cpf_ver.length - 1));
 for (i = (p.length - 1), d=2, soma=0; i >= 0; i--, d++)
     {
      num = parseInt(p.charAt(i), 10);
      soma += num*d;
     }
 num = soma / 11;
 num = parseInt(num, 10);
 num = 11 - (soma - (num * 11));
 if (num >= 10) num = 0;
 d2 = num;
 p = cpf_ver.substring((cpf_ver.length - 2), (cpf_ver.length - 1));
 if (d1 != parseInt(p, 10))
    {
     alert("CPF INCORRETO !");
     return false;
    }
 p = cpf_ver.substring((cpf_ver.length - 1), (cpf_ver.length));
 if (d2 != parseInt(p, 10))
    {
     alert("CPF INCORRETO !");
     return false;
    }
if(cpf_ver=="11111111111"||cpf_ver=="22222222222"||cpf_ver=="33333333333"||cpf_ver=="44444444444"||cpf_ver=="55555555555"||cpf_ver=="66666666666"||cpf_ver=="77777777777"||cpf_ver=="88888888888"||cpf_ver=="99999999999"||cpf_ver=="00000000000"||cpf_ver=="00000000191") 
	{
     alert("CPF INCORRETO !");
     return false;
	}
 return true;
}

// VERIFICA CPF
function cpf_ver(CPF)
{
	if (CPF.value!="" && !check_cpf(CPF.value)) {
		CPF.value  = "";
		CPF.focus();
		return false;
	}
	return true;
}

//RETORNA 1 PARA NÚMEROS E 0 PARA STRINGS
 function digito(dig)
{
 if (dig != "0" && dig !="1" && dig !="2" && dig !="3" && dig !="4"
     && dig !="5" && dig !="6" && dig !="7" && dig !="8" && dig !="9")
    return 0;
 else return 1;
}