/*

Written by Simon Willison:
http://simon.incutio.com/archive/2004/05/26/addLoadEvent

Queue up a whole series of events to be triggered when the document loads. If you simply use window.onload = func, then you run the risk of overwriting existing functions that are supposed to run when the onload event is triggered.

*/

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


/*

This function loops through all the text inputs on a page and stores their default values.
When a text input is brought into focus, its current value is checked against its default value.
If they are the same, the value is cleared.

This allows you to add placeholder text to inputs (recommended for accessibility) but users don't have to manually delete the placeholder text.

This function is triggered when the page loads.
The addLoadEvent.js file is required for this.

*/



addLoadEvent(clearInputs);

function clearInputs() {
	if (!document.getElementsByTagName) return false;
	var all_inputs = document.getElementsByTagName('input');
	for (var i=0;i<all_inputs.length;i++) {
		var current_input = all_inputs[i];
		if (current_input.getAttribute('type') == 'text' && current_input.getAttribute('value') != '') {
			current_input.default_text = current_input.getAttribute('value');
			current_input.onfocus = function() {
				if (this.getAttribute('value') == this.default_text) {
					this.setAttribute('value','');
				}
			}
		}
	}
}



// minmax.js: make IE5+/Win support CSS min/max-width/height
// version 1.0, 08-Aug-2003
// written by Andrew Clover <and@doxdesk.com>, use freely

/*@cc_on
@if (@_win32 && @_jscript_version>4)

var minmax_elements;

minmax_props= new Array(
  new Array('min-width', 'minWidth'),
  new Array('max-width', 'maxWidth'),
  new Array('min-height','minHeight'),
  new Array('max-height','maxHeight')
);

// Binding. Called on all new elements. If <body>, initialise; check all
// elements for minmax properties

function minmax_bind(el) {
  var i, em, ms;
  var st= el.style, cs= el.currentStyle;

  if (minmax_elements==window.undefined) {
    // initialise when body element has turned up, but only on IE
    if (!document.body || !document.body.currentStyle) return;
    minmax_elements= new Array();
    window.attachEvent('onresize', minmax_delayout);
    // make font size listener
    em= document.createElement('div');
    em.setAttribute('id', 'minmax_em');
    em.style.position= 'absolute'; em.style.visibility= 'hidden';
    em.style.fontSize= 'xx-large'; em.style.height= '5em';
    em.style.top='-5em'; em.style.left= '0';
    if (em.style.setExpression) {
      em.style.setExpression('width', 'minmax_checkFont()');
      document.body.insertBefore(em, document.body.firstChild);
    }
  }

  // transform hyphenated properties the browser has not caught to camelCase
  for (i= minmax_props.length; i-->0;)
    if (cs[minmax_props[i][0]])
      st[minmax_props[i][1]]= cs[minmax_props[i][0]];
  // add element with properties to list, store optimal size values
  for (i= minmax_props.length; i-->0;) {
    ms= cs[minmax_props[i][1]];
    if (ms && ms!='auto' && ms!='none' && ms!='0' && ms!='') {
      st.minmaxWidth= cs.width; st.minmaxHeight= cs.height;
      minmax_elements[minmax_elements.length]= el;
      // will need a layout later
      minmax_delayout();
      break;
  } }
}

// check for font size changes

var minmax_fontsize= 0;
function minmax_checkFont() {
  var fs= document.getElementById('minmax_em').offsetHeight;
  if (minmax_fontsize!=fs && minmax_fontsize!=0)
    minmax_delayout();
  minmax_fontsize= fs;
  return '5em';
}

// Layout. Called after window and font size-change. Go through elements we
// picked out earlier and set their size to the minimum, maximum and optimum,
// choosing whichever is appropriate

// Request re-layout at next available moment
var minmax_delaying= false;
function minmax_delayout() {
  if (minmax_delaying) return;
  minmax_delaying= true;
  window.setTimeout(minmax_layout, 0);
}

function minmax_stopdelaying() {
  minmax_delaying= false;
}

function minmax_layout() {
  window.setTimeout(minmax_stopdelaying, 100);
  var i, el, st, cs, optimal, inrange;
  for (i= minmax_elements.length; i-->0;) {
    el= minmax_elements[i]; st= el.style; cs= el.currentStyle;

    // horizontal size bounding
    st.width= st.minmaxWidth; optimal= el.offsetWidth;
    inrange= true;
    if (inrange && cs.minWidth && cs.minWidth!='0' && cs.minWidth!='auto' && cs.minWidth!='') {
      st.width= cs.minWidth;
      inrange= (el.offsetWidth<optimal);
    }
    if (inrange && cs.maxWidth && cs.maxWidth!='none' && cs.maxWidth!='auto' && cs.maxWidth!='') {
      st.width= cs.maxWidth;
      inrange= (el.offsetWidth>optimal);
    }
    if (inrange) st.width= st.minmaxWidth;

    // vertical size bounding
    st.height= st.minmaxHeight; optimal= el.offsetHeight;
    inrange= true;
    if (inrange && cs.minHeight && cs.minHeight!='0' && cs.minHeight!='auto' && cs.minHeight!='') {
      st.height= cs.minHeight;
      inrange= (el.offsetHeight<optimal);
    }
    if (inrange && cs.maxHeight && cs.maxHeight!='none' && cs.maxHeight!='auto' && cs.maxHeight!='') {
      st.height= cs.maxHeight;
      inrange= (el.offsetHeight>optimal);
    }
    if (inrange) st.height= st.minmaxHeight;
  }
}

// Scanning. Check document every so often until it has finished loading. Do
// nothing until <body> arrives, then call main init. Pass any new elements
// found on each scan to be bound   

var minmax_SCANDELAY= 500;

function minmax_scan() {
  var el;
  for (var i= 0; i<document.all.length; i++) {
    el= document.all[i];
    if (!el.minmax_bound) {
      el.minmax_bound= true;
      minmax_bind(el);
  } }
}

var minmax_scanner;
function minmax_stop() {
  window.clearInterval(minmax_scanner);
  minmax_scan();
}

minmax_scan();
minmax_scanner= window.setInterval(minmax_scan, minmax_SCANDELAY);
window.attachEvent('onload', minmax_stop);

@end @*/



/*

The following script enables :hover behaviour on form buttons in IE

*/


startHover = function() {
if (document.all&&document.getElementById) {

buttonRoot = document.getElementsByTagName("INPUT");
for(i=0;i<buttonRoot.length;i++){
	if(buttonRoot[i].className == 'button' || buttonRoot[i].className == 'searchSubmit'){
		buttonRoot[i].onmouseover=function() {
			this.className+=" over";
		}
		buttonRoot[i].onmouseout=function() {
			this.className=this.className.replace(" over", "");
		}	
		buttonRoot[i].onfocus=function() {
			this.className+=" over";
		}
		buttonRoot[i].onblur=function() {
			this.className=this.className.replace(" over", "");
		}			
	}
}

 }

}




/*

The following script counts how many inlineHelp divs there are on the FAQ pages and then hides them.

*/

countHelpItems = function()  {
	var countHelp = 0;

	pRoot = document.getElementsByTagName("div");

	for(i=0;i<pRoot.length;i++)	{

 		if(pRoot[i].className == 'inlinehelp')  {

 	 	countHelp = countHelp + 1;

	 	var IDtoHide = "help" + countHelp;

	 	var IDLinktoHide = "linkhelp" + countHelp;

    	document.getElementById(IDtoHide).style.display = "none";

	  	document.getElementById(IDLinktoHide).className = "helpLink";

		}
	}
}

window.onload=pageLoad;

function pageLoad(){
	startHover();
	countHelpItems();
}






/*

The following script counts enables show hide functionality of the inline help divs on the FAQ pages.

*/

function showHideHelp (div)  {

	var IDtoChange = "help" + div;

	var IDLinktoChange = "linkhelp" + div;      

	var elem = document.getElementById(IDtoChange); 

	var currentStatus = elem.style.display; 

	if (currentStatus == 'none' || currentStatus == '')  {

		elem.style.display = 'block';

		document.getElementById(IDLinktoChange).className = "helpLink downArrow"                 

   } 

	else 	{

		elem.style.display = 'none';

		document.getElementById(IDLinktoChange).className = "helpLink"

	}
}

//PM: Adding in for Active Comms 13 Jan

function hidePostalDetails() {
	if (!document.getElementById) return false;
	
	document.getElementById('postaldetails').className = 'hide';
}


function showPostalDetailsListener() {
	if (!document.getElementById) return false;
		
	document.getElementById('confirmpublications').onclick = function() {
		
		switch(document.getElementById('postaldetails').className) {
			case 'hide':
				document.getElementById('postaldetails').className = 'col-one';
			break;
			
			default:
				document.getElementById('postaldetails').className = 'hide';
		}
	}
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function disableDropdown() {
	if (getQueryVariable('simpleOrAdvanced') != 'simple')
	{
		with (window.document.advancedSearch)
		{
			fromDay.disabled=true;
			fromMonth.disabled=true;
			fromYear.disabled=true;
			toDay.disabled=true;
			toMonth.disabled=true;
			toYear.disabled=true;
		}
	}
}

//bj - added for police review form
function checkPoliceForm()
{
	var cname, corganisation, cemail, cmessage, err, erroroutput;
	var errors = [];
	err = 0;
	
	with (window.document.policecommsform)
	{
		cname = name;
		corganisation = organisation;
		cemail = email;
		cmessage = message;
	}
	
	if (cname.value == '')
	{
		errors[err] = 'Name';
		err++;
	}
	if (corganisation.value == '')
	{
		errors[err] = 'Organisation';
		err++;
	}
	if (cemail.value == '')
	{
		errors[err] = 'Email';
		err++;
	}
	else if (cemail.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	{
	}
	else
	{
		errors[err] = 'Email address is invalid';
		err++;
	}
	if (cmessage.value == '')
	{
		errors[err] = 'Message';
		err++;
	}
	
	if (errors.length > 0)
	{
		erroroutput = '';
		for( var errlist in errors ) {
    		erroroutput = erroroutput + '<li>' + errors[errlist] + '</li>';
		} 
		
		erroroutput = '<p><strong>Please check the following fields:</strong></p><ul>' + erroroutput + '</ul>';
		
		errmsg = document.getElementById("formErrors");
		errmsg.innerHTML = erroroutput;
		
		document.location.href = '#';
		
		return false;
	}
	else
	{
		return true;
	}
}


/* external links handling for xhtml 1.0 strict compliance */

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks; 
