// JavaScript Document for Recipe Pages

function submit_recipe_search_form() {
  document.form_search.submit();
}

ing_idx = 1;

function init_idx(val) {
  ing_idx = val;
}

function init_focus(page) {
  if(page == "recipes_add") {
    var title_box = document.getElementById('title');
    title_box.focus();
  }
}

function add_ingredient() {
  var div = document.getElementById('admin_recipe_ingredients');
  var table = document.getElementById('admin_recipe_ingredients_table');
  var row = document.createElement('tr');
  var td1 = document.createElement('td');
  var td2 = document.createElement('td');
  var td3 = document.createElement('td');
  var td4 = document.createElement('td');
  var td5 = document.createElement('td');
  var input1 = document.createElement('input');
  //input1.setAttribute('id', 'qty'); 
  input1.setAttribute('id', 'qty['+ parseInt(ing_idx) + ']');
  input1.setAttribute('name', 'qty['+ parseInt(ing_idx) + ']');
  input1.setAttribute('type', 'text');
  input1.className = 'admin_text_field_s';
  var input2 = document.createElement('input');
  input2.setAttribute('id', 'unit['+ parseInt(ing_idx) + ']');
  input2.setAttribute('name', 'unit['+ parseInt(ing_idx) + ']');
  input2.setAttribute('type', 'text');
  input2.className = 'admin_text_field_m';
  var input3 = document.createElement('input');
  input3.setAttribute('id', 'ingredient['+ parseInt(ing_idx) + ']');
  input3.setAttribute('name', 'ingredient['+ parseInt(ing_idx) + ']');
  input3.setAttribute('type', 'text');
  input3.className = 'admin_text_field_l';
  td1.appendChild(input1);
  td3.appendChild(input2);
  td5.appendChild(input3);
  row.appendChild(td1);
  row.appendChild(td2);
  row.appendChild(td3);
  row.appendChild(td4);
  row.appendChild(td5);
  var tbody = table.getElementsByTagName('tbody')[0];
  tbody.appendChild(row);
  //alert(table.innerHTML);
  //alert('before: ' + ing_idx);
  ing_idx++;
  //alert('after: ' + ing_idx);
  //alert(table.innerHTML);
  input1.focus();
}

function delete_ingredient() {
  var div = document.getElementById('admin_recipe_ingredients');
  var table = document.getElementById('admin_recipe_ingredients_table');
  var tbody = table.getElementsByTagName('tbody')[0];
  var rows = tbody.getElementsByTagName('tr');
  var len = parseInt(rows.length) - 1;
  if(len > 1) {
    tbody.removeChild(rows[len]);
    upperIdx = tbody.childNodes.length-1;
    var row = tbody.childNodes[upperIdx];
    var cell = row.childNodes[0];
    var elementId = 'qty[' + parseInt(upperIdx - 1) + ']';
    var td1 = document.getElementById(elementId);
    td1.focus();
    //alert('before: ' + ing_idx);
    ing_idx--;
    
    //alert('after: ' + ing_idx);
  } else {
    alert('You must add at least one ingredient.');
  }
}

function confirm_delete() {
  if(confirm("Are your sure you wish to delete?")) {
    return true;
  } else {
    return false;
  }
}

function pop_recipe(id) {
  var url = 'pf_recipe.php?i=' + id;
  var w = 600;
  window.open(url, 'recipeWin', 'status=1,location=1,toolbar=1,menubar=1,resizable=1,scrollbars=1,width=' + w + ',height=500');
}

function pop_recipe3x5(id) {
  var url = 'pf_recipe3x5.php?i=' + id;
  var w = 700;
  window.open(url, 'recipeWin', 'status=1,location=1,toolbar=1,menubar=1,resizable=1,scrollbars=1,width=' + w + ',height=500');
}

