<?php
include_once('includes/system_settings/system_settings_components.php');

$callFn = 'AJAX_'.check_vars($_GET['todo']);

switch($callFn) {
	default:
		if(function_exists($callFn)) {
			$returndata = $callFn();
		} else {
			$returndata = array('status' => false, 'errorMessage' => 'Request could not be handled, no such sys fn');
		}
}

function AJAX_get_system_settings() {
	global $js_files;
	$returndata = array();
	
	$variables = get_passed_variables('get');
	
	if($result = get_system_settings_page($variables)) {
		$returndata['status'] = true;
		$returndata['htmlData'] = $result;
		
		$char_type_used = '';
		if(!empty($js_files)) {
			foreach($js_files as $this_js_file) {
				$char_type_used = substr($this_js_file, (strpos($this_js_file, 'src=')+4), 1);
				
				$this_js_file = substr($this_js_file, (strpos($this_js_file, 'src=')+5));
				
				$this_js_file = substr($this_js_file, 0, (strpos($this_js_file, $char_type_used)-1));
				
				if(strpos($this_js_file, '?')) {
					$this_js_file = substr($this_js_file, 0, strpos($this_js_file, "?"));
				}
				
				$returndata['js_files'][] = $this_js_file;
			}
		}
		$returndata['charUsed'] = $char_type_used;
		
	} else {
		$returndata['status'] = false;
		$returndata['variables'] = $variables;
		$returndata['errorMessage'] = 'Error: Could not get system setting page';
		
		$returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;		
}

function AJAX_edit_system_user() {
	$returndata = array();
	
	$variables = get_passed_variables('get');
	
	if($result = edit_system_user($variables)) {
		$returndata['status'] = true;
		$returndata['htmlData'] = $result;
	} else {
		$returndata['status'] = false;
		$returndata['variables'] = $variables;
		$returndata['errorMessage'] = 'Error: Could not open user for edit';
		
		$returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;		
}

function AJAX_save_user_data() {
	$returndata = array();

	$variables = get_passed_variables('post');

	if($result = save_user_data($variables)) {
		$returndata['status'] = true;
		$returndata['popupMessage'] = 'User data save successful';
	} else {
		$returndata['status'] = false;
		$returndata['variables'] = $variables;
		$returndata['errorMessage'] = 'Error: Could not save user data';
		
		$returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;		
}

function AJAX_update_user_permission() {
	$returndata = array();

	$variables = get_passed_variables('get');

	if(!is_system_admin()) {
		$returndata['status'] = false;
		$returndata['errorMessage'] = 'Error: You do not have permission to update user permissions';
		return $returndata;
	}

	if($result = update_user_permission($variables)) {
		$returndata['status'] = true;
		$returndata['popupMessage'] = 'User permission update successful';
	} else {
		$returndata['status'] = false;
		$returndata['variables'] = $variables;
		$returndata['errorMessage'] = 'Error: Could not update user permission';
		
		$returndata['globalErrors'] = $GLOBALS['errors'];
	}
	
	return $returndata;		
}