function popupdate(name, sel, d, m, y) {
	if (document.getElementById) {
		document.write('<input type="text" name="quote['+name+']" value="'+sel+'" class="ti" id="date_output_'+name+'" readonly="readonly" /><input type="button" id="date_button_'+name+'" class="dateofbutton" onclick="return '+name+'widget.popup()" value="▼" />');
		this.popupele = null;
		this.currentday = d;
		this.currentmonth = m;
		this.currentyear = y;
		this.name = name;
	} else {
		document.write('<input type="text" name="quote['+name+']" value="'+sel+'" class="ti" id="date_output_'+name+'" />');
	}
}
popupdate.prototype.avoid = function(e) {
	if (document.all) e = window.event;
	if (e.stopPropagation) e.stopPropagation();
	e.returnValue = false;
	e.cancelBubble = true;
	return false;
}
popupdate.prototype.days = function(month, year) {
	var counts = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month == 2) {
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
			return 29;
		} else {
			return 28;
		}
	} else {
		return counts[month - 1];
	}
}
popupdate.prototype.monthof = function(month, year) {
	if (month < 1) {
		month += 12;
	} else if (month > 12) {
		month -= 12;
	}
	return month;
}
popupdate.prototype.yearof = function(month, year) {
	if (month < 1) {
		year --;
	} else if (month > 12) {
		year ++;
	}
	return year;
}
popupdate.prototype.didtodate = function(did) {
	var day = '0' + (did - Math.floor(did / 100) * 100);
	var mon = '0' + (Math.floor((did - Math.floor(did / 10000) * 10000) / 100));
	var yea = Math.floor(did / 10000);
	return day.substr(day.length - 2)+'/'+mon.substr(mon.length - 2)+'/200'+yea;
}
popupdate.prototype.selectdate = function(did) {
	this.display.value = this.didtodate(did);
}
popupdate.prototype.createtd = function(num, month, year) {
	var obj = this;
	function over() {
		this.className = this.baseclassname + ' over';
	}
	function out() {
		this.className = this.baseclassname;
	}
	function click(e) {
		obj.killclick();
		obj.selectdate(this.did);
		if (document.all) e = window.event;
		if (e.stopPropagation) e.stopPropagation();
		e.returnValue = false;
		e.cancelBubble = true;
		return false;
	}
	var td = document.createElement('td');
	td.innerHTML = num;
	if (((month < this.currentmonth) && (year == this.currentyear)) || ((month == this.currentmonth) && (year == this.currentyear) && (num < this.currentday)) || (year < this.currentyear)) {
		td.className='nc';
		td.onclick = obj.avoid;
	} else if ((month == this.monthnumber) && (year == this.yearnumber)) {
		td.onmouseover = over;
		td.onmouseout = out;
		td.onclick = click;
		td.did = (year - Math.floor(year / 1000) * 1000) * 10000 + month * 100 + num;
	} else {
		td.className='nc';
		td.onclick = obj.avoid;
	}

	td.baseclassname = td.className;

	return td;
}
popupdate.prototype.refresh = function() {
	var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	var days = ['S','M','T','W','T','F','S'];
	var x;
	var td, tr, tb;
	this.navlabel.innerHTML = months[this.monthnumber-1] + ' ' + this.yearnumber;

	tb = document.createElement('tbody');
	tr = document.createElement('tr');
	for (x = 0; x < 7; x ++) {
		td = document.createElement('th');
		td.innerHTML=days[x];
		td.onclick=this.avoid;
		tr.appendChild (td);
	}
	tb.appendChild(tr);

	tr = document.createElement('tr');
	var lastprevcount = this.days(this.monthof(this.monthnumber-1,this.yearnumber),this.yearof(this.monthnumber-1,this.yearnumber));
	var lastprev = new Date(this.yearof(this.monthnumber-1,this.yearnumber),this.monthof(this.monthnumber-1,this.yearnumber)-1,lastprevcount);
	var lastprevval = lastprev.getDay();
	var d, pd;
	var mm = this.monthof(this.monthnumber-1,this.yearnumber);
	var yy = this.yearof(this.monthnumber-1,this.yearnumber);
	for (d = 0; d <= lastprevval; d ++) {
		pd = lastprevcount - lastprevval + d;
		td = this.createtd(pd, mm, yy);
		tr.appendChild(td);
	}
	var lastday = this.days(this.monthof(this.monthnumber,this.yearnumber),this.yearof(this.monthnumber,this.yearnumber));
	var y = 0;

	for (x = 0; x < lastday; x ++) {
		if (d > 6) {
			d = 0;
			tb.appendChild(tr);
			tr = document.createElement('tr');
			y ++;
		}
		td = this.createtd(x + 1, this.monthnumber, this.yearnumber);
		tr.appendChild(td);
		d ++;
	}
	var end = 13;
	if (y == 5) end = 6;
	mm = this.monthof(this.monthnumber+1,this.yearnumber);
	yy = this.yearof(this.monthnumber+1,this.yearnumber);
	for (x = d; x <= end; x ++ ) {
		if (x == 7) {
			tb.appendChild(tr);
			tr = document.createElement('tr');
		}
		pd = x - d + 1;
		td = this.createtd(pd, mm, yy);
		tr.appendChild(td);
	}
	tb.appendChild(tr);

	td = this.month.getElementsByTagName('tbody');
	this.month.replaceChild(tb, td[0]);
}
popupdate.prototype.go = function(months) {
	this.monthnumber = this.monthnumber + months;
	while (this.monthnumber < 1) {
		this.monthnumber += 12;
		this.yearnumber --;
	}
	while (this.monthnumber > 12) {
		this.monthnumber -= 12;
		this.yearnumber ++;
	}
	if ((this.yearnumber < this.currentyear) || ((this.yearnumber == this.currentyear) && (this.monthnumber < this.currentmonth))) {
		this.monthnumber = this.currentmonth;
		this.yearnumber = this.currentyear;
	}
	this.refresh();
}
popupdate.prototype.initpopup = function() {
	this.display = document.forms[0]['quote['+this.name+']'];
	this.button = document.getElementById('date_button_'+this.name);
	this.daynumber = Number(this.display.value.substr(0,2));
	this.monthnumber = Number(this.display.value.substr(3,2));
	this.yearnumber = Number(this.display.value.substr(6));
	this.popupparent = this.display.parentNode;
	this.popupele = document.createElement('div');
	this.popupele.className='datepopup';
	var node = this.popupparent;
	var totaltop = 0;
	while (node && (node.style.position != 'relative')) {
		totaltop += node.offsetTop;
		node = node.offsetParent;
	}
	totaltop += this.popupparent.offsetHeight - 1;
	this.popupele.style.top = totaltop + 'px';
	this.popupele.style.left = (this.popupparent.offsetWidth - 135) + 'px';
	this.navshell = document.createElement('table');
	this.navshell.setAttribute('border', '0');
	this.navshell.setAttribute('cellPadding', '0');
	this.navshell.setAttribute('cellSpacing', '0');
	this.navshell.className = 'nav';
	this.popupele.appendChild(this.navshell);
	var tb = document.createElement('tbody');
	this.navshell.appendChild(tb);
	var tr = document.createElement('tr');
	tb.appendChild(tr);
	var obj = this;
	var button = function(jmp, txt) {
		var td = document.createElement('td');
		var div = document.createElement('div');
		div.className='btn';
		div.onclick=function(e) {
			if (document.all) e = window.event;
			obj.go(jmp);
			if (e.stopPropagation) e.stopPropagation();
			e.returnValue = false;
			e.cancelBubble = true;
			return false;
		}
		div.ondblclick=function() {
			e = window.event;
			if (e) {
				obj.go(jmp);
				if (e.stopPropagation) e.stopPropagation();
				e.returnValue = false;
				e.cancelBubble = true;
				return false;
			}
		}
		div.onmouseover = function() {
			this.className = 'btn over';
		}
		div.onmouseout = function() {
			this.className = 'btn';
		}
		div.onmousedown = function() {
			this.className = 'btn down';
		}
		div.onmouseup = function() {
			this.className = 'btn over';
		}
		div.innerHTML = txt;
		td.appendChild(div);
		return td;
	}
	tr.appendChild(button(-12, '◄◄'));
	tr.appendChild(button(-1, '◄'));
	td = document.createElement('td');
	this.navlabel = document.createElement('div');
	this.navlabel.className = 'navlabel';
	this.navlabel.onclick=this.avoid;
	td.appendChild(this.navlabel);
	tr.appendChild(td);
	tr.appendChild(button(1, '►'));
	tr.appendChild(button(12, '►►'));
	this.month = document.createElement('table');
	this.month.setAttribute('border', '0');
	this.month.setAttribute('cellPadding', '0');
	this.month.setAttribute('cellSpacing', '0');
	this.month.className="month";
	if (navigator.userAgent.indexOf("Firefox") != -1) {
		// browsers suck
		this.month.style.margin = '1px 0px 0px 1px';
	}
	tb = document.createElement('tbody');
	this.month.appendChild(tb);
	this.popupele.appendChild(this.month);
	var layout = document.getElementById('layout');
	layout.appendChild(this.popupele);
}
popupdate.prototype.killclick = function() {
	var obj = this;
	setTimeout(function() {
		document.body.onclick = null;
		obj.popupele.style.visibility='hidden';
	}, 50);
}
popupdate.prototype.popup = function() {
	if (this.popupele == null) {
		this.initpopup();
	}
	if (this.popupele.style.visibility != 'visible') {
		this.popupele.style.visibility='visible';
		this.refresh();
		var obj = this;
		setTimeout(function() {
			document.body.onclick = function() {
				obj.killclick();
			}
		}, 100);
		setTimeout(function() {
			var totalheight = window.scrollY != undefined?window.scrollY + window.innerHeight:(document.body.parentNode.clientHeight == 0?document.body.clientHeight + document.body.scrollTop:document.body.parentNode.clientHeight + document.body.parentNode.scrollTop);
			var totaltop = 0;
			var node = obj.popupele;
			while (node) {
				totaltop += node.offsetTop;
				node = node.offsetParent;
			}
			var offby = obj.popupele.offsetHeight + totaltop - totalheight;
			if (offby > 0) {
				window.scrollBy(0, offby);
			}
		}, 100);
	}
}
