var tipTitle = '';

function ag2FormTipsV3(sel, preamble, postamble, doCheckboxes) {    
  var config = {    
       sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
       interval: 200, // number = milliseconds for onMouseOver polling interval    
       over: showTip, // function = onMouseOver callback (REQUIRED)    
       timeout: 500, // number = milliseconds delay before onMouseOut    
       out: hideTip // function = onMouseOut callback (REQUIRED)    
  };

  //this is a highly unusual hack...stuffing variables into hidden divs? come on...but it's the fastest way...
  $('<div id="ag2FormTipPreamble"></div>').hide().appendTo('body').html(preamble);
  $('<div id="ag2FormTipPostamble"></div>').hide().appendTo('body').html(postamble);
    
  $('<div id="livetip"></div>').hide().appendTo('body');
  
  var checkboxText = (doCheckboxes == true) ? '' : ':not([type="checkbox"])';
  
  $(sel + checkboxText).hoverIntent(config);
}

function showTip(e) {
alert('showing');
  tipTitle = generateTip($(this).id);
  id = 
  $('#livetip')
     .css({
       top: $(this).offset().top + 12,
       left: $(this).offset().left + 265,
       padding: "10px"
     })
     .html('<div>' + tipTitle + '</div>')
     .show();
}

function hideTip(e) {
  $('#livetip').hide();
}


  
  var tipTitle = '';
  
    
function ag2FormTips(el, preamble, postamble, targetTag, doCheckboxes) {    
  // Experimental form input tips...
  //alert('pre: ' + preamble);
  
  targetTag = targetTag ? targetTag : 'input';
  var checkboxText = (doCheckboxes == true) ? '' : ':not([type="checkbox"])';
  var excludeSubmit = ':not([type="submit"])';
  
  //this is a highly unusual hack...stuffing variables into hidden divs? come on...but it's the fastest way...
  $('<div id="ag2FormTipPreamble"></div>').hide().appendTo('body').html(preamble);
  $('<div id="ag2FormTipPostamble"></div>').hide().appendTo('body').html(postamble);
    
  $('<div id="livetip"></div>').hide().appendTo('body');
  
  var tipTitle = '';
  $('#' + el).live('mouseover', function(event) {
   var $targets = $(event.target).closest(targetTag + checkboxText + excludeSubmit);
   if ($targets.length) {
     var target = $targets[0];
     
     // build up a friendly title for a field if we don't have one provided by default
     if (!target.title) 
     { 
       tipTitle = generateTip(target.id);
     }
     else {
       tipTitle = target.title;
     }
     //tipTitle = link.title ? link.title : 'Enter a ' + link.id.replace('_', ' ');
     target.title = '';
     $('#livetip')
     .css({
       top: $('#' + target.id).offset().top,
       left: $('#' + target.id).offset().left + $('#' + target.id).width() + 15,
       padding: "10px"
     })
     .html('<div>' + tipTitle + '</div>')
     .show();
   }
  }).live('mouseout', function(event) {
   var $targets = $(event.target).closest(targetTag  + checkboxText + excludeSubmit);
   if ($targets.length) {
     $targets.attr('title', tipTitle);
     $('#livetip').hide();
   }
  });
}

function ag2FormTipsOrig(el, preamble, postamble, targetTag) {    
  // Experimental form input tips...
  //alert('pre: ' + preamble);
  
  targetTag = targetTag ? targetTag : 'input';
  
  //this is a highly unusual hack...stuffing variables into hidden divs? come on...but it's the fastest way...
  $('<div id="ag2FormTipPreamble"></div>').hide().appendTo('body').html(preamble);
  $('<div id="ag2FormTipPostamble"></div>').hide().appendTo('body').html(postamble);
    
  $('<div id="livetip"></div>').hide().appendTo('body');
  
  var tipTitle = '';
  $('#' + el).bind('mouseover', function(event) {
   var $targets = $(event.target).closest(targetTag + ':not([type="checkbox"])');
   if ($targets.length) {
     var target = $targets[0];
     
     // build up a friendly title for a field if we don't have one provided by default
     tipTitle = generateTip(target.id);

     //tipTitle = link.title ? link.title : 'Enter a ' + link.id.replace('_', ' ');
     target.title = '';
     $('#livetip')
     .css({
       top: event.pageY + 12,
       left: event.pageX + 12,
       padding: "10px"
     })
     .html('<div>' + tipTitle + '</div>')
     .fadeIn('slow');
   }
  }).bind('mouseout', function(event) {
   var $targets = $(event.target).closest(targetTag  + ':not([type="checkbox"])');
   if ($targets.length) {
     $targets.attr('title', tipTitle);
     $('#livetip').hide();
   }
  }).bind('mousemove', function(event) {
   if ($(event.target).closest(targetTag  + ':not([type="checkbox"])').length) {
     $('#livetip').css({
       top: event.pageY + 12,
       left: event.pageX + 12
     });
   }
  });
}

function generateTip(id, showObj) {
  if (!$('#' + id).attr('title')) { 
    var parts = id.split('_');
    var obj = parts.shift();
    var pre = $('#ag2FormTipPreamble').html();
    var post = $('#ag2FormTipPostamble').html();
    
    //alert('now pre: ' + preamble);
    obj = (obj.lastIndexOf('sf') != -1) ? '' : obj;
    obj = (obj && pre) ? obj + "'s" : obj;
    obj = (!pre) ? obj.charAt(0).toUpperCase() + obj.slice(1) : ' ' + obj;
    
    var fld = '';
    
    for (part in parts) {
      fld += parts[part] + ' ';
    } 
    
    post = post ? ' ' + post : post;
    
    return pre + obj + post + ' ' + fld
  }
  
  return $('#' + id).title;
}

function findLastCheckbox(el) {
  var rows = $(el).parent().prevAll();
  var checkbox = false;
  
  rows.each(function() {
    if ($(this).find(':checkbox').size() > 0) {
      checkbox = $(this).find(':checkbox')[0];
      return false;
    }
  });
  
  if (!checkbox) { //then check previous colum 
    rows = $('.col1').find('div');
    rows.each(function() {
      if ($(this).find(':checkbox').size() > 0) {
        checkbox = $(this).find(':checkbox')[0];
      }
    });
  }

  return checkbox;
}

function ag2FormHideFields() {
  // hide form fields that should start hidden, but be smart for edit forms

  $('.start_hidden').each(function() {
    //alert('me : ' + this.id);
    
    //for checkboxes and their counterparts
   // var prevInput = $(this).parent().prev().find('input').size() > 0  // then it's in the same form column 
     //             ? $(this).parent().prev().find('input')[0] 
       //           : $('.col1').find('div:last').find(':checkbox')[0]; // otherwise it's in the previous column
    
    var prevInput = findLastCheckbox(this);
                          
    if (prevInput && $('#' + prevInput.id + ':checked').length == 0) {
      $(this).parent().hide();
    }
  });
  
  $('.start_hidden_select').each(function() { 
    //for selects and their counterparts
    var prevInput = $(this).parent().prev().find('select').size() > 0 // then it's in the same form column 
                  ? $(this).parent().prev().find('select')[0]
                  : $('.col1').find('div:last').find('select')[0];  // otherwise it's in the previous column
                                   
    if (prevInput && $('#' + prevInput.id + ' option:selected').text() != 'Other') {
      $(this).parent().hide();
    }
  });
}

var currentPassword = '';

// This only needs to be called for IE browsers
function ag2FormConcealPasswordIe(el) {
  var oldPwd = el;
  var newPwd = $('<input type="password" />');
  newPwd.attr('name', oldPwd.attr('name'));
  newPwd.val(oldPwd.val()).addClass('password').addClass('required');
  newPwd.attr('id', oldPwd.attr('id'));
    
  newPwd.blur(function() { 
    if (!$(this).val()) {
      $(this).val(currentPassword); 

      if ($(this).attr('type') != 'text') {
        ag2FormRevealPasswordIe($(this));
      }
    }
  });  
      
  newPwd.insertBefore(oldPwd);
  newPwd.focus();
  oldPwd.remove();
}

// This only needs to be called for IE browsers
function ag2FormRevealPasswordIe(el) {
  var oldPwd = el;
  var newPwd = $('<input type="text" />');
  newPwd.attr('name', oldPwd.attr('name'));
  newPwd.val(oldPwd.val()).addClass('password').addClass('required');
  newPwd.attr('id', oldPwd.attr('id'));
  
  newPwd.focus(function() { 
    if ($(this).val() == 'Password' || $(this).val() == 'Confirm Password') {
      currentPassword = $(this).val();
      $(this).val(''); 
    }
    if ($(this).attr('type') != 'password') {
      ag2FormConcealPasswordIe($(this));
    }
  });
  
  newPwd.insertBefore(oldPwd);
  oldPwd.remove();
}

function ag2FormRevealPasswords() {
  $('.password').each(function() {
    var oldPwd = $(this);
    
    if (!$.support.htmlSerialize) { // if screwed up IE flaw detected
      ag2FormRevealPasswordIe(oldPwd);  
    }
    else {
      var newPwd = oldPwd.clone(true);
      newPwd.attr('type', 'text');
      newPwd.insertBefore(oldPwd);
      oldPwd.remove();
    }    
  });
}

function ag2FormConcealPasswordOrig() {
  $('.password').each(function() {
    var oldPwd = $(this);
    var newPwd = oldPwd.clone();
    
    newPwd.attr('type', 'password');
    newPwd.insertBefore(oldPwd);
    oldPwd.remove();
  });
  
function ag2FormDisableEnterKey(selector) {
  $(selector).keypress(function(e) {
    return e.keyCode != 13;
  });
}
}
