function fieldIsEmpty(itm) {
    return (itm == undefined) || (itm == null) || (itm.value == undefined) || (itm.value == null) || (trim(itm.value).length == 0);
}

function trim(stringToTrim) {
    if ((stringToTrim != null) && (stringToTrim != undefined)) {
        return stringToTrim.replace(/^\s+|\s+$/g,"");
    }
}

function ltrim(stringToTrim) {
    if ((stringToTrim != null) && (stringToTrim != undefined)) {
        return stringToTrim.replace(/^\s+/,"");
    }
}

function rtrim(stringToTrim) {
    if ((stringToTrim != null) && (stringToTrim != undefined)) {
        return stringToTrim.replace(/\s+$/,"");
    }    
}

function ofiOpenWindowCenterWH (urlString, windowName, wSize, hSize) {
    var iLeft   = parseInt((screen.availWidth/2)  - (wSize/2 ));
    var iTop    = parseInt((screen.availHeight/2) - (hSize/2));

    if ((windowName == null) || (windowName.length < 1) ) {
        windowName = "winOfi";
    }
    
    var w = open(urlString
                ,windowName
                ,"Scrollbars=1,resizable=1,location=0,toolbar=0,directories=0,status=0,menubar=0,width=" + wSize + ",height=" + hSize + ",left=" + iLeft + ",top=" + iTop + ",screenX=" + iLeft + ",screenY=" + iTop
                );

    if (w.opener == null) {
        w.opener = self;
    }   

    w.focus();            
}

function ofiOpenWindowCenter (urlString, windowName) {
   ofiOpenWindowCenterWH (urlString, windowName, 800, 600)
}

function ofiPopupFieldHelp(curentItemId, sessionId){
    ofiOpenWindowCenterWH("wwv_flow_item_help.show_help?p_item_id=" + curentItemId + "&p_session=" + sessionId,'Help',800,300);
    return;
}

function ofiCheckDate (objectToCheck) {      
   /* Traitement de la date style OFISA */
   var sai = trim(objectToCheck.value);
   var len = sai.length;
   var retVal = '';
   var dateOK = true;
   var daySai;
   var monSai;
   var yerSai;
   var tstDate;
   var refDate;
   var refYer;
   var objFocus = false

//alert(objectToCheck.value + " * length: " + len + " * refDate: " + refDate);
   if (len > 0) {
     refDate = new Date();
     refYer  = refDate.getYear();
     monSai  = refDate.getMonth();
     yerSai  = (refYer < 1000) ? 1900 + refYer : refYer;
     
     switch (len) {
         case 1:
         case 2:
             daySai = sai
             break;
         case 3:
         case 4:
             daySai = sai.substr(0,2);
             monSai = sai.substr(2,2) - 1;
             break;
         case 5:
             daySai = sai.substr(0,2);
             monSai = sai.substr(2,2) - 1;
             yerSai = yerSai.toString().substr(0,3) + sai.substr(4,1);
             break;
         case 6:
             daySai = sai.substr(0,2);
             monSai = sai.substr(2,2) - 1;
             yerSai = yerSai.toString().substr(0,2) + sai.substr(4,2);
             break;
         case 8:
             daySai = sai.substr(0,2);
             monSai = sai.substr(2,2) - 1;
             yerSai = sai.substr(4,4);
             break;
         default:    
             // dd.mm.yyyy ou faux format
             daySai = sai.substr(0,2);
             monSai = sai.substr(3,2) - 1;
             yerSai = sai.substr(6,4);
     }
     
     if ((monSai < 0) || (monSai > 11)) {
         dateOK = false;
     } else {
         //Un peu de gymnastique pour tester pour le 29.02
         refDate = (new Date(yerSai, monSai, daySai));
         tstDate = (new Date(refYer, monSai + 1, 1));
         tstDate.setDate(tstDate.getDate() - 1) 
         if (tstDate.getMonth() != refDate.getMonth()) {
             dateOK = false;
         }  
     }
     
     if (!dateOK) {
        alert("Mauvaise date");
        objFocus = true;
     } else {
         retVal  =      ((daySai.length == 1) ? "0" + daySai : daySai) + 
                  "." + ((monSai < 9) ? "0" + (monSai + 1) : (monSai + 1)) +
                  "." + yerSai;
     }     
     
     if (objectToCheck.value != retVal) {
        objectToCheck.value = retVal;
     }
   }
   
   if (objFocus) {
      objectToCheck.focus();
   }   
}

function formatDate(dateToFormat){
    var daySai  = dateToFormat.getDate();
    var monSai  = dateToFormat.getMonth();
    var refYer  = dateToFormat.getYear();
    var yerSai  = (refYer < 1000) ? 1900 + refYer : refYer;
    //alert("dd.mm.yyyy: " + daySai + "." + monSai + "." + yerSai);
    var retVal =      ((daySai < 10) ? "0" + daySai : daySai) + 
              "." + ((monSai < 9) ? "0" + (monSai + 1) : (monSai + 1)) +
              "." + yerSai;

    //alert("retVal: " + retVal);
    return retVal

}

function anotherDay(fldName, addDay){
    var fldVal = $v(fldName);
    //alert(fldName + " : " + fldVal);
    var refDate = new Date(fldVal.substring(6,10), fldVal.substring(3,5) - 1, fldVal.substring(0,2));
    refDate.setDate(refDate.getDate() -(-addDay));
    //alert("Date: " + refDate)
    $s(fldName, formatDate(refDate));
}

function prevDay(fldName){
    anotherDay(fldName, -1)
}

function nextDay(fldName){
    anotherDay(fldName, 1)
}

function ofiCopyElementValue(sourceElement, targetElement) {
//alert("Source id: " + sourceElement.id + " * Target: " + targetElement.id);
    if (targetElement.type == "checkbox") {
        if (sourceElement.type == "checkbox") {
            targetElement.checked = sourceElement.checked;
        } else {
            targetElement.checked = (sourceElement.value == 'X');
        }   
    } else {
        if (sourceElement.type == "checkbox") {
            targetElement.value = (sourceElement.checked ? 'X' : null);
        } else {
            targetElement.value = sourceElement.value;
        }
    }
}

function ofiDetailSetOneField (rwMode, docSource, sourceId, pagePrefix) {
   var sourceElement = docSource.getElementById(sourceId);
   var targetName;
   var targetElement;

   if (sourceElement != null) {
      targetName = sourceElement.getAttribute('apexid');
      if ((targetName != null) && (targetName != '')){
         targetElement = document.getElementById(pagePrefix + targetName);
         if (targetElement != null) {
            if (rwMode == 'W') {
               ofiCopyElementValue(targetElement, sourceElement);
            } else {
               ofiCopyElementValue(sourceElement, targetElement)
            }
         }
      }
   }
}

function ofiDetailSetValues (rwMode, idPage) {

   var idLine = idPage + '_LINE';
   var cnt;
   var fldId;
   var docSource = opener.document;

   var currLine = document.getElementById(idLine).value;

   for (cnt=1; cnt < 101; cnt++) {
      if (cnt < 10) {
         fldId = 'f0' + cnt;
      } else {
         fldId = 'f' + cnt;
      }
//alert("ofiDetailSetValues. field: " + fldId);      
      ofiDetailSetOneField(rwMode, docSource, fldId + '_' + currLine, idPage + '_');
   }   
}

function ofiSetFieldToValue (docCible, cibleId, assignArray) {
   var cibleElement = docCible.getElementById(cibleId);
   var sourceName;
   var valueToAssign;

   if (cibleElement != null) {
       sourceName = cibleElement.getAttribute('apexid');
       if ((sourceName != null) && (sourceName != '')){
           valueToAssign = assignArray[sourceName];
           if (valueToAssign != undefined) {
               //alert("Setting " + cibleId + " (" + sourceName + ") to " + valueToAssign);              
               if (cibleElement.type == "checkbox") {
                   cibleElement.checked = (valueToAssign == 'X');
               } else {
                   cibleElement.value   = valueToAssign;
               }
           }
       }
   }
}

function ofiRechercheSetValues (idPage, assignArray) {
    var idLine = idPage + '_LINE';
    var cnt;
    var fldId;
    var docToSet = opener.document;
    var currLine = document.getElementById(idLine).value;
    for (cnt=1; cnt < 101; cnt++) {
        if (cnt < 10) {
           fldId = 'f0' + cnt;
        } else {
           fldId = 'f' + cnt;
        }
    
        ofiSetFieldToValue(docToSet, fldId + '_' + currLine, assignArray);
    }   
}

function ofiDetailSetValuesAndClose(idPage) {
   ofiDetailSetValues('W', idPage);
   close();
}

function setCascadeLOV (pSlaveName, pValues, pIncludeNull, pDefaultValue) {
    var len;
    var j;
    var l_Select = $x(pSlaveName);

//alert("pIncludeNull: " + pIncludeNull + " * Values: " + pValues);
    if (l_Select){
        l_Select.length = 0;
        if (pValues){
            //var myJSONObject = pValues.parseJSON();
            var myJSONObject = JSON.parse(pValues);
        
            len = myJSONObject.LOV.length;

            if (pIncludeNull) {
                appendToSelect(l_Select, '%null%', "\xE0 saisir", pDefaultValue)
            }
        
            for (j=0;j<len;j++){  
                //alert(myJSONObject.LOV[j][0] + " / " + myJSONObject.LOV[j][1]); 
                appendToSelect(l_Select, myJSONObject.LOV[j][0], myJSONObject.LOV[j][1], pDefaultValue)
            }
        } else {
            if (pIncludeNull){
                appendToSelect(l_Select, '%null%', "\xE0 saisir", pDefaultValue)
            }
        }
    }
}
      
function cascadeLOV(pMasterName, pSlaveName, pCodLan, pWithoutTail, pLib, pIncludeNull, pDefaultValue) {
    var gReturn;
    var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=odpGetLOV',0);    
    //alert('Adding ' + $v(pMasterName));  
    get.addParam('x01',$v(pMasterName)); 
    get.addParam('x02',pCodLan); 
    get.addParam('x03',pWithoutTail); 
    get.addParam('x04',pLib); 
    
    gReturn = get.get();  
    get = null; 
    
    setCascadeLOV (pSlaveName, gReturn, pIncludeNull, pDefaultValue);
}

function appendToSelect(pSelect, pValue, pContent, pDefaultValue) {
    var l_Opt = document.createElement("option");
    l_Opt.value = pValue;
    if(document.all){
        /* IE */
        pSelect.options.add(l_Opt);
        l_Opt.innerText = pContent;
    } else {
        /* Firefox et al. */
        l_Opt.appendChild(document.createTextNode(pContent));
        pSelect.appendChild(l_Opt);
    }
    
    if ((pDefaultValue == pValue) || ((pDefaultValue == 'setNull') && (pValue == '%null%'))) {
       pSelect.value = pValue;
    }
}

function getSelectLabel(selectElement){
   return (selectElement.options[selectElement.options.selectedIndex].text);
}

function ofiGetInfoFromPage (sourcePage, targetPage, assignArray) {
    var sourceDoc = opener.document;
    var sourceElement
    var targetElement
    var cnt = 0

    for (cnt = 0; cnt < assignArray.length; cnt++) {
        sourceElement = sourceDoc.getElementById('P' + sourcePage + '_' + assignArray[cnt][0]);
        if (sourceElement != null) {
            targetElement = document.getElementById('P' + targetPage + '_' + assignArray[cnt][1]);
            if (targetElement != null) {
                if (sourceElement.type == "select-one") {
                    if ((targetElement.type == "select-one") || (assignArray[cnt][2] == "V")) {
                        targetElement.value = sourceElement.value;
                    } else {
                        //targetElement.value = sourceElement.options[sourceElement.options.selectedIndex].text;
                        targetElement.value = getSelectLabel(sourceElement);
                    }
             
                } else {
                    targetElement.value = sourceElement.value;
                }
          
            }
        }
    }   
}

function handleCheckboxes(cBox, fldName){
    $("[name=" + fldName + "]:checkbox").attr('checked', cBox.checked);
}


function initDebug(pNomDbg){
    var gReturn;
    var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=odpInitDebug',0);  
    get.addParam('x01',pNomDbg);  
    gReturn = get.get();  
    get = null;  
}

function writeDebug(pNomDbg, pMsgDbg){
    var gReturn;
    var get = new htmldb_Get(null,$v('pFlowId'),'APPLICATION_PROCESS=odpWriteDebug',0);  
    get.addParam('x01',pNomDbg);  
    get.addParam('x02',pMsgDbg);  
    gReturn = get.get();  
    get = null;  
}


