var current_category = "public";
var current_directory = "index";
var current_document_id = "index";
var current_username = "";
var cache_id = "";
var cache_target = "";

// PANEL ACTION 
function login() 
{
	open_loader('verifying input, please wait');
	var username = $("input[name=login_username]").val(); 
	var password = $("input[name=login_password]").val(); 
	var code = $("input[name=code]").val();
	
	$.ajax({type:"POST", url:"ajaxActionHandler.php", 
	data: ({action:"login", username:username, password:password, code:code}), 
	success: function(data) {$('#log').append(data); close_loader();}}); 
} // end login() 

function clear_forms_login()
{$("input[name=login_username]").val(''); $("input[name=login_password]").val('');}


function new_user() 
{
	open_loader('verifying input, please wait');
	var username = $("input[name=signup_username]").val(); 
	var email = $("input[name=signup_email]").val(); 
	var cemail = $("input[name=signup_cemail]").val(); 
	var password = $("input[name=signup_password]").val();
	var cpassword = $("input[name=signup_cpassword]").val(); 
	var captcha_c = $("input[name=recaptcha_challenge_field]").val();
	var captcha_r = $("#recaptcha_response_field").val();	
	if( $("input[name=signup_terms]").is(':checked')==true) {terms=1;} else {terms=0;} 
	
	$.ajax({type:"POST", url:"ajaxActionHandler.php", 
	data: ({action: "new_user", username:username, email:email, cemail:cemail, password:password, cpassword:cpassword, recaptcha_challenge_field:captcha_c, recaptcha_response_field:captcha_r, terms:terms}), 
	success: function(data) {$('#log').append(data); close_loader();}}); 
} // end new_user() 

function clear_forms_new_user() 
{$("input[name=username]").val(''); $("input[name=email]").val(''); $("input[name=cemail]").val(''); $("input[name=password]").val(''); $("input[name=cpassword]").val(''); $("#recaptcha_response_field").val(''); $("input[name=signup_terms]").attr('checked', false);}


function edit_profile(sessionUsername) 
{
	open_loader('verifying input, please wait');	
	var email = $("input[name=profile_email]").val();
	var cemail = $("input[name=profile_cemail]").val();
	var bio = $("[name=profile_bio]").html(); 
	var oldpassword = $("input[name=profile_oldpassword]").val(); 
	var password = $("input[name=profile_newpassword]").val(); 
	var cpassword = $("input[name=profile_cpassword]").val();	
	$.ajax({type: "POST", url: "ajaxActionHandler.php", 
	data: ({action:"edit_profile", email:email, cemail:cemail, bio:bio, oldpassword:oldpassword, password:password, cpassword:cpassword, username:sessionUsername}), 
	success: function(data) {$('#log').html(data); display(); close_loader();}}); 
} // end documentdb_editDocument()

function fetch_edit_profile(sessionUsername) 
{
	open_loader('fetching data, please wait');	
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_user_email", username: sessionUsername}), 
	success: function(data) {$("input[name=profile_email]").val(data); $("input[name=profile_cemail]").val(data);}});
	
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_user_bio", username: sessionUsername}), 
	success: function(data) {$("[name=profile_bio]").html(data); show_editor('#editor-profile');}}); 
	close_loader();
} // end fetch_directorydb_editDirectory(id, sessionUsername) 

function clear_forms_profile()
{$("input[name=profile_email]").val(''); $("input[name=profile_cemail]").val(''); $("[name=profile_bio]").html(''); $("input[name=profile_oldpassword]").val(''); $("input[name=profile_newpassword]").val(''); $("input[name=profile_cpassword]").val('');}

function fetch_profile_bio() 
{
	$.ajax({type:"POST", url:"ajaxFetchHandler.php", 
	data: ({fetch:"fetch_profile_bio", username:current_username}), 
	success: function(data) {$('#bio_container').html(data);}}); 
} // end fetch_profile_bio()

function new_directory(sessionUsername) 
{
	open_loader('verifying input, please wait');	
	var title = $("input[name=directory_title]").val(); 
	var category = $("[name=directory_category]:radio:checked").val(); //radio
	var comments = $("[name=directory_comments]:radio:checked").val(); //radio 
	$.ajax({type:"POST", url:"ajaxActionHandler.php", 
	data: ({action:"new_directory", title:title, category:category, comments:comments, username:sessionUsername}), 
	success: function(data) {$('#log').html(data); display(); close_loader();}}); 
} // end new_directory(sessionUsername) 

function edit_directory(id, sessionUsername) 
{
	open_loader('verifying input, please wait');
	var title = $("input[name=directory_title]").val(); 
	var category = $("[name=directory_category]:radio:checked").val(); //radio 
	var comments = $("[name=directory_comments]:radio:checked").val(); //radio 
	$.ajax({type:"POST", url:"ajaxActionHandler.php", 
	data: ({action:"edit_directory", id:id, title:title, category:category, comments:comments, username:sessionUsername}), 
	success: function(data) {$('#log').html(data); display(); close_loader();}}); 
} // end edit_directory(id, sessionUsername)   

function remove_directory(id, sessionUsername)
{
	var confirm_removal = confirm("All documents in this directory will also purged. Are you sure you want to delete this directory? ");
	if (confirm_removal==true)
	{
		open_loader('removing directory');	
		$.ajax({type: "POST", url: "ajaxActionHandler.php", 
		data: ({action: "remove_directory", id: id, username: sessionUsername}), 
		success: function(data) {$('#log').html(data); display(); close_loader();}}); 
	}
}

function fetch_edit_directory(id, sessionUsername) 
{
	open_loader('fetching data, please wait');	
	cache_id=id; cache_target='editor_directory';
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_directory_title", id: id, username: sessionUsername}), 
	success: function(data) {$("input[name=directory_title]").val(data);}}); 	
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_directory_category", id: id, username: sessionUsername}), 
	success: function(data) {$('[value='+data+']').attr('checked', true);}});
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_directory_comments", id: id, username: sessionUsername}), 
	success: function(data) {$('[value='+data+']').attr('checked', true); show_editor('#editor-directory');}});
	close_loader();  
} // end fetch_directorydb_editDirectory(id, sessionUsername)

function clear_forms_directory()
{$("input[name='directory_title']").val(''); $("input[name=directory_category]").attr("checked", false); $("input[name=directory_comments]").attr("checked", false);}


function new_document(sessionUsername) 
{
	open_loader('verifying input, please wait'); 
	var title = $("input[name=document_title]").val();
	var category = $("input[name=document_category]:radio:checked").val();//radio 
	var directory = $("[name=document_directory]").val();//select
	var content = $("[name=document_content]").html();
	$.ajax({type:"POST", url:"ajaxActionHandler.php", 
	data: ({action:"new_document", title:title, category:category, directory:directory, content:content, username:sessionUsername}), 
	success: function(data) {$('#log').html(data); display(); close_loader();}}); 
} // end documentdb_newDocument()

function edit_document(id, sessionUsername) 
{
	open_loader('verifying input, please wait');	
	var title = $("input[name=document_title]").val();
	var category = $("input[name=document_category]:radio:checked").val(); //radio
	var directory = $("[name=document_directory]").val(); //select
	var content = $("[name=document_content]").html();
	$.ajax({type: "POST", url: "ajaxActionHandler.php", 
	data: ({action: "edit_document", id: id, title: title, category: category, directory: directory, content:content, username: sessionUsername}), 
	success: function(data) {$('#log').html(data); display(); close_loader(); }});  
} // end documentdb_editDocument()

function fetch_edit_document(id, sessionUsername) 
{
	open_loader('fetching data, please wait');
	cache_id=id; cache_target='editor_document';
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_document_category", id: id, username: sessionUsername}), 
	success: function(data) {fetch_editor_directory_list(data); $('[value='+data+']').attr('checked', true);
	
		$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_document_directory", id: id, username: sessionUsername}), 
		success: function(data) {$('[value='+data+']').attr("selected", "selected"); }}); 
	
	}}); 
		
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_document_title", id: id, username: sessionUsername}), 
	success: function(data) {$("input[name=document_title]").val(data);}}); 
	
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_edit_document_content", id: id, username: sessionUsername}), 
	success: function(data) {$("[name='document_content']").html(data); show_editor('#editor-document');}}); 	
	
	close_loader();
} // end fetch_directorydb_editDirectory(id, sessionUsername) 

function clear_forms_document()
{$("input[name=document_title]").val(''); $("input[name=document_category]:radio").attr("checked", false); $("[name=document_content]").html(''); $('[name=document_directory]').html('');}

function remove_document(id, sessionUsername)
{
	var confirm_removal = confirm("Are you sure you want to remove this document?");
	if (confirm_removal==true)
	{
		open_loader('removing document');	
		$.ajax({type: "POST", url: "ajaxActionHandler.php", 
		data: ({action: "remove_document", id:id, username:sessionUsername}), 
		success: function(data) {$('#log').html(data); display(); close_loader();}}); 
	}
}

function fetch_preview_document(id, sessionUsername) 
{
	open_loader('fetching document, please wait');
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", 
	data: ({fetch:"fetch_preview_document", id:id, username:sessionUsername}), 
	success: function(data) {$('#viewer-document-content').html(data); 
	close_loader(); show_viewer();}}); 
} // end fetch_preview_document(sessionUsername) 

//FETCH
function display()
{fetch_profile_bio(); fetch_category_num_public(); fetch_category_num_personal(); fetch_directory_num(); fetch_directory_list(); fetch_document_list(); fetch_document();}


function clear_all_forms_editor()
{clear_forms_profile(); clear_forms_directory(); clear_forms_document();}

function fetch_category_num_public()
{$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_category_num_public", username: current_username}), success: function(data) {$("#category_num_public").html(data);}});}

function fetch_category_num_personal()
{$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_category_num_personal", username: current_username}), success: function(data) {$("#category_num_personal").html(data);}});}

function fetch_directory_num()
{$.ajax({type: "POST", url: "ajaxFetchHandler.php", data: ({fetch: "fetch_directory_num", category: current_category, username: current_username}), success: function(data) {$("#directory_num").html(data);}});}

 
function fetch_profile_bio() // display directory list on main index.php page 
{
	open_loader('fetching profile bio, please wait');
	$.ajax({type:"POST", url:"ajaxFetchHandler.php", 
	data: ({fetch:"fetch_profile_bio", username:current_username}), 
	success: function(data) {$('#bio_container').html(data); close_loader();}}); 
} // end directorydb_fetchDirectoryList() 

function fetch_directory_list() // display directory list on main index.php page 
{
	open_loader('fetching directory listing, please wait');
	$.ajax({type:"POST", url:"ajaxFetchHandler.php", 
	data: ({fetch:"fetch_directory_list", category:current_category, username:current_username}), 
	success: function(data) {$('#directorylist_container').html(data); fetch_directory_num(); close_loader();}}); 
} // end directorydb_fetchDirectoryList() 

function fetch_document_list() 
{
	open_loader('fetching document listing, please wait');
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", 
	data: ({fetch: "fetch_document_list", category:current_category, directory:current_directory, username:current_username}), 
	success: function(data) {$('#documentlist_container').html(data); close_loader();}}); 
} // end directorydb_fetchDirectoryList() 

function fetch_document() 
{
	open_loader('fetching document, please wait');
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", 
	data: ({fetch: "fetch_document", category:current_category, directory:current_directory, document_id:current_document_id, username:current_username}), 
	success: function(data) {$('#document_container').html(data); close_loader();}}); 
} // end fetch_document()  

//FETCHEDITOR
function display_editor_data()
{fetch_editor_category_select();}

function fetch_editor_category_select() //directory_category 
{
	$('[name=directory_category],[value='+current_category+']').attr("selected", "selected");
} // end fetch_editor_category_select() 

function fetch_editor_directory_list(category) 
{
	open_loader('fetching directory listing, please wait');
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", 
	data: ({fetchEditor:"fetch_editor_directory_list", category:category, username:current_username}), 
	success: function(data) {$('[name=document_directory]').html(data); close_loader(); }}); 
} // end directorydb_fetchDirectoryList() 


function update()
{
	fetch_category_num_public(); fetch_category_num_personal(); fetch_directory_num();
	
	$.ajax({type:"POST", url:"ajaxFetchHandler.php", 
	data: ({fetch:"fetch_profile_bio", username:current_username}), 
	success: function(data) {$('#bio_container').html(data);}}); 
	
	$.ajax({type:"POST", url:"ajaxFetchHandler.php", 
	data: ({fetch:"fetch_directory_list", category:current_category, username:current_username}), 
	success: function(data) {$('#directorylist_container').html(data); fetch_directory_num();}}); 

	$.ajax({type: "POST", url: "ajaxFetchHandler.php", 
	data: ({fetch: "fetch_document_list", category:current_category, directory:current_directory, username:current_username}), 
	success: function(data) {$('#documentlist_container').html(data);}}); 
	
	$.ajax({type: "POST", url: "ajaxFetchHandler.php", 
	data: ({fetch: "fetch_document", category:current_category, directory:current_directory, document_id:current_document_id, username:current_username}), 
	success: function(data) {$('#document_container').html(data);}}); 
	
}
	
function new_directory_comment(username,author,directoryid) 
{
	var content = $("[name=directory_comment_content]").html();
	$.ajax({type:"POST", url:"ajaxActionHandler.php", 
	data: ({action:"new_directory_comment", author:author, directoryid:directoryid, content:content, username:username}), 
	success: function(data) {$('#log').html(data); $("[name=directory_comment_content]").html('');}}); 
} // end new_directory_comment()

function directory_comment_update(username,directoryid)
{
	$.ajax({type:"POST", url:"ajaxFetchHandler.php", 
	data: ({fetch:"fetch_directory_comment", username:username, directoryid:directoryid}), 
	success: function(data) {$('#comments_container').html(data);}}); 
}

function remove_directory_comment(id,username)
{
	var confirm_removal = confirm("Are you sure you want to remove this comment? ");
	if (confirm_removal==true)
	{
		$.ajax({type: "POST", url: "ajaxActionHandler.php", 
		data: ({action: "remove_directory_comment", id:id, username:username}), 
		success: function(data) {$('#log').html(data);}}); 
	}
}