// $Id: usenet.js,v 1.7 2004/12/07 06:16:07 richw Exp $

long_months = new Array (
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
);

short_months = new Array (
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
);

function LookupURL(address, syear, smonth, eyear, emonth)
{
    r = 'http://groups.google.com/groups'
      + '?start=0'
      + '&amp;scoring=d'
      + '&amp;safe=images'
      + '&amp;num=100'
      + '&amp;lr='
      + '&amp;hl=en'
      + '&amp;filter=0'
      + '&amp;q=author:';
    if (address.match ("@") || address.match (" "))
	 r += address.replace (/ /g, ".");
    else r += "Rich.Wales"
	   +  "&amp;as_ugroup="
	   +  address;

    if (syear > 0) {
	if (emonth == 4 || emonth == 6 || emonth == 9 || emonth == 11)
	    eday = 30;
	else if (emonth == 2) {
	    if (eyear % 4 == 0)
		 eday = 29;
	    else eday = 28;
	}
	else eday = 31;
	s = '&amp;as_drrb=b'
	  + '&amp;as_miny=' + syear
	  + '&amp;as_minm=' + smonth
	  + '&amp;as_mind=1'
	  + '&amp;as_maxy=' + eyear
	  + '&amp;as_maxm=' + emonth
	  + '&amp;as_maxd=' + eday;
    }
    else {
	s = '&amp;as_drrb=quick';
    }

    return r + s;
}

function LookupYear(address, year)
{
    r = '<a href="'
      + LookupURL(address, year, 1, year, 12)
      + '">' + year + '</a>';
    return r;
}

function LookupMonth(address, year, month)
{
    r = '<a href="'
      + LookupURL(address, year, month, year, month)
      + '">' + short_months[month-1] + '</a>';
    return r;
}

function LookupMonthRange(address, year, smonth, emonth)
{
    r = '    ' + LookupYear(address, year) + ':\n';
    if (smonth > 1) {
	r += '    <s>';
	for (month = 1; month < smonth; month++) {
	    r += short_months[month-1];
	    if (month < 12)       r += ',';
	    if (month < smonth-1) r += ' ';
	}
	r += '</s>\n';
    }
    for (month = smonth; month <= emonth; month++) {
	r += '    ';
	r += LookupMonth(address, year, month);
	if (month < 12) r += ',';
	r += '\n';
    }
    if (emonth < 12) {
	r += '    <s>';
	for (month = emonth+1; month <= 12; month++) {
	    r += short_months[month-1];
	    if (month < 12) r += ', ';
	}
	r += '</s>';
    }
    return r;
}

function LookupFull(address, syear, smonth, eyear, emonth)
{
    // header
    r = '<p>\n\n<h3><a href="'
      + LookupURL(address, 0, 0, 0, 0)
      + '">' + address
      + '</a> <small>('
      + long_months[smonth-1] + ' ' + syear + ' - ';
    if (eyear == 0) {
	r += 'present';
	now    = new Date();
	eyear  = now.getFullYear();
	emonth = now.getMonth() + 1;
    }
    else {
	r += long_months[emonth-1] + ' ' + eyear;
    }
    r += ')</small></h3>\n<ul>\n';

    // all within a single year?
    if (syear == eyear) {
	r += '\n<li>'
	   + LookupMonthRange(address, eyear, smonth, emonth)
	   + '\n</li>\n';
    }

    // more than one year?
    else {
	// latest (possibly incomplete) year
	r += '\n<li>'
	   + LookupMonthRange(address, eyear, 1, emonth)
	   + '\n';
	if (eyear - syear >= 5 && eyear % 5 == 0)
	    r += '<br>&nbsp;<br>\n';
	r += '</li>\n';

	// middle (complete) years
	for (year = eyear - 1; year > syear; year--) {
	    r += '\n<li>'
	       + LookupMonthRange(address, year, 1, 12)
	       + '\n';
	    if (eyear - syear >= 5 && year % 5 == 0)
		r += '<br>&nbsp;<br>\n';
	    r += '</li>\n';
	}

	// earliest (possibly incomplete) year
	r += '\n<li>'
	   + LookupMonthRange(address, syear, smonth, 12)
	   + '\n</li>\n';
    }

    // footer
    r += '\n</ul>\n\n</p>\n';

    return r;
}

function LookupFullByYears(address, syear, eyear, detail)
{
    // header
    if (detail)
	 r = '<p>\n\n<h3>';
    else r = '';

    r += '<a href="'
       + LookupURL(address, 0, 0, 0, 0)
       + '">' + address
       + '</a> <small>(' + syear;
    if (eyear == 0) {
	r += ' - present';
	now    = new Date();
	eyear  = now.getFullYear();
    }
    else if (eyear != syear) {
	r += ' - ' + eyear;
    }
    r += ')</small>';
    if (detail) r += '</h3>';
    r += '\n';

    // years
    if (detail) {
	r += '<ul>\n<li>\n';
	for (year = syear; year <= eyear; year++) {
	    r += LookupYear(address, year);
	    if (year < eyear) r += ',';
	    r += '\n';
	}
	r += '\n</li>\n</ul>\n';
    }

    // footer
    r += '\n</p>\n';

    return r;
}

