//------------------------------------------------------------------------------
// FICHIER : $RCSfile: gsite.js,v $
// AUTEUR  : $Author: lc $
// VERSION : $Revision: 1.10 $
// DATE    : $Date: 2003/09/18 20:13:32 $
//------------------------------------------------------------------------------
// GSite - Web site management in PHP - main js file
//
// Copyright (C) 2002 COUDOUNEAU Laurent (lc@gsite.org)
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//------------------------------------------------------------------------------

//==============================================================================
//==============================================================================

function gsGetDivByName (sDiv) {
  var namedList = document.getElementsByName (sDiv);
  return namedList.item (0);
}

//==============================================================================
//==============================================================================

function gsSwitchDivVisibility (sDiv) {
  var oDiv = gsGetDivByName (sDiv);
  if (oDiv == null) return;
  oDiv.style.display = (oDiv.style.display == "none" ? "block" : "none");
}

//==============================================================================
//==============================================================================

var gsCurrentDiv = null;

function gsShowHideDiv (sDiv, sDivDft) {
  if (gsCurrentDiv == null) gsCurrentDiv = sDivDft;
  
  gsSwitchDivVisibility (gsCurrentDiv);
  gsSwitchDivVisibility (sDiv);
  
  gsCurrentDiv = sDiv;
}

//==============================================================================
//==============================================================================

function openBasicPopup (sURL, iWidth, iHeight, sName) {
  if (arguments.length == 3) {
    sName = 'popup';
  }
  
  if (arguments.length == 2) {
    sName   = 'popup';
    iHeight = 400;
  }
  
  if (arguments.length == 1) {
    sName   = 'popup';
    iWidth  = 660;
    iHeight = 400;
  }
  
  var sArgs = 'toolbar=0'+
              ',hotkeys=0'+
              ',location=0'+
              ',directories=0'+
              ',menubar=0'+
              ',personalbar=0'+
              ',status=0'+
              ',scrollbars=1'+
              ',resizable=1'+
              ',width='+iWidth+
              ',height='+iHeight;
  
  var wWindow = open (sURL, sName, sArgs);
  wWindow.window.focus ();
}

//==============================================================================
//==============================================================================

function openPopup (sModule, sParam, iWidth, iHeight, sName) {
  var sURL = 'popup.php?module='+escape (sModule);

  if (sParam != '') {
    sURL += sParam;
  }
  
  if (arguments.length == 4) {
    sName = 'popup';
  }
  
  if (arguments.length == 3) {
    sName   = 'popup';
    iHeight = 400;
  }
  
  if (arguments.length == 2) {
    sName   = 'popup';
    iWidth  = 660;
    iHeight = 400;
  }
  
  var sArgs = 'toolbar=0'+
              ',hotkeys=0'+
              ',location=0'+
              ',directories=0'+
              ',menubar=0'+
              ',personalbar=0'+
              ',status=0'+
              ',scrollbars=1'+
              ',resizable=1'+
              ',width='+iWidth+
              ',height='+iHeight;
  
  var wWindow = open (sURL, sName, sArgs);
  wWindow.window.focus ();
}

//==============================================================================
//==============================================================================

function openUser (sProject, sAnnu, sLocale, iUser, bMessenger) {
  var sURL = 'popup.php'+
             '?project='+escape (sProject)+
             '&locale='+escape (sLocale)+
             '&annu='+escape (sAnnu)+
             '&module=annu'+
             '&fct=showUser'+
             '&userid='+iUser+
             '&with_msg='+bMessenger;
  
  var sArgs = 'toolbar=0'+
              ',hotkeys=0'+
              ',location=1'+
              ',directories=0'+
              ',menubar=0'+
              ',personalbar=0'+
              ',status=0'+
              ',scrollbars=1'+
              ',resizable=1'+
              ',width=650'+
              ',height=400';
  
  var wWindow = open (sURL, 'user', sArgs);
  wWindow.window.focus ();
}

//==============================================================================
//==============================================================================

function openMessenger (sProject, sLocale, iTo) {
  var sURL = 'popup.php'+
             '?project='+escape (sProject)+
             '&locale='+escape (sLocale)+
             '&module=messenger'+
             '&fct=userMessages'+
             '&engine_cmd=newmsg'+
             '&msg_to='+iTo;
  
  var sArgs = 'toolbar=0'+
              ',hotkeys=0'+
              ',location=0'+
              ',directories=0'+
              ',menubar=0'+
              ',personalbar=0'+
              ',status=0'+
              ',scrollbars=1'+
              ',resizable=1'+
              ',width=650'+
              ',height=400';
  
  var wWindow = open (sURL, 'messenger', sArgs);
  wWindow.window.focus ();
}

//==============================================================================
//==============================================================================

function openBrowser (sProject, sAnnu, sProjectadm, sLocale, sForm, sFile, sPath) {
  var sURL = 'popup.php'+
             '?project='+escape (sProject)+
             '&locale='+escape (sLocale)+
             '&annu='+escape (sAnnu)+
             '&projectadm='+escape (sProjectadm)+
             '&module=dir'+
             '&fct=browser'+
             '&browser_form='+escape (sForm)+
             '&browser_file='+escape (sFile)+
             '&browser_path='+escape (sPath);
  
  var sArgs = 'toolbar=0'+
              ',hotkeys=0'+
              ',location=0'+
              ',directories=0'+
              ',menubar=0'+
              ',personalbar=0'+
              ',status=0'+
              ',scrollbars=1'+
              ',resizable=1'+
              ',width=600'+
              ',height=400';
  
  var wWindow = open (sURL, 'browser', sArgs);
  wWindow.window.focus ();
}

//==============================================================================
//==============================================================================

function eraseBrowserFields (oField1, oField2) {
  oField1.value = '';
  oField2.value = '';
}

//==============================================================================
//==============================================================================

function openDocumentPopup (sProject, sLocale, sDoc, sSet, sParam, iWidth, iHeight) {
  var sURL = 'document.php'+
             '?project='+escape (sProject)+
             '&locale='+escape (sLocale)+
             '&doc='+escape (sDoc)+
             '&set='+escape (sSet);
  
  if (sParam != '') {
    if (sParam[0] != '&') sURL += '&';
    
    sURL += sParam;
  }
  
  if (arguments.length == 6) {
    iHeight = 400;
  }
  
  if (arguments.length == 5) {
    iWidth  = 660;
    iHeight = 400;
  }
  
  var sArgs = 'toolbar=0'+
              ',hotkeys=0'+
              ',location=0'+
              ',directories=0'+
              ',menubar=0'+
              ',personalbar=0'+
              ',status=0'+
              ',scrollbars=1'+
              ',resizable=1'+
              ',width='+iWidth+
              ',height='+iHeight;
  
  var wWindow = open (sURL, 'popupdoc', sArgs);
  wWindow.window.focus ();
}

//==============================================================================
//==============================================================================

function checkFrenchZipCode (oZip, oDpt, sMsg) {
  var sZip = oZip.value;
  
  if (sZip.length == 0) {
    oDpt.value = 900;
    
  } else {
    if (sZip.length != 5) {
      window.alert (sMsg+' ('+sZip+')');
      return false;
    }
    
    var sCode = '';
    if ((sZip.charAt (0) == '9') && ((sZip.charAt (1) == '7') || (sZip.charAt (1) == '8'))) {
      sCode = sZip.charAt (0) + sZip.charAt (1) + sZip.charAt (2);
      
    } else {
      sCode = sZip.charAt (0) + sZip.charAt (1);
    }
    
    var iCode = Number (sCode);
    if (! (((iCode >    0) && (iCode <=  95)) ||
           ((iCode >= 971) && (iCode <= 976)) ||
            (iCode == 980) ||
           ((iCode >= 986) && (iCode <= 988)))) {
      window.alert (sMsg+' ('+sZip+')');
      return false;
    }
    
    oDpt.value = iCode;
  }
  
  return true;
}

//==============================================================================
//==============================================================================

function isYear (sYear) {
  if (sYear.length > 0) {
    if (! isNaN (Number (sYear))) {
      if (sYear >= 1752) return true;
    }
  }
  
  return false;
}

//==============================================================================
//==============================================================================

function isLeapYear (y) {
  return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
}

//==============================================================================
//==============================================================================

function daysInMonth (m, y) {
  if ((m >= 1) && (m <= 12)) {
    if (m ==  1) return 31;
    if (m ==  3) return 31;
    if (m ==  4) return 30;
    if (m ==  5) return 31;
    if (m ==  6) return 30;
    if (m ==  7) return 31;
    if (m ==  8) return 31;
    if (m ==  9) return 30;
    if (m == 10) return 31;
    if (m == 11) return 30;
    if (m == 12) return 31;
    
    if (isLeapYear (y)) {
      return 29;
      
    } else {
      return 28;
    }
    
  } else {
   return 0;
  }
}

//==============================================================================
//==============================================================================

function checkDate_fr (sDate) {
  if (sDate.match (/\d{2}\/\d{2}\/\d{4}/) == null) {
    return false;
  }
  
  var daystr   = sDate.substr (0, 2);
  var monthstr = sDate.substr (3, 2);
  var yearstr  = sDate.substr (6, 4);
  
  return ((daystr >= 1) && 
          (daystr <= daysInMonth (monthstr, yearstr)) &&
          (monthstr >= 1) && (monthstr <= 12) &&
          (yearstr >= 1752))
}

//==============================================================================
//==============================================================================

function checkDateNull_fr (sDate) {
  if (sDate.match (/\d{2}\/\d{2}\/\d{4}/) == null) {
    return false;
  }
  
  var daystr   = sDate.substr (0, 2);
  var monthstr = sDate.substr (3, 2);
  var yearstr  = sDate.substr (6, 4);
  
  return ( ((daystr >= 1) && 
           (daystr <= daysInMonth (monthstr, yearstr)) &&
           (monthstr >= 1) && (monthstr <= 12) &&
           (yearstr >= 1752)) || 
           ((daystr=='00') && (monthstr=='00') && (yearstr =='0000')) )
}

//==============================================================================
//==============================================================================

function checkDateOrder_fr (sDate1, sDate2) {
  var daystr1   = sDate1.substr (0, 2);
  var monthstr1 = sDate1.substr (3, 2);
  var yearstr1  = sDate1.substr (6, 4);
  
  var daystr2   = sDate2.substr (0, 2);
  var monthstr2 = sDate2.substr (3, 2);
  var yearstr2  = sDate2.substr (6, 4);
  
  if ((daystr1=='00') && (monthstr1=='00') && (yearstr1=='0000')) return true;
  if ((daystr2=='00') && (monthstr2=='00') && (yearstr2=='0000')) return true;
  
  var date1 = new Date (yearstr1, monthstr1 - 1, daystr1)
  var date2 = new Date (yearstr2, monthstr2 - 1, daystr2)
  
  return (date1 < date2)
}

//==============================================================================
//==============================================================================

function checkDate_en (sDate) {
  if (sDate.match (/\d{4}\/\d{2}\/\d{2}/) == null) {
    return false;
  }
  
  var yearstr  = sDate.substr (0, 4);
  var monthstr = sDate.substr (5, 2);
  var daystr   = sDate.substr (8, 2);
  
  return ((daystr >= 1) && 
          (daystr <= daysInMonth (monthstr, yearstr)) &&
          (monthstr >= 1) && (monthstr <= 12) &&
          (yearstr >= 1752))
}

//==============================================================================
//==============================================================================

function checkDateNull_en (sDate) {
  if (sDate.match (/\d{4}\/\d{2}\/\d{2}/) == null) {
    return false;
  }
  
  var yearstr  = sDate.substr (0, 4);
  var monthstr = sDate.substr (5, 2);
  var daystr   = sDate.substr (8, 2);
  
  return ( ((daystr >= 1) && 
           (daystr <= daysInMonth (monthstr, yearstr)) &&
           (monthstr >= 1) && (monthstr <= 12) &&
           (yearstr >= 1752)) || 
           ((daystr=='00') && (monthstr=='00') && (yearstr=='0000')) )
}

//==============================================================================
//==============================================================================

function checkDateOrder_en (sDate1, sDate2) {
  var yearstr1  = sDate1.substr (0, 4);
  var monthstr1 = sDate1.substr (5, 2);
  var daystr1   = sDate1.substr (8, 2);
  
  var yearstr2  = sDate2.substr (0, 4);
  var monthstr2 = sDate2.substr (5, 2);
  var daystr2   = sDate2.substr (8, 2);
  
  if ((daystr1=='00') && (monthstr1=='00') && (yearstr1=='0000')) return true;
  if ((daystr2=='00') && (monthstr2=='00') && (yearstr2=='0000')) return true;
  
  var date1 = new Date (yearstr1, monthstr1 - 1, daystr1)
  var date2 = new Date (yearstr2, monthstr2 - 1, daystr2)
  
  return (date1 < date2)
}

//------------------------------------------------------------------------------
// $Log: gsite.js,v $
// Revision 1.10  2003/09/18 20:13:32  lc
// openPopup : no location, no menubar
//
// Revision 1.9  2003/04/22 15:03:07  lc
// Div management.
//
// Revision 1.8  2003/04/01 14:43:45  lc
// Add gsGetDivByName
//
// Revision 1.7  2002/11/21 11:49:49  lc
// Bug on month nullity check.
// Add nullity check into date comparison.
//
// Revision 1.6  2002/11/08 11:25:21  lc
// Add function openBasicPopup
// Bug in openPopup for arguments count ?
//
// Revision 1.5  2002/09/13 07:51:08  lc
// LC0043 : add a popup to show infos about an user
//
// Revision 1.4  2002/09/12 10:49:56  lc
// Add popup name in openPopup function.
//
// Revision 1.3  2002/06/19 11:22:06  lc
// Bug LC0024.
// Add default values for height and width.
//
// Revision 1.2  2002/06/18 13:20:52  lc
// Add openMessenger.
//
// Revision 1.1  2002/06/14 07:39:34  lc
// Initial revision
//
//-- End of source  ------------------------------------------------------------
