/*
-----------------------------------------------
Date Script
-----------------------------------------------
*/
Date.prototype._baseFormat = "dd/mm/yyyy"
Date.prototype.format = 'dd mmmm yyyy'
Date.prototype._toString = Date.prototype.toString;
Date.prototype.toString = function() {
	if (!isNaN(this) && typeof this.format == 'string') {
		var sDate = this.format;
		var sRes = this.format;
		
		// Replace the day code
		while (sDate.search(/(dddd)|(ddd)|(dd)|(d)/i) >= 0) {
			if (RegExp.$1 != '') {
				sRes = sRes.replace(/dddd/i, longDay(this.getDay()));
				sDate = sDate.replace(/dddd/i, '');
			}
			else if (RegExp.$2 != '') {
				sRes = sRes.replace(/ddd/i, shortDay(this.getDay()));
				sDate = sDate.replace(/ddd/i, '');
			}
			else if (RegExp.$3 != '') {
				if (this.getDate() < 10)
				{
					sRes = sRes.replace(/dd/i, '0' + this.getDate());
				}
				else
				{
					sRes = sRes.replace(/dd/i, this.getDate());
				}
				sDate = sDate.replace(/dd/i, '');
			}
			else if (RegExp.$4 != '') {
				sRes = sRes.replace(/d/i, this.getDate());
				sDate = sDate.replace(/d/i, '');
			}
		}
		
		// Replace the month code
		while (sDate.search(/(mmmm)|(mmm)|(mm)|(m)/i) >= 0) {
			if (RegExp.$1 != '') {
				sRes = sRes.replace(/mmmm/i, longMonth(this.getMonth()));
				sDate = sDate.replace(/mmmm/i, '');
			}
			else if (RegExp.$2 != '') {
				sRes = sRes.replace(/mmm/i, shortMonth(this.getMonth()));
				sDate = sDate.replace(/mmm/i, '');
			}
			else if (RegExp.$3 != '') {
				if (this.getMonth() + 1 < 10)
					sRes = sRes.replace(/mm/i, '0' + (this.getMonth() + 1));
				else
					sRes = sRes.replace(/mm/i, this.getMonth() + 1);
				
				sDate = sDate.replace(/mm/i, '');
			}
			else if (RegExp.$4 != '') {
				sRes = sRes.replace(/m/i, this.getMonth() + 1);
				sDate = sDate.replace(/m/i, '');
			}
		}
		
		// Replace the year code
		if (sDate.search(/(yyyy)|(yy)/i) >= 0) {
			if (RegExp.$1 != '') {
				sRes = sRes.replace(/yyyy/i, longYear(this.getFullYear()));
				sDate = sDate.replace(/yyyy/i, '');
			}
			else if (RegExp.$2 != '') {
				sRes = sRes.replace(/yy/i, shortYear(this.getFullYear()));
				sDate = sDate.replace(/yy/i, '');
			}
		}
		
		// Return the code-replace string
		return sRes;
	}
	else
		return this._toString();
}

Date.Delims = ", ./-";
Date.parseUK = function(sDate) {
	var dateVal;
	var intCounter;
	var items = new Array();
	items[0] = "";
	for (intCounter = 0; intCounter < sDate.length; intCounter++) {
		if (Date.Delims.indexOf(sDate.charAt(intCounter)) > -1) {
			//intCounter++; // what if seperator is at end of strDate??
			if (items[items.length - 1] != "") 
				items[items.length] = "";
				// only add new item if last item is not null
		}
		else if ((items[items.length-1] == "") && (sDate.charAt(intCounter) == "0")) {
			//intCounter++;
			// remove leading zeros
		}
		else
 			items[items.length-1] += sDate.charAt(intCounter);
	}
	if (items.length == 3) {
	//Assume Day, Month + Year
		if (items[2]=='') items[2]='2000';
		
		// Using New Date("month day year") in Explorer,
		// a two figure year between 70 and 99 is interpreted as 1900 + year
		// numbers over 99 are interpreted as their full date value
		// numbers less than 70 produce NaN	
		if (parseInt(items[2]) < 70) items[2] = parseInt(items[2]) + 2000;
		
		if (parseInt(items[1]) == items[1]) {
			if (parseInt(items[0]) == items[0]) {
				dateVal = new Date(items[1] + "/" + items[0] + "/" + items[2]);
			}
			else {
				dateVal = new Date(items[0] + " " + items[1] + " " + items[2]);
			}
		} 
		else {
			dateVal = new Date(items[1] + " " + items[0] + " " + items[2]);
		}
	}
	else if (items.length == 2) {
	//Assume Day + Month - or - Month + Year
		var dteNow = new Date();
		if (items[1]=="") items[1]="2000";
		
		if (parseInt(items[1]) == items[1]) {
			if (parseInt(items[0]) == items[0]) {
			//day + month -or- month + year
			
				if (parseInt(items[1]) <= 12) {
				//Day + Month
					dateVal = new Date(items[1] + "/" + items[0] + "/" + dteNow.getFullYear());
				}
				else {
				//Month + Year
					if (parseInt(items[1])<70) items[1] = parseInt(items[1]) + 2000;
					dateVal = new Date(items[0] + "/1/" + items[1]);
				}
			}
			else {
			//Month + Year	
				if (parseInt(items[1])<70) items[1] = parseInt(items[1]) + 2000;
				dateVal = new Date(items[0] + " 1 " + items[1]);
			}
		} 
		else {
		//Day + Month
			dateVal = new Date(items[1] + " " + items[0] + " " + dteNow.getFullYear());
		}
	}
	else if (items.length == 1) {
	//Assume month
		var dteNow = new Date();
		if (parseInt(items[0]) == items[0]) {
			dateVal = new Date(items[0] + '/1/' + dteNow.getFullYear());
		}
		else {
			dateVal = new Date(items[0] + ' 1 ' + dteNow.getFullYear());
		}
	}
	else {
		dateVal = undefined;
	}
	
	return dateVal;
}
// this should support mmm d yy input. perhaps it should interpret ambiguous input on the
// basis of the default canonical format

function shortDay(iDay) {
	if (iDay >= 0 && iDay <= 6) return shortDay.days[iDay];
	else return "n/a";
}
shortDay.days = new Array();
shortDay.days[0] = "Sun";
shortDay.days[1] = "Mon";
shortDay.days[2] = "Tue";
shortDay.days[3] = "Wed";
shortDay.days[4] = "Thur";
shortDay.days[5] = "Fri";
shortDay.days[6] = "Sat";

function longDay(iDay) {
	if (iDay >= 0 && iDay <= 6) return longDay.days[iDay];
	else return "n/a";
}
longDay.days = new Array();
longDay.days[0] = "Sunday";
longDay.days[1] = "Monday";
longDay.days[2] = "Tuesday";
longDay.days[3] = "Wednesday";
longDay.days[4] = "Thursday";
longDay.days[5] = "Friday";
longDay.days[6] = "Saturday";

function shortMonth(iMonth) {
	if (iMonth >= 0 && iMonth <= 11) return shortMonth.months[iMonth];
	else return "n/a";
}
shortMonth.months = new Array();
shortMonth.months[0] = "Jan";
shortMonth.months[1] = "Feb";
shortMonth.months[2] = "Mar";
shortMonth.months[3] = "Apr";
shortMonth.months[4] = "May";
shortMonth.months[5] = "Jun";
shortMonth.months[6] = "Jul";
shortMonth.months[7] = "Aug";
shortMonth.months[8] = "Sep";
shortMonth.months[9] = "Oct";
shortMonth.months[10] = "Nov";
shortMonth.months[11] = "Dec";

function longMonth(iMonth) {
	if (iMonth >= 0 && iMonth <= 11) return longMonth.months[iMonth];
	else return "n/a";
}
longMonth.months = new Array();
longMonth.months[0] = "January";
longMonth.months[1] = "February";
longMonth.months[2] = "March";
longMonth.months[3] = "April";
longMonth.months[4] = "May";
longMonth.months[5] = "June";
longMonth.months[6] = "July";
longMonth.months[7] = "August";
longMonth.months[8] = "September";
longMonth.months[9] = "October";
longMonth.months[10] = "November";
longMonth.months[11] = "December";

// These year formatting function assume the iYear parameter
// was obtained by calling a date object's getFullYear method
// the getYear method is painfull to use and NN & IE differ
function longYear(iYear) {
	return iYear;
}
function shortYear(iYear) {
	if (iYear < 10) return '0' + iYear;
	if (iYear < 100) return iYear;
	else {
		var sYear = new String(iYear);
		return sYear.slice(sYear.length - 2);
	}
}

