var jsonTree = ""; var locationDropDownLevel = "1"; function clearOption(divName, text, disabled, selected) { if (!selected) $j("#" + divName).attr("disabled", disabled).empty().append("<option value=\"\">" + text + "</option>"); else $j("#" + divName).attr("disabled", disabled).empty().append("<option value=\"\" selected=\"selected\">" + text + "</option>") } function createOption(selectName, value, text, selected) { if (!selected) $j("#" + selectName).append("<option value=\"" + value + "\">" + text + "</option>"); else $j("#" + selectName).append("<option value=\"" + value + "\" selected=\"selected\">" + text + "</option>") } function trim(text) { return text } String.prototype.stripSpaces = function() { return this.replace(/\s/g, "") }; function populateTree(data) { jsonTree = data; locationDropDownLevel = $j("#locationDropDownLevel") ? $j("#locationDropDownLevel").val() : locationDropDownLevel; locationCode = $j("#treeOptions_type").val(); $j.each(jsonTree.LocationTree.Location, function(i, item) { $j("#topLoc").append("<optgroup id=\"" + "topLoc" + i + "\" label=\"" + trim(item.Name) + "\">"); $j.each(item.Location, function(j, topLoc) { $j("#topLoc" + i).append("<option value=\"" + topLoc.Code + "\">" + trim(topLoc.Name) + "</option>"); if (topLoc.Location != null && $j("#topLoc").val() == topLoc.Code) { if (topLoc.Location.length > 0) { clearOption("secondLoc", trim("Choose an optional region"), ""); $j.each(topLoc.Location, function(k, secondLoc) { createOption("secondLoc", secondLoc.Code, trim(secondLoc.Name)) }) } } }); $j("#topLoc").append("</optgroup>") }); switch (locationDropDownLevel) { case "1": { $j.each($j("#topLoc option"), function(idx, item) { if (item.value == "ENG") { $j("#topLoc").attr("selectedIndex", idx); $j("#topLoc").change() } }); break } case "2": { $j("#countryContainer").hide(); if (locationCode.toUpperCase() == "LO") { $j.each($j("#secondLoc option"), function(idx, item) { if (item.value == "LO") { $j("#secondLoc").attr("selectedIndex", idx); $j("#secondLoc").change() } }) } break } case "3": { $j("#countryContainer").hide(); $j("#regionContainer").hide(); break } default: { break } } } $j(function() { var type = $j("#treeOptions_type"); if (type.length > 0) { type = type.val(); var exclude = $j("#treeOptions_exclude").val().split(","); var ajaxBaseUrl = $j("#ajaxBaseUrl").val(); var locationDropDownLevel = $j("#locationDropDownLevel") ? $j("#locationDropDownLevel").val() : locationDropDownLevel; clearOption("secondLoc", trim("No optional regional choices"), "disabled"); clearOption("thirdLoc", trim("No optional location choices"), "disabled"); $j.ajax({ type: "GET", url: ajaxBaseUrl + "locations/tree/jsonp", dataType: "jsonp", cache: true, data: { type: type, exclude: exclude, locationDropDownLevel: locationDropDownLevel }, success: function(data) { populateTree(data) } }); $j("#topLoc").change(function() { clearOption("secondLoc", trim("No optional regional choices"), "disabled"); clearOption("thirdLoc", trim("No optional location choices"), "disabled"); $j.each(jsonTree.LocationTree.Location, function(i, item) { $j.each(item.Location, function(j, topLoc) { if (topLoc.Code == $j("#topLoc").val() && topLoc.Location != null) { if (topLoc.Location.length > 0) { clearOption("secondLoc", trim("Choose an optional region"), ""); $j.each(topLoc.Location, function(k, secondLoc) { createOption("secondLoc", secondLoc.Code, trim(secondLoc.Name)) }) } } }) }) }); $j("#secondLoc").change(function() { clearOption("thirdLoc", trim("No optional location choices"), "disabled"); $j.each(jsonTree.LocationTree.Location, function(i, item) { $j.each(item.Location, function(j, topLoc) { if (topLoc.Code == $j("#topLoc").val()) { $j.each(topLoc.Location, function(k, secondLoc) { if (secondLoc.Code == $j("#secondLoc").val() && secondLoc.Location != null) { if (secondLoc.Location.length > 0) { clearOption("thirdLoc", trim("Choose an optional location"), ""); $j.each(secondLoc.Location, function(l, thirdLoc) { createOption("thirdLoc", thirdLoc.Code, trim(thirdLoc.Name)) }) } } }) } }) }) }) } });