// youthtripprice.js by frank thx to QuirksMode
window.onload = init;

function init()
{
	optionTest = true;
	if (!document.forms[0])	//form doesnt exist->nothing to attach, request submit case
		{ 
		optionTest = false;
		return;
		}
	lgth = document.forms[0].placeselect.options.length - 1;
	document.forms[0].placeselect.options[lgth] = null;
	if (document.forms[0].placeselect.options[lgth])
		{ 
		optionTest = false;
		return;
		}
	if (!document.getElementById)
		{ 
		optionTest = false;
		return;
		}
	if (!(document.getElementsByTagName || document.all))
		{ 
		optionTest = false;
		return;
		}
	
	document.getElementById('endprice').firstChild.nodeValue = 'Bitte wählt zuerst einen Termin';
	document.getElementById('detailhide').style.display = 'none';
	document.getElementById('userform').style.display = 'none';
	document.getElementById('jstext').style.display = 'none';

	initeventhandlers();	

	//reset the selection after submit
	if(document.forms[0].dateselect.value && document.getElementById('placeselectindex'))
		{
		dateselected();
		document.forms[0].placeselect.selectedIndex = document.getElementById('placeselectindex').value;
		getprice();
		}
	
} //end init

function initeventhandlers ()
{
	if (!optionTest) return;

	document.forms[0].dateselect.onchange  = dateselected;
	document.forms[0].dateselect.onclick = dateselected;
	document.forms[0].placeselect.onchange  = getprice;
	document.forms[0].placeselect.onclick = getprice;
	document.getElementById('detailbuttons').onclick = function () {document.getElementById('detailhide').style.display = 'block';}
	
	var checkboxes = document.getElementById("priceform").getElementsByTagName("input");
	for (var i=0;i<checkboxes.length;i++)
	{
		checkboxes[i].onclick = getprice;
		checkboxes[i].onchange  = getprice;
	}
	
	if (document.getElementById('gallery'))	//if gallery images available attach handler
		{
	 	var galleryimagesarray = document.getElementById('gallery').getElementsByTagName('IMG');//get all the images in the gallery
		for (var i=0;i<galleryimagesarray.length;i++)	//assign the handlers to all the images in the gallery
			{
			galleryimagesarray[i].onmouseover = function () {document.getElementById('bigpicture').src = '/pictures/'+this.id.substring(1,15)+'.jpg';};
		    galleryimagesarray[i].onclick = function () {document.getElementById('bigpicture').src = '/pictures/'+this.id.substring(1,15)+'.jpg';};
			}
		}

} // end initeventhandlers


function dateselected()
{
	if (!optionTest) return;
	if(!document.forms[0].dateselect.value) return;
	document.forms[0].placeselect.length = 0;	//reset placeselect
	for(i=0;i<places[document.forms[0].dateselect.value].length;i+=3)
	{
		if (!(places[document.forms[0].dateselect.value][i] == 7)) document.forms[0].placeselect.options[i/3] = new Option(places[document.forms[0].dateselect.value][i],i/3);
	}

	document.getElementById('endprice').firstChild.nodeValue = 'Bitte wählt einen Abfahrtsort';
	document.getElementById('jstext').style.display = 'none';
	document.getElementById('userform').style.display = 'none';
} // end dateselected

function getprice()
{
	if (!optionTest) return;
	if (document.forms[0].dateselect.selectedIndex < 0) return;
	if (document.forms[0].placeselect.selectedIndex < 0) return;
	
	var baseprice = baseprices[(document.forms[0].dateselect.value)];
	var startplace = places[(document.forms[0].dateselect.value)];
	var additionalcharge = startplace[((document.forms[0].placeselect.selectedIndex)*3)+1];

	var extracharge = 0;
	var checkboxes = document.getElementById("priceform").getElementsByTagName("input");
	for (var i=0; i<checkboxes.length; i++) 
		{
		if (checkboxes[i].checked)
			{
			extracharge += extras[i]; 
			}
		}

	var price = baseprice + additionalcharge + extracharge;
	
	document.getElementById('endprice').firstChild.nodeValue = 'Der Preis für diese Kombination beträgt ' + price + ' Euro pro Person.';

	if ((!oldprices[0] == 0) && (!oldprices[document.forms[0].dateselect.value] == 0))
		{
		var oldbaseprice = oldprices[document.forms[0].dateselect.value];
		var oldprice = oldbaseprice + additionalcharge + extracharge;
		document.getElementById('endprice').firstChild.nodeValue += ' (war ' + oldprice + ' € p.P)'
		}

	document.getElementById('jstext').firstChild.nodeValue = 'Um diese Reise unverbindlich bei unserem Team anzufragen füllt bitte das Formular aus.';
	document.getElementById('jstext').style.display = 'block';
	document.getElementById('userform').style.display = 'block';

	document.getElementById('calcprice').value = price;
	document.getElementById('startplacename').value = startplace[(document.forms[0].placeselect.selectedIndex)*3]; //the startplacename is always the first element (price secound)
	document.getElementById('startplaceid').value = startplace[((document.forms[0].placeselect.selectedIndex)*3)+2]; //the startplaceid is always the third element (price secound)
} //end getprice
