<?

	/* 
	** if we have necessary fields, add node
	*/
	
	
	if((intval($pathway) <= 0) OR (!$UserInfo["Add Story"]))
	{
		/*
		** get pathway to Suggested Links
		*/
		$Query = "SELECT ID FROM pathway WHERE Name='Suggestions' ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		$DatabaseRow = mysql_fetch_row($DatabaseResult);

		//force pathway
		$pathway = $DatabaseRow[0];

		//force no sending of mail
		$inputSendMail = "CHECK";
	}
	
	
		
	/* 
	** check for duplicate 
	*/
	
	if ($inputResource != "")
	{
		$Query = "SELECT ID FROM node WHERE Resource = '$inputResource' ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		if(mysql_NumRows($DatabaseResult) > 0)
		{
			$ActionResults .= "Warning: resource already present!<BR>";
		}
	}
		
	// strip HTML tags from user input to guard against hacking
		
	$inputName = strip_tags($inputName);
	$inputDescription = strip_tags($inputDescription);
	$inputKeywords = strip_tags($inputKeywords);
	$inputIcon = strip_tags($inputIcon);
	$inputResource = strip_tags($inputResource);
	$inputDisplayOrder = strip_tags($inputDisplayOrder);
		
			

	$Query = "INSERT INTO node ";
	$Query .= "(Parent, Creator, Name, Description, Keywords, Content, Icon, Resource, Created, LinkCount, DisplayOrder, LastChecked) "; 
	$Query .= "VALUES ( ";
	$Query .= "$pathway, ";
	$Query .= $UserInfo["ID"] . ", ";
	$Query .= "'" . addslashes($inputName) . "', ";
	$Query .= "'" . addslashes($inputDescription) . "', ";
	$Query .= "'" . addslashes($inputKeywords) . "', ";
	$Query .= $inputContent . ", ";
	$Query .= "'" . addslashes($inputIcon) . "', ";
	$Query .= "'" . addslashes($inputResource) . "', ";
	$Query .= "now(), ";
	$Query .= "0, ";
	$Query .= addslashes($inputDisplayOrder) . ", ";
	$Query .= "'1970-02-18')"; //check new links right away
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	if(!$DatabaseResult)
	{
		$ActionResults .= "Add failed: " . mysql_error();
	}
	else
	{
		$newnode = mysql_insert_id($DatabaseLink);
		
		$inputDate = $inputDateYear . "-" . $inputDateMonth . "-" . $inputDateDay;
		
		/* 
		** process body according to the option they chose in the add_story screen 
		*/
		
		if ($inputDisplayMethod != "pass_through")
		{
			$inputBody = ereg_replace("\n", "<BR>", $inputBody);	
		}
		
		// strip HTML tags from user input to guard against hacking
		
		$inputAuthor = strip_tags($inputAuthor);
		$inputBody = strip_tags($inputBody, OK_STORY_TAGS);
				
		$Query = "INSERT INTO story ";
		$Query .= "(Node, Author, Date, Body) ";
		$Query .= "VALUES ( ";
		$Query .= "$newnode, ";
		$Query .= "'" . addslashes($inputAuthor) . "', ";
		$Query .= "'" . $inputDate . "', ";
		$Query .= "'" . addslashes($inputBody) . "') ";
		
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		if(!$DatabaseResult)
		{
			$ActionResults .= "Add failed: " . mysql_error();
		}
		else
		{
		
			$ActionResults .= "Node Added!";
		}
	}
	
	/* 
	** send mail to the list 
	*/
	if($inputSendMail == "")
	{
		$ActionResults .= "<BR>\n";
		$ActionResults .= "Mail: ";			
	
		$Query = "SELECT Private ";
		$Query .= "FROM pathway ";
		$Query .= "WHERE ID=$pathway ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		$DatabaseRow = mysql_fetch_row($DatabaseResult);
		$isPrivate = $DatabaseRow[0];

		$MailBody .= "PATHWAY\n   $pathway_PlainName\n";
		$MailBody .= "   <http://www.leonatkinson.com/brain/index.php3?SCREEN=view&pathway=$pathway>\n\n";
		$MailBody .= "NODE\n   $inputTitle <http://www.leonatkinson.com/brain/location.php3?inputNode=$newnode>\n\n";
		$MailBody .= "DESCRIPTION\n   $inputDescription\n\n";

		if($isPrivate == "N")
		{
			$MailTo = "brain@onelist.com";
			$MailFrom = "leon@leonatkinson.com";
			$MailSubject = "$inputTitle";

/*
			if(mail($MailTo, $MailSubject, $MailBody, "From: $MailFrom"))
			{
				$ActionResults .= "Sent!";
			}
			else
			{
				$ActionResults .= "Failed!";
			}
*/			
		}
		else
		{
			$ActionResults .= "Private!";
		}
	}
	
	
?>