var tour_price = 0.00;

function showValueonHtml(elementName, myString)
{
	document.getElementById(elementName).innerHTML = "";
	var myText = document.createTextNode(myString);
    var Ausgabebereich = document.getElementById(elementName);
    Ausgabebereich.appendChild(myText);
    Ausgabebereich = "";
}

function formatNumbers(myNumber)
{
	myNumber = String(myNumber);
	if(myNumber.indexOf(".") < 0)
	{
		myNumber = myNumber+".00";
	}else if( (myNumber.length - (myNumber.indexOf(".")+1)) < 2){
		myNumber = myNumber+"0";
	}
	return myNumber;
}

function calcPrice()
{
	//alert(choosedRoom);
	for(var i = 0; i < tmp.length; i++)
	{
		day = document.forms["buchung"].elements["station_constancy["+i+"]"].value;
		id = document.forms["buchung"].elements["station_id["+i+"]"].value;
		tour_price = eval(tour_price) + (eval(day) * eval(station_price[id]));
	}
	//alert(tour_price);
		if(choosedRoom == 1 && pAnzahl <= 2)
		{
			tour_price = eval(tour_price); 
		}else if(choosedRoom == 1 && !(pAnzahl%2))
		{
			tour_price = (pAnzahl/2)*eval(tour_price); 
			
		}else if(choosedRoom == 1 && !(pAnzahl%3))
		{
			tour_price = (pAnzahl/3)*eval(tour_price);
		}else if(choosedRoom == 2 && pAnzahl <= 3)
		{
			tour_price = eval(tour_price); 
		}else if(choosedRoom == 2 && pAnzahl >= 6)
		{
			tmp = 0;
			for(i = 0; i < pAnzahl; i+=3)
			{
				tmp++ ;
			}
			tour_price = tmp*eval(tour_price);
		}else if(choosedRoom == 2 && pAnzahl <= 6 && pAnzahl >= 3)
		{
			tmp = 0;
			for(i = 0; i <= pAnzahl; i+=3)
			{
				tmp++ ;
			}
			tour_price = tmp*eval(tour_price);
		}/*else{
			tour_price = eval(tour_price) * eval(pAnzahl);
		}*/
		tour_price = tour_price * pAnzahl;
	//tour_price = tour_price + noStudentCosts;
	//tour_price = tour_price + (pAnzahl * roomCosts);
	tour_price = formatNumbers(tour_price);
	showValueonHtml("price", tour_price);
	tour_price = 0.00;
}

function calcAllDays()
{
	var allDays = 0;
	for(var i = 0; i < tmp.length; i++)
	{
		if(document.forms["buchung"].elements["station_constancy["+i+"]"].value != "")
		{
			allDays = allDays + eval(document.forms["buchung"].elements["station_constancy["+i+"]"].value);
		}
	}
	showValueonHtml("allDays", allDays);
	calcPrice();
}

function showStations()
{
	for(var i = 0; i < tmp.length;i++)
	{
		elementName = "stationName["+i+"]";
		if(tmp[i].indexOf("---") == -1)
		{
			showValueonHtml(elementName, stationList[(idxtmp[i]-1)]);
			if(document.forms["buchung"].elements["station_constancy["+i+"]"].value == "")
			{
				document.forms["buchung"].elements["station_constancy["+i+"]"].value = 1;
			}
			document.forms["buchung"].elements["station_id["+i+"]"].value = tmp[i];
			document.forms["buchung"].elements["idtmp["+i+"]"].value = idxtmp[i];
		}else{
			showValueonHtml(elementName, "");
			document.forms["buchung"].elements["station_id["+i+"]"].value = "";
			document.forms["buchung"].elements["station_constancy["+i+"]"].value = "";
		}
	}
	calcAllDays();
	calcDistance();
	calcPrice();
}

function deleteStationFromList(id)
{

	tmp[id] = "---";
	idxtmp[id] = "---";
	showStations();
}

function getStation(id)
{

	var stop = false;
	var ferror = 0;
	
	//auswahl und speichern der Stations-ID
	for(var i = 0; i < tmp.length; i++)
	{
		if(tmp[i].indexOf("---") > -1)
		{
			break;
		}
	}
	tmp[i] = stationIdList[(id-1)];
	idxtmp[i] = id;
	
	//prüfen, das nicht mehr als 15 Stationen in die Liste gesetzt werden
	if(tmp.length >= 15 && i >= 15)
	{
		ferror = 1;
	}
	
	//prüfen, ob vorherige Station nicht die gleiche ist wie gerade gewählte

	if(tmp[(i-1)] == stationIdList[(id-1)] || tmp[(i+1)] == stationIdList[(id-1)])
	{
		ferror = 2;
	}
	switch(ferror)
	{
		case 0:
			showStations();
		break;
		case 1:
			alert(ferror_msg[0]);
		break;
		case 2:
			alert(ferror_msg[1].replace("%%station%%", stationList[(id-1)]));
			
			for(var x = 0; x < tmp.length; x++)
			{
				if(x == i)
				{
					tmp[x] = "---";
					idxtmp[x] = "---";
				}
			}
		break;
	}
	ferror = 0;
}

function calcDistance()
{
	var stationkm = 0;
	lastStation = 0;
	var distList = new Array();
	for(var i = 0; i < tmp.length; i++)
	{
		if(i == 0 || tmp[i].indexOf("---") > -1)
		{
			distList[i] = "---";
		}else{
			//alert(tmp[lastStation] + " => " + tmp[i] );
			distList[i] = matrix[tmp[lastStation]][tmp[i]];
			
			lastStation = i;
		}
	}
	for(i = 0; i < distList.length; i++)
	{
		if(distList[i] != "---")
		{
			stationdist = eval(distList[i]) / 90;
			stationdist = String(stationdist);
			if(stationdist.indexOf("."))
			{
				pos = stationdist.indexOf(".");
				stationdist = stationdist.substr(0, (pos+2));
			}
			stationkm = stationkm + eval(distList[i]);
		}else{
			stationdist = "";
		}
		showValueonHtml("stationdist["+i+"]", stationdist);
	}
	showValueonHtml("stationkm", stationkm);
	stationkm = 0;
}

function setTitle()
{
	for(var i = 0; i < stationList.length;i++)
	{
		if(document.getElementById((i+1)))
		{
			document.getElementById((i+1)).title = stationList[i];
			document.getElementById((i+1)).alt = stationList[i];
		}
	}
}

function openStationInfo(id)
{
	window.open("../backpackers/index.php?action=chooseStation&station_name="+stationList[(idxtmp[id]-1)],"details");
}
var hclose = 0;
function openHelp()
{
	if(hclose == 0)
	{
		document.getElementById("help").style.visibility = "visible";
		hclose = 1;
	}else{
		document.getElementById("help").style.visibility = "hidden";
		hclose = 0;	
	}
}

function closeHelp()
{
	document.getElementById("help").style.visibility = "hidden";
	hclose = 0;
}

