var DINING_GET_RESTUARANT_DETAILS_URL = '/dining/admin/get-restaurant';
var DINING_PRIMARY_FILTER = 0;
var DINING_SECONDARY_FILTER = 0;

function ModuleDiningSubmitFilters() { // <<<(

	var primaryFilters = new Array();
	var secondaryFilters = new Array();
	var select = null;
	var request = '';

	// get primary filters
	select = document.getElementById('module-dining-primary-filter');

	// loop until all selected options have been processed; selectedIndex will only return the first index that is selected
	while (-1 != select.selectedIndex) {
		
		// save selected option
		primaryFilters.push(select.options[select.selectedIndex].value);

		// deselect option to allow for processing next selected object
		select.options[select.selectedIndex].selected = false;
	}

	// get secondary filters
	select = document.getElementById('module-dining-secondary-filter');

	// loop until all selected options have been processed; selectedIndex will only return the first index that is selected
	while (-1 != select.selectedIndex) {
		
		// save selected option
		secondaryFilters.push(select.options[select.selectedIndex].value);

		// deselect option to allow for processing next selected object
		select.options[select.selectedIndex].selected = false;
	}

	// retrieve pathname of request, ignore query string
	request = window.location.pathname.toString();

	// build request
	request += '?primary-filter=' + primaryFilters.toString() + '&secondary-filter=' + secondaryFilters.toString() + '#module-dining-results';

	// redirect to filtered dining page
	window.location.href = request;

	return;
} // )>>>

function ModuleDiningPrimarySelectAll(isSelected) { // <<<(

	var select = null;

	// sanitize input
	if (isSelected) {
		isSelected = true;
	}
	else {
		isSelected = false;
	}

	// get primary filters
	select = document.getElementById('module-dining-primary-filter');

	// loop through all options and set selected based on 'isSelected' input
	for (var i = 0; select.options.length > i; i++) {
		select.options[i].selected = isSelected;
	}

	// update text of button
	if (null != document.getElementById('module-dining-primary-filter-select-all')) {

		if (true == isSelected) {
			document.getElementById('module-dining-primary-filter-select-all').value = 'Select None';
			document.getElementById('module-dining-primary-filter-select-all').onclick = function() {
				ModuleDiningPrimarySelectAll(false);
			};
		}
		else {
			document.getElementById('module-dining-primary-filter-select-all').value = 'Select All';
			document.getElementById('module-dining-primary-filter-select-all').onclick = function() {
				ModuleDiningPrimarySelectAll(true);
			};
		}
	}

	return;
} // )>>>

function ModuleDiningSecondarySelectAll(isSelected) { // <<<(

	var select = null;

	// sanitize input
	if (isSelected) {
		isSelected = true;
	}
	else {
		isSelected = false;
	}

	// get secondary filters
	select = document.getElementById('module-dining-secondary-filter');

	// loop through all options and set selected based on 'isSelected' input
	for (var i = 0; select.options.length > i; i++) {
		select.options[i].selected = isSelected;
	}

	// update text of button
	if (null != document.getElementById('module-dining-secondary-filter-select-all')) {

		if (true == isSelected) {
			document.getElementById('module-dining-secondary-filter-select-all').value = 'Select None';
			document.getElementById('module-dining-secondary-filter-select-all').onclick = function() {
				ModuleDiningSecondarySelectAll(false);
			};
		}
		else {
			document.getElementById('module-dining-secondary-filter-select-all').value = 'Select All';
			document.getElementById('module-dining-secondary-filter-select-all').onclick = function() {
				ModuleDiningSecondarySelectAll(true);
			};
		}
	}

	return;
} // )>>>

function ModuleDiningUpdateSelectText() { // <<<(

	var select = null;
	var allSelected = false;
	var index = 0;

	if (null != document.getElementById('module-dining-primary-filter')) {

		// get primary filter
		select = document.getElementById('module-dining-primary-filter');

		// update loop flags
		allSelected = true;
		index = 0;

		// determine if all primary filters are selected
		while ((select.options.length > index) && (true == allSelected)) {

			if (false == select.options[index].selected) {
				allSelected = false;
			}

			index++;
		}

		// check if all options were selected
		if ((true == allSelected) && (null != document.getElementById('module-dining-primary-filter-select-all'))) {
			document.getElementById('module-dining-primary-filter-select-all').innerHTML = 'Select: None';
			document.getElementById('module-dining-primary-filter-select-all').onclick = function() {
				ModuleDiningPrimarySelectAll(false);
			};
		}
	}

	if (null != document.getElementById('module-dining-secondary-filter')) {

		// get secondary filter
		select = document.getElementById('module-dining-secondary-filter');

		// update loop flags
		allSelected = true;
		index = 0;

		// determine if all secondary filters are selected
		while ((select.options.length > index) && (true == allSelected)) {

			if (false == select.options[index].selected) {
				allSelected = false;
			}

			index++;
		}

		// check if all options were selected
		if ((true == allSelected) && (null != document.getElementById('module-dining-secondary-filter-select-all'))) {
			document.getElementById('module-dining-secondary-filter-select-all').innerHTML = 'Select: None';
			document.getElementById('module-dining-secondary-filter-select-all').onclick = function() {
				ModuleDiningSecondarySelectAll(false);
			};
		}
	}

	select = null;

	return;
} // )>>>

function ModuleDiningValidateRestaurant(action) { // <<<(
    var problemMessage = 'Please provide data for the following fields: ';
    var invalidFields = '';
    var isValid = true;

    if ('' == document.getElementById('module-dining-admin-' + action + '-restaurant-name').value) {
        isValid = false;
        invalidFields += 'name';
    }
    if ('' == document.getElementById('module-dining-admin-' + action + '-restaurant-address').value) {
        isValid = false;
        if ('' != invalidFields) {
            invalidFields += ', ';
        } 
        invalidFields += 'address';
    }
    if ('' == document.getElementById('module-dining-admin-' + action + '-restaurant-phone').value) {
        isValid = false;
        if ('' != invalidFields) {
            invalidFields += ', ';
        } 
        invalidFields += 'phone';
    }
    if ('-1' == document.getElementById('module-dining-admin-' + action + '-primary-filter').selectedIndex) {
        isValid = false;
        if ('' != invalidFields) {
            invalidFields += ', ';
        } 
        invalidFields += 'restaurant type';
    }
    if ('-1' == document.getElementById('module-dining-admin-' + action + '-secondary-filter').selectedIndex) {
        isValid = false;
        if ('' != invalidFields) {
            invalidFields += ', ';
        } 
        invalidFields += 'location';
    }


    if (false == isValid) {
        alert(problemMessage + invalidFields + '.');
    }

    return isValid; 
} // )>>>

function ModuleAdminConfirmDeleteRestaurant() { // <<<(
    var confirmed = true;
    if (null == document.getElementById('module-dining-admin-delete-restaurant') 
            || -1 == document.getElementById('module-dining-admin-delete-restaurant').value) {
    } else {
        confirmed = confirm('Are you sure you wish to delete this restaurant?\nThis operation cannot be undone.\n');
    }
    return confirmed;
} // )>>>

function ModuleDiningAdminSelectRestaurant(restaurantId) { // <<<(

	// get new request
    var xmlRequest = GetXmlRequest();
	var requestUrl = DINING_GET_RESTUARANT_DETAILS_URL + '?restaurant-id=' + restaurantId;

    // query the server
    xmlRequest.open('GET', requestUrl, false );
    xmlRequest.send(null);
    
    // get document element
    if (null != xmlRequest.responseXML) {
        response = xmlRequest.responseXML.documentElement;

		// process the response
		if ((null != response.getElementsByTagName('message')) &&
			('success' == response.getElementsByTagName('message')[0].getAttribute('type'))) {
			ModuleDiningAdminPopulateRestaurantDetails(response);
		} else {
			// failure
			alert("Sorry, the data for the requested restaurant could\nnot be retrieved from the database.\nPlease refresh the page and try again.");
		}
    }

    return;
} // )>>>

function ModuleDiningAdminPopulateRestaurantDetails(response) { // <<<(

	var name = '';
	var address = '';
	var restaurantId = response.getElementsByTagName('restaurant')[0].getAttribute('id');
	var phone = '';
	var website = '';
	var tags = new Array();
	var primaryTagIds = new Array();
	var secondaryTagIds = new Array();
	var found = false;

	// get name
	if ((0 < response.getElementsByTagName('name').length) &&
		(true == response.getElementsByTagName('name')[0].hasChildNodes()))	{
		name = response.getElementsByTagName('name')[0].childNodes[0].nodeValue;
	}

	// get address
	if ((0 < response.getElementsByTagName('address').length) &&
		(true == response.getElementsByTagName('address')[0].hasChildNodes())) {
		address = response.getElementsByTagName('address')[0].childNodes[0].nodeValue;
	}

	// get phone
	if ((0 < response.getElementsByTagName('phone').length) &&
		(true == response.getElementsByTagName('phone')[0].hasChildNodes())) {
		phone = response.getElementsByTagName('phone')[0].childNodes[0].nodeValue;
	}

	// get website
	if ((0 < response.getElementsByTagName('website').length) &&
		(true == response.getElementsByTagName('website')[0].hasChildNodes())) {
		website = response.getElementsByTagName('website')[0].childNodes[0].nodeValue;
	}

	// get tags
	tags = response.getElementsByTagName('tag');
	
	// separate tags based on filter type
	for (var i = 0; tags.length > i; i++) {

		// check filter type
		if (DINING_PRIMARY_FILTER == tags[i].getAttribute('tag-type-id')) {

			primaryTagIds.push(tags[i].getAttribute('tag-id'));
		}
		else if (DINING_SECONDARY_FILTER == tags[i].getAttribute('tag-type-id')) {

			secondaryTagIds.push(tags[i].getAttribute('tag-id'));
		}
		else {
			// nothing to do
		}
	}

	// set name
	if (null != document.getElementById('module-dining-admin-edit-restaurant-name')) {
		document.getElementById('module-dining-admin-edit-restaurant-name').value = name;
	}

	// set address
	if (null != document.getElementById('module-dining-admin-edit-restaurant-address')) {
		document.getElementById('module-dining-admin-edit-restaurant-address').value = address;
	}

	// set phone
	if (null != document.getElementById('module-dining-admin-edit-restaurant-phone')) {
		document.getElementById('module-dining-admin-edit-restaurant-phone').value = phone;
	}

	// set website
	if (null != document.getElementById('module-dining-admin-edit-restaurant-website')) {
		document.getElementById('module-dining-admin-edit-restaurant-website').value = website;
	}

	// set primary tag selections
	if (null != document.getElementById('module-dining-admin-edit-primary-filter')) {

		var select = document.getElementById('module-dining-admin-edit-primary-filter');

		// loop through each options until end of list or all tags have been used
		for (var i = 0; (select.options.length > i) && (primaryTagIds.length > 0); i++) {
		
			found = false;
		
			// loop through primary tag IDs to check for selection
			for (var j = 0; primaryTagIds.length > j; j++) {
				
				// check if this filter is selected
				if (primaryTagIds[j] == select.options[i].value) {

					// set selected flag and remove tag from list
					select.options[i].selected = true;
					primaryTagIds.splice(j, 1);
					found = true;
				}
			}

			if (false == found) {
				select.options[i].selected = false;
			}
		}
	}

	// set secondary tag selections
	if (null != document.getElementById('module-dining-admin-edit-secondary-filter')) {

		var select = document.getElementById('module-dining-admin-edit-secondary-filter');

		// loop through each options
		for (var i = 0; select.options.length > i; i++) {

			found = false;
		
			// loop through secondary tag IDs to check for selection
			for (var j = 0; secondaryTagIds.length > j; j++) {
				
				// check if this filter is selected
				if (secondaryTagIds[j] == select.options[i].value) {

					// set selected flag and remove tag from list
					select.options[i].selected = true;
					secondaryTagIds.splice(j, 1);
					found = true;
				}
			}

			if (false == found) {
				select.options[i].selected = false;
			}
		}
	}

	// set selected restaurant id
	if (null != document.getElementById('module-dining-admin-selected-restaurant')) {
		document.getElementById('module-dining-admin-selected-restaurant').value = restaurantId;
	}

	return;
} // )>>>

function GetXmlRequest() { // <<<(

	var obj_xml_request = null;
	// firefox, Opera, Safari
	try {
		obj_xml_request = new XMLHttpRequest();
	}
	catch (e) {

		// Internet Explorer
		try {
			obj_xml_request = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {

			// Internet Explorer (alternative)
			try {
				obj_xml_request = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (e) {
				alert('Sorry, your browser does not support AJAX.');
			}
		}
	}

	return obj_xml_request;
} // )>>>

function ModuleDiningAdminValidateTag(tag) { // <<<(

	var isValid = false;
	var trimmedTag = tag;

	// remove leading and trailing white-space
	formattedTag = tag.replace(/^\s+|\s+$/g, '');

	// check that tag is not empty
	if ('' != trimmedTag) {
		isValid = true;
	}

	return isValid;

} // )>>>


