// VERTICALLY ALIGN FUNCTION
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = Math.ceil((ph-ah) / 2);
	$(this).css('margin-top', mh);
	});
};
})(jQuery);


$(document).ready(function() {

	// Hide on loads
		$(".hide-on-load").hide();

		// Complaint slide
		$(".complaint .radio > li > a").click(function(e) {
			e.preventDefault();
			$(this).parent().children(".hide-on-load").slideToggle(150);
		});

		// FAQ slide
		$(".questions > li > h4 > a").click(function(e) {
			e.preventDefault();
			var speed = 150;
			var allNodes = $(this).parent().parent().parent().find(".answer");
			var nodeToOpen = $(this).parent().parent().children(".answer");

			allNodes.each(function() {
				if ($(this) != nodeToOpen) {
					$(this).slideUp(speed);
				}
			});

			if (nodeToOpen.is(':visible')) {
				nodeToOpen.slideUp(speed);
			} else {
				nodeToOpen.slideDown(speed);
			}

		});
		$(".questions .answer").hide();
		

		
		// abonlist slide
		$(".abon-title > a").click(function(e) {
			e.preventDefault();
			var speed = 150;
			var allNodes = $(this).parent().parent().find(".content");
			var nodeToOpen = $(this).parent().parent().children(".content");

			allNodes.each(function() {
				if ($(this) != nodeToOpen) {
					$(this).slideUp(speed);
				}
			});

			if (nodeToOpen.is(':visible')) {
				nodeToOpen.slideUp(speed);
			} else {
				nodeToOpen.slideDown(speed);
			}

		});
		// $(".abon-list .content").hide();
		

		// Show question based on hashtag
		var hashtag = window.location.hash.substring(1);
		if (hashtag.substring(0, 1) == "q") {
			var hashselect = ".questions .question-" + hashtag.substring(1)
					+ " .answer";
			$(hashselect).slideDown(150);
		}

		// Auto-focus on login
		$("#LoginForm_Login").focus();

		// Tooltips
		$('.help').qtip( {
			content : $(this).title,
			position : {
				corner : {
					tooltip : 'leftMiddle', // Use the corner...
					target : 'rightMiddle' // ...and opposite corner
				}
			},
			style : {
				border : {
					width : 5,
					radius : 10
				},
				padding : 10,
				textAlign : 'center',
				tip : true, // Give it a speech bubble tip with automatic corner
							// detection
				name : 'cream' // Style it according to the preset 'cream'
								// style
			}
		});

		// User login field trigger
		$("#LoginForm_Login").ForceNumericOnly();

		// date picker settings
		$(".datepicker").datepicker( {
			regional : 'nl-BE',
			dateFormat : 'dd/mm/yy'
		});

		// date picker future only
		$(".future-only").datepicker("option", "minDate", "+1");
		$("#ADR_FROMDATE").click(function(e) {
			$("#active-from-future").trigger("click");
		});

		// subscriber form validation
		$("form.validate").validate();

		// person details form
		$(".is_business_radio").change(function(e) {
			var isBusiness = $('input:radio[name=is_business]:checked').val();
			if (isBusiness == "Y") {
				$(".business-field").slideDown();
			} else {
				$(".business-field").slideUp();
			}
		});

		// hide on load
		var isBusiness = $('input:radio[name=is_business]:checked').val();
		if (isBusiness == "Y") {
			$(".business-field").show();
		} else {
			$(".business-field").hide();
		}
		
		// Disable form on submission
		$('form').submit(function(){
			if($(this).hasClass("validate")){
				if($(this).valid()){
					// only overlay if valid
					showLoading();
				}
			}else{
				showLoading();
			}
		    
			return true;
		});

	}); // end document ready

function showLoading(){
	$('#loadingOverlay').show();
	$('#loadingOverlay .window').vAlign();
}

// Step back complaint procedure
function complaintStepBack() {
	$(".current-question input").attr('disabled', 'disabled');
	$(".current-question select").attr('disabled', 'disabled');
	$(".current-question textarea").attr('disabled', 'disabled');

	$("#complaint").submit();
}

// Numeric only control handler
jQuery.fn.ForceNumericOnly = function() {
	return this.each(function() {

		$(this).keyup(function(e) {
			//var key = e.charCode || e.keyCode || 0;

			var input = $(this).val() + "";

			if (/^[0-9]*$/.test(input)) {
				// is valid
			} else {

				$(this).val(input.replace(/[^0-9]/ig, ""));
				$("#loginnumberinfo").trigger('mouseover');

			}
		});

//		 $(this).keypress(function(e)
//			 {
//			 var key = e.charCode;
//			  
//			 var origInput = $(this).val() + "";
//			 var input = origInput;
//			 if(key != 0){
//				 input = input + String.fromCharCode(key);
//			 }
//			 
//			 if(origInput.length != input.length){
//			 
//			 if (/^[0-9]*$/.test(input)){
//			 //is valid
//			 }else{
//			        		
//				 
//				 e.preventDefault();
//				 $(this).val(input.replace(/[^0-9]/ig, ""));
//					        		
//				 $("#loginnumberinfo").trigger('mouseover');
//				        		
//				 
//			        		
//			 }
//			 
//			 }
//			 
//			 });

		})
};

