var nCookieDay = 30;
// JavaScript Document
function gID(objectId) { if(document.getElementById && document.getElementById(objectId)) return document.getElementById(objectId); else if (document.all && document.all(objectId)) return document.all(objectId); else return false;}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) { createCookie(name,"",-1); }

function addbehov(behov) {
	createCookie('behov',behov,nCookieDay);
	UpdateGUI();
	return true;
}

function addforbruk(fID,mets,tid,vikt) {
	var egen = gID('f'+fID).value;
	if( (egen != null && egen.length == 0) || tid == 0) {
		alert('Du måste beräkna kaloriförbrukningen innan du kan lägga till den i din energiförbrukning.');
		return false;
	}

	post = fID + "|" + egen + "|" + mets + "|" + tid + "|" + vikt
	var aForb = readCookie('forb');
	if (aForb != null) {
		if (aForb.length > 0)
			aForb = aForb + "," + post;
		else
			aForb = post;
		createCookie('forb',aForb,nCookieDay);
	} else {
		createCookie('forb',post,nCookieDay);
	}

	var nTot = readCookie('forbtot');
	if( nTot != null && !isNaN(nTot) )
		nTot = parseInt(nTot);
	else
		nTot = 0;
	nTot = parseInt(nTot)+parseInt(egen);
	createCookie('forbtot',nTot,nCookieDay);

	UpdateGUI();

	gID('f'+lID).value = "";

	return false;
}

function addkalori(lID,vikt,kcal) {
	var egen = gID('k'+lID).value;
	if( egen != null && egen.length == 0) {
		alert('Du måste ange en mängd innan du kan lägga till den i din energiförbrukning.');
		return false;
	}

	post = lID + "|" + egen + "|" + Math.round((kcal * (egen/vikt)))
	var aFood = readCookie('food');
	if (aFood != null) {
		if (aFood.length > 0)
			aFood = aFood + "," + post;
		else
			aFood = post;
		createCookie('food',aFood,nCookieDay);
	} else {
		createCookie('food',post,nCookieDay);
	}
	var nKcal = readCookie('foodkcal');
	if( nKcal != null && !isNaN(nKcal) )
		nKcal = parseInt(nKcal);
	else
		nKcal = 0;
	nKcal = Math.round(nKcal+(kcal * (egen/vikt)));
	createCookie('foodkcal',nKcal,nCookieDay);

	UpdateGUI();

	gID('k'+lID).value = "";
	
	return false;
}

function removeAll() {
	if(window.confirm('Vill du radera alla information som du har lagt till inkl din profil?')) {
		eraseCookie('behov');
	
		eraseCookie('food');
		eraseCookie('foodkcal');

		eraseCookie('forb');
		eraseCookie('forbtot');

		eraseCookie('alder');
		eraseCookie('langd');
		eraseCookie('vikt');
		
		UpdateGUI();
		
		window.location.reload();
	}

	return false;
}

function UpdateGUI() {
	var tot = 0;
	var s = readCookie('behov');
	if( s == null ) 
		s = "-";
	else
		tot = parseInt(s)
	gID('behov').innerHTML=s;

	s = readCookie('foodkcal');
	if( s == null ) { 
		s = "-" 
	} else { 
		tot = tot - parseInt(s);
		s = "-"+s;
	}
	gID('food').innerHTML=s;
	
	s = readCookie('forbtot');
	if( s == null ) { 
		s = "-" 
	} else { 
		tot = tot + parseInt(s);
		s = "+"+s;
	}
	gID('forbruk').innerHTML=s;

	gID('total').innerHTML=tot;
}