var f;

function checkForm(event, group, no_amount_text)
{
	var default_no_amount_text = 'Enter an amount please';
  if (document.food_log.group_id.value > 0 &&
      document.food_log.food_id.value > 0 &&
      document.food_log.measure_id.value > 0 &&
      document.getElementById('amount_'+group).value != '')
  {
    document.food_log.submit();
  }
  else
  {
    if (document.food_log.food_id.value == '')
    {
      document.getElementById('s_'+group).focus();
      // отключил вывод окна с поиском всего подряд. пусть что нибудь вводят
      //showSelect(event, 0, 'select.php?group_id='+group+'&name_id=food_id&name_name=s_'+group+'&page=food&word='+document.getElementById('s_'+group).value, 'search_food_'+group);
      return false;
    }
    if (document.food_log.measure_id.value == '')
    {
      document.getElementById('form_measure_id_'+group).focus();
      showSelect(event, 0, 'select.php?group_id='+group+'&name_id=measure_id&name_name=form_measure_id_'+group+'&page=measure&word='+document.getElementById('form_measure_id_'+group).value+'&layer=search_measure_'+group, 'search_measure_'+group);
      return false;
    }
    if (document.getElementById('amount_'+group).value == '')
    {
      document.getElementById('amount_'+group).focus();
      if (no_amount_text == undefined) {
    	  no_amount_text = default_no_amount_text;
      }
      alert(no_amount_text);
      return false;
    }
    alert('Food, Units or Amount are missing!');
    return false;
  }
}

function setMeasure(group_id, measure_id, measure_name)
{
	document.getElementById('form_measure_id_' + group_id).value = measure_name;
	//alert(document.getElementById('form_measure_id_' + group_id).value);
	if (document.food_log)
	{
		var amount = '1';
		// До 4 у нас стандартные величины
		if (measure_id <= 4)
		{
			amount = '';
		}
		document.getElementById('amount_'+group_id).value = amount;
		document.food_log.measure_id.value = measure_id;
	}
	else
	{
		document.getElementById('measure_id_'+group_id).value = measure_id;
	}
}

function loadMeasure(form, food_id)
{
  f = form;

  var re = /^(.*?)([^\/]*)$/;
  var path_arr = re.exec(location.pathname);
  var path = path_arr[1];

  var url = "http://" + location.host + "/"+path+"search_measure.php" + "?food_id=" + encodeURIComponent(food_id);
  if (req)
    req.abort();
  req = (!window.XMLHttpRequest)? (ActiveXObject ? (new ActiveXObject("Microsoft.XMLHTTP")):""):(new XMLHttpRequest());
  req.open("GET", url, true);
  req.onreadystatechange = new Function("ProcessM()");
  req.send(null);
}

function ProcessM()
{
  if (req.readyState == 4)
  {
    //try
    //{
      if (req.status == 200)
      {
        if(req.responseText == "")
        {
          //HideDiv(fid);
        }
        else
        {
          eval('measureLoadResult(' + req.responseText + ');');
        }
      }
    //}
    //catch(e){}
  }
}

measureLoadResult = function(names, ids)
{
  f.measure_id.length = 0;
  for (var i = 0; i < names.length; i++)
  {
    var o = new Option(names[i], ids[i], false, false)
    f.measure_id.add(o);
  }
}

///////////////////////////////////////////




function setData(name_id, name_name, id, name, group_id, measure_id, measure_name)
{
  document.getElementById(name_name).value = htmlspecialchars_decode(name);
  document.getElementById(name_id).value = id;
  setMeasure(group_id, measure_id, measure_name);

  document.getElementById(name_name).className = 'text';

  hideSelect('search_food_'+group_id);
  return false;
}

function setDataUser(name_id, name_name, id, name, group_id, measure_id, measure_name)
{
  document.getElementById(name_name).value = name;
  document.getElementById(name_id).value = id;
  document.getElementById('food_type').value = 'user';
  setMeasure(group_id, measure_id, measure_name);

  document.getElementById(name_name).className = 'text';

  hideSelect('search_food_'+group_id);
  return false;
}

function setDataMeasure(name_id, name_name, id, name, group_id)
{
	log('setDataMeasure name_id: '+name_id+' name_name: '+name_name+' id: '+id+' name: '+name+' group_id: '+group_id);
	document.getElementById(name_name).value = name;

	if (document.getElementById(name_id))
	{
		document.getElementById(name_id).value = id;
	}
	else
	{
		document.getElementById(name_id+'_'+group_id).value = id;
	}

	//document.getElementById(name_name).focus();

	var amount = '1';
	// До 4 у нас стандартные величины
	if (id <= 4)
	{
		amount = '';
	}
	//alert(id);
	document.getElementById('amount_'+group_id).value = amount;
	if (name_name.indexOf('form_measure_id_') == 0) {
		log('setDataMeasure hide search_measure_'+group_id);
		hideSelect('search_measure_'+group_id);
	}
	else {
		var m = name_name.match(/\[(\d+)\]/);
		var food_log_id = m[1];
		log('setDataMeasure hide search_measure_food_log_'+food_log_id);
		hideSelect('search_measure_food_log_'+food_log_id);
	}
	return false;
}

////////////////////////////
function showFavorites(date, group_id)
{
  document.getElementById('select_favorite_'+group_id).style.position = 'absolute';

  if (document.getElementById('select_favorite_'+group_id).style.visibility == 'visible')
  {
    document.getElementById('select_favorite_'+group_id).style.visibility = 'hidden';
  }
  else
  {
    
    var re = /^(.*?)([^\/]*)$/;
    var path_arr = re.exec(location.pathname);
    var path = path_arr[1];

    var url = "http://" + location.host + '/' + path + 'select.php?page=favorites&date='+date+'&group_id='+group_id;

    document.getElementById('select_favorite_'+group_id).style.visibility = 'hidden';
    document.getElementById('select_favorite_'+group_id).innerHTML = '';
    if (req)
      req.abort();
    req = (!window.XMLHttpRequest) ? (ActiveXObject ? (new ActiveXObject("Microsoft.XMLHTTP")) : '') : (new XMLHttpRequest());
    req.open("GET", url, true);
    req.onreadystatechange = new Function("loadContent('select_favorite_"+group_id+"')");
    req.send(null);
    addShowLayer('select_favorite_'+group_id);
  }
}

function showDayFavorites(date)
{
  document.getElementById('select_day_favorite').style.position = 'absolute';

  if (document.getElementById('select_day_favorite').style.visibility == 'visible')
  {
    document.getElementById('select_day_favorite').style.visibility = 'hidden';
  }
  else
  {
    
    var re = /^(.*?)([^\/]*)$/;
    var path_arr = re.exec(location.pathname);
    var path = path_arr[1];

    var url = "http://" + location.host + '/' + path + 'select.php?page=day_favorites&date='+date;

    document.getElementById('select_day_favorite').style.visibility = 'hidden';
    document.getElementById('select_day_favorite').innerHTML = '';
    if (req)
      req.abort();
    req = (!window.XMLHttpRequest) ? (ActiveXObject ? (new ActiveXObject("Microsoft.XMLHTTP")) : '') : (new XMLHttpRequest());
    req.open("GET", url, true);
    req.onreadystatechange = new Function("loadContent('select_day_favorite')");
    req.send(null);
    addShowLayer('select_day_favorite');
  }
}

function htmlspecialchars_decode(text) {
	text = text.replace(/&amp;/, '&');
	text = text.replace(/&lt;/, '<');
	text = text.replace(/&gt;/, '>');
	text = text.replace(/&quot;/, '"');
	text = text.replace(/&#039;/, "'");
	return text;
}
