// JavaScript Document
function processForm(option, clear) {
	if (option != 'Update_CMP') {
		if (document.getElementById('Result')) {
			document.getElementById('Result').style.display = 'none';
			document.getElementById('Result').innerHTML = '';
			document.getElementById('Result').className = '';
		}
		window.scrollTo(0, 0);
		if (document.getElementById('Animation')) {
			document.getElementById('Animation').style.display = 'block';
			if (document.getElementById('Submit_Paragraph')) {
				document.getElementById('Submit_Paragraph').style.display = 'none';
			}
			if($('#DropZone').length > 0){
				var myDropzone = Dropzone.forElement(".dropzone");
				if(myDropzone.getQueuedFiles().length > 0){
					myDropzone.processQueue();
				}else{
					setTimeout(function() {
						formProcessing(option, clear);
					}, 800);
				}
			}else{
				setTimeout(function() {
					formProcessing(option, clear);
				}, 800);
			}
		} else {
			if($('#DropZone').length > 0){
				var myDropzone = Dropzone.forElement(".dropzone");
				if(myDropzone.getQueuedFiles().length > 0){
					myDropzone.processQueue();
				}else{
					setTimeout(function() {
						formProcessing(option, clear);
					}, 800);
				}
			}else{
				setTimeout(function() {
					formProcessing(option, clear);
				}, 800);
			}
		}
	} else {
		if($('#DropZone').length > 0){
			var myDropzone = Dropzone.forElement(".dropzone");
			if(myDropzone.getQueuedFiles().length > 0){
				myDropzone.processQueue();
			}else{
				setTimeout(function() {
					formProcessing(option, clear);
				}, 800);
			}
		}else{
			setTimeout(function() {
				formProcessing(option, clear);
			}, 800);
		}
	}
}

function formProcessing(option, clear) {
	var destination = 'includes/ajax/processing-scripts/process-form.php';
	var data = new FormData();
	var now = new Date();
	data.append('Option', option);
	if (document.getElementById('Admin_Form')) {
		var fields = document.getElementById('Admin_Form').elements;
		var field_name = '';
		var field_value = '';
		var field_type = '';
		var field_choice = '';
		for (var i = 0; i < fields.length; i++) {
			field_name = fields[i].name;
			field_type = fields[i].type;		
			if(document.getElementById(field_name) || field_type == 'radio'){
				switch (field_type) {
					case 'text':					
						field_value = document.getElementById(field_name).value;
						data.append(field_name, field_value);
						break;
					case 'textarea':
						field_value = document.getElementById(field_name).value;
						data.append(field_name, field_value);
					break;
					case 'select-one':
						field_choice = document.getElementById(field_name).selectedIndex;
						field_value = document.getElementById(field_name).options[field_choice].value;
						data.append(field_name, field_value);
						break;
					case 'checkbox':
						if (document.getElementById(field_name).checked == true) {
							field_value = document.getElementById(field_name).value;
						} else {
							field_value = '';
						}
						data.append(field_name, field_value);
						break;
					case 'radio':
						var rdoBtns = document.getElementById('Admin_Form').elements[field_name];
						for (var k = 0; k < rdoBtns.length; k++) {
							if (rdoBtns[k].checked) {
								field_value = rdoBtns[k].value;
								break;
							}
						}
						data.append(field_name, field_value);
						break;
					case 'hidden':
						field_value = document.getElementById(field_name).value;
						data.append(field_name, field_value);
						break;
					case 'password':
						field_value = document.getElementById(field_name).value;
						data.append(field_name, field_value);
						break;
					case 'file':
						if(!($('#DropZone').length > 0)){
							if (document.getElementById(field_name)){
								if (document.getElementById(field_name).files.length > 0) {
									data.append(field_name, document.getElementById(field_name).files[0]);
								}
							}
						}
					break;
				}
			}
		}
	}
	data.append('Now', now);
	request = new XMLHttpRequest();
	request.open('POST', destination, true);
	request.send(data);
	request.onreadystatechange = function() {
		if (request.readyState == 4 && request.status == 200) {
			if (option != 'Update_CMP') {
				if (document.getElementById('Animation')) {
					document.getElementById('Animation').style.display = 'none';
					if (document.getElementById('Submit_Paragraph')) {
						document.getElementById('Submit_Paragraph').style.display = 'block';
					}
				}
				if (document.getElementById('Result')) {
					const result = JSON.parse(request.responseText);
					formEnder(result,option,clear);
				}
			}
		}
	};
}

function autoUpdate() {
	if ($('#PageContent')) {
		processForm('Update_CMP', 'No_Clear');
	} else if ($('#BlogContent')) {
		processForm('Edit_Blog_Post', 'No_Clear');
	}
}

function passwordField(fieldName){
	if(document.getElementById(fieldName)){
		let currentType = document.getElementById(fieldName).type;
		if(currentType == 'text'){
			document.getElementById(fieldName).type = 'password';
		}
		if(currentType == 'password'){
			document.getElementById(fieldName).type = 'text';
		}		
	}
}