function pop(ShowPage, intwidth, intheight) {

	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'poppage', winprops);

}

function preview(formname, listname, intwidth, intheight, myPage, myVar) {

	var sel = document.forms[formname].elements[listname];
	var myValue = sel.options[sel.selectedIndex].value;
	
	var ShowPage = myPage + '?' + myVar + '=' + myValue;
	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'poppage', winprops);
}

function validateNumber(field, msg, min, max) {
	if (!min) { min = 0 }
	if (!max) { max = 255 }
	if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateEmail(email, msg, optional) {

	if (!email.value && optional) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	
	if (!re_mail.test(email.value)) {
		alert(msg);
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function validateList(listname, msg) {

	var foo = listname.selectedIndex;

	if (foo == 0 || foo == -1) {
		alert(msg);
		return false;
	}
	
	return true;
	
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function addEvent(elm, evType, fn, useCapture) {
	// cross-browser event handling for IE5+, NS6 and Mozilla 
	// By Scott Andrew 
	if (elm.addEventListener) { 
		elm.addEventListener(evType, fn, useCapture); 
		return true; 
	} else if (elm.attachEvent) { 
		var r = elm.attachEvent('on' + evType, fn); 
		return r; 
	} else {
		elm['on' + evType] = fn;
	}
}

function iniSiteSwitcher() {
	if (document.getElementById('site_switcher')) {
		var site_switcher = document.getElementById('site_switcher');
		// attach onchange handler
		addEvent(site_switcher, 'change', siteSwitcher, false);
	}
}

function siteSwitcher() {
	var site_switcher = document.getElementById('site_switcher');
	var site = site_switcher.value;
	document.location.href = '/tpb/tools/siteswitcher.php?site=' + site;
}

function togglePass() {

	if (document.getElementById('passwordField') && document.getElementById('togglePasswordField')) {
	
		var passwordField = document.getElementById('passwordField');
		var togglePasswordField = document.getElementById('togglePasswordField');
		
		if (togglePasswordField.checked) {
			passwordField.disabled = false;
			passwordField.style.visibility = '';
			passwordField.focus();
		} else {
			passwordField.disabled = true;
			passwordField.style.visibility = 'hidden';
			passwordField.blur();
		}
	}
}

function togglePlanningID() {

	if (document.getElementById('input_reason') 
	&& document.getElementById('input_planningID') 
	&& document.getElementById('planningIDRow') 
	&& document.getElementById('otherReasonRow') 
	&& document.getElementById('input_reasonOther')) {
	
		var reason = document.getElementById('input_reason');
		var id_row = document.getElementById('planningIDRow');
		var other_row = document.getElementById('otherReasonRow');
		var id_field = document.getElementById('input_planningID'); 
		var other_field = document.getElementById('input_reasonOther'); 
		
		switch (reason.selectedIndex) {
			case 1 :
				id_row.style.display = '';
				id_field.focus();
				other_row.style.display = 'none';
				other_field.blur();
				break;
			case 2 :
				id_row.style.display = '';
				id_field.focus();
				other_row.style.display = 'none';
				other_field.blur();
				break;
			case 3 :
				id_row.style.display = '';
				id_field.focus();
				other_row.style.display = 'none';
				other_field.blur();
				break;
			case 5 :
				other_row.style.display = '';
				other_field.focus();
				id_row.style.display = 'none';
				id_field.blur();
				break;
			default :
				id_row.style.display = 'none';
				id_field.blur();
				other_row.style.display = 'none';
				other_field.blur();
		}
	}
}

function validateRegistration(formName) {

	if (!validateString(formName.firstname, 'Please provide your firstname.', 1, 100)) { return false; }
	if (!validateString(formName.surname, 'Please provide your surname.', 1, 100)) { return false; }
	if (!validateEmail(formName.email, 'Please supply a valid email address.', 0)) { return false; }
	if (!validateString(formName.organisation, 'Please provide the name of your organisation.', 1, 200)) { return false; }
	if (!validateList(formName.organisation_size_id, 'Please indicate the size of the organisation')) { return false; }
	if (!validateString(formName.postcode, 'Please provide your postcode.', 1, 20)) { return false; }
	if (!validateString(formName.username, 'Please provide a username between 4 and 20 characters long.', 4, 20)) { return false; }
	if (!validateString(formName.password, 'Please provide a password between 4 and 20 characters long.', 4, 20)) { return false; }
	
	if (document.getElementById('input_reason')) {
		var reason = document.getElementById('input_reason');
		if (reason.selectedIndex == 1 || reason.selectedIndex == 2) {
			if (!validateString(formName.planningID, 'Please provide your Planning ID', 1, 100)) { return false; }
		}
	}
}

addLoadEvent(iniSiteSwitcher);
addLoadEvent(togglePlanningID);
