#!/usr/bin/perl
print "Content-type: text/html\n\n" ;

use File::Copy;

require "blendiii.pl";
require "headeriii.pl";

use CGI ':cgi-lib';
ReadParse(*input);

$workid = $input{"id"};

my($trueid,$truename) = getUserFromSession();
$login = getUserGreet($trueid,$truename);

$caption = "post + publish new works!";
if($workid ne ""){
    $caption = "edit an existing work!";
}
print getHeader($caption,$login);

$textmode = "plain";


if($trueid eq ""){
    print<<__EOQ__;
<br><br>
The Blender of Love welcomes your romance-related creative work--<br>
in fact our whole purpose is to be an open forum for romantic expression--
<br>
but you need to 
<a href="signup.html">create a free account first</a>! 
<br><br>
If you already have an account, then please login above.
__EOQ__
exit;
}

#see if we're trying to load a work...

if($workid ne ""){
    $workid =~ /^(.*?)\.(.*?)\.(.*)$/;
    $year = $1;
    $mon = $2;
    $rest = $3;

    $file = "$dirworks/$year/$mon/$rest.work";

    open(READ,"$dirworks/$year/$mon/$rest.info");

    $userid = <READ>; chomp $userid;
    $username = <READ>; chomp $username;
    $authorname = <READ>; chomp $authorname;
    $title = <READ>; chomp $title;
    $descrip = <READ>; chomp $descrip;
    $kindofwork = <READ>; chomp $kindofwork;
    $feeling = <READ>; chomp $feeling;
    close READ;

    if($userid ne $trueid ){
	print<<__EOQ__;
<br><br>
       <font color="RED"><b>
YOU DO NOT SEEM TO BE THE AUTHOR OF THIS WORK.
</b></font>
__EOQ__
exit;
    }

    $frontpick = 0;
    open(PIX,"< $dirfrontpicks/$trueid");
    while(defined($line=<PIX>)){
	chomp $line;
	if($workid eq $line){
	    $frontpick = 1;
	}
    }


   if($frontpick){
	print<<__EOQ__;
<br><br>
<b>
Congratulations, this work was a Frontpage Pick!
<br><br>
Blender-Keeper Kirk has to do special stuff to let you edit this...<br>
edits may affect the front page for that month.
<br><br>
Please contact him for help! Thanks!
</b>
<br><br>

__EOQ__
exit;
    }


    open(READ,"$dirworks/$year/$mon/$rest.work");
    $content = "";
    while(defined($line=<READ>)){
	$content .= $line;
    }
    close READ;

    $textmode = "html";
    if($content =~ /^\<TT\>/){
	$content =~ s/^\<TT\>//;
	$content =~ s/\<\/TT\>$//;
	$content =~ s/\<BR\>//g;
	$textmode = "plain";
    }
    if($content =~ /^\<PRE\>/){
	$content =~ s/^\<PRE\>//;
	$content =~ s/\<\/PRE\>$//;
	$textmode = "pre";
    }


}#end workid is here
if($workid eq ""){
print<<__EOQ__;

The Blender of Love welcomes romance-related creative work.

<FONT COLOR=RED>
<UL>
<li>Make you sure you have the permission to post works if you are not the author.
<LI>The Blender of Love is a forum for <I>Romance</I>-- please don't send<BR>
    works involving other kinds of love only.
<li>Once your work has been submitted you can edit it from the "edit this work" link 
<br>on the work view page, but you must be logged in.
</UL>
</FONT>
__EOQ__
}


$moodoptions = getMoodOptions($feeling);
$typeoptions = getTypeOptions($kindofwork);
$textmodecheck{$textmode} = " CHECKED ";

print<<__EOQ__;
<FORM name="realform" method="post" action="submitworknow.cgi">
<TABLE BORDER=0>
<input type="hidden" name="workid" value="$workid">
<TR>
<TR><TD VALIGN="TOP"  align="right">Title:<BR>
<TD VALIGN="TOP" ><INPUT TYPE=TEXT  NAME="title"  SIZE=30 value="$title"><BR>


<TR><TD VALIGN="TOP"  align="right" >Author's Name:<BR>

<TD VALIGN="TOP" ><INPUT TYPE=TEXT  NAME="author"  SIZE=20 value="$authorname">
<br><i>(only if different from your login)</i><BR>

<TR><TD VALIGN="TOP"  align="right">A Brief Description: <BR>

<TD VALIGN="TOP" ><INPUT TYPE=TEXT  NAME="descrip" value="$descrip" SIZE=50><BR>


<TR><TD VALIGN="TOP"   align="right">What kind of work is this?<BR>
<TD VALIGN="TOP" ><SELECT NAME="category" >
$typeoptions
</SELECT><BR>
<TR><TD VALIGN="TOP"  align="right">How would you describe<br> 
the 'feeling of this work?'
<TD VALIGN="TOP" ><SELECT NAME="feeling" >
$moodoptions
</SELECT><BR>

<TR><TD VALIGN="TOP"   align="right">Choose how you want<br> the text to be displayed:
<BR>
<td valign="top" >
<INPUT TYPE=RADIO NAME="texttype" VALUE = "plain" $textmodecheck{"plain"}>Plain Text 
<I>Line breaks and most white space preserved</I> <BR>
<INPUT TYPE=RADIO NAME="texttype" VALUE = "pre" $textmodecheck{"pre"}>Preformatted Text
<I>text spacing preserved using monospace font.</I><BR>
<INPUT TYPE=RADIO NAME="texttype" VALUE = "html" $textmodecheck{"html"}>Raw HTML
<I>author provides HTML</I> formatting<BR>
<TR><TD VALIGN="TOP"  align="right">
The Work: <BR>

<TD VALIGN="TOP">
<TEXTAREA NAME="submission"  ROWS=30 COLS=60>$content</TEXTAREA><BR>
<BR>
<TR><td><TD VALIGN="TOP"><CENTER>
<INPUT TYPE=SUBMIT name="task" VALUE="preview">
<INPUT TYPE=SUBMIT name="task" VALUE="publish it!">

</FORM>


</center>

</TABLE>


<HR>

If there is a problem you can
email the <a href="/blend/av.cgi?id=1">Blender-Keeper</a> 

 directly
<HR>
<a href="/"><img src="/blimage/blenderi.gif" border = 0 alt ="[BLENDER]"  width=32 height=32>Back to the Blender</a>
</BODY>
</HTML>
__EOQ__




sub getMoodOptions{
    my($default) = @_;
    $buf = "";
    foreach $mood(@moods){
	$selected = "";
	if($mood eq $default){
	    $selected = " selected";
	}
	$buf .= "<option $selected >$mood \n";
    }
    return $buf;
}
sub getTypeOptions{
    my($default) = @_;
    $buf = "";
    foreach $worktype(@worktypes){
	$selected = "";
	if($worktype eq $default){
	    $selected = " selected";
	}
	$worktyped = $worktypedesc{$worktype};
	$buf .= qq(<option value="$worktype" $selected  >$worktyped\n);
    }
    return  $buf;
}
