<%@ include file="/WEB-INF/common/taglibs.jsp"%>

<head>
    <title><fmt:message key="extra.title"/></title>
    <meta name="menu" content="AddUnits"/>
    <script type="text/javascript" src="<c:url value='/scripts/jquery/jquery-1.3.js'/>"></script>
    
</head>

<body>

        <c:if test="${fn:contains(message, 'Please')}">
             <span class="error">${message}</span>
             </c:if>
             
    <label>Choose a datasource :</label>
    <br/>
    <div>
    
    <select name="datasourceNameField" id="datasourceNameField" multiple="false" size="10"> 
        <c:forEach items="${datasources}" var="datasource" varStatus="loop">
        <c:if test="${fn:contains(datasourcesWithExtraFile,datasource.id)}">
        <option name="datasourcename" value="${datasource.name}" id="${datasource.id}" class="extra">${datasource.name}</option>
        </c:if>
        <c:if test="${not fn:contains(datasourcesWithExtraFile,datasource.id)}">
        <option name="datasourcename" value="${datasource.name}" id="${datasource.id}">${datasource.name}</option>
        </c:if>
        </c:forEach>
     </select>
  
    </div>
     
    <div id="block" class="scrollable">
 <hr/>
     <s:form id="addUnitsForm" method="post" ENCTYPE="multipart/form-data" action="uploadAndHarvest" validate="false">
        <input type="hidden"  id="selectedDatasource" name="selectedDatasource">
        <label id="label">Selected datasource: </label><div class="scrollable" id="selectedDatasourceDiv"></div>
        <input id="selectedDatasourceID" name="selectedDatasourceID" type="hidden"/>
        <br/>
        <s:file name="upload" label="File" type="multipart/form-data"/>
     </s:form> 
     
     <a href="javascript:document.forms['addUnitsForm'].submit()"> Upload the new file</a>
     
     <div class="reuse">
        <br/><br/>
        A file does already exist for this datasource. 
         <a  href="javascript:document.forms['addUnitsForm'].submit()" >Use the existing uploaded file</a>
              If you upload a new file, the existing file on the server will be replaced.
      </div>
      
    </div>

<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function () {
    $('#block').hide();
     $('#label').hide();
      $('.reuse').hide();
    $('#datasourceNameField').change(function () {
      var clazz = $('#datasourceNameField option:selected').attr("class");
      var txt = $('#datasourceNameField option:selected').text();
      var id = $('#datasourceNameField option:selected').attr("id");
        $('#selectedDatasource').val(txt);
        $('#selectedDatasourceID').val(id);
        $('#selectedDatasourceDiv').html(txt);
        $('#block').show();
        $('#label').show();
        if(clazz=="extra"){
        $('.reuse').show();
        }
        else{ 
        $('.reuse').hide();
        }
    });
});
 
}); 
</script>

</body>