function showLogin(state){
	kt_log = document.getElementById("kt_login");
	var cloudObj = document.getElementById("cloud");
	if(state){
		kt_log.style.display = "inline";
		kt_log.style.border = "1px solid black";
		cloudObj.style.display = "inline";
	}
	else{
		kt_log.style.display = "none";
		cloudObj.style.display = "none";
	}
}
function kt_updateOptions(){
	var kt_OptZip = document.getElementById("zipText");
	url = 'optionsJSON.php?zip=';
	new Ajax.Request(url, {
	method:'get',
	parameters: {zip: kt_OptZip.value },
	requestHeaders: {Accept: 'application/json'},
	onSuccess: function(transport){
		var error = '';
		var cuisine = '';
		var dish = '';
		var features = '';
		eval(transport.responseText);
		if(cuisine.length == 0 && dish.length == 0 && features.length == 0 ){
			alert( "Currently there are no restaurants participating in this zip code. Please search again by the location nearest you.");
			var kt_OptCuisine = document.getElementById("cuisineOptions");
			var kt_OptDish = document.getElementById("dishOptions");
			clearOptions(kt_OptCuisine);
			clearOptions(kt_OptDish);			
			kt_OptZip.focus();
		}
		else if(cuisine.length == 1 && dish.length == 1 && features.length == 1 && cuisine[0][0] == 'Err'){
			alert( "Not a Vaild Zip Code");
			kt_OptZip.focus();
		}
		else{
			kt_DomWork(cuisine, dish, features);
		}
	}
}); 
	/*new Ajax.Request(url, {
		onSuccess: function(transport) {
			alert( transport.responseText);
			
			var t = transport.responseText.eval();
			
			//var json = transport.responseText.evalJSON();
			alert("!");
			
		}
	});*/
}
function kt_DomWork(c, d, f){
	var kt_OptCuisine = document.getElementById("cuisineOptions");
	var kt_OptDish = document.getElementById("dishOptions");

   if( c.length > 0 ){
		clearOptions(kt_OptCuisine);
		labelOptions(kt_OptCuisine, "by cuisine");
		try{
			for(x=0; x<c.length; x++){
				var p = document.createElement("option");
				p.value = "c-" + c[x][0];
				p.innerHTML = c[x][1];
				kt_OptCuisine.appendChild(p);
			}
		}
		catch (e){}

   }
   else{
		clearOptions(kt_OptCuisine);
		var p = document.createElement("option");
		p.value = "any";
//		p.innerHTML = "any";
		p.innerHTML = "none";		
		kt_OptCuisine.appendChild(p);
   }

   if( d.length > 0 || f.length > 0){
   
		clearOptions(kt_OptDish);
		labelOptions(kt_OptDish, "by dish/feature");
		if( d.length > 0 ){
			try{
				for(x=0; x<d.length; x++){
					var p = document.createElement("option");
					p.value = "d-" + d[x][0];
					p.innerHTML = d[x][1];
					kt_OptDish.appendChild(p);
				}
			}
			catch(e){}
		}
		else{
			var p = document.createElement("option");
			p.value = "any";
//			p.innerHTML = "any";
			p.innerHTML = "none";
			kt_OptDish.appendChild(p);
		}
		var q = document.createElement("option");
		q.value = 0;
		$kt_test = "----------------------------";
		q.innerHTML = $kt_test;
		kt_OptDish.appendChild(q);
		if( f.length > 0 ){
			try{
				for(x=0; x<f.length; x++){
					var p = document.createElement("option");
					p.value = "f-" + f[x][0];
					p.innerHTML = f[x][1];
					kt_OptDish.appendChild(p);
				}
			}
			catch(e){}
		}
		else{
			var p = document.createElement("option");
			p.value = "any";
//			p.innerHTML = "any";
			p.innerHTML = "none";
			kt_OptDish.appendChild(p);
		}
   }
   else{
		clearOptions(kt_OptCuisine);
		var p = document.createElement("option");
		p.value = 0;
		p.innerHTML = "No Dishes or Features Specified";
		kt_OptCuisine.appendChild(p);
   }
}

function labelOptions(opt, label){
		var p = document.createElement("option");
		p.value = '';
		p.innerHTML = label;
		var q = document.createElement("option");
		q.value = "any";
		q.innerHTML = "any";
		opt.appendChild(p);
		opt.appendChild(q);
}
function clearOptions(opt){
	while (opt.firstChild) {
    //The list is LIVE so it will re-index each call
    opt.removeChild(opt.firstChild);
 	};
}
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
function failedlogin(){
	if(gup('failedlogin') == 'yes'){
		showLogin(true);
	}
}
String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
}
function submitSearch(){
	if((document.form1.zipText.value == "by zip" || document.form1.zipText.value.trim() == "") &&
		(document.form1.searchName.value == "by name" || document.form1.searchName.value.trim() == "")  
	){
		alert("You must specify either a Zip or Name");
		document.form1.zipText.value = "by zip";
		document.form1.searchName.value = "by name";  
	}
	else{
		document.form1.action="search.php";
		document.form1.submit();
	}
}

