#!/usr/bin/perl
##########################################################
# CSVsearch Xtra & CSVread Xtra v 10/Dec/2005
# © 1999-2005 EZscripting.com
# Script by Alexandre Golovkine & Babelnotes.be
##########################################################
# Instructions: http://www.EZscripting.com/csvread/
# Instructions: http://www.EZscripting.com/csvsearch/
# FAQ:          http://www.EZscripting.com/faq/
# Help Forum:   http://www.EZscripting.com/helpforum/
##########################################################
## Template brackets are now [[fieldname]]
my $scriptname                = "csvsearch3.pl";
my $CSV_file                  = "database3.txt";
my $HTML_template	      = "template3.htm";
my $no_matches_found          = "<font face=Verdana size=2><b>Sorry, No results found:</b> Please check that you have not used plurals, eg <em>cd players</em> will not find any matches but <em>cd player</em> will.<br><br></font><font face=Verdana size=2>Note also that general terms such as <em>rattan furniture</em> will not return any results, however <em>rattan chair</em> will find several!<br><br></font><font face=Verdana size=2>For general searches pleaes use the site search, see top left, and below if all else fails you can always try Google:-)</font><br><br><font face=Verdana size=2>Good luck with your search.<br><br></font>";
my $num_random	              = 1;
my $records_per_page          = 6;
my $field_name_records        = 'records';
my $textPrevious 	      = "Previous";
my $textNext	 	      = "Next";
my $pageSelectorRange	      = 15;
my $order_desc                = 1;
my $separator                 = '|'; 
my $min_query_length          = 0;
my $min_query_length_error    = "<br><br><font face=Verdana size=2>Sorry, the minimum number of characters to search is $min_query_length!<br>Use the back button on your browser to try again</font><br><br>";
my $default_show_mode         = 0;
my $kill_image_buttons_value  = 1;
my @DateFields                = ('DATE');
my @no_search_fields          = ();
my @currency_fields           = ();
my $server_timezone_offset    = 0;
##########################################################
my $use_no_matches_template   = 0;
my $no_matches_template       = 'noresults.htm';
##########################################################
# Advanced Settings High Lighting
my $highLighting              = 0;
my @highLightColors           = ("#FFFFC1", "#CEFFCE", "#D7D7FF", "#FFCECE", "#CCCCCC");
my @no_highlight_fields       = ('DATE','ID');
##########################################################
# Advanced Settings auto ID  
my $ID_use                    = 1;
my $ID_field_name             = "ID";
##########################################################
# Advanced Settings Columns
my $use_columns               = 0;
my $columns                   = 2;
my $tableWidth                ="100%";
my $tableBorder               = 0;
my $tableCellpadding          = 6;
my $tableCellspacing          = 0;
my $tableStyle                = "";
my $tableBorderColor          = "teal";
my $tableBgColor              = "#ffffff";
##########################################################
# Advanced Settings Multi-database
my $field_name_database = 'mydatabase';
my %DB=();
$DB{'db1'}  = 'database.txt';
$DB{'db2'}  = 'database2.txt';
$DB{'db3'}  = 'database3.txt';
$DB{'db4'}  = 'database4.txt';
$DB{'db5'}  = 'database5.txt';
##########################################################
# Advanced Settings Multi-template
my $field_name_template = 'mytemplate';
my %TMP =();
$TMP{'tp1'} = 'template.htm';
$TMP{'tp2'} = 'template.htm';
$TMP{'tp3'} = 'template.htm';
$TMP{'tp4'} = 'template.htm';
$TMP{'tp5'} = 'template.htm';
##########################################################
# Advanced Settings Include Pages
my $use_includes        = 0;
my $error_on_missing    = 0;
my %INCL=();
$INCL{'include1'} = "main-menu.htm";
$INCL{'include2'} = "main-menu.htm";
$INCL{'include3'} = "main-menu.htm";
$INCL{'include4'} = "main-menu.htm";
$INCL{'include5'} = "main-menu.htm";
##########################################################
# EZscripting.com © 1999 - 2005
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts
# By downloading and using this script you agree to the terms and conditions
##########################################################
use strict;

open (MYFILE, '>>data.txt');
print MYFILE "3: ";
print MYFILE "$ENV{'QUERY_STRING'}\n";
close (MYFILE); 

print "Content-type: text/html\n\n";
error("Error: Bad record separator!") if !$separator or $separator=~m /["'\.\\\/]/;
my $my_separator=$separator;
$my_separator="\\".$separator if $separator eq '|'or  $separator eq ',' or  $separator eq '.' or  $separator eq '*';
my %DateFields;
for (@DateFields){$DateFields{$_}=1;}
(my $secs,my $mins,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=gmtime(time + ($server_timezone_offset * 3600));
my %INSERT=();
my %FORM=();
my $qs=$ENV{'QUERY_STRING'};
$qs=~s/(\?|\&)(|\w*\.)(x|y)=[^&\Z]*//g if $kill_image_buttons_value;

my $database;
## find database if $field_name_database
if ($field_name_database){
	$database=~m/(.*)/;
	$qs=~s/$field_name_database=([^\Z\&]*?)(\Z|\&)//;
	$CSV_file = $DB{$1} if $1;
	$database = $1 if $1;
}
error("Please, check path to your database file on head of script !") unless -f $CSV_file;
## read db
my @data = read_file($CSV_file);
chomp $data[0];
my @fields= split($my_separator, shift @data);
my $base_length = @data;
error("You have a corrupt data file") if !@fields;
error("Database is empty") if $base_length<1;
error("Problems loading include files") if ((!(load_includes())) && $error_on_missing);

## remove fields that are barred due to security restrictions
my $nbrforbiddenfields = @no_search_fields;
my $use_no_search_fields = 1;
if ($nbrforbiddenfields == 0) 
{ $use_no_search_fields = 0; }
else 
{ $use_no_search_fields = 1; }

my %forbiddenpositions;
if ($use_no_search_fields) 
{
   my $field; my $cnt = 0; my $forbiddenfield; my $line;
   ## check which fields are forbidden
   foreach $field(@fields)
   { foreach $forbiddenfield(@no_search_fields)
     {  if ($field eq $forbiddenfield) { $forbiddenpositions{$cnt} = "F";} }
     if (!defined($forbiddenpositions{$cnt})) { $forbiddenpositions{$cnt} = "P";}
     $cnt++;
   }
}

## check whether some fields should not be highlighted
my $nbrnohlfields = @no_highlight_fields;
my $use_no_highlight_fields = 1;
if ($nbrnohlfields == 0) 
{ $use_no_highlight_fields = 0;}
else 
{ $use_no_highlight_fields = 1;}

my %forbiddenhighlights;
if ($use_no_highlight_fields) 
{
   my $field; my $cnt = 0; my $nohlfield; my $line;
   ## check which fields are forbidden
   foreach $field(@fields)
   { foreach $nohlfield(@no_highlight_fields)
     {  if ($field eq $nohlfield) { $forbiddenhighlights{$cnt} = "N";} }
     if (!defined($forbiddenhighlights{$cnt})) { $forbiddenhighlights{$cnt} = "H";}
     $cnt++;
   }
}

my @conditions=split(/&/,$qs);
my $m=0;
my @header=();
my @search_value=();
my $method;
my $templateF;
my @searchTerms;
if($ID_use && $qs =~m/show=([^\&\Z]+)/){
	my $show= $1;
	if($field_name_template && $qs=~m/$field_name_template=([^\&\Z]+)/){
		$HTML_template=$TMP{$1};
		$templateF=$1;
		error("Please select correct path to your template file")  unless -f $HTML_template;
	}

	$method = "perfect";
	@data = search($show, $ID_field_name);
}
else{
	if($qs =~m/header=([^\&\Z]+)/){push @header,$1;}
	if($qs =~m/method=([^\&\Z]+)/){$method=$1;}
	foreach (@conditions){
		$_ =~ s/%(..)/pack("c",hex($1))/ge;
		(my $name, my $value) = split(/=/, $_);
                if (checkval($_)) 
                {
                   if($name eq 'search'){
                           push(@searchTerms, split(/\+/, $value));
                           minquerylength($value) if length($value) < $min_query_length;
                           @data = search($value, $header[0]);
                           $value=~tr/+/ /;
                           $value=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
                           push @search_value, $value;
                   }
                   elsif($name eq 'header' or $name eq 'method'){}
                   elsif($name eq 'hl') 
                   { if ($value eq 'on') { $highLighting = 1;}
                     elsif ($value eq 'off') { $highLighting = 0;}
                   }
                   elsif($value =~ /ez_range\(([\d\.]+),([\d\.]+)\)/)
                   {  push(@conditions,"$name>=$1");  push(@conditions,"$name<=$2"); }
                   elsif($name eq $field_name_records){$records_per_page=$value;}
                   elsif($field_name_template && $name eq $field_name_template ){
                           $HTML_template=$TMP{$value} if $value;
                           error("Please select correct path to your template file")  unless -f $HTML_template;
                   }
                   elsif($name eq 'order_by' or $name eq 'order' or $name eq 'page'){$FORM{$name} = $value;}
                   elsif($name eq 'display'){@data = random_record();}
                   elsif($_=~/([^=<>!]+)([\!=<>]+)([^=<>!]+)/){@data = search($3, $1, $2); push(@searchTerms,$3);}
                   $m++;
                }
	}
}		
(my $head, my $tmp, my $foot) = get_html($HTML_template);
my $result;
##sort
@data = reverse @data if $order_desc && !$FORM{order_by};
@data=order() if $FORM{order_by};
@data=random_record(1) if $FORM{order} eq 'random';
        
      if ($highLighting) 
      {   my $term; 
          my $cnt = 0;
          foreach $term(@searchTerms)
          {
            my $colorIndex = $cnt % (@highLightColors);
            my $highLightColor = $highLightColors[$colorIndex];
            my $fronthl = '<span style="background-color: '.$highLightColor.'">';
            my $endhl = '</span>';
            
            my $part;
            foreach $part(@data)
            {
              my @parts = split($my_separator, $part);
              my $cell = "";
              my $cnt2 = 0;
              foreach $cell(@parts)
              {  if (($use_no_highlight_fields == 0) || ($forbiddenhighlights{$cnt2} eq "H")) 
                 {  my $temp1 = $cell;my $temp2 = "";
                    my $oldindex = 0;my $i = iindex($temp1, $term);
                    my $pos_in_tot_string = $i;
                    while ($i >= 0) 
                    {  my $pre = substr($temp1, 0, $i);
                       $temp2 .= $pre;
                       if (not_in_tag(substr($cell, 0, $pos_in_tot_string)))
                       { $temp2 .= $fronthl.$term.$endhl;
                       } else 
                       { $temp2 .= $term;}
                       $temp1 = substr($temp1, $i+length($term));
                       $oldindex = $i;
                       $i = iindex($temp1, $term);
                       $pos_in_tot_string = length($temp2) + $i; }
                    $temp2 .= $temp1;
                    $cell = $temp2; }
                 $cnt2++; }
              $part = join($separator, @parts); } 
            $cnt++; } 
          } 

        ## output everything.

	my $p; my $w; my $pages_out;
	my $total = @data;         
	my $end = $FORM{page}*$records_per_page + $records_per_page;
	$end = $total if $end>$total;
        my $tr=0;
   
        for($w=$FORM{page}*$records_per_page; $w<$end;$w++){
		my @line = split($my_separator, $data[$w]);
		$m=0; %INSERT=();
		foreach(@fields){$INSERT{$_} = $line[$m++];}
                if ($use_columns) {
                   $result .= "<tr>" if $tr==0;                        
                   $result.="<td valign=top>".get_record($tmp)."</td>";
                   $result .= "</tr>" if $tr==$columns;                
                   $tr++;  $tr=0 if $tr>=$columns; }
                else {$result.=get_record($tmp);}
        }
        if (($tr>0) && $use_columns){ for($tr..$columns-1){$result .="<td>&nbsp;</td>";}
           $result .="</tr>" if $tr<=$columns; }
        my $pages;
        $qs=~s/page=([^\&\Z]*)//;
        $qs=~s/\&\&/\&/;
        $qs=~s/\&\Z//;
        $qs =~s/"/&#34;/g;
        $qs .="&$field_name_database=$database" if $database;
        $qs .="&$field_name_template=$templateF" if $templateF;
        
        #set page selector
        $pages_out= "&nbsp;<a href=\"$scriptname?$qs&page=".($FORM{page}-1)."\">$textPrevious</a> " if $FORM{page}>0;
	my $pageStart=0;
	my $pageEnd = $total/$records_per_page - 1;
	if($pageSelectorRange){
		$pageEnd = int($pageEnd+0.5);
		$pageStart= $FORM{page}-int($pageSelectorRange/2) if $FORM{page}>int($pageSelectorRange/2);
		$pageEnd=$pageStart+$pageSelectorRange-1 if $pageStart+$pageSelectorRange-1<$pageEnd;
		$pageStart= $pageEnd-$pageSelectorRange+1 if $pageEnd-$pageSelectorRange+1>=0;
	}
	for($pages=$pageStart; $pages <= $pageEnd; $pages++){
		$p=$pages+1;
		if($FORM{page}==$pages){$pages_out.= " $p";}
		else{$pages_out.= qq| <a href="$scriptname?$qs&page=$pages">$p</a>|;}
	}  
	$pages_out.= " <a href=\"$scriptname?$qs&page=".(1+$FORM{page})."\">$textNext</a>" if  $FORM{page} < $total/$records_per_page-1;

$INSERT{'#_matches'} = @data;
$INSERT{'#_total'} = $base_length;
$INSERT{'#_pages'} = $pages;
$INSERT{'#_page_selector'}=$pages_out;
$INSERT{'#_search'} = join(", ", @search_value);
$INSERT{'#_date'}=(1900+$year)."-".($mon+1)."-$mday";
$INSERT{'#_time'}="$hour".":"."$mins".":"."$secs"." GMT";

if ($default_show_mode){
   if ($use_no_matches_template) {
     nomatches();
   } 
   else {
     $result=get_record($no_matches_found)."<br>";
     $INSERT{'#_matches'} = 0;
     $INSERT{'#_total'} = 0;
     $INSERT{'#_pages'} = 1;
     $INSERT{'#_page_selector'}=1;
   }
}
unless (@data){
   if ($use_no_matches_template) {
     nomatches();
   } 
   else {
     $result = get_record($no_matches_found)."<br>";
     $INSERT{'#_page_selector'} = 1 ;
   }
}
my $tbl =" ";
$tbl .= "width=\"$tableWidth\"" if $tableWidth;
$tbl .= " border=\"$tableBorder\"";
$tbl .= " cellpadding=\"$tableCellpadding\"";
$tbl .= " cellspacing=\"$tableCellspacing\"";
$tbl .= " style=\"$tableStyle\"" if $tableStyle;
$tbl .= " bordercolor=\"$tableBorderColor\"" if $tableBorderColor;
$tbl .= " bgcolor=\"$tableBgColor\"" if $tableBgColor;

print get_record($head);
print "<table $tbl>" if $use_columns;
print $result;
print "</table>" if $use_columns;
print get_record($foot);
undef $result; undef $head; undef $foot;
exit;

#########################################################

sub nomatches {
     my $text;
     open(NOMATCH, "< $no_matches_template") or error("No Matches template cannot be found.");
     while (<NOMATCH>) { $text .= $_; }
     close (NOMATCH);
     $text =~ s{\[\[\s*(.*?)\s*\]\]}{exists($INCL{$1}) ? $INCL{$1} : "[[".$1."]]"}gsex if $use_includes;
     $text =~ s{\[\[\s*(.*?)\s*\]\]}{exists($INSERT{$1}) ? $INSERT{$1} : ""}gsex;
     print $text;
     exit;
}

sub minquerylength {
        (my $searchval) = @_;

        (my $head, my $tmp, my $foot) = get_html($HTML_template);
   	$result=get_record($min_query_length_error)."<br>";
	$INSERT{'#_matches'} = 0;
	$INSERT{'#_total'} = $base_length;
	$INSERT{'#_pages'} = 1;
	$INSERT{'#_page_selector'}=1;
        $INSERT{'#_search'} = $searchval;
        $INSERT{'#_date'}=(1900+$year)."-".($mon+1)."-$mday";
        $INSERT{'#_time'}="$hour".":"."$mins".":"."$secs"." GMT";
        print get_record($head), $result, get_record($foot);
        undef $result; undef $head; undef $foot;
        exit;
}

sub iindex {
   (my $string, my $searchingfor) = @_;

   my $idx = index(uc($string), uc($searchingfor));
   if ($idx >= 0)
   {
     $_[1] = substr($string, $idx, length($searchingfor));
   }
   return $idx;
}


sub not_in_tag
{
  (my $string) = @_;
  
  my @arr = split(//, $string);
  my $num_tag_open = grep(/</, @arr);
  my $num_tag_close = grep(/>/, @arr);
  
  return ($num_tag_open == $num_tag_close);
} 

#########################################################

sub order{
	my @myArr=();
	my @new=();
	my %myHash=();
	my $position;
 	my $i=0; my $ind; my $key;
 	foreach(@fields){$position=$i if $_ eq $FORM{order_by}; $i++;}
	$i=0;
 	foreach(@data){
 		my @line=split($my_separator,$_);
 		chomp $line[$position];
 		$line[$position] = modify_date($line[$position], 1) if $DateFields{$FORM{order_by}};
 		$ind = "$line[$position].$i";
 		$myHash{"$ind"} = $i;
 		$myArr[$i] = "$ind";
 		$i++;
 	}
 	if($FORM{order} eq '123' or $FORM{order} eq '321'){
           if (in_array($FORM{order_by},@currency_fields)) 
           { @myArr= sort {remove_alpha($a) <=> remove_alpha($b)} @myArr;
           } else { @myArr= sort { $a <=> $b } @myArr; }
        }   
 	else{@myArr = sort {lc  $a cmp lc $b } @myArr;}
 	@myArr = reverse  @myArr if $FORM{order} eq '321' or $FORM{order} eq 'cba';
 	$i=0;
 	foreach $key (@myArr){$new[$i++]= @data[$myHash{$key}];
 	}
 	return @new;
} 

sub random_record{
	my @new_data=();
	my $my_num_random;
        error("Can't display less than 1 random records. Set \$num_random to a value greater than 1.") if $num_random < 1;
        if($_[0] or $num_random > @data){$my_num_random=@data;}
        else{$my_num_random=$num_random;}
	my $tmp; my %uniq;
        for(0..$my_num_random-1){
        	$tmp=int(rand(@data));
        	redo if $uniq{$tmp};
        	$new_data[$_]=$data[$tmp];
        	$uniq{$tmp} = 1;
        }
	return @new_data;
}

sub checkfields
{
   my @fields = @_;
   my $field;  my $forbiddenfield;
   foreach $field(@fields)
   {
     foreach $forbiddenfield(@no_search_fields)
     {
        if ($field eq $forbiddenfield) 
        {
           return_no_results();
        }
     }
   }
}

sub return_no_results {
  if ($use_no_matches_template) {
     nomatches();
  } 
  else {
    (my $head, my $tmp, my $foot) = get_html($HTML_template);
    $result=get_record($no_matches_found)."<br>";
    $INSERT{'#_matches'} = 0;
    $INSERT{'#_total'} = 0;
    $INSERT{'#_pages'} = 1;
    $INSERT{'#_page_selector'}=1;
    print get_record($head), $result, get_record($foot);
  }
  exit;
}

sub search{
	my $word=shift;
	my $field=shift;
	my $action=shift;
        my @new_data=();
        $word=~tr/+/ /;
        $word=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;
        #$word=~s/\*/\\*/g;

	if ($DateFields{$field}){$word=modify_date($word);}	
	my $position=-1;
	my $m=0;
	if($field)
        {
          if ($use_no_search_fields) 
          { 
            checkfields($field); 
          }
	  $field=~tr/+/ /;
	  $field=~s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex($1))/eg;  	
	  foreach(@fields){$position=$m if $_ eq $field; $m++; }
          if ($method eq 'start') 
          {
             my $entry;
             foreach $entry(@data)
             {
                my @line = split($my_separator, $entry);
                if ($line[$position] =~ /^$word/i)
                {
                   push(@new_data, $entry)
                }
             }
             $default_show_mode = 0;
             return @new_data;
          }
	}
        if ($method eq 'start') { return_no_results();}
	my %match;
	$word =~s/ +/ /g;
	my @keys; my $key; my $record;
	if($word=~m/\A"(.*)"\Z/){$keys[0]=$1;}
	elsif(!$action and ($method eq "perfect" or $method eq "exact")){$keys[0]=$word;}
	else{@keys= split(" ", $word);}
	if($action eq '!='){for(0..@data-1){$match{$_} = 1;}}
	foreach $key (@keys){
		$m=0;
		foreach $record (@data){
			chomp $record;
			$record =~ s/&#34;/"/g;
			$record =~ s/&#39;/'/g;
			my @line = split($my_separator, $record);
			if($field && $position>-1){
                                my $iscurrency = in_array($field, @currency_fields);
				if($DateFields{$field} ){
					$line[$position]=modify_date($line[$position]); 
					unless ($line[$position]){$m++; next};
				}
				if($action eq '!='){
				  $match{$m} = 0 if $line[$position]=~m/$key/i;
				}
				elsif($action eq '>='){
                                  if ($iscurrency) {
                                     $match{$m} = 1 if remove_alpha($line[$position])>=$key;
                                  } else { $match{$m} = 1 if $line[$position]>=$key; }
                                }
				elsif($action eq '<='){
                                  if ($iscurrency) {
                                     $match{$m} = 1 if remove_alpha($line[$position])<=$key;
                                  } else { $match{$m} = 1 if $line[$position]<=$key; }
                                }
				elsif($action eq '>'){
                                  if ($iscurrency) {
                                     $match{$m} = 1 if remove_alpha($line[$position])>$key;
                                  } else { $match{$m} = 1 if $line[$position]>$key; } 
                                }
				elsif($action eq '<'){
                                  if ($iscurrency) {
                                     $match{$m} = 1 if remove_alpha($line[$position])<$key;
                                  } else { $match{$m} = 1 if $line[$position]<$key; } 
                                }
				elsif($action eq '!!'){
                                  if ($iscurrency) {
                                     $match{$m} = 1 if remove_alpha($line[$position])!=$key;
                                  } else { $match{$m} = 1 if $line[$position]!=$key; } 
                                }
				elsif($method eq "perfect"){
                                  $match{$m} = 1 if $line[$position] eq $key;
                                }
				else{$match{$m}++ if $line[$position]=~m/\Q$key/i;}
			}
			else{   my $cnt = 0;
				foreach(@line)
                                {
                                        if (($forbiddenpositions{$cnt} eq 'P') || $use_no_search_fields == 0) 
                                        {
					  if($method eq "perfect"){if($_ eq $key){$match{$m}++; last;}}
					  elsif ($_=~m/\Q$key/i){$match{$m}++; last;}
                                        }
                                        $cnt++;
		            }
			}
			$m++;
		}
	}
	$m=0; my $k=0;
	foreach(@data){
		$match{$m} = 0 if $match{$m}<@keys && $method eq "all";
		$new_data[$k++] = $_ if $match{$m};
		$m++;
	}
	$default_show_mode=0;
	return @new_data;
}


sub modify_date{
	my $date = shift; 
	my $switch = shift; 
	my $time;
	##date format 10:16:47 2003-10-19
	if ($date=~m/(\d+):(\d+):(\d+) (\d\d\d\d)-(\d+)-(\d+)/){
		if($switch){return 0 if $1>23 or $2>59 or $3>59;}
		return 0 if $6>31 or $5>12;
		$time = sprintf("%04d%02d%02d%02d%02d%02d",$4,$5,$6,$1,$2,$3);
		return $time;
	}
	##date format 2003-10-29 2003.10.29 2003/10/29
	elsif($date=~m/(\d\d\d\d)[-\/\.](\d+)[-\/\.](\d+)/){
		return 0 if $2==0 or $3>31;
		$time = sprintf("%04d%02d%02d",$1,$2,$3)."000000";
		return $time; 
	}
	elsif($date =~m/TODAY/i){
		my ($Tday, $Tmonth, $Ttime)= ($mday, $mon+1, "000000");
		$Ttime="235959" if $switch;
		$time = sprintf("%04d%02d%02d",1900+$year,$Tmonth,$Tday).$Ttime;
		return $time;		
	}
	elsif($date =~m/NOW/i){ 
		my ($Tday, $Tmonth, $Thour,$Tmins,$Tsec)= ($mday, $mon+1, $hour, $mins, $secs);
		$time = sprintf("%04d%02d%02d%02d%02d%02d",1900+$year,$Tmonth,$Tday,$Thour,$Tmins,$Tsec);
		return  $time;
	}	
	else{ return 0;}  	
}
sub in_array {
   (my $name, my @array) = @_;
   my $field;
   foreach $field(@array)
   { if ($field eq $name) { return 1;}
   } return 0;
} 
sub checkval {
   (my $str) = @_;

   my $name;  my $value;
   ($name, $value) = split(/=/, $str);
   if (defined($value) && !($value =~ m/^\s*$/))
   { return 1;}
   else {
     ($name, $value) = split(/</, $str);
     if (defined($value) && !($value =~ m/^\s*$/))
     { return 1; }
     else {
        ($name, $value) = split(/>/, $str);     
        if (defined($value) && !($value =~ m/^\s*$/)) 
        { return 1;}
        else { return 0;}
     }
   }
}
sub remove_alpha {
   (my $v) = @_;
   $v =~ s/,//g;
   return $v;
}
sub get_record{
	my $text = $_[0];
        $text =~ s{\[\[\s*(.*?)\s*\]\]}{exists($INCL{$1}) ? $INCL{$1} : "[[".$1."]]"}gsex if $use_includes;
	$text =~ s{\[\[\s*(.*?)\s*\]\]}{exists($INSERT{$1}) ? $INSERT{$1} : ""}gsex;
	return $text;
}
sub get_html{
	my @txt = read_file($_[0]);
	my $txt;
	foreach(@txt){$txt.=$_;}
	$txt=~/(.*)<template>(.*)<\/template>(.*)/s;
	error("Template-tag not found!") if !$1 or !$2;
	return ($1,$2,$3);
}
sub read_file{
	open(F, $_[0]) || error("Can't open file $_[0]!");
	my @data = <F>;
	close F;
	return @data;
}
sub error{
	print "<html><head><title>Error</title></head><body><br><br><br><font color=red><h3>$_[0]</h3></font></body></html>";
	exit;
}
sub load_includes {
   return 1 if ($use_includes == 0);
   my @incs = keys(%INCL);  my $key;  my $retval = 1;
   foreach $key(@incs)
   {
     open(INCF, "< $INCL{$key}") or ($retval = 0);
     my @incfile = <INCF>; close(INCF);
     $INCL{$key} = join('', @incfile);
   }
   return $retval;
}

##########################################################
# EZscripting.com © 1999 - 2005
# The scripts are available for private and commercial use
# Once purchased this script can be used in any website you build personally
# You may not sell the script in any format to anybody
# The scripts may only be distributed by EZscripting.com
# Do not post or email all or part of the this code in any form whatsoever
# The redistribution of modified versions of the scripts is prohibited
# EZscripting.com accepts no responsibility or liability
# whatsoever for any damages however caused when using our services or scripts
# By downloading and using this script you agree to the terms and conditions
##########################################################
