/*
Author: Jim Michaels
Abstract: take date string in the format "June 17, 2009 12:34:56" (sorry, no 
	milliseconds unless you do GregorianToJulian(2009,6,17,12,34,56,123)) and 
	return a string showing the number of years, months, days, hours left to go, 
	or "deadline passed".
Creation Date: January 2009
Current Date: October 24, 2009
Version: 2.1

include the following code in your HTML head tag:
<script language="JavaScript" type="text/javascript" src="julian-gregorian.js">
//GPL3  license
</script>
<script language="JavaScript" type="text/javascript" src="getdeadline.js">
//GPL3  license
</script>

include the following code in your body tag where you want the results to show:
<script language="JavaScript" type="text/javascript">
document.write(GetDeadlineDatestring("June 17, 2009"));document.close();
</script>


This Javascript function library uses the julian-gregorian.js function library 
written by the same author, Jim Michaels.
Unfortunately in Javascript, there is no way to #include scripts like you can 
in C.


Copyright 2009 Jim Michaels

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.


*/


function GetDeadlineDatestring(datestring) {
    /*
    datestring is a date string in the form "June 12, 2009 12:34:56", a string 
		that is acceptable to Date();

    I am including this just for fun.
	The code isn't 100% accurate due to the number of days in a month, so I 
		assume 30.
    Use for things like the DTV transition date.
    results are approximate due to the code's inability to calculate real
        gregorian calendar dates properly.
    something that could be done is to convert the 2 dates into a julian
        day, find the difference (subtract), and then convert the
        resulting julian day into gregorian calendar format.  I have
        libraries in JavaScript for this.
        alas, that method doesn't work.

	I may be able to redo the function so it will be accurate by using the 
		following algorithm:
		gnow=new Date();
		jnow=GregorianToJulian(gnow);
		gbackthen=Date(datestring);
		jbackthen=GregorianToJulian(gbackthen);
		jdiff=jnow-jbackthen;
		if (jdiff >= 0) then
		    gdiff=JulianToGregorian(jdiff);
		endif
		
    */


	var s="";
	var today = new Date();
	var deadline = new Date(datestring);
    var bDeadlinePassed=false;


	 //totally bogus results - deadline passed.
    var jd_deadline=JulianFromDate(deadline);
    var jd_today=JulianFromDate(today);
    var jd_diff=jd_deadline-jd_today;
    if (jd_diff < 0) {
		bDeadlinePassed=true;
		jd_diff=0;
	}
    var ddate_diff=JulianToDate(jd_diff);
	var add=GregorianToJulian(0,1,1,0,0,0,0);//necessary to add to do date diffs
	var y=JulianToGregorianY(jd_diff+add); //must add epoch to get proper date diff.
	var m=JulianToGregorianMo(jd_diff+add)-1; //by default this is one-based. since we are doing a diff, this should be zero-based.
	var d=JulianToGregorianD(jd_diff+add)-1; //by default this is one-based. since we are doing a diff, this should be zero-based.
    var ms=JulianToGregorianMs(jd_diff+add);
    var ss=JulianToGregorianS(jd_diff+add);
    var mm=JulianToGregorianMi(jd_diff+add);
    var hh=JulianToGregorianH(jd_diff+add);

    if (bDeadlinePassed) {
	    s="deadline passed";
    } else {
	    s=""+y+" years, "+m+" months, "+d+" days, "+hh+" hours left";
    }
	return s;
}

function GetDeadlineGregorian(Y,M,D,hh,mm,ss,ms) {
    /*
    Y - year -4713..whenever+
	M - month 1..12
	D - day 1..31
	hh - hour 0..23
	mm - minute 0..59
	ss - second 0..59
	ms - millisecond 0..999

    I am including this just for fun.
	The code isn't 100% accurate due to the number of days in a month, so I 
		assume 30.
    Use for things like the DTV transition date.
    results are approximate due to the code's inability to calculate real
        gregorian calendar dates properly.
    something that could be done is to convert the 2 dates into a julian
        day, find the difference (subtract), and then convert the
        resulting julian day into gregorian calendar format.  I have
        libraries in JavaScript for this.
        alas, that method doesn't work.

	I may be able to redo the function so it will be accurate by using the 
		following algorithm:
		gnow=new Date();
		jnow=GregorianToJulian(gnow);
		gbackthen=Date(dt);
		jbackthen=GregorianToJulian(gbackthen);
		jdiff=jnow-jbackthen;
		if (jdiff >= 0) then
		    gdiff=JulianToGregorian(jdiff);
		endif
		
    */


	var s="";
	var today = new Date();
	
    var bDeadlinePassed=false;


	 //totally bogus results - deadline passed.
    var jd_deadline=JulianFromDate(deadline);
    var jd_today=GregorianToJulian(Y,M,D,hh,mm,ss,ms);
    var jd_diff=jd_deadline-jd_today;
    if (jd_diff < 0) {
		bDeadlinePassed=true;
		jd_diff=0;
	}
    var ddate_diff=JulianToDate(jd_diff);
	var add=GregorianToJulian(0,1,1,0,0,0,0);//necessary to add to do date diffs
	var y=JulianToGregorianY(jd_diff+add); //must add epoch to get proper date diff.
	var m=JulianToGregorianMo(jd_diff+add)-1; //by default this is one-based. since we are doing a diff, this should be zero-based.
	var d=JulianToGregorianD(jd_diff+add)-1; //by default this is one-based. since we are doing a diff, this should be zero-based.
    var ms=JulianToGregorianMs(jd_diff+add);
    var ss=JulianToGregorianS(jd_diff+add);
    var mm=JulianToGregorianMi(jd_diff+add);
    var hh=JulianToGregorianH(jd_diff+add);

    if (bDeadlinePassed) {
	    s="deadline passed";
    } else {
	    s=""+y+" years, "+m+" months, "+d+" days, "+hh+" hours left";
    }
	return s;
}
















//don't recommend you use getdeadline0, it is inaccurate.
/*
function getdeadline0(dt) {
//    dt is a date string in the form "June 12, 2009", a string that is acceptable 
//		to Date();
//
//    I am including this just for fun.
//	The code isn't 100% accurate due to the number of days in a month, so I 
//		assume 30.
//    Use for things like the DTV transition date.
//    results are approximate due to the code's inability to calculate real
//        gregorian calendar dates properly.
//    something that could be done is to convert the 2 dates into a julian
//        day, find the difference (subtract), and then convert the
//        resulting julian day into gregorian calendar format.  I have
//        libraries in JavaScript for this.
//        alas, that method doesn't work.
//
//	I may be able to redo the function so it will be accurate by using the 
//		following algorithm:
//		gnow=new Date();
//		jnow=GregorianToJulian(gnow);
//		gbackthen=Date(dt);
//		jbackthen=GregorianToJulian(gbackthen);
//		jdiff=jnow-jbackthen;
//		if (jdiff >= 0) then
//		    gdiff=JulianToGregorian(jdiff);
//		endif


	var s="";
	var today = new Date();
	var deadline = new Date(dt);
    var bDeadlinePassed=false;

	var timediff = deadline-today;
	if (timediff<0) {
	    timediff=0;
        bDeadlinePassed=true;
	}
	timediff /=1000; //1000 milliseconds in second
	timediff /=3600; //3600 seconds in hour
	timediff /=24; //24 hours in day
	timediff = Math.floor(timediff);
	var y=Math.floor(timediff / 365);
	var m=Math.floor(timediff / 30) % 365;
	var d=(timediff % 30) % 365;
    var t=deadline-today;
    var ms=t%1000;t=Math.floor(t/1000);
    var ss=t%60;t=Math.floor(t/60);
    var mm=t%60;t=Math.floor(t/60);
    var hh=t%24;t=Math.floor(t/24);
    if (bDeadlinePassed) {
	    s="(deadline passed)";
    } else {
	    s="("+y+" years, "+m+" months, "+d+" days, "+hh+" hours left)";
    }
	return s;
}
*/