	function findValue(li,nonMatchedValue) {
		if( li == null ){ 
			searchString = "/search/"
			nonMatchedValue = document.Form1.text.value;


//Check if the search value is ="", if it is then just direct the user to the home page by rewriting the URL to nothing                     
   if( nonMatchedValue != "" ){ 
                 		nonMatchedValue = searchString + nonMatchedValue + "/"  
                        } else {
                                nonMatchedValue = ""
                        }
 			logSimple(sValue1,nonMatchedValue);
                        
			
			return;
		}

		// if coming from an AJAX call, let's use the CityId as the value
		if( !!li.extra ){
			 var sValue = li.extra[0];
			 var sValue1 = li.selectValue;
		 }

		// otherwise, let's just display the value in the text box
		else var sValue = li.selectValue;

		logSimple(sValue1,sValue);
		
		
		// event driven output from the key's ID in the array
		//var form = '<form id="dynForm" action="http://www.myexperiencedays.net/ac/post.php" method="post"><input type="hidden" name="q" value=';
	//alert("The VALUE is: " +form);

	//	alert("The search URL is: " +sValue+ " based on a matched value of: " +sValue1);
		
}


function selectItem(li) {
	findValue(li);
}

//Show the user the possible auto corrections
function formatItem(row) {
	// option to also show the URL for debug 
	//return row[0] + " (id: " + row[1] + ")";
	return row[0];
}



function logSimple(a,b) {

		var url = "http://www.myexperiencedays.net/ac/post.php";
		
		$("<li>").html("Selected: " +a+ " and: " +b).appendTo("#result");
		
		postTo(url, b);
}
		

function postTo(url, valuea) {
	var form = '<form id=\"dynForm\" action=\"http://www.myexperiencedays.net/ac/post.php\" method=\"post\"><input type=\"hidden\" name=\"q\" value=\"'+valuea+'\"></form>';
	document.body.innerHTML += form;

	document.body.innerHTML += form;
	document.getElementById("dynForm").submit();
}



function postToURL(url, values)
{
    values = values || {};

    var form = createElement("form", {action: url,
                                      method: "POST",
                                      style: "display: none"});
    for (var property in values)
    {
        if (values.hasOwnProperty(property))
        {
            var value = values[property];
            if (value instanceof Array)
            {
                for (var i = 0, l = value.length; i < l; i++)
                {
                    form.appendChild(createElement("input", {type: "hidden",
                                                             name: property,
                                                             value: value[i]}));
                }
            }
            else
            {
                form.appendChild(createElement("input", {type: "hidden",
                                                         name: property,
                                                         value: value}));
            }
        }
    }
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
}

	
function lookupAjax(){
	var oSuggest = $("#Experience")[0].autocompleter;

	oSuggest.findValue();


	return false;
}


$(document).ready(function() {
	

	$("#Experience").autocomplete(
		"/search.php",
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			onFindValue:findValue,
			formatItem:formatItem,
			autoFill:true
		}
	);

	
});

				
