/*global $, has */


// Exported namespace.
var castform = window.castform = {};


/**
 * Make a remote call to the server.
 *
 * @param {String} path
 * @param {String} id Id of the form or async function.
 * @param {Object|String} values
 * @param {Function(Boolean, Object|String)} pass
 */
castform.remoteCall = function remoteCall(path, id, values, pass) {
  function onSuccess(data) {
    console.log('success', data);
    pass(data.success, data.msg);
  }

  var data = JSON.stringify({ id: id, values: values });
  console.log('data', data);
  $.ajax({
    type: 'post',
    url: '/castform/' + path,
    data: data,
    success: onSuccess,
    contentType: 'application/json; charset=UTF-8',
    dataType: 'json'
  });
};


/**
 * Remotely calls a validation function on the server.
 */
castform.async = function async(id) {
  return function asyncCall(value, pass) {
    castform.remoteCall('async', id, value, pass);
  };
};


/**
 * Activate forms validation on the site.
 */
$(function() {
  for (var key in castform.options) {
    if (has(castform.options, key)) {
      castform.options[key] = castform.validateForm('#form-' + key,
                                castform.options[key]);
    }
  }
});

@@ -359,110 +359,8 @@
 ) %7B%0A
-  function onSuccess(data) %7B%0A    console.log('success', data);%0A    pass(data.success, data.msg);%0A  %7D%0A%0A
   va
@@ -416,37 +416,8 @@
 %7D);%0A
-  console.log('data', data);%0A
   $.
@@ -438,12 +438,12 @@
 e: '
-post
+POST
 ',%0A 
@@ -504,17 +504,199 @@
 ss: 
-onSuccess
+function onSuccess(data) %7B%0A      console.log('success', data);%0A      pass(data.success, data.msg);%0A    %7D,%0A    error: function onError(jqXHR, textStatus) %7B%0A      pass(false, textStatus);%0A    %7D
 ,%0A  
@@ -731,23 +731,8 @@
 json
-; charset=UTF-8
 ',%0A 

add fail function to ajax call