﻿// This is open source, based on GNU's General Public License (GPL).
// See http://www.gnu.org/copyleft for further details about the GNU GPL.



// Order-form configuration items
var photoOmitCharge = false;
var photoOmitDeliveryFee = false;
var photoOrderEmail = "fotograf" + at + GetDomain( document.location.toString() );
var photoOrderSubject = "Bilderbestellung";
var photoReferenceFieldId = "reference-field";
var photoNormalBackgroundColor = "";
var photoErrorBackgroundColor = "#FFE0E0";
var photoOrderCurrency = "Euro";
var photoDecimalPoint = ",";
var photoItemWidth = 225 + 75;
var photoItemPadding = 40;

// Templates for order-form components (refers to classes of 'content.css')
var photoUsageNotes =
	"<p>Mit Hilfe dieses Formulars k"+oe+"nnen Sie Bilder des Konzertes als Ausbelichtung auf hochwertigem Fotopapier " +
	"bestellen. Bitte geben Sie hierzu f"+ue+"r die einzelnen Bilder jeweils die Zahl der gew"+ue+"nschten Abz"+ue+"ge ein. " +
	"Jeder Klick in ein Bestellfeld erh"+oe+"ht die Bilderzahl, manuelle Korrekturen sind jederzeit m"+oe+"glich. " +
	"Klicken Sie auf ein Bild, um es in einer h"+oe+"heren Aufl"+oe+"sung zu betrachten.</p>\n" +
	"<p>Mit Ihrer Bilderbestellung unterst"+ue+"tzen Sie die F"+oe+"rderprojekte des Vereins „Kulturscheune Schnaittach e.&thinsp;V.“. " +
	"Den <a href=\"#Uebersicht\">Gesamtpreis Ihrer Bestellung</a>, den darin enthaltenen F"+oe+"rderbetrag sowie " +
	"<a href=\"#Zahlweise\">Hinweise zur Zahlweise</a> finden Sie am Ende des Formulars.</p>\n";
var photoTableStart =
	"<table width=\"100%\">\n";
var photoTableRowStart =
	"<tr>\n";
var photoSelection =
	"<td class=\"form-field\" style=\"padding-left: ${padding}\">" +
	"<input name=\"${fieldname}\" type=\"text\" size=\"2\" style=\"text-align: right\" " +
	"onclick=\"photoUpdateQuantitiesAndPrice( this, 1 )\" onchange=\"photoUpdateQuantitiesAndPrice( this )\">" +
	"&nbsp;&nbsp;${image}</td>\n";
var photoImageStyle = 
	" style=\"vertical-align: top\" ";
var photoEmptyCell =
	"<td>&nbsp;</td>";
var photoTableRowEnd =
	"</tr>\n";
var photoTableEnd =
	"</table>\n";
var photoPaperFormats = // value: format; basePrice; itemPrice; donation; postage
    {
    	Start:  "<p style=\"margin-top: 4ex\"><span style=\"display: inline-block; width: 15em\"><a name=\"Uebersicht\">Papierformat:</a></span>" +
            	"<select name=\"Papierformat\" onchange=\"photoUpdateQuantitiesAndPrice( this )\">\n",
    	G10:    "<option value=\"10x15 Hochglanz; 2.00; 2.45; 0.50; 2.00\">10&thinsp;&times;&thinsp;15 Hochglanz</option>\n",
    	M10:    "<option value=\"10x15 Edelmatt;  2.00; 2.45; 0.50; 2.00\">10&thinsp;&times;&thinsp;15 Edelmatt</option>\n",
    	G13:    "<option value=\"13x20 Hochglanz; 2.00; 3.45; 1.00; 2.00\">13&thinsp;&times;&thinsp;20 Hochglanz</option>\n",
    	M13:    "<option value=\"13x20 Edelmatt;  2.00; 3.45; 1.00; 2.00\">13&thinsp;&times;&thinsp;20 Edelmatt</option>\n",
    	G20:    "<option value=\"20x30 Hochglanz; 2.00; 5.95; 2.00; 2.00\">20&thinsp;&times;&thinsp;30 Hochglanz</option>\n",
    	M20:    "<option value=\"20x30 Edelmatt;  2.00; 5.95; 2.00; 2.00\">20&thinsp;&times;&thinsp;30 Edelmatt</option>\n",
    	End:    "</select></p>\n",
    	Default:{ G10: false, M10: false, G13: true, M13: false, G20: false, M20: false }
    };
var photoPaymentChoice =
	"<p><span style=\"display: inline-block; width: 15em\"><a href=\"#Zahlweise\">Zahlweise</a>:</span>" +
	"<select name=\"Zahlweise\" onchange=\"photoUpdateQuantitiesAndPrice( this )\">\n" +
	"<option value=\"Vorkasse;  0.00\" selected=\"\">Vorkasse</option>\n" +
	"<option value=\"Nachnahme; 4.00\">Nachnahme</option>\n" +
	"</select></p>\n";
var photoOrderSummary = 
	"<p><span style=\"display: inline-block; width: 15em\">Ausgew"+ae+"hlte Bilder:</span><span id=\"images\"></span><br>\n" +
	"<span style=\"display: inline-block; width: 15em\">Bestellte Abz"+ue+"ge:</span><span id=\"pieces\"></span></p>\n" +
	"<p><span style=\"display: inline-block; width: 15em\">Auftragspauschale:</span><span id=\"charge\"></span><br>\n" +
	"<span style=\"display: inline-block; width: 15em\">Preis f"+ue+"r Bildabz"+ue+"ge:</span><span id=\"price\"></span><br>\n" +
	"<span style=\"display: inline-block; width: 15em\">Porto und Versand:</span><span id=\"postage\"></span><br>\n" +
	"<span style=\"display: inline-block; width: 15em\">Gesamtpreis:</span><span id=\"total\"></span></p>\n<p>" +
	"<span style=\"display: inline-block; width: 15em\">Enthaltener F"+oe+"rderbetrag:</span><span id=\"charity\"></span><br>\n" +
	"Alle Preisangaben inkl. MwSt.</p>\n" +
	"<input type=\"hidden\" name=\"Einzelbilder\" value=\"\">\n" +
	"<input type=\"hidden\" name=\"Abzuege\" value=\"\">\n" +
	"<input type=\"hidden\" name=\"Grundpreis\" value=\"\">\n" +
	"<input type=\"hidden\" name=\"Bilderpreis\" value=\"\">\n" +
	"<input type=\"hidden\" name=\"Foerderbetrag\" value=\"\">\n" +
	"<input type=\"hidden\" name=\"Versand\" value=\"\">\n" +
	"<input type=\"hidden\" name=\"Gesamtpreis\" value=\"\">\n";
var photoAdditionalComments =
	"<p class=\"form-field\"><strong>Erg"+ae+"nzende Angaben:</strong><br>\n" +
	"<textarea name=\"Anmerkungen\" cols=\"60\" rows=\"6\"></textarea></p>";
var photoCheckFailureMessage =
    "Fehler bei der "+Ue+"berpr"+ue+"fung des Feldinhaltes. Bitte senden Sie eine kurze Fehlermeldung an <" + 
    photoOrderEmail + "> unter Nennung des Formularfeldes, Ihrer Eingabe und des verwendeten Web-Browsers.";
var photoDeliveryAddress = 
	"<p class=\"form-field\"><strong>Ihr vollst"+ae+"ndiger Name:</strong><br>\n" +
	"<input name=\"realname\" type=\"text\" size=\"60\" onchange=\"return photoCheckPattern( this, photoRealnamePattern, photoRealnameMessage )\"></p>\n" +
	"<p class=\"form-field\"><strong>Stra"+sz+"e und Hausnummer:</strong><br>\n" +
	"<input name=\"Strasse\" type=\"text\" size=\"60\" onchange=\"return photoCheckPattern( this, photoStreetPattern, photoStreetMessage )\"></p>\n" +
	"<p class=\"form-field\"><strong>Postleitzahl und Ort:</strong><br>\n" +
	"<input name=\"Ort\" type=\"text\" size=\"60\" onchange=\"return photoCheckPattern( this, photoTownPattern, photoTownMessage )\"></p>\n" +
	"<p class=\"form-field\"><strong>Land (Lieferung nur innerhalb Mitteleuropas):</strong><br>\n" +
	"<input name=\"Land\" type=\"text\" size=\"60\" value=\"Deutschland\" onchange=\"return photoCheckPattern( this, photoCountryPattern, photoCountryMessage )\"></p>\n" +
	"<p class=\"form-field\"><strong>E-Mail-Adresse:</strong><br>\n" +
	"<input id=\"" + photoReferenceFieldId + "\" name=\"email\" type=\"text\" size=\"60\" onchange=\"return photoCheckPattern( this, photoEmailPattern, photoEmailMessage )\"></p>\n";
var photoInvalidDataMessage =
	"Ung"+ue+"ltige Eingabe; bitte korrigieren Sie die rot unterlegten Eingabefelder.\n\n" +
	"Wenn Sie mit der Maus "+ue+"ber ein ung"+ue+"ltiges Eingabefeld fahren, erhalten Sie einen " +
	"Hinweis zum Fehler und zur notwendigen Korrektur.";
var photoImageSelectionMessage = 
	"Sie haben noch kein Bild ausgew"+ae+"hlt; bitte w"+ae+"hlen Sie mindestens ein Bild aus, " +
	"von dem Sie Abz"+ue+"ge bestellen m"+oe+"chten.";
var photoRealnamePattern = 
	"^\\S+\\s+\\S.+$";
var photoRealnameMessage = 
	"Ung"+ue+"ltiger Name; bitte geben Sie Ihren Namen als \"Vorname(n) Nachname\" ein.";
var photoStreetPattern = 
	"^\\S.+\\s\\d.+$";
var photoStreetMessage = 
	"Ung"+ue+"ltige Stra"+sz+"e oder Hausnummer; bitte geben Sie Ihre Anschrift als \"Stra"+sz+"e Hausnummer\" ein.";
var photoTownPattern = 
	"^\\d{5}\\s+\\S.+$";
var photoTownMessage = 
	"Ung"+ue+"ltige Postleitzahl oder ung"+ue+"ltiger Wohnort; bitte geben Sie Ihre Anschrift als \"PLZ Wohnort\" " + 
	"mit vier- bzw. f"+ue+"nfstelliger Postleitzahl ein.";
var photoCountryPattern = 
	"^\\S{3}";
var photoCountryMessage = 
	"Ung"+ue+"ltiger Landesname; bitte geben Sie das Zustellungsland f"+ue+"r Ihre Anschrift ohne Abk"+ue+"rzung ein.";
var photoEmailPattern =
	"^\\S+@(\\S{2,}\\.)+\\S{2,}$";
var photoEmailMessage =
	"Ung"+ue+"ltige E-Mail-Adresse; bitte geben Sie Ihre E-Mail-Adresse als \"name@domain.tld\" ein, wobei die " +
	"jede Domain sowie die Top-Level Domain (TLD) aus mindestens zwei Zeichen bestehen muss.";
var photoFormSubmission =
	"<p class=\"form-text\"><input type=\"submit\" name=\"submit\" value=\"Bestellung abschicken\" onclick=\"return photoCheckForm( this.form )\">\n" +
	"<input type=\"reset\" name=\"reset\" value=\"L"+oe+"schen\" onclick=\"photoResetQuantitiesAndPrice( this )\"></p>\n" +
	"<input type=\"hidden\" name=\"recipient\" value=\"${email}\">\n" +
	"<input type=\"hidden\" name=\"sort\" value=\"order:${fieldlist},Papierformat,Zahlweise,Einzelbilder,Abzuege," +
	"Grundpreis,Bilderpreis,Foerderbetrag,Versand,Gesamtpreis,Anmerkungen,realname,Strasse,Ort,Land,email\">\n" +
	"<input type=\"hidden\" name=\"subject\" value=\"${subject}\">\n" +
	"<input type=\"hidden\" name=\"redirect\" value=\"/orderform-ok.htm\">\n";
var photoServiceProvider =
	"<div style=\"height: 5px; margin-top: 3ex; background-image: url( separator.gif ); background-repeat: repeat-x\"></div>\n" +
	"<p style=\"margin-top: 3ex\">Dieser Bilderdienst ist keine Leistung des Vereins „Kulturscheune Schnaittach e.&thinsp;V.“ " +
	"bzw. der Simmelsdorfer M"+ue+"hle und erfolgt auch nicht in deren Auftrag bzw. auf deren Rechnung. Das Bestellformular " +
	"ist lediglich auf der Website der Simmelsdorfer M"+ue+"hle hinterlegt, um den Bestellvorgang f"+ue+"r Sie so " +
	"einfach wie m"+oe+"glich zu gestalten. Alle Bilder erhalten Sie direkt vom akkreditierten Fotografen " +
	"der Simmelsdorfer M"+ue+"hle:</p>\n" +
	"<p style=\"padding-left: 4em\">Stefan Hofmann<br>\n" +
	"Johannisgasse 13<br>\n" +
	"91220 Schnaittach<br><br>\n" +
	"<span style=\"display: inline-block; width: 5em\">Telefon:</span>(09153) 997928<br>\n" +
	"<span style=\"display: inline-block; width: 5em\">E-Mail:</span><a href=\"javascript: " +
	"OpenMailForm( 'fotograf', 'Anfrage zur Bilderbestellung' )\">fotograf<span>@</span>die-simmelsdorfer-muehle<span>.</span>de</a></p>";
var photoAdvancePayment =
	"<p>Bei <a name=\"Zahlweise\">Zahlung per Vorkasse</a> <a href=\"#\" onclick=\"window.print()\">drucken Sie dieses Formular</a> " +
	"bitte vor Absenden der Bestellung aus und "+ue+"berweisen Sie den errechneten Gesamtpreis auf folgendes Konto:</p>\n" +
	"<p style=\"padding-left: 4em\">Stefan Hofmann<br>\n" +
	"<span style=\"display: inline-block; width: 20em\">Sparkasse N"+ue+"rnberg, BLZ 760 501 01</span>BIC/SWIFT-Code: SSKNDE77XXX<br>\n" +
	"<span style=\"display: inline-block; width: 20em\">" + DescrambleString( "27D1A8DBCF7000EE71251DEFBA70A7CA10250DCF1A", 0x93 ) + 
	"</span>" + DescrambleString( "07035A789A70A09DF0A44CEF5A71B7CA80245D9E3A21974AD024CDDF6A51", 0x93 ) + "</p>";
var photoDeliveryConditions =
	"<p>Die Lieferzeit f"+ue+"r Ihre bestellten Abz"+ue+"ge betr"+ae+"gt 1&ndash;2 Wochen, bei Vorkasse " +
	"ab Eingang der Zahlung. R"+ue+"ckgabe oder Umtausch von Abz"+ue+"gen sind ausgeschlossen, sofern " +
	"die Ware nicht schadhaft ist.</p>\n";



// Photo Orderform 1.6.0, Copyright (c) 2009-2011, Stefan Hofmann

// This script transforms a form containing photos for visual display into a form
// with which copies of these photos can be selected and ordered by e-mail. The 
// form starts with a brief usage note, followed by the orderable items. Beneath
// the orderable items, the copies' format and their delivery option can be chosen.
// Next, the number of selected photos, the total number of copies, their unit price 
// and the total price are shown. Then follow the fields to enter the name of the
// recipient and the delivery address plus the recipient's e-mail address. The form
// ends with some administrative information about the supplier, the advance-payment
// instructions, and the time until delivery.

// The visually displayed photos must be contained within a form tag which already
// provides all necessary data to submit the filled form to the web server. In
// particular, the form tag must comprise the destination URL and submission method 
// as well as other data required by the hoster's offered form-submission interface.
// Photos whose extension starts with a double dot are ignored.

// The form must have the ID 'orderform'. Its fields are derived from the images' 
// filenames, replacing intermingled punctuation characters by underscores. Each 
// photo may be embraced by an '<a>' tag, which provides a link to a version with 
// a higher resolution. The '<a>' tag is modified such that clicked photos open in
// a new window.

// The available paper format can be specified by an optional array containing the
// property identifiers of the photoPaperFormats object. If unspecified, a default
// selector for "Q20" and "A4" (default choice) will be created.


function CreatePhotoOrderForm( form, paperFormats )
{
	var html;
	var link;
	var href;
	var next;
	var formId = form.id;
	var formWidth = form.clientWidth;
	var newHtml = "";
	var busy = true;
	var start = 0;
	var end = 0;
	var width = 0;
	var i = 0;
	
	
	// transform all image links not pertaining to a slide show to open // in a new window
	html = form.innerHTML;
	while( busy )
	{
		// try matching a link tag enclosing an image tag
		start = html.search( /<a /i );
		end = html.search( /<\/a>/i );
		if( start >= 0 && end >= 0 && start < end )
		{
			link = html.substring( start, end + 4 );
			if( link.search( /<img /i ) >= 0 )
			{
				// image within link, so modify the link to open in a new window
				next = link.search( />/ ) + 1;
				href = link.substring( 0, next );
				href = href.replace( /\s*target=("[^"">]*"|[^\s>]*)([\s>])/, "$2" );
				if( href.search( /javascript:/i ) < 0 )
				{
				    // open enlarged image in a new window only in case of a regular link
    				href = href.replace( />/, " target=\"_blank\">" );
    			}
				newHtml += html.substring( 0, start ) + href + link.substring( next, end + 4 );
				html = html.substring( end + 4 );
			}
			else
			{
				// no image within link, so skip the link
				newHtml += html.substring( 0, end + 4 );
				html = html.substring( end + 4 );
			}
		}
		else
		{
			// stop parsing if no more image tags
			newHtml += html;
			busy = false;
		}
	}
	form.innerHTML = newHtml;
	
	var headings = document.getElementsByTagName( "h1" );
	var paragraphs = document.getElementsByTagName( "p" );
	var images = new Array();
	var fields = new Array();
	var fieldname = undefined;
	var matches = undefined;
	var choice = undefined;
	var subject = undefined;

	// collect images contained inside the form
	form = document.getElementById( formId );
	html = form.innerHTML;
	busy = true;
	while( busy )
	{
		// try matching a link tag enclosing an image tag
		start = html.search( /<a /i );
		end = html.search( /<\/a>/i );
		if( start < 0 || end < 0 || start >= end )
		{
			// otherwise, try matching a pure image tag
			start = html.search( /<img /i );
			end = html.substring( start + 1 ).search( />/ );
		}		
		if( start >= 0 && end >= 0 && start < end )
		{
			// store found (linked) image tag, but skip those with double-dot extension
			var image = html.substring( start, end + 4 ).replace( /\n/g, "" );
			html = html.substring( end + 4 );
			if( image.search( /src=[^>]*[^.]\.(tif|jpg|png|gif)/i ) >= 0 )
			{
			    // replace slide-show function call with original link, which is stored in 
			    // a specially formatted name tag; currently disabled
			    if( false && image.search( /name="''/ ) >= 0 )
			    {
			        href = image.replace( /^.*\s+name="''([^"''"]*)''".*$/i, "$1" );
			        image = image.replace( /(\s+href)="[^""]*"/i, "$1=\"" + href + "\"" );
			    }
    			images.push( image );
    		}
		}
		else
		{
			// stop parsing if no more image tags
			busy = false;
		}
	}
	
	// do not create an orderform if no orderable items found
	if( images.length < 1 )
	{
	    return( false );
	}
	
	// create order form from collected image tags
	html = photoUsageNotes + photoTableStart;
	for( i = 0; i < images.length; i++ )
	{
		// retrieve image filename
		matches = images[ i ].match( /klein\/([^.]+)\.\w{3}/ );
		fieldname = "Anzahl_" + matches[ 1 ].replace( /-/g, "_" );
		fields.push( fieldname );
		// substitutes placeholders in photo-selection template
		if( width == 0 )
		{
			// first column starts without padding
			choice = photoSelection.replace( /\$\{padding\}/m, "0px" );
		}
		else
		{
			// subsequent columns have padding to column on the left
			choice = photoSelection.replace( /\$\{padding\}/m, photoItemPadding + "px" );
		}
		choice = choice.replace( /\$\{fieldname\}/m, fieldname );
		choice = choice.replace( /\$\{image\}/m, images[ i ] );
		choice = choice.replace( /<img ([^>]*)>/mi, "<img $1" + photoImageStyle + ">" );
		if( width == 0 )
		{
			// first column of row, which cannot be skipped
			html += photoTableRowStart;
		}
		html += choice;
		width += photoItemWidth;
		if( width + photoItemWidth > formWidth )
		{
			// content pane cannot host another column
			html += photoTableRowEnd;
			width = 0;
		}
	}
	while( width > 0 )
	{
		// need to append column to fill row
		html += photoEmptyCell;
		width += photoItemWidth;
		if( width + photoItemWidth > formWidth )
		{
			// content pane cannot host another column
			html += photoTableRowEnd;
			width = 0;
		}
	}
	html += photoTableEnd;
	
	// determine mail subject
	subject = photoOrderSubject;
	
	// append format selector
	if( typeof( paperFormats ) == "string" )
    {
        html += paperFormats;
    }
    else
    {
        if( ! paperFormats || typeof( paperFormats ) != "object" )
        {
            paperFormats = photoPaperFormats[ "Default" ];
        }
        html += photoPaperFormats[ "Start" ];
        for( var format in paperFormats  )
        {
            var option = photoPaperFormats[ format ];
            if( paperFormats[ format ] )
            {
                option = option.replace( "<option ", "<option selected=\"\" " );
            }
            html += option.replace( /\s+/g, " " );
        }
        html += photoPaperFormats[ "End" ];
    }
	
	// append order summary and fields for delivery address
	html += photoPaymentChoice;
	html += photoOrderSummary;
	html += photoDeliveryAddress;
	html += photoAdditionalComments;
	
	// append other (hidden) fields required for order submission and processing
	fields.sort();
	choice = photoFormSubmission.replace( /\$\{fieldlist\}/m, fields.join( "," ) );
	choice = choice.replace( /\$\{email\}/m, photoOrderEmail );
	choice = choice.replace( /\$\{subject\}/m, subject );
	html += choice;
	
	// supplemental data
	html += photoServiceProvider;
	html += photoAdvancePayment;
	html += photoDeliveryConditions;
	
	// replace form with transformed form and initialize
	form.innerHTML = html;
	try
	{
    	var photoReferenceField = document.getElementById( photoReferenceFieldId );
    	photoNormalBackgroundColor = photoReferenceField.currentStyle.backgroundColor;
    }
    catch( error )
    {
        // ignore
    }
    finally
    {
    	photoResetQuantitiesAndPrice();
    }
	return( true );
}



// This function resets all calculated values to their defaults and updates the
// related form fields, both the visible and the hidden fields.

function photoResetQuantitiesAndPrice( button )
{
	var form = document.getElementById( "orderform" );
	var field = undefined;
	var i = 0;
	
	for( i = 0; i < form.elements.length; i++ )
	{
		field = form.elements[ i ];
		field.title = "";
		field.style.background = photoNormalBackgroundColor;
	}
	photoFillInQuantitiesAndPrice( 0, 0, 0.00, 0.00, 0.00, 0.00 );
	return;
}



// This function counts the number and chosen copies of the selected photos,
// calculates the prices and fees, and updates the related (visible and hidden)
// form fields.

function photoUpdateQuantitiesAndPrice( field, count )
{
	var form = document.getElementById( "orderform" );
	var photoPaymentChoice = form.elements[ "Zahlweise" ];
	var paymentOption = photoPaymentChoice.options[ photoPaymentChoice.selectedIndex ].value;
	var paymentData = paymentOption.replace( " ", "" ).split( ";" );
	var cashOnDeliveryFee = paymentData[ 1 ] - 0.00;
	var formatChoice = form.elements[ "Papierformat" ];
	var formatOption = formatChoice.options[ formatChoice.selectedIndex ].value;
	var formatData = formatOption.replace( " ", "" ).split( ";" );
	var charge = 0.00;
	var unitprice = formatData [ 2 ] - 0.00;
	var donation = formatData [ 3 ] - 0.00;
	var postage = 0.00;
	var images = 0;
	var pieces = 0;
	var price = 0.00;
	var charity = 0.00;
	var i = 0;
	
	// reset or increment field if count is given
	if( count > 0 )
	{
		if( field.value > 0 )
		{
			field.value = field.value - 0 + count;
		}
		else
		{
			field.value = count;
		}
		field.select();
	}
	
	// sum up field values and highlight wrong ones
	for( i = 0; i < form.elements.length; i++ )
	{
		field = form.elements[ i ];
		if( field.name.search( /_/ ) >= 0 
		&&  field.name.search( /^(form_type|s_subject|s_name|s_phone|[rs]_email|s_message)$/ ) < 0 )
		{
			if( field.value >= 0 )
			{
				// field contains number
				if( field.value > 0 )
				{
					images++;
					pieces += (field.value - 0);
				}
				field.title = "";
				field.style.background = photoNormalBackgroundColor;
			}
			else
			{
				// field is not numeric
				field.title = "Ung"+ue+"ltige Anzahl";
				field.style.background = photoErrorBackgroundColor;
			}
		}
	}
	
	// calculate price and update totals
	if( images > 0 )
	{
    	charge = ( photoOmitCharge )? 0.00 : formatData[ 1 ] - 0.00;
		charity = donation * pieces;
		price = unitprice * pieces + charity;
		postage = ( photoOmitDeliveryFee )? 0.00 : formatData[ 4 ] - 0.00 + cashOnDeliveryFee;
	}
	photoFillInQuantitiesAndPrice( images, pieces, charge, price, charity, postage );
	return;
}



// This function updates both the visible and the hidden form fields with the
// supplied prices and fees.

function photoFillInQuantitiesAndPrice( images, pieces, charge, price, charity, postage )
{
	var form = document.getElementById( "orderform" );
	var total = charge - 0.00 + price + postage;

	photoFillInPricingItem( images,
    	form.elements[ "Einzelbilder" ],
	    document.getElementById( "images" ) );
	
	photoFillInPricingItem( pieces,
    	form.elements[ "Abzuege" ],
    	document.getElementById( "pieces" ) );
	
    photoFillInPricingItem( charge.toFixed( 2 ).replace( ".", photoDecimalPoint ) + " " + photoOrderCurrency,
    	form.elements[ "Grundpreis" ],
    	document.getElementById( "charge" ) );
	
	photoFillInPricingItem( price.toFixed( 2 ).replace( ".", photoDecimalPoint ) + " " + photoOrderCurrency,
    	form.elements[ "Bilderpreis" ],
    	document.getElementById( "price" ) );

    photoFillInPricingItem( charity.toFixed( 2 ).replace( ".", photoDecimalPoint ) + " " + photoOrderCurrency,
    	form.elements[ "Foerderbetrag" ], 
        document.getElementById( "charity" ) );
	
    photoFillInPricingItem( postage.toFixed( 2 ).replace( ".", photoDecimalPoint ) + " " + photoOrderCurrency,
    	form.elements[ "Versand" ],
    	document.getElementById( "postage" ) );
	
	photoFillInPricingItem( total.toFixed( 2 ).replace( ".", photoDecimalPoint ) + " " + photoOrderCurrency,
	    form.elements[ "Gesamtpreis" ],
	    document.getElementById( "total" ) );
	
	return;
}



// This function updates both the visible and the hidden form fields of a
// pricing item

function photoFillInPricingItem( value, record, display )
{
    if( record )
    {
    	record.value = value;
	}
    if( display )
    {
    	display.innerHTML = value;
	}
    return;
}



// This function checks all of the forms input fields for valid values and prompts
// the user to correct invalid entries. The function also prompts the user if no
// photos have been selected, i.e. the order's total has a value of zero.

function photoCheckForm( form )
{
	var success = true;
	var matches = form.elements[ "Gesamtpreis" ].value.replace( photoDecimalPoint, "." ).match( /(\d+(\.\d+)?)/ );
	var total = ( matches != null )? matches[ 1 ] : 0;
	
	// check all user input fields
	success &= photoCheckPattern( form.elements[ "realname" ], photoRealnamePattern, photoRealnameMessage );
	success &= photoCheckPattern( form.elements[ "s_name" ], photoRealnamePattern, photoRealnameMessage );
	success &= photoCheckPattern( form.elements[ "Strasse" ], photoStreetPattern, photoStreetMessage );
	success &= photoCheckPattern( form.elements[ "Ort" ], photoTownPattern, photoTownMessage );
	success &= photoCheckPattern( form.elements[ "Land" ], photoCountryPattern, photoCountryMessage );
	success &= photoCheckPattern( form.elements[ "email" ], photoEmailPattern, photoEmailMessage );
	success &= photoCheckPattern( form.elements[ "s_email" ], photoEmailPattern, photoEmailMessage );
	
	// display alert if a user input field contains invalid data
	if( ! success )
	{
		alert( photoInvalidDataMessage );
		return( false );
	}
	
	// check whether at least one item has been ordered
	if( total <= 0 )
	{
		alert( photoImageSelectionMessage );
		return( false );
	}
	
	// everything OK, form fields already reset by check functions
	return( true );
}



// This function checks whether a field's value matches a particular pattern. The 
// function marks the field as invalid if the check fails and assigns the supplied 
// message to the field's title. If the check succeeds, the field is unmarked and 
// its title is cleared. When an undefined field is supplied, the function returns
// success to ease the integration with different form mailers.

function photoCheckPattern( field, pattern, message )
{
    var error;
    var success = true;
    if( field )
    {
        try
        {
            // trim multiple and heading/trailing spaces
            var value = field.value.replace( /\s+/g, " " );
            value = value.replace( /^\s+/, "" );
            value = value.replace( /\s+$/, "" );
        	field.value = value;
        
        	// check pattern against trimmed value
        	var matches = value.match( pattern );
        	if( matches == null || matches.length == 0 )
        	{
        		// field does not match pattern
        		field.title = message;
        		field.style.background = photoErrorBackgroundColor;
            	success = false;
        	}
        	else
        	{
            	// field value is valid
            	field.title = "";
            	field.style.background = photoNormalBackgroundColor;
            	success = true;
            }
        }
        catch( error )
        {
            // indicate failure, but do not block form submission
    		field.title = photoCheckFailureMessage;
    		field.style.background = photoErrorBackgroundColor;
        }
    }
	return( success );
}



// This function checks whether a field's value is numeric and whether its value
// lies within the supplied interval. The function marks the field as invalid if 
// the check fails and assigns the supplied message to the field's title. If the
// check succeeds, the field is unmarked and its title is cleared.

function photoCheckNumber( field, lower, upper )
{
	var value = field.value - 0;
	
	if( value == NaN )
	{
		// field is not numeric
		field.title = "Ung"+ue+"ltiger Wert; bitte geben Sie eine Zahl ein";
		field.style.background = photoErrorBackgroundColor;
		return( false );
	}
	else if( !( lower <= value && value <= upper ) )
	{
		// field value lies outside of interval
		field.title = "Ung"+ue+"ltiger Wert; bitte geben Sie eine Zahl zwischen " + lower + " und " + upper + " ein";
		field.style.background = photoErrorBackgroundColor;
		return( false );
	}
	
	// field value is valid
	field.title = "";
	field.style.background = photoNormalBackgroundColor;
	return( true );
}

