// JavaScript Document
function processPopUpForm(option, formName) {
	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('AnimationAlt')) {
		if (document.getElementById('AnimationPopUp')) {
			document.getElementById('AnimationPopUp').style.display = 'block';
		} else if (document.getElementById('AnimationAlt')) {
			document.getElementById('AnimationAlt').style.display = 'block';
		} else {
			document.getElementById('Animation').style.display = 'block';
		}
		if (document.getElementById('Submit_Paragraph')) {
			document.getElementById('Submit_Paragraph').style.display = 'none';
		}
		setTimeout(function() {
			popUpProcessing(option, formName);
		}, 800);
	} else {
		popUpProcessing(option, formName);
	}
}

function popUpProcessing(option, formName) {
	var cssClass = '';
	var msg = '';
	var status = '';
	var albumId = '';
	var id = '';
	var destination = 'includes/ajax/processing-scripts/popup/process-popup.php';
	var data = new FormData();
	var now = new Date();
	data.append('Option', option);
	if (document.getElementById(formName)) {
		var fields = document.getElementById(formName).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;
			switch (field_type) {
				case 'text':
					field_value = document.forms[formName].elements[field_name].value;
					data.append(field_name, field_value);
					break;
				case 'hidden':
					field_value = document.forms[formName].elements[field_name].value;
					data.append(field_name, field_value);
					break;
				case 'textarea':
					field_value = document.forms[formName].elements[field_name].value;
					data.append(field_name, field_value);
					break;
				case 'radio':
					var rdoBtns = document.forms[formName].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 'select-one':
					field_choice = document.forms[formName].elements[field_name].selectedIndex;
					field_value = document.forms[formName].elements[field_name].options[field_choice].value;
					data.append(field_name, field_value);
					break;
				case 'file':
					if (document.forms[formName].elements[field_name].files.length > 0) {
						data.append(field_name, document.forms[formName].elements[field_name].files[0]);
					}
					break;
			}
		}
	}
	data.append('Now', now);
	console.log(data);
	request = new XMLHttpRequest();
	request.open('POST', destination, true);
	request.send(data);
	request.onreadystatechange = function() {
		if (request.readyState == 4 && request.status == 200) {
			if (document.getElementById('AnimationPopUp')) {
				document.getElementById('AnimationPopUp').style.display = 'none';
			} else if (document.getElementById('AnimationAlt')) {
				document.getElementById('AnimationAlt').style.display = 'none';
			} else {
				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')) {
				if (window.DOMParser) {
					parser = new DOMParser();
					xmlDoc = parser.parseFromString(request.responseText, 'text/xml');
				} else {
					// Internet Explorer
					xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
					xmlDoc.async = false;
					xmlDoc.loadXML(request.responseText);
				}
				cssClass = xmlDoc.getElementsByTagName('Class')[0].childNodes[0].nodeValue;
				msg = xmlDoc.getElementsByTagName('Msg')[0].childNodes[0].nodeValue;
				status = xmlDoc.getElementsByTagName('Status')[0].childNodes[0].nodeValue;
				id = xmlDoc.getElementsByTagName('Id')[0].childNodes[0].nodeValue;
				albumId = xmlDoc.getElementsByTagName('AlbumId')[0].childNodes[0].nodeValue;
				albumName = xmlDoc.getElementsByTagName('AlbumName')[0].childNodes[0].nodeValue;
				document.getElementById('Result').className = cssClass;
				document.getElementById('Result').innerHTML = msg;
				document.getElementById('Result').style.display = 'block';
				switch (option) {
					default:
						if (status == 1) {
							clearPopUpForm('PopUp_Form');
							//function for upating form info if necessary
							$('#PopUp').popUpWindow({
								action: 'close'
							});
						} else {
							document.getElementById('Result').className = cssClass;
							document.getElementById('Result').innerHTML = msg;
							document.getElementById('Result').style.display = 'block';
						}
						break;
				}
			}
		}
	};
}

function clearPopUpForm(formName) {
	if (document.getElementById(formName)) {
		var fields = document.getElementById(formName).elements;
		var field_name = '';
		var field_type = '';
		var field_choice = '';
		for (var i = 0; i < fields.length; i++) {
			field_name = fields[i].name;
			field_type = fields[i].type;
			switch (field_type) {
				case 'text':
					document.getElementById(field_name).value = '';
					break;
				case 'textarea':
					if ($('.trumbowyg-editor')) {
						$('#' + field_name).trumbowyg('empty');
					} else {
						$('#' + field_name).attr('value', '');
					}
					break;
				case 'select-one':
					if ($('#' + field_name).val() == 'New') {
						$('#' + field_name).prop('selectedIndex', 0);
					}
					break;
				case 'file':
					document.getElementById(field_name).value = '';
					var fieldLabel = document.getElementsByClassName('fileInputLabel');
					fieldLabel[0].innerHTML = 'PHOTOS/IMAGES';
					fieldLabel[0].style.backgroundColor = '#c42503';
					break;
			}
		}
	}
}