﻿/*
   Script name  : Ajax Auto Suggest
   File Name 	: script.js
   Developed By : Amit Patil (India)
   Email Id 	: amitpatil321@gmail.com
   last Updated : 9 June 2009
         This program is freeware.There is no copyright and bla bla bla.
   You can use it for your personal use.You can also make any changes to this script.
   But before using this script i would appericiate your mail.That will encourage me a lot.
   Any suggestions are always welcome.
         Have a fun with programming.   
*/
var item;

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}



function buildAutoSuggest(event, itemname){
	 //alert(event.keyCode);
	 var keyword = $("#"+itemname).val();
	 item = "#"+itemname;
	 itemnumber = item.substring(4,5);
	 //alert(keyword);
	 if(keyword.length)
	 {	 
		 if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
		 {
			 $("#loading").css("visibility","visible");
			 $.ajax({
			   type: "POST",
			   url: "ajax_server.php",
			   data: "data="+keyword,
			   success: function(msg){	
				if(msg != 0)
				{
				  // Move the suggestion box down
				  $("#ajax_response").css("top",18+(itemnumber*25));
				  // Fade in the suggestion box
				  $("#ajax_response").fadeIn("slow").html(msg);
				}
				else
				{
					// Fade in suggestion box
				  $("#ajax_response").fadeIn("slow");
				  // Send no match message to suggestion box
				  $("#ajax_response").html('<div style="text-align:left;">No Match.  Please enter grams and calories manually.</div>');
				}
				$("#loading").css("visibility","hidden");
			   }
			 });
		 }
		 else
		 {
			switch (event.keyCode)
			{
			 case 40:
			 {
				  found = 0;
				  $("li").each(function(){
					 if($(this).attr("class") == "selected")
						found = 1;
				  });
				  if(found == 1)
				  {
					var sel = $("li[class='selected']");
					sel.next().addClass("selected");
					sel.removeClass("selected");
				  }
				  else
					$("li:first").addClass("selected");
				 }
			 break;
			 case 38:
			 {
				  found = 0;
				  $("li").each(function(){
					 if($(this).attr("class") == "selected")
						found = 1;
				  });
				  if(found == 1)
				  {
					var sel = $("li[class='selected']");
					sel.prev().addClass("selected");
					sel.removeClass("selected");
				  }
				  else
					$("li:last").addClass("selected");
			 }
			 break;
			 case 13:
			 {
				var fullitem = $("li[class='selected'] a").text();
				$(item).val(fullitem);
				$.ajax({
			    	type: "POST",
			    	url: "ajax_server.php",
			    	data: "itemname=" + item + "&itemval=" + fullitem,
			    	dataType: "script",
			    	success: function(data){	
						if(data != 0)
						{
							data;									 			
				 		}
						else
						{	
				  			alert('No Grams or Calories');
						}
			   		}
			 	});
			 	$("#ajax_response").fadeOut("slow");
			 }
			 break;
			}
		 }
	 }
	 else
		$("#ajax_response").fadeOut("slow");
}



$(document).ready(function(){
	$(document).click(function(){
		$("#ajax_response").fadeOut('slow');
	});
	//$("#ing1Nameid").focus();
	/*var offset = $("#keyword").offset();
	var width = $("#keyword").width()-2;
	$("#ajax_response").css("left",offset.left);
	$("#ajax_response").css("width",width); */
	$("#ajax_response").mouseover(function(){
		$(this).find("li a:first-child").mouseover(function () {
			  $(this).addClass("selected");
		});
		$(this).find("li a:first-child").mouseout(function () {
			  $(this).removeClass("selected");
		});
		//User clicks on selected item, item is entered into field and grams and calories set
		$(this).find("li a:first-child").click(function () {
			$(item).val($(this).text());
			var fullitems = $(item).val();
			$.ajax({
			    type: "POST",
			    url: "ajax_server.php",
			    data: "itemname=" + item + "&itemval=" + fullitems,
			   	dataType: "script",
			    success: function(data){	
					if(data != 0)
					{
						data;									 			
				 	}
					else
					{	
				  		alert('No Grams or Calories');
					}
				}
			});
			$("#ajax_response").fadeOut("slow");
		});
	});
});


