﻿Protoform = function (form, success) {
	this.form = $(form);
	this.onSuccess = success;

	this.highlightClassName = 'protoformHighlight';

	this.onSubmit = function (e) {
		if (e) { Event.stop(e); }
		this.clearHighlights();
		this.clearMessages();
		this.disableForm();
		this.lastRequest = this.form.request({onComplete:this.onComplete.bind(this)});
	};

	this.disableFormActual = function () {
		var element;
		for (var i = this.form.elements.length; i--;)
		{
			element = this.form.elements[i];
			if (!element.disabled)
			{
				element.disabled = true;
				element._protoform_disabled = true;
			}
		}
	};

	this.disableForm = function () {
		window.setTimeout(this.disableFormActual.bind(this), 1);
	};

	this.clearHighlights = function () {
		for (var i = this.form.elements.length; i--;)
			$(this.form.elements[i]).removeClassName(this.highlightClassName);
	};

	this.clearMessages = function () {
		var node;
		if (node = $(this.form.id +'_message'))
			node.update();
		for (var i = this.form.elements.length; i--;)
			if (node = $(this.form.id +'_'+ this.form.elements[i].name +'_message'))
				node.update();
	};

	this.onComplete = function (request) {
		this.enableForm();

		var response = request.responseText.evalJSON(true);
		if (response.success)
			this.onSuccess.call(this, request, response);
		else
			this.onFailure.call(this, request, response);
	};

	this.onFailure = function (request, response) {
		var node;

		if (response.message)
			if (node = $(this.form.id +'_message'))
				node.update(response.message)
			else
				alert(response.message);

		for (var i = this.form.elements.length; i--;)
		{
			var element = this.form.elements[i];
			var reaction = response.reactions[element.name];

			if (reaction && reaction.highlight)
				$(element).addClassName(this.highlightClassName);

			node = $(this.form.id +'_'+ element.name +'_message');
			if (reaction && node)
				node.update(reaction.message ? reaction.message : "");
		}
	};

	this.enableForm = function () {
		var element;
		for (var i = this.form.elements.length; i--;)
		{
			element = this.form.elements[i];
			if (element._protoform_disabled)
			{
				element.disabled = false;
				element._protoform_disabled = undefined;
			}
		}
	};

	this.form.observe('submit', this.onSubmit.bind(this));
};

bookOrderFormSuccess = function () {
	alert("Thank you. Your order has been submitted, and will be processed as soon as possible.");
	$("bookOrderForm").reset();
};

contactFormSuccess = function () {
	alert("Thank you. Your enquiry has been sent, and one of our staff will be in contact with you shortly.");
	$("contactForm").reset();
};

albumSendFormSuccess = function () {
	alert("Thank you. Your album has been sent.");
	$("albumSendForm").reset();
};

updateAlbumImageCountInnerHTML = function (html) {
	var nodes = Element.getElementsByClassName(document, "albumImageCount");
	for (var i = nodes.length; i--;)
		nodes[i].innerHTML = html;
};

addAlbumImageSuccess = function (transport) {
	//	react to a positive response for adding an image to this album
	
	var json = transport.responseText.evalJSON();
	
	if (json.imageCount)
		updateAlbumImageCountInnerHTML(String(json.imageCount));
};

clearAlbumSuccess = function (transport) {
	var json = transport.responseText.evalJSON();
	location.reload();
};

printAlbum = function () {
	window.open("in.asp?f=printsessionalbum");
};

clearAlbum = function () {
	if (confirm("Are you sure you want to clear your album?"))
	{
		new Ajax.Request("submit.asp", {
				method: "get",
				onSuccess: clearAlbumSuccess,
				parameters: {
					"submit.action": "clearAlbum"
				}
			});
	}
};

albumRemoveSuccess = function (transport) {
	var json = transport.responseText.evalJSON();
	location.reload();
};

albumRemove = function (id) {
	if (confirm("Are you sure you want to remove this image from album?"))
	{
		new Ajax.Request("submit.asp", {
				method: "get",
				onSuccess: albumRemoveSuccess,
				parameters: {
					"submit.action": "albumRemove",
					"id": id
				}
			});
	}
};

currentAlbumImage = null;

addCurrentAlbumImage = function () {
	if (currentAlbumImage)
		addAlbumImage(currentAlbumImage);
};

addAlbumImage = function (id) {
	//	instructs the server to add an image to this session's album
	
	new Ajax.Request("submit.asp", {
			method: "get",
			onSuccess: addAlbumImageSuccess,
			parameters: {
				"submit.action": "albumAdd",
				"id": id
			}
		});
	
	updateAlbumImageCountInnerHTML("&hellip;");
};

hookAlbumImageLinks = function () {
	//	listens for clicks on lightbox-triggering links so we can store the contentfile id for passing to the server if the user wants to add the image to their album

	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName('a');
	
	for (var i = anchors.length; i--;)
	{
		var anchor = anchors[i];
		var relAttribute = String(anchor.getAttribute('rel'));
		var idAttribute = String(anchor.getAttribute('id'));
		
		if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox')) && idAttribute.substr(0, 12).toLowerCase() == "contentfile-")
		{
			Event.observe(anchor, 'click', function() {
				var idAttribute = String(this.getAttribute('id'));
				var idValue = parseInt(idAttribute.substr(12), 10);
				if (!idValue)
					currentAlbumImage = null;
				else
					currentAlbumImage = idValue;
			}.bind(anchor));
		}
	}
};

showCCDetails = function () {
	var nodes = Element.getElementsByClassName(document, "ccDetail");
	for (var i = nodes.length; i--;)
		nodes[i].show();
};

hideCCDetails = function () {
	var nodes = Element.getElementsByClassName(document, "ccDetail");
	for (var i = nodes.length; i--;)
		nodes[i].hide();
};

bookFormBookPrice = 49.95;
bookFormShippingPrice = 10.95;

formatCurrency = function (n) {
	return (n < 0 ? "-" : "") + "$"+ Math.abs(n).toFixed(2);
};

updateBookOrderFormPrices = function () {
	$("bookPrice").innerHTML = formatCurrency(bookFormBookPrice);
	$("shippingPrice").value = formatCurrency(bookFormShippingPrice);
	
	var qty = parseInt($("bookOrderForm_qty").value, 10);
	if (qty < 1 || isNaN(qty))
	{
		$("totalPrice").value = "";
	}
	else
	{
		var total = (bookFormBookPrice * qty) + bookFormShippingPrice;
		$("totalPrice").value = formatCurrency(total);
	}
};

var albumSendProtoForm;

Event.observe(window, "load", function(){
	var frm;

	if (frm	= $("contactForm"))
	{
		frm.action = "submit.asp";
		new Protoform(frm, contactFormSuccess);
	}
	
	if (frm	= $("albumSendForm"))
	{
		frm.action = "submit.asp";
		albumSendProtoForm = new Protoform(frm, albumSendFormSuccess);
	}
	
	if (frm = $("bookOrderForm"))
	{
		frm.action = "submit.asp";
		albumSendProtoForm = new Protoform(frm, bookOrderFormSuccess);
		
		Event.observe("ccoption_online", "click", showCCDetails);
		Event.observe("ccoption_call", "click", hideCCDetails);
		
		if ($("ccoption_online").checked)
			showCCDetails();
			
		updateBookOrderFormPrices();
		
		new Form.Element.Observer("bookOrderForm_qty", 0.2, updateBookOrderFormPrices);
	}
	
	hookAlbumImageLinks();
});

formSubmit = function (name) { document.forms[name].submit(); };

showCCCVC2Help = function () {
	window.open("popup.asp?mod=cvc2", "_blank", "width=512,height=384");
};
