/*
	DROP-DOWN DATE PICKER
	use writeDatePicker function inline
	writeDatePicker(outFormat,outIDName,showYears,editable,showDay)
	outFormat:String: YYYY-MM-DD | MM\\\\YYYY (comes out as MM\YYYY)
	outIDName:String: Name of result field
	showYears:Number: show +/- years in year selector
	editable:Boolean: show result field for manual editing
	showDay:Boolean: show/hide day picker
	
*/

function padString(str){
	str = String(str);
	while(str.length < 2){
		str = "0" + str;
	}
	return str;
}

function numToMonth(str){
	return months[str];
}

var months = ["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

function updateOutput(outFormat,outIDName,showDay){
	if(showDay){ 
		var dayPicker = document.getElementById(outIDName + "DayPicker").value;
	}
	var monthPicker = document.getElementById(outIDName + "MonthPicker").value;
	var yearPicker = document.getElementById(outIDName + "YearPicker").value;
	var output = document.getElementById(outIDName);
	var outStr = "";
	if((dayPicker == "") || monthPicker == "" || yearPicker == ""){
		output.value = 	"";
		return;
	}
	switch(outFormat){
		case "YYYY-MM-DD":
			outStr = yearPicker + "-" + monthPicker + "-" + dayPicker;		
			break;
		case "MM.YYYY":
			outStr = monthPicker + "." + yearPicker;
			break;
	}
	output.value = outStr;
}

function getInitialDate(initialDate,outFormat,outIDName,showDay){
	if(showDay) var dayPicker = document.getElementById(outIDName + "DayPicker");
	var monthPicker = document.getElementById(outIDName + "MonthPicker");
	var yearPicker = document.getElementById(outIDName + "YearPicker");
	var outStr = "";
	var dVal,mVal,yVal;
	switch(outFormat){
		case "YYYY-MM-DD":
			var tempArr = initialDate.split("-");
			yVal = tempArr[0];
			mVal = padString(tempArr[1]);
			dVal = padString(tempArr[2]);
			break;
		case "MM.YYYY":
			var tempArr = initialDate.split(".");
			mVal = padString(tempArr[0]);
			yVal = tempArr[1];
			dVal = "";
			break;
	}
	return [dVal,mVal,yVal];
}

function writeDatePicker(outFormat,outIDName,showYears,editable,showDay,initialDate){
	var initialDateArr;
	if(initialDate){
		initialDateArr = getInitialDate(initialDate,outFormat,outIDName,showDay);
	}

	if(showDay){
		document.writeln('<select onchange="updateOutput(\'' + outFormat + '\',\'' + outIDName + '\',' +  showDay + ')" id="' + outIDName + 'DayPicker" name="' + outIDName + 'DayPicker">');
		document.writeln('<option value=""></option>');
		for(var i=1; i<=31; i++){
			var thisVal = padString(i);
			if(initialDate && initialDateArr[0] == thisVal){
				document.writeln('<option selected="selected" value="' + thisVal + '">' + thisVal + '</option>');
			} else {
				document.writeln('<option value="' + thisVal + '">' + thisVal + '</option>');
			}
		}
		document.writeln('</select>');
	}
	
	document.writeln('<select onchange="updateOutput(\'' + outFormat + '\',\'' + outIDName + '\',' +  showDay + ')" id="' + outIDName + 'MonthPicker" name="' + outIDName + 'MonthPicker">');
	document.writeln('<option value=""></option>');
	for(var i=1; i<=12; i++){
		var thisVal = padString(i);
		if(initialDate && initialDateArr[1] == thisVal){
			document.writeln('<option selected="selected" value="' + thisVal + '">' + numToMonth(i) + '</option>');
		} else {
			document.writeln('<option value="' + thisVal + '">' + numToMonth(i) + '</option>');
		}
	}
	document.writeln('</select>');
	
	var curYear = new Date().getFullYear();
	var reverseYears;
	if(showYears > 0){
		reverseYears = false;
	} else {
		reverseYears = true;
	}
	document.writeln('<select onchange="updateOutput(\'' + outFormat + '\',\'' + outIDName + '\',' +  showDay + ')" id="' + outIDName + 'YearPicker" name="' + outIDName + 'YearPicker">');
	document.writeln('<option value=""></option>');
	if(reverseYears){
		for(i = curYear; i >= curYear + showYears; i--){
			if(initialDate && initialDateArr[2] == i){
				document.write('<option selected="selected" value="' + i + '">' + i + '</option>');
			} else {
				document.write('<option value="' + i + '">' + i + '</option>');
			}
		}
	} else {
		for(i = curYear; i <= curYear + showYears; i++){
			if(initialDate && initialDateArr[2] == i){
				document.write('<option selected="selected" value="' + i + '">' + i + '</option>');
			} else {
				document.write('<option value="' + i + '">' + i + '</option>');
			}
		}
	}
	document.writeln('</select>');
	if(editable){
		document.writeln('<input type="text" id="' + outIDName + '" name="' + outIDName + '" />');
	} else {
		document.writeln('<input type="hidden" id="' + outIDName + '" name="' + outIDName + '" />');
	}
	updateOutput(outFormat,outIDName,showDay);
}
