
/***************************************************************************/
/* public: get_items()
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object   the table row
 * @param   string   the action calling this script (over, out or click)
 * @param   string   the default background color
 * @param   string   the color to use for mouseover
 * @param   string   the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 *
 */

function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = theRow ;
    var currentColor = null;
    var newColor     = null;

    currentColor = theCells.style.backgroundColor ;

    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }

    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
        if (theAction == 'out') {
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }

    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            //newColor = (thePointerColor != '')
            //         ? thePointerColor
            //         : theDefaultColor;
            newColor = theDefaultColor ;
 
        }
    } // end 4

    // 5. Sets the new color...

    theCells.style.backgroundColor = newColor ;

    return true;

} // end of the 'setPointer()' function


/***************************************************************************/
/* public: category_select_tree (theCell, theForm, click_event, theDefaultColor, thePointerColor, theMarkColor)
 *
 */

function category_select_tree (theCell, theForm, click_event, theDefaultColor, thePointerColor, theMarkColor) {
  var formObj = document.forms[theForm] ;
  var current_category_id = formObj.elements["category_selected"].value ;
  var default_category_id = formObj.elements["category_default"].value ;
  var category_id = theCell.category_id ;
  var child_group = theCell.child_group ;
  var new_category_id = default_category_id ;
  var click_handler = click_event ? eval(click_event) : null ;

  if (typeof(formObj.lastCellClicked) == "undefined") {
    formObj.lastCellClicked = null ;
  }

  if ((formObj.lastCellClicked != null) && (formObj.lastCellClicked != theCell)) {
    setPointer(formObj.lastCellClicked, 'click', theDefaultColor, thePointerColor, theMarkColor) ;
  }

  setPointer(theCell, 'click', theDefaultColor, thePointerColor, theMarkColor) ;

  if (current_category_id != category_id) {
    new_category_id = category_id ;
    formObj.lastCellClicked = theCell ;
  }
  else {
    formObj.lastCellClicked = null ;
  }

  formObj.elements["category_selected"].value = new_category_id ;

  if (click_handler) {
    click_handler (new_category_id, theCell) ;
  }

}



/***************************************************************************/
/* public: zone_select_tree(theCell, theForm, click_event, theDefaultColor, thePointerColor, theMarkColor)
 *
 */

function zone_select_tree (theCell, theForm, click_event, theDefaultColor, thePointerColor, theMarkColor) {
  var formObj = document.forms[theForm] ;
  var current_zone_id = formObj.elements["zone_selected"].value ;
  var default_zone_id = formObj.elements["zone_default"].value ;
  var zone_id = theCell.zone_id ;
  var new_zone_id = default_zone_id ;
  var click_handler = click_event ? eval(click_event) : null ;

  if (typeof(formObj.lastCellClicked) == "undefined") {
    formObj.lastCellClicked = null ;
  }

  if ((formObj.lastCellClicked != null) && (formObj.lastCellClicked != theCell)) {
    setPointer(formObj.lastCellClicked, 'click', theDefaultColor, thePointerColor, theMarkColor) ;
  }

  setPointer(theCell, 'click', theDefaultColor, thePointerColor, theMarkColor) ;

  if (current_zone_id != zone_id) {
    new_zone_id = zone_id ;
    formObj.lastCellClicked = theCell ;
  }
  else {
    formObj.lastCellClicked = null ;
  }

  formObj.elements["zone_selected"].value = new_zone_id ;

  if (click_handler) {
    click_handler (new_zone_id) ;
  }

}



/***************************************************************************/
/* public: trim (chaine)
 *
 */

function trim (chaine) {
  re = /^\s*((.|\s)+)/i ;
  if (result = re.exec(chaine)) {
    chaine = result[1] ;
    
    re = /((.|\s)+[^\s])\s*$/i ;
    if (result = re.exec(chaine)) {
      chaine = result[1] ;
    }
  }

  return (chaine) ;

}


/***************************************************************************/
/* public: openWindow (url, width, height)
 *
 */

function openWindow (url, width, height) {
  window.open (url, "", "toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=no, width=" + width + ", height=" + height) ;
}


/***************************************************************************/
/* public: ex_openWindow (url, window_features)
 *
 */

function ex_openWindow (url, window_features) {
  var new_window = window.open (url, '', window_features) ;
  new_window.focus() ;
}


/***************************************************************************/
/* public: extract_file_name (full_filename)
 *
 */

function extract_file_name (full_filename) {
  filename = "" ;
  re = /(.*[\\\\\/])?([\w\.\ ]+)$/i ;
  result = re.exec (full_filename) ;
  if (result) filename = result[2] ;
  
  return (filename) ;

}


/***************************************************************************/
/* public: extract_file_ext (full_filename)
 *
 */

function extract_file_ext (full_filename) {
  ext = "" ;
  filename = trim(extract_file_name (full_filename)) ;
  re = /(\..*)$/i ;
  result = re.exec(filename) ;
  if (result) ext = result[1] ;

  return (ext) ;

}


/***************************************************************************/
/* public: is_empty (value)
 *
 */

function is_empty (value) {
  if (value == null) return (true) ;
  re = /^\s*$/ ;
  if (re.exec(value)) return (true) ;
  return (false) ;

}


/***************************************************************************/
/* public: is_email (value)
 *
 */

function is_email (value) {
  re = /^[^\@]+\@[^\@]+$/ ;
  if (re.exec(value)) return (true) ;
  return (false) ;

}


/***************************************************************************/
/* public: is_numeric (value)
 *
 */

function is_numeric (value) {
  re = /^\d+$/ ;
  if (re.exec(value)) return (true) ;
  return (false) ;

}


/***************************************************************************/
/* fills left side of a number with zero until certain length is reached
 *
 */

function decimal_to_string (n, d) {
  var result = n.toString() ;

  while (result.length < d) {
    result = '0' + result ;
  }

  return (result) ;

}



/***************************************************************************/
/* Binary / Hexadecimal / decimal conversion functions
 *
 */

function dec_digit_to_hex (aDigit) {
  return("0123456789ABCDEF".substring(aDigit, aDigit+1))
}

function to_hex (n, d) {
  var hexValue = '' ;
  
  for (var i = 0 ; i < d ; i++) {
    hexValue = dec_digit_to_hex ((0xf << (4 * i) & n) >> (4 * i)) + hexValue ;
  }

  return (hexValue) ;

}

function to_bin (n, d) {
  var binValue = n.toString(2) ;
  
  while (binValue.length < d) {
    binValue = '0' + binValue ;
  }
  
  return (binValue) ;

}

function to_decimal (hexNum) {
  var tmp = '0x' + hexNum ;
  return (eval(tmp)) ;
}


/***************************************************************************/
/* public : open_custom_link (url, winFeatures) 
 *
 *   winFeatures - sous forme Hexadécimal sur 9 digits x xxx xxx xx
 *     (x) xxx xxx xx : 1 - Fenêtre courante
 *                      2 - Nouvelle fenêtre : Utilise Width et Height
 *                      3 - Fenêtre personnalisée (Les valeurs suivantes 
 *                          concernent surtout la 2ème et 3ème option)
 *  
 *     x (xxx) xxx  xx  : Width
 *     x  xxx (xxx) xx  : Height
 *     x  xxx  xxx (xx) : Valeur sur 8 bits. bbbbbbbb - Seuls les 7 bits inférieurs sont utilisés.
 *                        00000001 - toolbar
 *                        00000010 - location
 *                        00000100 - directories
 *                        00001000 - status
 *                        00010000 - menubar
 *                        00100000 - scrollbars
 *                        01000000 - resizable
 *
 */

function open_custom_link (url, winFeatures) {

  if (winFeatures.length != 9) {
    return ;
  }
  
  var win_type   = (to_decimal(winFeatures.substring(0,1))) ;
  var win_width  = (to_decimal(winFeatures) & 0x0fff00000) >> (5 * 4) ;
  var win_height = (to_decimal(winFeatures) & 0x0000fff00) >> (2 * 4) ;
  var win_prefs  = (to_decimal(winFeatures) & 0x0000000ff) >> (0 * 4) ;
  
  win_width = (win_width == 0) ? screen.width : win_width ;
  win_height = (win_height == 0) ? screen.height : win_height ;
  
  switch (win_type) {
    case 2 :
      new_window = true ;
      win_prefs = 127 ;
      break ;
    
    case 3 :
      new_window = true ;
      break ;    
    
    default :
      new_window = false ;  
  
  }

  // -- construction des chaînes de préférence
  if (new_window) {
    var str_win_prefs = 'width=' + win_width ;
    str_win_prefs += ',height=' + win_height ;
    
    str_win_prefs += ',toolbar='     + (((win_prefs &  1) ==  1) ? 'yes' : 'no') ;
    str_win_prefs += ',location='    + (((win_prefs &  2) ==  2) ? 'yes' : 'no') ;
    str_win_prefs += ',directories=' + (((win_prefs &  4) ==  4) ? 'yes' : 'no') ;
    str_win_prefs += ',status='      + (((win_prefs &  8) ==  8) ? 'yes' : 'no') ;
    str_win_prefs += ',menubar='     + (((win_prefs & 16) == 16) ? 'yes' : 'no') ;
    str_win_prefs += ',scrollbars='  + (((win_prefs & 32) == 32) ? 'yes' : 'no') ;
    str_win_prefs += ',resizable='   + (((win_prefs & 64) == 64) ? 'yes' : 'no') ;
    
    window.open (url, '', str_win_prefs) ;
    return ;

  }
  
  window.location.href = url ;
  
}


/***************************************************************************/
/* public : open_internal_link (value, link_type, winfeatures) 
 *
 *   link_type: 0 - Indique que 'value' est un category_id
 *              1 - Indique que 'value' est un article_id
 *   
 *   winfeatures : Valeur hexadécimale similaire à celle employée dans la 
 *                 fonction 'open_custom_link'
 *
 */

function open_internal_link (value, link_type, winfeatures) {
  switch (link_type) {
    case 0 :
      url = 'category.php?category_id=' + value ;
      break ;

    case 1 :
      url = 'article.php?article_id=' + value ;
      break ;

  }

  open_custom_link (url, winfeatures) ;

}


/***************************************************************************/
/* public: is_date (date_value, showErr)
 *
 */

function is_date (date_value, showErr) {
  var month_end_list = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;
  
  re = /^(\d+)\/(\d+)\/(\d+)$/ ;
  var date_result = re.exec(date_value) ;
  if (!date_result) {
    if (showErr) alert ("Cette valeur n'est pas une date au format jj/mm/aa") ;
    return (false) ;
  }

  var day = parseFloat(date_result[1]) ;
  var month = parseFloat(date_result[2]) ;
  var year = parseFloat(date_result[3]) ;

  // -- Vérification année
  if (year == 0) {
    if (showErr) alert ('Année incorrecte') ;
    return(false) ;

  }
  
  // -- Vérification mois
  if ((month == 0) || (month > 12)) {
    if (showErr) alert ('Mois incorrect') ;
    return(false) ;
  }
  
  // -- Vérification jour
  var month_end = (month == 2) ? ((year % 4) ? 28 : 29) : month_end_list [month - 1] ;
  
  if (day == 0) {
    if (showErr) alert ("Jour ne peut être égal à zéro") ;
    return (false) ;
  }
  
  if (day > month_end) {
    if (showErr) alert ("Jour trop grand pour le mois sélectionné") ;
    return (false) ;
  }

  return (true) ;

}


/***************************************************************************/
/* find_control (frmObj, ctrlNamePattern)
 * 
 *   Fonction effectuant la recherche de controles dans un FORM à 
 *   partir d'expressions régulières définies par 'ctrlNamePattern'.
 *
 *   Si rien n'est trouvé, la valeur NULL est envoyée.
 *
 *   Le résultat renvoyé est un tableau pour chaque controle trouvé.
 *   
 *     Ex: result[0][0] -> Nom du controle
 *                  [1] -> Désigne la 1ère parenthèse capturante
 *                  ...
 *                  [9] -> Désigne la 9ème parenthèse capturante 
 *     
 */ 

  function find_control (frmObj, ctrlNamePattern) {
    var ctrlObj, ctrlName, match ;
    var matches = new Array() ;
    
    var re = new RegExp() ;
    re.compile (ctrlNamePattern) ;
    
    var i = 0 ;
    
    for (ctrlName in frmObj.elements) {
      ctrlObj = frmObj.elements[ctrlName] ;
      
      if ((typeof(ctrlObj) == 'object') && (ctrlObj != null)) {
        if ((ctrlObj.type == "text") || (ctrlObj.type == "textarea") || 
            (ctrlObj.type == "file") || (ctrlObj.type == "hidden") ||
            (ctrlObj.type == "checkbox")) {
          if (match = re.exec(ctrlName)) {
            matches[i++] = match ;
          }
        }
      }
    
    }
    
    if (matches.length == 0) {
      matches = null;
    }
    
    return (matches) ;
  
  }


/***************************************************************************/
/* get_control_value
 * 
 */
 
 function get_control_value (ctrl_obj) {
   var value = '' ;
   
   if (ctrl_obj.length) {
     for (var i = 0 ; i < ctrl_obj.length ; i++) {
       if (ctrl_obj[i].checked) {
         value = ctrl_obj[i].value ;
         break ;
       }
     }
   }
   else {
     value = ctrl_obj.value ;
   }
   
   return (value) ;
   
 }
 
 
 
