package input;
use mmt;
#use CGI qw/:standard :html4/;
use CGI qw/:standard :xhtml/;
@ISA = qw(mmt);

#---------------------------------------------------------------#
sub key_display{
#---------------------------------------------------------------#

=head2 key_display キー項目画面作成

キー項目の入力用画面を作成する。

=cut

	my $s = shift;
	my $onLoadsub = "onloadsub();";
	$s->{m}->{title} = $s->{m}->{table} unless(exists $s->{m}->{title});
	print $s->{in}->header(-type=>"text/html; charset=euc-jp"),
		$s->{in}->start_html(-title=>$s->{m}->{title} ." - ".
				$s->{in}->url(),
			-style=>[{'src'=>$s->{cons}->{css}.'sne.css'},
						{'src'=>$s->{cons}->{css}.'calendar.css'},
					],
			-script=>[{-language=>'JavaScript',
					-src=>$s->{cons}->{js}.'prototype.js'},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}yahoo.js"},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}dom.js"},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}event.js"},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}calendar.js"},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}initcompletion.js"},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}complete.js"},
			{-language=>'JavaScript',-src=>"$s->{cons}->{js}ecl_new.js"},
				],
			-onLoad=>$onLoadsub."document.form.$s->{next}.focus();document.form.$s->{next}.select();");
	my $rand = $s->{dbh}->random_str() . "=1";
	my $url = $s->{in}->url();
	$url =~ s/\.cgi/\.cgi\?$rand/;
	print $s->{in}->start_form(-name=>'form',-method=>'post',
			-onSubmit=>"submitsub();return false;",
			-action=>$s->{in}->url());
	print qq{<input type="hidden" name="table" value=$s->{m}->{table}>};
	print $s->{in}->hidden(-name=>'next'),"\n";
	if(defined $s->{m}->{timestamp}){
		print $s->{in}->hidden(-name=>'timestamp');
	}
	print h2($s->{m}->{title});
	$s->addScript();
	print $s->formScript({'a5'=>$s->{m}->{editA5},
				'f1'=>$s->{m}->{editF1}});
	$s->make_key_disp();
	$s->make_get_button_disp();
}
sub make_key_disp{
	my $s = shift;
	print "<table border=1>\n";
        my $i = 0;
	for(@{$s->{m}->{key}}){
		print Tr(th("<div id=_l_key$i>" . $s->Label($_) . "</div>"),
			td([$s->input_field($i,$_,'key') .
			$s->{cons}->{explain}->{$s->{m}->{-explain}}->{$_},
			"<div id=kdisp$i>".
			$s->{m}->{d}->{$_}."</div>" .
			"<div id=cal_key$i style='position:absolute;display:none'></div>"
			])),
			"\n";
		$i++;
	}
	print "</table>";
}
sub	make_get_button_disp{
	my $s = shift;
	print	
		$s->{in}->submit(-name=>'action',
			-value=>$s->{msg}->{action}->[6],
			-tabindex=>1,
			-onFocus=>qq{document.form.next.value="action[1]"}),
		$s->{in}->submit(-name=>'action',
			-value=>$s->{msg}->{action}->[5],
			-tabindex=>1,
			-onFocus=>qq{document.form.next.value="action[1]"}),
		"[<span -class=err id=errstr>$s->{errstr}</span>]",
		"<hr>\n";
}
sub data_display{
	my $s = shift;
	my ($line,$item);
	$s->{rwt}->{seq_no} = 0;
	for(1 .. $s->{rwt}->{maxline}){
		$s->{rwt}->{seq_no}++;
		$line .= $s->rwt_dtail();
	}
	$s->item_line($line);
}
sub display{
	my $s = shift;
	my ($line,$item);
	$s->{next} = 'key0';
	$item = $s->list_out() if($s->{in}->param('action') eq 
					$s->{msg}->{action}->[6]);
	$s->{next} = $s->{item_s}||"item0_1" if($item);
	$s->key_display();
	$s->item_line($item);
	$s->up_load_form();
	$s->sub_window();
}
sub item_line{
	my $s = shift;
	my $item = shift;
	$s->{item_width} = 32;
	$s->{item_height} = 277 unless($s->{item_height});
	$line = "<table border=1>\n<tr>";
	$line .= th({-width=>'32px',-nowrap},['NO']);
	my $width;
	for(@{$s->{m}->{item}}){
		$width = $s->{m}->{width}->{$_} || '100';
		next if($width < 0);
		$line .= th({-width=>$width .'px',-nowrap},
			[$s->Label($_)]);
		$s->{item_width} += $width;
	}
	$line .= th({-width=>'16px',-nowrap},[' ']);
	$s->{item_width} += 16;
	$line .= "</tr></table>\n";
	print "<div style='width:$s->{item_width}px'>\n";
	print $line;
	print "</div>\n";
	print "<style media=screen>";
	print ".detail{ width:$s->{item_width}px; height:$s->{item_height}px; overflow-y:scroll; }";
	print "</style>";
	print "<style media=print>";
	print ".detail{ width:$s->{item_width}px; }";
	print "</style>";
	print "<div class=detail>";
	print "<table border=1>\n";

	print $item;
	print "</table></div>\n";
	$s->syori_button();
	print "</form>\n";
}
sub syori_button{
	my $s = shift;
	print $s->{in}->submit(-name=>'action',-value=>$s->{msg}->{action}->[1],
			-tabindex=>$s->{tabindex}->{action} || 1,
			-onFocus=>qq{document.form.next.value="action"},
			-onBlur=>qq{document.form.next.value="key0"});
	print $s->{in}->submit(-name=>'action',-value=>$s->{msg}->{action}->[4],
			-tabindex=>$s->{tabindex}->{action} || 1,
			-onFocus=>qq{document.form.next.value="action"},
			-onBlur=>qq{document.form.next.value="key0"}),
		$s->{dbh}->menu();
}
sub data_get{
}
sub list_out{
	my $s = shift;
	my $in = $s->{in};
	my $dbh = $s->{dbh};
	my ($sql,$sth,$ref,$line);
	$s->{rwt}->{sth} = $s->rwt_init_prepare();
	$s->{rwt}->{sth}->execute();
	$s->{rwt}->{seq_no}=0;
	while($s->{rwt}->{ref} = $s->rwt_dataget()){
		$s->{rwt}->{line} = [];
		$s->{rwt}->{rskip} = 0;
		$s->{rwt}->{endsw} = 0;
		$s->rwt_tisel($s);
		if($s->{rwt}->{endsw} != 0){
			$ref = undef;
			last;
		}
		next if($s->{rwt}->{rskip} == 1);
		$s->{rwt}->{seq_no}++;
		$line .= $s->rwt_dtail($s);
	}
	while($s->{rwt}->{seq_no}++ < $s->{rwt}->{maxline}){
		$line .= $s->rwt_dtail($s);
	}
	return $line;
}
sub rwt_dtail{
	my $s = shift;
	my ($line,$line2);
	my $ref = $s->{rwt}->{ref};
	my $i = -1;
	my $j = $s->{rwt}->{seq_no};
	$line = "<tr>" . td({-width=>'32px',-nowrap=>'',-align=>'center'},
			[$s->{rwt}->{seq_no} .
			"<input type=hidden name=_no value=${j}>"]);
	my ($width,$hidden);
	for(@{$s->{m}->{item}}){
		$i++;
		$width = $s->{m}->{width}->{$_} || '100';
		$hidden = qq{<input type=hidden name=item${i}_${j} value="$ref->{$_}"><span id="disp${i}_${j}">};
		if($width < 0){
			if($s->{m}->{disp}->{$_} =~ /hidden/){
				$line =~ s|</td>$||;
			}else{
				$line =~ s|</td>$|<br>|;
			}
		}
		if(! $s->{m}->{disp}->{$_}){
			$line2 = td({-width=>$width .'px',
				-nowrap=>'',-align=>'left',
						$s->{rwt}->{opt}->{$_}},
				$s->input_field($i,$_,$s->{rwt}->{seq_no}));
		}elsif($s->{m}->{disp}->{$_} =~ /hidden/){
			$s->{in}->param("item${i}_${j}","$ref->{$_}") if($ref->{$_} ne '');
			$hidden = $s->{in}->hidden(-name=>"item${i}_${j}") . qq{<span id="disp${i}_${j}">};
			$line2 = $hidden ."</span></td>";
		}elsif($s->{rwt}->{edit}->{$_} =~ /^[0-9a-f][0-9a-f]$/i){
			$line2 = td({-width=>$width .'px',
				-nowrap=>'',-align=>'right',
						$s->{rwt}->{opt}->{$_}},
							$hidden .
							$s->group_inji($_) .
							"</span>");
		}else{	$line2 = td({-width=>$width .'px',
				-nowrap,
						$s->{rwt}->{opt}->{$_}},
							$hidden .
							$s->group_inji($_) .
							"</span>");
		}
		if($width < 0){
			$line2 =~ s|<td[^>]*>||;
		}
		$line .= $line2;
	}
	$line .= "</tr>\n";
	return $line;
}
sub addScript{
	my $s = shift;
	my $script;
	$script = <<END;
function evntsub(name,func){
	if(ret = name.name.match(/(.*)_(.*)/)){
		funcName = ret[1] + "_"+func+"(name,ret);";
		Try.these(
			function (){eval(funcName)}
		);
	}
}
function addZero(n,s,a){
	var s1 = new Array();
	var sign = 1;
	var aa = 0;
	if(a == 1){ aa = 0.5;}
	if(isNaN(n)|| n == -Infinity|| n == Infinity){
		n=0;
	}
	if(n < 0){
		n = n * -1;
		sign = -1;
	}
	var n2 = Math.floor(n * Math.pow(10,s)+aa);
	for(;s >= 1;s--){
		s1.push(n2%10);
		n2 = (n2 - s1[s1.length-1])/10
	}
	n2=n2*sign;
	return n2+'.'+s1.reverse().join('');
}

if (typeof SAMPLE == "undefined") {
    var SAMPLE = {};
}
SAMPLE.isMousedown = false;
SAMPLE.Value = "undefined";
SAMPLE.dblclick = function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
	SAMPLE.Value = target.value;
}
SAMPLE.mousedown = function(e) {
    SAMPLE.isMousedown = true;
}
SAMPLE.mouseup = function(e) {
    SAMPLE.isMousedown = false;
	SAMPLE.Value = "undefined";
}
SAMPLE.mouseover = function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    if (SAMPLE.isMousedown) {
        if (target.type == "text") {
			if(SAMPLE.Value != "undefined"){
           		target.value = SAMPLE.Value;
			}
        }
    }
}
Event.observe(document, "dblclick", SAMPLE.dblclick);
Event.observe(document, "mousedown", SAMPLE.mousedown);
Event.observe(document, "mouseup", SAMPLE.mouseup);
Event.observe(document, "mouseover", SAMPLE.mouseover);



END
	$s->{script} .= $script;
}

sub input_field{
	my $s = shift;
	my $i = shift;
	my $name = shift;
	my $j = shift || 0;
	my $style = 'text-align: left';
	my $addComma = "evntsub(this,'Change');";
	my $item = 'item';
	my $jj;
	my $tabindex = 1;
	$tabindex = $s->{tabindex}->{$name} || 1;
	my	$override = 1;
	if($j eq 'key'){
		$item = 'key';
		$jj = '';
		$override = 0;
	}else{
		$item = 'item';
		$jj = "_$j";
		$override = 1;
	}
	$override = $s->set_override($override,$i,$name,$j);
	if($s->type_numeric($name)){
		$style = 'text-align: right';
	}
	if(grep {"$item$i" eq $_} @{$s->{m}->{editA5}}){
		$addComma .= "this.value=addComma(this.value)";
	}
	if(grep {"$item$i" eq $_} @{$s->{m}->{editF1}}){
		$addComma .= "this.value=edit_F1(this.value)";
	}
	if($s->{m}->{$name}->{Type} eq 'text'){
		return $s->{in}->textarea(-name=>"$item$i$jj",-rows=>5,-cols=>70,
			-onFocus=>qq{document.form.next.value="$item$i"},
			-tabindex=>$tabindex);
	}elsif($s->{m}->{$name}->{Type} =~ /enum\((.*)\)/){
		return $s->{in}->popup_menu(-name=>"$item${i}${jj}",
			-values=> [map {s/'//g;$_} split /,/,$1],
			-tabindex=>$tabindex);
	}else{	return $s->{in}->textfield(-name=>"$item${i}${jj}",
			-size=>int($s->{m}->{$name}->{Size}*1.3),
			-maxlength=>$s->{m}->{$name}->{Size},
			-default=>$s->{rwt}->{ref}->{"$name"},
			-override=>$override,
			-style=>$style,
			-onChange=>$addComma,
			-onBlur=>"evntsub(this,'Blur');",
			-tabindex=>$tabindex,
			-onFocus=>qq{document.form.next.value="$item$i";evntsub(this,'Focus');});
	}
}
sub set_override{
	my $s = shift;
	my ($override,$i,$name,$j) = @_;
	return $override;
}

1;

