function editProject(projectId) {
	var todo = "projects&todo=edit_project&id=" + projectId;

	getAjax(todo, editProjectReponse);
}

function editProjectReponse(response) {
//	console.log(response);
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		document.getElementById("project_main_container").innerHTML = responseArray.htmlData;
		initDatepicker();
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function editProjectTask(taskId, projectId) {
	var todo = "projects&todo=edit_project_task&task_id=" + taskId + "&project_id=" + projectId;
	getAjax(todo, editProjectTaskReponse);
}

function editProjectTaskReponse(response) {
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		document.getElementById("project_main_container").innerHTML = responseArray.htmlData;
		initDatepicker();
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function saveProjectTask() {
	var todo = "projects&todo=save_project_task";
	var postVar = '';
	
	postVar += "&task_id=" + document.getElementById("task_id").value;
	postVar += "&project_id=" + document.getElementById("project_id").value;
	postVar += "&name=" + document.getElementById("name").value;
	if(!document.getElementById("owner").disabled) {
		postVar += "&owner=" + document.getElementById("owner").options[document.getElementById("owner").selectedIndex].value;
	}
	postVar += "&end_date=" + document.getElementById("end_date").value;
	postVar += "&status=" + document.getElementById("status").options[document.getElementById("status").selectedIndex].value;
	postVar += "&description=" + encodeURIComponent(document.getElementById("taskDescription").innerHTML);
	postVar += "&reminder_active=";
	if(document.getElementById("reminder_active").checked) {
		postVar += "1"
	} else {
		postVar += "0"
	}

	postAjax(todo, postVar, saveProjectTaskReponse);
}

function saveProjectTaskReponse(response) {
	console.log(response);
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		editProject(responseArray.projectId);
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function filerProjectTasks() {
	var i, j, taskFilters, tasks, showAll;
	
	tasks = document.getElementsByClassName("projectTaskListRow");
	taskFilters = document.getElementsByClassName("taskFilters");
	
	if(tasks == undefined) {
		return false;
	}
	
	for(i=0; i < tasks.length; i++) {
		tasks[i].style.display = 'none';
	}
	
	for(i=0; i < taskFilters.length; i++) {
		if(!taskFilters[i].classList.contains('taskFilteronly')) {
			if(taskFilters[i].checked) {
				for(j=0; j < tasks.length; j++) {
					if(tasks[j].classList.contains(taskFilters[i].value)) {
						tasks[j].style.display = '';
					}
				}
			}
		}
	}
	
	for(i=0; i < taskFilters.length; i++) {
		if(taskFilters[i].classList.contains('taskFilteronly')) {
			if(taskFilters[i].checked) {
				for(j=0; j < tasks.length; j++) {
					if(!tasks[j].classList.contains(taskFilters[i].value)) {
						console.log('Going to hide as class ' + taskFilters[i].value + ' was not found ');
						tasks[j].style.display = 'none';
					}
				}
			}
		}
	}
}

function toggleEditTask() {
	var targetDiv = document.getElementById("taskDescription");
	
	if(targetDiv.contentEditable == "true") {
		targetDiv.contentEditable = "false";
		targetDiv.classList.add("border-color-green");
	} else {
		targetDiv.contentEditable = "true";
		targetDiv.classList.remove("border-color-green");
	}
}

function addLinkToDescription() {
	var targetDiv = document.getElementById("taskDescription");
	var linkName = document.getElementById("add_link_name");
	var linkURL = document.getElementById("add_link_url");
	
	var newLink = document.createElement('a');
	var linkText = document.createTextNode(document.getElementById("add_link_name").value);

	newLink.appendChild(linkText);
	newLink.title = linkName.value;
	var useUrl = linkURL.value;
	/*
	if(useUrl.indexOf("http") != 0) {
		useUrl = "https://" + useUrl;
	}
	*/
	newLink.href = useUrl;
	newLink.target = '_blank';
	
	targetDiv.appendChild(newLink);

	linkName.value = "";
	linkURL.value = "";
}

function deleteTask(taskId) {
	var todo = "projects&todo=delete_project_task&task_id=" + taskId;
	getAjax(todo, deleteTaskReponse);
}

function deleteTaskReponse(response) {
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		editProject(responseArray.projectId);
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function undoProjectTaskDelete() {
	var todo = "projects&todo=undo_project_task_delete";
	getAjax(todo, undoProjectTaskDeleteReponse);
}

function undoProjectTaskDeleteReponse(response) {
	console.log(response);
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		editProject(responseArray.projectId);
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

function testProjectReminderFunction() {
	var dateInput = document.getElementById('reminder_function_test_date');
	var todo = "projects&todo=test_project_reminder_function&date=" + dateInput.value;
	
	getAjax(todo, testProjectReminderFunctionReponse);
}

function testProjectReminderFunctionReponse(response) {
	console.log(response);
	try {
		var	responseArray = JSON.parse(response);
	} catch(err) {
		console.log(err + ', response ' + response);
	}

	if(responseArray.status) {
		document.getElementById('reminder_function_test_result_container').innerHTML = responseArray.htmlData;
	} else {
		popupMessage(responseArray.errorMessage);
	}
}

