

function prepare_structuredDataSearch() {
	// (v0.3) Written 2006 by Steve Tucker, http://www.stevetucker.co.uk
	if (!document.getElementById) return false;
	if (!document.getElementsByClassName) return false
	if (!document.getElementsByClassName('data_search')) return false;
	
	var $data_containers = document.getElementsByClassName('data_search');
	
	for (var $i=0; $i<$data_containers.length; $i++) {
		
		if (!$data_containers[$i].getAttribute('id')) {
			$data_containers[$i].setAttribute('id','data_container'+$i);
		}
		$data_container_id = $data_containers[$i].getAttribute('id');
	
		var $form = document.createElement('form');
		$form.className = 'structured_data_search';
		
		var $field_searchTerm = document.createElement('input');
		$field_searchTerm.setAttribute('name','search_term');
		$field_searchTerm.setAttribute('type','text');
		$field_searchTerm.setAttribute('alt','Search Term Entry Box');
		$field_searchTerm.setAttribute('value','Search For...');
		$field_searchTerm.setAttribute('id','field_searchTerm_'+$i);
		$field_searchTerm.setAttribute('data_container_id',$data_container_id);
		$field_searchTerm.className = 'textbox';
		$field_searchTerm.onkeyup = function() {
			var $data_container_id = this.getAttribute('data_container_id');
			var $field_id = this.getAttribute('id');
			structuredDataSearch($data_container_id,$field_id);
		}
		$field_searchTerm.onclick = function() {
			if (this.getAttribute('value') == 'Search For...') {
			      this.setAttribute('value','');
			}
		}
		
		$form.appendChild($field_searchTerm);
		
		var $parent = $data_containers[$i].parentNode;
		$parent.insertBefore($form,$data_containers[$i]);
	}
}


function structuredDataSearch($data_container_id,$field_id) {
	
	var $error_message = document.getElementById('error-'+$data_container_id);
	if ($error_message) removeNode($error_message);
	
	var $data_container = document.getElementById($data_container_id);
	
	$data_rows = $data_container.getElementsByTagName('li');
	if ($data_rows.length < 1) $data_rows = $data_container.getElementsByTagName('tr');
	if ($data_rows.length < 1) $data_rows = $data_container.getElementsByTagName('dd');
	
	if ($data_rows.length < 1) return false;
	
	var $field_searchTerm = document.getElementById($field_id);
	var $search_term = $field_searchTerm.value.toLowerCase();

	for (var $i=0; $i<$data_rows.length; $i++) {
		if ($data_rows[$i].className == 'omit_search') continue;
		if (typeof innerXHTML == 'function') {
			var $data = innerXHTML($data_rows[$i]).toLowerCase();
		}
		else {
			var $data = $data_rows[$i].innerHTML.toLowerCase();
		}
		$data = $data.replace(/(<([^>]+)>)/ig,"");
		if (!$data.match($search_term)) {
			$data_rows[$i].style.display = 'none';
		}
		else {
			$data_rows[$i].style.display = '';
			var $data_found = true;
		}
	}
	
	if (!$data_found) {
		var $para = document.createElement('p');
		$para.setAttribute('id','error-'+$data_container_id)
		var $text = document.createTextNode('No results found matching "'+$search_term+'"');
		$para.appendChild($text);
		var $parent = $data_container.parentNode;
		$parent.insertBefore($para,$data_container);
	}
}







/*==============================================================================*/
/*  The following functions may be deleted if you already have them on your	*/
/*  website, or if you would prefer to use the non-standard innerHTML		*/
/*==============================================================================*/


document.getElementsByClassName = function($name) {
	var $results = new Array();
	var $elements = document.getElementsByTagName("*");
	for (var $i=0; $i<$elements.length; $i++) {
		var $classes = $elements[$i].className.split(" ");
		for (var $j=0; $j<$classes.length; $j++) {
			if ($classes[$j] == $name) {
				$results[$results.length] = $elements[$i];
			}
		}
	}
	return $results;
};


function removeNode($node) {
	var $parent_node = $node.parentNode;
	var $handle = $parent_node.removeChild($node);
	return $handle;
}


innerXHTML = function($source,$string) {
	// (v0.3) Written 2006 by Steve Tucker, http://www.stevetucker.co.uk
	if (!($source.nodeType == 1)) return false;
	var $children = $source.childNodes;
	var $xhtml = '';
	if (!$string) {
		for (var $i=0; $i<$children.length; $i++) {
			if ($children[$i].nodeType == 3) {
				var $text_content = $children[$i].nodeValue;
				$text_content = $text_content.replace(/</g,'&lt;');
				$text_content = $text_content.replace(/>/g,'&gt;');
				$xhtml += $text_content;
			}
			else if ($children[$i].nodeType == 8) {
				$xhtml += '<!--'+$children[$i].nodeValue+'-->';
			}
			else {
				$xhtml += '<'+$children[$i].nodeName.toLowerCase();
				var $attributes = $children[$i].attributes;
 				for (var $j=0; $j<$attributes.length; $j++) {
					var $attName = $attributes[$j].nodeName.toLowerCase();
					var $attValue = $attributes[$j].nodeValue;
					if ($attName == 'style' && $children[$i].style.cssText) {
						$xhtml += ' style="'+$children[$i].style.cssText.toLowerCase()+'"';
					}
					else if ($attValue && $attName != 'contenteditable') {
						$xhtml += ' '+$attName+'="'+$attValue+'"';
					}
				}
				$xhtml += '>'+innerXHTML($children[$i]);
				$xhtml += '</'+$children[$i].nodeName.toLowerCase()+'>';
			}
		}
	}
	else {
		while ($children.length>0) {
			$source.removeChild($children[0]);
		}
		$xhtml = $string;
		while ($string) {
			var $returned = translateXHTML($string);
			var $elements = $returned[0];
			$string = $returned[1];
			if ($elements) $source.appendChild($elements);
		}
	}
	return $xhtml;
}
function translateXHTML($string) {
	var $match = /^<\/[a-z0-9]{1,}>/i.test($string);
	if ($match) {
		var $return = Array;
		$return[0] = false;
		$return[1] = $string.replace(/^<\/[a-z0-9]{1,}>/i,'');
		return $return;
	}
	$match = /^<[a-z]{1,}/i.test($string);
	if ($match) {
		$string = $string.replace(/^</,'');
		var $element = $string.match(/[a-z0-9]{1,}/i);
		if ($element) {
			var $new_element = document.createElement($element[0]);
			$string = $string.replace(/[a-z0-9]{1,}/i,'');
			var $attribute = true;
			while ($attribute) {
				$string = $string.replace(/^\s{1,}/,'');
				$attribute = $string.match(/^[a-z1-9_-]{1,}="[^"]{0,}"/i);
				if ($attribute) {
					$attribute = $attribute[0];
					$string = $string.replace(/^[a-z1-9_-]{1,}="[^"]{0,}"/i,'');
					var $attName = $attribute.match(/^[a-z1-9_-]{1,}/i);
					$attribute = $attribute.replace(/^[a-z1-9_-]{1,}="/i,'');
					$attribute = $attribute.replace(/;{0,1}"$/,'');
					if ($attribute) {
						var $attValue = $attribute;
						if ($attName == 'value') {
							$new_element.value = $attValue;
						}
						else if ($attName == 'class') {
							$new_element.className = $attValue;
						}
						else if ($attName == 'style') {
							var $style = $attValue.split(';');
							for (var $i=0; $i<$style.length; $i++) {
								var $this_style = $style[$i].split(':');
								$this_style[0] = $this_style[0].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/,'');
								$this_style[1] = $this_style[1].toLowerCase().replace(/(^\s{0,})|(\s{0,1}$)/,'');
								if (/-{1,}/g.test($this_style[0])) {
									var $this_style_words = $this_style[0].split(/-/g);
									$this_style[0] = '';
									for (var $j=0; $j<$this_style_words.length; $j++) {
										if ($j==0) {
											$this_style[0] = $this_style_words[0];
											continue;
										}
										var $first_letter = $this_style_words[$j].toUpperCase().match(/^[a-z]{1,1}/i);
										$this_style[0] += $first_letter+$this_style_words[$j].replace(/^[a-z]{1,1}/,'');
									}
								}
								$new_element.style[$this_style[0]] = $this_style[1];
							}
						}
						else {
							$new_element.setAttribute($attName,$attValue);
						}
					}
					else $attribute = true;
				}
			}
			$match = /^>/.test($string);
			if ($match) {
				$string = $string.replace(/^>/,'');
				var $child = true;
				while ($child) {
					var $returned = translateXHTML($string,false);
					$child = $returned[0];
					if ($child) $new_element.appendChild($child);
					$string = $returned[1];
				}
			}
			$string = $string.replace(/^\/>/,'');
		}
	}
	$match = /^[^<>]{1,}/i.test($string);
	if ($match && !$new_element) {
		var $text_content = $string.match(/^[^<>]{1,}/i)[0];
		$text_content = $text_content.replace(/&lt;/g,'<');
		$text_content = $text_content.replace(/&gt;/g,'>');
		var $new_element = document.createTextNode($text_content);
		$string = $string.replace(/^[^<>]{1,}/i,'');
	}
	$match = /^<!--[^<>]{1,}-->/i.test($string);
	if ($match && !$new_element) {
		if (document.createComment) {
			$string = $string.replace(/^<!--/i,'');
			var $text_content = $string.match(/^[^<>]{0,}-->{1,}/i);
			$text_content = $text_content[0].replace(/-->{1,1}$/,'');			
			var $new_element = document.createComment($text_content);
			$string = $string.replace(/^[^<>]{1,}-->/i,'');
		}
		else $string = $string.replace(/^<!--[^<>]{1,}-->/i,'');
	}
	var $return = Array;
	$return[0] = $new_element;
	$return[1] = $string;
	return $return;
}
