var IE = (document.all);


function $(id){
	if(document.getElementById){
		if(document.getElementById(id)){
			return document.getElementById(id);
		}else if(document.all){
			return document.all[id];
		}
	}
	return null;
}

function live_search_box_key_press(e){
	var evt = (window.event) ? window.event : e;
	var KeyID = evt.keyCode;
	//console.log(KeyID);
	if(KeyID == 13){
		evt.preventDefault();
		evt.returnValue = false;
		return false;
		// not going to work
		$("SEGoldenQuestion1").value = temp_list[0].value;
	}
	
	var val = $("SEGoldenQuestion1").value;
	
	if(val != search_value){
		
		search_value = val.toLowerCase();
		
		if(search_value.length>=3){
			var temp_list = new Array();
			
			for(var i=0; i<breeds.length; i++){
				var option = breeds[i];
				if(option.text.toLowerCase().indexOf(search_value) == 0 || option.text.toLowerCase().indexOf(" " + search_value) >= 1){
					temp_list.push(option);
				}
			}
		
			if(temp_list.length == 1){
				//$("SEGoldenQuestion1").value = temp_list[0].value;
				$("SEGoldenQuestion1").value = temp_list[0].text;
				var old = $('live_search_list'); 
				if(old){
					old.parentNode.parentNode.removeChild(old.parentNode);
				}
			}else{
				make_new_list_interface(temp_list);
			}
		}
	}
}



// live_search_instruction_string is defined in the page to pick up the 
// translation from the XML
function make_new_list_interface(data){
	var old = $('live_search_list'); 
	if(old){
		old.parentNode.parentNode.removeChild(old.parentNode);
	}
	
	if(data.length>0){
		var live_search_results = document.createElement("div");
		live_search_results.appendChild(document.createTextNode(live_search_instruction_string))
		
		var x = document.createElement("img");
		x.src = "propimages/prop3/x.gif";
		x.width = 14;
		x.height = 13;
		x.id = "live_search_close_link";
		// from in-page javascript
		x.alt = close_string;
		var c = function(){
			var old = $('live_search_list'); 
			if(old){
				old.parentNode.parentNode.removeChild(old.parentNode);
			}
		}
		
		if(IE){
			x.attachEvent('onclick', c);
		} else {
			x.addEventListener('click', c, false);
		}
		
		
		live_search_results.appendChild(x);
		var list = document.createElement("ul");
		list.id = "live_search_list";	
		
		for(var i=0; i<data.length; i++){
			var piece = data[i];
			var link = document.createElement("a");
			link.href = "#live_search_box";
			link.appendChild(document.createTextNode(piece.text))
			link.click_data = piece;
			
			var f = function (e) {
				var me;
				if (e.target){
					me = e.target;
				} else {
					me = e.srcElement;
				}
				
				//$("SEGoldenQuestion1").value = me.click_data.value;
				$("SEGoldenQuestion1").value = me.click_data.text;
				
				live_search_results.parentNode.removeChild(live_search_results);
			};
			
			if(IE){
				link.attachEvent('onclick', f);
			} else {
				link.addEventListener('click', f, false);
			}
			
			var item = document.createElement("li")
			item.appendChild(link);
			list.appendChild(item);
		}
		
		live_search_results.appendChild(list);
		live_search_container.appendChild(live_search_results);
	}
}

// prepare some variables
var breed_parent;
var breed_options;

var search_value = "";

var live_search_box;
var live_search_value_field;
var live_search_results;
var live_search_container;

var breeds = new Array();

var ready = new domFunction(function(){
	var breedList = $('SEGoldenQuestion1');
	//alert(breedList.value);
	if(breedList){
		// prepare some variables
		breed_parent = breedList.parentNode;
		breed_options = breedList.options;

		// move the options into an array for use later
		for(var i=0; i< breed_options.length; i++){
			breeds[i] = new Object();
			breeds[i].text = breed_options[i].text;
			breeds[i].value = breed_options[i].value;
		}
		
		// remove the old interface
		breed_parent.removeChild(breedList);
		
		// create the new interface
		live_search_box = document.createElement("input");
		live_search_box.id = "SEGoldenQuestion1";
		live_search_box.CssClass = "form_fields";
		live_search_box.onkeyup = live_search_box_key_press;
		live_search_box.name = breedList.name;
		
		//live_search_value_field = document.createElement('input');
		//live_search_value_field.id = live_search_value_field.name = "SEGoldenQuestion1";
		//live_search_value_field.type = "hidden";
		
		if(breedList.selectedIndex>0){
			live_search_box.value = breedList.options[breedList.selectedIndex].text;
			//live_search_value_field.value = breedList.options[breedList.selectedIndex].value;
		}
		
		live_search_container = document.createElement("div");
		live_search_container.id = "live_search_container";
		
		live_search_container.appendChild(live_search_box);
		//live_search_container.appendChild(live_search_value_field);
		// add the new interface to the page
		breed_parent.appendChild(live_search_container);
	
	}
}, { 'additionalRow' : 'id' });
