//===========================================================================
// JavaScript for almanac
// this version copyright (c) 2006 Simon kershaw Crucix Ltd
// 25 July 2006
//

// some of this is based on code examples in _Ajax Hacks_ by Bruce Perry.
// Copyright (c) 2006 O'Reilly Media Inc., 0-596-10169-4.
//
// Also uses 'prototype.js' JavaScript and Ajax library
// Copyright (c) 2005 Sam Stephenson

//===========================================================================


//===========================================================================

window.onload = function()
{
	getData( "/month.html?date=today", "mainblock" );
	getData( "/month.html?date=today&mini=1", "minicalblock" );
	getData( "/login_panel.html?", "loginpanel" );
	getData( "/principal_feasts.html?year=today", "pfeastsblock" );
}

//===========================================================================

function startUp( mainThing )
{
	getData( mainThing, "mainblock" );
	getData( "/month.html?date=today&mini=1", "minicalblock" );
	getData( "/login_panel.html?", "loginpanel" );
	getData( "/principal_feasts.html?year=today", "pfeastsblock" );
}

//===========================================================================

function getData( go_url, entryID )
{
    var now = new Date();
    var ts = "&ts=" + now.getTime();

    var ref_form = $( "optrefreshform" );
    var dest = '';
    if ( go_url == "/register.html?register=1" && ref_form )
    {
	if ( ref_form.destination.value )
	{
	    dest = "&dest=" + ref_form.destination.value;
	}
    }

    var xmlHttp =
	new Ajax.Request(
			 go_url + dest + ts,
			 {
			     method: "get",
			     onComplete: function() { responseHandler( xmlHttp, "res" + entryID ); }
			 }
			);
    return false;
}

//===========================================================================

function getHiddenData( go_url, entryID, entryLink, htmlObj )
{
    var xmlHttp =
	new Ajax.Request(
			 go_url,
			 {
			     method: "get",
				 onComplete: function() 
				 { responseHandlerHidden( xmlHttp, entryID, entryLink, htmlObj ); }
			 }
			);
    return false;
}

//===========================================================================

function getOverlayData( go_url, caption )
{
    var xmlHttp =
	new Ajax.Request(
			 go_url,
			 {
			     method: "get",
				 onComplete: function() 
				 { responseHandlerOverlay( xmlHttp, caption ); }
			 }
			);
    return false;
}

//===========================================================================

function responseHandler( xmlHttp, entryID )
{
    if ( xmlHttp.responseIsFailure() )
    {
	alert( "A problem occurred communicating with the web server (for " + entryID + "). Please try again." );
    }
    else
    {
	$( entryID ).innerHTML = xmlHttp.transport.responseText;
	setCurrentYMD();
    }
}

//===========================================================================

function responseHandlerHidden( xmlHttp, entryID, entryLink, htmlObj )
{
    if ( xmlHttp.responseIsFailure() )
    {
	alert( "A problem occurred communicating with the web server (for res" + entryID + "). Please try again." );
    }
    else
    {
	$( "res" + entryID ).innerHTML = xmlHttp.transport.responseText;
	showMore( entryID, entryLink, htmlObj );
    }
}

//===========================================================================

function responseHandlerOverlay( xmlHttp, caption )
{
    if ( xmlHttp.responseIsFailure() )
    {
	alert( "A problem occurred communicating with the web server (for res" + entryID + "). Please try again." );
    }
    else
    {
	var text = xmlHttp.transport.responseText;

	overlib( text, FGCOLOR, '#FFFEFE', BGCOLOR, '#880000', TEXTSIZE, '+0', WIDTH, '500', DELAY, "0", CAPTION, caption, CAPTIONSIZE, '+0', RELX, '120', RELY, '150' );
    }
}

//===========================================================================

function showMore( entryID, entryLink, htmlObj )
{
    showID = ( 'show' + ( entryID ) );
    hideID = ( 'hide' + ( entryID ) );
    if ( $( showID ).style.display )
    {
	if ( entryLink != 0 )
	{
                $( showID ).style.display = "block";
                $( hideID ).style.display = "none";
                htmlObj.blur();
	}
	else
	{ 
	    $( showID ).style.display = "none";
	    $( hideID ).style.display = "block";
        }
    }
    else
    {
	location.href = entryLink;
    }
    return false;
}

//===========================================================================

function postLogin()
{
    var htmlObj = $( 'loginform' );

    if ( htmlObj.username.value == "" )
    {
	alert( "You must specify your user name." );
	return( false );
    }

    if ( htmlObj.password.value == "" )
    {
	alert( "You must specify your password." );
	return( false );
    }

    var go_url = "/login_panel.html?signin=1";
    var xmlHttp = 
	new Ajax.Request( 
			 go_url,
			 {
			     method: "post",
				 parameters: Form.serialize( htmlObj ),
				 onComplete: function()
				 { responseHandlerLogin( xmlHttp ); }
			 }
			);
    htmlObj.submit();
    return false;
}

//===========================================================================

function postLogout()
{
    var go_url = "/login_panel.html?signout=1";
    var xmlHttp = 
	new Ajax.Request( 
			 go_url,
			 {
			     method: "post",
				 onComplete: function()
				 { responseHandlerLogin( xmlHttp ); }
			 }
			);

    var htmlObj = $( 'logoutform' );
    htmlObj.submit();
    return( false );
}

//===========================================================================

function responseHandlerLogin( xmlHttp )
{
    if ( xmlHttp.responseIsFailure() )
    {
	alert( "A problem occurred communicating with the web server (for resloginpanel). Please try again." );
    }
    else
    {
	document.getElementById( "resloginpanel" ).innerHTML = xmlHttp.transport.responseText;
	var err = $( "loginalert" );
	if ( err )
	{
	    alert( "Login failed\n" + err.innerHTML );
	}
	else
	{
	    var entryID = "optrefresh";
	    var ref_form = $( entryID + "form" );
	    var dest = ref_form.destination.value;
	    refresh( dest, entryID, "form" );
	}
    }
}


//===========================================================================

function refresh( go_url, entryID, suffix)
{
    //var htmlObj = $( entryID + suffix );
    getData( go_url, entryID );
    return false;
}


//===========================================================================

function postRegister()
{
    var htmlObj = $( 'regform' );

    if ( htmlObj.name.value == "" )
    {
	alert( "You must specify your name." );
	return false;
    }

    if ( htmlObj.email.value == "" )
    {
	alert( "You must specify your email address." );
	return false;
    }


    if ( htmlObj.pw1.value == "" )
    {
	alert( "You must specify a password." );
	return false;
    }

    if ( htmlObj.pw2.value == "" )
    {
	alert( "You must specify the password again." );
	return false;
    }

    if ( htmlObj.pw1.value != htmlObj.pw2.value )
    {
	alert( "You have entered the password differently. The password values must be identical." );
	return false;
    }

    // don't check the short id -- we don't insist on one

    // if we have got this far then submit the form

    var go_url = "/register.html?register=2";
    var xmlHttp = 
	new Ajax.Request( 
			 go_url,
			 {
			     method: "post",
				 parameters: Form.serialize( htmlObj ),
				 onComplete: function()
				 { responseHandler( xmlHttp, "resmainblock" ); }
			 }
			);
    htmlObj.submit();
    return false;
}

//===========================================================================

function currentTab()
{
    var ul = $( "almanactabs" );
    var ul_list = ul.getElementsByTagName( "li" );
    for ( var ix = 0; ix < ul_list.length; ix++ )
    {
	if ( ul_list[ix].className == "selected" )
	{
	    return ul_list[ix].getAttribute( "id" );
	}
    }
    return "";
}

//===========================================================================

function refreshTab( url_args, entryID )
{
    // refresh the current tab using the specified url bits
    var current_tab = currentTab();
    //    return selectTab( current_tab, go_url, url_args, entryID );

    // FIXME this shouldn't be coded here
    if ( current_tab == "atabmonth" )
    {
	tab_url = "/month.html?";
    }
    else if ( current_tab == "atabweek" )
    {
	tab_url = "/week.html?";
    }
    else if ( current_tab == "atabday" )
    {
	tab_url = "/day.html?";
    }
    else
    {
	// can't refresh a non-date tab
	alert( "Sorry -- you must select a date-based tab first" );
	return false;
    }

    //    return selectTab( current_tab, tab_url, url_args, entryID );
    setSelectedTab( current_tab );

    setLoading( "res" + entryID );
    return getData( tab_url + url_args, entryID );
}

//===========================================================================

function setSelectedTab( tabID )
{
	// unselect all tabs
	var ul = $( "almanactabs" );
	var ul_list = ul.getElementsByTagName( "li" );
	for ( var ix = 0; ix < ul_list.length; ix++ )
	{
		ul_list[ix].className = "";
	}
	// now select the one we want
	var sel_tab = $( tabID );
	sel_tab.className = "selected";
}

//===========================================================================

function selectTab( tabID, go_url, url_args, entryID )
{
    setSelectedTab( tabID );

    setLoading( "res" + entryID );

    var new_args = getCurrentYMD();
    if ( new_args )
    {
	return getData( go_url + url_args + new_args, entryID );
    }
    else
    {
	return getData( go_url, entryID );
    }
}

//===========================================================================

function getCurrentYMD()
{
	var year_el, year, month_el, month, dom_el, dom, res;
	year_el = $( "currentyear" );
	if ( year_el )
	{
		year = year_el.getAttribute( "rel" );
	}
	month_el = $( "currentmonth" );
	if ( month_el )
	{
		month = month_el.getAttribute( "rel" );
	}
	dom_el = $( "currentdom" );
	if ( dom_el )
	{
		dom = dom_el.getAttribute( "rel" );
	}
	if ( year_el && month_el && dom_el )
	{
	    if ( dom == "31" && ( month == "4" || month == "6" || month == "9" || month == "11" ) )
	    {
		dom = "30";
	    }
	    if ( month == "2" && ( dom == "29" || dom == "30" || dom == "31" ) )
	    {
		// fixme -- this will reject 29 Feb even in a leap year
		dom = "28";
	    }
	    res = year + "-" + month + "-" + dom;
	}
	else
	{
		res = "";
	}
	return res;
}

//===========================================================================

function setCurrentYMD()
{
    var new_year_el, new_year, new_month_el, new_month, new_dom_el, new_dom;
    new_year_el = $( "newyear" );
    if ( new_year_el )
    {
	new_year = new_year_el.getAttribute( "rel" );
    }

    new_month_el = $( "newmonth" );
    if ( new_month_el )
    {
	new_month = new_month_el.getAttribute( "rel" );
    }

    new_dom_el = $( "newdom" );
    if ( new_dom_el )
    {
	new_dom = new_dom_el.getAttribute( "rel" );
    }
    updateCurrentYMD( new_year, new_month, new_dom );
}

//===========================================================================

function updateCurrentYMD( new_year, new_month, new_dom )
{
    var year_el, month_el, dom_el;
    year_el = $( "currentyear" );
    if ( year_el && new_year )
    {
	year_el.setAttribute( "rel", new_year );
    }

    month_el = $( "currentmonth" );
    if ( month_el && new_month )
    {
	month_el.setAttribute( "rel", new_month );
    }

    dom_el = $( "currentdom" );
    if ( dom_el && new_dom )
    {
	dom_el.setAttribute( "rel", new_dom );
    }
}

//===========================================================================

function setLoading( entryID )
{
    //$( entryID ).innerHTML = "<p><img src='/etc/loading.gif'><em>loading</em></p>";
}

//===========================================================================


//===========================================================================
// end of file
//===========================================================================
