var isDOM = typeof document.addEventListener != "undefined";
var bgcolor;

String.prototype.trim = function()
{
	return( this.replace(/(^\s+)|(\s+$)/g, "") );
}

function getCommonEvent(e)
{
	var obj = e;
	
	if ( !isDOM )
	{
		obj.e = e;
		obj.layerX = e.offsetX;
		obj.layerY = e.offsetY;
		obj.target = e.srcElement;
		obj.originalTarget = e.srcElement;
		obj.explicitOriginalTarget = e.srcElement;
		obj.preventDefault = function() { this.e.returnValue = false; }
		obj.stopPropagation = function() { this.e.cancelBubble = true; }
	}
	
	return( obj );
}

function bindEvent(n, evt, func)
{
	if ( isDOM )
		return( n.addEventListener(evt, func, false) );
	else
		return( n.attachEvent("on"+ evt, func) );
}

function select_property_use(s)
{
	var val, other, arr = [], i, len;
	
	if ( s.selectedIndex == -1 )
		return;
	else if ( (val = s.options[s.selectedIndex].value) == "" )
		return;
	else if ( val == "other" )
	{
		if ( (other = prompt("Please insert property use:", "")) )
		{
			len = s.options.length;
			for (i = 0; i < len; i++)
				arr.push( {value:s.options[i].value, text:s.options[i].text} );
			
			s.options.length = 2;
			
			for (i = 2; i < len; i++)
			{
				if ( arr[i].text == "-" )
				{
					s.options[s.options.length] = new Option(other, other);
					s.options[s.options.length] = new Option("-", "");
					s.selectedIndex = i;
				} else
					s.options[s.options.length] = new Option(arr[i].text, arr[i].value);
			}
		} else
			s.selectedIndex = 0;
	}
}

function inputHover(e)
{
	e = getCommonEvent(e || event);
	e.target.style.backgroundColor = bgcolor;
}

function inputOff(e)
{
	var focused;
	
	e = getCommonEvent(e || event);

	if ( (focused = e.target.getAttribute("focus")) && focused == "1" )
		return;
	
	e.target.style.backgroundColor = "#FFFFFF";
}

function inputFocus(e)
{
	e = getCommonEvent(e || event);
	e.target.setAttribute("focus", "1");
	e.target.style.backgroundColor = bgcolor;
}

function inputBlur(e)
{
	e = getCommonEvent(e || event);
	e.target.setAttribute("focus", "0");
	e.target.style.backgroundColor = "#FFFFFF";
}

function rowHover(e)
{
	e = getCommonEvent(e || event);
	
	var n = e.target;
	
	if ( n.tagName != "DIV" )
		n = getRow( n );
	
	n.style.border = "1px dashed #000000";
}

function rowOff(e)
{
	var focused, n;
	
	e = getCommonEvent(e || event);
	n = e.target;

	if ( n.tagName != "DIV" )
		n = getRow( n );
	
	if ( (focused = n.getAttribute("focus")) && focused == "1" )
		return;
	
	n.style.border = "1px solid #FFFFFF";
}

function getRow(n)
{
	while ( n.type != 1 )
	{
		n = n.parentNode;
		
		if ( typeof n.tagName != "undefined" && n.className.indexOf("form_row") > -1 )
			break;
	}
	return( n );
}

function bindInputColors()
{
	var typeAttr, n, i, x, n_len, len = document.forms.length;
	
	for (i = 0; i < len; i++)
	{
		n_len = document.forms[i].elements.length;
		for (x = 0; x < n_len; x++)
		{
			n = document.forms[i].elements[x];
			if ( ( n.tagName == "INPUT" || n.tagName == "TEXTAREA" || n.tagName == "SELECT" ) &&
					(typeAttr = n.getAttribute("type")) != "checkbox" &&
					typeAttr != "radio"
				)
			{
				bindEvent(n, "mouseover", inputHover);
				bindEvent(n, "mouseout", inputOff);
				bindEvent(n, "focus", inputFocus);
				bindEvent(n, "blur", inputBlur);
			}
		}
	}
}

function init_form()
{
	var divs = document.getElementsByTagName("DIV");
	var i, len = divs.length;
	
	for (i = 0; i < len; i++)
	{
		if ( divs[i].className.indexOf("form_row") > -1 )
		{
			bindEvent(divs[i], "mouseover", rowHover);
			bindEvent(divs[i], "mouseout", rowOff);
		}
	}
	
	bindInputColors();
}

bindEvent(window, "load", init_form);