//***** VALIDATION *****//
function removeSpaces(s) {
	var string = s;
	string = string.replace(/ /g, "");
	return string;
}

function validateInput(string, type, maxlength) {
	var isValid = true;

	// perform type-specific validation
	switch (type) {
		case "string":
			if (removeSpaces(string).length < 1 || removeSpaces(string) == null)
				isValid = false;
			break;
			
		case "email":
			var emailReg = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
			if (!string.match(emailReg))
				isValid = false;
			break;
			
		default:
			isValid = false;
	}
	
	// check if string is longer than maxlength
	if (string.length > maxlength)
		isValid = false;
		

	// return true or false
	if (isValid)
		return true;
	else
		return false;
}


$(function() {
	//format the FAQ if on that page
	if ($("body").hasClass("page-id-30")) {
		var menu = '<div class="anchor-menu"><ul>';
		$("#main h5").each(function(index) {
			$(this).attr("id", "faq-" + index);
			$(this).prepend('<span style="color:#00A5DD">Q: </span>');
			$(this).next("p").prepend('<strong style="color:#00A5DD">A: </strong>');
			menu += '<li><a href="#faq-' + index + '">' + $(this).html().replace("Q: ", "") + '</a></li>';
			$(this).append('<a href="#main" class="back-to-top">Back to top</a>');
		});
		menu += '</ul></div>';
		
		$("#main h2:first").after(menu);
	}
	
	$('.anchor-menu a[href*=#], a.back-to-top').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});
});
