function go_away(kam) {
  window.location = "main.php?" + kam;
}

function go_away_new(kam,path) {
  window.location = path+"main.php?" + kam;
}


function collect_options() {
}

function checkQuantity(o){
	var temp = parseInt(o.value,10);
	if (isNaN (temp)) temp = 1;
	if (temp <= 0) temp = 1;
	o.value = temp;
}

function sum_textfield(field,amount) {
  var which_one = document.getElementById(field);
  var temp = parseInt(which_one.value);
  if (isNaN(temp)) {
	which_one.value = 1;
	return true;
  }
  if (amount > 0 && temp == 99) {
	which_one.value = 99;
	return true;
  }
  if (amount < 0 && temp == 1) {
	which_one.value = 1;
	return true;
  }
  which_one.value = temp + amount;
}

function multi_count(how_many) {
  var count = 0;
  for(i = 0;i < how_many;i++) {
    if(document.getElementById('multi' + i).checked) {
      count++;
    }
  }
  return count;
}

function multi_checkbox(el,how_many,min,max) {
  if(max > 0 && el.checked) {
    if(multi_count(how_many) > max) {
      el.checked = false;
    }
  }
}

function validate_multi(how_many,min) {
  if(min > 0 && multi_count(how_many) < min) {
    var plural = min == 1 ? "" : "s";
    alert('You must chose at least ' + min + ' option' + plural + '.');
  } else {
    document.forms[1].submit();
  }
}

function textarea_limit(fieldMax,fieldObj,numChars) {
  if (fieldObj.value.length > numChars) {
    fieldObj.value = fieldObj.value.substr(0,numChars);
  }
  fieldMax.value = numChars - fieldObj.value.length;
}

function doLoad()
{
    // the timeout value should be the same as in the "refresh" meta-tag
    setTimeout( "refresh()", 120*1000 );
}

function compute_title(frm) {
  custloc = frm.elements['custloc_name'];
  cuisine = frm.elements['custloc_pref_cuisine'];

  andFind = frm.elements['search'];
  if (andFind.value) {
    andFindText = " (and find " + andFind.value + ")";
  } else {
    andFindText = "";
  }

  if (frm.elements['custloc_pref_fav_only']) {
    favOnly = frm.elements['custloc_pref_fav_only'];
    if (favOnly.checked) {
      favText = "my favourites ";
    } else {
      favText = "";
    }
  } else {
      favText = "";
  }

  if (frm.elements['locality_postcode']) {
    customPostcode = frm.elements['locality_postcode'];
    if (customPostcode.value) {
      postcodeText = " in " + customPostcode.value;
    } else {
      postcodeText = "";
    }
  } else {
      postcodeText = "";
  }

  offer = frm.elements['custloc_pref_rest_offer'];
  switch (offer.options[offer.selectedIndex].value) {
  case "D":
    offerText = "delivery";
    break;
  case "C":
    offerText = "collection";
    break;
  default:
    offerText = "delivery or collection";
  }

  return 'Search ' + favText + 'for ' + cuisine.options[cuisine.selectedIndex].value + ' restaurants that offer ' + offerText + postcodeText + andFindText;
}

function refresh()
{
    //  This version of the refresh function will be invoked
    //  for browsers that support JavaScript version 1.2
    //  The argument to the location.reload function determines
    //  if the browser should retrieve the document from the
    //  web-server.
    window.location.reload( true );
}

function show_hide(id) {
	var x = document.getElementById(id);
	if (x.style.display == "none"){
		x.style.display = "";
	} else {
		x.style.display = "none";
	}
	return false;
}

function expcoll(x,y) {
	var gif_obj = document.getElementById(x);
	var check_field = document.getElementById(y);
	if (check_field.style.display == "none") {
    gif_obj.src = 'images/icons/collapse.gif';
	} else {
    gif_obj.src = 'images/icons/expand.gif';
	}
	return false;
}

function expcoll_myaccount(x,y) {
	var gif_obj = document.getElementById(x);
	var check_field = document.getElementById(y);
	if (check_field.style.display == "none") {
    gif_obj.src = 'images/icons/expand.gif';
	} else {
    gif_obj.src = 'images/icons/collapse.gif';
	}
	return false;
}

function edit_panel_pizza(n) {
	/* show edit_panel_pizza_n and hide the others */
	var k = 0;
	var prev,temp;
	no_of_inputs = new Array();
	input_offsets = new Array();
	var prev_index, temp_count = 0;
	while (document.getElementById("edit_panel_pizza_" + k)){
		temp = document.getElementById("edit_panel_pizza_" + k);
		if (temp.style.display != "none") prev_index = k;	/* preserve the previously visible div's index */
		temp.style.display = "none";

		/* count the number of <inputs> within this div */
		str0 = temp.innerHTML.toLowerCase();	/* to lower case because ie converts the source to upper */
		no_of_inputs[k] = 0;
		pos = str0.indexOf("<input");
		while ( pos != -1 ) {
		   no_of_inputs[k]++;
		   pos = str0.indexOf("<input",pos+15);	/* another input won't occur within the next 15 chars */
		}
		input_offsets[k] = temp_count;	/* keep the offset of this bunch of inputs within the group of divs */
		temp_count += no_of_inputs[k];

		k++;
	}
	

	/* now k has the number of divs = number of sizes */

	var q = document.getElementById("edit_panel_pizza_" + n);
	q.style.display = "";	/* show the chosen div */

	if (prev_index == n) {	/* clicked on same size again, no need to do the rest */
		return false;
	}
	
	/* go through the individual inputs within the chosen div and check or uncheck the inputs */
	for (l = input_offsets[n]; l < input_offsets[n] + no_of_inputs[n]; l++) {

		inp1 = document.edit_panel_form.elements[k + l]; /* k must be added - it's the number of size inputs that precedes the divs section */
		inp1.checked = false;

		/* go through the previous visible div's inputs and see if there is a checked one with the same name and value */
		for (ll = input_offsets[prev_index]; ll < input_offsets[prev_index] + no_of_inputs[prev_index]; ll++) {
			inp0 = document.edit_panel_form.elements[k + ll];
			if (inp0.checked && inp0.name == inp1.name && inp0.value == inp1.value) {
				inp1.checked = true;	/* found an input with same name and value */
				break;
			}
		}
	}

	/* make sure all inputs (checkboxes and radios) within the invisible divs are unchecked */
	for (l = 0; l < k; l++) {
		if (l != n) {
			for (ll = input_offsets[l]; ll < input_offsets[l] + no_of_inputs[l]; ll++) {
				inp0 = document.edit_panel_form.elements[k + ll];
				inp0.checked = false;
			}
		}
	}

	/* make sure the "No thanks" pizza priced option is checked if no other option is checked */
	var option_checked = false; 
	var no_thanks_i = -1;
	for (l = input_offsets[n]; l < input_offsets[n] + no_of_inputs[n]; l++) {
		inp1 = document.edit_panel_form.elements[k + l];
		if (inp1.name != "pizza_option") { 
			continue;
		}
		if (inp1.value == "No thanks") {
			no_thanks_i = k+l;		/* save the "No thanks" input */
		}
		if (inp1.checked) {
			option_checked = true;
			break;
		}
	}
	if ( !option_checked && no_thanks_i != -1) {
		document.edit_panel_form.elements[no_thanks_i].checked = true;
	}

	/* now make sure the background colors of the following regions alternate correctly */
	var i = q.getElementsByTagName("tr");
	var lastOne = i[i.length - 1];
	var nextOne = document.getElementById("edit_panel_bottom");
	/* if the bg colors of the new neighbouring regions are the same, reassign the classes */
	if (lastOne.className == nextOne.className) { 
		var qq = document.getElementById("edit_panel_main_table");
		for (var j = nextOne.rowIndex; j < qq.rows.length - 1; j++) {
			if (qq.rows[j].className == "region_bg_1") {
				qq.rows[j].className = "region_bg_0";
			} else {
				qq.rows[j].className = "region_bg_1";
			}
		}
	}

	return true;
}

function edit_panel_special_select(select_obj){
	var i = select_obj.selectedIndex;
/*	var component_count = select_obj.name.substring(18,select_obj.name.indexOf("_",19));*/

	var div_id = select_obj.name + "_div";
	var the_div = document.getElementById(div_id);
	var the_options = eval (select_obj.name + "_options");
	if (the_options[i].length > 2) {
		the_div.innerHTML = the_options[i];
		the_div.firstChild.options[0].selected = true;
	} else {
		the_div.innerHTML = "";
	}
}

function onchange_payment_method(ordr_payment_method,server_path) {
	var new_protocol;
	if (ordr_payment_method == "card") {
		new_protocol = "https";
	} else {
		new_protocol = "http";
	}
  return new_protocol + '://' + server_path + 'main.php?menu=order&submenu=choose_time&payment=' + trimAll(ordr_payment_method);
}

function leftTrim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) {
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function trimAll(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function popupWin(what) {
	window.open ("http://hungryhouse.co.uk/include/wrapper.php?c="+what,"HH","toolbar=no,location=no,status=no,height=480,width=640,top=20,left=20,resizable=yes,scrollbars=yes");
	return false;
}
