// ==UserScript==
// @name          Blogger Date enhancer
// @description	  Fixes Blogger's post editor to accept a freeform date.
// @namespace     http://rhyley.port5.com/gm/
// @include	http://www.blogger.com/post-*
// @include	http://blogger.com/post-*

(function() {

var sf = document.getElementById("stuffform");
var pd = document.getElementById("postDate");
var po = document.getElementById("postoptions");
var pb = document.getElementById("postButtons")

// Hide the hateful selects

style1 = document.createElement("style");
style1.type = "text/css";
style1.innerHTML = '#postDate {display:none;}';
style1.innerHTML += '#postButtons * {display:inline;}';
document.getElementsByTagName('head')[0].appendChild(style1);

// get the date pre-populated in the form, and format it 'h:mm AM m/d/yyyy'

function getPostDate() {
	var longDate = sf.postHour.options[sf.postHour.selectedIndex].value; 
	longDate += ":";
	var min = (sf.postMinute.options[sf.postMinute.selectedIndex].value.length == 1) ? '0' + sf.postMinute.options[sf.postMinute.selectedIndex].value : sf.postMinute.options[sf.postMinute.selectedIndex].value;
	longDate += min;
	longDate += " ";
	var AmPm = (sf.postAMPM.options[sf.postAMPM.selectedIndex].value == 0)? "AM" : "PM";
	longDate += AmPm;
	longDate += " ";
	longDate += (sf.postMonth.selectedIndex + 1); // very important, that
	longDate += "/";
	longDate += sf.postDay.options[sf.postDay.selectedIndex].value;
	longDate += "/";
	longDate += sf.postYear.options[sf.postYear.selectedIndex].value;
	return longDate;
}

// Takes the text input and populates it back to the selects blogger uses 

window.setPostDate = function setPostDate(blah) {
	blah = blah.split(" ");
	HoMi = blah[0].split(":");
	AmPm = (blah[1] == "AM")? 0 : 1;
	DMYe = blah[2].split("/");
	document.forms[0].postHour.selectedIndex = (HoMi[0] - 1);
	document.forms[0].postMinute.selectedIndex = HoMi[1];
	document.forms[0].postAMPM.selectedIndex = AmPm;
	document.forms[0].postMonth.selectedIndex = (DMYe[0] - 1);
	document.forms[0].postDay.selectedIndex = (DMYe[1] - 1);
	document.forms[0].postYear.selectedIndex = (DMYe[2] - 1990);
}

// create the text input box and style it

var texty = document.createElement("input");
texty.setAttribute("type","text");
texty.setAttribute("name","texty");
texty.setAttribute("value",getPostDate());
texty.setAttribute("onchange", "setPostDate(this.value);");
texty.setAttribute("onfocus", "this.select();");

var posty = document.createElement("span");
posty.innerHTML = pb.innerHTML;
posty.setAttribute("id","postButtons");
posty.setAttribute("style","margin:-2em 3.5em 0 0");

var container = document.createElement("div");
container.setAttribute("style","float: right; margin:-2.5em 1em 0 0");
container.setAttribute("class","section");

container.appendChild(posty);
container.appendChild(texty);

po.appendChild(container);

pb.parentNode.removeChild(pb);

})();