//
// impdate.js - v1.0 JavaScript object for Gregorian <-> Imperial date
//              format translation.
// Copyright 2000 by Michael Schwitzgebel
//
function setLeapYear( testYear )
	{
    if ( testYear % 4 == 0 && !(testYear % 100 == 0 && testYear % 400 != 0) )
		{
        this.yearDays = 366;
        this.monthDays[1] = 29;
        }
	else
		{
        this.yearDays = 365;
        this.monthDays[1] = 28;
        }

	}

function setFromCurrentDate()
    {
    var date = new Date();

    // assign variables
    this.impYear = this.gregYear = date.getFullYear();
    this.impSegment = this.gregDay = date.getDate();
	--this.impSegment;
    this.gregMonth = date.getMonth();
    this.impMil    = Math.floor(this.gregYear / 1000);

    this.gregHour	= date.getHours();
    this.gregMinute	= date.getMinutes();

	this.setLeapYear( this.gregYear );

	this.cvtGregToImp();
    }

function setGregDate( dateString )
	{
	var i;
	var nameMonth;
	var posYear;
	var posMonth;

	if ( (posMonth = dateString.indexOf(' ',0)) == -1
		 || (posYear = dateString.indexOf(' ',posMonth+1)) == -1 )
		{
		if ( (posMonth = dateString.indexOf('-',0)) == -1
			 || (posYear = dateString.indexOf('-',posMonth+1)) == -1 )
			{
			alert("Error: Invalid Gregorian date");
			return false;
			}
		}

	nameMonth = dateString.substring(posMonth+1,posYear);
	for ( i=0; nameMonth != this.monthNames[i] && i<36; i++ );
	if ( i == 36 )
		{
		var temp = "There's no month of '" + nameMonth 
			+ "' in the Gregorian calendar, squig-brain."; 
		alert(temp);
		return false;
		}
	this.gregMonth	= (i % 12);
	this.gregDay	= dateString.substring(0,posMonth);
	this.gregYear	= dateString.substring(posYear+1);

	this.setLeapYear(this.gregYear);
	if ( eval(this.gregDay) > this.monthDays[this.gregMonth] )
		{
		var temp = "Silly grot... " + this.monthNames[this.gregMonth] +
			" has only " + this.monthDays[this.gregMonth] + " days!";
		alert(temp);
		return false;
		}
		

	this.gregHour	= 12;
	this.gregMinute	= 0;

	this.cvtGregToImp();
			
	return true;
	}

function setImpDate( dateString )
	{
	var pos;
	
	if ( (pos = dateString.indexOf("/",0)) == -1 )
		{
		if ( (pos = dateString.indexOf(".",0)) == -1 )
			{
			alert("Work with me, here.  Try a VALID Imperial date.");
			return false;
			}
		}
	if ( dateString.substring(pos+1,pos+2) == 'M' || dateString.substring(pos+1,pos+2) == 'm' )
		this.impMil = dateString.substring(pos+2);
	else
		this.impMil = dateString.substring(pos+1);

	dateString = dateString.substring(0,pos);
	if ( dateString.length < 7 )
		{
		alert("Work with me, here.  Try a VALID Imperial date.");
		return false;
		}

	this.impCheck	= dateString.substring(0,1);
	this.impSegment	= dateString.substring(1,4);
	this.impYear	= dateString.substring(4,7);

	this.cvtImpToGreg();

	return true;
	}

function cvtGregToImp()
    {
    // assign variables
    this.impYear	= this.gregYear;
    this.impSegment = this.gregDay - 1;
    this.impMil		= Math.floor(this.gregYear / 1000);

    if ( (this.gregYear % 1000) != 0 )
      this.impMil++;
    this.impYear -= (1000 * (this.impMil-1));
    if ( this.impYear.toString().length > 3 )
        this.impYear = this.impYear.toString().substr(this.impYear.toString().length-3,3);

    // Convert Day
    for ( var i=0; i < this.gregMonth && i<12; i++ )
      {
      this.impSegment += this.monthDays[i];
      }

    this.impSegment += ((this.gregHour / 24) + (this.gregMinute / 1440));
    this.impSegment = Math.floor( (this.impSegment * 1000)/(this.yearDays) ) + 1001;
    this.impSegment = this.impSegment.toString().substr(1);

    this.impCheck   = "1";
    }

function cvtImpToGreg()
	{
	this.gregYear = ((this.impMil-1) * 1000) + eval(this.impYear);
	if ( this.impYear == 0 )
		this.gregYear += 1000;

	this.setLeapYear(this.gregYear);

	this.gregDay = Math.floor( ((this.impSegment-1) * this.yearDays * .001) + 1 );
	for (this.gregMonth=0; (this.gregMonth < 12) && (this.gregDay > this.monthDays[this.gregMonth]); this.gregMonth++)
		{
		this.gregDay -= this.monthDays[this.gregMonth];
		}
	}

function getGregDate()
    {
    return this.gregDay + "-"
           + this.monthNames[this.gregMonth] + "-"
           + this.gregYear;
    }

function getImpDate()
    {
    return this.impCheck
           + this.impSegment
           + this.nowts.substr(0,3-this.impYear.toString().length) + this.impYear
           + ".M" 
		   + this.nowts.substr(0,2-this.impMil.toString().length) + this.impMil;
    }

function impDateObj( dateString )
    {
	// Set up impDateObj methods
	this.cvtGregToImp	= cvtGregToImp;
	this.cvtImpToGreg	= cvtImpToGreg;
    this.setFromCurrentDate = setFromCurrentDate;
    this.setImpDate		= setImpDate;
    this.setGregDate	= setGregDate;
    this.getImpDate		= getImpDate;
    this.getGregDate	= getGregDate;
	this.setLeapYear	= setLeapYear;

	// Define translation arrays
    this.monthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    this.monthNames = new Array("Jan", "Feb", "Mar", "Apr",
                                "May", "Jun", "Jul", "Aug",
                                "Sep", "Oct", "Nov", "Dec",
								"january", "february", "march", "april",
                                "may", "june", "july", "august",
                                "september", "october", "november", "december",								"jan", "feb", "mar", "apr",
								"may", "jun", "jul", "aug",
								"sep", "oct", "nov", "dec");
    this.yearDays = 365;
    this.nowts = "000000";

    if ( dateString == "" )
        {
        this.inputType = "current";
        this.setFromCurrentDate();
        }
    else if ( dateString.indexOf("/",0) != -1
              || dateString.indexOf(".",0) != -1 )
        {
        this.inputType = "Imperial";
		this.setImpDate( dateString );
        }
    else
        {
        this.inputType = "Gregorian";
        this.setGregDate( dateString );
        }

    }

newImpDateObj = new impDateObj("");





