
var IMAGE_FILTER = false;

window.addEvent('domready', function() { 
	 onerror = stop_err;
	// sort any external links that need to open in a new window
	external_links();
	
	// add js processing to forms 
	set_js_forms();
	
	// set up random header images
	init_random_header();
	
	// init CMS dialogs
	if (window.init_modal_dialogs)
		init_modal_dialogs();
	// init "confirm delete" messages
	init_confirms();
});

function init_confirms() {
	$$('.cms-del').each(function(link) { 
		link.addEvent('click', confirm_delete);
	});
}

function confirm_delete(e) {
	e = new Event(e).stop();
	if (confirm('Really delete this item?'))
		document.location = this.href;
}

function init_random_header() {
	var n = $random(1, 3);
	var filters = new Array('c', 'r'); // commercial / residential
	var filter = IMAGE_FILTER ? IMAGE_FILTER : filters[$random(0, 1)];
	var bg_img = 'url(css/images/headers/'+filter+n+'.jpg)';
	$('header').setStyle('background-image', bg_img);
}

function stop_err() {
	return true;
}

function set_js_forms() {
	f = $$('form.js-check');
	if (f) {
		f.addEvent('submit', check_fields);
	}
}

function check_fields() {
	var action = this.form_action.value;
	err = '';
	if (!this.name.value)
		err += "You must enter your name\n";
	if (!this.email.value)
		err += "You must enter your email address\n";
	if (action == 'contact') {
		if (!this.message.value)
			err += "You must enter a message";
	}
	if (action == 'residential') {
		if (!this.postcode.value)
			err += "You must enter your postcode\n";
		if (!this.telephone.value)
			err += "You must enter your telephone\n";
	}
	if (action == 'commercial') {
		if (!this.postcode.value)
			err += "You must enter your postcode\n";
		if (!this.telephone.value)
			err += "You must enter your telephone\n";
	}
	if (err) {
		alert(err);
		return false;
	}
	else
		return true;
}

function external_links() {
	var anchors = $$('a');
	anchors.each(function(anchor) {
		if (anchor.getProperty('href') && anchor.getProperty('rel') == 'external') {
			anchor.setProperty('target', '_blank');
			anchor.title += ' (opens in a new window)';
		}
	});
}


function goback() {
	window.history.back(1);
	return false;
}


