var TTWin;
var msgWin;
var login_visible = false;

function WindowUnload() {
//  alert("window closing - " + event.type + " - " + window.location );
	return true;
}

function LaunchBoard() {
	KillPopups();
	document.forms[0].acode.value = "b";
	document.forms[0].submit();
	return false;
}

function POBox() {
	KillPopups();
	msgWin=window.open("","displayWindow","menubar=no,scrollbars=no,status=no,width=300,height=125");
	msgWin.document.write("<head><title>Mail Donation To<\/title><\/head>");
	msgWin.document.write("<font face=arial size=2><center><i>Mail your donation to:<\/i><b><br>Philip A. Smith<br>PO Box 210711<br>Buswick Station<br>Brooklyn, NY 11211-210711<\/b><p><i>Paypal of $20 or more to:<\/i><br><b>donations@realbook.us<\/b><\/p><\/center><\/font>");
//	return false;
}

function TopTen() {
	KillPopups();
	setTimeout('KillPopups()',30000);
	TTWin=window.open("","displayWindow","menubar=no,scrollbars=no,status=no,width=420,height=380");
	TTWin.document.write("<head>");
	TTWin.document.write("<style type='text\/css' media='screen,projection'>@import 'login.css';<\/style>");
	TTWin.document.write("<title>Top 40 Views<\/title>");
	TTWin.document.write("<\/head><body bgcolor='Gainsboro'>");
	TTWin.document.write("<font face=arial size=2><b>" + topten.innerHTML + "<\/b><\/font><\/body>");
}

function KillPopups() {
	if (msgWin != null) {
		if(false == msgWin.closed) 
		{
			msgWin.close();
		}
	}
	if (TTWin != null ) {
		if (false == TTWin.closed) {
			TTWin.close();
		}
	}
}

function SetIndex(iIndex) {
	KillPopups();
	document.forms[0].acode.value = "i";
	document.forms[0].SongID.value = "0";
	document.forms[0].index.value = iIndex;
	document.forms[0].submit();
	return false;
}

function MyRealbook() {
	KillPopups();
	document.forms[0].acode.value = "m";
	document.forms[0].submit();
	return false;
}

function MySetList()
{
	KillPopups();
	document.forms[0].acode.value = "q";
	document.forms[0].submit();
	return false;
}

function Register()
{
	KillPopups();
	document.forms[0].acode.value = "r";
	document.forms[0].submit();
	return false;
}

function CheckLogin()
{
    if (IsEmail() && IsPassword())
    {
        showspinner("loginprog");
    	document.forms[0].acode.value = "l";
    	document.forms[0].submit();
        return true;
    }
    else
        return false;
}

function CheckLost()
{
    if (IsEmail())
    {
        showspinner("loginprog");
    	document.forms[0].acode.value = "p";
    	document.forms[0].submit();
        return true;
    }
}

function IsEmail() {
    var strTemp = document.forms[0].email.value;

    if (strTemp.length < 1) {
      alert("Please enter your Email Address or User Name.");
      document.forms[0].email.focus();
      return false;
    }

    return true;
}

function IsPassword() {
    var strTemp = document.forms[0].password.value;

    if (strTemp.length < 1)
    {
        alert("Please enter your Password.");
        document.forms[0].password.focus();
        return false;
    }
    else
    {
        if (strTemp.length < 5)
        {
            alert("Password must be at least 5 characters");
            document.forms[0].password.focus();
            return false;
        }
    }

    return true;
}

function GetLogin()
{
    if (login_visible)
        HideDiv("login");
    else
    {
        ShowDiv("login");
        document.forms[0].email.focus();
    }
}

function BadLogin(usr, pwd)
{
    if (login_visible)
        HideDiv("login");
    else
    {
        alert("User Name/Email Address and/or Password incorrect.");
        ShowDiv("login");
        document.forms[0].email.value = usr;
        document.forms[0].password.value = pwd;
        document.forms[0].email.focus();
    }
}

function UserStatus()
{
    alert("To gain access to the charts you must follow the instructions in the confirmation email.");
}

function EmailSent(e_addr)
{
    alert("Password has been e-mailed to: " + e_addr);
}

function LogOut()
{
    document.forms[0].acode.value = "o";
    document.forms[0].submit();
}

function ShowDiv(divid)
{
	if (document.layers)
		document.layers[divid].visibility="show";
	else
		document.getElementById(divid).style.visibility="visible";
	login_visible = true;
		
}

function HideDiv(divid)
{
	if (document.layers)
		document.layers[divid].visibility="hide";
	else
		document.getElementById(divid).style.visibility="hidden";
		
	login_visible = false;
}

function search()
{
    var strTemp = document.forms[0].searchfor.value;

    KillPopups();
    if (strTemp.length < 1) {
      alert("Please enter a song name to search for.");
      document.forms[0].searchfor.focus();
      return false;
    }
    else
    {
        if (CheckSearch(strTemp))
        {
            showspinner("prompt");
            //ShowDiv("spinner");
	        document.forms[0].acode.value = "s";
	        document.forms[0].submit();
	        return false;
        }
        else
        {
          alert("Please enter an ACTUAL song name to search for.");
          document.forms[0].searchfor.focus();
          return false;
        }
	}
}

function clearitems()
{
	KillPopups();
    if (login_visible)
        HideDiv("login");
}

function CheckSearch(strsearch)
{
    if (strsearch.length <= 25)
    {
        if (/^[a-zA-Z0-9\.\-\_]+/.test(strsearch))
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
    else
    {
        return(false);
    }
}

function showspinner(divid)
{
    document.getElementById(divid).innerHTML = document.getElementById("spinner").innerHTML;
}