var myVariables = "";
var arr = new Object();
var group_name_arr = new Object();

var ItemsModal = new Class(
{
   items_id:0,

   Implements: [Options, Events],
   
   options:
   {
      id: 'items',
      className: 'generic_dialog',
      topOffset: 125,
      width: 532,
      onShow: function()
      {
	this.container.setStyle('display', 'block');
      },
      onHide: function()
      {
         this.container.setStyle('display', 'none');
      }
   },

   container: {},
   bkg:{},
   initialize: function(options)
   {
      this.setOptions(options);
      // Create and inject the modal box
      this.bkg = new Element('div',
      {
	  'class': 'generic_dialog_background',
	  'styles': { 'display': 'block', 'visibility': 'hidden' }
      }).setStyle('opacity', 0);
      
      this.container = new Element('div',
      {
         'class': this.options.className,
         'id': this.options.id,
         'styles': { 'display': 'block', 'visibility': 'hidden' }
      }).adopt(
         new Element('div',
         {
            'class': 'generic_dialog_popup',
            'styles': { 'top': this.options.topOffset + 'px' }
         }).adopt(
            new Element('table',
            {
               'class': 'pop_dialog_table',
               'styles': { 'width': this.options.width + 'px' }
            }).adopt(
               new Element('tbody').adopt(
                  new Element('tr').adopt(
                     new Element('td', { 'class': 'pop_topleft' }),
                     new Element('td', { 'class': 'pop_border pop_top' }),
                     new Element('td', { 'class': 'pop_topright' })
                  ),
                  
                  new Element('tr').adopt(
                     new Element('td', { 'class': 'pop_border pop_side' }),
                     new Element('td', { 'class': 'pop_content' }).adopt(
                        new Element('h2', { 'class': 'dialog_title' }).adopt(
                           new Element('span')
                        ),
                        
                        new Element('div', { 'class': 'dialog_content' }).adopt(
                           new Element('div', { 'class': 'dialog_summary' }),
                           new Element('div', { 'class': 'dialog_body1' }),
                           new Element('div', {'class': 'dialog_buttons' }).adopt(
                        	   new Element('input',
                        	   {
                        	       'type': 'button',
                        	       'value': 'Continue',
                        	       'name': 'close',
                        	       'class': 'inputsubmit',
                        	       'id': this.options.id + '-close1'
                        	   }),
                        	   new Element('input',
                                   {
                        	       'type': 'button',
                        	       'value': 'No, Thank You',
                        	       'name': 'close',
                        	       'class': 'inputsubmit',
                        	       'id': this.options.id + '-close2'
                        	   })
                           )
                        )
                     ),
                     new Element('td', { 'class': 'pop_border pop_side' })
                  ),
                  
                  new Element('tr').adopt(
                     new Element('td', { 'class': 'pop_bottomleft' }),
                     new Element('td', { 'class': 'pop_border pop_bottom' }),
                     new Element('td', { 'class': 'pop_bottomright' })
                  )
               )
            )
         )
      ).setStyle('opacity', 0);
      
      this.bkg.inject(document.body);
      this.container.inject(document.body);
      // Add some events
      $(this.options.id + '-close1').addEvent('click', function() { this.hide(); }.bind(this));
      $(this.options.id + '-close2').addEvent('click', function() { this.hide(); }.bind(this));
      window.addEvent('keypress', function(e) { if (e.key == 'esc') this.hide(); }.bind(this));
      $(document.body).addEvent('click', function(e)
      {
         if (this.isOpen() && !e.target.getParent('.generic_dialog')) { this.hide(); }
      }.bind(this));
      
      return this;
   },


   show: function()
   {
       this.bkg.setStyle('opacity',0.5);
       this.bkg.setStyle('visibility','visible');
       this.bkg.setStyle('display', 'block');
       
       $('review_ok').setStyle('display','none');
       $('review_non').setStyle('display','inline');
       this.fireEvent('show');

       //$('review_non').inject($('review'));
       return this;
   },
   
   hide: function()
   {
       this.bkg.setStyle('opacity',0);
       this.bkg.setStyle('visibility','hidden');
       this.bkg.setStyle('display', 'none');
       this.fireEvent('hide');
       this.container.getElement('.dialog_body1').empty();
       var its= this.items_id;
       var suggested_items = its.split(",");
       for(var p=0; p<suggested_items.length; p++)
       {
    	   if($('customize_'+suggested_items[p]))
    	   $('customize_'+suggested_items[p]).setStyle('display', 'inline');
       }
       
       $('continue').click();
       return this;
   },
   
   setContent: function(bodyHtml)
   {
      this.setTitle('We suggest these items with your food.').setSummary('').setBody(bodyHtml);
      return this;
   },
   
   setTitle: function(titleText)
   {
      this.container.getElement('.dialog_title span').set('text', titleText);
      return this;
   },
   
   setSummary: function(summaryHtml)
   {
      this.container.getElement('.dialog_summary').set('html', summaryHtml);
      return this;
   },
   
   setBody: function(bodyHtml)
   {
       this.items_id =bodyHtml;
       var ar = $$('.item_suggested');// $('item_div_'+this.item_id).getElements('input');
	 	  
       var suggested_items = bodyHtml.split(",");
       for(var p=0; p<suggested_items.length; p++)
       {
    	   if($('customize_'+suggested_items[p]))
    	       $('customize_'+suggested_items[p]).setStyle('display', 'none');
    	   //$('item_details_'+suggested_items[p]).setStyle('display', 'block').setStyle('visibility', 'visible');
    	   if($('item_suggested_div_'+suggested_items[p]))
    	       $('item_suggested_div_'+suggested_items[p]).clone().inject(this.container.getElement('.dialog_body1'));
       } 
      return this;
   },
   
   isOpen: function()
   {
      return (this.container.getStyle('opacity') == '0') ? false : true;
   }
   
   
});


var ItemOptionsModal = new Class(
{
   item_id:0,

   Implements: [Options, Events],
   
   options:
   {
      id: 'item_option',
      className: 'generic_dialog',
      topOffset: 0,
      width: 532,
      onShow: function()
      {
	 this.container.setStyle('display', 'block');
      },
      onHide: function()
      {
         this.container.setStyle('display', 'none');
      }
   },

   container: {},
   bkg:{},
   initialize: function(options)
   {
      this.setOptions(options);
      // Create and inject the modal box
      this.bkg = new Element('div',
      {
	  'class': 'generic_dialog_background',
	  'styles': { 'display': 'block', 'visibility': 'hidden' }
      }).setStyle('opacity', 0);
      
      this.container = new Element('div',
      {
         'class': this.options.className,
         'id': this.options.id,
         'styles': { 'display': 'block', 'visibility': 'hidden' }
      }).adopt(
         new Element('div',
         {
            'class': 'generic_dialog_popup',
            'styles': { 'top': this.options.topOffset + 'px' }
         }).adopt(
            new Element('table',
            {
               'class': 'pop_dialog_table',
               'styles': { 'width': this.options.width + 'px' }
            }).adopt(
               new Element('tbody').adopt(
                  new Element('tr').adopt(
                     new Element('td', { 'class': 'pop_topleft' }),
                     new Element('td', { 'class': 'pop_border pop_top' }),
                     new Element('td', { 'class': 'pop_topright' })
                  ),
                  
                  new Element('tr').adopt(
                     new Element('td', { 'class': 'pop_border pop_side' }),
                     new Element('td', { 'class': 'pop_content' }).adopt(
                        new Element('h2', { 'class': 'dialog_title' }).adopt(
                           new Element('span')
                        ),
                        
                        new Element('div', { 'class': 'dialog_content' }).adopt(
                           new Element('div', { 'class': 'dialog_summary' }),
                           new Element('div', { 'class': 'dialog_body' }),
                           new Element('div', {'class': 'dialog_buttons' }).adopt(
                        		   new Element('input',
                                           {
                                              'type': 'button',
                                              'value': 'Add to Cart',
                                              'name': 'add',
                                              'class': 'inputsubmit',
                                              'id': this.options.id + '-add'
                                           }),
                                           new Element('input',
                                                   {
                                                      'type': 'button',
                                                      'value': 'Close',
                                                      'name': 'close',
                                                      'class': 'inputsubmit',
                                                      'id': this.options.id + '-close'
                                                   })
                              
                           )
                        )
                     ),
                     new Element('td', { 'class': 'pop_border pop_side' })
                  ),
                  
                  new Element('tr').adopt(
                     new Element('td', { 'class': 'pop_bottomleft' }),
                     new Element('td', { 'class': 'pop_border pop_bottom' }),
                     new Element('td', { 'class': 'pop_bottomright' })
                  )
               )
            )
         )
      ).setStyle('opacity', 0);
      
      this.bkg.inject(document.body);
      this.container.inject(document.body);
      // Add some events
      $(this.options.id + '-add').addEvent('click', function() 
      {
    	  var options_count = 0;
    	  var inst='';
    	  var myVariables = '';
    	  var cost=0;
    	  var options_ids='';
    	  var ctg_options_ids='';
    	  var ar = $$('.item_chk');// $('item_div_'+this.item_id).getElements('input');
    	  group_name_arr = new Object();
    	  mandatory_group_name_arr = [];
    	  
    	  $del = '';
    	  tiv2 = this.item_id;
    	  ar.each(function(item)
          {
    	      tiv1 = Number(item.get('item_id'));
    	      if(tiv1 == tiv2)
    	      {
    		  if(item.get('mandatory')=='1')
    		  {
    		      found = false;
    		      for(var t = 0; t < mandatory_group_name_arr.length; t++)
    		      {
    			  if(mandatory_group_name_arr[t] == item.get('group_name').replace(/^\s*([\S\s]*?)\s*$/, '$1'))
    			  {
    			      found = true;
    			      break;
    			  }
    		      }
    		      if(!found)
    			  mandatory_group_name_arr.push(item.get('group_name').replace(/^\s*([\S\s]*?)\s*$/, '$1'));
    		  }
    	      }
    	  });
    	  
    	  ar.each(function(item)
    	  {
    	      if(item.checked)
    	      {
    		  inst = '';
    		  if( typeof group_name_arr[item.get('group_name')] == 'undefined' )
    		  {
    		      group_name_arr[item.get('group_name')]=[];
    		  }
    		  if(item.get('cost')!='0')
    		  {
    		      cost += Number(item.get('cost'));
    		      var x = Number(item.get('cost'));
    		      var result = x.toFixed(2);
    		      inst+=item.get('name')+' ($'+result+')';
    		  }
    		  else
    		      inst+=item.get('name');
    		  inst=escape(inst);
    		  num = group_name_arr[item.get('group_name')].push(inst);
    		  for(var p = 0; p < mandatory_group_name_arr.length; p++)
    		  {
    		      if(mandatory_group_name_arr[p] == item.get('group_name').replace(/^\s*([\S\s]*?)\s*$/, '$1'))
    		      {
    			  mandatory_group_name_arr.splice(p, 1);
    		      }
    		  };
    		  if(item.get('is_category_option')=='1')
    		      ctg_options_ids += item.get('option_id')+',';
    		  else
    		      options_ids += item.get('option_id')+',';
    		
    		
    	      }
    	      
    	  });
    	  
    	  var $err_message = '';
    	  if(mandatory_group_name_arr.length > 0)
    	  {
    	      for(var p = 0; p < mandatory_group_name_arr.length; p++)
    	      {
    		  $err_message = $err_message+"PLEASE SELECT "+mandatory_group_name_arr[p]+"\n";
    	      }
    	      alert($err_message);
    	      return;
    	  }
    	  
    	  ar.each(function(item)
    	  {
    	      if(item.checked)
    	      {
    		  item.checked=false;
    	      }
    	  });
    	  
    	  inst = '';
    	  for(var i in (group_name_arr))
    	  {
    	      inst += i+": ";
    	      for(j=0;j<group_name_arr[i].length; j++)
    		  inst += group_name_arr[i][j]+", ";
    	      inst += "<br />";
    	  }
    	  
    	  if($('item_details_w_'+this.item_id).get('value') == 'Please type the options here.')
    	      $('item_details_w_'+this.item_id).set('value', '');
    	  if($('item_details_w_'+this.item_id).get('value') != '')
    	      inst+='Instructions: ';
    	  
    	  inst+=$('item_details_w_'+this.item_id).get('value');
    	inst=escape(inst);
    	  /*
    	  if(inst=='')
    	  {
    	      var answer = confirm ("You have not selected an Option. If you would like to select an Option Click OK. Otherwise Click CANCEL to Proceed.");
    	      if (answer)
    	          return;
    	  }
    	  */
    	  
    	  if( typeof arr[this.item_id] == 'undefined' )
    	  {
    	      arr[this.item_id]=[];
    	  }
    	  
    	  var i = 0;
    	  var founded=false;
    	  for(i = 0; i < arr[this.item_id].length; i++)
    	  {
    	      if(arr[this.item_id][i]==inst)
    	      {
    		  length11=arr[this.item_id].length-1;
    		  num=Number($('qty_'+this.item_id).getSelected().get('value'));
    		  $('item_count_0_'+this.item_id).set('value', num);
    		  myVariables +="item_n_"+this.item_id+"="+length11+"&"+
    		  "item_count_"+i+"_"+this.item_id+"="+$('item_count_0_'+this.item_id).get('value')+"&"+
    		  "item_cost_"+i+"_"+this.item_id+"="+cost+"&"+
    		  "item_ctg_options_id_"+i+"_"+this.item_id+"="+ctg_options_ids+"&"+
    		  "item_options_id_"+i+"_"+this.item_id+"="+options_ids+"&"+
  		  "item_details_w_"+i+"_"+this.item_id+"="+inst+"&";
    		  founded=true;
    	      }
    	  }
    	  if(!founded)
    	  {
    	      arr[this.item_id].push(inst);
    	      $('item_count_0_'+this.item_id).set('value',$('qty_'+this.item_id).getSelected().get('value'));
    	      myVariables +="item_n_"+this.item_id+"="+i+"&"+
    	      "item_count_"+i+"_"+this.item_id+"="+$('item_count_0_'+this.item_id).get('value')+"&"+
    	      "item_cost_"+i+"_"+this.item_id+"="+cost+"&"+
    	      "item_ctg_options_id_"+i+"_"+this.item_id+"="+ctg_options_ids+"&"+
    	      "item_options_id_"+i+"_"+this.item_id+"="+options_ids+"&"+
	      "item_details_"+i+"_"+this.item_id+"="+inst+"&";
    	  }
    	  
    	  $('item_details_w_'+this.item_id).set('value','');
    	  delete group_name_arr;
    	  
    	  //myVariables +='options_id='+options_ids;
    	  
    	  //alert(myVariables);
    	  
    	 // hiframe.location="orderoutside.php?"+myVariables;
    	  
    	 	//update();
		new Request({
		    noCache:true,
		    url:'order_server.php',
		    onRequest: function(){
		   // alert('ctgopt='+ctg_options_ids);////////////////
		    stopCount();
		    $('loader').style.display = 'inline';
		    },
		    onSuccess: function(responseText){
		
		        document.getElementById('screen').value =responseText;
		        $('loader').style.display = 'none';
		        doTimer();
		    },
		}).post('item_id='+this.item_id+'&ctg_options_id='+ctg_options_ids+'&options_id='+options_ids+'&item_count='+$('item_count_0_'+this.item_id).get('value')+'&item_details='+inst);
		
          
    	  this.hide(); 
      }.bind(this));
      
      
      $(this.options.id + '-close').addEvent('click', function() { this.hide(); }.bind(this));
      window.addEvent('keypress', function(e) { if (e.key == 'esc') this.hide(); }.bind(this));
      $(document.body).addEvent('click', function(e)
      {
         if (this.isOpen() && !e.target.getParent('.generic_dialog')) { this.hide(); }
      }.bind(this));
      
      return this;
   },
   
   show: function()
   {
       this.bkg.setStyle('opacity',0.5);
       this.bkg.setStyle('visibility','visible');
       this.bkg.setStyle('display', 'block');
       
	  this.fireEvent('show');
      return this;
   },
   
   hide: function()
   {
	   this.bkg.setStyle('opacity',0);
       this.bkg.setStyle('visibility','hidden');
       this.bkg.setStyle('display', 'none');
	   //$('item_div_'+this.item_id).grab(this.container.getElement('.dialog_body'));
       var ar = $$('.item_chk');
       ar.each(function(item)
    	    	  {
    	    	    if(item.checked)
    	    	    {
    	    	    	item.checked=false;
    	    	    }
    	    	  });
       
	  this.container.getElement('.dialog_body').clone(true, true).inject($('item_div_'+this.item_id));
	  
	  this.container.getElement('.dialog_body').empty();
	  $(this.item_id).setStyle('visibility', 'collapse');
	  $(this.item_id).setStyle('display', 'none');
	  this.fireEvent('hide');
      return this;
   },
   
   setContent: function(titleText, summaryHtml, bodyHtml)
   {
      this.setTitle(titleText).setSummary(summaryHtml).setBody(bodyHtml);
      return this;
   },
   
   setTitle: function(titleText)
   {
      this.container.getElement('.dialog_title span').set('text', titleText);
      return this;
   },
   
   setSummary: function(summaryHtml)
   {
      this.container.getElement('.dialog_summary').set('html', summaryHtml);
      return this;
   },
   
   setBody: function(bodyHtml)
   {
	  this.item_id =bodyHtml;
	  $(bodyHtml).setStyle('visibility', 'visible');
	  $(bodyHtml).setStyle('display', 'block');
	  
	  //$(bodyHtml).clone(true, true).inject(this.container.getElement('.dialog_body'));
	  
	  this.container.getElement('.dialog_body').grab($(bodyHtml));
      return this;
   },
   
   isOpen: function()
   {
      return (this.container.getStyle('opacity') == '0') ? false : true;
   }
});


var container;
var t;
var timer_is_on=0;
function update()
{
    new Request({
	noCache:true,
	url:'order_server.php',
        onRequest: function()
        {
	if($('vertical_slide')!=null)
	{
	    $('vertical_slide').empty();
	    $('vertical_slide').destroy();
	}
	},
        onSuccess: function(responseText)
        {
            $('screen').set('html',responseText);
            if($('message').get('text')!='')
            {
        	//console.debug("Message a galis t = "+t);
        	
        	container = new Element('div',{
        	'id': 'vertical_slide',
        	'style':'background: #F3F1F1; color: #000000; padding:5px; border: 1px solid #ff0000; font: 16px Verdana, Arial, Helvetica, sans-serif;'
        	    }).set('text', $('message').get('text'));
            container.inject($('slide'));
            
            var myVerticalSlide = new Fx.Slide('vertical_slide').hide();
            new Fx.Morph('v_toggle').start().chain(function()
        	    {
        	stopCount();
        	      myVerticalSlide.toggle();
        	      this.start.delay(3000, this); }).chain(function()
        	      {
        		  myVerticalSlide.toggle();
        		  doTimer();
    	   	       });
            }
            //$('loader').style.display = 'none';
            
        },
        onFailure: function()
        {
        }
        }).post();
        //mytimeout=setTimeout('update()', 5000);
        //
    t=setTimeout("update()",2600);
    //console.debug("timeouty ashxatum a t = "+t);
}
function doTimer()
{
    //console.log("DBG: doTimer()ashxatec");
if (!timer_is_on)
  {
  timer_is_on=1;
  update();
  }
}
function stopCount()
{
    //console.log("DBG: stopTimer()ashxatec");
clearTimeout(t);
timer_is_on=0;
}
function alert_message(message)
{
	    var container = new Element('div',
		{
	          'id': 'vertical_slide',
	          'style':'background: #F3F1F1; color: #000000; padding: 5px; border: 1px solid #D0C8C8; font: 16px Verdana, Arial, Helvetica, sans-serif;'
	        }).set('text', message);
	        container.inject($('slide'));
		var myVerticalSlide = new Fx.Slide('slide').hide();
		new Fx.Morph('v_toggle').start().chain(function(){myVerticalSlide.toggle(); this.start.delay(3000, this); }).chain(function(){myVerticalSlide.toggle(); });
}
function Delivery(id)
{  //alert("Delivery = "+id);
    new Request({
	noCache:true,
	    url:'order_server.php',
	    onRequest: function(){
	       stopCount();
	    },
	    onSuccess: function(responseText){
	
	        document.getElementById('screen').value =responseText;
	        doTimer();
	    },
	}).post('delivery_rate='+id);
}
function setCoupon(pord,str)
{
    if (str=="")
    {
      document.getElementById("txtHint_"+pord).innerHTML="";
      return;
    }
    new Request({
	noCache:true,
	url:'checkcoupon.php',
	onRequest: function(){
	stopCount();
	},
	onSuccess: function(responseText)
	{
	    document.getElementById("txtHint_"+pord).innerHTML =responseText;
	    doTimer();
	},
    }).post('coupon='+str);
}
function Delete(id)
{  
    new Request({
	noCache:true,
	url:'order_server.php',
	    onRequest: function(){
	      //console.debug("timeoutic araj"+t);
	      stopCount();
	      //console.debug("timeoutic heto");
	      $('loader').style.display = 'inline';
	    },
	    onSuccess: function(responseText){
		$('loader').style.display = 'none';
		doTimer();
		//console.debug("taza timeout = "+t);
		document.getElementById('screen').value =responseText;
	    },
	}).post('del='+id);
	
}
function Delete_client(id)
{  
    //alert("delete client = "+id);
    var answer = confirm('Are you sure you want to delete order?')
    if (answer)
    {
	new Request({
	    noCache:true,
	    url:'order_server.php',
	    onRequest: function(){
    	    	stopCount();
    	    	$('loader').style.display = 'inline';
	    },
	    onSuccess: function(responseText){
	
	        document.getElementById('screen').value =responseText;
	        $('loader').style.display = 'none';
	        doTimer();
	    },
	}).post('del_cl='+id);
    }
}
function Done(session_id)
{   //alert("done = "+session_id);
    var answer = confirm('Your order will be marked complete now. You cannot modify your order once submit. Please confirm.')
    if (answer)
    {
    new Request({
	noCache:true,
	    url:'order_server.php',
	    onRequest: function(){
		stopCount();
		$('loader').style.display = 'inline';
	    },
	    onSuccess: function(responseText){
	
	        document.getElementById('screen').value =responseText;
	        $('loader').style.display = 'none';
	        doTimer();
	    },
	}).post('done='+session_id);
    }
	
}
function Leave(session_id)
{   //alert("leave = "+session_id);
    var answer = confirm('Are you sure you want to leave order?')
    if (answer)
    {
        new Request({
            noCache:true,
	    url:'order_server.php',
	    onRequest: function(){
            	stopCount();
            	$('loader').style.display = 'inline';
	    },
	    onSuccess: function(responseText){
	
	        document.getElementById('screen').value =responseText;
	        $('loader').style.display = 'none';
	        doTimer();
	    },
	}).post('leave='+session_id);
    }
	
}

userAgentLowerCase = navigator.userAgent.toLowerCase();
function resizeTextarea(t) 
{
    if ( !t.initialRows ) t.initialRows = t.rows;
   
    a = t.value.split('\n');
    b=0;
    for (x=0; x < a.length; x++) {
      if (a[x].length >= t.cols) b+= Math.floor(a[x].length / t.cols);
    }
   
    b += a.length;
   
    if (userAgentLowerCase.indexOf('opera') != -1) b += 2;
   
    if (b > t.rows || b < t.rows)
      t.rows = (b < t.initialRows ? t.initialRows : b);
  }
  
function clickButton(e, buttonid)
{
    var bt = document.getElementById(buttonid);
    if (typeof bt == 'object')
    {
	if(navigator.appName.indexOf("Netscape")>(-1))
        {
            if (e.keyCode == 13)
            {
                alert(buttonid);
                bt.click();
                return false;
	    }
	}
	if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
  	{
	    if (event.keyCode == 13)
	    {
		alert(buttonid);
		bt.click();
		return false;
	    }
        }
    }
} 


function setVisibilityButtons(id, visibility) 
{
    document.getElementById(id).style.display = visibility;
    document.getElementById(id).style.visibility = "visible";
}
function setClassButtons(id, myclassname) 
{
    document.getElementById(id).className = myclassname;
    document.getElementById(id).style.visibility = "visible";	 
}



function showTextbox(item_id, item_details, item_description)
{
    document.getElementById(item_id).style.display = 'inline';
    document.getElementById(item_id).style.visibility = "visible";
}

function showTable(item_id, item_details, item_description)
{
    var inst = '';
    if(document.getElementById('item_details_'+item_id).value == 'Please type the options here.')
	document.getElementById('item_details_'+item_id).value = '';
    if(document.getElementById('item_details_'+item_id).value != '')
	inst+='Instructions: ';
    
    if(item_details=='1')
    {
	if(document.getElementById('item_details_'+item_id).value=='')
	{
	    // Create a regular expression to search all +s in the string
	    var lsRegExp = /\+/g;
	    // Return the decoded string
	    tex = unescape(String(item_description).replace(lsRegExp, " "));
	    alert("Please Specify the options.\n"+tex);
	    return;
	}
    }
    
    if( typeof arr[item_id] == 'undefined' )
    {
	arr[item_id]=[];
    }
    
    var i = 0;
    var founded=false;
    for(i = 0; i < arr[item_id].length; i++)
    {
	if(arr[item_id][i]==document.getElementById('item_details_'+item_id).value)
	{
	    length11=arr[item_id].length-1;
	    document.getElementById('item_count_0_'+item_id).value = Number(document.getElementById('item_count_0_'+item_id).value) + 1;
	    myVariables +="item_n_"+item_id+"="+length11+"&"+
	    "item_count_"+i+"_"+item_id+"="+document.getElementById('item_count_0_'+item_id).value+"&"+
	    "item_details_"+i+"_"+item_id+"="+inst+escape(document.getElementById('item_details_'+item_id).value)+"&";
	    founded=true;
	}
    }
    if(!founded)
    {
	arr[item_id].push(document.getElementById('item_details_'+item_id).value);
	document.getElementById('item_count_0_'+item_id).value = 1;
	myVariables +="item_n_"+item_id+"="+i+"&"+
	"item_count_"+i+"_"+item_id+"="+document.getElementById('item_count_0_'+item_id).value+"&"+
	"item_details_"+i+"_"+item_id+"="+inst+escape(document.getElementById('item_details_'+item_id).value)+"&";
    }
    
    
    new Request({
	noCache:true,
	    url:'order_server.php',
	    onRequest: function(){
	      //  alert('onRequest');
	    },
	    onSuccess: function(responseText){
	
	        document.getElementById('screen').value =responseText;
	    },
	}).post('item_id='+item_id+'&item_count='+document.getElementById('item_count_0_'+item_id).value+'&item_details='+inst+escape(document.getElementById('item_details_'+item_id).value));
	
    //hiframe.location="orderoutside.php?"+myVariables;
}

function showReorderTable(item_id,item_count,item_options_id,item_description)
{
    var ar = $$('.item_chk');
    var mySplitResult = item_options_id.split(",");
    var fresh_options_ids = '';
    group_name_arr = new Object();
    cost = 0;
    var inst = '';
   
    var i = 0;
    
	for(i = 0; i < mySplitResult.length; i++)
	{
	    ar.each(function(item)
            {
		if(item.get('option_id') == mySplitResult[i])
		{
		    inst = '';
		    if( typeof group_name_arr[item.get('group_name')] == 'undefined' )
		    {
			group_name_arr[item.get('group_name')]=[];
		    }
	    		  
		    //alert('found');
		    if(item.get('cost')!='0')
		    {
			cost += Number(item.get('cost'));
			var x = Number(item.get('cost'));
			var result = x.toFixed(2);
			inst+=item.get('name')+' ($'+result+')';
		    }
		    else
			inst+=item.get('name');
		    num = group_name_arr[item.get('group_name')].push(inst);
		    fresh_options_ids += item.get('option_id')+',';
		}
	    });
	}
	
	inst = '';
  	for(var i in (group_name_arr))
  	{
  	    inst += i+": ";
  	    for(j=0;j<group_name_arr[i].length; j++)
  		inst += group_name_arr[i][j]+", ";
  	    inst += "<br />";
  	}
  	
	
	if( typeof arr[this.item_id] == 'undefined' )
	{
	    arr[this.item_id]=[];
	}
	
	var i = 0;
	var founded=false;
	for(i = 0; i < arr[this.item_id].length; i++)
	{
	    if(arr[this.item_id][i]==inst)
	    {
  		  length11=arr[this.item_id].length-1;
  		  num=Number($('qty_'+this.item_id).getSelected().get('value'));
  		  $('item_count_0_'+this.item_id).set('value', num);
  		  myVariables +="item_n_"+this.item_id+"="+length11+"&"+
  		  "item_count_"+i+"_"+this.item_id+"="+item_count+"&"+
  		  "item_cost_"+i+"_"+this.item_id+"="+cost+"&"+
  		  "item_options_id_"+i+"_"+this.item_id+"="+fresh_options_ids+"&"+
  		  "item_details_w_"+i+"_"+this.item_id+"="+inst+"&";
  		  founded=true;
  	    }
  	}
	if(!founded)
	{
	    arr[this.item_id].push(inst);
  	    //arr[item_id].push(item_description);
	    document.getElementById('item_count_0_'+item_id).value = item_count;
	    myVariables += "item_n_"+item_id+"="+i+"&"+
  		"item_count_"+i+"_"+item_id+"="+item_count+"&"+
  		"item_cost_"+i+"_"+item_id+"="+cost+"&"+
  		"item_options_id_"+i+"_"+item_id+"="+fresh_options_ids+"&"+
  		"item_details_"+i+"_"+item_id+"="+inst+"&";
  	}
  	  
	delete group_name_arr;
	hiframe.location="orderoutside.php?"+myVariables;
}

function AAA(item_id,sub_id)
{
    if(Number(document.getElementById('item_count_'+sub_id+'_'+item_id).value)!=0)
	  {
		  document.getElementById('item_count_'+sub_id+'_'+item_id).value = Number(document.getElementById('item_count_'+sub_id+'_'+item_id).value) - 1;
		  myVariables += 'item_count_'+sub_id+'_'+item_id+"="+document.getElementById('item_count_'+sub_id+'_'+item_id).value+"&";
        hiframe.location="orderoutside.php?"+myVariables;
    }
}

function removeTable(item_id,sub_id)
{
    if(Number(document.getElementById('item_count_'+sub_id+'_'+item_id).value)!=0)
	  {
  	  document.getElementById('item_count_'+sub_id+'_'+item_id).value = Number(document.getElementById('item_count_'+sub_id+'_'+item_id).value) - 1;
		  myVariables += 'item_count_'+sub_id+'_'+item_id+"="+document.getElementById('item_count_'+sub_id+'_'+item_id).value+"&";
        hiframe.location="orderoutside.php?"+myVariables;
    }
}

/* mootools */
function urldecode (str)
{
    return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}


window.addEvent("domready",function()
{
	  if(!$("cart"))
	  {
		  return 
	  }
	  var C=$("cart_follow");
	  if(!C)
	  {
		  return arguments.callee.delay(500)
	  }
	  if(!Browser.Engine.trident4&&C)
	  {
		  var A=new Fx.Tween(C,{
								  property:"margin-top",
								  transition:Fx.Transitions.Quad.easeInOut,
								  duration:1000,
								  link:"cancel"
				  });
		  var B=0;
		  var D=0;
		  window.addEvent("scroll",function()
			{
			  var F=$("cart");
			  var E={size:window.getSize(),scroll:window.getScroll()};
			  var G=F.getPosition();
			  if(!D)
			  {
				  D=G.y;
			  }
			  var H=F.getCoordinates();
			  if(!noFollow)
			  {
				  if(E.size.y<H.height)
				  {
					  if(B>E.scroll.y)
					  {
						  A.start((E.scroll.y-D+10).limit(0,10000000));
					  }
					  else
					  {
						  A.start(((E.scroll.y+E.size.y)-(D+H.height)-10).limit(0,10000000));
					  }
				  }
				  else
				  {
					  A.start((E.scroll.y-D+10).limit(0,10000000));
				  }
			  }
			  B=E.scroll.y;
			})
	  }
	});

    var noFollow=false;
    function toggleFollow(A)
    {
    	  if(!$("follow_toggler"))
    	  {
    		  return;
    	  }
    	  if(A)
    	  {
    		  noFollow=!noFollow;
    	  }
    	  if(noFollow)
    	  {
    		  $("follow_toggler").set("html","Follow me!");
    	  }
    	  else
    	  {
    		  $("follow_toggler").set("html","Don't follow me!");
    	  }
    }
    
    
    var modal1 = 0;

    var sugg_count=0;
    function suggested(body)
    {
    	sugg_count++;
    	modal1.setContent(urldecode(body));
    	if($('review'))
    		$('review').fireEvent('click');
    }


    window.addEvent('domready', function()
    {
    	modal1 = new ItemsModal(
    	{
    		id: 'primeModal',
    		
    		onShow: function()
    		{
    			this.container.setStyle('display', 'block').fade(1);
    		},
    		onHide: function()
    		{
    			(function()
    			{
    				this.setStyle('display', 'none');
    			}).delay(0, this.container.fade(0));
    		}
    	});
    	if($('review'))
    		$('review').addEvent('click', function() { modal1.show(); });
    });
   

    var modal = 0;

    function wind(title, summary, body)
    {
    	modal.setContent( urldecode(title) , urldecode(summary), urldecode(body));
    	if($('open'))
    	$('open').fireEvent('click');
    }

    
    
    
    var add_edit_menu_modal = new Class(
		{
		   id:0,
		   opt:'',

		   Implements: [Options, Events],
		   
		   options:
		   {
		      id: 'add_edit_menu_id',
		      className: 'generic_dialog',
		      topOffset: 0,
		      width: 610,
		      onShow: function()
		      {
				 this.container.setStyle('display', 'block');
		         
		      },
		      onHide: function()
		      {
		         this.container.setStyle('display', 'none');
		      }
		   },

		   container: {},
		   bkg:{},
		   initialize: function(options)
		   {
		      this.setOptions(options);
		      // Create and inject the modal box
		      this.bkg = new Element('div',
		    	      {
		    	         'class': 'generic_dialog_background',
		    	         'styles': { 'display': 'block', 'visibility': 'hidden' }
		    	      }).setStyle('opacity', 0);
		      
		      this.container = new Element('div',
		      {
		         'class': this.options.className,
		         'id': this.options.id,
		         'styles': { 'display': 'block', 'visibility': 'hidden' }
		      }).adopt(
		         new Element('div',
		         {
		            'class': 'generic_dialog_popup',
		            'styles': { 'top': this.options.topOffset + 'px' }
		         }).adopt(
		            new Element('table',
		            {
		               'class': 'pop_dialog_table',
		               'styles': { 'width': this.options.width + 'px' }
		            }).adopt(
		               new Element('tbody').adopt(
		                  new Element('tr').adopt(
		                     new Element('td', { 'class': 'pop_topleft' }),
		                     new Element('td', { 'class': 'pop_border pop_top' }),
		                     new Element('td', { 'class': 'pop_topright' })
		                  ),
		                  
		                  new Element('tr').adopt(
		                     new Element('td', { 'class': 'pop_border pop_side' }),
		                     new Element('td', { 'class': 'pop_content' }).adopt(
		                        new Element('h2', { 'class': 'dialog_title' }).adopt(
		                           new Element('span')
		                        ),
		                        
		                        new Element('div', { 'class': 'dialog_content' }).adopt(
		                           new Element('div', { 'class': 'dialog_summary' }),
		                           new Element('div', { 'class': 'dialog_body' }),
		                           new Element('div', {'class': 'dialog_buttons' }).adopt(
		                        		   new Element('input',
		                                                   {
		                                                      'type': 'button',
		                                                      'value': 'Close',
		                                                      'name': 'close',
		                                                      'class': 'inputsubmit',
		                                                      'id': this.options.id + '-close_add_edit_menu_id'
		                                                   })
		                              
		                           )
		                        )
		                     ),
		                     new Element('td', { 'class': 'pop_border pop_side' })
		                  ),
		                  
		                  new Element('tr').adopt(
		                     new Element('td', { 'class': 'pop_bottomleft' }),
		                     new Element('td', { 'class': 'pop_border pop_bottom' }),
		                     new Element('td', { 'class': 'pop_bottomright' })
		                  )
		               )
		            )
		         )
		      ).setStyle('opacity', 0);
		      
		      this.bkg.inject(document.body);
		      this.container.inject(document.body);
		      // Add some events
		      
		      
		      $(this.options.id + '-close_add_edit_menu_id').addEvent('click', function() { this.hide(); }.bind(this));
		      window.addEvent('keypress', function(e) { if (e.key == 'esc') this.hide(); }.bind(this));
		      //window.addEvent('domready', function()  { new FormCheck('formular');       }.bind(this));
		      $(document.body).addEvent('click', function(e)
		      {
		         if (this.isOpen() && !e.target.getParent('.generic_dialog')) { this.hide(); }
		      }.bind(this));
		      
		      return this;
		   },
		   
		   show: function()
		   {
		      this.bkg.setStyle('opacity',0.5);
		      this.bkg.setStyle('visibility','visible');
		      this.bkg.setStyle('display', 'block');
		       
		      this.fireEvent('show');
		      return this;
		   },
		   
		   hide: function()
		   {
		       this.bkg.setStyle('opacity',0);
		       this.bkg.setStyle('visibility','hidden');
		       this.bkg.setStyle('display', 'none');
			
		       this.container.getElement('.dialog_body').clone(true, true).inject($(this.opt+'_div_'+this.id));
		       
		       this.container.getElement('.dialog_body').empty();
		       $(this.opt+'_div_'+this.id).setStyle('visibility', 'collapse');
		       $(this.opt+'_div_'+this.id).setStyle('display', 'none');
		       this.fireEvent('hide');
		       return this;
		   },
		   
		   setContent: function(titleText, summaryHtml, bodyHtml, opt)
		   {
		      this.setTitle(titleText).setSummary(summaryHtml).setBody(bodyHtml).setOpt(opt);
		      return this;
		   },
		   
		   setTitle: function(titleText)
		   {
		      this.container.getElement('.dialog_title span').set('text', titleText);
		      return this;
		   },
		   
		   setSummary: function(summaryHtml)
		   {
		      this.container.getElement('.dialog_summary').set('html', summaryHtml);
		      return this;
		   },
		   
		   setBody: function(bodyHtml)
		   {
		       this.id =bodyHtml;
		       return this;
		   },
		   setOpt: function(opt)
		   {
			  this.opt =opt;
			  $(this.opt+'_'+this.id).setStyle('visibility', 'visible');
			  $(this.opt+'_'+this.id).setStyle('display', 'block');
			  this.container.getElement('.dialog_body').grab($(this.opt+'_'+this.id));
			  
		      return this;
		   },
		   
		   isOpen: function()
		   {
		      return (this.container.getStyle('opacity') == '0') ? false : true;
		   }
		});
    
    var add_edit_menu_id = 0;
    function modal_window(title, summary, body, opt)
    {
        add_edit_menu_id.setContent( urldecode(title) , urldecode(summary), urldecode(body), urldecode(opt));
        if($('add_edit_menu_id'))
           $('add_edit_menu_id').fireEvent('click');
    }

    window.addEvent('domready', function()
    {
        add_edit_menu_id = new add_edit_menu_modal(
        {
        	id: 'primeModal',
        	onShow: function(){ this.container.setStyle('display', 'block').fade(1); },
        	//onHide: function(){(function(){ this.setStyle('display', 'none');}).delay(1, this.container.fade(0));}
        	onHide: function(){ this.container.setStyle('display', 'none').fade(0); }
        });
        
        if($('add_edit_menu_id'))
           $('add_edit_menu_id').addEvent('click', function() { add_edit_menu_id.show(); });
        

    	modal = new ItemOptionsModal(
    	{
    		id: 'primeModal',
    		
    		onShow: function()
    		{
    			this.container.setStyle('display', 'block').fade(1);
    		},
    		onHide: function()
    		{
    			(function()
    			{
    				this.setStyle('display', 'none');
    			}).delay(600, this.container.fade(0));
    		}
    	});
    	if($('open'))
       $('open').addEvent('click', function() { modal.show(); });
    
    	    	
    });
    
   
    function setfavorite(customer_id, restaurant_id)
    {  //alert("cid="+customer_id+"rid="+restaurant_id);
        new Request({
            noCache:true,
    	    url:'setfavorite.php',
    	    onRequest: function(){
            //alert('mtav');
    	    },
    	    onSuccess: function(responseText){
    	
    	       // document.getElementById('screen').value =responseText;
    		
    	    },
    	}).post('customer_id='+customer_id+'&restaurant_id='+restaurant_id+'&oper=set');
    }
    function removefavorite(customer_id, restaurant_id)
    {  //alert("cid="+customer_id+"rid="+restaurant_id);
        new Request({
            noCache:true,
    	    url:'setfavorite.php',
    	    onRequest: function(){
    	    },
    	    onSuccess: function(responseText){
    	
    	       // document.getElementById('screen').value =responseText;
    	    },
    	}).post('customer_id='+customer_id+'&restaurant_id='+restaurant_id+'&oper=del');
    }
    
  
