/**
 *
 *  Custom validation functions for jquery.validate.js
 *
 *  Functions defined here can be used by adding the name of the function to the class-attribute of an input-element.
 *  For example:
 *
 *  <input name="postcode" class="zippcode-nl" />
 *
 */

/**
 * Default settings
 */
jQuery.validator.setDefaults(
{
	errorPlacement: function(error, element)
	{
		// only append error if there is not one already there
		if (element.parent().find(".error:visible").size() <= 1)
		{
			error.appendTo( element.parent() );
		}
	},
    submitHandler: function(form)
    {
	  // before applying the loading-mask, modify some default settings
      // see jquery/jquery.loading.js
	  $.loading.maskCss.background  = '#fff';
	  $.loading.maskCss.opacity     = .75;
	  $.loading.working.time		= 20000;
	  $.loading.working.text        = 'Nog steeds bezig met verzenden...';
	  $.loading.error.time 			= 120000;
	  $.loading.error.text          = 'Verzenden duurt te lang; waarschijnlijk is er iets fout gegaan.<br />' +
                                      'Probeer het formulier opnieuw te verzenden.<br />' +
									  '<a class="stop-loading" href="#">Klik hier om terug te gaan naar het formulier</a>';

      // define error-function which must be called when the server does not answer the post-request
      $.loading.error.run = function(opts)
      {
        var e = opts.error, self = this;
        e.timeout = setTimeout(function()
        {
          self.width('350px').html(opts.text = e.text).addClass(e.classname);
          self.find('a.stop-loading').click( function()
		  {
		  	// remove loading-mask
		    self.closest('form').loading(false);

		    // abort post-request
		    if (window.stop)
		      window.stop(); // firefox (unfortunately the animated gif is also stopped, so when the submitbutton is pressed for the second time no animation is shown)
		    else if (document.execCommand)
 		      document.execCommand("Stop"); // ie

		    return false;
          });
          opts.place.call(self, opts);
        }, e.time);
      }

	  var sendText = 'Bezig met verzenden - een moment geduld a.u.b.';
      if (additionalSendText = $(form).find("input[type='submit']").attr('title'))
      {
	  	 sendText += '\n' + additionalSendText;
	  }

	  // apply the loading mask
	  $(form).loading(
	  {
	  	text: sendText,
	    mask: true,
	    align: 'center'
	  });

    // trick google analytics to assess true conversion
    if(form.action.indexOf("perl/aanmelding") != -1) {
      // uncomment following line to track subscriptions to a specific
      // server
      //page = '/conversion/' + form.action.substr(8);
      page = '/conversion/aanmelding';
      pageTracker._trackPageview (page);
    }
    
    // submit the form
	  form.submit();
   
	  // hack for IE6/7; after form-submit IE stops all gif-animations. Reload image to restart animation:
   	  var urlBackImage = $('.loading').css('backgroundImage');
	  $('.loading').css('background-image', urlBackImage);
    }
});


/**
 * "zippcode-nl"
 * check for dutch zippcode
 */
jQuery.validator.addMethod(
	"zippcode-nl",
	function(value, element)
	{
		value = jQuery.trim(value);
		if (!value) return true;
		var zippcodeRegExp = /^[0-9]{4}\s*[a-z|A-Z]{2}$/;
		return zippcodeRegExp.test(value);
	},
	"Vul a.u.b. een geldige postcode in");  // default message; can be overruled by title-attrubute

jQuery.validator.addMethod(
    "phonenumber-nl",
    function(value, element)
    {
		value = jQuery.trim(value);
		if (!value) return true;
		var phonenumberRegExp = /^[-0-9. +()]+$/;
		return phonenumberRegExp.test(value);
    },
    "Vul a.u.b. een geldig telefoonnummer in");

/**
 * "at-least-one"
 * can be used to check whether at least one checkbox of a checkboxgroup is checked
 * For example:
 *  <input type="checkbox" name="fruitstukken[]" class="at-least-one" value="peer" 		title="kies tenminste een fruitstuk"/>
 *  <input type="checkbox" name="fruitstukken[]" class="at-least-one" value="banaan" />
 */
jQuery.validator.addMethod(
	"at-least-one",
	function(value, element)
	{
		var name = $(element).attr('name');
		return ($("input.at-least-one[name='" + name + "']:checked").length > 0);
	},
	"Kies tenminste &eacute;&eacute;n vinkje"); // default message; can be overruled by title-attrubute

/**
 * "origin_alt-check"
 * Specific check used in signup form.
 * Whether this field is required depends on the option chosen in the selectbox name="origin"
 */
jQuery.validator.addMethod(
    "origin_alt-check",
    function(value, element)
    {
		if ($("select[name='origin'] option:selected").attr('class') == 'origin_alt-required')
		{
		    value = jQuery.trim(value);
			if (!value) return false;
		}
		return true;
    },
    "Dit veld is verplicht");

