$(document).ready( function() {
  
  // autocomplete uitzetten
  $("form#form_registration").attr("autocomplete", "off");
  
  
  /***************************
   * realtime form validatie *
   ***************************/
  $("#form_contact, #form_appointment, #form_callback, #form_newsletter, #form_complaint, #form_change, #form_mailme, #form_rentes_vervolg, #form_signout").validate({
		rules: {
			lastname : {
			  required  : true,
			  minlength : 2
			},
      email : {
        required  : true,
        email     : true
      },
      firstname : {
        required  : true
      },
      street : {
        required  : true
      },
      number : {
        required  : true
      },
      zip : {
        required  : true,
        minlength : 6,
        maxlength : 10,
        zipNL     : true
      },
      city : {
        required  : true
      },
      phonenumber : {
        required  : true,
        number: true,
        minlength : 10,
        maxlength : 10
      },
      your_email : {
        required  : true
      }
      
		},
		messages: {
			lastname    : {
			  required  : "Achternaam verplicht",
			  minlength : jQuery.format("Tenminste {0} karakters vereist!")
			},
			email       : {
			  required  : "Email verplicht",
			  email     : "Email niet juist"
			},
			firstname       : {
			  required  : "Voornaam verplicht"
			},
			street       : {
			  required  : "Straatnaam verplicht"
			},
			number       : {
			  required  : "Huisnummer verplicht"
			},
			zip       : {
			  required  : "Postcode verplicht",
			  minlength : jQuery.format("Tenminste {0} karakters vereist!"),
        maxlength : jQuery.format("Maximaal {0} karakters!"),
			  zipNL     : "Postcode niet juist"
			},
			city       : {
			  required  : "Woonplaats verplicht"
			},
			phonenumber       : {
			  required  : "Vul een geldig telefoonnummer in",
			  number    : "Vul aub alleen cijfers in",
			  minlength : jQuery.format("Tenminste {0} karakters vereist!"),
			  maxlength : jQuery.format("Maximaal {0} karakters!")
			},
			your_email       : {
			  required  : "Email verplicht"
			}
    },
    errorClass    : "error",
    validClass    : "valid",
    wrapper       : "span"
  });
  
  jQuery.validator.addMethod("zipNL", function(value, element, param) {
    return this.optional(element) || /^[1-9]{1}[0-9]{3}[ ]?[A-Za-z]{2}$/.test(value);
  });
     
  // hiden van container advies-type in forms
  $('#goalcontainer').hide();
  $('#advisorcontainer').hide();

});

/*
*   Switch in forms voor advies-type
*/

function doTargetSwitch(newpulldown) {
  if(newpulldown == 'Hypotheken')
  {
    $('#goalcontainer').show();
    $('#form_appointment_goal :option').remove();
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Maak een keuze...").text('Maak een keuze...'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Aankoop woning").text('Aankoop woning'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Tweede hypotheek").text('Tweede hypotheek'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Oversluiten hypotheek").text('Oversluiten hypotheek'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "WOZ waarde").text('WOZ waarde'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Updategesprek").text('Updategesprek'));
  }
  if(newpulldown == 'Verzekeringen')
  {
    $('#goalcontainer').show();
    $('#form_appointment_goal :option').remove();
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Maak een keuze...").text('Maak een keuze...'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Reisverzekering").text('Reisverzekering'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Inboedel/Opstal verzekering").text('Inboedel/Opstal verzekering'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Autoverzekering").text('Autoverzekering'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Overlijdensrisicoverzekering").text('Overlijdensrisicoverzekering'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Rechtsbijstandverzekering").text('Rechtsbijstandverzekering'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Uitvaartverzekering").text('Uitvaartverzekering'));
  }
  if(newpulldown == 'Pensioenen')
  {
    $('#goalcontainer').show();
    $('#form_appointment_goal :option').remove();
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Maak een keuze...").text('Maak een keuze...'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Orientatie").text('Orientatie'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Fiscale jaarruimte berekenen").text('Fiscale jaarruimte berekenen'));
    $('#form_appointment_goal').append($("<option></option>").attr("value", "Pensioentekort").text('Pensioentekort'));
  }
  if(newpulldown == 'Sparen en beleggen')
  {
    $('#goalcontainer').hide();
  }
  if(newpulldown == 'Financieringen')
  {
    $('#goalcontainer').hide();
  }
  
  if(newpulldown == 'Ik ben nog geen klant')
  {
    $('#advisorcontainer').hide();
  }
  
  if(newpulldown == 'Ik ben al klant')
  {
    $('#advisorcontainer').show();
  }
}


/*
 * Selects an option by value
 *
 * @name     selectOptions
 * @param    value specifies, which options should be selected
 * @example  jQuery("#myselect").selectOptions("val1");
 *
 */

jQuery.fn.selectOptions = function(value) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;

			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}