/***************************************************************/
/*  Validierungsfunktionen Fastbooking / Flightmore  (italienisch) */
/***************************************************************/

function init_datum()
{	
	today = new Date ();
	var iHdate = today.getTime() + (1000 *60 * 60 *24) * 3;
	hdate = new Date (iHdate);
	
	s1 = hdate.getDate();
	s2 = hdate.getMonth() + 1;
	s3 = hdate.getFullYear();

	document.startform.mindatum.value = s3*10000 + s2*100 + s1;
	document.startform.maxdatum.value = (s3+1)*10000 + s2*100 + 1;
	
	s = new String(s1);
	if(s.length == 1) s1 = '0' + s1;
	s = new String(s2);
	if(s.length == 1) s2 = '0' + s2;
	strToday = s1 +'.' + s2 + '.' + s3;
	
	document.startform.hdate.value = strToday;
	document.startform.startdatum.value = document.startform.hdate.value;
	document.startform.mindatumt.value = document.startform.hdate.value;
	document.startform.maxdatumt.value = '01.' + s2 + '.' + (s3+1);

	var iRdate = today.getTime() + (1000 *60 * 60 *24) * 30;
	rdate = new Date (iRdate);
	
	s1 = rdate.getDate();
	s2 = rdate.getMonth() + 1;
	s3 = rdate.getFullYear();
	
	s = new String(s1);
	if(s.length == 1) s1 = '0' + s1;
	s = new String(s2);
	if(s.length == 1) s2 = '0' + s2;
	strToday = s1 +'.' + s2 + '.' + s3;
	
	document.startform.rdate.value = new String(strToday);
	document.startform.endedatum.value = document.startform.rdate.value;
}

function check_startmaske (theForm)
{
	
	var dest = theForm.destt.value;
	if (theForm.destt.value == "")
	{
		alert("\nInserisci la tua destinazione !");
		theForm.destt.focus();
		return (false);
	}
	
	
	if (theForm.agentc.value == 'edi') {
		//theForm.destt.value = escape(theForm.destt.value);
	}
	
//  und nun die Datumsbetrachtung

	hdat = extract_datum (theForm.hdate.value);
			
	hd3 = parseInt (hdat / 10000);
	hd1 = hdat % 100;
	hd2 = parseInt ((hdat-hd3*10000)/100.);

	theForm.hdate1.value	= hd1;
	theForm.hdate2.value = hd3*100 + hd2;

	if (! teste_datum (hd1,hd2,hd3))
	{
		alert ("Data non valida !");
		theForm.hdate.focus();
		return (false);
	}
		
	if (hdat < theForm.mindatum.value)
	{
		alert ("Non è possibile partire prima del "+theForm.mindatumt.value+" !");
		theForm.hdate.focus();
		return (false);
	}
					
	rdat = extract_datum (theForm.rdate.value);
			
	rd3 = parseInt (rdat / 10000);
	rd1 = rdat % 100;
	rd2 = parseInt ((rdat-rd3*10000)/100.);

	theForm.rdate1.value	= rd1;
	theForm.rdate2.value = rd3*100 + rd2;

	if (rdat > theForm.maxdatum.value)
	{
		alert ("L'ultima data di ritorno è il "+theForm.maxdatumt.value+" !");
		theForm.rdate.focus();
		return (false);
	}
	if (! teste_datum (rd1,rd2,rd3))
	{
		alert ("Data non valida !");
		theForm.rdate.focus();
		return (false);
	}

	if (rdat < hdat)
	{
		alert ("Ritorno selezionato prima della partenza !");
		theForm.hdate.focus();
		return (false);
	}	
	
	if (theForm.adult.value == 0 && theForm.youth.value == 0) 
	{
		alert("Seleziona almeno 1 passeggero !");
		theForm.adult.focus();	
		return (false);
	}
	if (theForm.adult.value > 0 && theForm.youth.value > 0) 
	{
	alert("Non è possibile richiedere contemporaneamente 1 passeggero adulto e 1 passeggere con lo sconto giovani !");
		theForm.adult.focus();	
		return (false);
	}

//	Window.open("", "flightpopup", "location=0,resizable=1,width=650,height=350, status=yes, scrollbars=yes");
	return (true);
}

function change_mode ()   //  für "weitere Optionen"
{
	document.startform.mode.value = 0;
	return (true);
}

function check_datum (typ)
{
// wir müssen zunächst die möglichen 3 Datumsteile separieren
	if (typ == 1)
		str = new String(document.forms.startform.hdate.value);
	else
		str = new String(document.forms.startform.rdate.value);

	dat = extract_datum (str);

	jahr = parseInt (dat / 10000);
	tag = dat % 100;
	monat = parseInt ((dat-jahr*10000)/100.);

	status = 0;
	
	while (true)
	{	
		
		if (dat <= 0)
		{
			alert ("Inserisci la data nel formato GG.MM.AAAA !");
			break;
		}

		if (dat < parseInt(document.forms.startform.mindatum.value))
		{
			alert ("Non è possibile partire prima del "+document.forms.startform.mindatumt.value+" !");
			break;
		}
		if (dat > parseInt(document.forms.startform.maxdatum.value))
		{
			alert ("L'ultima data di ritorno è il "+document.forms.startform.maxdatumt.value+" !");
			break;
		}

		status = 1;
		break;
	}

	if (status == 0) 
	{
		//if (typ == 1) document.forms.startform.hdate.value = document.forms.startform.startdatum.value;
		//if (typ == 2) document.forms.startform.rdate.value = document.forms.startform.endedatum.value;
	}	
	else
	{
		if (tag < 10) 
			datstr = "0"+tag;
		else
			datstr = tag;
		
		datstr += ".";
		
		if (monat < 10) 
			datstr += "0"+monat;
		else
			datstr += monat;
		datstr += ".";

		datstr += jahr;
		
		if (typ == 1) document.forms.startform.hdate.value = datstr;
		if (typ == 2) document.forms.startform.rdate.value = datstr;
	}	
	
	return (true);		
}

function extract_datum (datstr)
{
//  Umwandlung eines Datums von Textform in Zahlenform, also 'DD.MM.YYYY' nach YYYYMMDD
//  Als zugelassene Feldtrenner sind '/', '-' und '.' erlaubt.

	pos1 = datstr.indexOf (".");	
	if (pos1 < 0) pos1 = datstr.indexOf ("-");
	if (pos1 < 0) pos1 = datstr.indexOf ("/");
	
	if (pos1 < 0) return (0);
	
	pos2 = datstr.indexOf (".",pos1+1);	
	if (pos2 < 0) pos1 = datstr.indexOf ("-",pos1+1);
	if (pos2 < 0) pos1 = datstr.indexOf ("/", pos1+1);
	
	if (pos2 < 0) return (0);

	tag = parseInt (parseFloat(datstr));
	monat = parseInt (parseFloat(datstr.substr(pos1+1,2)));
	jahr = parseInt (parseFloat(datstr.substr(pos2+1,4)));

	if (jahr <= 99) jahr += 2000;

	if (! teste_datum (tag,monat,jahr)) return (0);

	dat = jahr*10000 + monat*100 + tag;	
		
	return (dat);
}

function Kalender_hin(Wert)
{
	posleft = 250;
	postop = 150;

	href = new String ("http://flight1.onlinetravel.ch/cgi-bin/flightmore?mask=fastbook&mode=14&direction=1") + "&vendor=" + document.startform.vendor.value + "&agentc=" + document.startform.agentc.value + "&language=" + document.startform.language.value + "&hdate=" + document.startform.hdate.value + "&hdate1=" + document.startform.hdate1.value + "&hdate2=" + document.startform.hdate2.value + "&mindatum=" + document.startform.mindatum.value + "&maxdatum=" + document.startform.maxdatum.value;
	neu = open(href,"Kalender","width=175,height=170,scrolling=no,left=" + posleft + ",top=" + postop);
}
function Kalender_back(Wert)
{
	posleft = 250;
	postop = 150;

	href = new String ("http://flight1.onlinetravel.ch/cgi-bin/flightmore?mask=fastbook&mode=14&direction=2") + "&vendor=" + document.startform.vendor.value + "&agentc=" + document.startform.agentc.value + "&language=" + document.startform.language.value + "&rdate=" + document.startform.rdate.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=175,height=170,scrolling=no,left=" + posleft + ",top=" + postop);
}

function teste_datum (tag,monat,jahr)
{
	mleng = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
		
	if (tag > 0 && tag <= mleng[monat-1]) return (true);
		
	if (monat == 2 && tag == 29)   //  beim Februar könnte es sich um ein Schaltjahr handeln
	{
      if (! (jahr%100) && (jahr/100)%4 ) return (false);
		if (jahr % 4) return (false);
		return (true);
	}
				
	return (false);
}