document.write("<style>.formHelp{display:none;}</style>");
document.write("<style>.formValidator{display:none;}</style>");
jQuery.addMutator(function(ctx) {
 jQuery(ctx).find('.SFCascade[SFDepends]').each(function() {
  var depName = this.getAttribute('SFDepends');
  this.removeAttribute('SFDepends');
  var allOptions = [];
  var $parent = jQuery(this);
  var select = $parent.find('select');
  select.find('option').each(function() {
   var $option = jQuery(this);
   var catList = this.getAttribute('SFCats').split(' ');
   var cats = {};
   for (var i = 0; i < catList.length; i++) {
    var cat = catList[i];
    if (cat.length) cats[cat] = true;
   }
   allOptions.push([cats, this.getAttribute("value"), $option.text()]);
  });
  var otherItem = jQuery(select[0].form).find("[name='" + depName + "']");
  function updateOptions() {
   var $text = $parent.find('.SFCascadeText');
   var curVal = select.val();
   select.empty();
   var otherVal = otherItem.val();
   //alert('otherVal(' + otherVal + ')');
   var foundSomething = false;
   for (var i = 0; i < allOptions.length; i++) {
    var option = allOptions[i];
    //alert(webslinger.toJSON(option));
    if (option[0][otherVal]) {
     select.append("<option value='" + option[1] + "'>" + option[2] + "</option>");
     foundSomething = true;
    }
   }
   $text.hide();
   select.show();
   if (foundSomething) {
    select.val(curVal);
   } else {
    if ($text.length) {
     select.hide();
     $text.show();
     var otherValue = $parent.attr('SFOtherValue');
     if (otherValue) {
      select.append("<option selected value='" + otherValue + "'>" + otherValue + "</option>");
     }
    } else {
    }
   }
  }
  otherItem.change(updateOptions);
  updateOptions();
  //alert(webslinger.toJSON(allOptions));
 });
 var $ctx = jQuery(ctx);
 var links = jQuery("a.select_addedit", ctx);
 links.each(function() {
  var actionButton = jQuery(this);
  var fieldName = this.getAttribute("fieldName");
  var ptr = this.parentNode;
  while (ptr != null && ptr.nodeName != "FORM") ptr = ptr.parentNode;
  if (ptr == null) return;
  var selector = jQuery("select[name='" + fieldName + "']", ptr);
  if (selector.length == 0) return;
  if (!selector.attr("changer_installed")) {
   selector.attr("changer_installed", true).change(function() {
    if (this.selectedIndex == 0) {
     actionButton
      .show()
      .empty()
      .append("<span>" + actionButton.attr("addTitle") + "</span>")
      .attr("wsWidgetName", actionButton.attr("addWidget"))
      .attr("wsTitle", actionButton.attr("addTitle"))
      .attr("href", actionButton.attr("addHref"));
    } else {
     actionButton
      .show()
      .empty()
      .append("<span>" + actionButton.attr("editTitle") + "</span>")
      .attr("wsWidgetName", actionButton.attr("editWidget"))
      .attr("wsTitle", actionButton.attr("editTitle"))
      .attr("href", actionButton.attr("editHref") + this.options[this.selectedIndex].value);
    }
    selector.WidgetSendResizeEvent();
   });
  }
  selector.change();
  actionButton.applyMutators();
 });
 Date.format = 'mm/dd/yyyy';
 jQuery('.date-picker', ctx).datePicker({startDate: "01/01/1970"}); //.bind("dpClosed", function() { alert("dpClosed"); jQuery(this).sendResizeEvent(); });;
 //jQuery("a.date-picker", ctx).click(function() { setTimeout(function() { jQuery(this).sendResizeEvent(); }, 1000); return false; });
 var s = jQuery(".formValidator", ctx).hide();
 jQuery(".formHelp:not(:empty)", ctx)
  .before("<span class='formHelpIcon' style='display: inline; font-size: 120%; color: red; cursor: pointer;' href='#'>?</span>")
  .css("background-color", "#ffffff")
  .css("border", "1px solid black")
  .css("display", "none")
  .css("position", "absolute")
  .each(
   function() {
    var helpBox = jQuery(this);
    jQuery(this.previousSibling)
     .mousemove(
      function(e) {
       var xy = webslinger.fixMouseEvent(e);
       helpBox.css("top", xy[1] + 10).css("left", xy[0] + 10);
      }
     ).hover(
      function(e) {
       var xy = webslinger.fixMouseEvent(e);
       helpBox.css("top", xy[1] + 10).css("left", xy[0] + 10).show();
      },
      function(e) {
       helpBox.hide();
      }
     ).click(function() { return false; });
    jQuery("body").append(this);
   }
  );
 jQuery("input[disabled], textarea[disabled], select[disabled]", ctx).addClass("disabled");
 jQuery("input[overlayLabel], textarea[overlayLabel]", ctx).each(function() {
  var $this = jQuery(this);
  var id = this.getAttribute('id');
  this.removeAttribute('id');
  $this.css('position', 'relative');
  $this.after('<span class="OverlayLabelContainer"></span>');
  var $container = jQuery($this.get(0).nextSibling);
  if (id) $container.attr('id', id);
  $container.append($this);
  $container.append('<span class="OverlayLabelText"></span>');
  var $label = jQuery($container.get(0).lastChild);
  $label.click(function() {
   $this.focus();
  });
  $label.append(this.getAttribute('overlayLabel'));
  $this.focus(function() {
   $label.hide();
  }).blur(function() {
   if ($this.val() == null || $this.val().length == 0) {
    $label.show();
   } else {
    $label.hide();
   }
  }).blur();
 });
 jQuery("input[SFLookup]", ctx)
  .each(function() {
   jQuery(this).after("<span class='formLookup' style='display: inline; font-size: 120%; color: green; cursor: pointer;'>" + this.getAttribute('SFLookup') + "?</span>");
   var input = this;
   jQuery(this.nextSibling).click(function() {
    alert('clicked(' + input.getAttribute('SFLookup') + ')');
   });
  }
 );
 $ctx.find('form').each(function() {
  var $form = jQuery(this);
  var deps = {};
  $form.find('[sfDisplay]').each(function() {
   deps[this.getAttribute('sfDisplay')] = true;
  });
  for (var id in deps) {
   $form.find('[speedFormId="' + id + '"]').change(function() {
    var $items = $form.find('[sfDisplay="' + this.getAttribute('speedFormId') + '"]');
    var type = this.getAttribute('type');
    if (type == null) type = 'text';
    type = type.toLowerCase();
    var active;
    if ('checkbox' == type || 'radio' == type) {
     active = this.checked;
    } else {
     active = jQuery(this).val() != '';
    }
    if (active) {
     $items.show();
    } else {
     $items.hide();
    }
   }).change();
  }
 });
 $ctx.find('.formfielderror').each(function() {
  var $this = jQuery(this);
  $this.find('.formfield-errors').hide();
  $this.transientFocus(function() { return true; }, function() {
   $this.find('.formfield-errors').show();
   return function() {
    $this.find('.formfield-errors').hide();
   };
  });
 });
 jQuery('.formfielderror:first').click();
});


