#include "wsubformHTML.h"	//サブフォーム専用ヘッダファイルをインクルードする。
#include <fcgi_stdio.h>

extern char gridtitles[MAX_POS][MAX_LEN];	//wsubformHTMLが利用するgridのタイトル。

extern TextBox	 *begin;	//textboxで使用する連鎖ポインタの開始位置
extern TextBox	 *tp;	//textboxで使用する連鎖ポインタ

typedef struct stat Dir;	//ファイル操作用
static Dir dirptr;
static Dir  *fptr = &dirptr;

char tmp[MAX_STR];
static int sqlcode = 0;	//PostgreSQLの返すコード

int DBcount[MAX_FORM];	//wsubformHTMLで利用するDBの現在の最大行数
extern char KEY[256][MAX_STR];	//INSERTとUPDATEが使用する更新項目
extern int IKEY[256];	//INSERTとUPDATEが使用する更新項目
char COMMANDNAME[128];
int no_max;

static char prefecture[47][15] = {"北海道","青森県","岩手県","秋田県","山形県","宮城県","福島県","茨城県","栃木県","群馬県","埼玉県","千葉県","東京都","神奈川県","新潟県","富山県","石川県","福井県","山梨県","長野県","岐阜県","静岡県","愛知県","三重県","滋賀県","京都府","大阪府","兵庫県","奈良県","和歌山県","鳥取県","島根県","岡山県","広島県","山口県","徳島県","香川県","愛媛県","高知県","福岡県","佐賀県","長崎県","熊本県","大分県","宮崎県","鹿児島県","沖縄県"};

int DBConnect(void)
{
	// DB接続用変数宣言です。DB名、ユーザ名、パスワードを設定してください。
	EXEC SQL BEGIN DECLARE SECTION;
		char dbname[12];
		char user[12];
		char passwd[12];
	EXEC SQL END DECLARE SECTION;

	strcpy(dbname,"apsch");
	strcpy(user,"cms");
	strcpy(passwd,"cms");

#ifdef DEBUG
	// ECPGデバッグ文出力
	ECPGdebug (0, stderr);
#endif

	// 接続
	memset (sqlca.sqlstate, '0', 5);
	EXEC SQL CONNECT TO :dbname AS dbConnection user :user USING :passwd;
	if (memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
		sprintf(tmp,"CONNECT エラー。終了します。:%d - %s",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
	//	logmsg(tmp);
		exit (EXIT_FAILURE);
	}
}

int Fetch_otodoke_Fetch(int no)
{
	static char DBbuff[MAX_STR];
	int i = 0;
	char *qs;
	int qcount;

	DBConnect();
	memset (sqlca.sqlstate, '0', 5);

	EXEC SQL BEGIN DECLARE SECTION;
		int rec_max;
		int rec_count;
		char	 key[256][MAX_STR];
		int	 ikey;
		int	No;	//TEXT
		char	yubinNo[7 + 1];	//TEXT
		char	todofuken[24 + 1];	//TEXT
		char	jusho1[96 + 1];	//TEXT
		char	jusho2[96 + 1];	//TEXT
		char	shimei1[96 + 1];	//TEXT
		char	shimei_f1[192 + 1];	//TEXT
		char	shimei2[96 + 1];	//TEXT
		char	shimei_f2[200 + 1];	//TEXT
		char	TEL[13 + 1];	//TEXT
		char	hinmei[90 + 1];	//TEXT
		int		hinsyu;		//NUMERIC
		char	yoryo[5 + 1];	//TEXT
		char	hm_kubun[2 + 1];	//TEXT
		char	grade[144 + 1];	//TEXT
		char	size[10 + 1];	//TEXT
		int		kingaku;		//NUMERIC
		unsigned char		jikankibou;		//BOOL
		int		takuhai_s;		//NUMERIC
		unsigned char		tuchi;		//BOOL
		char	denNo[13 + 1];	//TEXT
		unsigned char		tokui;		//BOOL
		char	uketukeDATE[10 + 1];	//TEXT
		char	expDATE[10 + 1];	//TEXT
		char	hopeDATE[10 + 1];	//TEXT
		char	bikou[256 + 1];	//TEXT
		char	hassoCD[5 + 1];	//TEXT
		char	hatutenCD[7 + 1];	//TEXT
		unsigned char		hatuEND;		//BOOL
		unsigned char		prEND;		//BOOL
		int		denNUM;		//NUMERIC
		int		hakoNUM;		//NUMERIC
		char	memo[1024 + 1];	//TEXT
		unsigned char		reserve;		//BOOL
		char	resDATE[10 + 1];	//TEXT
		unsigned char		print;		//BOOL
		int		prNUM;		//NUMERIC
		char	daitaiCD[15 + 1];	//TEXT
		unsigned char		daitai;		//BOOL
		char	FAX[13 + 1];	//TEXT
		char	keitaiTEL[13 + 1];	//TEXT
		int		hopeTIME;		//NUMERIC
		int		hopeTIME2;		//NUMERIC
		int		hopeTIME3;		//NUMERIC
		unsigned char		umu;		//BOOL
		int		soryo;		//NUMERIC
		int		hatu_kosuu;		//NUMERIC
		int		hakodai;		//NUMERIC
		char	chitai[60 + 1];	//TEXT
		char	mail[50 + 1];	//TEXT
		int		tesuu;		//NUMERIC
		int		tac;		//NUMERIC
		unsigned char		reitou;		//BOOL
		char	YahooID[32 + 1];	//TEXT
		char	shiwakeCD[13 + 1];	//TEXT
		unsigned char		kariseikyu;		//BOOL
		int		cc_kingaku;		//NUMERIC
		int		st_kingaku;		//NUMERIC
		int		base_soryo;		//NUMERIC
		unsigned char		seikyuEND;		//BOOL
		int		den_hakoNUM;		//NUMERIC
		int		soukin_tesuuryo;		//NUMERIC
	EXEC SQL END DECLARE SECTION;

	sqlcode = 0;
	DBcount[no] = 0;

	rec_count = 0;

	if((qs = getenv("QUERY_STRING")) != (char *)NULL)
	{
		qcount = query_getHTML(qs,'&');
		for(i=0;i < qcount;i++)
		{
			sprintf(key[i],"%s",qt.get[i]);
		}
	}
	else
	{
		sprintf(key[0],"NONE");
		printf("%s<br>\n",key[0]);
	}

//	以下の部分はリレーションテーブルを利用する場合にコメントを外して使用可能にして利用してください。
	sprintf(KEY[0],"%s",key[0]);

	// DBの行数を取得
	EXEC SQL 
	SELECT 
	COUNT(*)
	INTO :rec_count
	FROM	otodoke
	WHERE hassoCD= :key[0];
	// SELECTエラー 
	if(memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
		sprintf(tmp,"%s->SELECT COUNTエラー:%d - %s",COMMANDNAME,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
		logmsg(tmp);
		return(-1) ;
	}

	if(rec_count == 0)
	{
		sprintf(hassoCD,"%s",key[0]);
		no_max = 1;
	}
	else
	{
		// Noの最大数を取得
		EXEC SQL 
		SELECT 
		MAX(No)
		INTO :rec_max
		FROM	otodoke;
	//	WHERE hassoCD = :key[0];

		// SELECTエラー 
		if(memcmp (sqlca.sqlstate, "00000", 5) != 0)
		{
			sprintf(tmp,"%s->SELECT MAX()エラー:%d - %s",COMMANDNAME,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
			logmsg(tmp);
			return(-1) ;
		}

		no_max = rec_max+1;
	}

//	DBcount[no] = rec_count;
	DBcount[no] = 1;

	EXEC SQL DECLARE otodoke_Fetch_CURSOR CURSOR FOR 
	SELECT 
		No,
		coalesce(yubinNo,' ','0'),
		coalesce(todofuken,' ','0'),
		coalesce(jusho1,' ','0'),
		coalesce(jusho2,' ','0'),
		coalesce(shimei1,' ','0'),
		coalesce(shimei_f1,' ','0'),
		coalesce(shimei2,' ','0'),
		coalesce(shimei_f2,' ','0'),
		coalesce(TEL,' ','0'),
		coalesce(hinmei,' ','0'),
		hinsyu,
		coalesce(yoryo,' ','0'),
		coalesce(hm_kubun,' ','0'),
		coalesce(grade,' ','0'),
		coalesce(size,' ','0'),
		kingaku,
		jikankibou,
		takuhai_s,
		tuchi,
		coalesce(denNo,' ','0'),
		tokui,
		coalesce(uketukeDATE,' ','0'),
		coalesce(expDATE,' ','0'),
		coalesce(hopeDATE,' ','0'),
		coalesce(bikou,' ','0'),
		coalesce(hassoCD,' ','0'),
		coalesce(hatutenCD,' ','0'),
		hatuEND,
		prEND,
		denNUM,
		hakoNUM,
		coalesce(memo,' ','0'),
		reserve,
		coalesce(resDATE,' ','0'),
		print,
		prNUM,
		coalesce(daitaiCD,' ','0'),
		daitai,
		coalesce(FAX,' ','0'),
		coalesce(keitaiTEL,' ','0'),
		hopeTIME,
		hopeTIME2,
		hopeTIME3,
		umu,
		soryo,
		hatu_kosuu,
		hakodai,
		coalesce(chitai,' ','0'),
		coalesce(mail,' ','0'),
		tesuu,
		tac,
		reitou,
		coalesce(YahooID,' ','0'),
		coalesce(shiwakeCD,' ','0'),
		kariseikyu,
		cc_kingaku,
		st_kingaku,
		base_soryo,
		seikyuEND,
		den_hakoNUM,
		soukin_tesuuryo
	FROM	otodoke
	WHERE hassoCD= :key[0];
//	ORDER BY hassoCD;

	// カーソルオープン
	EXEC SQL OPEN otodoke_Fetch_CURSOR;

	// CURSOR OPENエラー 
	if (memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
		sprintf(tmp,"%s->CURSOR OPEN()エラー:%d - %s",COMMANDNAME,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
		logmsg(tmp);
		return(-1);
	}

	for(i=0;i<DBcount[no];i++)
	{
		EXEC SQL FETCH otodoke_Fetch_CURSOR INTO
			:No,
			:yubinNo,
			:todofuken,
			:jusho1,
			:jusho2,
			:shimei1,
			:shimei_f1,
			:shimei2,
			:shimei_f2,
			:TEL,
			:hinmei,
			:hinsyu,
			:yoryo,
			:hm_kubun,
			:grade,
			:size,
			:kingaku,
			:jikankibou,
			:takuhai_s,
			:tuchi,
			:denNo,
			:tokui,
			:uketukeDATE,
			:expDATE,
			:hopeDATE,
			:bikou,
			:hassoCD,
			:hatutenCD,
			:hatuEND,
			:prEND,
			:denNUM,
			:hakoNUM,
			:memo,
			:reserve,
			:resDATE,
			:print,
			:prNUM,
			:daitaiCD,
			:daitai,
			:FAX,
			:keitaiTEL,
			:hopeTIME,
			:hopeTIME2,
			:hopeTIME3,
			:umu,
			:soryo,
			:hatu_kosuu,
			:hakodai,
			:chitai,
			:mail,
			:tesuu,
			:tac,
			:reitou,
			:YahooID,
			:shiwakeCD,
			:kariseikyu,
			:cc_kingaku,
			:st_kingaku,
			:base_soryo,
			:seikyuEND,
			:den_hakoNUM,
			:soukin_tesuuryo;

	// テーブル項目をsplitbDBbuff[][]にセットする
		sf_to_text(No,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,0,DBbuff,5 + 1);
		sf_assgetDB(no,i,1,yubinNo,7 + 1);
		sf_assgetDB(no,i,2,todofuken,24 + 1);
		sf_assgetDB(no,i,3,jusho1,96 + 1);
		sf_assgetDB(no,i,4,jusho2,96 + 1);
		sf_assgetDB(no,i,5,shimei1,96 + 1);
		sf_assgetDB(no,i,6,shimei_f1,192 + 1);
		sf_assgetDB(no,i,7,shimei2,96 + 1);
		sf_assgetDB(no,i,8,shimei_f2,200 + 1);
		sf_assgetDB(no,i,9,TEL,13 + 1);
		sf_assgetDB(no,i,10,hinmei,90 + 1);
		sf_to_text(hinsyu,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,11,DBbuff,3 + 1);
		sf_assgetDB(no,i,12,yoryo,5 + 1);
		sf_assgetDB(no,i,13,hm_kubun,2 + 1);
		sf_assgetDB(no,i,14,grade,144 + 1);
		sf_assgetDB(no,i,15,size,10 + 1);
		sf_to_text(kingaku,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,16,DBbuff,5 + 1);
		sprintf(DBbuff,"%c",jikankibou);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,17,DBbuff,3 + 1);
		sf_to_text(takuhai_s,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,18,DBbuff,3 + 1);
		sprintf(DBbuff,"%c",tuchi);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,19,DBbuff,3 + 1);
		sf_assgetDB(no,i,20,denNo,13 + 1);
		sprintf(DBbuff,"%c",tokui);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,21,DBbuff,3 + 1);
		sf_assgetDB(no,i,22,uketukeDATE,10 + 1);
		sf_assgetDB(no,i,23,expDATE,10 + 1);
		sf_assgetDB(no,i,24,hopeDATE,10 + 1);
		sf_assgetDB(no,i,25,bikou,256 + 1);
		sf_assgetDB(no,i,26,hassoCD,5 + 1);
		sf_assgetDB(no,i,27,hatutenCD,7 + 1);
		sprintf(DBbuff,"%c",hatuEND);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,28,DBbuff,3 + 1);
		sprintf(DBbuff,"%c",prEND);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,29,DBbuff,3 + 1);
		sf_to_text(denNUM,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,30,DBbuff,3 + 1);
		sf_to_text(hakoNUM,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,31,DBbuff,3 + 1);
		sf_assgetDB(no,i,32,memo,1024 + 1);
		sprintf(DBbuff,"%c",reserve);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,33,DBbuff,2 + 1);
		sf_assgetDB(no,i,34,resDATE,10 + 1);
		sprintf(DBbuff,"%c",print);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,35,DBbuff,2 + 1);
		sf_to_text(prNUM,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,36,DBbuff,3 + 1);
		sf_assgetDB(no,i,37,daitaiCD,15 + 1);
		sprintf(DBbuff,"%c",daitai);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,38,DBbuff,2 + 1);
		sf_assgetDB(no,i,39,FAX,13 + 1);
		sf_assgetDB(no,i,40,keitaiTEL,13 + 1);
		sf_to_text(hopeTIME,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,41,DBbuff,3 + 1);
		sf_to_text(hopeTIME2,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,42,DBbuff,2 + 1);
		sf_to_text(hopeTIME3,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,43,DBbuff,3 + 1);
		sprintf(DBbuff,"%c",umu);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,44,DBbuff,3 + 1);
		sf_to_text(soryo,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,45,DBbuff,4 + 1);
		sf_to_text(hatu_kosuu,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,46,DBbuff,4 + 1);
		sf_to_text(hakodai,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,47,DBbuff,4 + 1);
		sf_assgetDB(no,i,48,chitai,60 + 1);
		sf_assgetDB(no,i,49,mail,50 + 1);
		sf_to_text(tesuu,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,50,DBbuff,4 + 1);
		sf_to_text(tac,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,51,DBbuff,4 + 1);
		sprintf(DBbuff,"%c",reitou);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,52,DBbuff,2 + 1);
		sf_assgetDB(no,i,53,YahooID,32 + 1);
		sf_assgetDB(no,i,54,shiwakeCD,13 + 1);
		sprintf(DBbuff,"%c",kariseikyu);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,55,DBbuff,3 + 1);
		sf_to_text(cc_kingaku,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,56,DBbuff,4 + 1);
		sf_to_text(st_kingaku,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,57,DBbuff,4 + 1);
		sf_to_text(base_soryo,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,58,DBbuff,4 + 1);
		sprintf(DBbuff,"%c",seikyuEND);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,59,DBbuff,3 + 1);
		sf_to_text(den_hakoNUM,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,60,DBbuff,3 + 1);
		sf_to_text(soukin_tesuuryo,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,61,DBbuff,4 + 1);

	}
	// カーソルクローズ 
	EXEC SQL CLOSE otodoke_Fetch_CURSOR;

//	DBcount[no] = rec_count;
	DBcount[no] = 1;

	// 結果チェック
	if (sqlca.sqlcode == 0)
	{
		sprintf(tmp,"%s->FETCH結果チェック:%d - %s",COMMANDNAME,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
		logmsg(tmp);
	}
	// 切断
	sqlca.sqlcode = 0;
	EXEC SQL DISCONNECT dbConnection;
	return(0);
}

// DBから入出力(画面)ウインドウに書き込みます。
void otodoke_Fetch_text_writeHTML(int no, int post, int line,int maxcol, char **db_titles)
{
	int i;
	int j;
	int k;
	int x;
	char s[MAX_TMP];

	printf("Content-type: text/html\n\n");
	printf("<html>\n");
	printf("<head>\n");

	printf("<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"); 
	printf("<title>form text_writeHTML page</title>\n");
	printf("</head>\n");
	printf("<body>\n");
	printf("<br>\n");
	printf("<table border=0>\n");
	printf("<tr bgcolor=#ffffff>\n");
	printf("<a href=\"http://codoo2hk.corede.net/takuhai/takuhai.html\" target=\"_parent\">[TOPへ]</a>\n");
	printf("<a href=\"#\" onClick=\"history.back(); return false;\">[前に戻る]</a>\n");
//テーブル名_Menu.pgc のコードを利用する場合、デバッグ時の送信試験はGETを使用すると便利です。
	//送信はPOSTを使用します。
//	printf("<form method=\"post\"  action=\"/cgi-bin/otodoke_Insert.cgi\">\n");
//	printf("<form method=\"get\"  action=\"/cgi-bin/otodoke_Insert.fcgi\">\n");
	printf("<form name=\"form%05d\" method=\"post\"  action=\"/cgi-bin/otodoke_Insert.fcgi\">\n",line);
	printf("<input type=\"submit\" value=\"お届け先の追加を実行\">\n");
	printf("<input type=\"reset\" value=\"元に戻す\">\n");
	sf_tbegin();
	for(i=0,k=0;tp->next !=(TextBox *)NULL;i++,k++)
	{
		printf("<tr>");
		if(ft[no].splitDBbuff[line][i] != (char *)NULL)
		{
			printf("<td>");
			printf("%s:",*(db_titles+i));
			printf("</td>");
			printf("<td>");
//logmsg(*(db_titles+i));
			switch(tp->ftype)
			{
			case TEXT:
				if(ft[no].pos[i].action == 0)
				{
					switch(i)
					{
				//	郵便番号
					case 1:
printf("<script type=\"text/javascript\">\n\n");
printf("<!--\n");
printf("function yubin_search%05d()\n",line);
printf("{\n");
printf("	var zipcode%05d = document.getElementById('zipcode%05d').value;\n",line,line);
printf("	window.open('http://codoo2hk.corede.net/cgi-bin/yubin_Fetch.cgi?field='+zipcode%05d+'&amp;field=%05d'+'&amp;field=2'+'&amp;field=3');\n",line,line);
printf("}\n");
printf("//-->\n");
printf("</script>\n\n");
 
						printf("<input type=\"text\" id=\"zipcode%05d\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\" onchange=\"yubin_search%05d()\">\n",line,k,ft[no].pos[i].len/2,ft[no].splitDBbuff[line][i],ft[no].pos[i].len,line);
						break;

					case 2:
				//	都道府県
						printf("<select name=\"field%02d\" id=\"todofuken\" size=\"1\">\n",k,k);
						printf("<option value=\"\">選択なし</option>\n");
						for(j=0;j<26;j++)
						{
							if(!strcmp(ft[no].splitDBbuff[line][i],prefecture[j]))
							{
								printf("<option value=\"%s\" selected>%s</option>\n",prefecture[j],prefecture[j]);
							}
							else
							{
								printf("<option value=\"%s\">%s</option>\n",prefecture[j],prefecture[j]);
							}
						}
						printf("</select>\n");
						break;

					case 10:
				//	住所１
						printf("<input type=\"text\"  id=\"address\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\">\n",k,ft[no].pos[i].len/2,ft[no].splitDBbuff[line][i],ft[no].pos[i].len);
						break;

					case 25: case 32:
						printf("<textarea  name=\"field%02d\" rows=\"4\" cols=\"%d\" ></textarea><br>\n",i,tp->len/4);
						break;
					case 26:
						printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i],ft[no].pos[i].len);
						break;
					default:
					//	printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i],ft[no].pos[i].len);
						printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
						break;
					}
				}
				else
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" readonly >>\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
				}
				break;
			case NUMERIC:
				if(ft[no].pos[i].action == 0)
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
				}
				else
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" readonly >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
				}
				break;
			case BOOL:
				if(ft[no].pos[i].action == 0)
				{
				//	if(ft[no].splitDBbuff[line][i][0] == 'f')
						printf("<input type=\"checkbox\" name=\"field%02d\" size=\"%d\" value=\"t\">\n",k,ft[no].pos[i].len);
				//	else
				//		printf("<input type=\"checkbox\" name=\"field%02d\" size=\"%d\" value=\"%s\" checked>\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i]);
				//	printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"1\">\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i]);
				}
				else
				{
				//この部分はチェックボックスに使用出来ますが更新や追加には使用出来ません。表示のみに利用可能です。
				//	if(ft[no].splitDBbuff[line][i][0] == 'f')
						printf("<input type=\"checkbox\" name=\"field%02d\" size=\"%d\" value=\"%s\" readonly>\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i]);
				//	else
				//		printf("<input type=\"checkbox\" name=\"field%02d\" size=\"%d\" value=\"%s\" checked readonly>\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i]);
				//	printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"1\" readonly>\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i]);
				}
				break;
			case CURRENCY:
				if(ft[no].pos[i].action == 0)
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"15\" >\n",k,ft[no].pos[i].len);
				}
				else
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"15\" readonly >\n",k,ft[no].pos[i].len);
				}
				break;
			case DOUBLE:
				if(ft[no].pos[i].action == 0)
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
				}
				else
				{
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\" readonly >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
				}
				break;
			default:
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" >\n",k,ft[no].pos[i].len);
				break;
			}
		//	sprintf(s,"%s",ft[no].splitDBbuff[line][i]);
		//	sf_tput(no,i,s);
			sf_tnext();
			printf("</td>");
		}
		else
		{
			sf_tnext();
		}
	}
	printf("</tr>");
	printf("<tr>");
	if(ft[no].splitDBbuff[line][i] != (char *)NULL)
	{
		printf("<td>");
		printf("%s:",*(db_titles+i));
		printf("</td>");
		printf("<td>");
		switch(tp->ftype)
		{
		case TEXT:
			if(ft[no].pos[i].action == 0)
			{
				switch(i)
				{
					case 25: case 32:
						printf("<textarea  name=\"field%02d\" rows=\"4\" cols=\"%d\" ></textarea><br>\n",i,tp->len/4);
						break;
				case 26:
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i],ft[no].pos[i].len);
					break;
				default:
					printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
					break;
				}
			}
			else
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" readonly >>\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
			}
			break;
		case NUMERIC:
			if(ft[no].pos[i].action == 0)
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
			}
			else
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" readonly >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
			}
			break;
		case BOOL:
			if(ft[no].pos[i].action == 0)
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"1\">\n",k,ft[no].pos[i].len);
			}
			else
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"1\" readonly>\n",k,ft[no].pos[i].len);
			}
			break;
		case CURRENCY:
			if(ft[no].pos[i].action == 0)
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"15\" >\n",k,ft[no].pos[i].len);
			}
			else
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"15\" readonly >\n",k,ft[no].pos[i].len);
			}
			break;
		case DOUBLE:
			if(ft[no].pos[i].action == 0)
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" maxlength=\"%d\" >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
			}
			else
			{
				printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"%s\" maxlength=\"%d\" readonly >\n",k,ft[no].pos[i].len,ft[no].pos[i].len);
			}
			break;
		default:
			printf("<input type=\"text\" name=\"field%02d\" size=\"%d\" value=\"\" >\n",k,ft[no].pos[i].len,ft[no].splitDBbuff[line][i]);
			break;
		}
		sprintf(s,"%s",ft[no].splitDBbuff[line][i]);
		sf_tput(no,i,s);
		printf("</td>");
	}
	printf("</tr>");
	printf("</form>\n");
	printf("</body>\n");
	printf("</html>\n");
}
// FETCHした後にHTMLのテーブルにDBの行列を書き込みます。
int otodoke_Fetch_grid_dispHTML(int no, int startline, int maxcol, int grid_ey, int grid_sy)
{
	int i, j;
	char s[MAX_TMP];
	char hs[MAX_TMP];

	if((Fetch_otodoke_Fetch(no)) == 0)
	{
		for(i=startline;i<DBcount[no];i++)
		{
			for(j=0;j<maxcol;j++)
			{
				sf_gputDBHTML(no,i,j,startline,s);
			}
		}

		return(0);
	}
	else
	{
		return(-1);
	}
}

int subformHTML(void)
{
	int i;
	char s[MAX_STR];

	int startline;

	int line;
	setlocale(LC_ALL,"");

//------------------------------------------------------------------------
//下ののハイフンのラインまでの以下の関数はDBと画面の設計を実際のフォームに適用します。
//これらの関数が無い場合は設計情報はDB設計以外は反映されません。

	static int xcol = 242;
	static int rows = 73;

//for label
	static int label_length[] = {0};
	static int label_no[] = {0};
	static int label_align[] = {0};
	static char *label_text[1][128] = {""};
	static int label_x[] = {0};
	static int label_y[] = {0};
//for textbox
//	static int textbox_length[] = {5,7,24,96,96,96,192,96,200,13,90,5,5,2,144,10,15,3,3,3,13,3,10,10,10,209,5,7,3,3,5,5,209,2,10,2,5,5,2,13,13,3,2,3,3,15,6,15,60,50,15,15,2,32,13,3,15,15,15,3,5,15,0};
//	static int textbox_length[] = {5,7,24,64,64,64,64,64,64,13,64,5,5,2,64,10,15,3,3,3,13,3,10,10,10,209,5,7,3,3,5,5,209,2,10,2,5,15,2,13,13,3,2,3,3,15,6,15,60,50,15,15,2,32,13,3,15,15,15,3,5,15,0};
	static int textbox_length[] = {5,7,12,64,64,64,64,64,64,13,64,5,5,2,64,10,15,3,3,3,13,3,10,10,10,209,5,7,3,3,5,5,209,2,10,2,5,5,2,13,13,3,2,3,3,15,6,15,60,50,15,15,2,32,13,3,15,15,15,3,5,15,0};
	static int textbox_no[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,0};
	static int textbox_align[] = {2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,1,2,1,2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,1,2,2,2,2,1,1,1,2,1,1,1,2,2,1,1,2,2,2,2,1,1,1,2,1,1,0};
	static int textbox_action[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	static int textbox_skipstep[] = {1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0};
	static int textbox_field[] = {2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,4,3,2,3,1,3,1,1,1,1,1,1,3,3,2,2,1,3,1,3,2,1,3,1,1,1,1,1,3,4,4,4,1,1,4,4,3,1,1,3,4,4,4,3,2,4,0};
	static int textbox_x[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	static int textbox_y[] = {6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,0};


//for grid
	static int cell_length[] = {2,2,2,2,2,2,3,3,3,3,3,2,2,2,2,2,5,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,2,3,2,3,2,2,3,3,3,2,3,3,4,4,4,3,3,4,4,2,3,3,3,4,4,4,3,3,4,0};
	static int cell_align[] = {2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,1,2,1,2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,1,2,2,2,2,1,1,1,2,1,1,1,2,2,1,1,2,2,2,2,1,1,1,2,1,1,0};
	static int cell_action[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	static int grid_field[] = {2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,4,3,2,3,1,3,1,1,1,1,1,1,3,3,2,2,1,3,1,3,2,1,3,1,1,2,2,2,3,4,4,4,1,1,4,4,3,1,1,3,4,4,4,3,2,4,0};
	static int grid_skipstep[] = {1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0};
	char *grid_titles[63][128] = {"Grid_1","Grid_2","Grid_3","Grid_4","Grid_5","Grid_6","Grid_7","Grid_8","Grid_9","Grid_10","Grid_11","Grid_12","Grid_13","Grid_14","Grid_15","Grid_16","Grid_17","Grid_18","Grid_19","Grid_20","Grid_21","Grid_22","Grid_23","Grid_24","Grid_25","Grid_26","Grid_27","Grid_28","Grid_29","Grid_30","Grid_31","Grid_32","Grid_33","Grid_34","Grid_35","Grid_36","Grid_37","Grid_38","Grid_39","Grid_40","Grid_41","Grid_42","Grid_43","Grid_44","Grid_45","Grid_46","Grid_47","Grid_48","Grid_49","Grid_50","Grid_51","Grid_52","Grid_53","Grid_54","Grid_55","Grid_56","Grid_57","Grid_58","Grid_59","Grid_60","Grid_61","Grid_62",""};
	static char *db_titles[63][128] = {"No","郵便番号","都道府県","住所１","住所２","氏名１","シメイ","氏名２","シメイ２","TEL","品名","品種","容量","品名区分","グレード","サイズ","金額","到着時間希望","宅配業者","お届け通知","伝票番号","得意客","受付日","発送日","希望日","備考","発送元CD","発店CD","発送済","印刷済","伝票番号","箱数","メモ","予約","予約日","印字可","印字枚数","のし","代替","FAX","携帯TEL","希望時間帯１","希望時間帯２","希望時間帯３","お届け通知の有無","送料","発送個数","箱代","地域帯","メール","手数料","消費税額","冷凍と冷蔵","YahooID","仕分CD","仮請求","チルドとクール料金","速達とタイム料金","基本送料","請求書作成済","伝票毎の箱数","送金手数料",0};
//	static char *db_titles[63][128] = {"No","yubinNo","todofuken","jusho1","jusho2","shimei1","shimei_f1","shimei2","shimei_f2","TEL","hinmei","hinsyu","yoryo","hm_kubun","grade","size","kingaku","jikankibou","takuhai_s","tuchi","denNo","tokui","uketukeDATE","expDATE","hopeDATE","bikou","hassoCD","hatutenCD","hatuEND","prEND","denNUM","hakoNUM","memo","reserve","resDATE","print","prNUM","daitaiCD","daitai","FAX","keitaiTEL","hopeTIME","hopeTIME2","hopeTIME3","umu","soryo","hatu_kosuu","hakodai","chitai","mail","tesuu","tac","reitou","YahooID","shiwakeCD","kariseikyu","cc_kingaku","st_kingaku","base_soryo","seikyuEND","den_hakoNUM","soukin_tesuuryo",0};
	static int grid_sx = 0;
	static int grid_ex = 239;
	static int grid_sy = 4;
	static int grid_ey = 4;

	static int maxtext;
	static int no = 0;

	char **gt;
	char **lt;

	gt = (char **)&grid_titles;

	xcol = MakeWsubFormHTML(no,xcol,rows,0,2,grid_sx,grid_sy,grid_ex,grid_ey,cell_length,cell_align,cell_action,grid_field,grid_skipstep,gt,62);

	lt = (char **)&label_text;

//	以下の関数は<SELECT>でコンボボックスを作成する場合、一つのCGIで重複して利用出来ませんのでどちらかを削除してください。
	maxtext = MakeTextBoxHTML(no,0,2,xcol,rows,label_no,lt,label_x,label_y,label_length,textbox_no,textbox_x,textbox_y,textbox_length,textbox_align,textbox_action,textbox_field,textbox_skipstep, 62);

//------------------------------------------------------------------------

	for(i=0;i<MAX_POS;i++)
	{
		memset(gridtitles[i],'\0',MAX_LEN);
	}

	sf_startcell(0);

	startline = 0;	//画面初期行

	if(otodoke_Fetch_grid_dispHTML(no,startline,maxtext,grid_sy,grid_ey) == -1)	//	*splitDBbuff[][]を表示する。
	{
		printf("grid_dispHTML():error\n");
	}

	//DBへの入力用フォームの作成
	for(i=startline;i<DBcount[no];i++)
	{
		otodoke_Fetch_text_writeHTML(no,0,i,maxtext,*db_titles);
	}

}

int main(int argc, char *argv[])
{
	sprintf(COMMANDNAME,"%s",argv[0]);
	while (FCGI_Accept() >= 0) {
		subformHTML();
	}
}

