// Defining "indexOf" function for Internet Explorer
// It returns the index of the first occurance of an item in the array
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj, start){
        for(var i = ( start || 0 ); i < this.length; i++){
            if(this[i] == obj){ return i; }
        }
    }
}

// Defining addEvent function since Internet Explorer does not support the official way of adding events
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function inputWatermark()
{
	if(WMInputs.length < 2 || WMInputs.length%2 != 0){
		return;
	}

	for (i = 0; i < WMInputs.length; i++)
	{
		if(i%2 == 0 && (document.getElementById(WMInputs[i]) || document.getElementsByName(WMInputs[i])[0]))
		{
			var cur = (document.getElementById(WMInputs[i])) ? (document.getElementById(WMInputs[i])) : (document.getElementsByName(WMInputs[i])[0]);
			if(cur.value == '') ShowWM(cur, WMInputs[i + 1]);
			addEvent(cur,"focus",onFocusHandler);
			addEvent(cur,"blur",onBlurHandler);
		}
	}
}

function HideWM(obj)
{
	var _id = "d_" + obj.id;
	var _d = document.getElementById(_id);
	if(_d) _d.style.display = "none";
	obj.focus();
}


function ShowWM(obj, value)
{
	var _id = "d_" + obj.id;
	var _d = document.getElementById(_id);
	if(!_d)
	{
		_d = document.createElement("DIV");
		_d.id = _id;
		//_d.style.padding = "1px 1px 1px 1px";
		_d.className = "watermark";
		_d.style.position = "absolute";
		_d.style.cursor = "text";
		_d.innerHTML = value;
		document.forms[0].appendChild(_d);
		_d.onclick = function() { HideWM(obj);}
	}
	
	_d.style.top = cmGetY(obj);
	_d.style.left = cmGetX(obj);
	_d.style.width = cmGetWidth(obj);
	_d.style.height = cmGetHeight(obj);
	_d.style.display = "";
}


function onFocusHandler()
{
	var inpname = this.id ? this.id : this.name;
	HideWM(this);
}

function onBlurHandler()
{
	var inpname = this.id ? this.id : this.name;
	
	if(this.value == '')
	{
		ShowWM(this, WMInputs[WMInputs.indexOf(inpname) + 1])
	}
}

addEvent(window,"load",inputWatermark);
window.onresize = function() { inputWatermark(); }
// Defining array that holds the IDs or Names of the inputs and the default text to display
// If you are using Names remeber that I am taking only the first one.
// The format is : 'ID1','VALUE1','ID2','VALUE2'....
var WMInputs = new Array();

//WMInputs.push('a_RS__ctl0__ctl0_UNVerticalTxt');
//WMInputs.push('firstvalue');
//);


var n_aval = "custom_modules/catalog/images/n_aval.gif";
function __er(obj) { if(obj.src.indexOf(n_aval) == -1 ) { obj.src = n_aval; } }
