jsonTree="";locationDropDownLevel="1";String.prototype.stripSpaces=function(){return this.replace(/\s/g,"")};function setLocation(a,c){var b=$j("#ftLocations"),d=$j("#locationCode");if(d.length==0){b.append('<input type="hidden" name="locationCode" id="locationCode" value=""/>');d=$j("#locationCode")}d.val(a);$j.cookie("locationCode",a,{path:"/"});$j.cookie("ftLocLevel"+c,a,{path:"/"})}function getLocation(a){return $j.cookie("ftLocLevel"+a)} function clearOption(a,c,b,d){d?a.attr("disabled",b).empty().append('<option value="" selected="selected">'+c+"</option>"):a.attr("disabled",b).empty().append('<option value="">'+c+"</option>")}function createOption(a,c,b,d){d?a.append('<option value="'+c+'" selected="selected">'+b+"</option>"):a.append('<option value="'+c+'">'+b+"</option>")}function isLocationSelected(a,c){if(a==getLocation(c))return true;return false} function populateTree(a){jsonTree=a;var c=$j("#topLoc"),b=$j("#secondLoc"),d=$j("#thirdLoc");a=getLocation(3);clearOption(b,"No optional regional choices","disabled");clearOption(d,"No optional location choices","disabled");c.change(function(){clearOption(b,"No optional regional choices","disabled");clearOption(d,"No optional location choices","disabled");$j.each(jsonTree.LocationTree.Location,function(h,g){$j.each(g.Location,function(i,e){if(e.Code==c.val()&&e.Location!=null){clearOption(b,"Choose an optional region", "");$j.each(e.Location,function(j,f){createOption(b,f.Code,f.Name)})}})});setLocation($j(this).val(),1)});b.change(function(){clearOption(d,"No optional location choices","disabled");$j.each(jsonTree.LocationTree.Location,function(h,g){$j.each(g.Location,function(i,e){e.Code==c.val()&&$j.each(e.Location,function(j,f){if(f.Code==b.val()&&f.Location!=null)if(f.Location.length>0){clearOption(d,"Choose an optional location","");$j.each(f.Location,function(l,k){createOption(d,k.Code,k.Name)})}})})});setLocation($j(this).val(), 2)});d.change(function(){setLocation($j(this).val(),3)});locationDropDownLevel=$j("#locationDropDownLevel")?$j("#locationDropDownLevel").val():locationDropDownLevel;locationCode=$j("#treeOptions_type").val();$j.each(jsonTree.LocationTree.Location,function(h,g){c.append('<optgroup id="topLoc'+h+'" label="'+g.Name+'">');$j.each(g.Location,function(i,e){createOption(c,e.Code,e.Name,isLocationSelected(e.Code,1));if(e.Location!=null&&c.val()==e.Code){clearOption(b,"Choose an optional region","");$j.each(e.Location, function(j,f){createOption(b,f.Code,f.Name,isLocationSelected(f.Code,2))})}});c.append("</optgroup>")});switch(locationDropDownLevel){case "2":$j("#countryContainer").hide();locationCode.toUpperCase()=="LO"&&$j.each($j("#secondLoc option"),function(h,g){if(g.value=="LO"){b.attr("selectedIndex",h);b.change()}});break;case "3":$j("#countryContainer").hide();$j("#regionContainer").hide();break}b.trigger("change");if(a!=null&&d.children().length>1){d.removeAttr("disabled");d.val(a)}} $j(function(){typeElem=$j("#treeOptions_type");excludeElem=$j("#treeOptions_exclude");$j("#countryContainer").html('<div class="formField"><select class="level-1" id="topLoc" name="countryCode"></select></div>');$j("#regionContainer").html('<div class="formField"><select name="regionCode" class="level-2" id="secondLoc"></select></div>');$j("#locationContainer").html('<div class="formField"><select name="locationPickerCode" class="level-3" id="thirdLoc"></select></div>');if(typeElem.length>0&&excludeElem.length> 0){type=typeElem.val();exclude=excludeElem.val().split(",");ajaxBaseUrl=$j("#ajaxBaseUrl").val();locationDropDownLevel=$j("#locationDropDownLevel")?$j("#locationDropDownLevel").val():locationDropDownLevel;$j.ajax({type:"GET",url:ajaxBaseUrl+"locations/tree/jsonp",dataType:"jsonp",cache:true,data:{type:type,exclude:exclude,locationDropDownLevel:locationDropDownLevel},success:function(a){populateTree(a)}})}});