
//
// Erstellt am: 02.07.2008
// Autor: M
//
// Beschreibung
//
// ***
//
// LETZTES UPDATE: 27.11.2008, Korrektur der Telefonnummer.
//

DHTML = (document.getElementById || document.all || document.layers);
if (DHTML) {



/*
 * Returns either true or false, depending if the regexp matches the passed
 * string.
 */
function
is_email (str)
{
	str = new String(str);

	res =	(	str.search('@') >= 1				&&
			str.lastIndexOf('.') >  str.search('@')		&&
			str.lastIndexOf('.') >= str.length-5)

	return res;
}



/*
 * Checks if all needed fields have been filled before submitting the form.
 */
function
check_fields ()
{
	var cust_first	= document.getElementById ("Best_Vorname").value;
	var cust_name	= document.getElementById ("Best_Name").value;
	var cust_street	= document.getElementById ("Best_Strasse").value;
	var cust_city	= document.getElementById ("Best_Ort").value;
	var cust_tel	= document.getElementById ("Best_Telefon").value;
	var cust_mail	= document.getElementById ("Best_Email").value;
	var dec_fname	= document.getElementById ("Verst_Vorname").value;
	var dec_lname	= document.getElementById ("Verst_Nachname").value;
	var fhof	= document.getElementsByName ("Friedhof")[0].value;

	if (	cust_first	&&
		cust_name	&&
		cust_street	&&
		cust_city	&&
		cust_tel	&&
		dec_fname	&&
		dec_lname	&&
		cust_mail	&&
		fhof)
	{
		if (is_email (cust_mail)) {
			return check_date ();
		}
		else {
			window.alert ("Die von Ihnen angegebene Email-Adresse "+
					"scheint ungültig zu sein.");
		}
	}
	else {
		window.alert ("Bitte erfassen Sie alle erforderlichen Daten "+
			"(mit einem * gekennzeichnete Felder)");
	}

	return false;
}



/*
 * It is not allowed to order less than two days before the date of the
 * ceremony. This function checks the date fields and if necessary informs the
 * user of this.
 */
function
check_date ()
{
	var rc = false; /* init */

	/* Get the values of the date elements: */
	var selday	= document.getElementsByName ("Tag")[0].value;
	var selmonth	= document.getElementsByName ("Monat")[0].value;
	var selyear	= document.getElementsByName ("Jahr")[0].value;
	var selhour	= document.getElementsByName ("Stunde")[0].value;
	var selmin	= document.getElementsByName ("Minute")[0].value;

	/* Check time input: */
	if (	isNaN(selhour)      ||
		isNaN(selmin)       ||
		selhour > 24        ||
		selhour < 0         ||
		selmin  > 60        ||
		selmin  < 0)
	{
		/* If the selected time is invalid, manipulate the selected 
		 * day so it holds an invalid data. This will force the next 
		 * comparison to throw an alert box: */
		selday = "asdf";
	}

	/* Has a valid date been chosen? */
	if (is_valid_date (selday, selmonth, selyear)) {
		/* To get the days in between the current date and the one 
		 * of the ceremony, the selected date is converted into 
		 * milliseconds before subtracting it from the current 
		 * timestamp: */
		var selms = Date.parse(selmonth+"/"+selday+"/"+selyear+" "+
				selhour+":"+selmin+":00");

		/* Convert current datetime into milliseconds: */
		var curdat	= new Date();
		var curday	= curdat.getDate();
		var curmonth	= curdat.getMonth() + 1;   /* starts with 0! */
		var curyear	= get_current_year();
		var curhour	= curdat.getHours();
		var curmin	= curdat.getMinutes();
		var curms	= Date.parse(curmonth+"/"+curday+"/"+curyear+
					" "+curhour+":"+curmin+":00");
		/* Add two days to the current datetime - the resulting 
		 * datetime tells the earliest possible ceremony: */
		var earliest = curms + 172800000;
		/* 48 hours * 60 minutes * 60 seconds * 1000 milliseconds */

		var msgtext = "Die Bestellung muss mind. 2 Tage vor der "+
			"Trauerfeier erfolgen.\nFür dringende Fälle nehmen "+
			"Sie mit uns bitte telefonisch Kontakt auf: "+
			"0043/1/271 82 51";

		/* Date of ceremony in the past? */
		if (selms < curms) {
			window.alert (msgtext);
		}
		else {
			/* Two days at least in between? */
			if (selms < earliest) {
				window.alert (msgtext);
			}
			else {
				rc = true;
			}
		}
	}
	else {
		window.alert ("Bitte wählen Sie ein gültiges Datum der "+
			"Trauerfeier aus.");
	}

	return rc;
}



/*
 * Checks if the passed date is valid. This check is done so no one does select
 * the 30th of february and so forth.
 */
function
is_valid_date (day, month, year)
{
	var rc = false; /* init return code */

	if (	!isNaN(day)	||	/* Are day and   */
		!isNaN(month)	||	/* month and     */
		!isNaN(year))		/* year numbers? */
	{
		var curyear = get_current_year ();

		/* Validate year: */
		if (	year < curyear   ||
			year > curyear + 50)
		{
			return rc;
		}

		/* Validate month: */
		if (	month < 0   ||
			month > 12)
		{
			return rc;
		}

		/* Table that assigns the number of days for each month: */
		var month_table = new Array(
			31,	/* January			*/
			65535,	/* February, temp-value		*/
			31,	/* March			*/
			30,	/* April			*/
			31,	/* May				*/
			30,	/* June				*/
			31,	/* July				*/
			31,	/* August			*/
			30,	/* September			*/
			31,	/* October			*/
			30,	/* November			*/
			31);	/* December			*/

		/* For February a temp-value was assigned, because the number 
		 * of days for this month switch every year, but can be 
		 * calculated: */
		if (	((year % 4)	== 0)	&&
			((year % 100)	!= 0)	||
			((year % 400)	== 0))
		{
			month_table[1] = 29;
		}
		else {
			month_table[1] = 28;
		}

		/* Validate day: */
		if (	day > 0 &&
			day <= month_table[month-1])
		{
			rc = true;  /* finally: valid date */
		}
	}

	return rc;
}



/*
 * Returns the current year, 4 bytes.
 */
function
get_current_year ()
{
	var dato = new Date ();
	var year = dato.getYear();

	/* Some browser use only two bytes for saving the current year; so the
	* following check will add 1900 years if neccessary: */
	if (year < 999) {
		year += 1900;
	}

	return year;
}



/*
 * If an input field with the passed id can be found, a popup opens and lists
 * all possible sayings. The user is able to click onto once which auto-closes
 * the popup and writes the saying into the matching field.
 */
function
choose_saying (input_name)
{
	krnzbst2_newwin ("?for="+input_name);
}


function
krnzbst2_newwin (url)
{
	// Calculate the screen coordinates where the window should open:
	var width	= (arguments.length > 1) ? arguments [1] : 550;
	var height	= (arguments.length > 2) ? arguments [2] : 400;
	var x		= screen.availWidth/2	- width/2;
	var y		= screen.availHeight/2	- height/2;

	var newwin = window.open(url,"",
		"width="+width+",height="+height+",left="+x+ 
		",top="+y+",screenX="+x+",screenY="+y+ 
		",scrollbars=yes");
}


} /* if DHTML */



