function Calendar(d,callBack) {
  this.startDate = 1900;
  this.endDate = (new Date()).getFullYear();
  this.setDate(d);
  this.callBack = callBack || null;
};

Calendar.prototype = new ObjectEvents();

Calendar.prototype.setDate = function(d) {
  d = d.split('/');
  if(parseInt(d[2]) < 30) d[2] = 2000+parseInt(d[2]);
  d = d.join('/');
  if(d) d = new Date(d);
  if(!d || isNaN(d))
    d = new Date();
  this.date = d.getDate();
  this.month = d.getMonth();
  this.year = d.getFullYear();
};

Calendar.prototype.monthNames = ['January', 'February', 'March', 'April',
				 'May', 'June', 'July', 'August',
				 'September', 'October', 'November', 'December'];


Calendar.prototype.dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

Calendar.prototype.daysInMonth = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
				  [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]];

Calendar.prototype.init = function() {
  var calHolder = make_el('DIV', {className:'calendar-holder'});
  this.calId = getId(calHolder);
  this.buildYears(calHolder);
  this.buildMonths(calHolder);
  this.buildDays(calHolder);
  this.addEvent(calHolder, 'click', 'processClick');
  return calHolder;
};

Calendar.prototype.buildDropDownHolder = function(name, where) {
  var btn = make_el('DIV', {className:'calendar-'+name+'-minus'}, '<', where);
  btn[name+'Offset'] = -1;
  btn = make_el('DIV', {className:'calendar-'+name+'-plus'}, '>', where);
  btn[name+'Offset'] = 1;
  return make_el('DIV', {className: 'calendar-'+name+'s dropdown' }, where);
};

Calendar.prototype.buildYears = function(where) {
  var holder = this.buildDropDownHolder('year', where);
  this.yearId = getId(make_el('P', {className: 'calendar-year'}, this.year, holder));
  var ul = make_el('UL', holder);
  for( var i = this.startDate; i <= this.endDate; i++ )
    make_el('LI',{yearIndx:i},i,ul);
};

Calendar.prototype.buildMonths = function(where) {
  var holder = this.buildDropDownHolder('month', where);
  this.monthId = getId(make_el('P', {className: 'calendar-month'}, this.monthNames[this.month], holder));
  var ul = make_el('UL', holder);
  for(var i=0,l=this.monthNames.length; i<l; i++)
    make_el('LI', {monthIndx:i}, this.monthNames[i], ul);
};

Calendar.prototype.buildDays = function(where) {
  var ul = make_el('UL', {className:'calendar-days'}, where);
  this.daysId = getId(ul);
  this.generateDays(ul);
};

Calendar.prototype.getDaysInMonth = function(year, month) {
  var leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  return this.daysInMonth[leap?1:0][month];
};

Calendar.prototype.getDaysInYearUpToDate = function(year, month, date) {
  var leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  var days = 0;
  for( var i=0; i<month; i++ )
    days += this.daysInMonth[leap?1:0][i];
  return days+date;
};

Calendar.prototype.generatePastMonthOffsetDays = function(ul) {
  var day = new Date(), year = this.year, month = this.month;
  day.setFullYear(year,month,1);
  day = day.getDay();
  if(day) {

    if(--month < 0) {
      month = 11;
      year--;
    }

    var daysOfLastMonth = this.getDaysInMonth(year, month);
    for(var i=daysOfLastMonth-day+1;
	i<=daysOfLastMonth; i++)
      make_el('LI', {className: 'calendar-past-month-name',
	    monthIndx:month, dateIndx:i}, /* i, */ ul);
  }
};

Calendar.prototype.generateNextMonthOffsetDays = function(ul) {
  var day = new Date(), year = this.year, month = this.month;
  day.setFullYear(year, month, this.getDaysInMonth(year, month));
  day = 6-day.getDay();
  if(day) {
    if(++month > 11) {
      month = 0;
      year++;
    }
    for(var i=1;
	i<day; i++)
      make_el('LI', {className: 'calendar-next-month-name',
	    monthIndx:month, dateIndx:i}, '0'+i,ul);
  }
};

Calendar.prototype.generateDays = function(ul) {
  ul.innerHTML = '';

/*   make_el('LI',{className:'calendar-day-name'}, ul); */
  for(var i = 0, l = this.dayNames.length;
      i<l; i++)
    make_el('LI', {className:'calendar-day-name'}, this.dayNames[i], ul);

/*   var daysSoFar = Calendar.prototype.getDaysInYearUpToDate(this.year, this.month, 1); */
/*   var week = Math.floor(daysSoFar/7)+1; */

  var day = new Date(), year = this.year, month = this.month;
  day.setFullYear(year,month,1);
  day = day.getDay();

/*   make_el('LI', {className:'calendar-week'}, week, ul); */
  this.generatePastMonthOffsetDays(ul);
  var daysOfMonth = this.getDaysInMonth(this.year, this.month);
  for( i=l=1; i<=daysOfMonth; i++, day++ ) {
    l = i;
    if( l<10 ) l = '0' + l;
    if(i == this.date)
      make_el('LI', {dateIndx:i, className: 'calendar-current-day'}, l, ul);
    else make_el('LI', {dateIndx:i}, l, ul);
/*     if( day%7 == 6 && i < daysOfMonth ) */
/*       make_el('LI', {className:'calendar-week'}, ++week, ul); */
  }
/*   this.generateNextMonthOffsetDays(ul); */
};

Calendar.prototype.refresh = function() {
  this.updateDays();
  this.updateMonth();
  this.updateYear();
};

Calendar.prototype.updateDays = function() {
  this.generateDays(get_by_id(this.daysId));
};

Calendar.prototype.updateMonth = function() {
  get_by_id(this.monthId).innerHTML = this.monthNames[this.month];
};

Calendar.prototype.updateYear = function() {
  get_by_id(this.yearId).innerHTML = this.year;
};

Calendar.prototype.setDateByIndex = function(indx) {
  this.date = indx;
  if(this.callBack)this.callBack(this.date, this.month, this.year);
};

Calendar.prototype.setMonthByIndex = function(indx) {
  this.month = indx;
  var daysOfMonth = this.getDaysInMonth(this.year, indx);
/*   if( daysOfMonth < this.date ) */
    this.setDateByIndex(daysOfMonth);
};

Calendar.prototype.setMonthByOffset = function(offset) {
  var indx = this.month + offset;
  if( indx < 0 ) {
    indx = 11;
    this.setYearByOffset(offset); /* this will break on offset > then |-1| */
  }
  else if( indx > 11 ) {
    indx = 0;
    this.setYearByOffset(offset); /* this will break on offset > then |-1| */
  }
  this.setMonthByIndex(indx);
};

Calendar.prototype.setYearByIndex = function(indx) {
  this.year = indx;
  var daysOfMonth = this.getDaysInMonth(this.year, this.month);
/*   if( daysOfMonth < this.date ) */
    this.setDateByIndex(daysOfMonth);
};

Calendar.prototype.setYearByOffset = function(offset) {
  this.setYearByIndex(this.year + offset);
};

Calendar.prototype.processClick = function(e) {
  var srcEl=get_srcEl(e), month=this.month, year=this.year, date=this.date;

   if( typeof srcEl.monthIndx == 'number' )
    this.setMonthByIndex(srcEl.monthIndx);

   if( typeof srcEl.yearIndx == 'number' )
    this.setYearByIndex(srcEl.yearIndx);

  else if( typeof srcEl.monthOffset == 'number' )
    this.setMonthByOffset(srcEl.monthOffset);

  else if( typeof srcEl.yearOffset == 'number' )
    this.setYearByOffset(srcEl.yearOffset);

  if( typeof srcEl.dateIndx == 'number' )
    this.setDateByIndex(srcEl.dateIndx);

  if( month != this.month || year != this.year || date != this.date )
    this.refresh();
};


function CalendarPopUp(day,month,year) {
  if(!day || !month || !year) return null;
  this.holderId = getId(day.parentNode);
  this.dayId = getId(day);
  this.monthId = getId(month);
  this.yearId = getId(year);
  this.fieldId = getId(make_el('INPUT', {type:'text', className: 'mgr-wide mgr-text',
	  value:month.value+'/'+day.value+'/'+year.value}, day.parentNode));
  var img = make_el('IMG', {src:'http://static.yuku.com/common/bypass/images/calendar-icon.gif'}, day.parentNode);
  this.addEvent(img, 'click', 'showPopUp');
  this.addEvent(get_by_id(this.fieldId), 'blur', 'refreshCalendar');
  this.addEvent(document, 'click', 'hidePopUp');
  this.addEvent(document, 'click', 'hidePopUp');
  className.add(day.parentNode, 'calendar-popup-holder');
  var me = this;
  this.calendar = new Calendar(month.value+'/'+day.value+'/'+year.value,
			       function(d, m, y) {me.updateVals(d, m, y)});
};

CalendarPopUp.prototype = new ObjectEvents();

CalendarPopUp.prototype.init = function() {
  var popup = make_el('DIV', {className: 'calendar-popup mgr-box'}, get_by_id(this.holderId));
  for(var i=1; i<4; i++)
    popup = make_el('DIV', {className: 'mgr-onionskin'+i}, popup);
  var heading = make_el('DIV', {className: 'mgr-boxheading'}, popup);
  popup = make_el('DIV', {className: 'mgr-boxbody'}, popup);
  var h2 = make_el('h2', 'Calendar', heading);
  popup.appendChild(this.calendar.init());
  var controls = make_el('DIV', {className:'mgr-box-controls'}, popup.parentNode);
  make_el('SPAN', {className: 'calendar-popup-close'}, 'x', controls)
};

CalendarPopUp.prototype.updateVals = function(d, m, y) {
  get_by_id(this.dayId).value = d;
  get_by_id(this.monthId).value = m+1;
  get_by_id(this.yearId).value = y;
  get_by_id(this.fieldId).value = m+1+'/'+d+'/'+y;
};

CalendarPopUp.prototype.showPopUp = function(e) {
  if(Math.round((FindMouseXY(e).y - getScrollPos().y) / getFrameSize().y*0.7))
    className.add(get_by_id(this.holderId), 'calendar-popup-visible-bottom');
  else
    className.add(get_by_id(this.holderId), 'calendar-popup-visible-top');
  this.refreshCalendar();
};

CalendarPopUp.prototype.refreshCalendar = function() {
  var val = get_by_id(this.fieldId).value;
  this.calendar.setDate(val);
  this.calendar.refresh();
  this.calendar.setDateByIndex(this.calendar.date?this.calendar.date:1);	/* badly hacked */
};

CalendarPopUp.prototype.hidePopUp = function(e) {
  var srcEl = get_srcEl(e);
  if(className.test(srcEl, 'calendar-popup-close')) {
    className.kill(get_by_id(this.holderId), 'calendar-popup-visible-bottom');
    className.kill(get_by_id(this.holderId), 'calendar-popup-visible-top');
  }
  else {
    while(!className.test(srcEl, 'calendar-popup') && (srcEl = srcEl.parentNode));
    if(!srcEl) {
      className.kill(get_by_id(this.holderId), 'calendar-popup-visible-bottom');
      className.kill(get_by_id(this.holderId), 'calendar-popup-visible-top');
    }
  }
};

function Web2Select(el,callBack) {
  this.id = 'web2select-'+(++ids);
  this.selId = getId(el);
  this.size = el.size || null;
  this.currentValue = el.value;
  this.width = 0;
  this.build(el);
  this.callBack = callBack;
};

Web2Select.prototype = new ObjectEvents();
Web2Select.prototype.build = function(el) {
  var selector = make_el('div',{id:this.id,
				className:el.className+' web2select'});
  var title = make_el('h2',{className:'current-value'},selector);
  var ul = make_el('ul',{className:'hide web2select-dropdown'},selector);
  if( el.length > 10 ) className.add(ul, 'web2select-large-dropdown');
  this.buildContent(ul,el.childNodes);
  if(this.size)ul.style.height = this.size+'em';
  selector.style.width = this.width+'em';
  ul.style.width = this.width+'em';
  insert_before(selector,el);

  this.addEvent(selector, 'DOMMouseScroll', 'scroll');
  this.addEvent(selector, 'mousewheel', 'scroll');
  this.addEvent(document,'click','click');
};

Web2Select.prototype.scroll = function(e) {
  var Up = false;
  if(e.detail)
    Up = e.detail < 0;
  else if(e.wheelDelta)
    Up = e.wheelDelta >= 0;

  var select = get_by_id(this.selId);
  if (Up) {
    if (select.selectedIndex > 0) {
      select.selectedIndex--;
      this.setValue(select.value);
    }
  }
  else if (select.selectedIndex < select.options.length - 1) {
    select.selectedIndex++;
    this.setValue(select.value);
  }
};

Web2Select.prototype.buildContent = function(where,nodes) {
  for(var i = 0,node, l = nodes.length; i < l; i++) {
    node = nodes[i];
    if(node.tagName) {
      if(node.tagName == 'OPTGROUP')
	where.appendChild(this.buildOptGroup(node));
      else if(node.tagName == 'OPTION')
	where.appendChild(this.buildOption(node));
    }
  }
};

Web2Select.prototype.buildOptGroup = function(node) {
  var optGroup = make_el('LI',{className:'optgroup'});
  make_el('H3',{className:'label'},node.label,optGroup)
  var ul = make_el('UL',optGroup);
  this.buildContent(ul,node.childNodes);
  return optGroup;
};

Web2Select.prototype.buildOption = function(node) {
  if(node.text.length > this.width) this.width = node.text.length;
  var li = make_el('li',{selValue:node.value,
			 id:this.id+node.value},node.text);
  if( node.value == this.currentValue )
  li.className = 'web2select-selected-option';
  return li;
};

Web2Select.prototype.click = function(e) {
  var srcEl = get_srcEl(e), sel = srcEl;
  while( sel && sel.id != this.id ) sel = sel.parentNode;
  if( sel ) {
    cancelBubble(e);
    if( srcEl.selValue ) {
      this.setValue(srcEl.selValue);
      this.callBack(srcEl.selValue);
    }
    if( srcEl && ( srcEl.className == 'current-value'
		  || ( srcEl.parentNode
		       && srcEl.parentNode.id == this.id) ) )
      this.open(e);
    else this.close();
  }
  else this.close();
};

Web2Select.prototype.open = function(e) {
  clearTimeout(this.timeId);
  var dropDown = get_by_tag('UL',get_by_id(this.id))[0];
  if(className.test(dropDown, 'show')) this.close();
  else {
    className.add(get_by_id(this.id), 'web2select-opened');
    if(Math.round((FindMouseXY(e).y - getScrollPos().y) / getFrameSize().y*0.7))
      className.add(dropDown, 'show-bottom');
    else className.add(dropDown, 'show-top');
    get_by_id(this.selId).focus();
  }
};

Web2Select.prototype.blurClose = function() {
  this.removeEvent(document,this.blrId);
  var me = this;
  this.timeId = setTimeout(function(){me.close();},200);
  className.kill(get_by_id(this.id),'focus');
};
Web2Select.prototype.close = function() {
  className.kill(get_by_id(this.id), 'web2select-opened');
  var dropDown = get_by_tag('UL',get_by_id(this.id))[0];
  className.kill(dropDown, 'show-top');
  className.kill(dropDown, 'show-bottom');
};

Web2Select.prototype.setValue = function(newValue,evnType) {
  var dropDown = get_by_id(this.id);
  switch(evnType) {
  case 'focus':
    className.add(dropDown,evnType);
    break;
  case 'blur':
    this.blrId = this.addEvent(document,'mouseup', 'blurClose');
    break;
  };
  if(newValue) {
    var curValue = get_by_tag('H2',dropDown)[0];
    kill_children(curValue);
    var node = get_by_id(this.id+this.currentValue);
    node.className = '';
    node = get_by_id(this.id+newValue);
    curValue.appendChild(make_text(node.innerHTML));
    node.className = 'web2select-selected-option';
    this.currentValue = newValue;
  }
};

function FormEl(el,callBack) {
  this.id = getId(el);
  this.setUp(el);
  this.callBack = callBack;
};

FormEl.prototype = new ObjectEvents();
FormEl.prototype.setUp = function(el) {
  className.add(el,'shift-left');
  this.addEvent(el,'change','change');
  this.addEvent(el,'blur','blur');
  this.addEvent(el,'focus','focus');
};

FormEl.prototype.getValue = function() {
  return get_by_id(this.id).value;
};

FormEl.prototype.setValue = function(newValue) {
  get_by_id(this.id).value = newValue;
};

FormEl.prototype.change = function(e) {
  this.callBack(this.getValue(),'change');
};

FormEl.prototype.focus = function(e) {
  var sel = get_by_id(this.id);
  this.keyUpId = this.addEvent(sel,'keyup','keyUp');
  this.callBack(false,'focus');
};

FormEl.prototype.blur = function() {
  this.removeEvent(document,this.keyUpId);
  this.callBack(false,'blur');
};

FormEl.prototype.keyUp = function(e) {
  this.callBack(this.getValue(),'keyup');
};


function styleSelect(name) {
  var w2select, regSelect;
  var updateW2 = function(val,evnType) {
    w2select.setValue(val,evnType);
  };

  var updateReg = function(val) {
    regSelect.setValue(val);
  };

  var select = get_by_name(name)[0];
  w2select = new Web2Select(select,updateReg);
  regSelect = new FormEl(select,updateW2);
  var v = select.value;
  select = null;
  updateW2(v);
};

function FormValidator(formId, validateKeyUpObj, validateBlurObj, callOffset) {
  this.formId = formId;
  this.validateKeyUpObj = validateKeyUpObj;
  this.validateBlurObj = validateBlurObj;
  this.callOffset = callOffset || 300;
  this.addEvent(get_by_id(formId), 'keyup', 'validate');
  this.addEvent(get_by_id(formId), 'submit', 'submit');
  for(var els = get_by_id(formId).elements, i = 0, l = els.length;
      i < l; this.addEvent(els[i++],'blur','validate'));
};

FormValidator.prototype = new ObjectEvents();

FormValidator.prototype.submit = function(e) {
  var els = get_by_id(this.formId);
  for(var i=0,el, l = els.length; i<l; i++) {
    el = els[i];
    this.checkForErrors(el,this.validateKeyUpObj,true);
    this.checkForErrors(el,this.validateBlurObj,true);
    if(el.errors)preventDefault(e);
  }
};

FormValidator.prototype.validate = function(e) {
  if(e.type == 'keyup')
    this.checkForErrors(get_srcEl(e),this.validateKeyUpObj);
  else
    this.checkForErrors(get_srcEl(e),this.validateBlurObj);
};

FormValidator.prototype.checkForErrors = function(el,obj,xlh_) {
  if(obj[el.name]) {
    var errors = obj[el.name](el);
    if(errors)
      if(errors['xlr']) {
	if(!el.id) el.id = 'validate-' + ++ids;
	if(!xlh_)this.initRequest(errors['xlr']+el.value.trim(),el.id);
      }
      else
	this.addErrors(el,errors);
    else
      this.removeErrors(el);
  }
};

FormValidator.prototype.initRequest = function(url,id) {
  this.requestSent = true;
  clearTimeout(this.tID);
  var me = this;
  var callBack = function(val) {
    me.requestSent = false;
    if(isNaN(val*1))
      me.addErrors(get_by_id(id),val);
    else
      me.removeErrors(get_by_id(id));
  };
  this.tID = setTimeout(function(){me.makeRequest(url,callBack)},this.callOffset);
};

FormValidator.prototype.makeRequest = function(url,callBack) {
  var me = this;
  x = getXmlHttpObject();
  x.open('GET', url, true);
  x.onreadystatechange = function() {
    if(x.readyState == 4)
      callBack(x.responseText.trim());
  };
  x.send(null);
};

FormValidator.prototype.checkForCommonErrors = function(el) {
  if(!el.value.trim()) return 'field cannot be empty';
  else if(el.tagName == 'SELECT' && !(el.value*1))
    return 'you must have something selected';
  return false;
};

FormValidator.prototype.showItemAsValid = function(item) {
  item = item.parentNode;
  className.kill(item,'invalid');
  className.add(item,'valid');
};

FormValidator.prototype.showItemAsInvalid = function(item) {
  item = item.parentNode;
  className.kill(item,'valid');
  className.add(item,'invalid');
};

FormValidator.prototype.addErrors = function(item,errors) {
  if(!this.requestSent) {
    var errorsHolder = this.getErrorsHolder(item);
    errorsHolder.innerHTML = errors;
    item.parentNode.appendChild(errorsHolder);
    this.showItemAsInvalid(item);
    item.errors = true;
  }
};

FormValidator.prototype.removeErrors = function(item) {
  var errorsHolder = this.getErrorsHolder(item);
  if(errorsHolder.parentNode)remove_el(errorsHolder);/* I feel really iffy about this one -=PVD=- */
  this.showItemAsValid(item)
  item.errors = null;
  this.requestSent = false;
};

FormValidator.prototype.getErrorsHolder = function(item) {
  var errorsHolder = get_by_tag('SPAN',item.parentNode);
  errorsHolder = errorsHolder.length?errorsHolder[errorsHolder.length-1]:null;	/* urgh -=PVD=- */
  if(!errorsHolder || errorsHolder.className != 'errors')
    errorsHolder = make_el('SPAN',{className:'errors'});
  return errorsHolder;
};

function Form(form) {
  this.formId = getId(form);
  //this.styleSelects();
  this.updateInputs();
  this.setUpFieldsets();
  this.setFocusBlurs();
  this.setUpFileUploads();
  this.addEvent(form, 'click', 'click');
};

Form.prototype = new ObjectEvents();

Form.prototype.setUpFieldsets = function() {
  var fieldsets = get_by_tag('FIELDSET',get_by_id(this.formId));
  for(var i=0, field; field = fieldsets[i]; i++)
    if(className.test(field, 'mgr-hide-fieldset')) {
      className.kill(field, 'mgr-hide-fieldset');
      className.add(field, 'mgr-fieldset-hidden');
    }
};

Form.prototype.setFocusBlurs = function() {
  var inputs = get_by_tag('INPUT',get_by_id(this.formId));
  for(var i=0, l=inputs.length,input; i<l; i++) {
    input = inputs[i];
    if(input.type == 'checkbox' || input.type == 'radio') {
      this.addEvent(input, 'focus', 'focus');
      this.addEvent(input, 'blur', 'blur');
    }
  }
};

Form.prototype.styleSelects = function() {
  var selects = get_by_tag('SELECT',get_by_id(this.formId));
  var select;
  for(var i=0,l=selects.length; i<l; i++) {
    select = selects[i];
    if(!className.test(select.parentNode, 'mgr-date'))
      this.styleSelect(select);
  }
};

Form.prototype.styleSelect = function(select) {
  var w2select, regSelect;
  var updateW2 = function(val,evnType) {
    w2select.setValue(val,evnType);
  };

  var updateReg = function(val) {
    regSelect.setValue(val);
  };

  w2select = new Web2Select(select,updateReg);
  regSelect = new FormEl(select,updateW2);
  var v = select.value;
  select = null;
  updateW2(v);
};

Form.prototype.setUpFileUploads = function() {
  var inputs = get_by_tag('INPUT',get_by_id(this.formId));
  for(var i=0, l=inputs.length,input; i<l; i++) {
    input = inputs[i];
    if(input.type == 'file') {
	var htmlStr = input.parentNode.innerHTML;
	var p = make_el('P', input.parentNode);
	make_el('SPAN',{className:'mgr-add-file',addFileStr:htmlStr}, 'add', p);
    }
  }
};

Form.prototype.updateInputs = function() {
  var inputs = get_by_tag('INPUT',get_by_id(this.formId));
  for(var i=0, l=inputs.length,input; i<l; i++) {
    input = inputs[i];
    if(input.type == 'checkbox') {
      className.kill(input.parentNode, 'mgr-checkbox-unchecked');
      className.kill(input.parentNode, 'mgr-checkbox-checked');
      className.add(input.parentNode, input.checked?'mgr-checkbox-checked':'mgr-checkbox-unchecked');
    }
    else if(input.type == 'radio') {
      className.kill(input.parentNode, 'mgr-radio-checked');
      className.kill(input.parentNode, 'mgr-radio-unchecked');
      className.add(input.parentNode, input.checked?'mgr-radio-checked':'mgr-radio-unchecked');
    }
  }
};

Form.prototype.focus = function(e) {
  var el = get_srcEl(e);
  className.add(el.parentNode, 'mgr-'+el.type+'-focused');
};

Form.prototype.blur = function(e) {
  var el = get_srcEl(e);
  className.kill(el.parentNode, 'mgr-'+el.type+'-focused');
};

Form.prototype.toggleFieldset = function(f) {
  if(className.test(f, 'mgr-fieldset-hidden'))
    className.kill(f, 'mgr-fieldset-hidden');
  else className.add(f, 'mgr-fieldset-hidden');
};

Form.prototype.click = function(e) {
  var srcEl = get_srcEl(e);
  if( srcEl.addFileStr ) this.addFile(srcEl);
  if( srcEl.className == 'mgr-remove-file' ) this.removeFile(srcEl);
  else if( srcEl.tagName == 'LEGEND' )
    this.toggleFieldset(srcEl.parentNode);
  else if( srcEl.type == 'radio' || srcEl.type == 'checkbox' )
    this.updateInputs();
  else if( srcEl.tagName == 'LABEL' && (srcEl = get_by_tag('input', srcEl)[0])
	   && (srcEl.type == 'radio' || srcEl.type == 'checkbox') )
    this.updateInputs();
};

Form.prototype.addFile = function(el) {
  var div = make_el('DIV');
  div.innerHTML = el.addFileStr+'<span class="mgr-remove-file">remove</span>';
  insert_before(div, el.parentNode);
};

Form.prototype.removeFile = function(el) {
  remove_el(el.parentNode);
};
