<?
	//
	// FORM CHECKER LIBRARY
	// 
	// Builds JavaScript and PHP code to check form data.
	//
	
	require_once("incl_dobject.phtml");
	
	class FormChecker extends DebuggableObject {
	
		function FormChecker($js_check_function = "_fc_check()") {
			$this->checks = array();
			$this->types = array();
			$this->errors = array();
			$this->checked = false;
			$this->js_check_function = $js_check_function;
			
			$this->AddType("nonblank",
				array(&$this, "DoCheckNonblank"),
				array(&$this, "RenderNonblank"));		
		}

		function AddCheck($field_name, $type, $error_message) {
			if (empty($this->types[$type])) 
				$this->error("Invalid check field type: '$type'", "FormChecker::AddCheck", __LINE__, __FILE__);
			$this->checks[] = array("field" => $field_name, "type" => $type, "error" => $error_message);
		}
		
		function AddType($type, $check_callback, $render_callback) {
			if (empty($type) || !is_string($type)) 
				$this->error("Invalid type name: $type", "FormChecker::AddType", __LINE__, __FILE__);
			if (!is_callable($check_callback))
				$this->error("Check callback not callable", "FormChecker::AddType", __LINE__, __FILE);
			if (!is_callable($render_callback))
				$this->error("Render callback not callable", "FormChecker::AddType", __LINE__, __FILE);
			$data = array("type" => $type, "check_cb" => $check_callback, "render_cb" => $render_callback);
			$this->types[] = $data;
		}
		
		function GetValue($field_name) {
			if (!isset($_REQUEST[$field_name]))
				return false;
			else
				return $_REQUEST[$field_name];
		}
		
		//
		// Check the values (this is the PHP-side handler)
		//
		function Check() {
			$this->errors = array();
			foreach ($this->checks as $check) {
				$field_name = $check["field"];
				$error = $check["error"];

				$type = $this->types[$check["type"]];
				xassert(is_array($type), "type '$check[type]' not found"); 

				$result = call__func($type["check_cb"], $field_name);
				if (!$result) 
					$this->errors[$field_name] = $error;
			}
			$this->checked = true;
			
			// if we don't have any errors, we succeeded!
			if (empty($this->errors))
				return true;
			else
				return false;
		}
		
		function GetErrors() {
			if (!$this->checked)
				$this->Check();
			return $this->errors;
		}
		
		function Render() {
			$html  = "";
			$html .= $this->RenderHeader();
			foreach ($this->checks as $check) {
				$html .= "
				_fc_check_$check[type]('$check[field]');";
			}
			$html .= $this->RenderFooter();
			return $html;
		}
		
		function RenderHeader() {
			$html = "
			//
			// FormChecker code begin:
			//
			function _fc_get(id) {
				if (document.getElementById)
					return document.getElementById(id);
				if (document.all)
					return document.all[id];
			}
			function {$this->js_check_function}() {
				var errors = '';";
			return $html;
		}
		
		function RenderFooter() {
			$html = "
			}
			//
			// end FormChecker code
			//";
			return $html;
		}
		
		// NONBLANK 
		function DoCheckNonblank($field_name) {
			if (trim($this->GetValue($field_name)) == "") 
				return false;
			return true;
		}
		function RenderNonblank($field_name) {
					
		}
	}
?>
