
function formatDate(theFormat, theDate) {
	var theShortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	var theLongMonths = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	var theLongDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	
	var delimiters = ["-","/"," ","."," "];
	var newDate = new Array();  // New date array after conversion
	
	var temp = new Array();
	for (var j= 0; j<delimiters.length; j++) {
			temp = theFormat.split(delimiters[j]);
			if(temp.length > 1) break;
	}

	for(var i = 0; i < temp.length; i++) {
		if(temp[i].toLowerCase().indexOf('d') != -1) doDays(temp[i],i);
		if(temp[i].toLowerCase().indexOf('m') != -1) doMonths(temp[i],i);
		if(temp[i].toLowerCase().indexOf('y') != -1) doYears(temp[i],i);
	}
	
	function doMonths(month, index) {
		switch (month.toLowerCase()) {
			case "m":
				newDate[index] = theDate.getMonth() + 1;
				break;
			case "mm":
				newDate[index] = (theDate.getMonth < 10)?"0"+(theDate.getMonth()+1):theDate.getMonth()+1;
				break;
			case "mmm":
				newDate[index] = theShortMonths[theDate.getMonth()];
				break;
			case "mmmm":
				newDate[index] = theLongMonths[theDate.getMonth()];
				break;
			case "m,":
				newDate[index] = theDate.getMonth() + 1 + ",";
				break;
			case "mm,":
				newDate[index] = (theDate.getMonth < 10)?"0"+(theDate.getMonth()+1) + ",":theDate.getMonth()+1 + ",";
				break;
			case "mmm,":
				newDate[index] = theShortMonths[theDate.getMonth()] + ",";
				break;
			case "mmmm,":
				newDate[index] = theLongMonths[theDate.getMonth()] + ",";
				break;
		}
		if(month.toUpperCase() === month) newDate[index] = newDate[index].toString().toUpperCase();
	}
	
	function doDays(day, index) {
		switch (day.toLowerCase()) {
			case "d":
				newDate[index] = theDate.getDate();
				break;
			case "dd":
				newDate[index] = (theDate.getDate() < 10) ? "0" + theDate.getDate():theDate.getDate();
				break;			
			case "ddd":
				newDate[index] = theLongDays[theDate.getDay()];
				break;			
			case "d,":
				newDate[index] = theDate.getDate() + ",";
				break;
			case "dd,":
				newDate[index] = (theDate.getDate() < 10) ? "0" + theDate.getDate():theDate.getDate() + ",";
				break;			
			case "ddd,":
				newDate[index] = theLongDays[theDate.getDay()] + ",";
				break;			
			}	
			if(day.toUpperCase() === day) newDate[index] = newDate[index].toString().toUpperCase();			
	}
	
	function doYears(year, index) {
		switch (year) {
			case "yy":
				newDate[index] = theDate.getFullYear().toString().substr(2,2);
				break;
			case "yyyy":
				newDate[index] = theDate.getFullYear();
				break;			
		}	
	}
	
	var formattedDate = "";
	for(var i=0; i<newDate.length; i++) {
		formattedDate += newDate[i];
		if(i != newDate.length - 1) formattedDate += delimiters[j];
	}
	return formattedDate;
}

function formatAllDates() {
	var theDate = new Date(MM_findObj("letterDate").value);
	for(var i in MM_findObj("letterDateStyle").options) {
		if(i.dateformat)
			i.text = formatDate(i.dateformat,theDate); 
  }
}

function newShowCalendar() {
	window.open("/calendar.cfm", "CalendarWindow", "width=250,height=200");
}
