/* Date selector widget for the 'release date'-style dates, with support for * TBA and unknown month or day. Usage: * * * * Will add a date selector to the HTML at that place, and automatically * read/write the value of the hidden field. Alternative usage: * * var obj = dateLoad(ref, serfunc); * * If 'ref' is set, it will behave as above with 'ref' being the input object. * Otherwise it will return the widget object. The setfunc, if set, will be * called whenever the date widget is focussed or its value is changed. * * The object returned by dateLoad() can be used as follows: * obj.date_val: Always contains the currently selected date. * obj.dateSet(val): Change the selected date */ function load(obj, serfunc) { var i; var selops = {style: 'width: 70px', onfocus:serfunc, onchange: serialize, tabIndex: 10}; var year = tag('select', selops, tag('option', {value:0}, '-year-'), tag('option', {value:9999}, 'TBA') ); for(i=(new Date()).getFullYear()+5; i>=1980; i--) year.appendChild(tag('option', {value: i}, i)); var month = tag('select', selops, tag('option', {value:99}, '-month-') ); for(i=1; i<=12; i++) month.appendChild(tag('option', {value: i}, i)); var day = tag('select', selops, tag('option', {value:99}, '-day-') ); for(i=1; i<=31; i++) day.appendChild(tag('option', {value: i}, i)); var div = tag('div', { date_obj: obj, date_serfunc: serfunc, date_val: obj ? obj.value : 0 }, year, month, day); div.dateSet = function(v){ set(div, v) }; set(div, div.date_val); return obj ? obj.parentNode.insertBefore(div, obj) : div; } function set(div, val) { val = +val || 0; val = [ Math.floor(val/10000), Math.floor(val/100)%100, val%100 ]; if(val[1] == 0) val[1] = 99; if(val[2] == 0) val[2] = 99; var l = byName(div, 'select'); for(var i=0; i