[795] | 1 | /* |
---|
| 2 | * Add selected item of selectbox "from" to "to" and |
---|
| 3 | * disable it in "from" |
---|
| 4 | */ |
---|
| 5 | function addOption(from, to, sort) { |
---|
| 6 | fromList = eval('document.forms[0].' + from); |
---|
| 7 | toList = eval('document.forms[0].' + to); |
---|
| 8 | button = eval('document.forms[0].btn_' + from); |
---|
| 9 | |
---|
| 10 | if(fromList.selectedIndex != -1 && !fromList.options[fromList.selectedIndex].disabled){ |
---|
| 11 | txt = new String(fromList.options[fromList.selectedIndex].text); |
---|
| 12 | val = fromList.options[fromList.selectedIndex].value; |
---|
| 13 | |
---|
| 14 | toList.options[toList.length] = new Option(txt,val); |
---|
| 15 | fromList.options[fromList.selectedIndex].disabled = true; |
---|
| 16 | // if is IE, emulates disabled |
---|
| 17 | if(navigator.appName == "Microsoft Internet Explorer") |
---|
| 18 | fromList.options[fromList.selectedIndex].style.color = "gray"; |
---|
| 19 | fromList.selectedIndex = -1; |
---|
| 20 | $(button).disable(); |
---|
| 21 | |
---|
| 22 | if(sort){ |
---|
| 23 | // sort selectbox "to" |
---|
| 24 | sortOptions(to); |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | /* |
---|
| 30 | * Remove selected options of selectbox "from" and enable them in selectbox "to" |
---|
| 31 | */ |
---|
| 32 | function removeOptions(from, to) { |
---|
| 33 | fromList = eval('document.forms[0].' + from); |
---|
| 34 | toList = eval('document.forms[0].' + to); |
---|
| 35 | // get color style of the selected option of fromList |
---|
| 36 | var color = fromList.options[fromList.selectedIndex].style.color; |
---|
| 37 | // walk through "from" list searching the selected items, |
---|
| 38 | // enable them in the "to" and remove from "from" |
---|
| 39 | for (i = 0; i < fromList.options.length; i++) { |
---|
| 40 | var current = fromList.options[i]; |
---|
| 41 | if (current.selected) { |
---|
| 42 | val = current.value; |
---|
| 43 | for (j = 0; j < toList.options.length; j++){ |
---|
| 44 | if (toList.options[j].value == val){ |
---|
| 45 | toList.options[j].disabled = false; |
---|
| 46 | // if is IE, emulates enabled |
---|
| 47 | if(navigator.appName == "Microsoft Internet Explorer") |
---|
| 48 | toList.options[j].style.color = color; |
---|
| 49 | break; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | fromList.options[i] = null; |
---|
| 53 | i--; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | button = eval('document.forms[0].btn_' + from); |
---|
| 57 | $(button).disable(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /* |
---|
| 61 | * Check if there is a selected item in the selectbox objName and enable or disable its related button |
---|
| 62 | */ |
---|
| 63 | function enableButton(objName) |
---|
| 64 | { |
---|
| 65 | select = eval('document.forms[0].' + objName); |
---|
| 66 | var buttonName = 'btn_' + objName; |
---|
| 67 | button = eval('document.forms[0].' + buttonName); |
---|
| 68 | if (select.selectedIndex != -1 && select.options[select.selectedIndex].value != -1){ |
---|
| 69 | $(button).enable(); |
---|
| 70 | } else { |
---|
| 71 | $(button).disable(); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /* |
---|
| 77 | * Populate selectbox with the array elements |
---|
| 78 | */ |
---|
| 79 | function populateSelect(objName, array) { |
---|
| 80 | obj = eval('document.forms[0].' + objName); |
---|
| 81 | |
---|
| 82 | while(obj.firstChild) |
---|
| 83 | obj.removeChild(obj.firstChild); |
---|
| 84 | |
---|
| 85 | // walk through array and add elements to the selectbox |
---|
| 86 | for (i = 0; i < array.length; i++) { |
---|
| 87 | if( isArray(array[i]) ) { |
---|
| 88 | obj.options[obj.length] = new Option(array[i].text, array[i].value); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /* |
---|
| 94 | * Populate the selectboxes. |
---|
| 95 | * @param objNameT Top selectbox name |
---|
| 96 | * @param arrayT Array with the elements of the top selectbox |
---|
| 97 | * @param sortT Boolean value to sort the top selectbox |
---|
| 98 | * @param objNameB Bottom selectbox name |
---|
| 99 | * @param arrayB Array with the elements of the bottom selectbox |
---|
| 100 | * @param sortB Boolean value to sort the bottom selectbox |
---|
| 101 | * @param bool execDiff This value indicates if the user wants to execute the diff function |
---|
| 102 | */ |
---|
| 103 | function populateSelects(objNameT, arrayT, sortT, |
---|
| 104 | objNameB, arrayB, sortB, |
---|
| 105 | execDiff) { |
---|
| 106 | if( isArray(arrayT) ) |
---|
| 107 | populateSelect(objNameT, arrayT); |
---|
| 108 | |
---|
| 109 | if( isArray(arrayB) ) |
---|
| 110 | populateSelect(objNameB, arrayB); |
---|
| 111 | |
---|
| 112 | if( sortT ) |
---|
| 113 | sortOptions(objNameT); |
---|
| 114 | |
---|
| 115 | if( sortB ) |
---|
| 116 | sortOptions(objNameB); |
---|
| 117 | |
---|
| 118 | if(execDiff) |
---|
| 119 | selectDiffMultiple( objNameB, objNameT ) |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /* |
---|
| 123 | * Select all items in a selectbox. |
---|
| 124 | */ |
---|
| 125 | function selectAllOptions( objName ) { |
---|
| 126 | obj = eval('document.forms[0].' + objName); |
---|
| 127 | |
---|
| 128 | for(i = 0; i < obj.length; i++) { |
---|
| 129 | obj.options[i].selected = true; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /* |
---|
| 134 | * Check if the parameter is an array. |
---|
| 135 | * Return true if it's an array, and false, if it's not. |
---|
| 136 | */ |
---|
| 137 | function isArray( obj ) { |
---|
| 138 | if (typeof obj == 'object') { |
---|
| 139 | var criterion = obj.constructor.toString().match(/array/i); |
---|
| 140 | return (criterion != null); |
---|
| 141 | } |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* |
---|
| 146 | * Get the values of a selectbox and return them in an array. |
---|
| 147 | */ |
---|
| 148 | function select2array(objName) { |
---|
| 149 | obj = eval('document.forms[0].' + objName); |
---|
| 150 | |
---|
| 151 | if(obj){ |
---|
| 152 | array = new Array(); |
---|
| 153 | |
---|
| 154 | for(i = 0; i < obj.length; i++) { |
---|
| 155 | array[i] = new Array(2); |
---|
| 156 | array[i].text = obj.options[i].text; |
---|
| 157 | array[i].value = obj.options[i].value; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | return array; |
---|
| 161 | } |
---|
| 162 | return null; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /* |
---|
| 166 | * Copy the values of an array to a selectbox. |
---|
| 167 | */ |
---|
| 168 | function array2select(array, objName) { |
---|
| 169 | obj = eval('document.forms[0].' + objName); |
---|
| 170 | |
---|
| 171 | if( isArray(array) ) { |
---|
| 172 | for(i = 0; i < obj.length; i++) { |
---|
| 173 | obj.options[i].text = array[i].text; |
---|
| 174 | obj.options[i].value = array[i].value; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /* |
---|
| 180 | * Redefine the javascript sort function. |
---|
| 181 | * We need to compare the attributes a.text and b.text. |
---|
| 182 | */ |
---|
| 183 | function sortFunction(a, b) { |
---|
| 184 | if(a.text > b.text) |
---|
| 185 | return 1; |
---|
| 186 | if(a.text < b.text) |
---|
| 187 | return -1; |
---|
| 188 | return 0; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /* |
---|
| 192 | * Sort the elements of a selectbox. |
---|
| 193 | */ |
---|
| 194 | function sortOptions(objName) { |
---|
| 195 | arrTexts = select2array(objName); |
---|
| 196 | arrTexts.sort(sortFunction); |
---|
| 197 | array2select(arrTexts, objName); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /* |
---|
| 201 | * Difference between two selectboxes. Remove the elements of the objName2 |
---|
| 202 | * that are found in objName1. |
---|
| 203 | */ |
---|
| 204 | function selectDiffMultiple(objName1, objName2) { |
---|
| 205 | array1 = select2array(objName1); |
---|
| 206 | array2 = select2array(objName2); |
---|
| 207 | obj2 = eval('document.forms[0].' + objName2); |
---|
| 208 | |
---|
| 209 | for( var i = 0; i < array1.length; i++ ) { |
---|
| 210 | for(var j = 0; j < array2.length; j++) { |
---|
| 211 | if(array1[i].value == array2[j].value){ |
---|
| 212 | obj2.options[j].disabled = true; |
---|
| 213 | // if is IE, emulates disabled |
---|
| 214 | if(navigator.appName == "Microsoft Internet Explorer") |
---|
| 215 | obj2.options[j].style.color = "gray"; |
---|
| 216 | break; |
---|
| 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|