<?php require($_SERVER['DOCUMENT_ROOT'] . '/admin/inc/sessionStart.php'); ?>

<?php
  $pageTitle = "Administration: Add a Recipe";
  $pageDescription = "";
  $pageKeywords = "";
?>

<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/header.php'); ?>

<script type="text/javascript" src="/Kickstrap/js/nicEdit.js"></script>
<link rel="stylesheet" href="//afsshareportal.com/colorbox/colorbox.css" />
<script src="//afsshareportal.com/colorbox/jquery.colorbox.js"></script>
<style>
	  #mainContent .row .span12{
			width: 95%;
	  }
	  
	  .remLen{
			width:30px;
	  }
	  
	  #ingredientTable, #allDirections, .newDirectionDiv, .newIngredientDiv{
			margin-left:30px;
	  }
	  
	  .formTitles{
			font-size:18px;
			color:#c71c22;
			font-weight:bold;
			margin:12px 0 6px 0;
	  }
	  
	  .nicEdit-main img{
			width:250px;
			display: block;
	  }
</style>

<script src="/Kickstrap/js/jquery-ui-1.9.0.custom.min.js"></script>
<link href="/Kickstrap/css/smoothness/jquery-ui-1.9.0.custom.css" rel="stylesheet">

<script>
	  function textCounter(field, countfield, maxlimit) {
			if (field.value.length > maxlimit) // if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
			// otherwise, update 'characters left' counter
			else 
			countfield.value = maxlimit - field.value.length;
	  }
		
		
	  window.onload = initForms;
	  
	  
	  function initForms() {
		   dimDefaultInputs();
		   dimDefaultTextAreas();
		   $(document).ready(function() {
				$(".error").hide();
		   });
	  }
		
	  function recipeNameValidation() {
			$(document).ready(function() {
				$("label#name_error").hide();
				var name = $("input#recipeName").val();
				if (name == "") {
					$("label#name_error").show();
					$("input#recipeName").addClass("error-field");
					$("input#recipeName").focus();
					return false;
				} else {
					$("label#name_error").hide();
					$("input#recipeName").removeClass("error-field");
					return false;
				}
			});
	  }
		
	  function prepTimeValidate() {
			
				$("input#preptimeBox").removeClass("error-field");
				$("span#prep_error").hide();
				var value = $("input#preptimeBox").val();
				if(value != "") {
					var checkOK = "0123456789";
					var allValid = true;
					for(var i=0; i<value.length; i++){
						var ch = value.charAt(i);
						for(var j=0; j<checkOK.length; j++){
							if(ch==checkOK.charAt(j)){
								allValid = true;
								break;
							} else{
								allValid = false;
							}
						}
						if(!allValid){
							$("span#prep_error").text(" You can only enter numbers. No letters or punctuation, please!");
							$("span#prep_error").show();
							$("input#preptimeBox").addClass("error-field");
							$("input#preptimeBox").focus();
							return false;
						} 
					}
					return true;
				}
				else{
					alert("Please enter something into the prep time input box");
					$("input#preptimeBox").focus();
					return false;
				}
			
	  }
		
	  function dimDefaultInputs() {
			var active_color = '#000'; // Colour of user provided text
			var inactive_color = '#999'; // Colour of default text
			 
			  $("input.default-value").css("color", inactive_color);
			  var default_values = new Array();
			  $("input.default-value").focus(function() {
				if (!default_values[this.id]) {
				  default_values[this.id] = this.value;
				}
				if (this.value == default_values[this.id]) {
				  this.value = '';
				  this.style.color = active_color;
				  $(this).removeClass("default-value");
				}
				$(this).blur(function() {
				  if (this.value == '') {
					this.style.color = inactive_color;
					this.value = default_values[this.id];
				  }
				});
			  });
	  }
		
	  function dimDefaultTextAreas() {
			var active_color = '#000'; // Colour of user provided text
			var inactive_color = '#999'; // Colour of default text
			 
			  $("textarea.default-value").css("color", inactive_color);
			  var default_values = new Array();
			  $("textarea.default-value").focus(function() {
				if (!default_values[this.id]) {
				  default_values[this.id] = this.value;
				}
				if (this.value == default_values[this.id]) {
				  this.value = '';
				  this.style.color = active_color;
				  $(this).removeClass("default-value");
				}
				$(this).blur(function() {
				  if (this.value == '') {
					this.style.color = inactive_color;
					$(this).addClass("default-value");
					this.value = default_values[this.id];
				  }
				});
			  });
	  }
		
	  var ingredientCounter = 1;
	  var directionsCounter = 1;
		
	  function outputIngredientLine2() {
			//alert(document.getElementById("ingredient100"));
			while(document.getElementById("ingredient" + ingredientCounter) != null){
				ingredientCounter++;
			}
			var output = "";
			output += "<label for='ingredient" + ingredientCounter + "'><label><input id='ingredient" + ingredientCounter + "' type='text' size='53' class='reqd default-value' value='Ingredient' name='ingredient[]' />";
			output += "&nbsp;<input type='checkbox' id= 'optionalIngredient" + ingredientCounter + "' name='optionalIngredient" + ingredientCounter + "' value='optional'> This ingredient is optional<br />";
			var newDiv = document.createElement("div");
			newDiv.setAttribute('id', 'ingredientLine' + ingredientCounter);
			newDiv.setAttribute('name', 'ingredientLine[]');
			newDiv.innerHTML = output;
			document.getElementById("ingredientTable").appendChild(newDiv);
			$("#ingredientTable").append("<br>");
			document.getElementById("ingredient" + (ingredientCounter-1)).removeAttribute("onblur");
			var ingposition = "ingredient" + ingredientCounter;
			dimDefaultInputs();
			document.getElementById(ingposition).focus();
			ingredientCounter++;
			//alert("hello");
			//document.getElementsByTagName("ingredient" + (ingredientCounter-1)).onblur = "";
	  }
		
	  function newDirectionStep() {
			while(document.getElementById("directions" + directionsCounter) != null){
				directionsCounter++;
			}
			var dirOutput = "";
			dirOutput += "<label for='directions" + directionsCounter + "'></label>Step " + directionsCounter + " <br /><textarea name='directions[]' id='directions" + directionsCounter + "' cols='51' rows='4'>Type your directions here</textarea>";
			var newDirDiv = document.createElement("div");
			newDirDiv.setAttribute('id', 'direction' + directionsCounter);
			newDirDiv.setAttribute('name', 'direction[]');
			newDirDiv.innerHTML = dirOutput;
			document.getElementById("allDirections").appendChild(newDirDiv);
			document.getElementById("directions" + (directionsCounter-1)).removeAttribute("onblur");
			dimDefaultTextAreas();
			$dirPosition = "directions" + directionsCounter;
			document.getElementById($dirPosition).focus();
			document.getElementById($dirPosition).select();
			directionsCounter++;
	  }
</script>

<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/mainNav.php'); ?>
<?php require($_SERVER['DOCUMENT_ROOT'] . '/admin/inc/class.recipes.inc.php'); ?>

<?php
	  if(isset($_POST['recipeName'])){
			$action = "process";
	  }else{
			$action = "new";
	  }
	  
?>

    
    <section class="container">
      <div id="blankBuffer"></div>
      <section class="container" id="storePortal">
			<div class="row">
				  <div class="span3 sideNav">
						<?php include('inc/sideNav.php'); ?>
				  </div>
		  
				  <div class="span9 portalContent">
						<div class="contentContain">
							  <h2>Add Recipe</h2>
							  <div id="updateText"></div>
						
							  
							  <?php
									$recipeObj = new recipes();
									if($action == "process"){
										  
										  $newRecipe = $recipeObj->insertRecipe();
										  
										  $theRecipe = $recipeObj->getRecipeByID($newRecipe);
										  
							  ?>
									
							  <?php
									}//end if($action == "process")
									else{
							  ?>
									<form action="addRecipes.php" method="post" id="recipeForm" name="recipeForm" enctype="multipart/form-data">
										  <?php include('https://afsshareportal.com/common/recipeForm.php'); ?>
									</form>
							  <?php
									}//end else for(if($action == "process"))
							  ?>
						</div>
				  </div>
			</div>
	    
	    
      </section>
      
    </section>
      
    
    
    <script>
	  new nicEditor({
			fullPanel : false,
			buttonList : ['image', 'upload']
	  }).panelInstance("recipe_photoSquHolder",{hasPanel : true});
	</script>
    
  
  <script>
	  
	  var theProduce = [];
	  getCategories();
	
   
	  function getCategories(){
		$.ajax({
		  type: "GET",
		  url: base_site + "lookUpFeatures.php",
		  data: "action=getCategories",
		  dataType: "jsonp",
		  jsonp : "callback",
		  jsonpCallback: "categorycallback"
		});
	  }
	  
	  function categorycallback(theResponse){
			var catHTML = "";
			$.each(theResponse, function(i, obj){
				  catHTML += "<option value='" + obj.ID +"'>" + obj.name + "</option><br/>";
				  //theCategories.push({"id": obj.id, "name": obj.name});
			});
			
			$("#recipCategory").append(catHTML);
	  }
	  
	  getProduce();
	  
	  function getProduce(){
			$.ajax({
				  type: "GET",
				  url: base_site + "lookUpFeatures.php",
				  data: "action=getAllProduce",
				  dataType: "jsonp",
				  jsonp : "callback",
				  jsonpCallback: "producecallback"
			});
	  }
	  
	  function producecallback(allProduce){
			$.each(allProduce, function(i, obj){
				  theProduce.push({"label": obj.name, "value": obj.name.replace(" ", "_")});
			});
	  }
	  
	  $("#mainIng").autocomplete({
			minLength: 0,
			focus: function(event, ui){
				  //$("#storeName").val(ui.item.store_name);
				  //return false;
			},
			select: function(event, ui) {
				  $("#mainIng").val(ui.item.label);
				  return false;
			},
			source: function(request, response) {
				  var results = $.ui.autocomplete.filter(theProduce, request.term);
		  
				  response(results.slice(0, 10));
			},
			}).data("ui-autocomplete")._renderItem = function(ul, item){
				  return $("<li>").append("<a>" + item.label).appendTo(ul);
	  };
	  
	  $("body").on("submit", "#recipeForm", function(e){
			//e.preventDefault();
			$("input[id^='ingredient']").each(function(){
				  if($(this).val() == "Ingredient"){
						$(this).parent("div").remove();
				  }
			});
			
			$("textarea[id^='direction']").each(function(){
				  //console.log($(this).val());
				  if($(this).val() == "Type your directions here"){
						$(this).parent("div").remove();
				  }
			});
			
			$("#recipe_photoSqu").attr("value", $("#recipe_photoSquHolderNewContent").find("img").attr("src").substring(1));
			
			return true;
	  });
   
   $(document).ready(function() {
   });
  </script>
<?php require($_SERVER['DOCUMENT_ROOT'] . '/common/footer.php'); ?>
