<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--  This XSL style sheet will perform initial error-checking and form validation
	for the GALEX Phase 2 templates.  For the latest version, please check the
	GALEX GI website at http://galexgi.gsfc.nasa.gov/Cycle1/Phase2/

	We do not guarantee that this form will catch all errors, but it should 
	catch all likely oddities.  

	A couple of errors that this script will NOT catch:
	1)  Embedded LaTeX or other formatting.  Please use plain text only; 
	    otherwise, you run the risk of the TAC or Technical Review team(s) 
	    being unable to read something important.  
	
	2)  Incorrect RA and DEC.  You must supply RA and DEC for the pointing 
	    and for each target.  We can check to make sure that you
	    have submitted RA and DEC's needed, but we can not check to 
	    make sure they are right.  

Thanks!

If you have questions, please email GALEX.helpdesk@galexgi.gsfc.nasa.gov

-->

<xsl:output method="html" indent="yes" />

<!-- global variables: other XML tables. -->


<xsl:template match="/" >

	<html>
	<head>
	<title>Validation Page: Proposal #<xsl:value-of select="//nasapropno" /></title>
	</head>
	<body> 
	<h1>Validation for Proposal #<xsl:value-of select="//nasapropno" /></h1>
	<h3>Please review this page for errors.  Missing or invalid required fields are 
	marked with an error, e.g. **MISSING**.</h3>  
	<h3> When you are satisified with your submission, please mail it to 
	finalsubmission@galexgi.gsfc.nasa.gov.</h3>

	<xsl:call-template name="mandatory-checks" />
	<!-- Validate as we go. -->
	<xsl:apply-templates />
	</body>

	<h3>If you are satisifed with your submission, please email the XML to
	finalsubmission@galexgi.gsfc.nasa.gov.</h3>  
	</html>
</xsl:template>

<xsl:template match="proposal">
	<xsl:apply-templates />
</xsl:template>

<xsl:template match="nasapropno">
	<p><xsl:value-of select="name()"/>:
	<xsl:choose>
		<xsl:when test="number(.)">
			<xsl:value-of select="." />
		</xsl:when>
		<xsl:otherwise>
			**MISSING**
		</xsl:otherwise>
	</xsl:choose>
	</p>
</xsl:template>			

<xsl:template match="totalobstime">
	<p>Total Obs Time:
	
	<xsl:if test="not(@units='ksec')">
		***ERROR: totalobstime required in ksec***
	</xsl:if>
	<xsl:if test="not(number(.))">
		***ERROR: totalobstime must be a number***
	</xsl:if>

	<xsl:value-of select="." /> <xsl:value-of select="@units" /><br />
<!--	Sum of requested target obtimes: <xsl:value-of select="sum(//observation/obtime) div 1000" /> ksec -->

	</p>
</xsl:template>

<xsl:template match="PI">
	<xsl:element name="p">
	PI Info: <br />
	<xsl:for-each select="./*">
		&#160;&#160;&#160;&#160;<xsl:apply-templates select="." /><br />
	</xsl:for-each>
	</xsl:element>
</xsl:template>

<xsl:template match="contact">
	<xsl:if test="not(.='')">
		<xsl:element name="p">
		Contact Info: 
		<xsl:choose>
			<xsl:when test="normalize-space(translate(contactPI,'YES','yes'))='yes'">
				Same as PI
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates />
			</xsl:otherwise>
		</xsl:choose>
		</xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template match="CoI_list">
	<xsl:if test="not(.='')">
		<xsl:element name="p">
		Co-Investigators: <xsl:element name="br" />
		<xsl:for-each select="coi">
			&#160;&#160;&#160;&#160;<xsl:value-of select="coiname" /> (<xsl:value-of select="coiinstitution" />) <xsl:element name="br" />
		</xsl:for-each>
		</xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template match="PI/*|contact/*">
	<xsl:choose>
		<xsl:when test="normalize-space(.)=''">
			***MISSING: <xsl:value-of select="name(.)" />***
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="." />
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>&#160; </xsl:text>
</xsl:template>

<xsl:template match="observations">
	<h3>Target List</h3>
	<table border="1">
	<xsl:apply-templates select="observation" />
	</table>
</xsl:template>

<xsl:template match="observation">
<xsl:variable name="nasapropno" select="number(//nasapropno)" />

	<tr><td colspan="3"><strong><center>TARGET</center></strong></td></tr>
	<tr><xsl:apply-templates select="objectname">
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	    <xsl:if test="//proposal/@version='2.0'">
	    	<xsl:apply-templates select="archive">
			<xsl:with-param name="test-type">YN</xsl:with-param>
		</xsl:apply-templates>
	    </xsl:if>

	</tr>
	

	<tr><td colspan="3"><center>GALEX Pointing</center></td></tr>
	<tr><xsl:apply-templates select="pointing" />
		<xsl:if test="not(pointing)">
		<td colspan="2"> ****Pointing Coordinates Missing.**** </td>
		</xsl:if>
	    <xsl:apply-templates select="obtime"><xsl:with-param name="test-type">number</xsl:with-param></xsl:apply-templates>
	</tr>
	<tr><td colspan="3"><center>Science Target(s)</center></td></tr>
	<xsl:apply-templates select="scienceTargets/sciencetarget" />
	<tr><td colspan="3"><center>Observation Info</center></td></tr>
	<tr>
	    <xsl:apply-templates select="aperture" />
	    <xsl:if test="normalize-space(aperture)='GRISM'">
		<xsl:apply-templates select="grismpre"><xsl:with-param name="span">2</xsl:with-param></xsl:apply-templates>
	    </xsl:if>
	</tr>
	<tr>
	    <xsl:apply-templates select="mode" />
            <xsl:apply-templates select="numsteps"><xsl:with-param name="test-type">number</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="signoise"><xsl:with-param name="test-type">number</xsl:with-param></xsl:apply-templates>
	</tr>
	<tr><xsl:apply-templates select="spectype" />
	    <xsl:apply-templates select="colorexcess"> <xsl:with-param name="test-type">optional</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="sourcetype" />
	</tr>
	<tr><xsl:apply-templates select="lambdaref"> <xsl:with-param name="test-type">number</xsl:with-param> </xsl:apply-templates>
	    <xsl:apply-templates select="fluxlambdaref"></xsl:apply-templates>
	    <xsl:apply-templates select="fluxacc" />
	</tr>
	<tr>
	    <xsl:apply-templates select="FUVONLY"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="fuvexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="GRAN"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="granexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="TIMECRITICAL"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="timecriticalexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="LOWZODI"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="lowzodiexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="TOO"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="tooexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="MOVE"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="moveexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="BRIGHTWAIVER"><xsl:with-param name="test-type">YN</xsl:with-param></xsl:apply-templates>
	    <xsl:apply-templates select="brightwaiverexplain">
		<xsl:with-param name="test-type">explain</xsl:with-param>
		<xsl:with-param name="span">2</xsl:with-param>
	    </xsl:apply-templates>
	</tr>
	<tr>
	    <xsl:apply-templates select="comments">
		<xsl:with-param name="test-type">optional</xsl:with-param>
		<xsl:with-param name="span">3</xsl:with-param>
	    </xsl:apply-templates>
	</tr>

	<tr><td colspan="3" bgcolor="#000000">&#160;</td></tr>

</xsl:template>


<xsl:template match="*">
	<xsl:param name="span">1</xsl:param>
	<xsl:param name="test-type">exists</xsl:param>

	<xsl:variable name="upcase" select="translate(normalize-space(.),'yesno','YESNO')" />
	<xsl:variable name="upcaseprev" select="translate(normalize-space(./preceding-sibling::node()/text()),'yesno','YESNO')" />

	<xsl:choose>
		<xsl:when test="$test-type='explain' and normalize-space(.)='' and $upcaseprev='YES'">
			<td colspan="{$span}">**EXPLANATION REQUIRED**</td>
		</xsl:when>
		<xsl:when test="$test-type='number' and not(number(.)) and not(normalize-space(translate(.,'new','NEW')) = 'NEW')">
			<td colspan="{$span}">**NOT A NUMBER: 
				<xsl:value-of select="name(.)" />**</td>
		</xsl:when>
		<xsl:when test="$test-type='YN' and not($upcase ='YES' or $upcase='NO')">
			<td colspan="{$span}">**YES or NO: <xsl:value-of select="name(.)" /> </td>
		</xsl:when>
		<xsl:when test="not($test-type='optional' or $test-type='explain') and normalize-space(.)=''">
			<td colspan="{$span}">**MISSING: 
				<xsl:value-of select="name(.)" />**</td>
		</xsl:when>
		<xsl:otherwise>
			<td colspan="{$span}"><xsl:value-of select="name(.)" />: <xsl:value-of select="." /></td>
		</xsl:otherwise>
	</xsl:choose>

</xsl:template>

<xsl:template match="pointing" >
	<xsl:choose>
		<xsl:when test="(ra/deg='' and ra/hrs='') or (dec/deg='')">
			<td colspan="1"> ***Coordinates missing.*** </td>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="ra" />
			<xsl:apply-templates select="dec" />
		</xsl:otherwise>
	</xsl:choose>

</xsl:template>

<xsl:template match="sciencetarget" >
	<xsl:choose>
		<xsl:when test="(ra/deg='' and ra/hrs='') or (dec/deg='') or not(sciencetargetname) or not(sciencetargetsize)">
			<tr><td colspan="3"> ***Target Information missing*** </td></tr>
		</xsl:when>
		<xsl:otherwise>
			<tr>
				<xsl:apply-templates select="sciencetargetname">
					<xsl:with-param name="span">3</xsl:with-param>
				</xsl:apply-templates>				
			</tr>
			<tr>
				<xsl:apply-templates select="ra" />
				<xsl:apply-templates select="dec" />
				<xsl:apply-templates select="sciencetargetsize">
					<xsl:with-param name="test-type">number</xsl:with-param>
				</xsl:apply-templates>
			</tr>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="ra|dec">

	<td colspan="1"> <xsl:value-of select="translate(name(.),'radec','RADEC')" />
			(<xsl:choose>
				<xsl:when test="hrs">hrs</xsl:when>
				<xsl:otherwise>deg</xsl:otherwise></xsl:choose>): 
		<xsl:choose>
		   <xsl:when test="hrs"> 
			<xsl:value-of select="hrs" />
		   </xsl:when>
		   <xsl:otherwise>
			<xsl:value-of select="deg" />
		   </xsl:otherwise>
		</xsl:choose>
		<xsl:if test="min or sec">
			:<xsl:value-of select="min" />
			<xsl:if test="sec">
				:<xsl:value-of select="sec" />
			</xsl:if>
		</xsl:if>
	</td>
</xsl:template>


<xsl:template match="title|programcategory">
	<p><xsl:value-of select="name(.)" />:
	<xsl:choose>
		<xsl:when test="normalize-space(.)=''">
			**MISSING**
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="." />
		</xsl:otherwise>
	</xsl:choose>
	</p>
</xsl:template>

<xsl:template match="//proposal/text()" />

<xsl:template name="mandatory-checks">
	<xsl:if test="not(//nasapropno)">
		<p><strong>Error:  please include the NASA proposal number</strong></p>
	</xsl:if>
	<xsl:for-each select="//observation">
		<xsl:if test="not(pointing)">
			<p><strong>Error: please inlcude the pointing for each observation.</strong></p>
		</xsl:if>
		<xsl:if test="not(scienceTargets/sciencetarget)">
			<p><strong>Error: please include a list of science target(s) for each pointing.</strong></p>
		</xsl:if>
		<xsl:if test="not(aperture)">
			<p><strong>Error: please include the aperture (GRISM or IMAGE) for each pointing.</strong></p>
		</xsl:if>
		<xsl:if test="not(obtime)">
			<p><strong>Error: please include the observing time for each pointing.</strong></p>
		</xsl:if>
		<xsl:if test="not(FUVONLY) or not(GRAN) or not(TIMECRITICAL) or not(MOVE) or not(TOO) or not(LOWZODI) or not(BRIGHTWAIVER)">
			<p><strong>Error: please specify YES or NO for each of FUVONLY, GRAN, TIMECRITICAL, LOWZODI, MOVE, TOO and BRIGHTWAIVER</strong></p>
		</xsl:if>
	</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
