﻿/**
* @fileOverview Overwrite the default configuration for participant validation in this file.
* The default configuration can be found in Sundio\generic\common\validator.js
* Specific configuration for adult participants
* @extends Validator.Config.ParticipantConfig
* @constructor
*/
Validator.Config.BrochureConfig = function() {
	this.productcode = {
		custom: function() {
			// At least one brochure checkbox should be selected
			return $("fieldset.brochures input[type=checkbox]:checked").length > 0;
		}
	};

	this.title = {
		required: true,
		custom: function() {
			return $("#mag-title").val() != "0";
		}
	};

	this.firstletter = {
		required: true,
		maxLength: 10,
		regex: Validator.regexes.text
	};

	this.lastname = {
		required: true,
		maxLength: 100,
		regex: Validator.regexes.text
	};

	this.country = {
		required: true
	};

	this.postal = {
		required: true,
		maxLength: 6,
		minLength: 1,
		regex: Validator.regexes.postal,
		loadStreetsByPostalCodeCallback: function(input, xmlDocument) {
			LoadStreets(input, xmlDocument);
		}
	};

	this.city = {
		required: true,
		maxLength: 100
	};

	this.street = {
		required: true,
		maxLength: 100
	};

	this.streetnr = {
		required: true,
		regex: Validator.regexes.number
	};

	this.streetnrext = {
		required: false,
		maxLength: 10
	};

	this.phoneprivate = {
		required: false,
		regex: Validator.regexes.phone,
		maxLength: 50
	};

	this.phonework = {
		required: false,
		regex: Validator.regexes.phone,
		maxLength: 50
	};

	this.email = {
		required: true,
		regex: Validator.regexes.email,
		maxLength: 250
	};

	LoadStreets = function(input, xmlDocument) {
		var city = null;
		if (xmlDocument !== null && ($('#mag-country').length == 0 || $('#mag-country option:selected').val() == 'NL')) {
			city = xmlDocument.getElementsByTagName("CITY");
			$("#mag-city").val(city[0].getAttribute("cityName"));
			var nStreets = city[0].getElementsByTagName("STREET").length;
			if (nStreets === 1) {
				$("#mag-street").val(city[0].firstChild.getAttribute("streetName"));
			}
			else {
				var index = 0;
				$("#mag-streetList").find('option').remove();
				while (index < nStreets) {
					$("#mag-streetList").append($('<option></option>').val(city[0].getElementsByTagName("STREET")[index].getAttribute("streetName"))
					.text(city[0].getElementsByTagName("STREET")[index].getAttribute("streetName")));
					index++;
				}
				$("#mag-street").val($("#mag-streetList option:selected").val());
			}
			ShowStreetFields(nStreets == 1);
			$("#mag-street").attr('readonly', true);
			$("#mag-city").attr('readonly', true);
			$("#mag-streetnr").focus();
		}
		else {
			ResetStreetFields();
		};
	};

	// Bind street list
	$("#mag-streetList").change(function() {
		$("#mag-street").val($("#mag-streetList option:selected").val());
	});

	ShowStreetFields = function(isSingleStreet) {
		if (isSingleStreet) {
			$("#mag-street").show();
			$("#mag-streetList").hide();
		}
		else {
			$("#mag-street").hide();
			$("#mag-streetList").show();
		}
	};

	ResetStreetFields = function() {
		$("#mag-street").attr('readonly', false);
		$("#mag-city").attr('readonly', false);
		$("#mag-street").val('');
		$("#mag-city").val('');
		$("#mag-streetList").find('option').remove();
		ShowStreetFields(true);
	};
};
