//  $Id: script.js,v 1.1 2007/06/12 19:21:28 dfurber Exp $
//  Copyright (C)2006 Gorges Web Sites. All Rights Reserved.

//  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;
}

//  inner text

function get_inner(obj) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  return obj ? (document.all ? obj.innerText : obj.innerHTML) : '';
}
function set_inner(obj, text) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if (document.all)
      obj.innerText = text;
    else
      obj.innerHTML = text;
  }
}
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;
  }
}

//  radio helpers

function get_radio(obj) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    var len = obj.length;
    if (typeof(len) == 'undefined')
      return obj.checked ? obj.value : '';
    for (var i = 0;  i < len;  i++)
      if (obj[i].checked)
        return obj[i].value;
  }
  return '';
}
function set_radio(obj, value) {
//  if (typeof(obj) == 'string')
//    obj = get_element(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());
//  }
}

//  enable/disable items

function enable_control(obj, state) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj)
	obj.disabled = state ? false : true;
    //document.$form_name.filter.disabled = (search == 'string') ? false : true;
}

//  help

function help_click(id) {
  var help = get_element(id);
  if (help) {
    var visible = help.style.visibility;
    help.style.visibility = (visible == 'visible') ? 'hidden' : 'visible';
    var icon = get_element('i' + id.substring(1));
    if (icon)
      icon.className = "help-icon help-" + ((visible == 'visible') ? '' : 'no') + "help";
  }
}

//  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;
}

//  style attributes

function set_opacity(obj, value) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if (document.all)
      obj.style.filter = (value < 1) ? 'alpha(opacity=' + (100 * (.75 * value)) + ')' : null;
    else if (typeof(obj.style.MozOpacity) != 'undefined')
      obj.style.MozOpacity = value;
    else
      obj.style.opacity = value;
  }
}

function set_display(obj, value) {
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj)
    obj.style.display = value;
}

//  shrink/expand

function shrink(url, id, state) {
  var obj;
  if (obj = get_element('r' + id)) {
    if (state < 0)
      state = (obj.style.display != 'none') ? 1 : 0;
    obj.style.display = state ? 'none' : 'block';
  }
  if (obj = get_element('s' + id))
    obj.style.display = state ? 'inline' : 'none';
  if (obj = get_element('e' + id))
    obj.style.display = state ? 'none' : 'inline';
  //  remember
  ajaxRequest(url + '&name=' + id + '&value=' + escape(state ? 1 : 0));
}
function shrinks(url, ids, state) {
  for (var id in ids)
    shrink(url, ids[id], state);
}

//  locations

function get_left(obj) {
  var result = 0;
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if (obj.offsetParent)
      while (true) {
        result += obj.offsetLeft;
        if (!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
    else if (obj.x)
      result += obj.x;
  }
  return result;
}
function get_top(obj) {
  var result = 0;
  if (typeof(obj) == 'string')
    obj = get_element(obj);
  if (obj) {
    if(obj.offsetParent)
      while (true) {
        result += obj.offsetTop;
        if (!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
    else if(obj.y)
      result += obj.y;
  }
  return result;
}

//  popup

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 popupURL(url) {
  mywindow = open(url, 'Image', '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;
}

//  dispatchers

function ajaxRequest(url) {
  try {
    var request = null;
    if (window.XMLHttpRequest)
      request = new XMLHttpRequest();
    else if (window.ActiveXObject)
      request = new ActiveXObject("Microsoft.XMLHTTP");
    if (request) {
      request.onreadystatechange = function() { };
      request.open('GET', url, true);
      request.send(null);
    }
  } catch(e) {
  }
}

// class switchers

function addClass(target, classValue)
{
 var pattern = new RegExp("(^| )" + classValue + "( |$)");

 if (!pattern.test(target.className))
 {
   if (target.className == "") {
     target.className = classValue;
   } else   {
     target.className += " " + classValue;
   }
 }

 return true;
}

function removeClass(target, classValue)
{
 var removedClass = target.className;
 var pattern = new RegExp("(^| )" + classValue + "( |$)");

 removedClass = removedClass.replace(pattern, "$1");
 removedClass = removedClass.replace(/ $/, "");

 target.className = removedClass;

 return true;
}


/*photo gallery*/
    function prevPic() {
        current = (current - 1 < 0) ? pics.length-1 : current-1;
        updateButtons();
    }

    function nextPic() {
        current = (current + 1 >= pics.length) ? 0 : current+1;
        updateButtons();
    }

    function replaceBigPhoto(index) {
        current = index;
        updateButtons();
    }

    function updateButtons() {
        for(var i=0;i<picture.length;i++)
        {
            change('tdImage'+i, 'nonSelectedImage');
        }
        change('tdImage'+current, 'selectedImage');

        document.mainImage.src = picture[current].src;
        document.mainImage.style.height = picture[current].height;
        document.mainImage.style.width = picture[current].width;
        document.getElementById("captionText").innerHTML = picture[current].caption;
    }

    var largeGallery;

    function popLargeGallery(projectID) {
        return false;
//      largeGallery = window.open('index.php?popup=true','sample_images','height=520,width=500,status=yes,toolbar=no,menubar=no,location=no');
//      largeGallery.focus();
    }

   function change(id, newClass) {
      identity=document.getElementById(id);
      identity.className=newClass;
   }

