$(document).ready(function() {
	
	$('#activate_profile').jqm({modal: true, trigger: 'a.open_profile_activation'});
	
	$("#show_profile_form").click(function () {
	    if ($(this).attr("checked")) {
			//do the stuff that you would do when 'checked'
			$("#activate_profile_form").show();
			return;
		} else {
			$("#activate_profile_form").hide();
			return;
		}
	});
	
	var options = { 
		//target:			'#return_input_profile',   // target element(s) to be updated with server response 
        beforeSubmit:	showRequest,  // pre-submit callback 
        success:		showResponse  // post-submit callback 
    };
	
	$("#form_activate_profile").validate({
		rules: {
			profile_url: {
		      required: true,
			  minlength: 3,
		      remote: {
		        url: "/account/manage-pub-profile/",
		        type: "post",
		        data: {
		          profile_url: function() {
		            return $("#profile_url").val();
		          },
				  validate: true
				}
		      }
		    }
		},
		messages: {
			profile_url: {
				 minlength: "Minimaal 3 tekens...",
				 required: "Vul een url in...",
				 remote: "Deze profiellink is al bezet"
			}
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit(options); 
 		}

	});
	
	var options_renewPassword = { 
        beforeSubmit:	showRequestPassword,  // pre-submit callback 
        success:		showResponsePassword  // post-submit callback 
    };
	
	$("#form_renew_password").validate({
		rules: {
			username_input: {
			  minlength: 3,
			  required: true
		    }
		},
		messages: {
			username_input: {
				 minlength: "Minimaal 3 tekens...",
				 required: "Vul uw gebruikersnaam in"
			}
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit(options_renewPassword); 
 		}

	});

});

function downloadPid(pid_hash) {

	setTimeout("loadDownloadPidDoc('"+pid_hash+"')",100);
	setTimeout("updatePidList()",500);
	
}

function loadDownloadPidDoc(pid_hash) {

	window.location = "/download/?pid="+pid_hash;
	
	
}

function updatePidList() {

	$.get('/account/downloads/?ajax=true', function(data) {
		$('#download_list').html(data);
	});
	
}

// post-submit callback 
function showRequest(responseText, statusText, xhr, $form)  { 
	
	$("#return_input_profile").html("<center><br/><br/><br/><br/><br/><br/><img src=\"/resources/images/small_loader.gif\" /><br/><br/>Uw profielinstellingen worden opgeslagen.</center>");
 
}

function showResponse(responseText, statusText, xhr, $form)  {
	
	var serverResp = JSON.parse(responseText);

	$("#return_input_profile").html(serverResp.popup_respons);
	$("span#public_profile_status").html(serverResp.page_response);
	
	$('#activate_profile').jqm({modal: true, trigger: 'a.open_profile_activation'});
	
	return true;
 
}

function showRequestPassword(responseText, statusText, xhr, $form)  { 
	
	$("#return_input_profile").html("<center><br/><br/><br/><br/><br/><br/><img src=\"/resources/images/small_loader.gif\" /><br/><br/>Er word een nieuw wachtwoord aangemaakt.</center>");
 
}
function showResponsePassword(responseText, statusText, xhr, $form)  {
	
	$("#return_renewPassword").html("Er is een nieuw wachtwoord verstuurd naar je e-mailadres.");
	
	return true;
 
}

 
