//  $Id: script.js,v 1.3 2008/03/31 19:19:05 tcaldwell Exp $
//  Copyright (C)2007 Gorges Web Sites. All Rights Reserved.

//  browser detection

var isIE = (document.all && (navigator.appVersion.indexOf('MSIE') != -1)) ? 1 : 0;
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 get_radio(form, obj) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  for (var i = 0;  i < obj.length;  i++)
    if (obj[i].checked)
      return obj[i].value;
  return 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 {
      alert('form=' + form + ' obj=' + obj + ' value=' + value);
      obj.value = value;
    }
  }
}

function set_radio(form, obj, value) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  if (obj) {
    var len = obj.length;
    if (typeof(len) == 'undefined')
      obj.checked = (obj.value == value.toString());
    for (var i = 0;  i < len;  i++)
      obj[i].checked = (obj[i].value == value.toString());
  }
}

function update_checkbox(obj, url) {
  if (typeof(obj) == 'string')
    obj = eval('document.' + form + '.' + obj);
  if (obj)
    $.get(url + '&value=' + (obj.checked ? '1' : ''));
}

function convert_float(value) {
  value = trim(value);
  var val = parseFloat(value);
  if (value.substr(value.length - 1, 1) == '%')
    val /= 100.0;
  return val;
}

//  help

function help_click(docRoot, hid, url, page) {
  //  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', docRoot + 'images/blank.gif');
  //  display or hide
  if (!shown) {
    //  move help and frame to top level
    var holder = $('#iframes');
    if (holder.size() && help.size() && (help.parent().get(0) != holder.get(0))) {
      holder.get(0).appendChild(help.get(0));
      help = $('#hlp' + hid);
    }
    //  set location of help
    var iconOffset = $(icon).offset();
    var iconWidth = $(icon).width();
    var docWidth = $(document).width();
    var helpWidth = help.width();
    var helpHeight = help.height();
    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) {
        frame.css('left', 0).css('top', 0);
        frame.width((help.width() - 2) + 'px').height((help.height() - 2) + 'px').show();
      }
    });
    //  unhelp icon
    $(icon).attr('src',  docRoot + 'images/icons/unhelp.gif');
  } else {
    //  hide
    if (isIE5 || isIE6)
      frame.hide();
    help.slideUp();
    //  clear unhelp icon
    $(icon).attr('src',  docRoot + 'images/blank.gif');
  }
  //  persistence
  $.get(url, { name:page, value:(shown ? false : hid) });
  //  hide all other help messages
  if (isIE5 || isIE6)
    $('.help-iframe').not(frame).hide();
  $('.help-container').not(help).hide();
}
function help_close() {
  $('.help-container').hide();
}

//  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('', function() { $('#r' + id).css('overflow', 'hidden'); });  // bizarre - but fixes IE7 bug
    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;
}



// instance edit image functions
function openImageBrowser( field ) {
  var url = 'image_browser.php?field=' + field;
  mywindow = open(url, 'Image Browser','location=no,status=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450');
  mywindow.location.href = url;
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

function openImageCropper( field ) {
  var args = $('#f' + field).val().replace('image_thumb.php?','');
  var url = 'image_cropper.php?field=' + field + '&' + args;
  mywindow = open(url, 'Image Cropper','location=no,status=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450');
  mywindow.location.href = url;
  mywindow.focus();
  if (mywindow.opener == null)
    mywindow.opener = self;
}

function setImageUrl( field, url ) {
  //alert(field + ' ' + url);
  $('#i' + field).remove();
  $('#f' + field)
    .val(url)
    .before('<img id="i' + field + '" src="' + url + '&size=218" alt="picture" />');
}

function removeImage( field ) {
  $('#f' + field).val('');
  $('#i' + field).slideUp();
}

