// JavaScript Document
var validateErrors =
{
	prefix:				'Folgende Angaben fehlen oder sind fehlerhaft:\n\n',
	postfix:			'\nWir können Ihre Bestellung erst abwickeln, wenn\nalle nötigen Angaben vollständig sind!',
	no_ticket_selected:	' - Bitte wählen Sie mindestens ein Ticket.\n',
	name_empty:			' - Name fehlt\n',
	vorname_empty:		' - Vorname fehlt\n',
	address_empty:		' - Straße, Haus-Nr. fehlt\n',
	plz_empty:			' - PLZ und Ort fehlt\n',
	email_empty:		' - e-Mail-Adresse fehlt\n',
	email_invalid:		' - e-Mail-Adresse ist fehlerhaft\n'
};

function calculateTotalCost(frm)
{
	var t1q = parseInt(frm.ticketsType1.value);
	var t2q = parseInt(frm.ticketsType2.value);
	var t3q = parseInt(frm.ticketsType3.value);
	var t4q = parseInt(frm.ticketsType4.value);
	var dscnt = frm.discountValue.checked;
	var total = ((dscnt == true) ? (t1q*50) : (t1q*60)) + ((dscnt == true) ? (t2q*140) : (t2q*150)) + ((dscnt == true) ? (t3q*115) : (t3q*125)) + ((dscnt == true) ? (t4q*240) : (t4q*250));
	if(total > 0) 
	{
		frm.transferCosts.options.item(1).selected = true;
		total += 6;
	}
	else frm.transferCosts.options.item(0).selected = true;
	frm.totalCost.value = total;
}

function ticketOrderFormValidate(frm)
{
	var errors = '';
	if(parseInt(frm.totalCost.value) == 0) errors += validateErrors.no_ticket_selected;
	var customerName = trimString(frm.customerName.value);
	if(customerName.length == 0) errors += validateErrors.name_empty;
	var customerVorname = trimString(frm.customerVorname.value);
	if(customerVorname.length == 0) errors += validateErrors.vorname_empty;
	var customerAddress = trimString(frm.customerAddress.value);
	if(customerAddress.length == 0) errors += validateErrors.address_empty;	
	var customerCity = trimString(frm.customerCity.value);
	if(customerCity.length == 0) errors += validateErrors.plz_empty;	
	var customerEmail = trimString(frm.customerEmail.value);
	if(customerEmail.length == 0) errors += validateErrors.email_empty;
	else if(!customerEmail.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/)) errors += validateErrors.email_invalid;
	if(errors.length > 0)
	{
		window.alert(validateErrors.prefix + errors + validateErrors.postfix); 
		return false;
	}
	else frm.submit();
}

function trimString(str)
{
	str = str.replace(/^\s+/, '');
	return str.replace(/\s+$/, '')
}