// Adjust cache setting to fix IE6 background image flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

// Detect UserAgent, store attributes in convenient object
var Engine = {
	detect: function() {
		var UA = navigator.userAgent;
		this.isMac = /Mac/.test(UA);
		this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
		this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
		this.isOpera = /Opera/.test(UA);
		this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
		this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
	}
}
Engine.detect();


// validate login form in template
function validateLogin() {
	if (cleanInput('txtGuiUser') && cleanInput('txtGuiPass')) {
		return true;
	}
	else {
        var LoginMessage = new Message($('LoginUser'), {
            showDuration: 5000, 
            offsets: {x: 172, y: 0}, 
            title: 'Login Error', 
            text: 'Please enter a username and password.',
            initialize:function(){
		        this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 250, wait: false}).set(0);
	        },
	        onShow: function(toolTip) {
		        this.fx.start(1);
	        },
	        onHide: function(toolTip) {
		        this.fx.start(0);
	        }
        });
		//alert('To be replaced with something more attractive:\nPlease enter a username and password');
		return false;
	}
}

// validate search form in template
function validateSearch() {
	if (cleanInput('txtGuiSearch')) {
		return true;
	}
	else {
	    var SearchMessage = new Message($('GuiSearch'), {
	        showDuration: 5000, 
            offsets: {x: 48, y: 28}, 
            title: 'Search Error', 
            text: 'Please enter the term(s) for which you would like to search.',
            initialize:function(){
		        this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 250, wait: false}).set(0);
	        },
	        onShow: function(toolTip) {
		        this.fx.start(1);
	        },
	        onHide: function(toolTip) {
		        this.fx.start(0);
	        }
        });
		//alert('To be replaced with something more attractive:\nPlease enter search terms');
		return false;
	}
}

// validate search form on search page
function validateSearchDefault() {
	if (cleanInput('txtSiteSearch')) {
		return true;
	}
	else {
	    var SearchMessage = new Message($('txtSiteSearch'), {
	        showDuration: 5000, 
            offsets: {x: 0, y: 22}, 
            title: 'Search Error', 
            text: 'Please some terms for which you would like to search.',
            initialize:function(){
		        this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 250, wait: false}).set(0);
	        },
	        onShow: function(toolTip) {
		        this.fx.start(1);
	        },
	        onHide: function(toolTip) {
		        this.fx.start(0);
	        }
        });
		//alert('To be replaced with something more attractive:\nPlease enter search terms');
		return false;
	}
}

// reset CMS-generated forms
function tcgFormReset() {
    if (confirm('This will empty all information that you entered into this form.  Are you sure that you want to continue?')) 
        window.location.href = window.location.href;
}

// Strip an input's value of everything resembling a tag, for .Net submissions '
function cleanInput(inputElement) {
	inputElement = $(inputElement);
	if (inputElement && inputElement.value) {
		inputElement.value = inputElement.value.replace(/(<[^>]*>)|(<)|(>)/gi,'');
		if (inputElement.value.length > 0) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}

function $(element) {
  if (typeof element == 'string')
    element = document.getElementById(element);
  return element;
}

// function for RadWindow - makes windows the same size across browsers
function OnClientShow(oWnd) { oWnd.SetSize(oWnd.GetWidth(), parseInt(oWnd.Height)); }