package Check;
use vars qw(@ISA @EXPORT @EXPORT_OK);

use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
	     element_mandatory_check_self
	     element_mandatory_check_depend
	     );
@EXPORT_OK = qw();

use Bool;
use CfgIO;
use HtmlForm;

#
# 要素が必須項目であるかどうか，条件付き必須も含む．
# elementはreg-cfg.txtで定義する
# 条件は、mandatory フィールドに記述する
# -- '依存するID=左のIDがこの値だったら必須'という形式のAND
#
sub is_element_mandatory {
    my ($eleid, $elestatus) = @_;

    my $name = &get_element_name($eleid);
    if (&get_element_need($eleid) eq 'mandatory') {
		return TRUE;
    }
    else { # 条件付き、
#	print "eleid = ($eleid)";
		my @ele_labels = &get_element_mandatory($eleid);
#	print "ele_labels = (".join(",",@ele_labels),")<br>\n";
		foreach my $ele_label (@ele_labels) {
			my ($depeleid, $label) = split(/=/, $ele_label);
#	    print "($depeleid, $label)<br>\n";
			my $depname = &get_element_name($depeleid);
			my $value = $elestatus->{$depname}{value};
#	    print "--($name, $value)<br>\n";
#	    $elestatus->{$depname}{result} = TRUE if ($value eq $label);
			return TRUE if ($value eq $label);
		}
		return FALSE;
#	$elestatus->{$name}{result} = FALSE;

    }
}


# 2011-04-21 added begin
# 入力してはいけない項目チェック
# 入力してはいなけい項目に入力したらTRUEを返す
sub is_element_nomandatory {
    my ($eleid, $elestatus) = @_;

    my $name = &get_element_name($eleid);
    # 依存する項目が特定の値のときに入力してはいけない項目か？
    # 依存関係は、mandatoryを流用する
    if (&get_element_need($eleid) eq 'nomandatory') {
		my @ele_labels = &get_element_mandatory($eleid);
		foreach my $ele_label (@ele_labels) {
			my ($depeleid, $label) = split(/=/, $ele_label);
			my $depname = &get_element_name($depeleid);
			my $value = $elestatus->{$depname}{value};
			return TRUE if ($value eq $label);
		}

	}
	return FALSE;
}
#end

#
# 必須項目の内容や，必須でない項目でも入力された内容のチェック
#
sub element_mandatory_check_self {
    my ($eleid, $elestatus) = @_;
    my $result;

    my $name = &get_element_name($eleid);
    my $check = &get_element_check($eleid);
    my $value = $elestatus->{$name}{value};
#    print "name = ($name) \n";
    if(&is_element_mandatory($eleid, $elestatus)) {
#	print "...mandatory<br>\n";
        # 項目（value）が指定された形式（check）か？
		$result = &fixed_check($check, $value);
    }
	# 2011-04-21 added begin
	elsif(&is_element_nomandatory($eleid, $elestatus)) {
		$result = FALSE;
	}
    # end
    else {
		$result = ($value) ? &fixed_check($check, $value) : TRUE;
    }
    $elestatus->{$name}{result} = $result;
}

#
# 要素 eleid の依存する要素に不備があれば、eleid 自身も不備であるとする
#  depeleidsには自身が依存するeleidを列挙
#
sub element_mandatory_check_depend {
    my ($eleid, $elestatus) = @_;

    my @depeleids = &get_element_depeleids($eleid);
    next unless (scalar(@depeleids) > 0);
    foreach my $depeleid (@depeleids) {
	if ($elestatus->{$depeleid}{result} == FALSE) {
	    $elestatus->{$name}{result} = FALSE;
	    last;
	}
    }
}

sub fixed_check {
    my ($check, $value) = @_;

#    print "fixed_check ($check)-($value)<br>\n";

    if ($check eq '') {
		return TRUE if (defined($value) && ($value ne 'none' && $value ne ""));
		return FALSE;
    }
    elsif ($check eq 'postcode') {
		return &check_as_postcode($value);
    }
    elsif ($check eq 'phone') {
		return &check_as_phone($value);
    }
    elsif ($check eq 'email') {
		return &check_as_email($value);
    }
    elsif ($check eq 'prefecture') {
		return &check_as_prefecture($value);
    }
    elsif ($check eq 'money') {
		return &check_as_money($value);
    }
    else {
		&syntax_error("This type checking is not supoort ($check)");
    }
}

sub check_as_postcode {
    my ($pcode) = @_;
    unless (length($pcode) == 8) { return FALSE; }
    unless ($pcode =~ /(\d\d\d)-(\d\d\d\d)/) { return FALSE; }
    return TRUE;
}

sub check_as_phone {
    my ($phone) = @_;
    unless ($phone) { return FALSE; }
    if ($phone =~ /[^\d\-\(\)]/) { return FALSE; }
    return TRUE;
}

sub check_as_email {
    my ($email) = @_;
    if ($email =~ /[^\w\d\.\@\-]/) { return FALSE; }
    unless ($email =~ /.+\@.+/) { return FALSE; }
    return TRUE;
}

sub check_as_prefecture {
    my ($pref) = @_;
    unless (int($pref) == $pref) { return FALSE; }
    unless ($pref >= 1 && $pref <= (&get_cityno() - 1)) { return FALSE; }
    return TRUE;
}

# =begin added by ishikawa
sub check_as_money {
    my ($money) = @_;
    if ($money =~ /([\d,]+)([^\d,]+)/) {
        if ($2 eq "円") {
            return TRUE;
        }
        return FALSE;
    }

    return FALSE;
}
# =end added by ishikawa

1;
