/**
	CHS: Neues Fenster öffnen
	*/
	function openWindow(sUrl, sTitle, iWidth, iHeight, iLeft, iTop)
	{
		sOptions = "width="+iWidth+",height="+iHeight+",left="+iLeft+",top="+iTop+",resizable=yes";
		newWindow = window.open(sUrl, sTitle, sOptions);
		newWindow.focus();
	}

/**
	CHS: Referenziert ein Objekt
	*/
	function ref(sName)
	{
		if (document.getElementById){
			return document.getElementById(sName);
		} else if (document.all) {
			return eval("document.all." + sName);
		} else {
			return false;
		}
	}
	
/**
	CHS: Schaltet die Sichtbarkeit um
	*/
	function toggle(sName)
	{
		oThingy = ref(sName);
	
		if (!oThingy.style) return false;
		
		if (oThingy.style.display == "none")
			oThingy.style.display = "";
		else
			oThingy.style.display = "none";
	}
	
/**
	HU: Berechnet die Gesamtkalorien
	*/
	function calcKcalges(){
		kcalges = ref('kcalges');
		kcal100g = ref('kcal100g');		
		fweight = ref('fweight');	
		fweighttype = ref('fweighttype');	
		inputNumberValidate(fweight); 
		if(fweighttype.value == 2){
			// Portionen in Menge umrechnen
			t_weight = fweight.value * portionsize.value;
		}
		else{
			t_weight = fweight.value;
		}
		kcalges.value = Math.round(t_weight/100*kcal100g.value)
	}
	
/**
	HU: Umrechnung Gewicht <-> Portionen
	*/
	function calcFweight(fweighttype){
		fweight = ref('fweight');
		portionsize = ref('portionsize');		
		if(fweighttype == 1){
			// Portionen in Menge umrechnen
			fweight.value = Math.round(fweight.value * portionsize.value);
			document.all.fweight_unit.innerHTML = 'g';
		}
		else{
			// Menge in Portionen umrechnen
			fweight.value = Math.round(fweight.value / portionsize.value * 10) / 10;			
			document.all.fweight_unit.innerHTML = 'Stk.';
		}
		calcKcalges();
	}
	
/**
	HU: Deaktiviert den Submit-Button eines Formulars
	*/
	function disableSubmit(button) {
		button.disabled = true; 
		button.value = 'bitte warten...';
	}
	
/**
	CHS: Alle Submits des Forms disablen
	Findet Verwendung in:
	- today.inc.html
	*/
	function disableAllSubmits()
	{
		if (document.getElementsByTagName)
		{
			var inputs = document.getElementsByTagName('input');
			for (var i = 0; i < inputs.length; i++)
			{
				if (inputs[i].getAttribute('type') == 'submit')
				{
					inputs[i].disabled = true;
//					inputs[i].value = 'bitte warten...';
				}
			}
		}
	}
	
/**
	CHS: Setzt den Wert eines Feldes im Formular
	*/
	function setValue(sFormID, sName, mValue)
	{
		var el = ref(sFormID);
		el[sName].value = mValue;
	}
	
/**
	CHS: Setze Aktion in Formular
	Findet Verwendung in:
	- today.inc.html
	*/
	function submitAction(sFormID, sActionValue)
	{
		var el = ref(sFormID);
		el.action.value = sActionValue;
		el.submit();
	}
	
	function doThis(sFormID, sActionValue) {
		var el = ref(sFormID);
		el.doThis.value = sActionValue;
		el.submit();
	}
	
/**
	HU: ???
	*/
	function goToUrl(Ziel, whichwindow) {
		whichwindow.location.href = Ziel;
	}

/**
	HU: ???
	*/
	function inputNumberValidate(inputfield)
	{
	 	inputfield.value = inputfield.value.replace(/,/,".");
		inputfield.value = inputfield.value.replace(/[^0-9.]+/,"");
	}

/**
	CHS: Tabellezeile hervorheben bei mouseOver
	Wird über onLoad aktiviert.
	Nur auf Tabellen der Klasse "body"
	*/
	function highlight()
	{
		if (document.getElementById && document.createTextNode)
		{
			var tables = document.getElementsByTagName('table');
			for(var i=0; i<tables.length; i++)
			{
				if (tables[i].className == 'body')
				{
					var trs = tables[i].getElementsByTagName('tr');
					for(var j=0; j<trs.length; j++)
					{
						trs[j].onmouseover = function() { this.className = 'highlight'; return false }
						trs[j].onmouseout = function() { this.className = ''; return false }
					}
				}
			}
		}
	}

/*	???
	*/
	function wait(prmSec)
	{
		prmSec *= 1000;
		var eDate = null;
		var eMsec = 0;
		var sDate = new Date();
		var sMsec = sDate.getTime();
		
		do {
			eDate = new Date();
			eMsec = eDate.getTime();
		} while ((eMsec-sMsec) < prmSec);
	}

/*
	CHS: Entfernt führende Nullen in einer Zeichenkette
	*/
	function stripLeadingZeroes(input)
	{
		if ((input.length > 1) && (input.substr(0,1) == '0'))
			return input.substr(1);
		else
			return input;
	}

/*
	CHS: Ermittelt Monat in Form von Ziffern anhand des Kürzels des Monatsnamens
	*/
	function getMonthNum(abbMonth)
	{
		var arrMon = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for(i=0; i<arrMon.length; i++)
		{
			if(abbMonth == arrMon[i])
				return i+1;
		}
	}

/*
	CHS: Wandelt einen Unix-Timestamp in ein lesbares Datum um
	*/
	function timeToHuman(timestamp)
	{
		var oDate = new Date(timestamp * 1000);
		result = new Array(
			oDate.getDay(),
			oDate.getMonth(),
			oDate.getFullYear(),
			oDate.getHours(),
			oDate.getMinutes(),
			oDate.getSeconds()
		);
		return(result);
	}

/*
	CHS: Liefert den Unix-Timestamp für ein Datum
	*/
	function getUnixTimestamp(iDay, iMonth, iYear, iHours, iMinutes, iSeconds)
	{
		var oDate = new Date(iYear,
			(stripLeadingZeroes(iMonth)-1), 
			stripLeadingZeroes(iDay),
			stripLeadingZeroes(iHours),
			stripLeadingZeroes(iMinutes),
			stripLeadingZeroes(iSeconds));
		return((oDate.getTime()/1000));
	}

/*
	CHS: Ausgabe und Umletung, falls die Sitzung abgelaufern ist.
	*/
	function forceRelogin()
	{
		alert('Die Sitzung ist abgelaufen, bitte melden Sie sich erneut an!');
		top.document.location.href = 'index.php';
	}
	
/*
	CHS: ???
	*/
	function reloadDiary()
	{
		opener.top.document.diary.document.location.reload();
	}
	
/*
	CHS: ???
	*/
	function reloadToday()
	{

		top.document.today.location.href = 'today.php';
		//opener.top.document.today.document.location.reload();	
	}
	
/*
	CHS: ???
	*/
	function reloadPhoto()
	{
		top.document.photo.document.location.reload();
	}
	
/*
	CHS: ???
	*/
	function reloadMenu()
	{
		top.document.foods.location.href = 'menus.php';
//		top.document.foods.document.location.reload();
	}
	
/*
	HU: Menu neuladen (von geöffnetem Fenster aus)
	*/
	function reloadMenuFromPopup()
	{
		opener.top.document.foods.location.href = 'menus.php';
//		top.document.foods.document.location.reload();
	}	
	
/*
	HU: Menu neuladen (von geöffnetem Fenster aus)
	*/
	function reloadTodayFromPopup()
	{
		opener.top.document.today.location.href = 'today.php';
//		top.document.foods.document.location.reload();
	}	
	
/*
	CHS: Auswahl des Tages
	Findet Verwendung in:
	- fitness.inc.html
	*/
	function todayIs(f)
	{
		f.today.value = getUnixTimestamp(f.day.value, f.month.value, f.year.value, 0, 0, 0);
		f.submit();
	}
	
/*
	CHS: Merkzettel leeren
	Findet Verwendung in:
	- fitness.inc.html
	*/
	function resetNotepad()
	{
		f = top.document.notepad.document.forms[0];
		f.what.value = 'reset';
		f.submit();
	}
	
/*
	CHS: Merkzettel aktualisieren
	Findet Verwendung in:
	- fitness.inc.html
	*/
	function refreshNotepad()
	{
		f = top.document.notepad.document.forms[0];
		f.submit();
	}

/*
	CHS: Speichern des Merkzettels
	Findet Verwendung in:
	- fitness.inc.html
	*/
	function saveNotepad()
	{
		f = top.document.notepad.document.forms[0];
		
		f.what.value = 'save';
		f.time.value = getUnixTimestamp(
			this.document.todayIs.day.value,
			this.document.todayIs.month.value, 
			this.document.todayIs.year.value, 
			this.document.timeToSave.hour.value, 
			this.document.timeToSave.minute.value, 0);
		
		f.submit();
		wait(1);
		reloadToday();
	}
	
/*
	CHS: Setzt die Uhrzeit in der Übersicht 
	Findet Verwendung in:
	- today.inc.html
	*/
	function setMealTime(time)
	{
		f = top.document.timeToSave;
		aTime = timeToHuman(time);
		
		for (i = 0; i <= f.hour.length; i++)
		{
			if (f.hour.options[i].value == aTime[3])
			{
				f.hour.options[i].selected = true;
				break;
			}
		}

		for (i = 0; i <= f.minute.length; i++)
		{
			if (f.minute.options[i].value == aTime[4])
			{
				f.minute.options[i].selected = true;
				break;
			}
		}
	}

/*
	CHS: Bestätigungs-PopUp
	Findet Verwendung in:
	- foods.inc.html
	- fitness.inc.html
	- today.inc.html
	*/
	function really(sMessage, sDoThis)
	{
		if (confirm(sMessage))
		{
			if (sDoThis != 'undefined') eval(sDoThis);
			return(true);
		}
		return(false);
	}

/**
 * qForm JSAPI: Bits Extensions Library
 * Author: Dan G. Switzer, II
 */
function getBitsum(sForm, sField){
	var f = ref(sForm);

	var iSum = 0;
	
	// loop through all checkbox elements, and if a checkbox is checked, grab the value
	for(var i = 0; i < f.elements.length; i++) {
		var e = f.elements[i];
		var isCheckbox = (e.type == 'checkbox') ? true : false;
		// if the option is checked, then add the 2 ^ i to the existing value
		if(isCheckbox && e.checked && e.name == sField) {
			iSum += parseInt(e.value, 10);
		}
	}

	return(iSum);
}


/**
 *	Name: openWin
 *	Author: Travis Beckham | squidfingers.com
 *	Description: This script is for opening external windows.
 */
openWin = function(url,name,width,height,xpos,ypos,chrome,scroll) {
	var x, y, w, h, moveX=0, moveY=0, features="";

	chrome = chrome ? "yes" : "no";
	scroll = scroll ? "yes" : "no";

	features += "toolbar="+chrome+",location="+chrome+",status="+chrome+",menubar="+chrome;
	features += ",scrollbars="+scroll+",resizable="+scroll;

	if(width) features += ",width="+width;
	if(height) features += ",height="+height;

	if(xpos && window.screen) {
		w = window.screen.availWidth;
		width = parseInt(width);
		switch(xpos) {
			case "left": x = 0; break;
			case "center": x = (w-width)/2; break;
			case "right": x = w-width; break;
			default: x = xpos;
		}
		features += ",screenX="+x+",left="+x;
		var moveX = x;
	}

	if(ypos && window.screen) {
		h = window.screen.availHeight;
		height = parseInt(height);
		switch(ypos) {
			case "top": y = 0; break;
			case "middle": y = (h-height)/2; break;
			case "bottom": y = h-height; break;
			default: y = ypos;
		}
		features += ",screenY="+y+",top="+y;
		var moveY = y;
	}

	openWinReference = window.open(url,name,features);

	if(moveX || moveY) {
		// position the window for browsers that don't recognize screenX, screenY
		openWinReference.moveTo(moveX,moveY);
	}

	openWinReference.focus();
}

openScroll = function(url,name,width,height) {
	openWin(url,name,width,height,false,false,false,"scroll");
}

openCenter = function(url,name,width,height) {
	openWin(url,name,width,height,"center","middle",false);
}

openCenterScroll = function(url,name,width,height) {
	openWin(url,name,width,height,"center","middle",false,"scroll");
}

openFull = function(url,name) {
	openWin(url,name,false,false,false,false,"chrome","scroll");
}
