$(document).ready(function() {

		//the min chars for nutname
		var min_chars = 3;

		//result texts
		var characters_error = 'Min amount of chars is 3';
		var checking_html = 'CHECKING...';

		//when button is clicked
		//$('#check_nutname_availability').click(function(){
		$('#nutname').keyup(function(){ 
			//run the character number check
			if($('#nutname').val().length == 0) {
			$('#createbutton').removeAttr("disabled");
			$('#createhidden').removeAttr("disabled");
			}
			else if($('#nutname').val().length < min_chars) {
				//if it's bellow the minimum show characters_error text '
				$('#nutname_availability_result').html('<span class="error">' + characters_error + '</b></span>');
				$('#createbutton').attr("disabled","disabled");
				$('#createhidden').attr("disabled","disabled");
			
			}else{
				//else show the cheking_text and run the function to check
				$('#nutname_availability_result').html('<span class="error">' + checking_html + '</b></span>');
				check_availability();
			}
		});

  });
  
function htmlEncode(value){
	if (value) {         
		return jQuery('<div />').text(value).html();    
	} 
	else {         return '';     }
}

//function to check nutname availability
function check_availability(){

		//get the nutname - should pass in
		var nutname = htmlEncode($('#nutname').val());
		//alert(nutname);
		//var nutname = $('#nutname').val();
		//use ajax to run the check
		// $.post("/../checkname.php", { nutname: nutname }, for linux1
		$.post("./checkname.php", { nutname: nutname },
			function(result){
				//if the result is 1
				if(result == 1){
					//show that the nutname is available
					$('#nutname_availability_result').html('<span class="is_available"><b>' + nutname + '</b> - AVAILABLE</span>');
					$('#createbutton').removeAttr("disabled");
					$('#createhidden').removeAttr("disabled");
				}else{
					//show that the nutname is NOT available
					$('#nutname_availability_result').html('<span class="is_not_available"><b>' + nutname + '</b> - NOT AVAILABLE</span>');
					$('#createbutton').attr("disabled","disabled");
					$('#createhidden').attr("disabled","disabled");
				}
		});

}
