
function funCountupObj(objName, spanName, baseYear, baseMonth, baseDate, baseHour, baseMinute, baseSecond, Occasion, Message)
{
	this.objName = objName;
	this.spanName = spanName;
	this.Occasion = Occasion;
	this.Message = Message;
	this.baseDate = new Date(baseYear, baseMonth, baseDate, baseHour, baseMinute, baseSecond);
	// ensure year is taken correctly, e.g. 1 A.D. above interprets as 1901
	this.baseDate.setFullYear(baseYear);
	this.baseTime = this.baseDate.getTime();
	
	// output nothing if the browser can't handle the year
	if(this.baseDate.getFullYear() == baseYear)
	{
		document.write('<span id="'+spanName+'">'+funGetCountupStr(this)+'</span>');
	
		if (document.getElementById)
			onLoadStr += "funUpdateCountup("+objName+");";
	}

}

function getNumDaysCu(selMonth, selYear)
{
	var numOfDays = 31;
	if (selMonth == 3 || selMonth == 5 || selMonth == 8 || selMonth == 10)
		numOfDays = 30;
	else if ( selMonth == 1 )
	{
		numOfDays = 28;
		
		// check for leap year, year must be divisible by 4 and not be the last year
		// of a century (1900), or be divisible by 400 (2000)
		if ( ((selYear % 100 != 0) && (selYear % 4 == 0)) || (selYear % 400 == 0) )
			numOfDays = 29;
	}
	return numOfDays;
}

function funGetCountupStr(obj)
{
	with(obj)
	{
		var today = new Date();
		var curTime = today.getTime();
		var diff = curTime - baseTime;
		// keep original occasion string in tact
		var outStr = Occasion;
		
		if (diff <= 0)
			return (Message);
		
		var sec = 1000;
		var min = (sec * 60);
		var hour = (min * 60);
		var day = (hour * 24);
		var week = (day * 7);
		
		if (outStr.indexOf('%y') >= 0 || outStr.indexOf('%mo') >= 0)
		{
			var tmpDate = new Date(baseTime);
			var curYear = today.getFullYear();
			var curMonth = today.getMonth();
			var baseYear = tmpDate.getFullYear();
			var baseMonth = tmpDate.getMonth();
			var baseCalDate = tmpDate.getDate();
			var yearsToGo = 0;
			var bSetYear = false;
			var monthsToGo = 0;
			
			// get the number of full years and move the date cursor forward
			tmpDate.setFullYear(curYear);
			yearsToGo = curYear - baseYear;
			if (tmpDate.getTime() > curTime)
			{
				// not a full year, back up one
				tmpDate.setFullYear(curYear - 1);
				yearsToGo = curYear - baseYear - 1;
			}
				
			baseYear = tmpDate.getFullYear();
			
			if (outStr.indexOf('%y') >= 0)
			{
				bSetYear = true;
				diff = (curTime - tmpDate.getTime());
				outStr = outStr.replace(/%y/g, yearsToGo);
			}
			
			if (outStr.indexOf('%mo') >= 0)
			{
				// if year is last year get the number of months to the end of the year
				// and move forward to the current year
				if (baseYear < curYear)
				{
					monthsToGo = 12 - baseMonth;
					tmpDate.setFullYear(curYear);
					tmpDate.setMonth(0);
				}
				
				baseMonth = tmpDate.getMonth();
				var safetyValve = 0;
	
				while (true && safetyValve < 12)
				{
					// used to stop infinite loop if there is a problem
					safetyValve++;
					
					// get the number of days in each month
					var nextMonth = tmpDate.getMonth() + 1;
					var tmpDays = getNumDaysCu(nextMonth, curYear);
					
					// the target date is larger than the number of days next month
					if (baseCalDate > tmpDays)
					{
						// if next month is the target month, just exit
						if (curMonth == nextMonth)
							break;
							
						// set date to lesser date then change month
						tmpDate.setDate(tmpDays);
						tmpDate.setMonth(nextMonth);
					}
					else
					{
						// set month first then reset date
						tmpDate.setMonth(nextMonth);
						tmpDate.setDate(baseCalDate);
					}
					
					// if we haven't passed the target yet, increment the month count
					if (tmpDate.getTime() <= curTime)
						monthsToGo++;
					else
					{
						// passed the date, rollback to last month and exit
						var prevMonth = tmpDate.getMonth() - 1;
						tmpDays = getNumDaysCu(prevMonth, curYear);
						if (baseCalDate > tmpDays)
						{
							tmpDate.setDate(tmpDays);
							tmpDate.setMonth(prevMonth);
						}
						else
						{
							tmpDate.setMonth(prevMonth);
							tmpDate.setDate(baseCalDate);
						}
						break;
					}
					
				}
			
				diff = (curTime - tmpDate.getTime());
				
				if (!bSetYear)
					monthsToGo += yearsToGo * 12;
				outStr = outStr.replace(/%mo/g, monthsToGo);
			}
		}
		
		if (outStr.indexOf('%w') >= 0)
		{
			var weeksToGo = Math.floor(diff / week);
			diff = diff - (weeksToGo * week);
			outStr = outStr.replace(/%w/g, weeksToGo);
		}
		
		if (outStr.indexOf('%d') >= 0)
		{
			var daysToGo = Math.floor(diff / day);
			diff = diff - (daysToGo * day);
			outStr = outStr.replace(/%d/g, daysToGo);
		}
		
		if (outStr.indexOf('%h') >= 0)
		{
			var hoursToGo = Math.floor(diff / hour);
			diff = diff - (hoursToGo * hour);
			// pad vals less than 10 with a '0' to give the span consistent size
			outStr = outStr.replace(/%h/g, (hoursToGo < 10 ? "0" : "") + hoursToGo);
		}
		
		if (outStr.indexOf('%m') >= 0)
		{
			var minsToGo = Math.floor(diff / min);
			diff = diff - (minsToGo * min);
			// pad vals less than 10 with a '0' to give the span consistent size
			outStr = outStr.replace(/%m/g, (minsToGo < 10 ? "0" : "") + minsToGo);
		}
		
		if (outStr.indexOf('%s') >= 0)
		{
			var secsToGo = Math.floor(diff / sec);
			diff = diff - (secsToGo * sec);
			// pad vals less than 10 with a '0' to give the span consistent size
			outStr = outStr.replace(/%s/g, (secsToGo < 10 ? "0" : "") + secsToGo);
		}
		
		return (outStr);
	}
}


function funUpdateCountup(obj) 
{
	with(obj)
	{
		var useSpan = document.getElementById(spanName);
		if(useSpan)
		{
			if (useSpan.innerHTML)
			{
				useSpan.innerHTML = funGetCountupStr(obj);
				setTimeout("funUpdateCountup("+objName+")",1000);
			}
		}
	}

}

