/*
 * Date libarary written by David McOrist v0.1
 */
function daysInMonth(inYear,inMonth) {
    var monthLengthArr = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if ((inYear % 100 == 0) && (inYear % 400 == 0)) {
		monthLengthArr[1] = 29;
    } else	{
		if (inYear % 4 == 0) {
			monthLengthArr[1] = 29;
        }
	}
	return monthLengthArr[inMonth-1];
}
function daysInPrevMonth(inYear,inMonth) {
    var monthLengthArr = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if ((inYear % 100 == 0) && (inYear % 400 == 0)) {
		monthLengthArr[1] = 29;
    } else	{
		if (inYear % 4 == 0) {
			monthLengthArr[1] = 29;
        }
	}
	return monthLengthArr[(inMonth+10)%12];
}
function zeroPadDate(in_d) {
	wk_d=in_d.split('-');
	if (wk_d[0].length==2) {wk_d[0]='20'+wk_d[0]};
	if (wk_d[1].length==1) {wk_d[1]='0'+wk_d[1]};
	if (wk_d[2].length==1) {wk_d[2]='0'+wk_d[2]};
	return wk_d.join('-');
}
function YMDWDCount(inDate1,inDate2) {
	var order='';var outDays=0; var outMonths=0; var outYears=0;var fromDate='';var toDate='';
	inDate1=zeroPadDate(inDate1);inDate2=zeroPadDate(inDate2);
	if (inDate1<inDate2) {fromDate=inDate1.split('-');var toDate=inDate2.split('-');dateOrder="past";}
	else {fromDate=inDate2.split('-');var toDate=inDate1.split('-');dateOrder="future";}
	var fromDateYear=parseInt(fromDate[0],10);var fromDateMonth=parseInt(fromDate[1],10);var fromDateDay=parseInt(fromDate[2],10);
	var toDateYear=parseInt(toDate[0],10);var toDateMonth=parseInt(toDate[1],10);var toDateDay=parseInt(toDate[2],10);
	var sCountDate = new Date(fromDateYear,fromDateMonth-1,fromDateDay);
	var eCountDate = new Date(toDateYear,toDateMonth-1,toDateDay);
	var one_day = 1000*60*60*24;
	var daysApart = Math.abs(Math.floor((eCountDate.getTime()-sCountDate.getTime())/one_day));
	var weeksApart = Math.floor(daysApart/7);
	var weeksRemainderDays = daysApart%7; 


	var totalMonths=(toDateYear*12+toDateMonth)-(fromDateYear*12+fromDateMonth);
var x=totalMonths;
	if (fromDateDay<=toDateDay) {
		// if the 'from' day of the month is less than the 'to' day of the month - simple. Sub one from the other
		outDays=toDateDay-fromDateDay;
	}
	else {
		if (fromDateDay>daysInPrevMonth(toDateYear,toDateMonth)) {
			// previous month has less days that the 'from' day of the month. So use the 'to' day of the month as the number of days.
			totalMonths--; 
			outDays=(toDateDay)+0; 
		} else { 
			// so number of days is the remaining days of last month, plus the 'to' day of the month
			outDays=(daysInPrevMonth(toDateYear,toDateMonth)-fromDateDay+toDateDay);
			if (outDays>=daysInMonth(toDateYear,toDateMonth) && 
				toDateDay==daysInMonth(toDateYear,toDateMonth) &&
				dateOrder=="past")
			{
				// but, if this is the last day of the month and the 'from' day of the month is greater, call round the days down to zero and call this a whole number of months and zero days
				outDays=0; 
			} else {
				//otherwise we have less than a whole month left over so subtract one from total months.
				totalMonths--; 
			}
		}
	};
	outMonths=totalMonths%12;
	outYears=Math.floor(totalMonths/12);

	var outDisplay='';var wkComma='';
	if (outDays==0 && outMonths==0 && outYears==0) {outDisplay='zero days';}
	else if (outDays==0 || weeksRemainderDays!=0 || weeksApart>52) {
		if (outYears==1) {outDisplay+=outYears+' year';wkComma=', ';} else if (outYears!=0) {outDisplay+=outYears+' years';wkComma=', ';};
		if (outMonths==1) {outDisplay+=wkComma+outMonths+" month";wkComma=", ";} else if (outMonths!=0) {outDisplay+=wkComma+outMonths+" months";wkComma=", ";};
		if (outDays==1) {outDisplay+=wkComma+outDays+" day";wkComma=", ";} else if (outDays!=0) {outDisplay+=wkComma+outDays+" days";wkComma=", ";};
	} else {
		if (weeksApart==1) {outDisplay+=weeksApart+' week';} else {outDisplay+=weeksApart+' weeks';}
	}
	return outDisplay;
}      
