function ajax_contact_form(form) {

	$(form + " #cf_submit").click(function(){
	
    var fields = "";
    var error = false;
    
    $('.cf_error').remove();
    
    $(form + " input[id*='cf_field']").each(function () {    
      //if ($(this).attr("rel") == "mandatory" && $(this).val() == "") {
      if ($(this).hasClass("error") || ($(this).attr("rel") == "mandatory" && ($(this).val() == "" || $(this).val() == "Name..." || $(this).val() == "Email...") || $(this).val() == "First Name" || $(this).val() == "Last Name" || $(this).val() == "Email")) {
        error = true;
        $(this).addClass("cf_error_field");
        $(this).after('<div class="cf_error">Please fill in this mandatory field correctly.</div>');
      } else {      
        fields += 'input' + "~" + $("label[for='"  + $(this).attr("id") + "']").text() + "~" + $(this).val() + "#";
      }
    });
    $(form + " textarea[id*='cf_field']").each(function () {     
      //if ($(this).attr("rel") == "mandatory" && $(this).val() == "") {
      if ($(this).hasClass("error") || ($(this).attr("rel") == "mandatory" && ($(this).val() == "" || $(this).val() == "Message..."))) {
        error = true;
        $(this).addClass("cf_error_field");
        $(this).after('<div class="cf_error">Please fill in this mandatory field correctly.</div>');
      } else {          
        fields += 'textarea' + "~" + $("label[for='"  + $(this).attr("id") + "']").text() + "~" + $(this).val() + "#";
      }
    });

    if (!error) {
    
      //alert(fields);
      

      $.ajax({
        type: 'POST',
        url: template_url + '/forms/index.php',
        data: {
          to: $("#cf_to").val(),
          from: $("#cf_from").val(),
          subject: $("#cf_subject").val(),
          success_message: $("#cf_success_message").val(),
          fields: fields,
          file: $("#cf_file").val(),
          file_tmp: $("#cf_file_tmp").val()
        },
        success: function(data){
          //alert("data: " + data);
          $('#cf_fields').html(data);         
        }
      });
      
      $('#cf_fields').html('<p class="contact_form_result"><span>Please wait...</span></p>');
      
      
    }

  	return false;
  	
	});




  
}


  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 ajax_news_form(form) {

	$(form + " #cf_submit").click(function(){
            
            
                  // First, disable the form from submitting
      $('form#news_form').submit(function() { return false; });


    var fields = "";
    var error = false;

    $('.cf_error').remove();

    $(form + " input[id*='cf_field']").each(function () {
      //if ($(this).attr("rel") == "mandatory" && $(this).val() == "") {
      if ($(this).hasClass("error") || ($(this).attr("rel") == "mandatory" && ($(this).val() == "" || $(this).val() == "Name..." || $(this).val() == "Email...") || $(this).val() == "First Name" || $(this).val() == "Last Name" || $(this).val() == "Email")) {
        error = true;
        $(this).addClass("cf_error_field");
        $(this).after('<div class="cf_error">Please fill in this mandatory field correctly.</div>');
      } else {      
        fields += 'input' + "~" + $("label[for='"  + $(this).attr("id") + "']").text() + "~" + $(this).val() + "#";
      }
    });


    if (!error) {

      //alert(fields);


      // Grab form action
      var formAction = $("form#news_form").attr("action");

      // Hacking together id for email field
      // Replace the xxxxx below:
      // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
      var id = "cvfh";
      var emailId = "cm-"+ id + "-" + id;

      //alert(emailId);

       // Validate email address with regex
      if (!checkEmail(emailId)) {
        alert("Please enter a valid email address");
        return;
      }
     

      // Serialize form values to be submitted with POST
      var str = $("form#news_form").serialize();

      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>



       $.ajax({
        url: template_url + "/forms/proxy.php",
        type: "POST",
        data: serialized,
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
            alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
          }
          else
          {
            $("#news-hide").hide(); // If successfully submitted hides the form
            $("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
            $("#confirmation").tabIndex = -1;
            $("#confirmation").focus(); // For screen reader accessibility
            // Fire off Google Analytics fake pageview
            //var pageTracker = _gat._getTracker("UA-XXXXX-X");
            //pageTracker._trackPageview("/newsletter_signup");
          }
        }
      });

    }

  	return false;

	});

}

$(document).ready(function() {

        $("#sub-news").click(function(){
            $('#sub-news').hide();
            $('#news-hide').fadeIn('slow');
        });

        

	ajax_contact_form("#contact_form");
        ajax_news_form("#news_form");

});







