﻿var $j = jQuery.noConflict();

$j(document).ready(function () {
	$j('img.social-twitter').hover(function () {
		$j(this).attr("src", $j(this).attr("src").replace("i_social_twitter.gif", "i_social_twitter_h.gif"));
	}, function () {
		$j(this).attr("src", $j(this).attr("src").replace("i_social_twitter_h.gif", "i_social_twitter.gif"));
	});

	$j('img.social-facebook').hover(function () {
		$j(this).attr("src", $j(this).attr("src").replace("i_social_facebook.gif", "i_social_facebook_h.gif"));
	}, function () {
	    $j(this).attr("src", $j(this).attr("src").replace("i_social_facebook_h.gif", "i_social_facebook.gif"));
	});
});

$j(function () {

	$j("ul.dropdown li").hover(function () {

		$j(this).addClass("hover");
		$j('ul:first', this).css('visibility', 'visible');

	}, function () {

		$j(this).removeClass("hover");
		$j('ul:first', this).css('visibility', 'hidden');

	});

	$j("ul.dropdown li ul li:has(ul)").find("a:first").append(" &raquo; ");

	$j("ul.dropdown li:has(ul)").hover(function () {
		$(this).addClass("hasChild");
	}, function () {
		$(this).removeClass("hasChild");
	});
});

function toggleDiv(strDivID) {
    if (document.getElementById(strDivID).style.display == 'none') {
        document.getElementById(strDivID).style.display = 'block';
    }
    else {
        document.getElementById(strDivID).style.display = 'none';
    }
}

/*************************************
Form
*************************************/

// Focus: remove watermark text and style
function FocusThisBox(textBox, value, cssClass) {
	if (textBox.value == value) {
		textBox.value = ''
		textBox.className = cssClass
	}
}

// Blur: set watermark text and style
function BlurThisBox(textBox, value, cssClass) {
	if (textBox.value == '') {
		textBox.value = value
		textBox.className = cssClass
	}
}

function IsHidden(element) {
	if (!element.style) return false
	if (element.style.display == 'none') return true
	if (!element.parentNode) return false
	return IsHidden(element.parentNode)
}

function CheckRequired(sender, args) {
	if (IsHidden(document.getElementById(sender.controltovalidate))) {
		args.IsValid = true;
		return
	}

	if (args.Value != '') {
		args.IsValid = true;
	}
	else {
		args.IsValid = false;
	}

	SetErrorMessage(sender, args.IsValid, "Veld is verplicht");
	//CheckMaxCharacters(sender, args);
}

function CheckPostalCodeFormatTextBox(sender, args) {
	CheckField(sender, args, "^[1-9][0-9]{3}( )?[a-zA-Z]{2}$", "Ongeldige postcode.");
}

function CheckPassword(sender, args) {
	CheckField(sender, args, "^.*(?=.{5,})", "Wachtwoord is te kort.");
}

function CheckEmailFormatTextBox(sender, args) {
	CheckField(sender, args,
				"^[A-Za-z0-9_-]+([.][A-Za-z0-9_-]+)*[@][A-Za-z0-9-][A-Za-z0-9-]+([.-][A-Za-z0-9-]+)*[.]([A-Za-z]){2,6}$",
				"Ongeldig e-mailadres.");
}

function CheckFormFieldCompare(sender, args) {
	var ids = sender.FieldsToCompare.split(',');
	var value = '';

	for (var i = 0; i < ids.length; i++)
	{
		var field = document.getElementById(ids[i]);

		if (value == '')
		{
			value = field.value;
		}

		if (value != field.value)
		{
			args.IsValid = false;
		}
	}

	SetErrorMessage(sender, args.IsValid, "Velden komen niet overeen.");
}

function CheckTelnrFormatTextBox(sender, args) {
	CheckField(sender, args, "^[0-9- ]{10,15}$", "Ongeldig telefoonnummer.");
}

function CheckNumberFormatTextBox(sender, args) {
	CheckField(sender, args, "^[0-9][0-9]*$", "Ongeldig nummer.");
}

function CheckDateFormatTextBox(sender, args) {
	args.IsValid = true;
	CheckField(sender, args, "(19|20)[0-9]{2}-(0|1)[0-9]-[0-3][0-9]", "Ongeldige datum");
}

function CheckMaxCharacters(sender, args) {
	var max = sender.MaxCharacters;
	var length = args.Value.length;
	var value = args.Value;

	if (max == 0) {
		args.isValid = true;
		return;
	}

	if (args.Value.length > max) {
		args.IsValid = false;
		SetErrorMessage(sender, args.IsValid, "De tekst is te lang.");
		return;
	}
}


function CheckDisclaimer(sender, args) {
	var x = document.getElementById(sender.ControlID);
	var error = "U dient akkoord te gaan met de algemene voorwaarden";

	if (x.checked == true) {
		args.IsValid = true;
		$(sender).parent().attr('title', '')
		$(sender).hide()
		$(sender).parent().removeClass('error');
		$(sender).parent().addClass('ok');
		$('#' + sender.controltovalidate).removeClass('error');
	}
	else {
		args.IsValid = false;

		$(sender).parent().attr('title', error)
		$(sender).show()
		$(sender).parent().removeClass('ok');
		$(sender).parent().addClass('error');
		$('#' + sender.controltovalidate).addClass('error');
	}

	return;
}

function CheckField(sender, args, regex, errormessage) {
	if (IsHidden(document.getElementById(sender.controltovalidate))) {
		args.IsValid = true;
		return
	}

	var error;

	var arrMatch = new RegExp(regex).exec(args.Value);

	if (args.valueOf == '') {
		args.IsValid = false;
	}
	else if (!arrMatch) {
		args.IsValid = false;
		error = errormessage;
	}
	else {
		args.IsValid = true;
	}

	if (args.IsValid) {
		CheckMaxCharacters(sender, args);
	}

	SetErrorMessage(sender, args.IsValid, error);
}

function SetErrorMessage(sender, isValid, error) {
	if (!error) error = sender.errormessage

	if (isValid) {
		sender.className = "validator-false";
		//$(sender).hide()
	}
	else {
		sender.className = "validator-true";
		//$(sender).show()
	}
}

function showDivByCheckBox(divItem, checkBoxItem) {
	var visibility = (checkBoxItem.checked) ? "block" : "none";
	document.getElementById(divItem).style.display = visibility;
}

function ChangeTextBoxView(textBoxID, checkbox) {
	var textbox = document.getElementById(textBoxID);

	textbox.disabled = !checkbox.checked;

	if (!textbox.disabled) {
		textbox.focus();
	}
	else {
		textbox.value = ""
	}

}

function ChangeItemCheck(checkboxID, checkboxItem) {
	var array = checkboxID.split("~");

	for (var i = 0; i < array.length; i++) {
		document.getElementById(array[i]).checked = checkboxItem.checked;
	}
}

function UncheckItem(checkBoxID, checkBoxItem) {
	if (!checkBoxItem.checked) document.getElementById(checkBoxID).checked = false;
}

function FillFullName(textboxId, initialsId, infixId, lastnameId) {
	var textbox = document.getElementById(textboxId);

	var initials = document.getElementById(initialsId);
	var infix = document.getElementById(infixId);
	var lastname = document.getElementById(lastnameId);

	textbox.value = (initials.value != "" && lastname.value != "")
				? initials.value + "~" + infix.value + "~" + lastname.value
				: "";
}

function FillTextBox(textBox, dropdownDay, dropdownMonth, dropdownYear, mindate, maxdate) {
	var dropdownboxDay = document.getElementById(dropdownDay);
	var dropdownboxMonth = document.getElementById(dropdownMonth);
	var dropdownboxYear = document.getElementById(dropdownYear);

	document.getElementById(textBox).value = "";

	if (dropdownboxDay.value != "Dag") {
		if (dropdownboxMonth.value != "00") {
			if (dropdownboxYear.value != "Jaar") {
				date = new Date(dropdownboxYear.value, dropdownboxMonth.value, dropdownboxDay.value);

				if (date >= mindate && date <= maxdate) {
					document.getElementById(textBox).value = dropdownboxDay.value + '-' + dropdownboxMonth.value + '-' + dropdownboxYear.value;
				}
			}
		}
	}
}

function MultipleFieldsValidatorEvaluateIsValid(val) {

	controltovalidateIDs = val.controlstovalidate.split(',');
	switch (val.condition) {
		case 'OR':
			for (var controltovalidateIDIndex in controltovalidateIDs) {
				var controlID = controltovalidateIDs[controltovalidateIDIndex];
				if (ValidatorTrim(ValidatorGetValue(controlID)) != '') {
					return true;
				}
			}
			return false;
			break;
		case 'XOR':
			for (var controltovalidateIDIndex in controltovalidateIDs) {
				var controlID = controltovalidateIDs[controltovalidateIDIndex];
				if (controltovalidateIDIndex == '0') {
					var previousResult = !(ValidatorTrim(ValidatorGetValue(controlID)) == '');
					continue;
				}
				var currentResult = !(ValidatorTrim(ValidatorGetValue(controlID)) == '');
				if (currentResult != previousResult) {
					return true;
				}
				previousResult != currentResult;
			}
			return false;
			break;
		case 'AND':
			for (var controltovalidateIDIndex in controltovalidateIDs) {
				var controlID = controltovalidateIDs[controltovalidateIDIndex];
				if (ValidatorTrim(ValidatorGetValue(controlID)) == '') {
					return false;
				}
			}
			return true;
			break;
	}
	return false;
}

function CheckFileExtension(sender, args) {
	var fileupload = document.getElementById(sender.controltovalidate);
	var extension = fileupload.value.replace(fileupload.value.substr(fileupload.value, fileupload.value.lastIndexOf(".") + 1), "");
	var forbiddenExtensions = new Array("bat", "exe", "zip");
	var error = '';

	args.IsValid = true;

	for (var i = 0; i < forbiddenExtensions.length; i++) {
		if (extension == forbiddenExtensions[i]) {
			args.IsValid = false;
			error = 'Dit bestandsformaat is niet toegestaan';
		}
	}

	SetErrorMessage(sender, args.IsValid, error);
}

function ToggleEvents(dropdownId, divId) {
	var dropdown = document.getElementById(dropdownId);
	var div = document.getElementById(divId);

	div.style.display = (dropdown.options[dropdown.selectedIndex].value == 'Inschrijven voor een evenement'
		|| dropdown.options[dropdown.selectedIndex].value == 'Uitschrijven voor een evenement')
		? "block"
		: "none";
}

function ShowQuestion(divId, questionID) {
	$('div.faq_answer').hide();
	$('li.question').removeClass('true');

	var div = document.getElementById(divId);
	var question = document.getElementById(questionID);

	div.style.display = "block";
	question.className = question.className + " true";
}

function ToggleDiv(divId, display) {
	var div = document.getElementById(divId);
	div.style.display = (display == "True") ? "block" : "none";
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

// Expand
function Expand(link, textboxId, divAnswerId) {

	var div = link.parentNode.parentNode;
	var textbox = document.getElementById(textboxId);
	var divAnswer = document.getElementById(divAnswerId);


	array = textbox.value.split('|');

	if (array[1] != '') {
		var currentQuestion = document.getElementById(array[1]);
	}

	if (array[2] != '') {
		var currentAnswer = document.getElementById(array[2]);
	}

	if (array[2] != '') {
		var currentAnswerDiv = document.getElementById(array[3]);
	}

	if (div == currentQuestion) {
		currentQuestion.className = 'expand-closed';
		currentAnswer.className = 'question false';
		currentAnswerDiv.style.display = 'none';
		currentQuestion = null;
		currentAnswer = null;

		textbox.value = '';
	}
	else {
		if (currentQuestion != null) {
			currentQuestion.className = 'expand-closed'
			currentAnswer.className = 'question false'
			currentAnswerDiv.style.display = 'none';
		}

		currentQuestion = div;
		currentQuestion.className = 'expand-opened';
		currentAnswer = link.parentNode;
		currentAnswer.className = 'question true';
		divAnswer.style.display = "block";

		textbox.value = link.innerHTML + '|' + currentQuestion.id + '|' + currentAnswer.id + '|' + divAnswerId;
	}
}

///////////////////
//  Blocks
///////////////////

var Blocks =
	{
		currentPath: '',

		Click: function (hyperlink) {
			var id = this.GetClass(hyperlink)
			this.CloseOthers(id)
			this.ShowBlock(id)

			$('.CAO_HH').show()

			var lastCharUrl = hyperlink.href.substring(hyperlink.href.length - 1, hyperlink.href.length)
			if (lastCharUrl == "#") {
				$('.faqserviceQuestions').hide()
			}
		},

		GetClass: function (hyperlink) {
			var classes = hyperlink.className.split(' ')
			for (var i = 0; i < classes.length; i++) {
				if (classes[i].substring(0, 3) == 'li-') {
					return classes[i].substring(3, classes[i].length)
				}
			}
		},

		CloseOthers: function (path) {
			if (path.indexOf(this.currentPath) != 0) {
				// Hide current folders
				var folders = this.currentPath.split('_')
				var folderPath = ''
				for (var i = 0; i < folders.length; i++) {
					folderPath += folders[i]
					this.HideBlock(folderPath)
					folderPath += '_'
				}

				// Show current
				var folders = path.split('_')
				var folderPath = ''
				for (var i = 0; i < folders.length; i++) {
					folderPath += folders[i]
					this.ShowBlock(folderPath)
					folderPath += '_'
				}

			}
			this.currentPath = path
		},

		HideBlock: function (folderPath) {
			$('.' + folderPath).hide()
			$('.li-' + folderPath).removeClass('true')
		},

		ShowBlock: function (folderPath) {
			$('.' + folderPath).show()
			$('.li-' + folderPath).addClass('true')
		}
	}
