var chooseRecipePopup = null;

function searchForRecipe(webContext,rowIndex) {
	var url = webContext +'/chooseRecipeForMeal.do?mode='+rowIndex;
	
	var width = 500;
	var height = 300;
	var top = (screen.height - height ) / 2;
	var left = (screen.width - width ) / 2;
	
	var windowOpts = "titlebar,toolbar=no,resizable,scrollbars,left=" +left 
		+",top=" +top +",width=" +width +",height=" +height;
	if ( chooseRecipePopup != null && !chooseRecipePopup.closed ) {
		chooseRecipePopup.close();
	}
	chooseRecipePopup = window.open(url, "choose_recipe", windowOpts);
	chooseRecipePopup.focus();
}

function addRecipe() {
	// get the recipe table
	var row = document.getElementById('recipe_row');
	
	// get the add row to insert the cloned row into
	var addRow = document.getElementById('recipe_add_row');
	
	// clone the recipe row
	var newRow = row.cloneNode(true);
	
	// remove 'id' attribute
	newRow.removeAttribute('id');
	
	// clear out quantity
	var quantity = findNodeWithAttributeValue(newRow,'name','\\.quantity$');
	if ( !quantity ) {
		alert("Did not find quantity!");
	} else {
		quantity.value = "1";
	}
	
	// set new course to selected index 0
	var course = findNodeWithAttributeValue(newRow,'name','\\.courseId$');
	if ( !course ) {
		alert("Did not find course!");
	} else {
		course.selectedIndex = 0;
	}
	
	// remove any selected recipe name
	var recipe = findNodeWithAttributeValue(newRow,'name','\\.recipeId$');
	if ( !recipe ) {
		alert("Did not find recipe!");
	} else {
		recipe.value = 0;
	}
	var recipeSpan = findNodeWithAttributeValue(newRow,'id','\\.name$');
	if ( !recipeSpan ) {
		alert("Did not find recipe name span!");
	} else {
		removeChildren(recipeSpan);
		recipeSpan.appendChild(document.createTextNode('('+msg_setRecipe+')'));
	}
	
	
	
	// find all 'name' attributes, and increment index values
	updateRecipeRowIndexValues(newRow,numRecipes);
		
	// insert into table
	addRow.parentNode.insertBefore(newRow,addRow);
	
	numRecipes++;
}

function updateRecipeRowIndexValues(row,index) {
	replaceAttribute(row,'name','recipe\\[\\d+\\]',
		'recipe[' +index +']');
	replaceAttribute(row,'id','recipe\\[\\d+\\]',
		'recipe[' +index +']');
	replaceAttribute(row,'href','\\d+',
		index);
}

function chooseRecipeForMeal(doc,rowNum,recipeId,recipeName) {
	var recipeNameSpan = doc.getElementById('meal.recipe['+rowNum+'].recipe.name');
	if ( !recipeNameSpan ) {
		alert("Could not get recipe name span!");
		return;
	}
	
	var recipeIdHidden = doc.forms[0].elements['meal.recipe['+rowNum+'].recipe.recipeId'];
	if ( !recipeIdHidden ) {
		alert("Could not get recipe id hidden element!");
		return;
	}

	// delete any current children, then insert name
	removeChildren(recipeNameSpan);
	
	// insert recipe name into span if name available
	if ( recipeName ) {
		recipeNameSpan.appendChild(doc.createTextNode(recipeName));
	}
	
	// set hidden select value to recipeId
	recipeIdHidden.value = recipeId;
}

function removeRecipe(recipeIndex) {
	// loop through all rows in recipe table, removing the 
	// row in question and then re-naming any subsequent rows
	// so index values decrease
	
	if ( recipeIndex < 1 ) {
		alert(getMessage('msg_noDeleteFirstRecipe'));
		return;
	}
	
	// forward to proper row
	var rowIdx = 0;
	var recipeRow = document.getElementById('recipe_row');
	if ( !recipeRow ) {
		alert("Could not get recipe_row!");
		return;
	}
	while ( recipeRow != null && rowIdx < recipeIndex ) {
		recipeRow = recipeRow.nextSibling;
		if ( recipeRow.nodeName.toLowerCase() == 'tr' ) {
			rowIdx++;
		}
	}
	
	var currRow = recipeRow.nextSibling;
	for ( var currIndex = recipeIndex; currIndex < (numRecipes - 1) && currRow != null 
		&& currRow.nextSibling != null; currRow = currRow.nextSibling ) 
	{
		if ( currRow.nodeName.toLowerCase() != 'tr' ) continue;
		updateRecipeRowIndexValues(currRow,currIndex);
		currIndex++;
	}
	
	// now remove recipeRow from table
	if ( recipeRow.parentNode.removeChild(recipeRow) ) {
		numRecipes--;
	} else {
		alert("Unable to remove recipe node!");
	}
}
