// JavaScript Document
function processSearch(option, clear, pgNbr) {
	if (document.getElementById('Result')) {
		document.getElementById('Result').style.display = 'none';
		document.getElementById('Result').innerHTML = '';
		document.getElementById('Result').className = '';
	}
	window.scrollTo(0, 0);
	$('#RowContainer').fadeOut('slow', 0.0, function() {
		if (document.getElementById('Animation')) {
			document.getElementById('Animation').style.display = 'block';
			if (document.getElementById('Search_Submit_Paragraph')) {
				document.getElementById('Search_Submit_Paragraph').style.display = 'none';
			}
			setTimeout(function() {
				searchProcessing(option, clear, pgNbr);
			}, 800);
		} else {
			searchProcessing(option, clear, pgNbr);
		}
	});
}

function searchProcessing(option, clear, pgNbr) {
	if(pgNbr != '' && document.getElementById('PgNbr')){
		document.getElementById('PgNbr').value = pgNbr;
	}
	var cssClass = '';
	var msg = '';
	var status = '';
	var id = '';
	var destination = 'includes/ajax/processing-scripts/process-search.php';
	var data = new FormData();
	var now = new Date();
	data.append('Option', option);
	if (document.getElementById('Search_Form')) {
		var fields = document.getElementById('Search_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;
			switch (field_type) {
				case 'text':
					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':
					//nothing
					break;
				case 'hidden':
					field_value = document.getElementById(field_name).value;
					data.append(field_name, field_value);
					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 (document.getElementById('Animation')) {
				document.getElementById('Animation').style.display = 'none';
				if (document.getElementById('Search_Submit_Paragraph')) {
					document.getElementById('Search_Submit_Paragraph').style.display = 'block';
				}
			}
			if(document.getElementById('RowContainer')){
				document.getElementById('RowContainer').innerHTML = request.responseText;
				$('#RowContainer').fadeIn('slow');
				if (status == 1 && clear != 'No_Clear') {
					clearForm_Search();
				}
			}				
		}
	};
}

function clearForm_Search() {
	if (document.getElementById('Search_Form')) {
		var fields = document.getElementById('Search_Form').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 'select-one':
					$('#' + field_name).selectmenu('destroy');
					$('#' + field_name).prop('selectedIndex', 0);
					$('#' + field_name).selectmenu();
					break;
				case 'checkbox':
					if (document.getElementById(field_name).checked == true) {
						$('#' + field_name).prop('checked', false).checkboxradio('refresh');
					}
					break;
				case 'radio':
					break;
				case 'hidden':
					if (field_name != 'Id') {
						document.getElementById(field_name).value = '';
					}
					break;
			}
		}
	}
}
