/*********************************************************/
/*  Validierungsfunktion für die Startmaske STA GETS     */
/*********************************************************/

var p2p = 0;

function init_page ()
{
	if(document.forms.startform.p2p) {
		p2p = parseInt(document.forms.startform.p2p.value);
	}
	
	if (p2p && (document.forms.startform.other.value.length > 1 || document.forms.startform.fromtother.value.length > 1))
	{
		anzeigen ("fromt_suisse", 0);
		anzeigen ("fromt_other", 1);
		document.forms.startform.fromt.value = "other";
		//document.startform.startfromt.value = "other"; // hinzu sma 29.05.08
	}
	else
	{			
		anzeigen ("fromt_suisse", 1);
		anzeigen ("fromt_other", 0);
	}
	// alert(20);
	change_fromt ();
	
	if (document.forms.startform.p2p_start)    //  P2P schon zu Beginn ?
	{
		if (parseInt(document.forms.startform.p2p_start.value) == 1 && parseInt(document.forms.startform.repeat.value) == 0) 
		{
			document.startform.fromt.value = "other";
			//document.startform.startfromt.value = "other"; // changes sma 29.05.08
			change_fromt ();
		}
	}
	
	if (p2p && document.forms.startform.rdestt && document.startform.fromt.value != "other")
	{
		// alert(30);
		document.forms.startform.rdestt.value = document.forms.startform.fromt.options[document.forms.startform.fromt.selectedIndex].text
	}
	// alert(40);
	if (document.forms.startform.rdestt && document.startform.fromt.value == "other")
	{
		// alert(50);
		/*
		if(document.forms.startform.rdestt.nodeName == 'SELECT') {
			// alert(60);
			for (var i = 0; i < document.forms.startform.rdestt.length; i++) {
				if (document.forms.startform.rdestt.options[i].value == document.forms.startform.fromtother.value) {
					document.forms.startform.rdestt.selectedIndex = i;
					break;
				}
			}
		}
		else{
			document.forms.startform.rdestt.value = document.forms.startform.fromtother.value;
		}
		*/
		document.forms.startform.rdestt.value = document.forms.startform.fromtother.value;
	}
	
	change_flugart ();

//  Anzeige und Abfrage der Geburtsdaten bei Jugendlichen und/oder Studenten
	yo = parseInt(document.forms.startform.youth.value);
	sd = parseInt(document.forms.startform.student.value);

	anzeigen ("gb1", 0);
	anzeigen ("gb2", 0);
	anzeigen ("gb3", 0);
	anzeigen ("gb4", 0);

	for (i=0 ; i<yo+sd ; i++)		
	{
		index = "gb"+String(i+1);
		anzeigen (index, 1);
	}
	
	for (i=0 ; i<document.forms.startform.carrt.length; i++)	
	{
		if (document.forms.startform.carrt.options[i].value == document.forms.startform.startcarrt.value)
		{
			document.forms.startform.carrt.selectedIndex = i;
			break;
		}
	}
}

function startform_valid (theForm)
{
	/* 
	for(i = 0; i < theForm.elements.length; i++) {
		alert(theForm.elements[i].name +' -> ' + theForm.elements[i].type);
	}
	*/
	if (document.startform.fromt.value == "other" || document.startform.fromt.value == "")
	{
		theForm.fromtother.style.backgroundColor = "#ffffff";
		if (theForm.fromtother.value == "")
		{
			theForm.fromtother.style.backgroundColor = "#00ffff";
//			alert ("<xsl:value-of select="e:i18n('Bitte gib einen Abflugort an')"/>!");
			alert("\n"+msg[0]+"!");
			theForm.fromtother.focus();
			return (false);
		}
	}
	theForm.destt.style.backgroundColor = "#ffffff";
	if (theForm.destt.value == "")
	{
		theForm.destt.style.backgroundColor = "#00ffff";
//		alert ("<xsl:value-of select="e:i18n('Bitte gib ein Reiseziel')"/>!");
		alert("\n"+msg[1]+"!");
		theForm.destt.focus();
		return (false);
	}

	if (theForm.rfromt && theForm.flugart[0].checked)
	{
		theForm.rfromt.style.backgroundColor = "#ffffff";
		if (theForm.rfromt.value == "")
		{
			theForm.rfromt.style.backgroundColor = "#00ffff";
//			alert ("<xsl:value-of select="e:i18n('Bitte gib einen Abflugort an')"/>!");
			alert("\n"+msg[2]+"!");
			theForm.rfromt.focus();
			return (false);
		}
	}

//  und nun die Datumsbetrachtung

	hd1 = parseInt (parseFloat(theForm.hdate1.value));
	hd3 = parseInt (theForm.hdate2.value/100.);
	hd2 = parseInt (theForm.hdate2.value % 100.);
	hdat = hd3*10000 + hd2*100 + hd1;
		
	if (! teste_datum (hd1,hd2,hd3))
	{
//		alert ("<xsl:value-of select="e:i18n('Dieses Datum gibt es nicht')"/>!");
		alert("\n"+msg[3]+"!");
		theForm.hdate1.focus();
		return (false);
	}
		
	if (hdat < theForm.mindatum.value)
	{
//		alert ("<xsl:value-of select="e:i18n('Du kannst frühestens fliegen ab dem ')"/>"+" "+theForm.mindatumt.value);
		alert("\n"+msg[4]+" "+theForm.mindatumt.value);
		theForm.hdate1.focus();
		return (false);
	}

	if (theForm.flugart[0].checked)
	{
		rd1 = parseInt (parseFloat(theForm.rdate1.value));
		rd3 = parseInt (theForm.rdate2.value/100.);
		rd2 = parseInt (theForm.rdate2.value % 100.);
		rdat = rd3*10000 + rd2*100 + rd1;

		if (rdat > theForm.maxdatum.value)
		{
//			alert ("<xsl:value-of select="e:i18n('Du kannst nicht später zurückreisen als der ')"/>"+" "+theForm.maxdatumt.value);
			alert("\n"+msg[5]+" "+theForm.maxdatumt.value);
			theForm.rdate1.focus();
			return (false);
		}
		if (! teste_datum (rd1,rd2,rd3))
		{
//			alert ("<xsl:value-of select="e:i18n('Dieses Datum gibt es nicht')"/>!");
			alert("\n"+msg[3]+"!");
			theForm.rdate1.focus();
			return (false);
		}

		if (rdat < hdat)
		{
//			alert ("<xsl:value-of select="e:i18n('Die Rückreise kann nur nach der Abreise erfolgen')"/>!");
			alert("\n"+msg[6]+"!");
			theForm.hdate1.focus();
			return (false);
		}
	}
	
	if (navigator.appName == 'Microsoft Internet Explorer') 
	{
		adult_zahl = parseInt (theForm.adult.value);
		youth_zahl = parseInt (theForm.youth.value);
		student_zahl = parseInt (theForm.student.value);
		child_zahl = parseInt (theForm.child.value);
		infant_zahl = parseInt (theForm.infant.value);
	}
	else
	{
		adult_zahl = parseInt (theForm.adult.options[theForm.adult.selectedIndex].value);
		youth_zahl = parseInt (theForm.youth.options[theForm.youth.selectedIndex].value);
		student_zahl = parseInt (theForm.student.options[theForm.student.selectedIndex].value);
		child_zahl = parseInt (theForm.child.options[theForm.child.selectedIndex].value);
		/* machne haben keine infant Auswahl, dann wird infant als hidden Feld auf 0 gesetzt */
		if(theForm.infant.type == 'select-one') {
			infant_zahl = parseInt (theForm.infant.options[theForm.infant.selectedIndex].value);
		}
		else {
			infant_zahl = 0;
		}
	}

	if (adult_zahl == 0 && youth_zahl == 0 && student_zahl == 0) 
	{
//		alert ("<xsl:value-of select="e:i18n('Du musst mindestens einen Erwachsenen oder einen Jugendlichen als Reiseteilnehmer wählen')"/>!");
		alert("\n"+msg[7]);
		theForm.adult.focus();	
		return (false);
	}
	if (adult_zahl != 0 && (youth_zahl != 0 || student_zahl != 0)) 
	{
//alert ("<xsl:value-of select="e:i18n('Du kannst nicht gleichzeitig Erwachsene und Jugendliche buchen, nimm die Buchung bitte in zwei separaten Buchungsvorgängen vor')"/>!");
		alert("\n"+msg[8]+"\n"+msg[14]);
		theForm.adult.focus();	
		return (false);
	}
	if (youth_zahl != 0 && student_zahl != 0) 
	{
//alert ("<xsl:value-of select="e:i18n('Du kannst nicht gleichzeitig Erwachsene und Jugendliche buchen, nimm die Buchung bitte in zwei separaten Buchungsvorgängen vor')"/>!");
		alert("\n"+msg[8]+"\n"+msg[14]);
		theForm.adult.focus();	
		return (false);
	}
	if (youth_zahl != 0 && (child_zahl+infant_zahl > 0)) 
	{
//alert ("<xsl:value-of select="e:i18n('Du kannst nicht gleichzeitig Jugendliche und Kinder buchen, nimm die Buchung bitte in zwei separaten Buchungsvorgängen vor')"/>!");
		alert("\n"+msg[9]+"\n"+msg[14]);
		theForm.adult.focus();	
		return (false);
	}
	if (student_zahl != 0 && (child_zahl+infant_zahl > 0)) 
	{
//alert ("<xsl:value-of select="e:i18n('Du kannst nicht gleichzeitig Studenten und Kinder buchen, nimm die Buchung bitte in zwei separaten Buchungsvorgängen vor')"/>!");
		alert("\n"+msg[10]+"\n"+msg[14]);
		theForm.adult.focus();	
		return (false);
	}
	
	if (youth_zahl || student_zahl)
	{
//		if (youth_zahl) typ = "<xsl:value-of select="e:i18n('Jugendliche(n)')"/>";
//		if (student_zahl) typ = "<xsl:value-of select="e:i18n('Studenten/Studentin')"/>";
		if (youth_zahl) 
		{
			typ = msg[11];
			min = parseInt (theForm.limity_min.value);
			max = parseInt (theForm.limity_max.value);
		}
		if (student_zahl) 
		{
			typ = msg[12];
			min = parseInt (theForm.limits_min.value);
			max = parseInt (theForm.limits_max.value);
		}
	
//  Berechnung der Altersgrenzen

		min_date = calc_datum_diff ((hd3-max-1)*10000+hd2*100+hd1, 1);
		max_date = (hd3-min)*10000+hd2*100+hd1;

		for (i=0 ; i<youth_zahl+student_zahl ; i++)
		{
			e = theForm.elements["pday"+String(i+1)];
			f = theForm.elements["pmonth"+String(i+1)];
			g = theForm.elements["pyear"+String(i+1)];

			if (! e || ! f || ! g) continue;

			rd1 = parseInt (parseFloat(e.value));
			rd2 = parseInt (parseFloat(f.value));
			rd3 = parseInt (parseFloat(g.value));

			if (! teste_datum (rd1,rd2,rd3))
			{
//				alert ("<xsl:value-of select="e:i18n('Dieses Datum gibt es nicht')"/>!");
				alert("\n"+msg[13]+" "+String(i+1)+". "+typ);
				e.focus();
				return (false);
			}

			gebdat = rd3*10000 + rd2*100 + rd1;

			//alert("gebdat = " + gebdat + "\nmax_date =" + max_date + "\nmin_date =" + min_date);
			if (gebdat < min_date || gebdat > max_date || ! rd1 || ! rd2 || ! rd3)
			{
//				alert ("<xsl:value-of select="e:i18n('Bitte gib ein gültiges Geburtsdatum ein, für den oder die ')"/>"+String(i+1)+". "+typ);
				alert("\n"+msg[13]+" "+String(i+1)+". "+typ);
				e.focus();	
				return (false);
			}

		}
	}
	
	/* Bestimmung, ob es sich um einen Gabelflug handelt */
	if(theForm.flugart[0].checked && theForm.rfromt.value.length >= 3 && theForm.rdestt.value.length >= 3) {
		var fromt  = theForm.fromt.value;
		if (fromt == 'other') fromt = theForm.fromtother.value;
		
		if (fromt != theForm.rdestt.value || theForm.destt.value != theForm.rfromt.value) {
			theForm.flugart[0].value = 'G';
		}
		else {
			theForm.flugart[0].value = 'R';
		}
	}
	//alert(theForm.flugart[0].value);
	return (true);
}

function change_backdate (offset)
{
//  Bei Auswahl des Hinflugdatums soll das Rückflugdatum automatisch
//  um <offset> Tage später vorgesetzt werden

	htag = parseInt (parseFloat(document.startform.hdate1.value));
	hjahr = parseInt (document.startform.hdate2.value / 100.);
	hmonat = parseInt (document.startform.hdate2.value % 100.);
	
	rdate = calc_datum_diff (hjahr*10000+hmonat*100+htag,offset);

	rtag = parseInt (rdate % 100);
	rmon = parseInt (rdate / 100.);

	document.startform.rdate1.selectedIndex =  rtag-1; 
	for (i=0 ; i<document.startform.rdate2.length ; i++)
	{
		if (parseInt(parseFloat(document.startform.rdate2.options[i].value)) == rmon)
		{
			document.startform.rdate2.selectedIndex = i;
			break;
		}
	}		

	return (true);	
}

function change_geb_anzeige ()
{
	ad = parseInt(document.forms.startform.adult.value);
	yo = parseInt(document.forms.startform.youth.value);
	sd = parseInt(document.forms.startform.student.value);

	anzeigen ("gb1", 0);
	anzeigen ("gb2", 0);
	anzeigen ("gb3", 0);
	anzeigen ("gb4", 0);

	for (i=0 ; i<yo+sd ; i++)		
	{
		index = "gb"+String(i+1);
		anzeigen (index, 1);
	}
}

function RegionOeffnen(typ) 
{
	if (typ == 1 && document.startform.fromt.value != "other")
	{
		document.startform.fromt.value = "other";
		//document.startform.startfromt.value = "other"; 
		anzeigen ("fromt_suisse", 0);
		anzeigen ("fromt_other", 1);
	}

	neuesFenster=open("","RegionFenster","width=600,height=350,resizable=1,toolbar=1,scrollbars=1");
	window.neuesFenster.focus(); 
} 

/* Ueberarbeitet und ergänzt sma 29.05.08 */
function change_fromt () {
	var f = document.startform;
	var from_is_dropdown = false;
	var dest_is_dropdown = false;
	var p2p_hack = false;
	p2p = 0;
	
	if(f.fromtother.value.length < 2) from_is_dropdown = true;
	if(f.rdestt.nodeName == 'SELECT') dest_is_dropdown = true;
	
	if(document.forms.startform.p2p) {
		var p2p = parseInt(document.forms.startform.p2p.value);
	}
	
	/*****************************************************************************
		Altlast. Fastbookingübergabe erfolgt immer als wäre es ein Point 
		to Point Kunde. -> Problem wenn er aber keine P2P möchte. 
		Wir möchten ein Dropdown, müssen aber den den Wert aus fromtother ermitteln.
	*****************************************************************************/
	if(!p2p && f.fromtother.value.length >=2) {
		from_is_dropdown = true;
		p2p_hack = true;
	}
	
	if (f.fromt.value == "other") {
		anzeigen ("fromt_suisse", 0);
		anzeigen ("fromt_other", 1);
		f.fromtother.focus();
		if (f.rdestt && dest_is_dropdown) {
			for (var i = 0; i < f.rdestt.length; i++) {
				//alert(f.rdestt.options[i].value.substr(pos+1, 3)  + " / " + f.fromtother.value.toUpperCase());
				var pos = f.rdestt.options[i].value.search("/");
				if (f.rdestt.options[i].value.substr(pos+1, 3) == f.fromtother.value.toUpperCase()) {
					//alert(105);
					f.rdestt.selectedIndex = i;
					break;
				}
			}
			f.fromtother.value = f.rdestt.options[f.rdestt.selectedIndex].value;
		}
		else {
			f.rdestt.value = f.fromtother.value;
		}
	}
	else if (f.rdestt) {
		//alert("from_is_dropdown is " + from_is_dropdown);
		//alert("dest_is_dropdown is " + dest_is_dropdown);
		
		if(from_is_dropdown && dest_is_dropdown) {
			if(p2p_hack) {
				for (var i = 0; i < f.fromt.length; i++) {
					var pos = f.fromt.options[i].value.search("/");
					if (f.fromt.options[i].value.substr(pos+1, 3) == f.fromtother.value.toUpperCase()) {
						f.fromt.selectedIndex = i;
						break;
					}
				}
				/* Nur beim ersten Mal darf der übergebene fromtother-Wert verwendet werden. Sonst springt das Dropdown immer wieder auf diesen ersten Wert */
				f.fromtother.value = "";
			}
			f.rdestt.selectedIndex = f.fromt.selectedIndex;
			
		}
		else if(from_is_dropdown && !dest_is_dropdown) {
			f.rdestt.value = f.fromt.options[f.fromt.selectedIndex].value; // changed 20081120 vorher selectedIndex].text gab ungewünschte OJ Anfragen (prüfen auf Nebenwirkungen)
		}
		else if(!from_is_dropdown && dest_is_dropdown) {
			for (var i = 0; i < f.rdestt.length; i++) {
				//alert(f.rdestt.options[i].value.substr(pos+1, 3)  + " / " + f.fromtother.value.toUpperCase());
				var pos = f.rdestt.options[i].value.search("/");
				if (f.rdestt.options[i].value.substr(pos+1, 3) == f.fromtother.value.toUpperCase()) {
				//alert(105);
				f.rdestt.selectedIndex = i;
				break;
				}
			}
			f.fromtother.value = f.rdestt.options[f.rdestt.selectedIndex].value;
		}
		/*****************************************************************************
			das müsste eigentlich oben abgehandelt werden, aber es schein Bedingungen zu geben, 
			wo f.fromt.value nicht korrekt auf "other" gesetzt wird. -> prüfen
		*****************************************************************************/
		else if(!from_is_dropdown && !dest_is_dropdown) {
			// alert(80);
			f.rdestt.value = f.fromtother.value;
		}
	}
}



function change_rfromt ()
{
	if (document.forms.startform.rfromt)
	{
		if(document.forms.startform.destt.nodeName == 'SELECT') {
			document.forms.startform.rfromt.selectedIndex = document.forms.startform.destt.selectedIndex;
		}
		else {
			document.forms.startform.rfromt.value = document.forms.startform.destt.value;
		}
	}
}

/* Wenn es sich bei rfromt um ein dropdown handelt */
function change_dd_rfromt() {
	document.forms.startform.rfromt.selectedIndex = document.forms.startform.destt.selectedIndex;
}
	
function change_flugart ()
{
	if (document.startform.flugart[0].checked)
	{
		document.startform.rdate1.disabled = 0;
		document.startform.rdate2.disabled = 0;
		document.startform.rtime.disabled = 0;
		document.startform.rfromt.disabled = 0;
		document.startform.rdestt.disabled = 0;
		anzeigen ("Kalender", 1);
	}
	if (document.startform.flugart[1].checked)
	{
		document.startform.rdate1.disabled = "true";
		document.startform.rdate2.disabled = "true";
		document.startform.rtime.disabled = "true";
		document.startform.rfromt.disabled = "true";
		document.startform.rdestt.disabled = "true";
		anzeigen ("Kalender", 0);
	}
}

function Kalender()
{
	posleft = 250;
	postop = 150;

	href = document.startform.cal_url.value + "&mode=14&direction=1" + "&unique_id=" + document.startform.unique_id.value + "&hdate1=" + document.startform.hdate1.value + "&hdate2=" + document.startform.hdate2.value + "&rdate1=" + document.startform.rdate1.value + "&rdate2=" + document.startform.rdate2.value + "&mindatum=" + document.startform.mindatum.value + "&maxdatum=" + document.startform.maxdatum.value;
	neu = open(href,"Kalender","width=380,height=250,scrolling=no,left=" + posleft + ",top=" + postop);
}


