<%@page import="com.capgemini.framework.common.SettingsHelper"%>
<%@page import="com.capgemini.framework.common.StringHelper" %>
<%@page import="com.capgemini.reports.database.javabean.enumeration.ReportCriteriaEnum"%>
<%@page import="com.capgemini.reports.database.manager.MngSchedulingCriteriaVal"%>
<%@page import="java.util.Map"%>
<%
    /*
     * Available request parameters:
     *	"isUpdateReport"       (boolean)             Is this a submission of a NEW report or an UPDATE?
     *	"idReport"             (long)    <Optional>	 Report Id in case of an UPDATE request
     */
	boolean isDevMode            = SettingsHelper.isDevMode();
    boolean isUpdate = Boolean.valueOf(request.getParameter("isUpdateReport"));
    Long    reportID             = isUpdate ? Long.valueOf(request.getParameter("idReport")) : new Long(-1);
    
    // Default values in each field. May contain nothing / existing values / development values
	String theDefaultDateDebut = "";
	String theDefaultDateFin = "";
	String theTypeOject = "";
	
    if (isDevMode){ // Devmode default values
	    theDefaultDateDebut = "";
	    theDefaultDateFin   = "";
	}
	
    if( isUpdate){ // Existing values to reload from DB
        Map<ReportCriteriaEnum, String> theFunctionalCriteriaValues = new MngSchedulingCriteriaVal().getFunctionalMap(reportID);
	
	    theDefaultDateDebut = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.DATE_DEBUT));
	    theDefaultDateFin   = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.DATE_FIN)); 
	    theTypeOject        = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.TYPE_OBJET));  
	}
%>

<!-- Build input radio CR / FPP -->
<%! private String isCRorFPP(final String theTypeOject) {
        String selectedCR = "";
        String selectedFPP = "";
        StringBuilder buildSelect = new StringBuilder();
        if("CR".equals(theTypeOject)){
            selectedCR ="selected=\"selected\"";
        }else{
            selectedFPP ="selected=\"selected\"";
        }
        buildSelect.append("<option ").append(selectedCR ).append(" value=\"CR\" >CR</option>");
        buildSelect.append("<option ").append(selectedFPP).append(" value=\"FPP\">FPP</option>");    
        return buildSelect.toString();
    }
%>

<!-- HTML, will probably be imported right inside a form -->
<table cellspacing="0" cellpadding="3" border="0">
	<tr>
		<td class="tdFieldTitle">Date d&eacute;but :</td>
		<td>
			<!-- Start calendar definition -->
			<input id="date_debut" type="text" name="date_debut" style="cursor:hand;" value="<%= theDefaultDateDebut %>" />
			<div class="flow-element">
                <div id="container_debut">
                </div>
            </div>
			<!-- End calendar definition -->
		</td>
	</tr>
	<tr>
		<td class="tdFieldTitle">Date fin :</td>
		<td>
			<!-- Start calendar definition -->
			<input id="date_fin" type="text" name="date_fin" style="cursor:hand;" value="<%= theDefaultDateFin %>" />
			<div class="flow-element">
                <div id="container_fin">
                </div>
            </div>
			<!-- End calendar definition -->
		</td>
	</tr>
	<tr>
		<td class="tdFieldTitle required">Type objet* :</td>
		<td>
			<select type="text" id="type_objet" name="type_objet">
				<% if( isUpdate ){ %>
					<%= isCRorFPP(theTypeOject) %> 
				<% }else{ %> 
					<option selected="selected" value="CR">CR</option>
					<option value="FPP">FPP</option>					
				<% } %>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdFieldTitle" colspan="2" style="text-align:center;"><br/>
			<div id="loading" style="display:none;">
				<i>Traitement en cours...</i>
                <br/>
                <img src="/kurt-web/ihm/progress.gif" vspace="6" width="129" height="20">
			</div>
		</td>
	</tr>
</table>

<!-- Internal functional Javascript methods (checks etc.) -->
<script>
	/* Automatic checks before sending the form */
	function checkForm() {
		var isCheckOk = false;
		var dateDebut = getDate(document.getElementById('date_debut').value);
		var dateFin = getDate(document.getElementById('date_fin').value);

		if (isOptionalFieldEmpty("type_objet")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire : Type objet.");
		}else if (!isOptionalFieldEmpty("date_fin") && isOptionalFieldEmpty("date_debut")){
			// when date fin exists, then date_debut is a mandatory field
			parent.Dialog("Erreur", "Date d&eacute;but obligatoire.");
		}else if (!isOptionalFieldEmpty("date_fin") && !isOptionalFieldEmpty("date_debut") && compareDates(dateDebut,dateFin)){
			parent.Dialog("Erreur", "Dates p&eacute;riode saisies incoh&eacute;rentes.");
		}else{
			isCheckOk = true;
		}
		return isCheckOk;
	}

	// Returns true if the time goes backwards
	function compareDates(dateDebut, dateFin) {
		var bool=false;
	    if (dateDebut.getTime() >= dateFin.getTime()) {
	    	bool=true;
	    }
		return bool;
	}

	function getDate(strDate){	
		return new Date(strDate.substring(6,10), strDate.substring(3,5)-1, strDate.substring(0,2));  
	}
	  		
</script>
<script>
	/* Initialize dates pickers */
	initPicker('date_debut','container_debut');
	initPicker('date_fin','container_fin');
</script>	