
$(document).ready(function() {
  prepareFields();
  $('#cancel').bind('click', function() { history.back(); });
  prepareHelp();
});

function prepareFields() {
  var fields = $('[initial]');

  for (var i in fields) {
    var field = fields[i];

    if (field.name == null || field.getAttribute == null) continue;

    field.originalClass = field.className;
    field.initial = field.getAttribute('initial');

    //$(window).bind('load', function() {
      setInitialValue(field);
    //});

    $(field).bind('blur', function() {
      if (this.value.length <= 0) {
        this.value = this.initial;
        this.className = this.className + ' blurred';
      }
    });

    $(field).bind('focus', function() {
      if (field.timer !=  null) {
        clearTimeout(field.timer); 
        field.timer = null;
      }
      if (this.value == this.initial) {
        this.value = '';
        this.className = this.originalClass;
      }
    });

    // just to be safe - reset the style if we find typing in here
    $(field).bind('keyup', function() {
      this.className = this.originalClass;
    });
  }
}

function setInitialValue(field) {
  if (field.value == null || field.value.length == 0) {
    field.value = field.initial;
    field.className = field.className + ' blurred';
  }
}

var nextHelp = null;

function prepareHelp() {
  $('form').append('<div id="help_box" class="help_box"><div id="help" class="help_inner"></div></div>');

  $('[help]').bind('focus', function(e) {
    if ($('#help_box').css('display') != 'none') {
      nextHelp = this;
    }
    else setHelp(this);
  });

  $('[help]').bind('blur', function(e) {
    $('#help_box').fadeOut('fast', function() {
      if (nextHelp != null) {
        setHelp(nextHelp);
        nextHelp = null;
      }
    });
  });
}

function setHelp(field) {
  var help = $(field).attr('help');
  var error  = $(field).attr('error');

  if (error != null && error.length > 0) {
    help = "<span class='error'>" + error + "</span>";
  }

  // set the contents of the help box
  $('#help').html(help);

  // position the help box
  var top = $(field).offset().top + 2;
  var left = $(field.form).offset().left + $(field.form).width() + 5;
  $('#help_box').css('left', left);
  $('#help_box').css('top', top);

  $('#help_box').fadeIn('fast', function() {
    // safety check to make sure something didn't change while we were animating
    if (nextHelp != null && field != nextHelp) {
      setHelp(nextHelp);
      nextHelp = null;
    }
  });
}

var validation_failure = false;

function reset() {
  validation_failure = false;
}

function mandatory(selector) {
  return check(selector, function(v) { return (v.length > 0); });
}

function minlength(selector, len, optional) {
  return check(selector, function(v) { return (optional && v.length == 0) || (v.length >= len); });
}

function pattern(selector, pattern) {
  return check(selector, function(v) { return pattern.test(v); });
}

function match(selector, comparator) {
  return check(selector, function(v) { return (v == $(comparator).val()); });
}

function check(selector, func) {
  var field = $(selector);
  if (field.length == 0) return true; 

  var value = field.val();

  if (!func(value)) {
    field.addClass('error');
    if (!validation_failure) field.focus();
    validation_failure = true;
    return false;
  }
  else {
    field.removeClass('error');
    return true;
  }
}


