/*
  Load the various systems used in forms and check to see if an element needs to be auto-focused. I used try-
  catch blocks here instead of object detection because 'if (csDateChooserSystem) {}' would throw an error no
  matter what I tried. This way no errors are generated and the load fails gracefully if the necessary
  javascript isn't present.

  Author: Mike Thomas
  Date: 08/01/2006
  Version: 1.01
*/

function Delegate() {}
Delegate.create = function (o, f) {
  var a = new Array();
  var l = arguments.length;
  for(var i = 2 ; i < l ; i++) a[i - 2] = arguments[i];
  return function() {
    var aP = [].concat(arguments, a);
    f.apply(o, aP);
  }
}

var csFormSystem = new Object();
csFormSystem.elements = new Array();
csFormSystem.load = function () {
  try { csAutoCompleteSystem.load() } catch(e) { }

  // Search for and focus any element with 'autofocus' set to 'true';
  var tags = new Array('input','select','textarea');
  var flag = false;
  for (var i in tags) {
    var tag = tags[i];
    if (document.getElementsByTagName) {
      var elements = document.getElementsByTagName(tag);
    } else if (document.all) {
      var elements = document.all.tags(tag);
    }
    for (var i = 0; i < elements.length; i++) {
      var element = elements[i];
      if (element.autofocus) {
        element.autofocus = null;
        element.focus();
        flag = true;
        break;
      }
    }
    if (flag) break;
  }
}
csFormSystem.elementObj = function (id) {
  this.element = document.getElementById(id);
  this.containerDiv = document.getElementById(id + '-container');
  this.labelSpan = document.getElementById(id + '-label');
  this.name = id.replace('form-','');
  this.autofocus = false;
  this.hint = null;
  csFormSystem.elements[this.name] = this;
}
var csFormElement = csFormSystem.elementObj.prototype;

csFormElement.hide = function () {
  this.containerDiv.style.display = 'none';
  csHintSystem.refresh();
}
csFormElement.show = function () {
  this.containerDiv.style.display = '';
  csHintSystem.refresh();
}
csFormElement.setLabel = function (label) {
  this.labelSpan.innerHTML = label;
}
csFormElement.setHint = function (hint) {
  this.hintObject.hint = hint;
}
csFormElement.disable = function () {
  this.element.disabled = true;
}
csFormElement.enable = function () {
  this.element.disabled = false;
}
csFormElement.setValue = function (value) {
  this.element.value = value;
}

if (window.addEventListener) window.addEventListener('load',Delegate.create(csFormSystem,csFormSystem.load),false);
else if (window.attachEvent) window.attachEvent('onload',Delegate.create(csFormSystem,csFormSystem.load));

/*
  Creates a hintObject for each form element on the page with a 'csHint' property set. When the element is
  focused the contents of the 'csHint' property are displayed to the right of the element. Since it uses the
  advanced style of event assignmentment, this will only work in newer browsers.

  Author: Mike Thomas
  Date: 07/28/2006
  Version: 2.0
*/
var csHintSystem = new Object();
var c = csHintSystem;
c.elements = Array();
c.tempElements = Array();
c.refresh = function () {
  for (var i in this.elements) {
    this.elements[i].refresh();
  }
}
c.setHint = function (element_name,hint) {
  csHintSystem.elements[element_name].hint = hint;
}
c.hideAll = function () {
  for (var i = 0; i < this.tempElements.length; i++) {
    try {
      this.tempElements[i].parentNode.removeChild(this.tempElements[i]);
    } catch (e) { }
  }
}

// Hint object constructor
c.hintObject = function(id,hint,mode) {
  this.element = document.getElementById(id);
  this.element.hintObj = this;
  this.hint = hint;
  this.mode = mode;
  this.visible = false;

  if (this.mode == 'manual') {
    this.button = document.createElement('img');
    this.button.src = 'http://www.cs.virginia.edu/~csadmin/images/icon-hint.gif';
    this.button.style.position = 'absolute';
    this.setPosition();
    this.button.onclick = Delegate.create(this,this.show2);
    this.button.style.cursor = 'pointer';
    this.element.parentNode.appendChild(this.button);
    this.distanceFromElement = 24;
  } else {
    if (window.addEventListener) {
      this.element.addEventListener('focus',Delegate.create(this,this.show),false);
      this.element.addEventListener('blur',Delegate.create(this,this.hide),false);
    } else if (window.attachEvent) {
      this.element.attachEvent('onfocus',Delegate.create(this,this.show));
      this.element.attachEvent('onblur',Delegate.create(this,this.hide));
    }
    this.distanceFromElement = 4;
  }
  csHintSystem.elements.push(this);
}
var h = c.hintObject.prototype;
// Delay 1 sec
h.show = function () {
  if (this.element.silentFocus) { // To handle autocomplete scrollbar problem with ie
    return;
  }
  this.timer = window.setTimeout(Delegate.create(this,this.show2),1000);
}
// Create the hint and shadow divs
h.show2 = function () {
  csHintSystem.hideAll();
  if (this.visible) {
    this.visible = false;
    return;
  } else {
    this.visible = true;
  }
  this.opacity = 0;
  this.hintbox = document.createElement('div');
  this.hintbox.className = 'form-hint';
  this.hintbox.innerHTML = '<div class="form-hint-title"><div style="float:right;">[<a href="javascript:csHintSystem.hideAll()">close</a>]</div>Hint for this field:</div>' + this.hint;
  this.hintbox.style.left = (findPosX(this.element) + this.element.offsetWidth + this.distanceFromElement) + 'px';
  this.hintbox.style.top = findPosY(this.element) + 'px';
  this.hintbox.style.opacity = 0;
  this.hintbox.style.position = 'absolute';
  this.hintbox.style.display = 'block';

  this.shadow = document.createElement('div');
  this.shadow.className = 'form-hint-shadow';
  this.shadow.innerHTML = this.hintbox.innerHTML;
  var temp = this.distanceFromElement + 5;
  this.shadow.style.left = (findPosX(this.element) + this.element.offsetWidth + temp) + 'px';
  this.shadow.style.top = (findPosY(this.element) + 7) + 'px';
  this.shadow.style.opacity = 0;
  this.shadow.style.position = 'absolute';
  this.shadow.style.display = 'block';

  var body = document.getElementsByTagName('body')[0];
  body.appendChild(this.hintbox);
  body.appendChild(this.shadow);
  csHintSystem.tempElements.push(this.hintbox);
  csHintSystem.tempElements.push(this.shadow);
  this.show3();
}
// Fade the hint and shadow in
h.show3 = function () {
  this.opacity += 0.10;
  var ieopacity = this.opacity * 100;
  this.hintbox.style.opacity = this.opacity;
  this.hintbox.style.filter = 'alpha(opacity=' + ieopacity + ')';
  this.shadow.style.opacity = this.opacity * .25;
  this.shadow.style.filter = 'alpha(opacity=' + (ieopacity * .25) + ')';
  if (this.hintbox.style.opacity < 1) {
    setTimeout(Delegate.create(this,this.show3),15);
  }
}
// Hide the hint and shadow
h.hide = function () {
  if (this.timer) clearTimeout(this.timer);
  if (this.hintbox && this.hintbox.parentNode) this.hintbox.parentNode.removeChild(this.hintbox);
  if (this.shadow && this.shadow.parentNode) this.shadow.parentNode.removeChild(this.shadow);
}
h.refresh = function () {
  this.setPosition();
}
h.setPosition = function () {
  if (this.mode == 'manual') {
    var temp = Math.round(this.element.offsetHeight / 2 - 8);
    this.button.style.left = (findPosX(this.element) + this.element.offsetWidth + 4) + 'px';
    this.button.style.top = (findPosY(this.element) + temp) + 'px';
  }
}


/*
  Used to toggle the size of textarea fields

  Author: Mike Thomas
  Date: 08/07/2005
  Version: 1.0
*/
function formTextAreaResize(id){
  var textarea = document.getElementById(id);
  if (textarea.style.width == '') {
    textarea.style.width = '400px';
  } else if ((textarea.style.width == '400px') && (textarea.style.height == '')) {
    textarea.style.height = '200px';
  } else {
    textarea.style.width = '';
    textarea.style.height = '';
  }
}

function formSelectChangeOptionSet(elementId,optionSet) {
  var element = document.getElementById(elementId);
  var data = element.dataSource[optionSet];
  element.options.length = 0;
  var count = 0;
  for (var i in data) {
    element.options[count] = new Option(data[i],i);
    count++;
  }
}



