<%@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);
	
	/* Conf 1*/
	String theDefaultGen1 = "";
	String theDefaultGenMont1 = "";
	String theDefaultProduct1 = "";
	String theDefaultUnit1 = "";
	
	/* Conf 2*/
	String theDefaultGen2 = "";
	String theDefaultGenMont2 = "";
	String theDefaultProduct2 = "";
	String theDefaultUnit2 = "";
	
	if (isDevMode){    // Default values for development environment only
		theDefaultGen1 = "";
		theDefaultGenMont1 = "";
		theDefaultProduct1 = "ZZ07";
		theDefaultUnit1 = "10";
		theDefaultGen2 = "";
		theDefaultGenMont2 = "";
		theDefaultProduct2 = "ZZ07";
		theDefaultUnit2 = "20";
	}

	if( isUpdate ){ // Recall existing data from DB 
        Map<ReportCriteriaEnum, String> theFunctionalCriteriaValues = new MngSchedulingCriteriaVal().getFunctionalMap(reportID);
	    
		theDefaultGen1     = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.GENERIC));
		theDefaultGenMont1 = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.GENERIC_ASSEMB));  
		theDefaultProduct1 = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.PRODUCT));
		theDefaultUnit1    = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.UNIT_CONFIGURATION_1));
        theDefaultUnit2    = StringHelper.replaceNullByEmpty( theFunctionalCriteriaValues.get(ReportCriteriaEnum.UNIT_CONFIGURATION_2)); 
		theDefaultGen2     = theDefaultGen1;      // Same both conf
		theDefaultGenMont2 = theDefaultGenMont1;  // Same both conf
		theDefaultProduct2 = theDefaultProduct1;  // Same both conf
	}
%>

<!-- HTML, will probably be imported right inside a form -->
<table cellspacing="0" cellpadding="3" border="0">
	<tr>
		<td class="tdFieldTitle delimitedPart" colspan="2">Informations de la configuration 1 :</td>
		<td class="tdFieldTitle delimitedPart" colspan="2">Informations de la configuration 2 :</td>
	</tr>
	<tr>
        <td>
            <br/>
        </td>
    </tr>
	<tr>
        <td></td>
    </tr>
	<tr>
		<td class="tdFieldTitle">G&eacute;n&eacute;rique &quot;Vue Technique&quot; 1 :</td>
        <td><input type="text" id="generic1" name="generic1" value="<%= theDefaultGen1 %>" onchange="adaptTechnicGenerics()"/></td>
		<td class="tdFieldTitle">G&eacute;n&eacute;rique &quot;Vue Technique&quot; 2 (non modifiable) :</td>
        <td><input type="text" id="generic2" name="generic2" value="<%= theDefaultGen2 %>" disabled/></td>
	</tr>
	<tr>
		<td class="tdFieldTitle">G&eacute;n&eacute;rique &quot;Vue Montage&quot; 1 :</td>
        <td><input type="text" id="generic_assemb1" name="generic_assemb1" value="<%= theDefaultGenMont1 %>" onchange="adaptAssembGenerics()"/></td>
		<td class="tdFieldTitle">G&eacute;n&eacute;rique &quot;Vue Montage&quot; 2 (non modifiable) :</td>
        <td><input type="text" id="generic_assemb2" name="generic_assemb2" value="<%= theDefaultGenMont2 %>" disabled/></td>
	</tr>
	<tr>
		<td class="tdFieldTitle required">Produit 1* :</td>
        <td><input type="text" id="product_id1" name="product_id1" value="<%= theDefaultProduct1 %>" onchange="adaptProducts()"/></td>
		<td class="tdFieldTitle">Produit 2 (non modifiable) :</td>
        <td><input type="text" id="product_id2" name="product_id2" value="<%= theDefaultProduct2 %>" disabled/></td>
	</tr>
	<tr>
		<td class="tdFieldTitle required">Unit 1* :</td>
        <td><input type="text" id="unit1" name="unit1" value="<%= theDefaultUnit1 %>"/></td>
		<td class="tdFieldTitle required">Unit 2* :</td>
        <td><input type="text" id="unit2" name="unit2" value="<%= theDefaultUnit2 %>"/></td>
	</tr>
	<tr>
        <td>
            <br/>
            <br/>
        </td>
    </tr>
	<tr>
        <td class="tdFieldTitle" colspan="4" 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() {
		//Conf 1
		var isCheckOk1 = false;
		var isGenericEmpty1 = isOptionalFieldEmpty("generic1");
		var isGenericAssembEmpty1 = isOptionalFieldEmpty("generic_assemb1") ;
		var isExlusive1 = XOR(isGenericEmpty1, isGenericAssembEmpty1);
		var bothEmpty1 = isGenericEmpty1 && isGenericAssembEmpty1;

		if (! (isExlusive1 || bothEmpty1) ){
			document.getElementById("generic1").focus();
			parent.Dialog("Erreur", "Saisir un G&eacute;n&eacute;rique ou un G&eacute;n&eacute;rique Montage dans la configuration 1, le ou est exclusif.");
		}else if (isMandatoryFieldEmpty("product_id1")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire dans la configuration 1 : Produit.");
		}else if (isMandatoryFieldEmpty("unit1")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire dans la configuration 1 : Unit.");
		}else if (!isNumber("unit1")){
			document.getElementById("unit1").focus();
			parent.Dialog("Erreur", "Vous devez saisir un entier dans le champ Unit de la configuration 1.");
		}else{
			isCheckOk1 = true;
		}

		if(!isCheckOk1){
			return isCheckOk;
		}
		
		//Conf 2
		var isCheckOk2 = false;
		var isGenericEmpty2 = isOptionalFieldEmpty("generic2");
		var isGenericAssembEmpty2 = isOptionalFieldEmpty("generic_assemb2") ;
		var isExlusive2 = XOR(isGenericEmpty2, isGenericAssembEmpty2);
		var bothEmpty2 = isGenericEmpty2 && isGenericAssembEmpty2;
		
		if (isMandatoryFieldEmpty("unit2")){
			parent.Dialog("Erreur", "Veuillez saisir le champ obligatoire dans la configuration 2 : Unit.");
		}else if (!isNumber("unit2")){
			document.getElementById("unit2").focus();
			parent.Dialog("Erreur", "Vous devez saisir un entier dans le champ Unit de la configuration 2.");
		}else if (document.getElementById("unit1").value==document.getElementById("unit2").value){
			document.getElementById("unit2").focus();
			parent.Dialog("Erreur", "Les Units saisis doivent &ecirc;tre diff&eacute;rents.");
		}else{
			isCheckOk2 = true;
		}
	
		return isCheckOk2;
	}

	function adaptProducts(){
		document.getElementById("product_id2").value=document.getElementById("product_id1").value;	
	}

	function adaptTechnicGenerics(){
		document.getElementById("generic2").value=document.getElementById("generic1").value;	
	}

	function adaptAssembGenerics(){
		document.getElementById("generic_assemb2").value=document.getElementById("generic_assemb1").value;	
	}
	
</script>