function formulator(formsToOutput, formsData) {$(function() {


	/*--------------------
	| PREP
	-------------------- */

	var forwardFields = 'form';
	var form, field, temp, element;
	var formHeadingTag = 'h3';
	var req_tracker = {};
	var earlyIE = navigator.appVersion.match(/MSIE (6|7)/);
	var isCB = $('body').is('.cb-page');

	//defaults - avoid having to specify the same thing (e.g. if every form is method==post, specify it here, once)
	var defaults = {
		form: {
			container: 'body',
			attr: {method: 'POST', action: 'http://www.espresso.co.uk/clipbank/post.php', 'class': 'formulator_form'},
			heading: 'some form'
		},
		field: {req: true}
	}

	/* --------------------
	| FORMS CONFIG - declare set-up for each form. Form is then built when called.
	-------------------- */

	if (!formsData)
		var formsData = {
			promo_general: {
				heading: ''+(isCB ? 'General enquiry' : 'General')+'',
				intro: isCB ? 'Get in touch if you have any general questions and we will get back to you within 72 hours' : 'Please complete the form below, including your question(s) in the Comments box. We will respond as quickly as possible.',
				fields: {
					'Salutation': {tag: 'select', label: 'Title', options: ['Choose an option', 'Mr', 'Miss', 'Mrs', 'Ms', 'Dr', 'Prof']},
					'First name': {tag: 'input', label: 'First name', attr: {type: 'text'}},
					'Last name': {clone: 'First name', except:{label: 'Last name'}},
					'Job title UK': {tag: 'select', label: 'Job title', options: ['Choose an option', 'Head Teacher', 'Deputy Head Teacher', 'ICT Coordinator', 'LA Advisor/Inspector', 'Student Teacher', 'Teacher', 'Trainer', 'Other'], locale:'uk'},
					'Job title us': {tag: 'input', label: 'Job title', attr:{type:'text'}, locale:'us'},
					'Email': {clone: 'First name', except:{label: 'Email', attr: {'class': 'large'}}},
					'Phone': {clone: 'First name', except:{label: 'Phone', req: false}},
					'School name': {clone: 'First name', except:{label: 'School name'}},
					'School postcode': {clone: 'First name', except: {label: 'School postcode', attr: {'class': 'small'}}, locale:'uk'},
					'LA': {clone: 'First name', except:{label: 'LA', req: false}, locale: 'uk'},
					'Country' : isCB ? {tag:'select', label:'Country', options:['Choose a Country','England','Scotland','Wales']} : null,
					'Address': {clone: 'First name', except: {label: 'Address', attr: {'class': 'larger'}}, locale: 'us'},
					'State': {tag:'select', label: 'State', options: ['Choose an option', 'AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', , 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'], locale: 'us'},
					'Zip': {clone: 'First name', except: {label: 'Zip', attr: {'class': 'small'}}, locale: 'us'},
					'Comments': {tag: 'textarea', label: 'Comments', noBreak: true, attr: {'class': 'small'}},
					'afterURL': {tag: 'input', attr: {type: 'hidden'}, start: 'http://'+(!isCB ? 'www.espresso'+(locale == 'us' ? 'education.com' : '.co.uk') : 'clipbank.channel4learning.com')+'/contact/thanks/'},
					'formTitle':{tag: 'input', attr: {type: 'hidden'}, start:'General website enquiry', locale: 'uk'},
					'locale': {tag: 'input', attr: {type: 'hidden'}, start: locale}
				},
				returnURL: '#',
				returnVars: [],
				submit: 'Submit'
			},
			promo_general_cb:{
				clone:'promo_general'
			},
			cb_tell_me_more: {
				clone: 'promo_general',
				except: {
					heading: 'Tell me more about Clipbank',
					intro: 'Get in touch if you want us to tell you more about Clipbank',
					fields: {
						'afterURL': {start: 'http://clipbank.channel4learning.com/contact/thanks/'}
					}
				}
			},
			promo_support: {
				clone: 'promo_general',
				except: {
					heading: 'Curriculum support',
					intro: 'Contact our curriculum support team'
				}
			},
			promo_feedback: {
				clone: 'promo_general',
				except: {
					heading: 'Tell us how you are using Clipbank',
					intro: 'Send in any comments or feedback'
				}
			},
			"newsletter-form": {
				clone: 'promo_general',
				except: {
					heading: 'Sign up for the weekly Clipbank newsletter',
					intro: 'Please note: This service is only available to Clipbank subscribers.',
					fields: {
						Comments: {noBreak: false},
						'Art': {tag: 'input', noBreak: true, req: false, attr: {type: 'checkbox'}, labelAfterField: false, label: 'Art & Design', before: "<p class='beforeFieldsPara'>Please tick subjects you are interested in <span>*</span>:</p>"},
						'Business': {tag: 'input', req: false, attr: {type: 'checkbox'}, noBreak: true, label: 'Business'},
						'CPSHE': {clone: 'Business', except: {label: 'CPSHE', before: "<div class='cl'></div>"}},
						'D&T': {clone: 'Business', except: {label: 'D&amp;T'}},
						'English': {clone: 'Business', except: {label: 'English', before: "<div class='cl'></div>"}},
						'French': {clone: 'Business', except: {label: 'French'}},
						'Geography': {clone: 'Business', except: {label: 'Geography', before: "<div class='cl'></div>"}},
						'German': {clone: 'Business', except: {label: 'German'}},
						'History': {clone: 'Business', except: {label: 'History', before: "<div class='cl'></div>"}},
						'ICT': {clone: 'Business', except: {label: 'ICT'}},
						'Maths': {clone: 'Business', except: {label: 'Maths', before: "<div class='cl'></div>"}},
						'Music': {clone: 'Business', except: {label: 'Music'}},
						'PE': {clone: 'Business', except: {label: 'PE', before: "<div class='cl'></div>"}},
						'RE': {clone: 'Business', except: {label: 'RE'}},
						'Science': {clone: 'Business', except: {label: 'Science', before: "<div class='cl'></div>"}},
						'Spanish': {clone: 'Business', except: {label: 'Spanish', noBreak:false}}
					}
				}
			},
			promo_training: {
				clone: 'promo_general',
				except: {
					heading: ''+(isCB ? 'Request training' : 'Training')+'',
					intro: isCB ? 'Get in touch to request your complimentary training or ask about training options and we will get back to you within 72 hours' : 'Find out more about our training options and how they can help you get the most out of your investment in Espresso',
					fields: {
						'training pref 1': {locale: 'uk', before: '<label>Preferred training dates</label><div class="cl2"></div>', tag: 'input', attr: {'class': 'small', type: 'text'}, date: true, label: '1st preference', noBreak: true, index: 11, req:false},
						'training pref 2': {clone: 'training pref 1', locale: 'uk', except: {label: '2nd preference', noBreak: false, before: false, req: false}, index: 12},
						'Pref start time': {clone:'First name', except:{label: 'Preferred start time', req: false}, index: 13, locale: 'uk'},
						'training location': {clone:'Job title UK', except:{label:'Location of training', options: [' -- Choose an option --', 'Classroom', 'ICT suite', 'Other'], req: false}, index: 14, locale: 'uk'},
						'whiteboards': {clone:'Job title UK', except:{label: 'Interactive whiteboards available', options:[' -- Choose an option -- ', 'None', 'Smart', 'Promethean', 'RM','Other'], req: false}, index: 15, locale: 'uk'},
						'Comments': {req: false}
					}
				}
			},
			req_training: {
				clone:'promo_general',
				except: {
					heading: 'Request training',
					intro: 'Get in touch to request your complimentary training or ask about training options and we will get back to you within 72 hours'
				}
			},
			promo_signup_uk: {
				clone: 'promo_general',
				except: {
					heading: 'Website Free Classroom Resources Request',
					intro: 'Please complete the following form to sign up for a test drive.',
					fields:{
						'formTitle':{start:'Website Free Classroom Resources Request'}
					}
				}
			},
			promo_techHelp: {
				clone: 'promo_general',
				except: {
					heading: 'Technical support',
					intro: "If you are having trouble accessing Espresso, please let us know your issue in as much detail as possible and we will get in contact shortly. If applicable, please copy and paste the URL that you are having problems with in the 'Issue' box.",
					fields: {
						'formTitle':{start:'Espresso technical support website enquiry', locale:'uk'},
						'Comments': {label: 'Issue'}
					}
				}
			},
			promo_techHelp_cb: {
				clone: 'promo_general',
				except: {
					heading: 'Request technical help or information',
					intro: "Get in touch with your technical questions and we will get back to you within 48 hours",
					fields: {
						'formTitle':{start:'Clipbank technical support website enquiry', locale:'uk'},
						'Comments': {label: 'Issue'}
					}
				}
			},
			promo_marketing: {
				clone: 'promo_general',
				except: {
					heading: 'Marketing',
					intro: 'Please complete the form below for any requests regarding  public relations, events and/or sponsorship.',
					fields:{
						'formTitle':{start:'Espresso marketing website enquiry', locale: 'uk'}
					}
				}
			},
			promo_howUsing: {
				clone: 'promo_general',
				except: {
					heading: 'Tell us how you are using Espresso',
					intro: 'We are always interested in hearing from schools. Tell us how you are using Espresso in your classroom and you may end up being featured on our website!'
				}
			},
			promo_demoReqUS: {
				heading: 'Request a demonstration',
				intro: 'Please complete the form below and we will be in touch to arrange your demo of Espresso.',
				attr: {action: 'https://crm.zoho.com/crm/WebToLeadForm', 'accept-charset': 'UTF-8'},
				fields: {
					'Number of pupils on roll': {tag: 'input', attr: {type: 'text'}, locale: 'uk'},
					'Salutation': {tag: 'select', label: 'Title', locale: 'us', options: ['Choose an option', 'Mr', 'Mrs', 'Ms', 'Dr', 'Prof']},
					'First Name': {tag: 'input', locale: 'us', attr: {type: 'text'}},
					'Last Name': {clone: 'First Name', locale: 'us'},
					'Designation': {clone: 'First Name', except: {label: 'Job title'}, locale: 'us'},
					'Account Name': {clone: 'First Name', locale: 'us', except: {label: 'Company'}},
					'LEADCF5': {clone: 'First Name', except: {label: 'Street', req: false}, locale: 'us'},
					'LEADCF6': {clone: 'First Name', except: {label: 'City'}, locale: 'us'},
					'LEADCF2': {clone: 'First Name', except: {label: 'County', req: false}, locale: 'us'},
					'LEADCF13': {tag: 'select', options: ['Choose an option', 'AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', , 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'], label: 'State', locale: 'us'},
					'LEADCF8': {clone: 'First Name', except: {label: 'Zip Code'}, locale: 'us'},
					'Email': {clone: 'First Name', locadle: 'us'},
					'Phone': {clone: 'First Name', except: {req: false}, locale: 'us'},
					'xnQsjsdp': {tag: 'input', attr: {type: 'hidden'}, start: '*GHCDIs0hEM$', locale: 'us'},
					'xmIwtLD': {tag: 'input', attr: {type: 'hidden'}, start: 'srob7twi44EcLLArAMEnU-h6b8rDomtR', locale: 'us'},
					'actionType': {tag: 'input', attr: {type: 'hidden'}, start: 'TGVhZHM=', locale: 'us'},
					'returnURL': {tag: 'input', attr: {type: 'hidden'}, start: 'http://www.espressoeducation.com/contact/thanks_demo', locale: 'us'},
					'Lead Status': {tag: 'input', attr: {type: 'hidden'}, start: 'Demo_Request', locale: 'us'},
					'Lead Source': {tag: 'input', attr: {type: 'hidden'}, start: 'Web_Demo_Req', locale: 'us'},
					'afterURL': {attr:{type:'hidden'},start: 'http://www.espressoeducation.com/contact/thanks_demo'}
				},
				submit:'Submit'
			},
			promo_demoReqUK: {
				clone: 'promo_general',
				except: {
					heading: 'Request a demonstration',
					intro: 'Please complete the form below and we will be in contact to arrange your no-obligation demonstration of Espresso.',
					fields:{
						'Number of pupils on roll':{clone:'First Name', except:{label:'Number of pupils on roll', tag:'input', attr:{type:'text','class':'small'}}, index: 10},
						'afterURL': {start: 'http://www.espresso.co.uk/contact/thanks_demo'},
						'formTitle': {start: 'Espresso website demo request'},
						'Comments': {req: false}
					}
				}
			},
			promo_demoReqUK_cb: {
				clone: 'promo_general',
				except: {
					heading: 'In school demonstration',
					intro: 'Get in touch to set up an in-school demonstration and we will get back to you within 48 hours',
					fields:{
						'afterURL': {start: 'http://clipbank.channel4learning.com/contact/thanks/'},
						'formTitle': {start: 'Clipbank website demo request'},
						'Comments': {req: false}
					}
				}
			},
			promo_quoteReqUK: {
				clone: 'promo_general',
				except: {
					heading: 'Request a quote',
					intro: 'Please complete the form below and we will be in touch with your personalised quote as quickly as possible.',
					fields: {
						'Number of pupils on roll':{clone:'First name', except:{label:'Number of pupils on roll', attr:{'class':'small'}}, index: 10, locale: 'uk'},
						'afterURL': {start: locale == 'us' ? 'http://www.espressoeducation.com/contact/thanks_quote' : 'http://www.espresso.co.uk/contact/thanks/'},
						'formTitle': {start:'Espresso website quote request'},
						'Comments': {req: false}
					}
				}
			},
			promo_quoteReqUK_cb: {
				clone: 'promo_general',
				except: {
					heading: 'Request a personalised no-obligation quote',
					intro: 'Get in touch to request a quotation and we will get back to you within 48 hours',
					fields: {
						'afterURL': {start: 'http://clipbank.channel4learning.com/contact/thanks/'},
						'formTitle': {start:'Clipbank website quote request'},
						'Comments': {req: false}
					}
				}
			},
			promo_quoteReqUS: {
				clone: 'promo_demoReqUS',
				except: {
					heading: 'Request a quote',
					intro: 'Please complete the form below and we will be in touch with your personalized quote as quickly as possible.',
					fields: {
						'returnURL': {start: 'http://www.espressoeducation.com/contact/thanks_quote'},
						'xmIwtLD': {start: 'srob7twi44FUavqLXdQ3uRRJ0yGCedn5'},
						'Lead Status': {start: 'Quote_Request'},
						'Lead Source': {start: 'Web_Quote_Req'}
					}
				}
			},
			promo_parentPupilCont: {
				clone: 'promo_general',
				except: {
					heading: 'Espresso pupil or parent website enquiry',
					intro: 'If your enquiry is about purchasing or accessing Espresso, please contact your school. For all other enquiries, please complete the form below and we will be in contact.',
					fields: {
						'Job title': '*omit*',
						'LA': '*omit*',
						'formTitle':{start:'Espresso pupil or parent website enquiry'}
					}
				}
			},
			promo_renewals_uk: {
				clone: 'promo_general',
				except: {
					heading: 'Subscribing schools',
					intro: 'Please complete this form if you subscribe to Espresso and would like further information on your subscription or on how we can support you.',
					fields:{
						'how_to_renew': {tag: 'input', req: false, attr: {type: 'checkbox'}, noBreak: true, labelAfterField: true, label: 'How to renew my subscription', before: "<p class='beforeFieldsPara'>Please tick what your query relates to <span>*</span>:</p>"},
						'when_renewal_due': {tag: 'input', attr: {type: 'checkbox'}, req: false, labelAfterField: true, label: 'When my subscription is due for renewal'},
						'home_access': {clone: 'when_renewal_due', except: {noBreak: true, label: 'Pupil Home Access'}},
						'content_club': {clone: 'when_renewal_due', except: {noBreak: true, label: 'Content Club'}},
						'other': {clone: 'when_renewal_due', except: {label: 'Other'}},
						'Comments': {noBreak: false}
					}
				}
			},
			promo_espNewsLetter_uk: {
				clone: 'promo_general',
				except:{
					heading: 'Subscribe to Espresso newsletters',
					intro: 'Please complete the form below to subscribe to Espresso newsletters.',
					fields: {
						'Phone':'*omit*',
						'LA':'*omit*',
						'Comments':'*omit*',
						'espresso_weekly_newsletter': { tag: 'input', req:false, attr: { type: 'checkbox' }, noBreak:false, labelAfterField: false, label: 'Espresso weekly newsletter', before:"<p class='beforeFieldsPara'>Please select your email preferences:</p>" },
						'espresso_perks':{ tag: 'input', req:false, attr: { type: 'checkbox' }, noBreak:false, labelAfterField: false, label: 'Espresso perks' },
						'espresso_product_updates': { clone: 'espresso_perks', except:{ label:'Espresso product updates' } },
						'espresso_technical_updates': { clone: 'espresso_perks', except:{ label:'Espresso technical updates' } },
						'afterURL': {start: 'http://www.espresso.co.uk/contact/thanks/'}
					}
				}
			},
			ideas_exchange:{
				heading: 'Ideas Exchange',
				intro: "<br /><p>Please complete the form and click 'send' to forward your ideas to Espresso.</p><p>Espresso will endeavour to publish responses but cannot guarantee to publish every response it receives.</p><p><b>Please note that in order to get a balance of contributions across a number of schools we will only publish one submission from any individual contributor in any month. </b></p>",
				fields: {
					'Subject': {tag: 'input', label: 'Subject',  attr: {type: 'text'}},
					'Planning reference': {tag: 'input', label: 'Planning reference (e.g.QCA or Framework)', attr: {type: 'text'}},
					'Name': {tag: 'input', label: 'Name',  attr: {type: 'text'}},
					'School': {tag: 'input', label: 'School',  attr: {type: 'text'}},
					'Email': {tag: 'input', label: 'Email',  attr: {type: 'text'}},
					'Age group': {tag: 'input', label: 'Age Group',  attr: {type: 'text'}},
					'Espresso resource': {tag: 'input', label: 'Espresso resource',  attr: {type: 'text'}},
					'Recommendation': {tag: 'textarea', label: 'Recommendation', noBreak: true},
					'afterURL': {tag: 'input', attr: {type:'hidden'}, start: 'http://www.espresso.co.uk/contact/thanks/'}
				},
				submit: 'Send'
			}
		};

	/* --------------------
	| BUILD FORMS
	-------------------- */

	for(var y in formsData) {
		
		//form should be output on this page? Continue if not.
		if (formsToOutput && $.inArray(y, formsToOutput) == -1) continue;
		if (location.href.indexOf('debug=true') != -1) alert(y);
		
		//establish this form's config - either its own, or cloning from another form
		if (!formsData[y].clone)
			var thisForm_config = formsData[y];
		else {
			var thisForm_config = $.extend(true, {}, formsData[formsData[y].clone]);
			if (formsData[y].except) $.extend(true, thisForm_config, formsData[y].except);
		}
		//uniqe fields of forms that clone other forms may demand particular a position
		if (thisForm_config.fields && formsData[y].clone) {
			var indexes = [];
			for(var r in thisForm_config.fields) if (thisForm_config.fields[r] && thisForm_config.fields[r].index) indexes.push(thisForm_config.fields[r]['index']+'|'+r);
			if (indexes.length != 0) {
				indexes.sort();
				for(var r in indexes) {
					var splitter = indexes[r].split('|');
					var counter=0, temp = {}, counter2=0;
					for(var s in thisForm_config.fields) {
						if (counter == splitter[0]) break; temp[s] = thisForm_config.fields[s]; counter++;
					}
					temp[splitter[1]] = thisForm_config.fields[splitter[1]];
					for(var s in thisForm_config.fields) {
						counter2++; if (counter2 < counter) continue; temp[s] = thisForm_config.fields[s];
					}
					thisForm_config.fields = temp;
				}
			}
		}

		//merge form settings with defaults so anything not set inherits default value
		thisForm_config = $.extend(true, {}, defaults.form, thisForm_config);

		//form
		(form = $('<form>')).attr({id: y}).attr(thisForm_config.attr ? thisForm_config.attr : null).appendTo(thisForm_config.container);

		//form heading & intro & clase button.
		if (thisForm_config.heading) $('<'+formHeadingTag+'>').html(thisForm_config.heading).appendTo(form);
		if (thisForm_config.intro) $('<p>').html(thisForm_config.intro).appendTo(form);
		$(document.createElement('button')).addClass('close').appendTo(form).text(' ');
		//if ($('<input>').attr(name='submit')) $(this).addClass('submitButton');

		//this is a hack to get ie7 to display the close button.
		if($.browser.msie && $.browser.version.substr(0,1)<8) { $(document.createElement('a')).appendTo(form).text('Close').css('textIndent','-999px') }

		//formulator hidden fields
		$('<input>').attr({type: 'hidden', name: 'form'}).val(y).appendTo(form);


		/* --------------------
		| fields
		-------------------- */

		if (thisForm_config.fields) {

			for(var r in thisForm_config.fields) {
				
				//if no field, field definition is conditional and current environment null, continue
				if (!thisForm_config.fields[r]) continue;

				//if false, i.e. attempt to omit inherited field from a clone, or if field is not for current locale, continue
				if (thisForm_config.fields[r] == '*omit*' || (thisForm_config.fields[r].locale && locale != thisForm_config.fields[r].locale)) continue;

				//establish this field's config - either its own, or cloning from another field
				if (!thisForm_config.fields[r].clone)
					var thisField_config = thisForm_config.fields[r];
				else {
					var thisField_config = $.extend(true, {}, thisForm_config.fields[thisForm_config.fields[r].clone]);
					var except = thisForm_config.fields[r].except;
					if (except) {
						$.extend(true, thisField_config, except);
						if (except.options) thisField_config.options = except.options;
					}
				}

				//merge settings with defaults so  " " " " ...
				thisField_config = $.extend(true, {}, defaults.field, thisField_config);

				//more prep
				var fieldIsHidden = thisField_config.attr && thisField_config.attr.type == 'hidden';

				//field required? log in req. tracker
				if (thisField_config.req) (req_tracker[y] = req_tracker[y] ? req_tracker[y] : {})[r] = thisField_config.req;

				//field has before content?
				if (thisField_config.before) form.append(thisField_config.before);

				//label (unless field is hidden or field has labelAfterField == true - in case of latter, output label after field)
				if (!fieldIsHidden && !thisField_config.labelAfterField) {
					var label = $('<label>').addClass('labelBefore').html(thisField_config.label ? thisField_config.label : r).appendTo(form);
					if (thisField_config.req) label.append($('<span>').text(' *'));
					if (thisField_config.noBreak) label.addClass('noBreak');
				}

				//field. If no break after, give a bit of margin right. If label after field not before, float field left
				var attr = thisField_config.attr;
				field = $('<'+thisField_config.tag+'>').attr('name', r).attr(attr ? attr : null).appendTo(form);
				if (thisField_config.noBreak) field.addClass('noBreak'+' '+(!thisField_config.labelAfterField ? 'fieldAfter' : 'fieldBefore'));
				if (thisField_config.attr && thisField_config.attr.type) field.addClass(thisField_config.attr.type);
				if (thisField_config.labelAfterField) field.addClass('fl');


				//field is date field? Load calendar (assumes Mitya's calendar script was loaded!)
				if (thisField_config.date && window['calendar']) {
					field.after(function() { return $('<div>').addClass('calendarContainer'); });
					new calendar({
						containerSelector: field.next('div'),
						callback_field: field,
						focusElements: field,
						showDayHeadings: true
					});
				}

				//options (if select)
				if (thisField_config.tag == 'select' && thisField_config.options) {
					for(var b in thisField_config.options) {
						if (thisField_config.options[b] instanceof Array) {
							var opt_text = thisField_config.options[b][0];
							var opt_val = thisField_config.options[b][1];
						} else {
							var opt_text = thisField_config.options[b];
							var opt_val = opt_text;
						}
						$('<option>').text(opt_text.replace(/^>/g, '')).val(opt_val ? opt_val : null).appendTo(field);
					}
				}


				//field's starting value? (for select options, starting val is one whose text starts with '>', else first)

				var start = thisField_config.start;
				switch(thisField_config.tag) {
					case 'textarea': if (start) field.html(start); break;
					case 'select':
						for(var b in thisField_config.options) {
							var startingOptionIndex = 0;
							if ((thisField_config.options[b] instanceof Array && thisField_config.options[b][0].substr(0, 1) == '>') || thisField_config.options[b].substr(0, 1) == '>') { startingOptionIndex = b; break; }
						}
						field.get(0).options.selectedIndex = startingOptionIndex;
						break;
					default: if (start) field.val(start); break;
				}

				//help text?
				if (thisField_config.info) {
					(element = $('<span>')).addClass('fieldInfo').text(thisField_config.info).appendTo(form);
					if (thisField_config.info.length > 50 || (thisField_config.attr['class'] && thisField_config.attr['class'].match(/^large/)))
						element.addClass('fieldInfo_under').before(function() {
							return $('<div>').addClass('cl');
						});
				}

				//label after field, not before?
				if (thisField_config.labelAfterField) {
					var label = $('<label>').css({width: 'auto'}).addClass('labelAfter').html(thisField_config.label ? thisField_config.label : r).appendTo(form);
					if (thisField_config.req) label.append($('<span>').text(' *'));
					if (thisField_config.noBreak) label.addClass('noBreak');
				}

				//afetr content?
				if (thisField_config.after) form.append(thisField_config.after);

				//clear after, unless has noBreak == true or field is hidden
				if (!fieldIsHidden && !thisField_config.noBreak) $('<div>').addClass('cl styled').appendTo(form);

			}
		}

		//add button(s)
		if (thisForm_config.submit)
			$('<input>').attr('type', 'submit').val(' ').appendTo(form).addClass('submit-button noLBClose');
		if (thisForm_config.clear)
			$('<input>').attr('type', 'reset').val(thisForm_config.clear).appendTo(form);

	}


	/* --------------------
	| VALIDATION - on form submit, scan for required fields (denoted by rel='req') and complain if any blank
	-------------------- */

	$('.formulator_form').submit(function() {
		document.charset='UTF-8';
		if (!req_tracker[$(this).attr('id')]) return;
		var reqFields = [];
		var badFields = [];
		var form = $(this);
		$.each(req_tracker[$(this).attr('id')], function(key, val) {
			if (val) reqFields.push(key);
		});
		$.each(reqFields, function(key, val) {
			var field = form.find('[name="'+val+'"]');
			if (field.val() == '' || field.val().match(/^Choose/i)) {
				badFields.push(field.prev('label').text().replace(' *', ''));
			}
		});
		if (badFields.length != 0) {
			alert("The following required fields were left blank:\n\n- "+badFields.join('\n- '));
			return false;
		}
	});


	/* --------------------
	 | SM - certain forms need to be taller
	-------------------- */

	if (locale == 'uk') $('#promo_training').css('height','780px');
	if (isCB) $('#promo_training, #newsletter-form').css('height','800px');


	/* --------------------
	| FORM JUMP - if form jump token passed in URL, show that form on page load
	-------------------- */

	var flag = location.href.match(/form_jump=([\w-]+)/);
	if (flag && $('#'+flag[1]).length == 1)  setTimeout(function() { $('#' +flag[1]).lightbox(); }, 1000);
	
	
});}
