(function() { 
function $(id) { return typeof id =='string'?document.getElementById(id):false; }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
window.InitForms = function() {
	var fi = 0;	
	this.maxTabIndex = 0;
	if( !document.forms[0] ) { return; }
	while( document.forms[fi] ) {
		window.InitForm(document.forms[fi]);
		window.QFormize(document.forms[fi]);
		fi++;			
	}
	window.InitHelperVals();
	window.InitFormVals();	
};
window.InitForm = function(f) {
	if( !f ) return false;
	var _onblur = function() { this.className = this.className.replace(/\bactive\b/,''); };			
	if( f.className && f.className.indexOf('noinit') != -1 ) { return false; }
	var tabs = f.className.indexOf('notabs') == -1 ? true : false;
	var sbtn = f.className.indexOf('nosubmitbtn') != -1 ? $(f.name+'btn') : null;
	if( sbtn ) {
		sbtn.onclick = function() {
			if( firefox ) {
				this.type = 'submit';
				this.className = 'pleasewait';
				this.value = 'One moment ...';
			} else {
				this.style.visibility = "hidden";
			}
			return true;
		};
		if( sbtn.className.indexOf('btn') == 0 ) {
			sbtn.onmouseover = function() { this.className = this.className + '-o'; };
			sbtn.onmouseout = function() { this.className = this.className.replace(/-o/,'');  };
		}
	}
	var parts = f.elements;
	var o;
	var autoselect;
	for( var i = 0; i < parts.length; i++ ) {
		o = parts[i];
		if( /fieldset/i.test(o.tagName) ) continue;
		if( o.type == 'hidden' ) { continue; }
		if( o.className.indexOf('selectcopy') != -1 ) {
			o.onfocus = function() {
				this.style.backgroundColor='#FFAA33';							
				this.select();
				 if( document.all ) {
				 	var Copied = this.createTextRange();
				 	Copied.execCommand('Copy');
				 }
			};
			o.onblur = function() { this.style.backgroundColor=''; };									
		} else {
			if(tabs) { o.tabIndex = ++this.maxTabIndex; }
			if( o.type == "button" && o.className.indexOf('btn') === 0) {
				o.onmouseover = function() { this.className = this.className + '-o'; };
				o.onmouseout = function() { this.className = this.className.replace(/-o/,''); };
			} else if( /text|password|select|checkbox/i.test(o.type) ) {
				o.onfocus = function() {
											if( /\bhelper\b/.test(this.className) ) { this.value = ''; }
											this.className = (this.className.replace(/\bhelper\b/,'') + ' active').trim();
											if( typeof this.select == 'function' ) { this.select(); }
										};
			} else {					
				o.onfocus = function() { this.className = (this.className.replace(/\bhelper\b/,'') + ' active').trim(); };
			}
			o.onblur = _onblur;
			try { 
				if( o.className.indexOf('autoselect') != -1 ) { o.select(); } 
			} catch(e) { }
		}
	}	
	if( sbtn && tabs ) { sbtn.tabIndex = ++this.maxTabIndex; }
};
window.InitFormVals = function() {
	var f;
	if( typeof self.qform == 'undefined' || typeof qform != 'object' ) return;
	for( var o in qform ) {
		f = document.forms[o];
		if( !f ) continue;
		for( var oo in qform[o] ) {
			if( f.elements['f_'+oo] ) {
				f.elements['f_'+oo].value = qform[o][oo];		
			}
		}
	}
}
window.InitHelperVals = function() {
	var f;
	var defaults = self.qform_defaults || qform_defaults || window.qform_defaults || 0;
	if( typeof defaults != 'object' ) return;
	for( var o in defaults ) {
		f = document.forms[o];
		if( !f ) continue;
		for( var oo in defaults[o] ) {
			if( f.elements['f_'+oo] ) {
				f.elements['f_'+oo].className = 'helper';
				f.elements['f_'+oo].value = defaults[o][oo];		
			}
		}
	}
};
window.QFormize = function(f) {
	var fname = f.name || f.id || false;
	if( !fname || f.qform ) return;
	var e = document.createElement('input');
	e.setAttribute('type','hidden');
	e.setAttribute('name','qform');
	e.setAttribute('id','qform');
	f.appendChild(e);
	e.value = fname;
};
})();
