var swapEnabled = true;

function moveOption (sourceSelect, targetSelect, 
                     keepSourceLabel, unmovableSourceValues, 
                     keepTargetLabel) {

    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 != keepSourceLabel);
            if (canMove && unmovableSourceValues != null) {
                // make sure we don't move any options defined as unmovable
                for (var j = 0; j < unmovableSourceValues.length; j++) {
                    if (unmovableSourceValues[j] == option.value) {
                        canMove = false;
                        break;
                    }
                }
            }

            // 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;
            }
        }
    }

    // move them over one by one
    var targetOptions = targetSelect.options;
    if (selectedIds.length > 0) {
        targetSelect.selectedIndex = -1;
        for (var i = 0; i < selectedIds.length; i++) {
            option = new Option (sourceOptions[selectedIds[i]].text, sourceOptions[selectedIds[i]].value);
            
            // replace the target value if its the last one
            if (targetOptions.length == 1 && targetOptions[0].text == keepTargetLabel) {
                targetOptions[0] = option;
                targetOptions[0].selected = true;
            } else {
                targetOptions[targetOptions.length] = option;
                targetOptions[targetOptions.length-1].selected = true;
            }
        }
    }

    // notify the Select Elements that their contents have changed
    if (targetSelect["onchange"]) {
        targetSelect.onchange();
    }
    if (sourceSelect["onchange"]) {
        sourceSelect.onchange();
    }

    // remove values from the source 
    for (var i = selectedIds.length; i > -1; i--) {
        sourceOptions[selectedIds[i]] = null;
    }

    // make sure we don't get an empty list
    if (sourceOptions.length == 0) {
        sourceOptions[0] = new Option (keepSourceLabel, keepSourceLabel);
    }

    // if we moved anything, put the focus on the target list box
    if (selectedIds.length > 0) targetSelect.focus ();
}


function moveUp(sourceSelect) {
    if (sourceSelect.length > 1) {
        var options = sourceSelect.options;

        // find which ones are selected...
        var selectedIds = new Array ();
        var index = 0;
        for (var i = 1; i < sourceSelect.length; i++) {
            if (options[i].selected) {
                selectedIds[index] = i;
                index++;
            }
        }

        // move each selected option up 
        var selId;
        for (var i = 0; i < selectedIds.length; i++) {
            selId = selectedIds[i];
            privateMoveUp (options, selId);
            options[selId].selected = false;
            options[selId-1].selected = true;
        }

        sourceSelect.focus ();
    }
}

function moveDown(sourceSelect) {
    if (sourceSelect.length > 1) {
        var options = sourceSelect.options;

        // find which ones are selected
        var selectedIds = new Array ();
        var index = 0;
        for (var i = sourceSelect.length-2; i >= 0; i--) {
            if (sourceSelect.options[i].selected) {
                selectedIds[index] = i;
                index++;
            }
        }

        // move each selected element down
        var selId;
        for (var i = 0; i < selectedIds.length; i++) {
            selId = selectedIds[i];
            privateMoveDown (options, selId);
            options[selId].selected = false;
            options[selId+1].selected = true;
        }
        
        sourceSelect.focus ();
    }
}


function moveTop(sourceSelect) {
    var selIndex = sourceSelect.selectedIndex;

    if (sourceSelect.length > 1 && selIndex > 0) {
        var options = sourceSelect.options;

        for (var i = selIndex; i > 0; i--) {
            privateMoveUp (options, i);
        }
        
        sourceSelect.focus ();
        sourceSelect.selectedIndex = 0;
    }
}

function moveBottom(sourceSelect) {
    var selIndex = sourceSelect.selectedIndex;

    // gots to have at least 2 items and something selected, but not the last one 
    if (sourceSelect.length > 1 && selIndex > -1 && selIndex < sourceSelect.length - 1) {
        var options = sourceSelect.options;
      
        for (var i = selIndex; i < sourceSelect.length - 1; i++) {
            privateMoveDown (options, i);
        }

        sourceSelect.focus ();
        sourceSelect.selectedIndex = sourceSelect.length - 1;
    }
}

/*
 * Do not call this function directly. 
 * As it does NO bounds checking.
 * Please use the moveUp or moveTop calls.
 */
function privateMoveUp (options, index) {
    var newOption = new Option (options[index-1].text, options[index-1].value);
    options[index-1].text = options[index].text;
    options[index-1].value = options[index].value;
    options[index].text = newOption.text;
    options[index].value = newOption.value;
}

/*
 * Do not call this function directly. 
 * As it does NO bounds checking.
 * Please use the moveDown or moveBottom calls.
 */
function privateMoveDown (options, index) {
    var newOption = new Option (options[index+1].text, options[index+1].value);
    options[index+1].text = options[index].text;
    options[index+1].value = options[index].value;
    options[index].text = newOption.text;
    options[index].value = newOption.value;
}


/**
 * Used when submitting a dueling list boxes element. 
 * Stores all the values into hidden form parameters so we can get them out
 */
function unselectSelected(selectArray) {
    for (var i = 0; i < selectArray.length; i++) {
		selectArray.options[i].selected = false;
	}
}

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;
            }
        }
    }
}

var win_popup = null;

function openWindow(URL, windowName, Features, Width, Height, showType) {
	if (document.all && showType == 'Fullscreen') {
		if (Features.length > 0) {
			Features+=',fullscreen=1';
		}
		else {
			Features+='fullscreen=1';
		}
	}				
	
	if (win_popup && win_popup.open && !win_popup.closed) {
		win_popup.location.href=URL;
		win_popup.focus();
	} 
	else {
		win_popup = window.open(URL,windowName,Features);
	}

	
	if (document.all) {
		if (showType == 'Maximized') {
			win_popup.moveTo(0, 0);
			win_popup.resizeTo(screen.availWidth,screen.availHeight); //- resize if not full screen mode
		}
		else if (showType == 'Centered') {

			win_popup.moveTo((screen.availWidth - Width) / 2, (screen.availHeight - Height) / 2);				
		}	
	}
	else if ((document.layers||document.getElementById)) {
		if (showType == 'Maximized' || showType == 'Fullscreen') {
			win_popup.moveTo(0, 0);
								
			if (win_popup.outerHeight<screen.availHeight||win_popup.outerWidth<screen.availWidth){
				win_popup.outerHeight = win_popup.availHeight;
				win_popup.outerWidth = win_popup.availWidth;
			}
		}
		else if (showType == 'Centered') {
			win_popup.moveTo((win_popup.availWidth - Width) / 2, (win_popup.availHeight - Height) / 2);				
		}
	}
}

// if the user leaves this page or the browser is closed, then close the open window as well.
function closeWin() {
	if (win_popup && win_popup.open && !win_popup.closed) win_popup.close();
}

function focusWin() {
	if (win_popup && win_popup.open && !win_popup.closed) win_popup.focus();
}

function closeWindow() {
	window.close();				
	opener.document.frmMain.submit();
	opener.focus();
}

function ToggleForm(Section, ImageHideShow)
{
	var sDisplay = Section.style.display;

	if(sDisplay == "none")
	{
		MM_swapImage(ImageHideShow,'','/images/icons/hide.gif',1)
		sDisplay = "inline";
	}
	else if(sDisplay == "inline")
	{
		MM_swapImage(ImageHideShow,'','/images/icons/show.gif',1)
		sDisplay = "none";
	}

	Section.style.display = sDisplay;
}


function moveOver(selectTo, selectFrom) {
	if (swapEnabled) {
		var boxLengthFrom = selectFrom.length;
		var boxLengthTo = selectTo.length;
		
		var count = 0;
		var isNew;
		for (i = 0; i < boxLengthFrom; i++) {
			if (selectFrom.options[i].selected) {
				isNew = true;

				if (boxLengthTo != 0) {
					for (x = 0; x < boxLengthTo; x++) {
						if (selectTo.options[x].value == selectFrom.options[i].value) {
							isNew = false;
							break;
						}
					}
				} 

				if (isNew) {
					newoption = new Option(selectFrom.options[i].text, selectFrom.options[i].value, false, false);
					selectTo.options[boxLengthTo] = newoption;
					boxLengthTo++;
				}				

			}
			count++;
		}

		removeMe(selectFrom); 

		selectFrom.selectedIndex=-1;
	}
}

function clearSelect(selectField) {	for (var i = selectField.options.length;i>=0;i--) {		selectField.options.remove(i);	}}
function create2DArray(x, y) {
  array = new Array();
  for (var i = 0; i < x; i++)
    array[i] = new Array(y); // optional presizing of array
  return array;
}

function createSelectArray(selectField) {
	var newArray = create2DArray(selectField.length, 2);
		for (var i = 0;i<selectField.length;i++) {		newArray[i][0] = selectField.options[i].value;		newArray[i][1] = selectField.options[i].text;	}	return newArray;	}

function resetAssociations(initialArray, selectField) {
	clearSelect(selectField);

	for (var i = 0; i < initialArray.length; i++) {
		newoption = new Option(initialArray[i][1], initialArray[i][0], false, false);
		selectField.options[i] = newoption;
	}

}

function removeMe(selectBox) {
	var boxLength = selectBox.length;
	arrSelected = new Array();

	var count = 0;
	for (i = 0; i < boxLength; i++) {
		if (selectBox.options[i].selected) {
			arrSelected[count] = selectBox.options[i].value;
		}
		count++;
	}

	var x;
	for (i = 0; i < boxLength; i++) {
		for (x = 0; x < arrSelected.length; x++) {
			if (selectBox.options[i].value == arrSelected[x]) {
				selectBox.options[i] = null;
			}
		}
		boxLength = selectBox.length;
	}
}

function selectAll(selectBox) {
	var boxLength = selectBox.length;
	var count = 0;
	
	if (boxLength != 0) {
		for (i = 0; i < boxLength; i++) {
			selectBox.options[i].selected = true;
		}
	}
}

function saveMe() {
	var strValues = "";
	var boxLength = document.choiceForm.choiceBox.length;
	var count = 0;
	
	if (boxLength != 0) {
		for (i = 0; i < boxLength; i++) {
			if (count == 0) {
				strValues = document.choiceForm.choiceBox.options[i].value;
			}
			else {
				strValues = strValues + "," + document.choiceForm.choiceBox.options[i].value;
			}
			count++;
		}
	}

	if (strValues.length == 0) {
		alert("You have not made any selections");
	}
	else {
		alert("Here are the values you've selected:\r\n" + strValues);
   }
}

function associateIt(ID) {
	document.frmFind.associateid.value = ID;
	saveAllSelected([ document.frm.ID ], [ document.frmFind.IDs ], ',', '\\', '--None--');
	document.frmFind.submit();
}

function searchClear() {
	document.frmFind.page.value = '';
	document.frmFind.sortBy.value = '';
	document.frmFind.sortMethod.value = '';
}

var win = null;

function openAssociationToolWindow(URL, windowName, Features, ID, Item, showType) {

	if (ID.length > 0) {
		if (document.all && showType == 'Fullscreen') {
			if (Features.length > 0) {
				//Features+=',fullscreen=1';
			}
			else {
				//Features+='fullscreen=1';
			}
		}				
		
		if (win && win.open && !win.closed) {
			win.location.href=URL;
			win.focus();
		} 
		else {
			win = window.open(URL,windowName,Features);
		}

		win.moveTo(0, 0);

		if (document.all && showType == 'Maximized' || 1==1) {  // made sure this always happened
			win.resizeTo(screen.availWidth,screen.availHeight); //- resize if not full screen mode
		}
		else if ((document.layers||document.getElementById) && (showType == 'Maximized' || showType == 'Fullscreen')) {
			if (win.outerHeight<screen.availHeight||win.outerWidth<screen.availWidth){
				win.outerHeight = win.availHeight;
				win.outerWidth = win.availWidth;
			}
		}
	}
	else {
		alert('ALERT!!! You need to create a new ' + Item + ' first before performing associations.  Please enter all required fields, save your ' + Item + ', then try again.');
	}
}

// opener.location.reload();
// opener.document.frmMain.submit();

// if the user leaves this page or the browser is closed, then close the open window as well.
function closeWin() {
	if (win && win.open && !win.closed) win.close();
}

function focusWin() {
	if (win && win.open && !win.closed) win.focus();
}

function ToggleForm(Section, ImageHideShow)
{
	var sDisplay = Section.style.display;

	if(sDisplay == "none")
	{
		MM_swapImage(ImageHideShow,'','/images/icons/hide.gif',1)
		sDisplay = "inline";
	}
	else if(sDisplay == "inline")
	{
		MM_swapImage(ImageHideShow,'','/images/icons/show.gif',1)
		sDisplay = "none";
	}

	Section.style.display = sDisplay;
}

function eBayAddToQuery() {

var strcid = '';
strcid = frmSearch.cid.options[frmSearch.cid.selectedIndex].value;
if (strcid.length > 0) { strcid = ' ' + strcid; }

var strpricerange = '';
var strminprice = ''; 
var strmaxprice = ''; 
var aPR;
strpricerange = frmSearch.PriceRange.options[frmSearch.PriceRange.selectedIndex].value;
if (strpricerange.length > 0) { 
	aPR = strpricerange.split('|');
	
	strminprice = aPR[0];
	strmaxprice = aPR[1];
	
	frmSearch.minPrice.value = strminprice;
	frmSearch.maxPrice.value = strmaxprice;
}

frmSearch.query.value = frmSearch.query.value + strcid + ' ';
}