Adr = {
over: function (x) {self.status=x.hash; return true;},
out: function () {self.status=''; return true;},
cadr: function () {
	var a = String.fromCharCode(64);
	var d = String.fromCharCode(46);
	var c = "co";
	return "Louise"+a+c+"tswoldcbt"+d+c+d+"uk";
	},
clnk: function () {return "mailto:"+Adr.cadr()+"?subject=CBT";},
cont: function () {window.location = Adr.clnk();},
load: function () {document.getElementById("cadr").firstChild.nodeValue=Adr.cadr();},
adrover: function () {setTimeout("self.status=Adr.clnk()", 20)}
}

contact = Adr.cont

App = {
form: null,
page:null,
pagenum:null,
focus: null,
btn: {},

allow: function (elem, ok) {
	if (ok) {
		elem.style.borderBottomStyle = elem.style.borderRightStyle
		elem.style.borderBottomColor = elem.style.borderRightColor
		}
	else {
		elem.style.borderBottomStyle = "solid"
		elem.style.borderBottomColor = "red"
		}
	},

back: function () {
	this.page.removeAttribute("class")
	this.page.style.visibility = "hidden"
	this.number(false)
	do {this.page = this.page.previousSibling} while (this.page.tagName != "DIV")
	this.page.setAttribute("class", "current")
	this.page.style.visibility = "visible"
	this.number(true)
	this.buttons()
	this.check()
	},

buttons: function () {
	var n = this.page.getAttribute("page")
	
	if (n == "1" || this.page.getAttribute("back") == "false")
		this.btn.back.setAttribute("disabled", "true")
	else
		this.btn.back.removeAttribute("disabled")
	
	if (this.page.getAttribute("finish") == "true")
		setTimeout(this.finish_enable, 850)
	else
		this.btn.finish.setAttribute("disabled", "true")
	
	if (n == "3") this.btn.next.setAttribute("disabled", "true")

	this.focusPrompt(null)
	},

check_coderx: /[A-Za-z]{1,2}[0-9]{1,2}\s+[0-9]{1,2}[A-Za-z]{2}/,

check_codepartrx: new RegExp(
	'^[a-z]{1,2}$'
		+ '|^[a-z]{1,2}[0-9]{1,2}$'
		+ '|^[a-z]{1,2}[0-9]{1,2} +$'
		+ '|^[a-z]{1,2}[0-9]{1,2} +[0-9]{1,2}$'
		+ '|^[a-z]{1,2}[0-9]{1,2} +[0-9]{1,2}[a-z]{1,2}$',
	'i'),

check_mailrx: new RegExp(
	'^[^@]+'
		+ '@'
		+ '[A-Za-z0-9\x2d\x2e]{2,}'
		+ '\x2e[A-Za-z]{2,4}$',
	''),

check_mailpartrx: new RegExp(
	'^[^@]+$'
		+ '|^[^@]+@$'
		+ '|^[^@]+@[A-Za-z0-9\x2d\x2e]{2,}$'
		+ '|^[^@]+@[A-Za-z0-9\x2d\x2e]{2,}\x2e$'
		+ '|^[^@]+@[A-Za-z0-9\x2d\x2e]{2,}\x2e[A-Za-z]{2,4}$',
	''),

check_phonerx: /^0[1-9][0-9]{8,9}([^0-9]+[^\n])?$/,

check_phonepartrx: /^0$|^0[1-9]$|^0[1-9][0-9]{2,9}$|^0[1-9][0-9]{8,9}([^0-9]+[^\n])?$/,

check: function () {
	var ok = true
	var empty = true
	var choice = null, choiceinp = null
	var belowFocus = (!App.key_first)
	var inp = App.page.firstChild
	while (inp) {
		inp = inp.nextSibling
		if (inp && inp.tagName == "INPUT") {
			empty = false
			var here = (inp == App.focus)
			var valid = App.valid(inp, here)
			var phone = (inp.id.substr(0, 6) == "phone-")
			App.allow(inp, valid || ((phone || belowFocus) && inp.value == ""))
			if (here) valid = App.valid(inp, false)
			if (inp == App.focus) belowFocus = true
			if (inp.getAttribute("upper")) inp.value = inp.value.toUpperCase()
			if (!inp.getAttribute("disabled")) {
				if (inp.id == "e-mail" && !valid) ok = false
				if (inp.id == "code" && !valid) ok = false
				else if (phone) {
					if (choice === null) choice = false
					if (!choiceinp) choiceinp = inp
					if (valid) choice = true
					else if (inp.value != "") ok = false
					}
				else if (!inp.getAttribute("optional")) {
					if (!valid) ok = false
					}
				}
			}
		}
	if (empty) ok = (App.page.getAttribute("next") == "true")
	if (choice === false) {
		if (App.focus && App.focus.id.substr(0, 6) != "phone-") App.allow(choiceinp, false)
		ok = false
		}
	if (ok) App.btn.next.removeAttribute("disabled")
	else App.btn.next.setAttribute("disabled", "true")
	App.defaults()
	return ok
	},

defaults: function () {
	with (this.btn) {
		if (enter) {
			enter.removeAttribute("default")
			enter = null
			}
		if (!finish.getAttribute("disabled")) enter = finish
		else if (!next.getAttribute("disabled")) enter = next
		else if (!back.getAttribute("disabled")) enter = back
		if (enter) enter.setAttribute("default", "true")
		}
	},

finish: function () {
	this.btn.finish.setAttribute("disabled", "true")
	this.defaults()
	try {
		with (document) {
			var n1 = getElementById("forename").value.replace(/^ +| +$/g, "")
			if (n1.indexOf(" ") > 0) n1 = "| " + n1
			var n2 = getElementById("surname").value.replace(/^ +| +$/g, "")
			if (n2.indexOf(" ") > 0) n2 = "| " + n1
			var n3 = getElementById("district").value
			getElementById("details").value = '\n'
				+ getElementById("title").value + ' '
				+ n1 + ' ' + n2 + '\n'
				+ 'e-mail: ' + getElementById("e-mail").value + '\n\n'
				+ ' ' + getElementById("street").value + '\n'
				+ (n3? ' ' + n3 + '\n' : "")
				+ ' ' + getElementById("town").value + '\n'
				+ ' ' + getElementById("code").value + '\n\n'
				+ 'day:     ' + getElementById("phone-day").value + '\n'
				+ 'evening: ' + getElementById("phone-eve").value + '\n'
				+ 'other:   ' + getElementById("phone-oth").value + '\n'
			getElementById("realname").value =
				getElementById("subject").value + ': '
				+ getElementById("forename").value + ' '
				+ getElementById("surname").value
			getElementById("email").value =
				getElementById("e-mail").value
			var a = "\u0040", d = "\u002e", u = "uk"
			getElementById("recipient").value =
				"rod"+d+"whiteley"+a+u+"online"+d+"co"+d+u
			forms["refer"].submit()
			setTimeout(this.finish_clear, 100)
			}
		} catch (ex) {alert(ex)}
	},

finish_clear: function () {
	with (document) {
		getElementById("recipient").value = 
			getElementById("subject").value
		getElementById("details").value = "sent"
		}
	with (App) {
		page.setAttribute("next", "true")
		next()
		btn.finish.blur()
		}
	},

finish_enable: function () {
	with (App.btn.finish) {
		removeAttribute("disabled")
		focus()
		}
	App.defaults()
	},

focusInput: function (event, focus) {
	var elem = event.target || event.srcElement
	if (!elem) return

	var has = elem.getAttribute("focussed")
	if (focus && !has) {
		elem.setAttribute("focussed", "true")
		elem.style.backgroundColor = "#dfd"
		this.focus = elem
		this.check()
		}
	else if (!focus && has) {
		elem.removeAttribute("focussed")
		elem.style.backgroundColor = "#fff"
		this.focus = null
		}
	Drop.cancel(elem, focus)
	},

focusLabel: function (elem) {
	while (elem && elem.tagName != "INPUT") elem = elem.nextSibling
	elem.focus()
	},

focusPrompt: function (elem) {
	if (!App.key_first) return
	if (!elem) elem = App.page.getElementsByTagName("P")[0]
	while (elem && elem.tagName != "INPUT") elem = elem.nextSibling
	if (elem && elem.focus) elem.focus()
	else with (App.btn) {
		if (!finish.getAttribute("disabled")) finish.focus()
		else if (!next.getAttribute("disabled")) next.focus()
		else if (!back.getAttribute("disabled")) back.focus()
		}
	},

key_timer: null,
key_first: false,

key: function (event) {
	if (this.key_timer) {
		clearTimeout(this.key_timer)
		this.key_timer = null
		}
	this.key_first = true

	if ((event.target || event.srcElement) == Drop.input) {
		if (Drop.key(event)) return
		}
	else if (event.keyCode == 13 && this.btn.enter) {
		switch (this.btn.enter.id) {
			case "button-back":
				this.back()
				break
			case "button-next":
				this.next()
				break
			case "button-finish":
				this.finish()
				break
			}
		}
	else {
 		this.key_timer = setTimeout(this.check, 330)
 		}
	},

load: function () {
//	try { 
		this.form = document.getElementById("apply")
		this.page = this.form.firstChild
		this.pagenum = document.getElementById("pagenum")
		this.btn.back = document.getElementById("button-back")
		this.btn.next = document.getElementById("button-next")
		this.btn.finish = document.getElementById("button-finish")
		this.btn.enter = null
		Drop.init()
		while (this.page.tagName != "DIV") this.page = this.page.nextSibling
		if (document.getElementById("details").value) {
			this.page.removeAttribute("class")
			this.page.style.visibility = "hidden"
			this.page = document.getElementById("blank")
			this.page.setAttribute("class", "current")
			this.page.style.visibility = "visible"
			}
		else this.next()
//		}
//	catch (ex) {}
	},

next: function () {
	if (!this.check()) return
	this.page.removeAttribute("class")
	this.page.style.visibility = "hidden"
	this.number(false)
	do {this.page = this.page.nextSibling} while (this.page.tagName != "DIV")
	if (this.page.getAttribute("page")) {
		this.page.setAttribute("class", "current")
		this.page.style.visibility = "visible"
		this.number(true)
		}
	this.buttons()
	this.check()
	},

number: function (seen) {
	var n = this.page.getAttribute("page")
	var num = document.getElementById("num-" + n)
	if (num) {
		with (num.style) {
			borderColor = seen? "#99a":"transparent"
			backgroundColor = seen? "#e8f0fc":"transparent"
			}
		}
	this.pagenum.style.visibility = num? "visible":"hidden"
	},

valid: function (inp, part) {
	if (inp.getAttribute("disabled") || inp.getAttribute("optional")) return true
	var v = inp.value.replace(/^\s+|\s+$/g, "")
	if (part && v.length < 2) return true
	if (inp.id == "e-mail") {
		if (part) return App.check_mailpartrx.test(v)
		else return App.check_mailrx.test(v)
		}
	else if (inp.id == "code") {
		if (part) return App.check_codepartrx.test(v)
		else return App.check_coderx.test(v)
		}
	else if (inp.id.substr(0, 6) == "phone-") {
		if (part) return App.check_phonepartrx.test(v.replace(/[\(\)\u002e\u002d ]/g, ""))
		else return App.check_phonerx.test(v.replace(/[\(\)\u002e\u002d ]/g, ""))
		}
	else return (v.replace(/[^A-Za-z]/g, "").length >= 2)
	}

}


Drop = {

active: false,
armed: false,
button: null,
current: null,
input: null,

cancel: function (elem, focus) {
	if (this.armed) return
	if (this.active && elem != this.input & focus) this.show(false)
	},

down: function () {
	if (this.active) this.armed = true
	else this.show(true)
	},

init: function () {
	this.button = document.getElementById("title-drop")
	this.input = document.getElementById("title")
	this.menu = document.getElementById("title-menu")
	},

key: function (event) {
	switch (event.keyCode) {
		case 13:
			if (this.active) this.pClick(null)
			break
		case 27:
			if (this.active) this.show(false)
			break
		case 38:
			if (this.active) {
				var p = this.current.previousSibling
				while (p && !p.tagName) p = p.previousSibling
				if (!p) {
					p = this.menu.lastChild
					while (p && !p.tagName) p = p.previousSibling
					}
				this.pCurrent(p)
				}
			else this.show(true)
			break
		case 40:
			if (this.active) {
				var p = this.current.nextSibling
				while (p && !p.tagName) p = p.nextSibling
				if (!p) {
					p = this.menu.firstChild
					while (p && !p.tagName) p = p.nextSibling
					}
				this.pCurrent(p)
				}
			else this.show(true)
			break
		default:
			if (this.active) this.show(false)
			return false
		}
	return true
	},

out: function () {
	this.armed = false
	},

pClick: function (event) {
	if (event) this.current = event.target || event.srcElement
	if (!this.current) return
	this.input.value = this.current.firstChild.nodeValue
	this.show(false)
	this.input.focus()
	App.check()
	},

pCurrent: function (p) {
	if (this.current) {
		this.current.style.backgroundColor = "#fff"
		this.current = null
		}
	if (p) {
		this.current = p
		p.style.backgroundColor = "#bcd"
		}
	},

pOver: function (event) {
	this.pCurrent(event.target || event.srcElement)
	},

pOut: function (event) {
	if ((event.target || event.srcElement) == this.current) this.pCurrent(null)
	},

show: function (seen) {
	if (seen === null) this.active ^= true
	else if (seen == this.active) return
	else this.active = seen
	with (this.menu.style) {
		left = this.input.offsetLeft
		top = this.input.offsetTop + this.input.offsetHeight
		visibility = this.active? "visible":"hidden"
		}
	if (this.active) {
		var p = this.menu.firstChild, ps = null
		var t = this.input.value
		var w = this.input.offsetWidth + "px"
		while (p) {
			if (p && p.tagName) {
				if (ps == null || p.firstChild.nodeValue == t) ps = p
				p.style.width = w
				}
			p = p.nextSibling
			}
		this.pCurrent(ps)
		}
	else this.pCurrent(null)
	this.button.style.borderStyle = this.active? "inset":"outset"
	},

up: function () {
	if (this.armed) {
		this.show(false)
		this.armed = false
		}
	this.input.focus()
	}

}

