/**
 * 日時入力ポップアップウィンドウ
 * 参考:
 *   AllAbout サンプル:http://allabout.co.jp/career/javascript/closeup/CU20020529/
 * 注意事項:
 *   呼び出し元のformにname属性を付けること(名前は何でも良い)
 */
function dateinput(obj, y, m) {
	var nowDate;
	var strWeek;
	var lastDay;
	var cntWeek;
	var year;
	var month;
	if (y == undefined) {
		nowDate = new Date();
	}
	else {
		year  = y;
		month = m;
		nowDate = new Date(year, month, 1);
	}
	year  = nowDate.getYear();
	month = nowDate.getMonth();
	if (year < 1900) { year += 1900; }
	
	// prev,next month
	var prev_month, prev_year;
	var next_month, next_year;
	if (month == 12) {
		next_month = 1; next_year = year+1;
	}
	else {
		next_month = month+1; next_year = year;
	}
	if (month == 1) {
		prev_month = 12; prev_year = year - 1;
	}
	else {
		prev_month = month - 1; prev_year = year;
	}

	strWeek = "<font color=red>Sun</font>,Mon,Tue,Wed,Thu,Fri,<font color=blue>Sat</font>"
	strWeek = strWeek.split(",");
	nowDate = new Date(year, month, 1);
	cntWeek = nowDate.getDay();
	nowDate.setMonth(month + 1);
	nowDate.setDate(0);
	lastDay = nowDate.getDate();
	
	var w=240;
	var h=240;
	var x=100;
	var y=20;
	if(document.all){
		// IE6 マウス座標を基準にした位置に表示
		x=window.event.screenX+30;
		y=window.event.screenY-180;
	} else if (document.layers || document.getElementById){
		// その他のブラウザ
		x+=window.screenX;
		y+=window.screenY;
	}
	//y = obj.form.name.obj.name.y;
	
	mkSubWin('','dateinput',x,y,w,h);
	
	wnd.document.write("<html>\n");
	wnd.document.write("<head>\n");
	wnd.document.write('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">\n');
	
	wnd.document.write("</head>\n");
	wnd.document.write("<body>\n");

	wnd.document.write('<form name="f">\n');
	
	wnd.document.write('<table border="1">\n');
	wnd.document.write("<tr>");
	wnd.document.write("<td>");
	
	wnd.document.write('<input type="button" value="<"');
	wnd.document.write('  onClick="self.opener.dateinput(self.opener.document.');
	wnd.document.write(obj.form.name+'.'+obj.name+','+prev_year+','+prev_month+')">\n');

	wnd.document.write("<" + "/td>");
	wnd.document.write("<td colspan=5 align=center>");
	wnd.document.write(year + '-' + (month + 1));
	wnd.document.write("<" + "/td>");
	wnd.document.write("<td>");
	
	wnd.document.write('<input type="button" value=">" ');
	wnd.document.write('onClick="self.opener.dateinput(self.opener.document.');
	wnd.document.write(obj.form.name+'.'+obj.name+','+next_year+','+next_month+')">\n');

	wnd.document.write("<" + "/td>");
	wnd.document.write("</tr>");

	wnd.document.write("<tr>");
	for (tmp in strWeek) {
		wnd.document.write('<td width="26px">' + strWeek[tmp] + "<" + "/td>");
	}
	wnd.document.write("<" + "/tr>");
	wnd.document.write("<tr>");

	for (i = 0; i < cntWeek; i++) { wnd.document.write("<td><br /><","/td>"); };

	for (tmp=1;tmp<=lastDay;tmp++) {

		wrtDate  = new Date(year, month, tmp);
		wrtDay   = wrtDate.getDate();
		wrtMonth = wrtDate.getMonth() + 1;
		wrtYear  = wrtDate.getYear();
		if (wrtYear < 1900) { wrtYear=1900 + wrtYear; };
		//wrtDateTime = wrtYear+'-'+wrtMonth+'-'+wrtDay+' '+'00:00:00';
		wrtDateTime = wrtYear+'-'+wrtMonth+'-'+wrtDay;
		
		wnd.document.write('<td align="right">');
		
		wrtdateA  = '<a href="javascript:function v(){';
		wrtdateA += '   self.opener.document.'+obj.form.name;
		wrtdateA += '.'+obj.name+'.value=(\'';
		wrtdateA += wrtDateTime;
		wrtdateA += ' \'+document.f.hour.value+\':\'+document.f.min.value+\':\'+document.f.sec.value);';
		wrtdateA += 'self.close()};v()" ';
		wrtdateA += '>\n';
		
		wrtdateA += '<font color="#000000">\n';
		wrtdateA += tmp;
		wrtdateA += '</font>\n';
		wrtdateA += '</a>\n';
		wnd.document.write(wrtdateA);
		
		wnd.document.write("<" + "/td>");
		cntWeek++;
		if (cntWeek > 6 && tmp != lastDay) {
			wnd.document.write("<" + "/tr><tr>");
			cntWeek = 0;
		}
	}
	for (i = 0; i < (7 - cntWeek); i++) { wnd.document.write('<td><br /></td>'); };
	wnd.document.write('</tr></table>');
	
	//----->
	wnd.document.write('<select name="hour">');
	for (i = 0; i < 24; i++) {
		if (i < 10)
			str_i = '0'+i;
		else
			str_i = ''+i;
		wnd.document.write('<option value="'+str_i+'">'+str_i+'</option>\n');
	}
	wnd.document.write('</select>\n:');
	
	wnd.document.write('<select name="min">');
	for (i = 0; i < 60; i++) {
		if (i < 10)
			str_i = '0'+i;
		else
			str_i = ''+i;
		wnd.document.write('<option value="'+str_i+'">'+str_i+'</option>\n');
	}
	wnd.document.write('</select>\n:');
	
	wnd.document.write('<select name="sec">');
	for (i = 0; i < 60; i++) {
		if (i < 10)
			str_i = '0'+i;
		else
			str_i = ''+i;
		wnd.document.write('<option value="'+str_i+'">'+str_i+'</option>\n');
	}
	wnd.document.write('</select>\n');
	
	wrtdateA  = '<a href="javascript:function v(){';
	wrtdateA += '   self.opener.document.'+obj.form.name;
	wrtdateA += '.'+obj.name+'.value=(\'\');self.close()};v()" ';
	wrtdateA += '>\n';
	wrtdateA += '<font color="#000000">\n';
	wrtdateA += 'Clear';
	wrtdateA += '</font>\n';
	wrtdateA += '</a>\n';
	wnd.document.write(wrtdateA);
	//<-----
	
	wnd.document.write("</form>\n");
	
	wnd.document.write("</body>\n");
	wnd.document.write("</html>\n");

	wnd.document.close();
}

/********************************************************************
 * 簡易サブウインドウ開き
 *  Syntax : mkSubWin(URL,winName,x,y,w,h)
 *  例     : mkSubWin(winIndex,'test.htm','win0',100,200,150,300)
 * ------------------------------------------------------------------
 */

var wnd;

function mkSubWin(URL,winName,x,y,w,h){

    var para =""
             +" left="        +x
             +",screenX="     +x
             +",top="         +y
             +",screenY="     +y
             +",toolbar="     +0
             +",location="    +0
             +",directories=" +0
             +",status="      +0
             +",menubar="     +0
             +",scrollbars="  +0
             +",resizable="   +1
             +",innerWidth="  +w
             +",innerHeight=" +h
             +",width="       +w
             +",height="      +h

        wnd=window.open(URL,winName,para);
        wnd.focus()

}