/** requires mootools 1.2 **/
function loadRecipe(id)
{
	new Request({
		url: location.href,
		method: 'post',
		onComplete: function(ret) {
			$('home').innerHTML = unescape(ret);
			$('printerButton').style.display = 'block';
		}
	}).send('func=loadRecipe&id='+id);
}
function hidePrinterButton()
{
	$('printerButton').style.display = 'none';
}
function removeLine(el, type)
{
	el.getParent('div').getParent('div').removeChild(el.getParent('div'));
	
	var tmp = 1;
	if (type == 'ingredient')
	{
		var elArray = $('ingredientList').getElements('div');
		elArray.each(function(a) {
			var str = a.getElement('span').innerHTML;
			var oldNum = str.substr(0,str.length-2);
			
			a.getElement('span').innerHTML = tmp+'. ';
			a.getElements('select').each(function(b) {
				b.name = b.name.replace('['+oldNum+']', '['+tmp+']');
			});
			a.getElement('input').name = a.getElement('input').name.replace('['+oldNum+']', '['+tmp+']');
			tmp = tmp*1 + 1;
		});
		ing_num = ing_num - 1;
		tmp = 1;
	}
	else if (type == 'direction')
	{
		var elArray = $('directionList').getElements('div');
		elArray.each(function(a) {
			var str = a.getElement('span').innerHTML;
			var oldNum = str.substr(0,str.length-2);
			
			a.getElement('span').innerHTML = tmp+'. ';
			a.getElement('textarea').name = a.getElement('textarea').name.replace('['+oldNum+']', '['+tmp+']');
			tmp = tmp*1 + 1;
		});
		step_num = step_num - 1;
	}
}


var step_num = 0;
var ing_num = 0;
function addDirection(el)
{
	var pElement = el.parentNode;
	step_num = step_num + 1;
	var container = new Element('div', {
	'styles': {
	'float': 'left',
	'clear': 'both',
	'margin': '2px 0px',
	'vertical-align': 'top'
	}
	});
	var qty = new Element('span', {
	'html': step_num+'. '
	});
	var direction = new Element('textarea', {
	'type': 'text',
	'name': 'direction['+step_num+'][direction]',
	'styles': {
	'width': '360px',
	'height': '40px'
	}
	});
	var acr = new Element('acronym', {
	'onclick': "removeLine(this, 'direction')",
	'html': '<img src="/images/icons/cross.png" border="0" />'
	});
	container.appendChild(qty);
	container.appendChild(direction);
	container.appendChild(acr);
	pElement.appendChild(container);
}
function addIngredient(el)
{
	var pElement = el.parentNode;
	var container = new Element('div', {
	'styles': {
	'float': 'left',
	'clear': 'both',
	'margin': '2px 0px',
	'vertical-align': 'top'
	}
	});
	ing_num = ing_num + 1;
	new Request({
		url: location.href,
		method: 'post',
		onComplete: function(ret) {
			var span1 = new Element('span');
			var span2 = new Element('span');
			var span3 = new Element('span');
			var acr = new Element('acronym', {
			'onclick': "removeLine(this, 'ingredient')",
			'html': '<img src="/images/icons/cross.png" border="0" />'
			});

			span1.innerHTML = ing_num+'. ';
			span2.innerHTML = ret;

			var txt = new Element('input', {
			'type': 'text',
			'name': 'ingredient['+ing_num+'][ingredient]',
			'styles': {
			'width': '180px',
			'margin-left': '5px'
			}
			});

			new Request({
				url: location.href,
				method: 'post',
				onComplete: function(res) {
					span3.innerHTML = res;

					container.appendChild(span1);
					container.appendChild(span2);
					container.appendChild(span3);
					container.appendChild(txt);
					container.appendChild(acr);

					pElement.appendChild(container);
				}
			}).send('func=returnMeasurementDropDown&name=ingredient['+ing_num+'][measurement]');

		}
	}).send('func=returnAmountDropDown&name=ingredient['+ing_num+'][amount]');

}
function saveRecipe()
{
	if (verifySaveRecipe())
	{
		$('recipe_form').set('send', {
			url: location.href,
			method: 'post',
			onComplete: function(ret) {
				showSuccess();
				loadAddRecipe(ret);
			}
		});
		$('recipe_form').send();
	}
	else
	{
		showError('Please fill in the required fields.');
	}
}
function loadAddRecipe(id)
{
	clean_vars();
	new Request({
		url: location.href,
		method: 'post',
		onComplete: function(ret) {
			//ret = escape(ret);
			var returnArray = JSON.decode(ret);
			$('home').innerHTML = unescape(returnArray['html']);
			ing_num = returnArray['ingredient_cnt'];
			step_num = returnArray['direction_cnt'];
			$('printerButton').style.display = 'none';
		}
	}).send('func=loadAddRecipe&id='+id);
}
function showSuccess()
{

}
function showError(txt)
{
	alert(txt);
}
function clean_vars()
{
	step_num = 0;
	ing_num = 0;
}
function checkAmountDropDown(el)
{
	if (el.value == 'custom')
	{
		el.parentNode.innerHTML = '<input type="text" style="width: 60px;" name="'+el.name+'" />';
	}
}
function verifySaveRecipe()
{
	var form = $('recipe_form');
	var ret = 1;
	if (form.title.value == '')
	{
		form.title.addClass('recipeErrorBox');
		setTimeout("resetError()", 3000);
		ret = 0;
	}
	else if (form.category_id.value == '')
	{
		form.category_id.addClass('recipeErrorBox');
		setTimeout("resetError()", 3000);
		ret = 0;
	}
	var ingList = $('ingredientList').getElements('input');
	if(ingList.length <= 0)
	{
		$('ingredientList').addClass('recipeErrorBox');
		setTimeout("resetError()", 3000);
		ret = 0;
	}
	else
	{
		ingList.each(function(el) {
			if (el.name.search('ingredient') == 0 && el.value == '')
			{
				el.addClass('recipeErrorBox');
				setTimeout("resetError()", 3000);
				ret = 0;
			}
		});
	}
	var dirList = $('directionList').getElements('textarea');
	if(ingList.length <= 0)
	{
		$('directionList').addClass('recipeErrorBox');
		setTimeout("resetError()", 3000);
		ret = 0;
	}
	else
	{
		dirList.each(function(el) {
			if (el.name.search('direction') == 0 && el.value == '')
			{
				el.addClass('recipeErrorBox');
				setTimeout("resetError()", 3000);
				ret = 0;
			}
		});
	}

	return ret;
}
function resetError()
{
	$('recipe_form').getElements('.recipeErrorBox').each(function(el) {
		el.removeClass('recipeErrorBox');
	});
}