var ie6andbelow = function () {
var n = navigator.userAgent.indexOf("MSIE");
	return n > 0
		&& parseInt(navigator.userAgent.substring(n + 5)) < 7;
}();
function $e(tagName, className, id) {
	var e = document.createElement(tagName);
	if (className) e.className = className;
	if (id) e.id = id;
	return $x(e);
}
function $t(str) {
	return document.createTextNode(str);
}
function $h(s) {
	var div = $e("div");
	div.innerHTML = s;
	return div.firstChild;
}
function $x(e) {
	e.show = function () { e.style.display = "block"; }
	e.hide = function () { e.style.display = "none"; }
	e.clear = function () {
		var n = this.childNodes.length;
		for (var i = 0; i < n; i++) {
			this.removeChild(this.firstChild);
		}
		return this;
	}
	e.append = function (child) {
		if (typeof(child) == "object") {
			if (child instanceof Array) {
				for (var i = 0; i < child.length; i++) {
					this.append(child[i]);
				}
			} else {
				this.appendChild(child);
			}
		} else {
			this.appendChild($t(String(child)));
		}
		return this;
	}
	e.replace = function (n) {
		return this.clear().append(n);
	}
	return e;
}
function onClickCalendarButton(b){
	var cont=$x(b.nextSibling);
	if(cont.hasChildNodes()){
		b.innerHTML='...';
		cont.clear();
		b.parentNode.style.zIndex=0;
	}else{
		b.innerHTML='×';
		b.parentNode.style.zIndex=1;
		createCalendar(cont, b.previousSibling.value);
	}
}
function createCalendar(cont, date) {
	var curdate = new Date();
	var cy = curdate.getFullYear();
	var year, month;
	var dt = dateValueOf(date);
	if (!dt) {
		dt = curdate;
	}
	year = dt.getFullYear();
	month = dt.getMonth() + 1;
	var cal = $h('<table><thead><tr><td colspan="2">&lt;&lt;</td><td colspan="3"></td><td colspan="2">&gt;&gt;</td></tr><tr><td class="w1">日</td><td class="w2">月</td><td class="w2">火</td><td class="w2">水</td><td class="w2">木</td><td class="w2">金</td><td class="w3">土</td></tr></thead><tbody><tr><td></td></tr></tbody></table>');
	cal.setMonth = function (y, m) {
		y = (m < 1 ? y - 1 : (m > 12 ? y + 1 : y));
		m = (m < 1 ? m + 12 : (m > 12 ? m - 12 : m));
		var dt = new Date(y, m - 1, 1);
		var dow = dt.getDay();
		var tm = dt.getTime() - dow * 86400000;
		var dstyle = [1, 2, 2, 2, 2, 2, 3];
		var tbody = $x(this.lastChild).clear();
		dt.setTime(tm);
		while (dt.getMonth() + 1 == m || dt.getDate() > 7) {
			var row = $e("tr");
			for (var dow = 0; dow < 7; dow++) {
				var col = $e("td",
							(dt.getMonth() + 1 == m ? "w" : "x") + dstyle[dow]
							).append(dt.getDate());
				col.date = (dt.getFullYear() + "/"
					+ (dt.getMonth() < 9 ? "0" : "") + (dt.getMonth() + 1) + "/"
					+ (dt.getDate() < 10 ? "0" : "") + dt.getDate());
				col.onmouseover = function () {
					this.style.backgroundColor = "#ddd";
				}
				col.onmouseout = function () {
					this.style.backgroundColor = "#fff";
				}
				col.onclick = function () {
					var c = this.parentNode.parentNode.parentNode;
					var d = c.parentNode.previousSibling.previousSibling;
					d.value = this.date;
					if (d.onchange) {
						d.onchange();
					}
					c.parentNode.previousSibling.click();
				}
				row.append(col);
				tm += 86400000;
				dt.setTime(tm);
			}
			tbody.append(row);
		}
		var head = this.firstChild.firstChild.firstChild;
		head.onclick = function () {
			this.parentNode.parentNode.parentNode.setMonth(y, m - 1);
		}
		head = $x(head.nextSibling).clear();
		var ydisp = $e("span").append(y);
		var yselect = $e("select");
		yselect.size = 10;
		yselect.style.display = "none";
		ydisp.onclick = function() {
			var i;
			for (i = -40; i <= 40; i++) {
				if (y + i > cy + 10) {
					break;
				}
				yselect.append($e("option").append(y + i));
			}
			yselect.selectedIndex = 40;
			yselect.style.display
				= (yselect.style.display == "block" ? "none" : "block");
		}
		var mdisp = $e("span").append(m);
		var mselect = $e("select");
		mselect.size = 12;
		mselect.style.display = "none";
		mdisp.onclick = function() {
			for (var i = 1; i <= 12; i++) {
				mselect.append($e("option").append(i));
			}
			mselect.selectedIndex = m - 1;
			mselect.style.display
				= (mselect.style.display == "block" ? "none" : "block");
		}
		head.append(ydisp.append(yselect)).append("／").append(mdisp.append(mselect));
		yselect.onchange = function() {
			this.parentNode.parentNode.parentNode.parentNode.parentNode.setMonth(y + this.selectedIndex - 40, m);
		}
		mselect.onchange = function() {
			this.parentNode.parentNode.parentNode.parentNode.parentNode.setMonth(y, this.selectedIndex + 1);
		}
		head = head.nextSibling;
		head.onclick = function () {
			this.parentNode.parentNode.parentNode.setMonth(y, m + 1);
		}
		if (ie6andbelow) {
			var mat = cal.nextSibling;
			if (!mat) {
				mat = $e("iframe");
				mat.style.zIndex = 1;
				cal.parentNode.appendChild(mat);
			}
			mat.style.width = (cal.clientWidth + 2) + "px";
			mat.style.height = (cal.clientHeight + 1) + "px";
		}
	}
	cont.append(cal);
	cal.setMonth(year, month);
	return cal;
}
function dateValueOf(val) {
	var y, m, d;
	var error = false;
	var a = val.match(/^([0-9]{1,4})\/([0-9]{1,2})\/([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})\/([0-9]{1,2})$/)
		|| val.match(/^([0-9]{1,2})([0-9]{2})$/)
		|| val.match(/^([0-9]{1,4})([0-9]{2})([0-9]{2})$/);
	if (a) {
		if (a.length == 4) {
			y = Number(a[1]);
			m = Number(a[2]);
			d = Number(a[3]);
			if (y < 100) {
				y = (y < 50 ? y + 2000 : y + 1900);
			}
		} else {
			m = Number(a[1]);
			d = Number(a[2]);
			var today = new Date();
			y = today.getFullYear();
		}
	} else {
		error = true;
	}
	error = error || (y < 1900 || y > 2099 || m < 1 || m > 12 || d < 1 || d > 31);
	return error ? null : new Date(y, m - 1, d);
}

function submitParentForm(elem){
  var f = getParentForm(elem);
  if(f != null){
    f.submit();
  }
}

function getParentForm(elem){
  var n = elem;
  while(n = n.parentNode){
    if(n.tagName.match(/^form$/i)){
      return n;
    }
  }
  return null;
}

function formValuesAsQuery(f){
  var ret = '';
  for(var i = 0;i < f.elements.length;i++){
    var elem = f.elements[i];
    var name = elem.name;
    var value = '';
    switch(elem.type){
      case 'checkbox' : elem.checked ? value = elem.value : value = null;break;
      case 'radio' : elem.checked ? value = elem.value : value = null;break;
      default : value = elem.value;break;
    }
    if(value != null){
      ret += name + '=' + value;
      if(i + 1 != f.elements.length){
        ret += '&';
      }
    }
  }
  return ret;
}

function Initializer() {
	this.initFuncs = [];
	this.add = function(func) {
		this.initFuncs[this.initFuncs.length] = func;
	}
	this.execute = function() {
		for (var i = 0; i < this.initFuncs.length; i++) {
			this.initFuncs[i].call();
		}
	}
}
var initializer = new Initializer();