function checkEmail(email) { 
  var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var emailVal = $(email).val();
  return pattern.test(emailVal);
}

$(function(){

  $("#contact_form input:submit").click(function(){

    // disable form
    $("#contact_form").submit(function() {return false; });

    var formAction = $("#contact_form").attr("action");

    var emailId = $("#email-input")

    if(!checkEmail(emailId)) {
      alert("Please enter a valid email address")
      return;
    }

    // serialize form
    var str = $("form#contact_form").serialize();

    $.ajax({
      type: "POST",
      url: "http://muranolic.com/php/submit.php",
      data : str,
      beforeSend: function(){
        $("#loader p").html("Sending Information...").parent().show();
      },
      error: function(){
        $("#loader p").html("Please try again there has been an error.").parent().show();
      },
      success: function(){
        $("#loader p").html("Success..").parent().show();
      },
      complete: function(){
        $("#loader p").html("Thank you!").parent().show();
        setTimeout(function(){
          $("#loader").fadeOut('slow');
          }, 500);
        }
      })
    });

 


      // parse listings from live MS site using Pure.js  - Added by Moxie 10.12.2011
    if($("#available-listings").length > 0){
            
      $.getJSON('http://modernspacesnyc.com/murano_json?callback=?', function(data){
        var directive = {
          'tr' :{
            'available<-available':{
              'th.tableImg img@src' : 'available.img',
              'th.tableImg a@href' : 'http://modernspacesnyc.com/listings/new-developments/queens/long-island-city/#{available.id}',
              'th.availId' : 'available.id',
              'th.availBed' : 'available.beds',
              'th.availBath' : 'available.baths',
              'th.availFeet' : 'available.sq_feet',
              'th.availPrice' : 'available.price',
              'th.availAgent a': {
                'contact<-available.contact':{
                  '.' : 'contact.agent',
                  '@href' : 'contact.url'
                }
              },
              'th.availView a@href' : 'http://modernspacesnyc.com/listings/new-developments/queens/long-island-city/#{available.id}'

            }
          }
        };             
        $("#available-listings").render(data, directive);
      });  
    }   
              
    // This was not working??!
    var listings = $("#load-listings");

    $(document).ajaxStart(function(){        
      // console.log('start')
      listings.show();
    }).ajaxStop(function(){
      // console.log('stop')
      listings.hide();
    });                       
    
  });
