function Ajax()
{ 
	var xmlhttp=false; 
	try 
	{ 
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 
	return xmlhttp; 
}

function _loadList() {
	var div=document.getElementById("ingredients_list");
	div.innerHTML = "<img alt='' src='images/ajax-loader.gif' />";
	
	var ajax=Ajax();
	ajax.open("POST", "classes/ingredients_list.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(null);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			txt=unescape(ajax.responseText);
			txt2=txt.replace(/\+/gi," ");
			//div.style.textAlign = 'left';
			div.innerHTML=txt2;
			ShowIngredients(9);
		}
	}
}

function Procesar()
{
	cont = 0;
	first = 1;
	
	calories = 0;
	fat_calories = 0;
	fat = 0;
	sat_fat = 0;
	trans_fat = 0;
	cholesterol = 0;
	sodium = 0;
	carbs = 0;
	fiber = 0;
	sugar = 0;
	protein = 0;
	
	obj_selected_ingredients = document.getElementById('selected_ingredients');
	obj_selected_ingredients.innerHTML = "";
	CleanDIVs();
	for (var i=0;i<document.frmCalculator.elements.length;i++)
	{
		var x = document.frmCalculator.elements[i];
		if (x.id.substring(0,3) == 'chk')
		{
			if(x.checked == true)
			{
				if(first == 1)
				{
					obj_selected_ingredients.innerHTML += x.id.replace("chk_","");	
					first = 0;
				} else {
					obj_selected_ingredients.innerHTML += ", "+x.id.replace("chk_","");
				}
				obj_valores = document.getElementById(x.id.replace("chk_","hidden_"));
				var valores = new Array();
				valores = obj_valores.value.split(',');
				
				calories += parseFloat(valores[0]);
				fat_calories += parseFloat(valores[1]);
				fat += parseFloat(valores[2]);
				sat_fat += parseFloat(valores[3]);
				trans_fat += parseFloat(valores[4]);
				cholesterol += parseFloat(valores[5]);
				sodium += parseFloat(valores[6]);
				carbs += parseFloat(valores[7]);
				fiber += parseFloat(valores[8]);
				sugar += parseFloat(valores[9]);
				protein += parseFloat(valores[10]);
			}
		}
	}
	
	obj = document.getElementById('divCalories');
	obj.innerHTML = "Calories "+calories.toFixed(2)+" 			Cal from Fat "+fat_calories.toFixed(2);
	
	obj = document.getElementById('divFatGR');
	obj.innerHTML = fat.toFixed(2)+"g";
	obj = document.getElementById('divFatDV');
	obj.innerHTML = "<strong>"+calcFatDV(fat.toFixed(2))+"%</strong>";
	
	obj = document.getElementById('divSaturedFatGR');
	obj.innerHTML = sat_fat.toFixed(2)+"g";
	obj = document.getElementById('divSaturedFatDV');
	obj.innerHTML = "<strong>"+calcSatFatDV(sat_fat.toFixed(2))+"%</strong>";
	
	obj = document.getElementById('divTransFatGR');
	obj.innerHTML = trans_fat.toFixed(2)+"g";
	obj = document.getElementById('divTransFatDV');
	obj.innerHTML = "<strong>"+calcTransFatDV(trans_fat.toFixed(2))+"%</strong>";	
	
	obj = document.getElementById('divCholesterolGR');
	obj.innerHTML = cholesterol.toFixed(2)+"mg";
	obj = document.getElementById('divCholesterolDV');
	obj.innerHTML = "<strong>"+calcCholesterolDV(cholesterol.toFixed(2))+"%</strong>";
	
	obj = document.getElementById('divSodiumGR');
	obj.innerHTML = sodium.toFixed(2)+"mg";
	obj = document.getElementById('divSodiumDV');
	obj.innerHTML = "<strong>"+calcSodiumDV(sodium.toFixed(2))+"%</strong>";
	
	obj = document.getElementById('divCarbsGR');
	obj.innerHTML = carbs.toFixed(2)+"g";
	obj = document.getElementById('divCarbsDV');
	obj.innerHTML = "<strong>"+calcCarbsDV(carbs.toFixed(2))+"%</strong>";
	
	obj = document.getElementById('divFiberGR');
	obj.innerHTML = fiber.toFixed(2)+"g";
	obj = document.getElementById('divFiberDV');
	obj.innerHTML = "<strong>"+calcFiberDV(fiber.toFixed(2))+"%</strong>";
	
	obj = document.getElementById('divSugarGR');
	obj.innerHTML = sugar.toFixed(2)+"g";
	obj = document.getElementById('divSugarDV');
	obj.innerHTML = "<strong>"+calcSugarDV(sugar.toFixed(2))+"%</strong>";	
	
	obj = document.getElementById('divProteinGR');
	obj.innerHTML = protein.toFixed(2)+"g";
	obj = document.getElementById('divProteinDV');
	obj.innerHTML = "<strong>"+calcProteinDV(protein.toFixed(2))+"%</strong>";
}

function ResetAll()
{
	cont = 0;
	obj_selected_ingredients = document.getElementById('selected_ingredients');
	obj_selected_ingredients.innerHTML = "";
	for (var i=0;i<document.frmCalculator.elements.length;i++)
	{
		var x = document.frmCalculator.elements[i];
		if (x.id.substring(0,3) == 'chk')
		{
			x.checked == false;
		}
	}
	CleanDIVs();
}

function CleanDIVs()
{
	obj = document.getElementById('divCalories');
	obj.innerHTML = "Calories 0 			Cal from Fat 0";
	
	obj = document.getElementById('divFatGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divFatDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divSaturedFatGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divSaturedFatDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divTransFatGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divTransFatDV');
	obj.innerHTML = "<strong>0%</strong>";	
	
	obj = document.getElementById('divCholesterolGR');
	obj.innerHTML = "0mg";
	obj = document.getElementById('divCholesterolDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divSodiumGR');
	obj.innerHTML = "0mg";
	obj = document.getElementById('divSodiumDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divCarbsGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divCarbsDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divFiberGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divFiberDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divSugarGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divSugarDV');
	obj.innerHTML = "<strong>0%</strong>";
	
	obj = document.getElementById('divProteinGR');
	obj.innerHTML = "0g";
	obj = document.getElementById('divProteinDV');
	obj.innerHTML = "<strong>0%</strong>";	
}

function calcFatDV(value)
{
	return parseInt((value*100/65));
}

function calcSatFatDV(value)
{
	return parseInt((value*100/20));
}

function calcTransFatDV(value)
{
	return parseInt((value*100/50));
}

function calcCholesterolDV(value)
{
	return parseInt((value*100/300));
}

function calcSodiumDV(value)
{
	return parseInt((value*100/2400));
}

function calcCarbsDV(value)
{
	return parseInt((value*100/300));
}

function calcFiberDV(value)
{
	return parseInt((value*100/25));
}

function calcSugarDV(value)
{
	return parseInt((value*100/50));
}

function calcProteinDV(value)
{
	return parseInt((value*100/50));
}

function _loadList2() {
	var div=document.getElementById("ingredients_list");
	div.innerHTML = "<img alt='' src='images/ajax-loader.gif' />";
	
	var div2=document.getElementById("results");
	div2.innerHTML = "";
	div2.style.display = 'block';
	
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredients.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(null);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			txt=unescape(ajax.responseText);
			txt2=txt.replace(/\+/gi," ");
			div.innerHTML=txt2;
		}
	}
}

function _loadDetails(id) {
	var div=document.getElementById("results");
	div.innerHTML = "<img alt='' src='images/ajax-loader.gif' />";
	
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredients_details.php?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+id);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			txt=unescape(ajax.responseText);
			txt2=txt.replace(/\+/gi," ");
			div.innerHTML=txt2;
		}
	}
}

function UpdateIngredientsDetails(id) {
	document.frmIngredientsDetails.submit();
	var nombre_archivo_tmp = document.frmIngredientsDetails.myfile.value;
	if(nombre_archivo_tmp == '')
	{
		if(document.frmIngredientsDetails.txtImage.value == '')
		{
			var nombre_archivo = "";
		} else {
			var nombre_archivo = document.frmIngredientsDetails.txtImage.value;
		}
	} else {
		var nombre_archivo = nombre_archivo_tmp.substr(nombre_archivo_tmp.lastIndexOf('\\')+1,(nombre_archivo_tmp.length-nombre_archivo_tmp.lastIndexOf('\\')+1));
	}
	if(document.frmIngredientsDetails.chkEnabled.checked == true)
	{
		habilitado = 1;	
	} else {
		habilitado = 0;
	}
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredients_details_update.php?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+id+"&name="+document.frmIngredientsDetails.txtName.value+"&category="+document.frmIngredientsDetails.cboCategory.value+"&description="+document.frmIngredientsDetails.txtDescription.value+"&calories="+document.frmIngredientsDetails.txtCalories.value+"&protein="+document.frmIngredientsDetails.txtProtein.value+"&carbs="+document.frmIngredientsDetails.txtCarbs.value+"&fat="+document.frmIngredientsDetails.txtFat.value+"&cholesterol="+document.frmIngredientsDetails.txtCholesterol.value+"&sat_fat="+document.frmIngredientsDetails.txtSat_Fat.value+"&mono_fat="+document.frmIngredientsDetails.txtMono_Fat.value+"&poly_fat="+document.frmIngredientsDetails.txtPoly_Fat.value+"&sodium="+document.frmIngredientsDetails.txtSodium.value+"&vit_a="+document.frmIngredientsDetails.txtVitA.value+"&vit_c="+document.frmIngredientsDetails.txtVitC.value+"&calcium="+document.frmIngredientsDetails.txtCalcium.value+"&iron="+document.frmIngredientsDetails.txtIron.value+"&fiber="+document.frmIngredientsDetails.txtFiber.value+"&sugar="+document.frmIngredientsDetails.txtSugar.value+"&fat_calories="+document.frmIngredientsDetails.txtFat_Calories.value+"&trans_fat="+document.frmIngredientsDetails.txtTransFat.value+"&enabled="+habilitado+"&image="+nombre_archivo);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			if(ajax.responseText == "Ok!")
			{
				alert("Update succefully!.");
				location.href = 'admin.html';
			}
		}
	}
}

function DeleteIngredient(id) {
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredient_delete.php?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+id);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			if(ajax.responseText == "Ok!")
			{
				alert("Delete succefully!.");
				_loadList2();
			}
		}
	}
}

function NewIngredient() {
	var div=document.getElementById("results");
	div.innerHTML = "<img alt='' src='images/ajax-loader.gif' />";
	
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredient_new.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(null);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			txt=unescape(ajax.responseText);
			txt2=txt.replace(/\+/gi," ");
			div.innerHTML=txt2;
		}
	}
}

function UpdateNewIngredient() {
	document.frmNewIngredient.submit();
	var nombre_archivo_tmp = document.frmNewIngredient.myfile.value;
	var nombre_archivo = nombre_archivo_tmp.substr(nombre_archivo_tmp.lastIndexOf('\\')+1,(nombre_archivo_tmp.length-nombre_archivo_tmp.lastIndexOf('\\')+1));
	if(document.frmNewIngredient.chkEnabled.checked == true)
	{
		habilitado = 1;	
	} else {
		habilitado = 0;
	}
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredient_new_update.php?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("name="+document.frmNewIngredient.txtName.value+"&category="+document.frmNewIngredient.cboCategory.value+"&description="+document.frmNewIngredient.txtDescription.value+"&calories="+document.frmNewIngredient.txtCalories.value+"&protein="+document.frmNewIngredient.txtProtein.value+"&carbs="+document.frmNewIngredient.txtCarbs.value+"&fat="+document.frmNewIngredient.txtFat.value+"&cholesterol="+document.frmNewIngredient.txtCholesterol.value+"&sat_fat="+document.frmNewIngredient.txtSat_Fat.value+"&mono_fat="+document.frmNewIngredient.txtMono_Fat.value+"&poly_fat="+document.frmNewIngredient.txtPoly_Fat.value+"&sodium="+document.frmNewIngredient.txtSodium.value+"&vit_a="+document.frmNewIngredient.txtVitA.value+"&vit_c="+document.frmNewIngredient.txtVitC.value+"&calcium="+document.frmNewIngredient.txtCalcium.value+"&iron="+document.frmNewIngredient.txtIron.value+"&fiber="+document.frmNewIngredient.txtFiber.value+"&sugar="+document.frmNewIngredient.txtSugar.value+"&fat_calories="+document.frmNewIngredient.txtFat_Calories.value+"&trans_fat="+document.frmNewIngredient.txtTransFat.value+"&enabled="+habilitado+"&image="+nombre_archivo);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			if(ajax.responseText == "Ok!")
			{
				alert("Update succefully!.");
				location.href = 'admin.html';
			}
		}
	}
}

function DeleteImage(id,image) {
	var ajax=Ajax();
	ajax.open("POST", "classes/admin_ingredients_image_delete.php?", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+id+"&image="+image);
	
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			_loadDetails(id);
		}
	}
}

function ShowIngredients(id)
{
	x = 1;
	while(x<=9)
	{
		obj = document.getElementById('Cat_'+x);
		if(x == id)
		{
			obj.style.display = "block";
		} else {
			obj.style.display = "none";
		}
		x+=1;
	}
}

function PrintNutritionLabel()
{
	obj = document.getElementById('buttons');
	obj.style.display = 'none';
	var label = document.getElementById('resultado');
	var ventimp = window.open(' ', 'popimpr');
	ventimp.document.write( label.innerHTML );
	ventimp.document.close();
	ventimp.print( );
	ventimp.close();
	obj.style.display = 'block';
}