$(document).ready(function() {
	
	$("#profileSearchFormSubmit").click(function(){
		validateProfileSearchForm();
		return false;
	});
	
	$("#profileSearchForm").submit(function(){
		validateProfileSearchForm();
		return false;
	});

	$("#profileModifyFormSubmit").click(function(){
		validateProfileModifyForm();
		return false;
	});
	
	$("#profileCommentFormSubmit").click(function(){
		validateProfileCommentForm();
		return false;
	});
	
	$("#profilePhotoModifyFormSubmit").click(function(){
		$("#profilePhotoModifyForm").submit();
		return false;
	});
	
	
	$("#profileYoutubeFormSubmit").click(function(){
		validateProfileYoutubeForm();
		return false;
	});
	

});

function validateProfileYoutubeForm() {

	var profileYoutubeForm = document.forms.profileYoutubeForm;
	var url = trim(profileYoutubeForm.url.value);
	var pattern = /^http:\/\/(.*)\.youtube\.com\/watch\?v=/;

	if (url.length > 0 && pattern.test(url)) {
		profileYoutubeForm.submit();
	} else {
		$("#error_profileYoutubeForm_url").addClass("error");
	}
}

function validateProfileSearchForm() {

	var profileSearchForm = document.forms.profileSearchForm;
	var name = trim(profileSearchForm.naam.value);

	if (name.length > 0) {
		window.location = "http://" + location.hostname + "/zappsporters/zoek?naam=" + escape(name);
	} else {
		// alert("Je bent vergeten een naam in te vullen.");
	}
}


function validateProfileModifyForm() {

	var profileModifyForm = document.forms.profileModifyForm;

	var name = trim(profileModifyForm.name.value);
	var birthdate = trim(profileModifyForm.birthdate.value);
	var description = profileModifyForm.description.value;
	var password = profileModifyForm.password.value;
	var password_again = profileModifyForm.password_again.value;
	
	var error = false;

	if (name == "") {
		$("#error_profileModifyForm_name").addClass("error");
		error = true;
	} else {
		$("#error_profileModifyForm_name").removeClass("error");
	}

	if (birthdate == "") {
		$("#error_profileModifyForm_birthdate").addClass("error");
		error = true;
	} else {
		$("#error_profileModifyForm_birthdate").removeClass("error");
	}

	if (description == "") {
		$("#error_profileModifyForm_description").addClass("error-4");
		error = true;
	} else {
		$("#error_profileModifyForm_description").removeClass("error-4");
	}
	
	if (password != "" && password != password_again) {
		$("#error_profileModifyForm_password").addClass("error");
		$("#error_profileModifyForm_password_again").addClass("error");
		error = true;
	} else {
		$("#error_profileModifyForm_password").removeClass("error");
		$("#error_profileModifyForm_password_again").removeClass("error");
	}

	if (error == false) {
		profileModifyForm.password.value = hex_md5(password);
		profileModifyForm.password_again.value = hex_md5(password_again);
		profileModifyForm.submit();
	}
}

function validateProfileCommentForm(){

	var profileCommentForm = document.forms.profileCommentForm;
	var description = profileCommentForm.description.value;	
	var error = false;
	
	if (description == "") {
		// alert("Je bent vergeten een bericht in te vullen.");
		error = true;
	}
	
	if (error == false) {
		profileCommentForm.submit();
	}
}
