var tP = {
  // initialisation function, call with load
  init: function() {
    if (!document.getElementById) return;    
	    
	  tP.filter_startdate = document.getElementById('filter_startdate');
	  tP.filter_enddate = document.getElementById('filter_enddate');
	  tP.enddate_label = document.getElementById('enddate_label');
	  
	    
    tP.addEvent(tP.filter_startdate, 'keyup', tP.datefunction, false);
	    
	    
		tP.eventFilterform = document.getElementById('eventFilterform');		
		tP.radioObj = document.forms['eventFilterform'].elements['location_filter_type'];	
		
    
		tP.location_filter_type_town = document.getElementById('location_filter_type_town');	
		tP.location_filter_type_postcode = document.getElementById('location_filter_type_postcode');			
    tP.addEvent(tP.location_filter_type_town, 'click', tP.getCheckedValue, false);
    tP.addEvent(tP.location_filter_type_postcode, 'click', tP.getCheckedValue, false);
    tP.addEvent(tP.location_filter_type_town, 'keypress', tP.getCheckedValue, false);
    tP.addEvent(tP.location_filter_type_postcode, 'keypress', tP.getCheckedValue, false);
	    
	  tP.getCheckedValue();
  },
  
  
  datefunction: function() {
  	if (tP.filter_enddate.value == '') {  
  		if (tP.filter_startdate.value.match(/\d\d\/\d\d\/\d\d\d\d/)) {
  			tP.filter_enddate.value = tP.filter_startdate.value;
	      tP.enddate_label.style.visibility = 'hidden';
  		}  	
  	} 
  },
  
	getCheckedValue: function() {
		if(!tP.radioObj)
			return "";
		var radioLength = tP.radioObj.length;
		if(radioLength == undefined) {
			return "";
		}
		for(var i = 0; i < radioLength; i++) {
			if(tP.radioObj[i].checked) {
				if (tP.radioObj[i].value == 'town') {
					document.getElementById('filter_town_cont').style.display = 'block';
					document.getElementById('filter_postcode_cont').style.display = 'none';
				} else {
					document.getElementById('filter_town_cont').style.display = 'none';
					document.getElementById('filter_postcode_cont').style.display = 'block';
				}
				
			}
		}
		return "";
	},

  
  
	
	// function to add event listener, also caches events so they can be removed when the
	// page unloads to avoid memory leaks in IE
  addEvent: function(elm, evType, fn, useCapture) {
		// for W3C DOM complience
    if (elm.addEventListener) {
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } 
		// for IE...
		else if (elm.attachEvent) {
      var r = elm.attachEvent('on' + evType, fn);
      //EventCache.add(elm, evType, fn);
      return r;
    } else {
			// for anyone else not IE or Moz... Safari etc
      elm['on' + evType] = fn;
    }
  }
	
}

tP.addEvent(window, 'load', tP.init, false);