// JavaScript Document

// Abrir uma nova janela redimensionada
MyWindow=null;
function abre(url,name,w,h,resize,scroll)
{
  if(MyWindow != null)
  {
    if(!MyWindow.closed)
    {
      if(MyWindow.name != name)
      {
        MyWindow.close();
        MyWindow =
        window.open(url,name,"toolbar=no,resizable="+resize+",status=yes,menubar=no,scrollbars="+scroll+",top="+((screen.availHeight/2)-(h/2))+",left="+((screen.availWidth/2)-(w/2))+",width="+w+",height="+h)
      }
      else
      {
        MyWindow.focus();
      }
    }
    else
    {
      MyWindow =
      window.open(url,name,"toolbar=no,resizable="+resize+",status=yes,menubar=no,scrollbars="+scroll+",top="+((screen.availHeight/2)-(h/2))+",left="+((screen.availWidth/2)-(w/2))+",width="+w+",height="+h)
    }
  }
  else
  {
    MyWindow =
    window.open(url,name,"toolbar=no,resizable="+resize+",status=yes,menubar=no,scrollbars="+scroll+",top="+((screen.availHeight/2)-(h/2))+",left="+((screen.availWidth/2)-(w/2))+",width="+w+",height="+h)
  }
}
// Fim abrir uma nova janela redimensionada

//Configura o sistem de Menu
function horizontal() 
{    
	var navItems = document.getElementById("barra").getElementsByTagName("li");            
	for (var i=0; i< navItems.length; i++) 
	{
		if((navItems[i].className == "menuvertical") || (navItems[i].className == "submenu"))           
		{                  
			if(navItems[i].getElementsByTagName('ul')[0] != null)                  
			{                         
				navItems[i].onmouseover=function() 
				{
					this.getElementsByTagName('ul')[0].style.display="block";this.style.backgroundColor = "#f9f9f9";
				}                         
				navItems[i].onmouseout=function() 
				{
					this.getElementsByTagName('ul')[0].style.display="none";
					this.style.backgroundColor = "#FFFFFF";
				}                  
			}           
		}    
	} 
} 
//Fim configura o sistema de Menu

//Alerta com confirmação 
function confirma(msg,lnk,ret)
{
  if(confirm(msg))
  {
    window.location.href=lnk;
	}
	else
	{
	  alert(ret);
	}
}			  
// Fim alerta com confirmação

// Texto na barra de status
window.defaultStatus=".:: Escola de Rádio <--> © 2010 Newton Carvalho ::.";
// Fim texto na barra de status

// Alterar login do Administrador
function altPwdAdm()
{
  var erro = false;
  var f = document.form;

  if (f.login.value == "") 
  { 
	  erro = true; 
  	alert('O campo "Login" não pode ser nulo!'); 
  	f.login.focus(); 
  }

  if ((!erro) && (f.senha.value.length < 6)) 
  { 
    erro = true; 
		alert('O campo "Senha" não pode ser nulo ou menor que 6 digitos!'); 
		f.senha.focus(); 
  }

  if (!erro) 
  { 
    f.submit(); 
  }
}
// Fim altera login do administrador

// Captura a tecla enter
function onEnter(evt)
{
	var key_code = evt.keyCode  ? evt.keyCode  :
	evt.charCode ? evt.charCode :
	evt.which    ? evt.which    : void 0;
	
	if (key_code == 13)
	{
    userLogin();
  }
}
// Fim captura a tecla enter

// Login no Curso Online
function userLogin()
{
if(document.getElementById('iuser').value == '') 
  { 
    window.alert('Favor informar o Login de acesso!'); 
    document.getElementById('iuser').focus();
    return false;
  }
  else
  if(document.getElementById('ipwd').value == '') 
  { 
    window.alert('Favor informar a Senha de acesso!'); 
    document.getElementById('ipwd').focus();
    return false;
  }
  else
  {
    document.form.submit();
  }
}
// Fim login no Curso Online

// Cadastra novo aluno
function newUser()
{
	var erro = false;
	var f = document.form;
	if (f.login.value == "") 
	{ 
		erro = true; 
		alert('O campo "Login" está vazio!'); 
		f.login.focus(); 
		return false; 
	}
	if ((!erro) && (f.senha.value.length < 6)) 
	{ 
		erro = true; 
		alert('O campo "Senha" não pode ser nulo ou menor que 6 digitos!'); 
		f.senha.focus(); 
		return false;
	}
	if ((!erro) && (f.confirma.value.length < 6)) 
	{ 
		erro = true; 
		alert('O campo "Confirmar senha" não pode ser nulo ou menor que 6 digitos!'); 
		f.confirma.focus(); 
		return false;
	}
	if ((!erro) && (!(f.senha.value == f.confirma.value))) 
	{ 
		erro = true; 
		alert('Confirmação não coincide com a senha!'); 
		f.confirma.value = ""; 
		f.senha.value = ""; 
		f.senha.focus(); 
		return false;
	}
	if (f.nome.value == "") 
	{ 
		erro = true; 
		alert('O campo "Nome" está vazio!'); 
		f.nome.focus(); 
		return false;
	}
	if ((!erro) && (f.ddd.value.length < 2)) 
	{ 
		erro = true; 
		alert('O campo "DDD" não pode ser nulo ou menor que 2 digitos!'); 
		f.ddd.focus(); 
		return false;
	}
	if ((!erro) && (f.telefone.value.length < 8)) 
	{ 
		erro = true; 
		alert('O campo "Telefone" não pode ser nulo ou menor que 8 digitos!'); 
		f.telefone.focus(); 
		return false;
	}
	if (f.email.value == "") 
	{ 
		erro = true; 
		alert('O campo "E-Mail" está vazio!'); 
		f.email.focus(); 
		return false;
	}
	if (f.endereco.value == "") 
	{ 
		erro = true; 
		alert('O campo "Endereço" está vazio!'); 
		f.endereco.focus(); 
		return false;
	}
	if (f.cidade.value == "") 
	{ 
		erro = true; 
		alert('O campo "Cidade" está vazio!'); 
		f.cidade.focus(); 
		return false;
	}
	if (f.uf.value == "") 
	{ 
		erro = true; 
		alert('O campo "UF" está vazio!'); 
		f.uf.focus(); 
		return false;
	}
	if (f.cpf.value == "") 
	{ 
		erro = true; 
		alert('O campo "CPF" está vazio!'); 
		f.cpf.focus(); 
		return false;
	}
	if (!erro) 
	{ 
		f.submit(); 
	}
}

// Permite apenas números
function numbers(evt)
{
	var key_code = evt.keyCode  ? evt.keyCode  :
	evt.charCode ? evt.charCode :
	evt.which    ? evt.which    : void 0;
	if (key_code == 8  ||  key_code == 9  ||  key_code == 13  ||  key_code == 27  ||  key_code == 46)
	{
		return true;
	}
	else if ((key_code >= 35)  &&  (key_code <= 40))
	{
		return true
	}
	else if ((key_code >= 48)  &&  (key_code <= 57))
	{
		return true
	}
	return false;
}
// Fim permite apenas números

//Mascara para campos com moeda
function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) //onKeyPress
{
  var sep = 0;
  var key = '';
  var i = j = 0;
  var len = len2 = 0;
  var strCheck = '0123456789';
  var aux = aux2 = '';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13) return true;
  var t = new String(objTextBox.value);
  if (whichCode == 8)
  {
    objTextBox.value = t.substring(0, t.length-1);
  }
  key = String.fromCharCode(whichCode); // Valor para o código da Chave
  if (strCheck.indexOf(key) == -1) return false; // Chave inválida
  len = objTextBox.value.length;
  for(i = 0; i < len; i++)
  if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
  aux = '';
  for(; i < len; i++)
  if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
  aux += key;
  len = aux.length;
  if (len == 0) objTextBox.value = '';
  if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
  if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
  if (len > 2)
  {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--)
    {
      if (j == 3)
      {
        aux2 += SeparadorMilesimo;
        j = 0;
      }
      aux2 += aux.charAt(i);
      j++;
    }
    objTextBox.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    objTextBox.value += aux2.charAt(i);
    objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
  }
  return false;
}
//Fim mascara para campo com moeda

//Valida o CPF
function ValidarCPF(Objcpf,evt,id) //onBlur
{
  if(document.getElementById(id).value != '')
	{
		var cpf = document.getElementById(id).value;
		var filtro = /^\d{3}\d{3}\d{3}\d{2}$/i;
		if(!filtro.test(cpf))
		{
			window.alert("CPF inválido!");
			document.getElementById(id).value = '';
			document.getElementById(id).focus();
			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!");
			document.getElementById(id).value = '';
			document.getElementById(id).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)))
		{
			window.alert("CPF inválido!");
			document.getElementById(id).value = '';
			document.getElementById(id).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)))
		{
			window.alert("CPF inválido!");
			document.getElementById(id).value = '';
			document.getElementById(id).focus();
			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;
}
//Fim valida CPF

//Adicionar site aos favoritos
function add_bookmark() 
{ 
  var browsName = navigator.appName; 
  if (browsName == "Microsoft Internet Explorer") 
  { 
    window.external.AddFavorite('http://www.escoladeradio.com.br','Escola de Rádio' );
  } 
	else if (browsName == "Netscape") 
  { 
    window.alert ("Para adicionar a Escola de Rádio ao seus Favoritos tecle CTRL+D"); 
  }
}
//Fim adicionar site aos favoritos

//Alterar senha do aluno
function altPwd()
{
  var erro = false;
  var f = document.form;
  if ((!erro) && (f.senha.value.length < 6)) 
  { 
    erro = true; 
	  alert('O campo "Senha" não pode ser nulo ou menor que 6 digitos!'); 
	  f.senha.focus(); 
  }

  if ((!erro) && (f.confirma.value.length < 6)) 
  { 
    erro = true; 
	  alert('O campo "Confirmar senha" não pode ser nulo ou menor que 6 digitos!'); 
 	  f.confirma.focus(); 
  }

  if ((!erro) && (!(f.senha.value == f.confirma.value))) 
  { 
    erro = true; 
	  alert('Confirmação não coincide com a senha!'); 
	  f.confirma.value = ""; 
	  f.senha.value = ""; 
	  f.senha.focus(); 
  }

  if (!erro) 
  { 
    f.submit(); 
  }
}
//Fim alterar senha do aluno