// JavaScript Document

function overTDdir(cel){
	cel.style.cursor='hand';
	cel.style.backgroundColor=fundo_luz_coluna_direita;
	cel.style.color=luz_texto_coluna_direita;
}

function outTDdir(cel){
	cel.style.backgroundColor='';
	cel.style.color=texto_coluna_direita;
}

function overTDesq(cel){
	cel.style.cursor='hand';
	cel.style.backgroundColor=fundo_luz_coluna_esquerda;
	cel.style.color=luz_texto_coluna_esquerda;
}

function outTDesq(cel){
	cel.style.backgroundColor='';
	cel.style.color=texto_coluna_esquerda;
}

function MudaPagina(sentido) {
	document.form_filmes.comando_pagina.value = sentido;
	document.form_filmes.submit();	
}	

function mudaGenero(obj){	
	num = obj.options[obj.selectedIndex].value;
	document.form_filmes.cod_genero.value = num;
	if (num == 59) document.form_filmes.classif.value = 'C';
	document.form_filmes.absPage.value = '1';
	document.form_filmes.submit();
}


function Comentar() {	
	f1 = document.comentarios;	
	coment = f1.comentario.value;		
	if (coment.length > 50) {				
		f1.submit();
	}
	else {
		alert("Seu Comentário deve conter no mínimo 50 caracteres. Obrigado!");
	}
}

msgdirty = false;

function check_mail(strmail) {
	//alert ("oi")	
	if(strmail == '') 
		return;
	chartosplit = ',;';
	protectchar = '"';
	temp = '';
	armail = new Array();
	inthechar = false; 
	lt = '<';
	gt = '>'; 
	isclosed = true;

	for(i=0;i<strmail.length;i++) {
		thischar = strmail.charAt(i);
		if(thischar == lt && isclosed) isclosed = false;
		if(thischar == gt && !isclosed) isclosed = true;
		if(thischar == protectchar) inthechar =	(inthechar)?0:1;
		if(chartosplit.indexOf(thischar) != -1 && !inthechar && isclosed) {
			armail[armail.length] = temp; temp = '';
		} 
		else
		  temp += thischar;
	}
	armail[armail.length] = temp; 

	for(i=0;i<armail.length;i++) {
		thismail = armail[i]; strPat = /(.*)<(.*)>/;
		matchArray = thismail.match(strPat); 
		if (matchArray != null) 
			strEmail = matchArray[2];
		else {
			strPat = /([-a-zA-Z0-9_$+.]+@[-a-zA-Z0-9_.]+[-a-zA-Z0-9_]+)((.*))/; 
			matchArray = thismail.match(strPat); 
			if (matchArray != null) 
				strEmail = matchArray[1]; 
			else 
				strEmail = thismail;
		}
		if (strEmail.charAt(0) == '"' && strEmail.charAt(strEmail.length-1) == '"')
			 strEmail = strEmail.substring(1,strEmail.length-1) 
		if(strEmail.charAt(0) == '<' && strEmail.charAt(strEmail.length-1) == '>') 
			strEmail = strEmail.substring(1,strEmail.length-1)
		strPat = /([-a-zA-Z0-9_$+.]+@[-a-zA-Z0-9_.]+[-a-zA-Z0-9_]+)((.*))/;
		matchArray = strEmail.match(strPat); 
		if(matchArray == null){					
			return false;}
		else {
			return true;
		}
	}
}

function isDate(str, inenglish, separator, minyear, maxyear) {
	inenglish = defaultIfNull(inenglish, false);
	separator = defaultIfNull(separator, '/');
	minyear = defaultIfNull(minyear, 1900);
	maxyear = defaultIfNull(maxyear, 2100);
	str = trim(str);
	var sep1 = inStr(1, str, separator);
	if(!inInterval(sep1, 2, 3)) return(false);
	var sep2 = inStr(sep1 + 1, str, separator);
	if(!inInterval(sep2 - sep1, 2, 3)) return(false);
	if(!inInterval(str.length - sep2, 1, maxyear.length)) return(false);
	var aux = str.substr(0, sep1 - 1);
	var mes = str.substr(sep1, sep2 - sep1 - 1);
	var ano = str.substr(sep2);
	var dia = (inenglish?mes:aux);
	mes = (inenglish?aux:mes);
	if(!isNumeric(dia, '')) return(false); else dia = parseInt(leftZeros(dia));
	if(!isNumeric(mes, '')) return(false); else mes = parseInt(leftZeros(mes));
	if(!isNumeric(ano, '')) return(false); else ano = parseInt(ano);
	if(!inInterval(ano, minyear, maxyear)) return(false);
	if(!inInterval(mes, 1, 12)) return(false);
	if(!inInterval(dia, 1, monthDays(mes, ano))) return(false);
	return(true); 
}

function leftZeros(where, howmuch) {
	howmuch = defaultIfNull(howmuch, 2);
	where += '';
	if(howmuch == 0) { 
		for(var i = 0; i < howmuch; i++) where = '0' + where;
		return(where.substr(where.length - howmuch)); 
	} else { 
		for(; where.substr(0, 1) == '0'; where = where.substr(1));
		if(where == '') where = '0';
		return(where); } 
} 

function monthDays(wmonth, wyear) {
return((wmonth == 2)?(((wyear % 4) > 0 || (wyear % 100 == 0 && wyear % 400 > 0))?28:29):((wmonth < 8)?(30 + (wmonth % 2)):(31 - (wmonth % 2)))); }

function trim(what) {
return(rTrim(lTrim(what))); }

function lTrim(what) {
return(what.substr(0, 1) == ' '?lTrim(what.substr(1)):what); }

function rTrim(what) {
return(what.substr(what.length - 1, 1) == ' '?rTrim(what.substr(0, what.length - 1)):what); }

function inStr(strini, strtotal, strfind) {
strini = defaultIfNull(strini, 1);
if(strini < 1) strini = 1;
for(var i = strini - 1; i < strtotal.length - strfind.length + 1; i++)
	if(strtotal.substr(i, strfind.length).toUpperCase() == strfind.toUpperCase()) return(i + 1);
return(0); }

function defaultIfNull(whichparameter, whichdefault) {
if(whichdefault == null) whichdefault = '';
if(whichdefault == undefined) whichdefault = '';
if(whichparameter == null) return(whichdefault);
if(whichparameter == undefined) return(whichdefault);
return(whichparameter); }

function isNumeric(str, separator, canbeneg) {
separator = trim(defaultIfNull((separator + ' ').charAt(0), ','));
canbeneg = defaultIfNull(canbeneg, true);
var foundcomma = false;
var intSize;
str = trim(str);
intSize = str.length;
if(intSize == 0) return(false); 
if(canbeneg) {
	if(str.charAt(0) == '-') {
		str = str.substr(1);	
	} else {
		if(str.charAt(0) == '+') str = str.substr(1); }
} else {
	if(str.charAt(0) == '+') str = str.substr(1); }
if(intSize == 0) return(false); 
for(var i = 0; i < intSize; i++) {
	if(str.charAt(i) == separator) {
		if((i == 0)||(i = intSize - 1)||(foundcomma)) return(false);
		foundcomma = true;
	} else {
		if((str.charAt(i) < '0')||(str.charAt(i) > '9')) return(false); } }
return(true); }

function FormataNumero(v, isMoney, dec, sep) {	
	if (dec == null){dec = 2;}
	if (sep == null){sep = ",";}
	vatual = v.value;	
	vatual = RetiraSinais(vatual);	
	if ((vatual.length >= 3) && (vatual.indexOf("0") == 0))
		vatual = vatual.substr(1, vatual.length -1);		
	if (vatual.length > dec) {
		if (isMoney) {
			Ndecimal = vatual.substr((vatual.length - dec),dec);
			vr = vatual.substring(0,(vatual.length - dec));											
		}
		else {
			vr = vatual;
		}
		tam = vr.length;	
		if (tam > 3) {
			indice = parseInt(tam / 3);
			resto = (tam % 3);		
			valor = ""
			for (i = 1; i <= indice; i++){			
				valor = vr.substr((tam - (3 * i)), 3) + valor ;			
				if ((indice != i) || (resto > 0))
					valor = "." + valor;			
			}
			if (resto >= 1)
				valor = vr.substr(0,resto) + valor;				
		}
		else {
			valor = vr;			
		}
		if (isMoney){
			valor = valor + sep + Ndecimal;
		}
		v.value = valor;
	}	
	else {
		if (isMoney) {
			if (vatual.length == 1)
				v.value = "0" + sep + "0" + vatual;
			else				
				v.value = "0" + sep + vatual;
		}		
	}	
	return;
} 

function RetiraSinais(vatual){
	vatual = vatual.replace(".","");
	vatual = vatual.replace(",","");	
	return(vatual);
}

function inInterval(whichnum, whichmin, whichmax) {
whichmin = defaultIfNull(whichmin, 0);
whichmax = defaultIfNull(whichmax, 100);
return((whichnum >= whichmin)&&(whichnum <= whichmax)); }

function doNumber() {
var whichcode = window.event.keyCode;
if(whichcode < chr('0')) whichcode = 0;
if(whichcode > chr('9')) whichcode = 0;
window.event.keyCode = whichcode; }

function chr(which) {
return(which.charCodeAt(0)); }

function checaCPF(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")
		{
		//alert("CPF inválido.");
		//ocument.tempo.cpf.focus();		
		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)))

		{
		//alert("CPF inválido.");
		//document.tempo.cpf.focus();		
		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)))

		{
		//alert("CPF inválido.");
		//document.tempo.cpf.focus();		
		return(false);
		}
	return(true);
 }
 
 function ShowLogin() {
	nomebairro = document.cadastro.bairro
	if (nomebairro.value == 'Outros')
		Tablelogin.style.display = "none";	
	else 
		Tablelogin.style.display = "block";	
}

function LoginCliente(){
	f = document.login;
	if (f.login_cliente.value == ''){
		alert("Preencha o campo login.")
		f.login_cliente.focus();
		return;
	}
	if (f.senha_cliente.value == ''){
		alert("Preencha o campo senha.")
		f.senha_cliente.focus();
		return;
	}
	f.comando.value = 'C';
	f.submit();
}

function displayGeneros(generos){
	if (generos.style.display == 'none'){
		generos.style.display = 'block';
	}
	else {
		generos.style.display  = 'none';
	}
}

  function EnviarCadastro(tipo){
	toSubmit = true;
	strAlerta = "Por Favor, corrija os seguintes erros: ";
	f1=document.cadastro;
	
	if (tipo == 'C'){
		strLogin = trim(f1.login.value);
		if (strLogin == ""){
		strAlerta = strAlerta + "\nPreencha o campo Login.";
		toSubmit = false;
		}
		else 
		if (strLogin.length < 4){
			strAlerta = strAlerta + "\nLogin inválido. O login deve ter no mínimo 4 dígitos.";
			toSubmit = false;
		}
	}

	if (trim(f1.nome.value) == ""){
		strAlerta = strAlerta + "\nO campo 'Nome' não pode estar vazio."
		toSubmit = false;
	}
	if (f1.bairro.selectedIndex == 0){
		strAlerta = strAlerta + "\nSelecione um 'Bairro' na lista."
		toSubmit = false;
	}
	strCPF = trim(f1.cpf.value)
	if (strCPF == ""){
		strAlerta = strAlerta +  "\nO campo 'CPF' não pode estar vazio."
		toSubmit = false;
	}
	else {
		if (!(checaCPF(strCPF))){
			strAlerta = strAlerta +  "\nAtenção: Número de CPF inválido."
			toSubmit = false;
		}
     }
	if (trim(f1.endereco.value) == ""){
		strAlerta = strAlerta +  "\nO campo 'Endereço' não pode estar vazio."
		toSubmit = false;
	}		
	if (trim(f1.numero.value) == ""){
		strAlerta = strAlerta +  "\nO campo 'Numero' não pode estar vazio."
		toSubmit = false;
	}		
/*	if (trim(f1.cep.value) == ""){
		strAlerta = strAlerta +  "\nO campo 'CEP' não pode estar vazio."
		toSubmit = false;
	}	*/
	strTel = trim(f1.telefone.value)
	strCel = trim(f1.celular.value)
	if ((strTel == "") && (strCel == "")){
		strAlerta = strAlerta +  "\nVocê deve informar seu Telefone ou Celular."
		toSubmit = false;
		}

	 if ((strCel != "") && (strCel.length < 8)) {
		strAlerta = strAlerta +  "\nO número do celular deve ter 8 dígitos."
	    toSubmit = false;
		 }

	 if ((strTel != "") && (strTel.length < 8)) {
			strAlerta = strAlerta +  "\nO número do telefone deve ter 8 dígitos."
			toSubmit = false;
		 }
	
	
	
	if (f1.data_de_nasc.value == "") {
		strAlerta = strAlerta +  "\nO campo 'Data de Nascimento' não pode estar vazio."
		toSubmit = false;
	}
	else 
		if (!(isDate(f1.data_de_nasc.value,false,"/",1910,1990))){
			strAlerta = strAlerta + "\nO campo 'Data de Nascimento' foi preenchido com uma data inválida."
			toSubmit = false;
		}	
	strEmail = trim(f1.email.value)
	if  (strEmail == "") { 
		strAlerta = strAlerta +  "\nO campo 'Email' não pode estar vazio."
		toSubmit = false;
	}
	else 
	if (!(check_mail(strEmail))){
		strAlerta = strAlerta +  "\nPreencha o campo 'Email' corretamente."
		toSubmit = false;
	}
/*========================================================================================================*/
	f1.clienteOK.value = "N";
	if (f1.bairro.options[f1.bairro.selectedIndex].text != "Outros"){     /*um bairro para entrega foi selecionado*/
		f1.clienteOK.value = "S";
		if ((tipo == "A") || ((tipo == "C") && (f1.bairro.options[f1.bairro.selectedIndex].text != "Outros"))){  /*só faz a crítica se for bom cliente*/
			strlogin = trim(f1.login.value);
			if (strlogin.length == 0){
				strAlerta = strAlerta +  "\nO campo 'Login' não pode estar vazio."
				alert(strAlerta)
				return
				}
			if (strlogin.length < 4){
				strAlerta = strAlerta +  "\nO 'Login' tem que ter, no mínimo, 4 dígitos."
				alert(strAlerta)
				return
				}
				/*================*/
			
			if (f1.senha.value == null) {strsenha = "";}
			else {strsenha = trim(f1.senha.value);}
			
			if (strsenha.length == 0){
				strAlerta = strAlerta +  "\nO campo 'Senha' não pode estar vazio."
				alert(strAlerta)
				return
				}
			if (strsenha.length < 4){
				strAlerta = strAlerta +  "\nA 'Senha' tem que ter, no mínimo, 4 dígitos."
				alert(strAlerta)
				return
				}
			if (tipo == 'C') {
				strconfirma = trim(f1.confirma.value);
				if (strsenha != strconfirma){
					strAlerta = strAlerta +  "\nA 'Senha' não confere com campo de confirmação."
					toSubmit = false;
				}
			}
		} /* fim da critica da senha */
		
			/*critica nova senha*/
		
		if (tipo == 'A'){
			strNsenha = trim(f1.novasenha.value);
			if (strNsenha.length > 0){
				if (strNsenha.length < 4){
					strAlerta = strAlerta +  "\nA 'Nova Senha' tem que ter, no mínimo, 4 dígitos."
					alert(strAlerta)
					return
				}
				if (strsenha == strNsenha) {
					strAlerta = strAlerta +  "\nA 'Nova Senha' tem que ser diferente da senha."
					alert(strAlerta)
					return
				}
				strconfirma = trim(f1.confirma.value);
				if (strNsenha != strconfirma){
					strAlerta = strAlerta +  "\n'Nova Senha' não confere com campo de confirmação."
					toSubmit = false;
				}
			}
		}			
	}
	if (toSubmit){
		f1.submit();	}
	else {
		alert(strAlerta);	
	}
}
function validata(obj){
	if (obj.value.length==2) obj.value = obj.value + "/";
	if (obj.value.length==5) obj.value = obj.value + "/";
}

function AbrirJanela(arq, jan, larg, alt)   {
     features = "toolbar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=" + larg + ", height=" + alt   
     novajanela=window.open(arq, jan, features)
}

function FecharPedido(){			
	if ((document.cesta.nome_cliente.value == "") || (document.cesta.endereco.value == "") || (document.cesta.bairro.value == "")) {
		alert("Seus dados estão incompletos. Não será possível fechar o pedido!");
		return;
	}
	document.cesta.comando.value = "P";
	document.cesta.submit();
}

function Help(){
	if (document.login.cpf.value == ''){
		alert("Preencha o campo CPF!");
		return;
	}
	document.login.comando.value = 'H';
	document.login.submit();
}

function getFilmes() { 

  // Escolha aleatória do 1º banner
  Indice = Math.floor(Math.random() * cod_filmes.length);

  document.write("<a href='javascript:GoDVD()' class='lnkColD'><img src='imagens/" + img_filmes[Indice] + "' width='80' height='115' id='img_vitrine_filmes' border='2' style='boder-color:#333333'><br><span id='titulo_vitrine_filmes' style='font-weight:bold'>" + titulos[Indice] + "</span></a>" );
  timer1 = window.setTimeout('rotateFilme();', 5000);
}

function rotateFilme() {
  if (++Indice > cod_filmes.length-1) 
    Indice = 0;
    
  img_vitrine_filmes.src = 'imagens/' + img_filmes[Indice];
  titulo_vitrine_filmes.innerHTML = titulos[Indice];  
  timer1 = window.setTimeout('rotateFilme();', 5000);
}


function getFilmesV() { 

  // Escolha aleatória do 1º banner
  IndiceV = Math.floor(Math.random() * cod_filmesV.length);

  document.write("<a href='javascript:goVenda()' class='lnkColD'><img src='imagens/" + img_filmesV[IndiceV] + "' width='80' height='115' id='img_venda_filmes' border='2' class='bordaFilmes'><br><span id='titulo_venda_filmes' style='font-weight:bold'>" + titulosV[IndiceV] + "</span></a>" );
  timer2 = window.setTimeout('rotateFilmeV();', 5000);
}

function rotateFilmeV() {
  if (++IndiceV > cod_filmesV.length-1) 
    IndiceV = 0;
    
  img_venda_filmes.src = 'imagens/' + img_filmesV[IndiceV];
  titulo_venda_filmes.innerHTML = titulosV[IndiceV];  
  timer2 = window.setTimeout('rotateFilmeV();', 5000);
}

function GoDVD(){
	location.href='dvd.asp?codigoFilme=' + cod_filmes[Indice];
}

function goVenda(){
	location.href='venda_filmes.asp';
}

function getProdutos() { 

  // Escolha aleatória do 1º banner
  Indice = Math.floor(Math.random() * produtos.length);

  document.write("<img src='imagens/produtos/prod_" + produtos[Indice] + ".gif' width='60' height='60' id='img_vitrine_produtos' border='2'><br><span id='nome_vitrine_produtos'>" + titulos[Indice] + "</span>" );
  window.setTimeout('rotateProduto();', 10000);
}

function rotateProduto() {
  if (++Indice > produtos.length-1) 
    Indice = 0;
    
  img_vitrine_produtos.src = 'imagens/produtos/prod_' + produtos[Indice] + '.gif';    
  titulo_vitrine_produtos.innerHTML = nomes[Indice];  
  window.setTimeout('rotateProduto();', 10000);
}

function buscar_filme(){
	f1 = document.procura_filmes;
	if ((f1.busca_ator.value.length > 0) || (f1.busca_titulo.value.length > 0) || (f1.busca_genero.selectedIndex > 0)){
		f1.submit();
	}
	else {
		alert("Preencha ao menos um dos campos da procura!!");		
	}
}

function buscar_produto(){
	f1 = document.busca_produto;
	if (f1.nome_produto.value.length > 0) {
		f1.submit();
	}
	else {
		alert("Preencha o campo da procura!!");		
	}
}

function ComprarProd(cod){
	eval("qtd = document.produtos.qtd_" + cod + ".value;");
	url = "adicionar_produto.asp?cd_produto=" + cod + "&qtd=" + qtd
	location.href = url;
}	

function solicitarBusca(chave){
	f1 = document.busca;
	if (chave){
		if (!(checkMarked(f1.busca))){
			alert("Você deve selecionar pelo menos um dos filmes!");
			return;
		}
	}
	f1.submit();
}

function un_CheckAll(col,valor){
	if (col.length){
		for(x=0;x<col.length;x++){
			col[x].checked = valor;			
		}
	}
	else
		col.checked = valor;	
}

function checkMarked(col){
	marked = false;
	if (col.length){
		for(x=0;x<col.length;x++){
			if (col[x].checked)
				marked = true;
		}
	}
	else
		marked = col.checked;
	return(marked);
}

function Indicar(){	
	if (!(dirty))
		document.amigo.mensagem.value = "";	
	if (document.amigo.nome.value.length < 1){
		alert("Por favor preencha o campo 'Nome do amigo'.");
		return;
	}
	if (!(check_mail(document.amigo.email.value))){
		alert("Por favor preencha o campo 'Email do amigo' com um e-mail válido.");
		return;
	}
	document.amigo.submit();	
}

function setHidden(){
	obj = window.event.srcElement;
	matrix = obj.name.split("_");
	f1 = obj.form;
	campo = eval("f1." + matrix[1]);
	campo.value = obj.value;
	f1.submit();
}