<%@ taglib uri="infoglue-page" prefix="page" %>
<%@ taglib uri="infoglue-common" prefix="common" %>
<%@ taglib uri="infoglue-structure" prefix="structure" %>
<%@ taglib uri="infoglue-content" prefix="content" %>
<%@ taglib uri="infoglue-management" prefix="management" %>
<%@ taglib uri="infoglue-page" prefix="tl" %>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/mailer-1.1" prefix="mt" %>

<%@ page contentType="text/html; charset=UTF-8" %>

<page:pageContext id="pc"/>

<common:urlBuilder id="currentUrl"/>
<common:urlBuilder id="currentFullUrl" fullBaseUrl="true"/>

<c:choose>
	<c:when test="${param.contentId != null && param.contentId > -1}">
		<content:content id="articleContent" contentId="${param.contentId}"/>
	</c:when>
	<c:otherwise>
		<content:content id="articleContent" propertyName="Article" useInheritance="false"/>
	</c:otherwise>
</c:choose>

<content:assetUrl id="headlineImageUrl" propertyName="Article" assetKey="header" useInheritance="false"/>
<c:if test="${articleContent != null}">
	<content:contentVersion id="articleContentVersion" content="${articleContent}"/>
	<c:if test="${articleContentVersion != null}">
		<management:principal id="articleModifier" contentVersion="${articleContentVersion}"/>
		<content:contentAttribute id="title" contentId="${articleContent.id}" attributeName="Title" useInheritance="false"/>
		<content:contentAttribute id="leadIn" contentId="${articleContent.id}" attributeName="Leadin" useInheritance="false"/>
		<content:contentAttribute id="fullText" contentId="${articleContent.id}" attributeName="FullText" useInheritance="false"/>
		<content:contentAttribute id="author" contentId="${articleContent.id}" attributeName="Author" useInheritance="false"/>
	</c:if>
</c:if>

<c:if test=’${articleContent == null}’>
	<c:set var="title" value="Ingen artikel vald"/>
	<c:set var="leadIn" value="Ingen artikel vald"/>
	<c:set var="fullText" value="Ingen artikel vald"/>
</c:if>
<%
String fullText = (String)pageContext.getAttribute("fullText");
if(fullText.indexOf("<p>") == -1)
	pageContext.setAttribute("fullText", "<p>" + fullText + "</p>");
%>

<c:if test="${headlineImageUrl != ’’}">
	<img src="<c:out value="${headlineImageUrl}"/>" style="border: 1px solid #8499b3;"/>
</c:if>

<h1><c:out value="${title}" escapeXml="false"/></h1>
<c:out value="${fullText}" escapeXml="false"/>

<c:if test="${pc.isInPageComponentMode}">
<p>
<content:editOnSight id="editOnSightHTML" contentId="${articleContent.id}" attributeName="FullText" html="Edit article"/>
<content:assignPropertyBinding id="assignHTML" propertyName="Article" html="Choose article"/>
<content:assignPropertyBinding id="assignNewHTML" propertyName="Article" html="Create new article" createNew="true"/>
<c:choose>
	<c:when test=’${articleContent != null}’>
		<c:out value="${editOnSightHTML}" escapeXml="false"/> | 
	</c:when>
	<c:otherwise>
	</c:otherwise>
</c:choose>
<c:out value="${assignHTML}" escapeXml="false"/> | <c:out value="${assignNewHTML}" escapeXml="false"/>
</p>
</c:if>

<ig:slot id="articleEnd" inherit="false"></ig:slot>


<script type="text/javascript">
	function closeDiv(divId)
	{
		document.getElementById(divId).style.display = "none";
	}
	function openDiv(divId)
	{
		document.getElementById(divId).style.display = "block";
	}
</script>

<c:set var="display" value="none"/>
<c:if test="${param.sendTip == ’true’}"><c:set var="display" value="block"/></c:if>
<div id="tipFriend" style="display: <c:out value="${display}"/>; position: absolute; top: 200px; width: 520px; height: 200px; background-color: white; padding: 5px; border: 1px solid #333;">

<% 
//TODO - gör om till taglib...
javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest)pageContext.getRequest();
String referer = req.getHeader("Referer");
if(referer != null)
{
	int index = referer.indexOf(req.getServerName());
	boolean internalUrl = true;
	if(index == -1)
		internalUrl = false;
		
	pageContext.setAttribute("internalUrl", new Boolean(internalUrl)); 
}
%>

<c:choose>
	<c:when test="${internalUrl == true && param.sendTip == ’true’ && not empty param.receiverEmail && not empty param.senderEmail}">
		Trying to sent message with data:<br/> 
		SenderEmail: <c:out value="${param.senderEmail}"/>
		ReceiverEmail: <c:out value="${param.receiverEmail}"/>
		Tip URL: <c:out value="${param.tipUrl}"/><br/>
		SMTP Server: <c:out value="${pc.smtpServer}"/><br/>
		<c:set var="smtpServer" value="${pc.smtpServer}"/>
		<mt:mail server="<%= (String)pageContext.getAttribute("smtpServer") %>">
		
			<c:choose>
	    	    <c:when test="${param.senderEmail != null && param.senderEmail != ’’}">
					<mt:from><c:out value="${param.senderEmail}"/></mt:from>
	        	</c:when>
        		<c:otherwise>
					<mt:from>no-reply@infoglue.org</mt:from>
        		</c:otherwise>
        	</c:choose>

			<mt:setrecipient type="to"><c:out value="${param.receiverEmail}"/></mt:setrecipient>
			<mt:setrecipient type="cc"><c:out value="${param.senderEmail}"/></mt:setrecipient>
			<mt:setrecipient type="cc">mattias.bogeblad@modul1.se</mt:setrecipient>
			<mt:subject>A friend wants you to visit this page</mt:subject>
			<mt:message type="html" charset="utf-8">
				<div>
				<a href="mailto:"<c:out value="${param.senderEmail}"/>"><c:out value="${param.senderEmail}"/></a><br />
				A friend wants you to check this url<br /><br />
				<a href="<c:out value="${param.tipUrl}"/>"><c:out value="${param.tipUrl}"/></a>
				<c:if test="${not empty param.message}">
				<br /><br />
				Comment:<br/>
				<%
				String message = pageContext.getRequest().getParameter("message");
				message = message.replaceAll("\n", "<br/>"); 
				pageContext.setAttribute("message", message);
				%>
				<div><pre><c:out value="${message}" escapeXml="false"/><br/></pre></div>
				</c:if>
				<br /><br /><br />
				<hr>
				Do not reply to this message - it was automatically sent from the infoglue.org site.
				</div></mt:message>

			<mt:send>
				<mt:error id="err">
					Error sending mail... <c:out value="${err}"/>
		       	</mt:error>
			</mt:send>
		</mt:mail>
		
		<c:set var="messageSent" value="true"/>
	</c:when>
	<c:otherwise>

		<form name="tipForm" action="<c:out value="${currentUrl}"/>" method="post">
			<input type="hidden" name="sendTip" value="true"/>
			<input type="hidden" name="tipUrl" value="<c:out value="${currentFullUrl}"/>"/>
		
			<fieldset>
				<legend>Tip a friend</legend>
				<p>
					<label for="senderEmail">Your email <c:if test="${empty param.senderEmail}"><span style="color:red;">*</span></c:if></label>
					<input id="senderEmail" type="text" name="senderEmail"/>
				</p>
				<p>
					<label for="receiverEmail">Your friends email <c:if test="${empty param.receiverEmail}"><span style="color:red;">*</span></c:if></label>
					<input id="receiverEmail"  type="text" name="receiverEmail"/>
				</p>
				<p>
					<label for="message">Message <c:if test="${empty param.message}"><span style="color:red;">*</span></c:if></label>
					<textarea id="message" name="message" rows="5" cols="30"></textarea>
				</p>
				<p>&nbsp;</p>
				<p>
					<input class="submitButton" type="submit" value="Submit"/>
					<input class="submitButton" type="button" onclick="closeDiv(’tipFriend’);" value="Close"/>
				</p>
			</fieldset>
		</form>
	
	</c:otherwise>
</c:choose>

</div>

<c:set var="display" value="none"/>
<c:if test="${param.sendComment == ’true’}"><c:set var="display" value="block"/></c:if>

<div id="commentArticle" style="display: <c:out value="${display}"/>; position: absolute; top: 200px; width: 520px; height: 200px; background-color: white; padding: 5px; border: 1px solid #333;">

<c:choose>
	<c:when test="${internalUrl == true && param.sendComment == ’true’ && not empty param.commentSenderEmail}">
		Trying to sent comment with data:<br/> 
		SenderEmail: <c:out value="${param.commentSenderEmail}"/><br/>
		The comment will be published after review
		
		<c:set var="smtpServer" value="${pc.smtpServer}"/>
		<mt:mail server="<%= (String)pageContext.getAttribute("smtpServer") %>">
		
			<c:choose>
	    	    <c:when test="${param.commentSenderEmail != null && param.commentSenderEmail != ’’}">
					<mt:from><c:out value="${param.senderEmail}"/></mt:from>
	        	</c:when>
        		<c:otherwise>
					<mt:from>no-reply@infoglue.org</mt:from>
        		</c:otherwise>
        	</c:choose>

			<mt:setrecipient type="to">bogeblad@yahoo.com</mt:setrecipient>
			<mt:setrecipient type="cc"><c:out value="${param.commentSenderEmail}"/></mt:setrecipient>
			<mt:subject>A comment was added - <c:out value="${param.subject}"/></mt:subject>
			<mt:message type="html" charset="utf-8">
				<div>
				Jag vill testa detta... skicka din kommentar
				<a href="mailto:"<c:out value="${param.commentSenderEmail}"/>"><c:out value="${param.commentSenderEmail}"/></a><br />
				A person wants to add a comment to this url:<br /><br />
				<a href="<c:out value="${param.commentUrl}"/>"><c:out value="${param.commentUrl}"/></a>
				<c:if test="${not empty param.comment}">
				<br /><br />
				Comment:<br/>
				<%
				String comment = pageContext.getRequest().getParameter("comment");
				comment = comment.replaceAll("\n", "<br/>"); 
				pageContext.setAttribute("comment", comment);
				%>
				<div><pre><c:out value="${comment}" escapeXml="false"/><br/></pre></div>
				</c:if>
				<br /><br /><br />
				<hr>
				Do not reply to this message - it was automatically sent from the infoglue.org site.
				</div></mt:message>

			<mt:send>
				<mt:error id="err">
					Error sending mail... <c:out value="${err}"/>
		       	</mt:error>
			</mt:send>
		</mt:mail>
		
		<c:set var="messageSent" value="true"/>
	</c:when>
	<c:otherwise>

		<form name="commentArticleForm" action="<c:out value="${currentUrl}"/>" method="post">
			<input type="hidden" name="sendComment" value="true"/>
			<input type="hidden" name="commentUrl" value="<c:out value="${currentFullUrl}"/>"/>
			<input type="hidden" name="commentContentId" value="${commentContentId}"/>
		
			<fieldset>
				<legend>Comment article</legend>
				<p>
					<label for="subject">Subject <c:if test="${empty param.subject}"><span style="color:red;">*</span></c:if></label>
					<input id="subject" type="text" name="subject"/>
				</p>
				<p>
					<label for="commentSenderEmail">Your email <c:if test="${empty param.commentSenderEmail}"><span style="color:red;">*</span></c:if></label>
					<input id="commentSenderEmail" type="text" name="commentSenderEmail"/>
				</p>
				<p>
					<label for="comment">Comment <c:if test="${empty param.comment}"><span style="color:red;">*</span></c:if></label>
					<textarea id="comment"  name="comment" rows="5" cols="30"></textarea>
				</p>
				<p>&nbsp;</p>
				<p>
					<input class="submitButton" type="submit" value="Submit"/>
					<input class="submitButton" type="button" onclick="closeDiv(’commentArticle’);" value="Close"/>
				</p>
			</fieldset>
		</form>

	</c:otherwise>
</c:choose>

</div>

<div id="printField">
	<ul>
        <li class="print"><a href="javascript:window.print();" title="Print page">Print</a></li>
        <li class="tellaFriend"><a href="javascript:openDiv(’tipFriend’);" title="Tip a friend">Tip a friend</a></li>
        <li class="comment"><a href="javascript:openDiv(’commentArticle’);" title="Comment article">Comment</a></li>
	</ul>
</div>

<content:contentAttribute contentId="${articleContent.id}" id="canComment" attributeName="CanComment" disableEditOnSight="true"/>
<c:if test="${canComment==’true’}">
  <common:include relationAttributeName="RelatedComponents" contentName="IGC Article comment component"/>
</c:if>

<%--
<?xml version=’1.0’ encoding=’UTF-8’?><properties><property name=’Article’ type=’binding’ entity=’Content’ multiple=’false’ allowedContentTypeDefinitionNames=’Article’ description=’No description yet’/></properties>
--%>