// Cre8ted by RE

var dom = document.getElementById;
var ie = document.all;

//
// objects
//

function findObj(id, frame) {
    var d, o;
    if (id != null)
		d = document;
		if (frame && frame != '' && window.frames[frame]) {
			d = window.frames[frame].document;
		}
        if (dom && (o = d.getElementById(id)) != null)
			return o;
		else
			return MM_findObj(id); //fall-back
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

//
// events
//

function addEvent(obj, evnt, func) {
	if (obj.addEventListener)	// w3c
		obj.addEventListener(evnt, eval(func), false);
	else if (window.attachEvent)	// ie
		obj.attachEvent('on' + evnt, eval(func));
}

function handleError() {
	return true;
}

//
// windows
//

function bookmarkPage() {
	if (window.external)
		window.external.AddFavorite(document.location, document.title);
	else
		alert('Sorry! Your browser doesn\'t support this function.');
}

function newWindow(tag) {
	var win;
	win = window.open(tag.href);
	win.focus();
	return false;
}

function printWindow(tag) {
	var win;
	if (tag != null)
		win = window.open(tag.href, 'printPage', 'scrollbars=1,status=1,width=700,height=700');
	else {
		var url = document.location.href;
		var qs = document.location.search;
		if (qs != '')
			qs = '&print=1';
		else
			qs = '?print=1';
		win = window.open(url + qs, 'printPage', 'scrollbars=1,status=1,width=700,height=700');
	}
	win.focus;
	return false;
}

function printPage(close) {
	window.print();
	if (close)
		window.close();
	return false;
}

function mapWindow(pc, msg) {
	if (msg)
		alert('You are now being taken to the Ordnance Survey Get-a-map service.\nUse of this service is subject to the terms and conditions found on the website.')
	var w = 740;
	var h = 520;
	var l = Math.round(0.5 * (screen.availWidth - w));
	var t = Math.round(0.5 * (screen.availHeight - h)) - 20;
	pc = escape(pc).toUpperCase()
	var win = window.open('http://getamap.ordnancesurvey.co.uk/getamap/frames.htm?mapAction=gaz&gazName=pc&gazString=' + pc, 'gam', 'left='+l+',screenX='+l+',top='+t+',screenY='+t+',width='+w+',height='+h+',status,scrolling=no');
	win.focus();
	return false;
}

//
// cookies
//

function getCookie(name) {
	var n = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(n) == 0) return c.substring(n.length,c.length);
	}
	return null;
}

function setCookie(name, value, days, path) {
	var e = '';
	var p = '';
	if (days) {
		var dt = new Date();
		dt.setTime(dt.getTime()+(days*24*60*60*1000));
		e = '; expires='+dt.toGMTString();
	}
	if (path)
		p = '; path=' + p;
	document.cookie = name + '=' + value + e + p;
}

function deleteCookie(name) {
	setCookie(name, '', -1);
}

//
// encryption
//

var alphaBase = '@.comukeairtn-slpdhgbyfvwxzqj_1234567890 ';
var encKey = 'c+pb$5UkX%1sZvy9m37ni^h0qr2f6xeN!CD*BVFw_';

function textScramble(seed, base, key) {
	var hash = '';
	var i;
	var idx;
	var s;
	for (i = 0; i < seed.length; i++) {
		s = seed.charAt(i);
		idx = base.indexOf(s);
		hash += ((idx == -1) ? s : key.charAt(idx));
	}
	return hash;
}

function generateHash(seed) {
	var s;
	seed = seed.toLowerCase();
	s = textScramble(seed, alphaBase, encKey);
	return s;
}

function generateMailer(frame, hash, linkText, cssClass) {
	var d, s;
	d = document;
	if (frame && frame != '' && window.frames[frame]) {
		d = window.frames[frame].document;
	}
	if (linkText == null || linkText == '')
		linkText = textScramble(hash, encKey, alphaBase);
	s = '<a hr'+'ef="ma'+'ilto'+':' + textScramble(hash, encKey, alphaBase) + '"';
	if (cssClass != null && cssClass != '')
		s += ' class="' + cssClass + '"';
	s += '>' + linkText;
	s += '</a>';
	//alert(s);
	d.write(s);
}

function generateText(hash) {
	var s;
	s = textScramble(hash, encKey, alphaBase);
	//alert(s);
	return s;
}

//
// styles
//

function changeStyle(obj, attr, value) {
	if (obj != null)
		eval('obj.style.' + attr + ' = \'' + value + '\';');
}

function changeStyleById(id, attr, value) {
	var o;
	if ((o = findObj(id)) != null)
		eval('o.style.' + attr + ' = \'' + value + '\';');
}
