var lastMouseX;
var lastMouseY;
var curPopupWindow = null;

function setLastMousePosition(e) {
	if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
	lastMouseX = e.screenX;
	lastMouseY = e.screenY;
}

function openPopupReturn(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
	return openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
	openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus) {
	closePopup();

	if (snapToLastMousePosition) {
		if (lastMouseX - pWidth < 0) {
			lastMouseX = pWidth;
		}
		if (lastMouseY + pHeight > screen.height) {
			lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
		}
		lastMouseX -= pWidth;
		lastMouseY += 10;
		features += "screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
	}

	if (closeOnLoseFocus) {
		curPopupWindow = window.open(url, name, features, false);
		curPopupWindow.focus();
		return curPopupWindow;
	} else {
		win = window.open(url, name, features, false);
		win.focus();
		return win;
	}
}

function closePopup() {
	if (curPopupWindow != null) {
       
		if (!curPopupWindow.closed)
			curPopupWindow.close();
		
		curPopupWindow = null;
	}
}

function openLookup(baseURL, tbl, frm, fld_id, fld_txt, param) {
	var str = baseURL + "/lookup/lookup.cfm?tbl=" + tbl + "&frm=" + frm + "&fld_id=" + fld_id + "&fld_txt=" + fld_txt;

	if (param == undefined) param = '';
	
	str += '&param=' + param;
	
	openPopup(str, "lookup", 430, 300, "width=430,height=300,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}

function openCombo(baseURL, frm, fld, evt) {
	var str = baseURL + "/lookup/combo.cfm?frm=" + frm + "&fld=" + fld + "&evt=" + evt;
	
	return openPopup(str, "combo", 300, 200, "width=300,height=200,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}

function showDate(baseURL, fld, frm) {
	var str = baseURL + "/lookup/cal.cfm?frm=" + frm + "&fld=" + fld;
	if (eval("document." + frm + "." + fld).value.length > 0)
		str = str + "&cdate=" + eval("document." + frm + "." + fld).value;
	
		openPopup(str, "calwin", 170, 190, "width=170,height=190,toolbar=no,status=no,directories=no,menubar=no,resizable=no,scrollable=no", true);
}


/**
 * Used when submitting a dueling list boxes element. 
 * Stores all the values into hidden form parameters so we can get them out
 */
function saveAllSelected (fromSelectArray, toArray, delim, escape, emptyLabel) {
    var i,j,escapedValue;
    // loop through all the select elements
    for (i = 0; i < fromSelectArray.length; i++) {
        toArray[i].value = ''; // clear out the value to start
        // now loop through all the values in the select element
        for (j = 0; j < fromSelectArray[i].length; j++) {
            // copy over the value as long as it is not the emptyLabel
            if (!(fromSelectArray[i].length == 1 && fromSelectArray[i].options[0].value == emptyLabel)) {
                toArray[i].value += fromSelectArray[i].options[j].value.replace(new RegExp(delim,"g"), escape+delim);
            }

            // add the delimiter (except after the last one)
            if (j + 1 < fromSelectArray[i].length) {
                toArray[i].value += delim;
            }
        }
    }
}

function removeOption(sourceSelect, keepLabel) {
    var sourceOptions = sourceSelect.options;    
    var canMove;
    var option;
    
    // find which ones are selected...
    var selectedIds = new Array ();
    var index = 0;
    for (var i = 0; i < sourceSelect.length; i++) {
        option = sourceOptions[i];
        if (option.selected) {
            canMove = (option.text != keepLabel);
						
            // if this option can be moved we add it to our array of elements to move
            if (canMove) {
                selectedIds[index] = i;
                index++;
            } else {
                // if we can't move this option, then unselect it
                option.selected = false;
            }
        }
    }
   
		// remove selected values from the source, starting with the last one selected
    for (var i = selectedIds.length - 1; i > -1; i--) {
        sourceSelect.remove(selectedIds[i]);
    }

    // Workaround here for a bug in IE:
    // If you have a select element with many values, and you've scrolled to 
    // the bottom and move an option from the top-most element you can now see,
    // IE would not refresh the select element, leaving a hole in the list. 
    // By forcing the select element disabled and back, it seems to refresh the
    // element properly.
    sourceSelect.disabled = true;
    sourceSelect.disabled = false;

    // make sure we don't get an empty list
    if (sourceOptions.length == 0) {
        sourceOptions[0] = new Option (keepLabel, '');
    }
}

function openWindow2(url) {
	var popupWin2 = window.open(url, 'popup2', 'toolbar=no,menubar=no,scrollbars=yes,resizable=yes,dependent,width=790,height=500,left=50,top=50');
	popupWin2.focus();
}

function openAuctionWindow(baseURL, liveauctionid) {
	var str = baseURL + "/liveauctions/index.cfm?action=liveauction&liveauctionid=" + liveauctionid;

	openPopup(str, "liveauction", 980, 700, "width=980,height=700,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=auto", true);
}

