// Global variable to hold name of input element that
// will be updated by the popup.  This is set in the 
// 'fireMyPopup' routine.
var fieldname;

// Function called by the links in the popup.  Updates
// the value in the input field with value chosen in 
// the popup.
function setInputField(fieldValue) {
	formname = "MultiSample"
	document[formname][fieldname].value=fieldValue;
	fadeOutMyPopup();
}

// Browser safe opacity handling function

function setOpacity( value ) {
	document.getElementById("styled_popup").style.opacity = value / 10;
	document.getElementById("styled_popup").style.filter = 'alpha(opacity=' + value * 10 + ')';
}

function fadeInMyPopup() {
	for( var i = 0 ; i <= 100 ; i++ )
		setTimeout( 'setOpacity(' + (i / 10) + ')' , 2 * i );
}

function fadeOutMyPopup() {
	for( var i = 0 ; i <= 100 ; i++ ) {
		setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 2 * i );
	}

	 setTimeout('closeMyPopup()', 800 );
}

function closeMyPopup() {
	document.getElementById("styled_popup").style.display = "none"
}

function fireMyPopup(ref, target) {
	setOpacity( 0 );

	ok=0; // it's just to start the loop, we don't use it to get out.
	while (!ok) {
		ref = ref.parentNode;
		//check that the node is a tag, not text (type=3)
		if (ref.nodeType==1) {
			if (String(ref.nodeName)=="FIELDSET") {
				parentFieldset = ref.id;
				ok = 1;
			}
			if (String(ref.nodeName)=="BODY") {
				ok = 1;
				return false;
			}
		}
	}


	// Get the position of the parent fieldset
	x = ref.offsetLeft;
	y = ref.offsetTop;

	// Offset it a little
	x = x + 15;
	y = y + 15;

	// Find the row number if any that is generated by
	// the wforms repeat class.  This is a '-n' string
	// that is appended to all elements when the wforms
	// scripts create duplicates.
	len = parentFieldset.length - 1;

	s = parentFieldset.substring(len)
	n = parseInt(s);
	// If n is an integer, then we are working on a 
	// repeated element, so add in the -n string to get
	// the correct input field to use.
	if (isNaN(n)) {
		fieldname = target;
	} else {
		fieldname = target + "-" + s;
	}

	// Now place the popup in the correct position and
	// display it.
	obj = document.getElementById("styled_popup");
	obj.style.left = x + "px";
	obj.style.top = y + "px";
	obj.style.display = "block";
	fadeInMyPopup();

	return false;
}
