function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
}

/**
 * Dynamically transforms the search field at the bottom of the page into a
 * Safari Search control in Safari
 */
var safariSearch = {
	init: function() {
		if(navigator.userAgent.toLowerCase().indexOf('webkit') != -1) {
			safariSearch.build();
		}
	},
	build: function() {
		var baseObj = document.getElementById('base_search');
		if(typeof(baseObj) != 'undefined' && baseObj != null)
		{
			this.label = baseObj.getElementsByTagName('label')[0];
			this.field = baseObj.getElementsByTagName('input');
			// loop through all inputs in the LI - capture just the textfield
			for (var i=0; i<this.field.length; i++) {
				if (this.field[i].type == 'text') {
					this.field = this.field[i];
					break;
				}
			}
			
			if(typeof(this.field) != 'undefined')
			{
				this.field.setAttribute('type','search');
				this.field.setAttribute('placeholder',this.label.firstChild.nodeValue);
				this.field.setAttribute('results','20');
				this.field.setAttribute('class',this.field.className.replace("fake_safari",""));
			}
		}
	}
}

var ieSearch = {	
	init: function() {
		ieSearch.build();
	},
	build: function() {
		var baseObj = document.getElementById('base_search');
		
		if(typeof(baseObj)  != 'undefined' && baseObj != null)
		{
			this.fields = baseObj.getElementsByTagName('input');
			// loop through all inputs in the LI - capture just the textfield
			for (var i=0; i<this.fields.length; i++) {
				switch(this.fields[i].type)
				{
					case 'text':
						this.field = this.fields[i];
						break;
					case 'image':
						this.button = this.fields[i];
						break;
				}
			}
			
			if(typeof(this.field) != 'undefined')
			{
				addEvent(this.field,'keypress',this.checkEnter,false);
			}
		}
	},
	checkEnter: function(e) {
		if(e.keyCode == 13)
		{
			e.cancelBubble = true;
			e.returnValue = false;	
			
			ieSearch.submitForm();
		}
	},
	submitForm: function() {
		this.button.click();
		//this.form = document.getElementById("Form1");
		//this.form.submit();
	}
}

addEvent(window,'load',safariSearch.init,false);
addEvent(window,'load',ieSearch.init,false);

// onclick popup function
POPUP_W = 600;
POPUP_H = 620;
POPUP_SCROLL = true;
POPUP_RESIZE = true;
POPUP_EXTRAS = 'location=0,status=0,menubar=0,directories=no,toolbar=no';
// USAGE: popuplink(['non-js url',] this[, w[, h[, scroll[, resize[, extras]]]]])
function popuplink() {
	var undef, i=0, args=popuplink.arguments;
	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');
	var target = args[i++].getAttribute('target') || '_blank';
	var w = args[i++];
	var h = args[i++];
	var s = (args[i]===undef) ? POPUP_SCROLL : args[i++];
	var r = (args[i]===undef) ? POPUP_RESIZE : args[i++];
	var features = 'width=' + (w || POPUP_W)
				 + ',height=' + (h || POPUP_H)
				 + ',scrollbars=' + (s ? 'yes,' : 'no,')
				 + ',resizable=' + (r ? 'yes,' : 'no,')
				 + (args[i] || POPUP_EXTRAS);
	var win = window.open(url, target, features);
	win.focus();
	return false;
}

/* text input default text (stored in <label for="" title="default text"/>*/
var DefaultText = Class.create({
	initialize: function(label){
		var elLabel = $(label);
		
		if(!elLabel){return;}
		
		var clearHandler = this.clearDefaultText.bindAsEventListener(this);
		var replaceHandler = this.replaceDefaultText.bindAsEventListener(this);
		
		var txtInput = $(elLabel.readAttribute('for'));
		if(txtInput){
			// add default text to text input from label's title
			txtInput.defaultText = elLabel.readAttribute('title');
			txtInput.observe('focus', clearHandler);
			txtInput.observe('blur', replaceHandler);
			
			//set the initial default text
			if(txtInput.value == ''){
				txtInput.value = txtInput.defaultText;
			}
		}
	},
	
	clearDefaultText: function(e){
		var elTxtInput = e.element();
		if (elTxtInput.value == elTxtInput.defaultText) {
			elTxtInput.value = '';
		}
		else{
			// select the users search term
			elTxtInput.select();
		}
		
	},
	
	replaceDefaultText: function(e){
		var elTxtInput = e.element();
		if (elTxtInput.value == '' && elTxtInput.defaultText) {
		  elTxtInput.value = elTxtInput.defaultText;
		}	
	}
});
/*
sample usage:
// Search for operas textbox
document.observe('dom:loaded', function(){
	if($('opera_searchbox')){
		var defaultText = new DefaultText($$('#opera_searchbox label'));
	}
});
*/

// Class to fire click on a specific button when the Enter key is entered into a textbox, 
// instead of the default form post.
var TextButtonAssociate = Class.create({
	initialize: function(elTextInput, elButton, bValidate){
		if(!elTextInput || !elButton){return;}
		
		this.elTextInput = $(elTextInput); 		// text | textarea input element
		this.elButton = $(elButton);			// button input element
		this._bValidate = bValidate || false; 	// optional arg to validate text input
		
		elTextInput.observe('keypress', this.__onKeyPress.bindAsEventListener(this));
	},
	
	__onKeyPress: function(e){
		if(e.keyCode == Event.KEY_RETURN) {
			e.stop(); // prevent default form submittal
			if(this._bValidate){
				if(this._isValidValue(this.elTextInput.value)){
					this.elButton.click(); // fire associated button's click event
				}
			}
			else{
				this.elButton.click(); // fire associated button's click event
			}
		}
	},
	
	_isValidValue: function(sValue){
		var isValid = true;

		if(this.elTextInput.defaultText){ // check for the custom default text value
			isValid = (sValue != this.elTextInput.defaultText);
		}
		if(isValid){
			isValid = (sValue.length > 0); 
		}
		return isValid;
	}
});