
use Notepad;
my $Notepad = Notepad->new($db, $admin, \%conf);





#************************************************************
#
#************************************************************	
sub notepad_new {
  my ($attr) = @_;

  if ($attr->{AID}) {
    $Notepad->notepad_new( $attr );
    if ($Notepad->{TOTAL} && $Notepad->{TODAY} + $Notepad->{ACTIVE} > 0) {
      return "(". (($Notepad->{TODAY}) ? $html->color_mark($Notepad->{TODAY}, $_COLORS[6]) : $Notepad->{TODAY} )."/$Notepad->{ACTIVE})";
     }
   }

  return '';	
}



#************************************************************
#
#************************************************************	
sub notepad_main {
@STATUS = ("$_ACTIVE", "$_CLOSED", "$_INWORK");


	if ($FORM{message}) {				
		$html->message('info', $_INFO, "$FORM{message}");	
	}

my $status_bar = msgs_status_bar();
	print $html->br();
# TESTING 
$list_of_notice = $Notepad->notepad_notice({});

foreach my $line ( @$list_of_notice ) {
	my $popup_title_new = $line->[4]; 
	$popup_title_new =~ s/\'/\\\'/g;
	$popup_title_new =~ s/\"/\\\"/g;
	my $popup_text = $line->[5]; 
	$popup_text =~ s/\'/\\\'/g;
	$popup_text =~ s/\"/\\\"/g;
	$notice .= 'setNotepadNotice(' . $line->[1] . ', \'' . $popup_text . '\', \'' . $popup_title_new . '\', \''. $line->[8]. '\');' . "\n"; 
}
	
$html->tpl_show(_include('notepad_popupwindow', 'Notepad'), { 	 
														NOTICE => $notice, 
														 
														 });
	
	

# /TESTING 	
	if(!defined($FORM{NOTE_STATUS})) {
		
		$FORM{NOTE_STATUS} = 'ALL';
		
	}
	
	my $table = $html->table(	{	width      => '100%',
									caption    => $_NOTEPAD,
									border     => 1,
									title      => [$_DATE. '/' . $_TIME, $_ADDED ,$_STATUS, $_SUBJECT, '-', '-'],
									cols_align => ['left', 'right', 'right', 'right', 'center', 'center'],
									pages      => $Storage->{TOTAL},
									header		=> 
	(($FORM{NOTE_STATUS} eq 'ALL') ? $html->b($_ALL) : $html->button($_ALL, "index=$index&NOTE_STATUS=ALL", { BUTTON => 1 })) . ' ' .
	(($FORM{NOTE_STATUS}==0 and $FORM{NOTE_STATUS} ne 'ALL') ? $html->b($_ACTIVE) : $html->button($_ACTIVE, "index=$index&NOTE_STATUS=0", { BUTTON => 1 })) . ' ' . 
	(($FORM{NOTE_STATUS}==1) ?	$html->b($_CLOSED) : $html->button($_CLOSED, "index=$index&NOTE_STATUS=1", { BUTTON => 1 })) . ' ' .
	(($FORM{NOTE_STATUS}==2) ? $html->b($_INWORK) : $html->button($_INWORK, "index=$index&NOTE_STATUS=2", { BUTTON => 1 })),           
									ID         => 'NOTEPAD_ID'
								} 
	);			
	
	if ($FORM{NOTE_STATUS} eq 'ALL') {
			$FORM{NOTE_STATUS} = undef;
	}
		
	$list = $Notepad->notepad_list_notes( { AID => $admin->{AID}, NOTE_STATUS => $FORM{NOTE_STATUS} });
	foreach my $line ( @$list ) {
		$table->addrow(	$line->[1],  
						$line->[2], 
						$STATUS[$line->[3]],
						$line->[4],  
						$html->button($_SHOW, "index=$index&chg=$line->[0]", { CLASS => 'show' }), 
						$html->button($_DEL, "index=$index&del=$line->[0]", { MESSAGE => "$_DEL $_SUPPLIER $line->[4]?", CLASS => 'del' })
		);
	}
	print $table->show() . $html->br();

	if ($FORM{chg}) {
		$Notepad->{ACTION}='change';
		$Notepad->{ACTION_LNG}=$_CHANGE;
		$Notepad->notepad_note_info({ ID => $FORM{chg},});
		$Notepad->{STATUS}=$html->form_select("STATUS", 
							{	SELECTED        => $Notepad->{STATUS},
								SEL_ARRAY		=> [ @STATUS ], 
								ARRAY_NUM_ID 	=> 0,
								NO_ID			=> 1
							});	
		
		if (! $Notepad->{errno}) {
			$html->message('info', $_INFO, "$_CHANGING");	
		}
	}
	
	if (!$FORM{chg}) {
		
		if (!$FORM{change}) {
			$Notepad->{ACTION}='add_main';
			$Notepad->{ACTION_LNG}=$_ADD;
		} else {
			$Notepad->{ACTION}='change';
			$Notepad->{ACTION_LNG}=$_CHANGE;
		}
		
		$Notepad->{NOTIFIED}='00:00:00';
		$Notepad->{STATUS}=$html->form_select("STATUS", 
							{	SELECTED		=> $Notepad->{STATUS},
								SEL_ARRAY		=> [ @STATUS ], 
								ARRAY_NUM_ID 	=> 0,
								NO_ID			=> 1
							});
	}
my $date .=	$html->date_fld2('DATE', { FORM_NAME => 'notepad_form', WEEK_DAYS => \@WEEKDAYS, MONTHES => \@MONTHES, DATE => $Notepad->{DATE}});

	
	
	if(!$FORM{add_main} and  !$FORM{change}) {
	
		$html->tpl_show(_include('notepad_main_form', 'Notepad'), {	%$Notepad,
															  		ALIGN => 'right',
															  		DATE => $date,														  
															  	
															  	});
	}
	if ($FORM{add_main}) {
		
		if ($FORM{TEXT} ne '') {
			if (! $FORM{SUBJECT}) {
	            $FORM{SUBJECT}=substr($FORM{TEXT}, 0,20).'...';
	     	}
		
			$Notepad->notepad_add_note({ %FORM });
			if (! $Notepad->{errno}) {
				$html->tpl_show(_include('notepad_redirect', 'Notepad'), { 
						SECTION => '',
						MESSAGE => "$_ADDED",
					
					});		
			}
		}
		else {
			
			
			
			$html->message('info', $_INFO, "$ERR_FIELDS_FOR_NOTIFIED_AND_SUBJECT_ARE_REQUIRED");
			$FORM{STATUS}=$html->form_select("STATUS", 
				{	SELECTED		=> $FORM{STATUS},
					SEL_ARRAY		=> [ @STATUS ], 
					ARRAY_NUM_ID 	=> 0,
					NO_ID			=> 1
				});
			$html->tpl_show(_include('notepad_main_form', 'Notepad'), {%$Notepad, 
																		%FORM,
																		DATE => $date
																		});
		
		}
	}

	elsif ($FORM{change}) {
		#$Notepad->{debug} = 1;
		if ($FORM{TEXT} ne '') {
			if (! $FORM{SUBJECT}) {
	            $FORM{SUBJECT}=substr($FORM{TEXT}, 0,20).'...';
	     	}
	     	
			$Notepad->notepad_note_change({ %FORM });
			if (! $Notepad->{errno}) {
	        $html->tpl_show(_include('notepad_redirect', 'Notepad'), { 
						SECTION => '&chg=' . $FORM{ID},
						MESSAGE => "$_CHANGED",
					});			
	    	}
		}
		else {
	
			$html->message('info', $_INFO, "$ERR_FIELDS_FOR_NOTIFIED_AND_SUBJECT_ARE_REQUIRED");
			$FORM{STATUS}=$html->form_select("STATUS", 
				{	SELECTED		=> $FORM{STATUS},
					SEL_ARRAY		=> [ @STATUS ], 
					ARRAY_NUM_ID 	=> 0,
					NO_ID			=> 1
				});
			$FORM{chg}=$FORM{ID};	
			$html->tpl_show(_include('notepad_main_form', 'Notepad'), {%$Notepad, 
																		%FORM,
																		DATE => $date,
																		ALIGN => 'right'} );

		}
	

	}
	elsif ($FORM{del}) {
		
		$Notepad->notepad_del_note( {ID =>$FORM{del}} );
		if (! $Notepad->{errno}){
			$html->tpl_show(_include('notepad_redirect', 'Notepad'), { 
						SECTION => '',
						MESSAGE => "$_DELETED",
			});
		}	
	}
		
}

1