/******************************************************************************/
/******************************************************************************/

var MENU_FN_FORM_NAME = "menu_footnote_form";


/******************************************************************************/
function menu_edit_fn(ctrl, item_id){
  
   var myAJAXParams = {
      method: "POST",
      url: "php/ajax/menu_item_ctrl.php",
      timeout_secs: 2,
      headers: [ 
      ],
      body: "action=Info" +
        "&" + "item_id" + "=" + escape(encodeURI(item_id)),
      onrequest: function myRequest(ajax){
         show_form_output(MENU_FN_FORM_NAME, "LOAD", "Loading:");
         hide_form(MENU_FN_FORM_NAME);
         LoadAndSelect("MenuFootnoteEditAdd");
      },
      onload: function myLoad(ajax){
         alert(ajax.response.Text);
         //alert(ajax.response.XML);
         
         if(ajax.response.JSON){
            var json = ajax.response.JSON;
            
            var input_note = document.getElementById("menu_fn_note");
            var input_price = document.getElementById("menu_fn_price_us");
            var input_group = document.getElementById("menu_fn_group");
            
            input_note.value = json.desc;
            if(json.price_us && json.price_us > 0){
               input_price.value = json.price_us;
            }
            set_select_by_id(input_group, json.group_id);
            
            
            // set the form action to now be Update instead of Add
            set_form_action(MENU_FN_FORM_NAME, "UpdateFN", "Update");
            
            var input_item_id = document.getElementById("menu_item_id");
            input_item_id.value = item_id;
            
            
            hide_form_output(MENU_FN_FORM_NAME);
            show_form(MENU_FN_FORM_NAME);
            
            update_fn_example();
            
         }else if(ajax.response.XML){
            // Dont support this any more
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "We nolonger support XML response :(");
         }else{
            // ERROR?
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "Response was invalid (JSON or XML)");
         }
                  
      },
      ontimeout: function myTimout(ajax){
         show_form_output(MENU_FN_FORM_NAME, "ERROR", "Request Timed Out, please try again");
      },
      onerror: function myError(ajax, infostr){
         if(ajax.response.StatusCode == 404){
            // file not found?
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "File \"" + myAJAXParams.url + "\" was not found on the server<br />\n(" + infostr + ")");
         }else{
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "Error: " + infostr + "(" + ajax.response.StatusCode + ")");
         }
      }
   };
   
   (new AJAXRequest()).request(myAJAXParams);


   

}



/******************************************************************************/
function validate_menu_fn_form(form){
   var input_note = document.getElementById("menu_fn_note");
   var input_price = document.getElementById("menu_fn_price_us");
   var input_group = document.getElementById("menu_fn_group");
   
   var input_action = getElementsByClassName(form, "form_action");
   var form_action = escape(encodeURI(input_action[0].value));
   
   var input_item_id = document.getElementById("menu_item_id");
   
   
   var valid_input = true;
   
   if(!input_note.value){
      show_form_input_invalid(input_note.parentNode, false, "");
      valid_input = false;
   }
   
   var price = false;
   if(input_price.value){
      price = parseFloat(input_price.value).toFixed(2);
      if(isNaN(price)){
         // ERROR
         show_form_input_invalid(input_price.parentNode, false, "");
         valid_input = false;
      }
   }
   
   if(!valid_input){
      return false;
   }
   
   
   
   var myAJAXParams = {
      method: "POST",
      url: form.action,
      timeout_secs: 2,
      headers: [ 
      ],
      body: "action=" + form_action + 
        "&" + "note" + "=" + escape(encodeURI(input_note.value)) +
        "&" + "price_us" + "=" + escape(encodeURI(price)) +
        "&" + "group" + "=" + escape(encodeURI(input_group.value)) +
        "&" + "item_id" + "=" + escape(encodeURI(input_item_id.value)),
      onrequest: function myRequest(ajax){
         show_form_output(MENU_FN_FORM_NAME, "LOAD", "Loading:");
      },
      onload: function myLoad(ajax){
         alert(ajax.response.Text);
         //alert(ajax.response.XML);
         
         if(ajax.response.JSON){
            var json = ajax.response.JSON;
            
            if(json.status){
               hide_form(MENU_FN_FORM_NAME);
               show_form_output(MENU_FN_FORM_NAME, "NOTICE", "Menu Footnote has been added/updated");
            }else{
               show_form_output(MENU_FN_FORM_NAME, "ERROR", "Status error, please retry");
            }
         }else if(ajax.response.XML){
            // Dont support this any more
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "We nolonger support XML response :(");
         }else{
            // ERROR?
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "Response was invalid (JSON or XML)");
         }
                  
      },
      ontimeout: function myTimout(ajax){
         show_form_output(MENU_FN_FORM_NAME, "ERROR", "Request Timed Out, please try again");
      },
      onerror: function myError(ajax, infostr){
         if(ajax.response.StatusCode == 404){
            // file not found?
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "File \"" + myAJAXParams.url + "\" was not found on the server<br />\n(" + infostr + ")");
         }else{
            show_form_output(MENU_FN_FORM_NAME, "ERROR", "Error: " + infostr + "(" + ajax.response.StatusCode + ")");
         }
      }
   };
   
   (new AJAXRequest()).request(myAJAXParams);
   
   return false;
}

/******************************************************************************/
function update_fn_example(){
   var example = document.getElementById("MenuFootnoteExample");
   
   //var footnotes = example.getElementsByClassName("menu_footnote");
   var footnotes = getElementsByClassName(example, "menu_footnote");
   var groups = getElementsByClassName(example, "menu_column_title");
   
   var input_note = document.getElementById("menu_fn_note");
   var input_price = document.getElementById("menu_fn_price_us");
   var input_group = document.getElementById("menu_fn_group");
   
   var price = (input_price.value && (input_price.value > 0))?"$" + parseFloat(input_price.value).toFixed(2):"";
   
   groups[0].innerHTML = input_group.options[input_group.selectedIndex].text;
   footnotes[0].innerHTML = "* " + input_note.value + " " + price + "<br />\n";
}

/******************************************************************************/
function clear_footnote_form(){
   var input_note = document.getElementById("menu_fn_note");
   var input_price = document.getElementById("menu_fn_price_us");
   var input_group = document.getElementById("menu_fn_group");
   
   input_note.value = "";
   input_price.value = "";
   
   update_fn_example();
}






