
$('form.simple-form').each(function() {
  var $self = $(this)
	var action = $self.attr('action')
	$self.submit(function() {
    var err = 0
    $('input[required]', $self).removeClass('error').each(function() {
      if (!$(this).val().trim()) {
        $(this).addClass('error')
        err++
      }
    })
    if (err) {
      alert('请填写表单中的必填项。')
    }
    else {
      var req = $.post(action, $self.serialize(), null ,'json')
      req.then(function(resp) {
        if (resp.result) {
          $self[0].reset()
          alert('提交成功, 谢谢您的支持!')
        }
        else
          alert(resp.error)
      }, function() {
        alert('提交失败: 服务器错误.')
      })
      return false
    }
		return false
	})
})

$('.map-container').each(function() {
  var p1 = $(this).attr('data-p').split(/\s*,\s*/g)
  var p2 = $(this).attr('data-m').split(/\s*,\s*/g)
  var map = new BMap.Map(this);
  map.centerAndZoom(new BMap.Point(parseFloat(p1[0]), parseFloat(p1[1])), 18);
  map.enableScrollWheelZoom(true);
  map.addControl(new BMap.NavigationControl());
  var marker = new BMap.Marker(new BMap.Point(parseFloat(p2[0]), parseFloat(p2[1])))
  map.addOverlay(marker);
  var infoWindow = new BMap.InfoWindow($($(this).attr('data-i')).html());
  marker.openInfoWindow(infoWindow);
  marker.addEventListener("click", function(){          
    this.openInfoWindow(infoWindow);
  })
})
