function post(frm) {
  if(validate(frm)) {
    $.ajax({
      type: 'POST',
      url: '/blank.jsp',
      data: $(frm).serialize(),
      success: function() {
        $(frm).parent('div').load("/ajax" + $(frm).attr("action"));
        if(_gaq)
          _gaq.push(['_trackPageview', $(frm).attr("action")]);
      }
    });
    return false;
  }
  return false;
}

function validate(frm) {
  var reqs = $('.required:visible', frm);
  var $first = 0;
  var errors = 0;
  for(i=0; i<reqs.length; i++) {
    $req = $(reqs[i]);
    if(!hasValue($req)) {
      if(!$first) $first = $req;
      $req.addClass('error').change(function() {
        if(hasValue($(this))) {
          $(this).removeClass('error');
        }
      });
      errors++;
    }
  }

  if(errors) {
    alert('Please answer all the required questions.');
    $first.focus();
    return false;
  }
  return true;
}

function hasValue($req) {
  r = false;
  type = $req.attr('type');
  if(type == 'checkbox' || type == 'radio')
    r = $('input[name='+$req.attr('name')+']:checked').length != 0;
  else
    r = $req.val() != '';

  return r;
}

function openPopupWindow(url, width, height, scrolling, name) {
  if (!width) width = 350;
  if (!height) height = 350;
  if (!scrolling) scrolling = "no"; 
  if (!name) name = "win"+Math.round(Math.random()*1000); 
  
  features = "width="+width+","
           + "height="+height+","
           + "toolbar=no,"
           + "location=no,"
           + "status=no,"
           + "menubar=no,"
           + "scrollbars="+scrolling+","
           + "top=50,"//+(window.screen.height-height)/2+","
           + "left=50";//+(window.screen.width-width)/2;
  window.open(url,name,features);
}

