jQuery.noConflict();

var activeMenuId;
var cart;

jQuery(document).ready(function() {

  jQuery("#commentform").submit(function(){
  	var adrid = jQuery("#com_aid").val();
  	var nname = jQuery("#com_nick").val();
  	var email = jQuery("#com_email").val();
  	var favdish = jQuery("#com_favdish").val();
  	var head = jQuery("#com_headline").val();
  	var com = jQuery("#com_comment").val();
    jQuery.post("/includes/code/restcomment.php", {comment: com, aid:adrid, fdish:favdish, headline:head, comemail:email, nick:nname}, function(res) {
         if (res == "required") {
  		   alert("One or more required fields are missing.");
   	     } else if (res=="ok") {
    	   alert("Your comment has been submitted successfully! Thank you for your interest!");
           jQuery("#commentform").hide();
    	 } else {
  		   alert("Problem occurred while inserting error report. Please try again later");
         }
     });
    return false;
  });

  jQuery("#testimonialform").submit(function(){
  	var nname = jQuery("#tes_nick").val();
  	var email = jQuery("#tes_email").val();
  	var favrest = jQuery("#tes_favrest").val();
  	var head = jQuery("#tes_headline").val();
  	var com = jQuery("#tes_comment").val();
    jQuery.post("/includes/code/addtestimonial.php", {comment: com, frest:favrest, headline:head, tesemail:email, nick:nname}, function(res) {
         if (res == "required") {
  		   alert("One or more required fields are missing.");
   	     } else if (res=="ok") {
    	   alert("Your testimonial has been submitted successfully! Thank you for your interest!");
           jQuery("#testimonialform").hide();
    	 } else {
  		   alert("Problem occurred while inserting error report. Please try again later");
         }
     });
    return false;
  });

  jQuery("#reporterrorform").submit(function(){
  	var nname = jQuery("#err_nick").val();
  	var email = jQuery("#err_email").val();
  	var rid = jQuery("#err_restid").val();
  	var type = jQuery("#err_type").val();
  	var com = jQuery("#err_comment").val();
    jQuery.post("/includes/code/adderror.php", {comment: com, restid:rid, errtype:type, erremail:email, nick:nname}, function(res) {
         if (res == "required") {
  		   alert("One or more required fields are missing.");
   	     } else if (res=="ok") {
    	   alert("Your error report has been submitted successfully! Thank you for your interest!");
           jQuery("#reporterrorform").hide();
    	 } else {
  		   alert("Problem occurred while inserting error report. Please try again later");
         }
     });
    return false;
  });

  jQuery("#errorform").submit(function(){
	var nname = jQuery("#err_name").val();
	var email = jQuery("#err_email").val();
	var subject = jQuery("#err_subject").val();
	var msg = jQuery("#err_msg").val();
	jQuery.post("/includes/code/mysqlerror.php", {errmsg:msg, errname:nname, errsubject:subject, erremail:email}, function(res){
		if (res == "required"){
			alert('message field is empty');
		}
		else if (res == "ok"){
			alert('Your error report has been submitted successully! Thank you for sparing your time');
			jQuery("#errorform").hide();
		}
		else if(res == "error"){
			alert('Problem occurred while submitting the error report. Please try later');
		}
		else if(res == "yes"){
			alert('Problem with the php script');
		}
	 });
	return false;
  });

  jQuery("#menuselect").change(function(){
     updatemenusections();
  });

  if (location.pathname == "/restaurant_menu.html" || (location.pathname.substr(location.pathname.lastIndexOf('/')) == "/menu")) {
    updatemenusections();
    loadcart();
  }

  if (location.pathname == "/menu_checkout.html") {
    cartcheckout();
  }
 });

function updatemenusections() {
    activeAddrId =  jQuery("#addrid").val();
    activeMenuId =  jQuery("#menuselect").val();
    jQuery("#menusections").html("Loading menu...");
    jQuery("#sectionlinks").html("Loading menu...");

    jQuery.post("/includes/code/restaurant_menulinks.php", {menuid:activeMenuId}, function(res){
      jQuery("#sectionlinks").html(res);
    });
    jQuery.post("/includes/code/restaurant_menusection.php", {menuid:activeMenuId}, function(res){
      jQuery("#menusections").html(res);
      myLightbox.updateImageList();
    });
};

function loadcart() {
    jQuery("#cartcontent").html("Loading cart...");
    jQuery.post("/includes/code/restaurant_cart.php", {}, function(res){
      jQuery("#cartcontent").html(res);
    });
};



function addtocart(link) {
   activeAddrId =  jQuery("#addrid").val();
   jQuery("#cartcontent").html("Updating Cart...");
   var item;
   var option = -1;
   var command = 'add';
   item = link.id.substring(4);
   if(link.name!=link.id) {
      option = link.name.substring(link.id.length+1);
   }
   jQuery.post("/includes/code/restaurant_cart.php", {addrid: activeAddrId, menuid:activeMenuId, itemid:item, priceid:option, cmd:command}, function(res){
      jQuery("#cartcontent").html(res);
   });
   return false;
}

function remfromcart(link) {
   jQuery("#cartcontent").html("Updating Cart...");
   var item;
   var option = -1;
   var command = 'del';
   item = link.name.substring(12);
   if(link.name!=link.id) {
      option = link.id.substr(link.id.lastIndexOf('_')+1);
   }
   jQuery.post("/includes/code/restaurant_cart.php", {menuid:activeMenuId, itemid:item, priceid:option, cmd:command}, function(res){
      jQuery("#cartcontent").html(res);
   });
   return false;
}

function updatecart(link) {
   var key = link.id.substring(12);
   var valueinput = document.getElementById("cartitem_"+key);
   var itemvalue = valueinput.value;
   jQuery("#cartcontent").html("Updating Cart...");
   var item;
   var option = -1;
   var command = 'upd';
   item = link.name.substring(12);
   if(link.name!=link.id) {
      option = link.id.substr(link.id.lastIndexOf('_')+1);
   }

   jQuery.post("/includes/code/restaurant_cart.php", {menuid:activeMenuId, itemid:item, priceid:option, cmd:command, val:itemvalue}, function(res){
      jQuery("#cartcontent").html(res);
   });
   return false;
}

function cartcheckout() {
    jQuery("#menucheckout").html("Loading cart...");

    jQuery.post("/includes/code/restaurant_cartdetails.php", {}, function(res){
      jQuery("#menucheckout").html(res);
    });
    return false;
}

function controloption(inp) {
   var key = inp.id.substring(7);
   var price = document.getElementById("optprice_"+key).value;
   //alert (key + '/' + price);
}

function sendcartform() {
  var crtform = jQuery("#cartform").serialize();
  jQuery.post("/includes/code/restaurant_cartdetails.php", crtform, function(res){
      jQuery("#menucheckout").html(res);
    });
  return false;
}

function backtomenu() {
    updatemenusections();
    return false;
}

function backtoselection() {
    jQuery.post("/includes/code/restaurant_cartdetails.php", {}, function(res){
      jQuery("#menucheckout").html(res);
    });
    return false;
}

function removeitems() {
   jQuery("#cartcontent").html("Updating Cart...");
   var command = 'remall';

   jQuery.post("/includes/code/restaurant_cart.php", {cmd:command}, function(res){
      jQuery("#cartcontent").html(res);
   });
   return false;
}

