// -------------------------------------------------------------------
// left2right()
//  Move selected option in the left select list to the right
// -------------------------------------------------------------------
function left2right(obj) { 
    var m1 = obj['left_list[]']; 
    var m2 = obj['right_list[]']; 
    var m2len = m2.length; 
    var m1len = m1.length; 
    for ( i=0; i<m1len; i++) { 
        if (m1.options[i].selected == true ) { 
            m2len = m2.length; 
            //m2.options[m2len]=new Option(m1.options[i].value); 
            m2.options[m2len]=new Option(); 
            m2.options[m2len].value = m1.options[i].value;
            m2.options[m2len].text = m1.options[i].text;
            if (m2len > 0) { m2.options[m2len-1].selected = false;}
            m2.options[m2len].selected = true;
        } 
    } 
    for ( i = (m1len-1); i>=0; i--){ 
        if (m1.options[i].selected == true ) { 
            m1.options[i] = null; 
        } 
    } 
} 
// -------------------------------------------------------------------
// right2left()
//  Move selected option in the right select list to the left
// -------------------------------------------------------------------
function right2left(obj) { 
    var m1 = obj['left_list[]'];
    var m2 = obj['right_list[]'];
    var m2len = m2.length; 
    var m1len = m1.length; 
    for ( j=0; j<m2len; j++){ 
        if (m2.options[j].selected == true ) { 
            m1len = m1.length; 
            //m1.options[m1len]=new Option(m2.options[j].value); 
            m1.options[m1len]=new Option(); 
            m1.options[m1len].value = m2.options[j].value;
            m1.options[m1len].text = m2.options[j].text;
            if (m1len > 0) {m1.options[m1len-1].selected = false;}
            m1.options[m1len].selected = true;
        } 
    } 
    for ( j=(m2len-1); j>=0; j--) { 
        if (m2.options[j].selected == true ) { 
            m2.options[j] = null; 
        } 
    } 
}

// -------------------------------------------------------------------
// highlight_right()
//  highlight fields in right_list if empty then notify the user 
// -------------------------------------------------------------------
function highlight_right(obj) { 
    var m2 = obj['right_list[]'];
    var m2len = m2.length ; 
    for (k=0; k<m2.length; k++) {  
      m2.options[k].selected = true;  
    }  
    if (!m2.options[0]) { 
         alert('Please select at least one field.')  
       return false;  
    } else {  
       return true;  
    }  
}

function highlight_all(obj) { 
    var m2 = obj['right_list[]'];
    var m2len = m2.length ; 
    for (k=0; k<m2.length; k++) {  
      m2.options[k].selected = true;  
    }
    
    var n2 = obj['left_list[]'];
    var n2len = n2.length ; 
    for (l=0; l<n2.length; l++) {  
      n2.options[l].selected = true;  
    }
  
    if (!m2.options[0]) { 
         alert('Please select at least one field.')  
       return false;  
    } else {  
       return true;  
    }  
}


// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}
function confirm_download(num_rec) {
    return confirm("Large downloads may take time.  Please be patient.\n"+
                   "If you are having issues downloading try breaking up your search criteria.\n"+
                   "If issues persist please contact the author.\n"+
                   "Are you sure you wish to download "+num_rec+" records?");
}

function Toggle(node) {
    // Unfold the branch if it isn't visible
    if (node.nextSibling.style.display == 'none') {
        // Change the image (if there is an image)
        if (node.childNodes.length > 0) {
            if (node.childNodes.item(0).nodeName == "INPUT") {
                node.childNodes.item(0).value = "Hide Display & Download Options";
            }
        }
        node.nextSibling.style.display = 'block';
    }
    // Collapse the branch if it IS visible
    else {
        // Change the image (if there is an image)
        if (node.childNodes.length > 0) {
            if (node.childNodes.item(0).nodeName == "INPUT") {
                node.childNodes.item(0).value = "Show Display & Download Options";
            }
        }
        node.nextSibling.style.display = 'none';
    }
}
