//  $Id: script.js,v 1.7 2008/02/13 20:46:42 mclark Exp $
//  Copyright (C)2008 Gorges Web Sites. All Rights Reserved.

//  browser detection

var isIE5 = (document.all && (navigator.appVersion.indexOf('MSIE 5') != -1)) ? 1 : 0;
var isIE6 = (document.all && (navigator.appVersion.indexOf('MSIE 6') != -1)) ? 1 : 0;

//  text trimming

function trim(text) {
  if (text.length > 0) {
    while (text.substring(0,1) == ' ')
      text = text.substring(1, text.length);
    while (text.substring(text.length - 1, text.length) == ' ')
      text = text.substring(0, text.length - 1);
  };
  return text;
}

//  finding an element

function get_element(id) {
  if (document.getElementById)
    return document.getElementById(id);
  else if (document.all)
    return document.all[id];
  return null;
}

//  values

function get_value(form, obj) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  return obj ? obj.value : null;
}
function set_value(form, obj, value) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  if (obj) {
    if (typeof(obj.selectedIndex) == 'number')
      obj.selectedIndex = value;
    else
      obj.value = value;
  }
}

//  help

function help_click(hid) {
  //  toggle help
  var help = $('#hlp' + hid);
  var frame = $('#hfrm' + hid);
  var shown = (help.css('display') != 'none');
  var icon = $('#i' + hid + ' img');
  //  reset all help icons (note: not() not working)
  $('.icon-help img').not(icon).attr('src', 'images/blank.gif');
  //  display or hide
  if (!shown) {
    //  set location of help
    var iconOffset = $(icon).offset();
    var iconWidth = $(icon).width();
    var docWidth = $(document).width();
    var helpWidth = help.width();
    var helpHeight = help.height();
    //alert('docWidth=' + docWidth + ' iconOffset.left=' + iconOffset.left + ' iconWidth=' + iconWidth + ' help=' + helpWidth + ',' + helpHeight);
    //alert('+=' + (iconOffset + iconWidth + helpWidth) + ', docWidth=' + docWidth);
    help.css('top', iconOffset.top - (helpHeight / 3));
    help.css('left', iconOffset.left + ((iconOffset.left + iconWidth + helpWidth < docWidth) ? iconWidth : -(helpWidth + 12)));
    //  show
    help.slideDown('', function() {
      if (isIE5 || isIE6) {
        var helpOffset = help.offset();
        frame.css('top', helpOffset.top);
        frame.css('left', helpOffset.left);
        frame.width($('#hdiv' + hid).width() + 'px');
        frame.height($('#hdiv' + hid).height() + 'px');
        frame.show();
      }
    });
    //  unhelp icon
    $(icon).attr('src', 'images/icons/unhelp.gif');
  } else {
    //  hide
    if (isIE5 || isIE6)
      frame.hide();
    help.slideUp();
    //  clear unhelp icon
    $(icon).attr('src', 'images/blank.gif');
  }
  //  hide all other help messages
  if (isIE5 || isIE6)
    $('.help-iframe').not(frame).hide();
  $('.help-container').not(help).hide();
}
function help_close() {
  $('.help-container').hide();
}


function desc_click(id) {
  $('#d' + id).slideToggle('slow');
}
function desc_close(id) {
  $('#d' + id).slideUp('slow');
}

//  calendar

function y2k(number) {
  return (number < 1000) ? number + 1900 : number;
}
var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var field = '';
function padout(number) {
  return (number < 10) ? '0' + number : number;
}
function restart() {
  if (field)
    eval('document.' + field + '.value = "" + padout(month - 0 + 1) + "/" + padout(day) + "/" + year');
  mywindow.close();
}
function popup_calendar(field2) {
  field = field2;
  mywindow = open('calendar.html', 'Calendar', 'location=no,status=no,directories=no,menubar=no,resizable=no,width=350,height=270');
  mywindow.location.href = 'calendar.html';
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

//  shrink/expand

function shrink(url, id, state) {
  var obj;
  //  current state
  var current = ($('#r' + id).css('display') != 'none');
  //  toggle?
  if (state < 0)
    state = !current;
  if (current != state) {
    //  show/hide module
    if (state)
      $('#r' + id).slideDown();
    else
      $('#r' + id).slideUp();
    //  triangle
    $('#' + (state ? 'e' : 's') + id).show();
    $('#' + (state ? 's' : 'e') + id).hide();
    //  state persistence
    $.get(url, { name:id, value:state });
  }
  return false;
}
function shrinks(url, ids, state) {
  for (var i = 0;  i < ids.length;  i++)
    shrink(url, ids[i], state);
  return false;
}

//  popups

function popup(query) {
  mywindow = open('instance_popup.php?' + query, 'Edit', 'location=no,status=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450');
  mywindow.location.href = 'instance_popup.php?' + query;
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

function popup_url(url, target) {
  mywindow = open(url, target ? target : '_new', 'location=no,status=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=650,height=450');
  mywindow.location.href = url;
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

function popup_window(url, target) {
  mywindow = open(url, target ? target : '_new', 'location=yes,status=yes,directories=yes,menubar=yes,scrollbars=yes,resizable=yes');
  mywindow.location.href = url;
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}