  function check_card(number) {
    // remove non-numerics
    var v = "0123456789";
    var w = "";
    for (i=0; i < number.length; i++) {
      x = number.charAt(i);
      if (v.indexOf(x,0) != -1) { w += x; }
    }
    // TODO: american express is only 13 digits, but do we want to validate then
    if (w.length == 16) {
      if (!cardval(w)) {
        $('card_number').className = "invalid";
        $('order_cc_number').className = "invalid";
        $('card_messages').innerHTML = "<strong>Invalid Card Number:</strong> please re-check your digits.";
        setTimeout(Effect.Shake('card_messages', { duration: 1, distance: 10 }), 500);
        return false;
      } else {
        $('card_number').className = "valid";
        $('order_cc_number').className = "valid";
        $('card_messages').innerHTML = "";
        $('order_cc_expiration').focus();
      }
    }
  }

  function cardval(w) {
    // validate number
    j = w.length / 2;
    if (j < 6.5 || j > 8 || j == 7) { return false; }
    k = Math.floor(j);
    m = Math.ceil(j) - k;
    c = 0;
    for (i=0; i<k; i++) {
      a = w.charAt(i*2+m) * 2;
      c += a > 9 ? Math.floor(a/10 + a%10) : a;
    }
    for (i=0; i<k+m; i++) { c += w.charAt(i*2+1-m) * 1; }
    return (c%10 == 0);
  }

  function expand_menu_item(tab, id) {
    var menuItems = $('tab-group-' + tab).select('div.menu_item_prices');
    for (i=0; i < menuItems.length; i++) {
      if (menuItems[i].visible()) {
        new Effect.toggle(menuItems[i].id, 'blind');
      }
    }
    new Effect.toggle('menu_item_details_' + id, 'blind');
    new Effect.toggle('menu_item_prices_' + id, 'blind');

  }

  function load_customizer(item, price) {
    var active = $('item_' + item + '_price_' + price);
    var inactive = new Array();
    var itemPrices = $('prices_for_item_' + item).select('div.item_price');
    new Ajax.Updater('item-customizer-' + item, '/item/customize/' + price, {asynchronous:true, evalScripts:true}); 
    if ($('menu_item_details_' + item).visible()) { new Effect.BlindUp('menu_item_details_' + item); }
   // if (! ($('item-customizer-' + item).visible())) { 
     // setTimeout(new Effect.BlindDown('item-customizer-' + item), 15000); 
    //}

    for (i=0; i < itemPrices.length; i++) {
      if (! (itemPrices[i].id == active.id)) { 
        inactive.push(itemPrices[i]); 
      }
    }

   new Effect.multiple(inactive, Effect.Morph, {
                 style: {
                  background: '#555',
              //     background: 'transparent url(' + "'/images/item_price_bg.jpg'" + ') repeat-x',
                   fontSize: '12px',
                   fontWeight: 'bold',
                   marginBottom: '0',
                   color: '#999'
                 }
               });

   new Effect.Morph(active, {
                 style: {
                   background: '#ABAD85',
                   //backgroundColor: '#ABAD85',
                   fontSize: '20px',
                   marginBottom: '-15px',
                   fontWeight: 'bold',
                   color: '#333'
                }
               });
    if (! ($('item-customizer-' + item).visible())) { new Effect.BlindDown('item-customizer-' + item); }
  }

  function open_compact_group(id) {
    var compactGroups = $('compact-menu-wrapper').select('div.compact-group-items');
    for (i=0; i < compactGroups.length; i++) {
      if (compactGroups[i].visible()) {
        new Effect.BlindUp(compactGroups[i].id);
      }
    }
    if (! $('compact-items-' + id).visible()) {
      new Effect.BlindDown('compact-items-' + id); 
    }
  }

  function open_topping_menu(id) {
    var listItems = $('master').select('div');
    var headers = $('master').select('h3');
    for (i=0; i < listItems.length; i++) {
      if (listItems[i].visible()) {
		listItems[i].hide();
	/*	new Effect.Move(headers[i], {
		  x: -50, y: 0, mode: 'relative', duration: 0.3,
		  transition: Effect.Transitions.sinoidal
		});
	*/
		new Effect.Morph(headers[i], {
		  style: 'color: #525432;', // CSS Properties
		  duration: 0.5 // Core Effect properties
		});
      } else {
		if (listItems[i] == $('topping-group-' + id)) {
			new Effect.Appear('topping-group-' + id);
			
		/*	new Effect.Move('topping-header-' + id, {
			  x: 50, y: 0, mode: 'relative', duration: 0.3,
			  transition: Effect.Transitions.sinoidal
			});
		*/
			new Effect.Morph('topping-header-' + id, {
			  style: 'color: #000;', // CSS Properties
			  duration: 0.8 // Core Effect properties
			});
			
		}
	  }
    }

  }

  function createMarker(point, number, string) {
    var marker = new GMarker(point);
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(string);
      });
      return marker;
  }

  function createSmallMarker(point) {
    var tinyIcon = new GIcon();
    tinyIcon.image = "/images/icons/mm_20_red.png";
    //tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    tinyIcon.iconSize = new GSize(12, 20);
    tinyIcon.shadowSize = new GSize(22, 20);
    tinyIcon.iconAnchor = new GPoint(6, 20);
    tinyIcon.infoWindowAnchor = new GPoint(5, 1);
    markerOptions = { icon:tinyIcon };
    var marker = new GMarker(point, markerOptions);
    return marker;

  }

  function flip(who,what) { $(who).innerHTML = $(what).innerHTML; }

  function init_toppings(list, state) {
    var listItems = $(list).select('li');
    if (state == "closed") {
      Effect.multiple(listItems, Effect.Appear, {speed: 0.1, delay: 0.75});
    }
    if (state == "open") {
      Effect.multiple(listItems, Effect.Fade, {speed: 0.1, delay: 0.5});
    }
  }

  function update_price(item, amount) {
 //   var my_item = $(item).innerHTML;
 //   total = parseFloat(amount);
 //   $(item).innerHTML = "$" + total.toFixed(2);
  }

  function get_price(item) {
    var my_item = $(item);
    var my_html = my_item.innerHTML;
    var total = parseFloat(my_html.match(/\d+\.\d{2}/));
    return total;
  }

  function more(amount, form, item) {
    var my_item = $(item);
    var my_form = $(form);
    var my_display = $(form + '_display');
    var number = parseInt(my_form.value);
    var my_amount = parseFloat(amount);
    var my_price = get_price(item);
    var new_price = (parseFloat(my_price) + my_amount);
//    update_price(item, new_price);
    my_form.value = number + 1;
    my_display.innerHTML = number + 1;
//    new Effect.Highlight(item, {duration: .5});
  }

  function less(amount, form, item) {
    var my_item = $(item);
    var my_form = $(form);
    var my_amount = parseFloat(amount);
    var my_display = $(form + '_display');
    var number = parseInt(my_form.value);
    if (number >= 1) {
      my_form.value = number - 1;
      my_display.innerHTML = number - 1;
      var my_price = get_price(item);
      var new_price = (parseFloat(my_price) - my_amount);
//      update_price(item, new_price);
//      new Effect.Shake(item, {duration: .3, distance: 5});
    }
  }

  function calc_cost(amount, item, state) {
    var temp = $(item)
    var temp_html = temp.innerHTML
    var total = parseFloat(temp_html.replace(/\$/,""));
    var amount = parseFloat(amount);
    if (state == 1) {
      calculation = (total + amount).toFixed(2);
      update_price(item, calculation);
      //  $(item).innerHTML = "$" + calculation.toString();
      new Effect.Highlight(item, {duration: .5});
    }
    if (state == 0) {
      calculation = (total - amount).toFixed(2);
      update_price(item, calculation);
      // $(item).innerHTML = "$" + calculation.toString();
      new Effect.Shake(item, {duration: .3, distance: 5});
    }
  }

  function hold_topping(topping, id) {
    list_item = $('hold_the_' + id);
    href_link = "javascript: reset_hold_topping('" + topping + "', " + id + ");"
    list_item.innerHTML = "";
    list_item.className = "hold-the"
    var href = Builder.node('a', {
                 href: href_link
               }, topping);
    var option = Builder.node ('input', {
                   type: 'hidden',
                   name: 'toppings' + '[remove][' + id + ']'
                   });
    list_item.appendChild(option);
    list_item.appendChild(href);
  }

  function reset_hold_topping(topping, id) {
    list_item = $('hold_the_' + id);
    list_item.className = "normal"
    href_link = "javascript: hold_topping('" + topping + "', " + id + ");"
    list_item.innerHTML = "";
    var href = Builder.node('a', {
                 href: href_link
                }, topping);
   list_item.appendChild(href);
  }

  function add_topping(amount, item, portion) {
    var temp = $(item);
    var temp_html = temp.innerHTML;
    //  alert("temp_html= " +temp_html);
    var total = parseFloat(temp_html.replace(/\$/,""));
    //alert ("total = "+total);
    var amnt = parseFloat(amount);
    //  alert ("amount = "+amnt);
    var port = parseFloat(portion);
    //  alert ("portion = "+port);
    calculation = parseFloat((amnt * port) + total).toFixed(2);
    //  alert("Calc = "+calculation.toString);
//    temp.innerHTML = "$" + calculation.toString();
 //   new Effect.Highlight(item, {duration: .5});
  }

  function del_topping(amount, item, list) {
    new Effect.Puff(list, {duration: 0.5});
    var my_list = $(list);
    var temp = $(item);
    var temp_html = temp.innerHTML;
    //      alert("temp_html= " +temp_html);
    var total = parseFloat(temp_html.match(/\d+\.\d{2}/));
    //      alert ("total = "+total);
    var amnt = parseFloat(amount);
    calculation = parseFloat(total - amnt ).toFixed(2);
    //      alert("Calc = "+calculation.toString);
//    temp.innerHTML = "$" + calculation.toString();
    my_list.remove(my_list);
//    new Effect.Shake(item, {duration: .3, distance: 5});
  }

	function del_single_topping(value, menu_item_id, topping, topping_id, group_id) {
		var href_link = "javascript: add_single_topping(" + menu_item_id + ", '" + topping + "', " + topping_id + ", " + group_id + ")"
		var list_item = $('topping-' + topping_id + '_tgid-' + group_id);
		list_item.removeClassName("add-the");
		list_item.innerHTML = "";
		var href = Builder.node('a', { 
								href: href_link
	             }, topping);
		var span = Builder.node('span', {
			innerHTML: '(+$' + value + ')'
		})
		list_item.appendChild(href);
		list_item.appendChild(span);
	};

	function add_single_topping(menu_item_id, topping, topping_id, group_id) {
		  var list_item = $('topping-' + topping_id + '_tgid-' + group_id);
			var match = list_item.id.match(/^topping-(\d+)_tgid-(\d+)$/);
  		var element_html = list_item.innerHTML.replace(/\<span\>|\<\/span\>/gi,"");
			var element_name = element_html.replace(/\(\+\$\d+\.\d{2}\)/,"");
      var element_price = element_html.match(/\(\+\$\d+\.\d{2}\)/);
      var amount = element_html.replace(/.*\(/,"");
      var digits = amount.match(/\d+\.\d{2}/)
      var value = parseFloat(digits[0]);
			var href_link = "#"
			var href_link = "javascript:del_single_topping('" + value.toFixed(2) + "', " + menu_item_id + ", '" + topping + "', " + topping_id + ", " + group_id + ");";

			list_item.innerHTML = "";
	  list_item.addClassName("add-the");
	  var href = Builder.node('a', {
	               href: href_link
	             }, topping);
	  var option = Builder.node ('input', {
	          type: 'hidden',
	         name: 'toppings' + '[whole][' + match[1] + '_'+ match[2] + ']',
	          value: value
	  });
	  list_item.appendChild(option);
	  list_item.appendChild(href);
	}

function dblClickBad() {
  alert('WOOPS! You double-clicked..\n\nDouble clicking the signup button causes errors.\n\nYour account may not be set up properly because of this.')
}
