/* fancy stuff: only for layout, no functionality
 * domTableEnhance: highlight rows with mouseover and click
 * formhighlight: highlight form textboxes when focused
 */
/* ############## fancy table enhance */
/* the table must have the class "js-fancytable" defined */
function domTableEnhance() {
	if(!document.createTextNode){return;}
	var tableClass='js-fancytable';
	var colourClass='enhancedtablecolouredrow';
	var hoverClass='enhancedtablerowhover';
	var activeClass='enhancedtableactive';
	var alltables,bodies,i,j,k,addClass,trs,c,a;
	alltables=document.getElementsByTagName('table');
	for (k=0;k<alltables.length;k++) {
		if(!alltables[k].className.match(tableClass)){continue;}
		bodies=alltables[k].getElementsByTagName('tbody');
		for (i=0;i<bodies.length;i++) {
			trs=bodies[i].getElementsByTagName('tr');
			for (j=0;j<trs.length;j++) {
				if(trs[j].getElementsByTagName('td').length>0) {
					addClass=j%2==0?' '+colourClass:'';
					trs[j].className=trs[j].className+addClass;
					trs[j].onclick=function() {
						if(this.className.match(activeClass)) {
							var rep=this.className.match(' '+activeClass)?' '+activeClass:activeClass;
							this.className=this.className.replace(rep,'');
						} else {
							this.className+=this.className?' '+activeClass:activeClass;
						}
					}
					trs[j].onmouseover=function(){
						this.className=this.className+' '+hoverClass;
					}
					trs[j].onmouseout=function(){
						var rep=this.className.match(' '+hoverClass)?' '+hoverClass:hoverClass;
						this.className=this.className.replace(rep,'');
					}
				}
			}
		}
	}
}
// initialization
addOnloadEvent(domTableEnhance);
//Fancy form highlight function
// TODO: use style classes instead of colors, usage of function cssjs (ultabs.js)
function form() {
	var dbgColor = '#FFF'; // Background color, Default
	var fbgColor = '#FFFFCC'; // Background color, onFocus
	var tagInput = document.getElementsByTagName('input');
	var tagText = document.getElementsByTagName('textarea');
	for (var i = 0; i < tagInput.length; i++)  {
		if (tagInput[i] 
		    && !tagInput[i].className.match('error')
			&& tagInput[i].getAttribute('type') != ''
			&& tagInput[i].getAttribute('type').toLowerCase() == 'text'
			|| tagInput[i].getAttribute('type').toLowerCase() == 'password') {
			tagInput[i].style.backgroundColor = dbgColor;
			thisValue = tagInput[i].getAttribute("title");
			if (thisValue) {
				tagInput[i].value = thisValue;
			}
			tagInput[i].onfocus = function() {
				this.style.backgroundColor = fbgColor;
				if (this.value != this.getAttribute("title") && this.value != '') {
					return;
				} else {
					this.value = '';
				}
			};
			tagInput[i].onblur = function() {
				this.style.backgroundColor = dbgColor;
				if (this.value == '' && this.getAttribute("title") != null) {
					this.value = this.getAttribute("title");
				}
			};
		}
	}
	for (var i = 0; i < tagText.length; i++){
		if (tagText[i]){
			tagText[i].style.backgroundColor = dbgColor;
			thisValue = tagText[i].getAttribute("title");
			if (thisValue){
				tagText[i].value = thisValue;
			}
			tagText[i].onfocus = function(){
				this.style.backgroundColor = fbgColor;
				if (this.value != this.getAttribute("title") && this.value != ''){
					return;
				}else{
					this.value = '';
				}
			};
			tagText[i].onblur = function(){
				this.style.backgroundColor = dbgColor;
				if (this.value == '' && this.getAttribute("title") != null){
					this.value =  this.getAttribute("title");
				}
			};
		}
	}
}
// initialization
addOnloadEvent(form);
