var selectedLocationCode="",searchLocationCode="",topLocHtml="",secondLocHtml="",thirdLocHtml="",topLocSelectedIndex=0,secondLocSelectedIndex=0,thirdLocSelectedIndex=0;jsonTree="";locationDropDownLevel="1";String.prototype.stripSpaces=function(){return this.replace(/\s/g,"")};
function setLocation(b,c){var d=$j("#ftLocations"),a=$j("#locationCode");0==a.length&&(d.append('<input type="hidden" name="locationCode" id="locationCode" value=""/>'),a=$j("#locationCode"));a.val(b);$j("input[name=locationCode]").val(b);$j.cookie("locationCode",b,{path:"/"});$j.cookie("ftLocLevel"+c,b,{path:"/"})}function getLocation(b){return $j.cookie("ftLocLevel"+b)}
function clearOption(b,c,d,a){a?b.attr("disabled",d).empty().append('<option value="" selected="selected">'+c+"</option>"):b.attr("disabled",d).empty().append('<option value="">'+c+"</option>")}function createOption(b,c,d,a){a?b.append('<option value="'+c+'" selected="selected">'+d+"</option>"):b.append('<option value="'+c+'">'+d+"</option>")}function isLocationSelected(b,c){return b==getLocation(c)?!0:!1}
function populateTree(b){jsonTree=b;var c=$j("#topLoc"),d=$j("#secondLoc"),a=$j("#thirdLoc"),g=getLocation(3);clearOption(d,"No optional regional choices","disabled");clearOption(a,"No optional location choices","disabled");c.change(function(){clearOption(d,"No optional regional choices","disabled");clearOption(a,"No optional location choices","disabled");$j.each(jsonTree.LocationTree.Location,function(b,a){$j.each(a.Location,function(b,a){a.Code==c.val()&&null!=a.Location&&(clearOption(d,"Choose an optional region",
""),$j.each(a.Location,function(b,a){createOption(d,a.Code,a.Name)}))})});setLocation($j(this).val(),1)});d.change(function(){clearOption(a,"No optional location choices","disabled");$j.each(jsonTree.LocationTree.Location,function(b,e){$j.each(e.Location,function(b,h){h.Code==c.val()&&$j.each(h.Location,function(b,c){c.Code==d.val()&&null!=c.Location&&0<c.Location.length&&(clearOption(a,"Choose an optional location",""),$j.each(c.Location,function(b,c){createOption(a,c.Code,c.Name)}))})})});setLocation($j(this).val(),
2)});a.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(b,a){c.append('<optgroup id="topLoc'+b+'" label="'+a.Name+'">');$j.each(a.Location,function(b,a){createOption(c,a.Code,a.Name,isLocationSelected(a.Code,1));null!=a.Location&&c.val()==a.Code&&(clearOption(d,"Choose an optional region",""),$j.each(a.Location,
function(a,b){createOption(d,b.Code,b.Name,isLocationSelected(b.Code,2))}))});c.append("</optgroup>")});switch(locationDropDownLevel){case "2":$j("#countryContainer").hide();"LO"==locationCode.toUpperCase()&&$j.each($j("#secondLoc option"),function(a,b){"LO"==b.value&&(d.attr("selectedIndex",a),d.change())});break;case "3":$j("#countryContainer").hide(),$j("#regionContainer").hide()}if(document.getElementById("searchFormType_locationPicker")&&$j("#searchFormType_locationPicker").attr("checked")){var e=
"",f="";$j("#topLoc option").each(function(a){0==a&&""!=$j(this).val()&&(e=$j(this).val());if($j(this).attr("selected")&&""!=$j(this).val())return f=$j(this).val(),!1});$j("#secondLoc option").each(function(a){0==a&&""!=$j(this).val()&&(e=$j(this).val());if($j(this).attr("selected")&&""!=$j(this).val())return f=$j(this).val(),d.change(),!1});$j("#thirdLoc option").each(function(a){""!=g&&$j(this).val()==g&&$j(this).attr("selected","selected");0==a&&""!=$j(this).val()&&(e=$j(this).val());if($j(this).attr("selected")&&
""!=$j(this).val())return f=$j(this).val(),!1});if(""!=e||""!=f)""!=f?$j("input[name=locationCode]").val(f):""!=e?$j("input[name=locationCode]").val(e):$j("input[name=locationCode]").val("")}}
$j(function(){typeElem=$j("#treeOptions_type");excludeElem=$j("#treeOptions_exclude");InitializeLocationContainers();0<typeElem.length&&0<excludeElem.length&&(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:!0,data:{type:type,exclude:exclude,locationDropDownLevel:locationDropDownLevel},
success:function(b){populateTree(b)}}))});
$j("#searchFormType_search").click(function(){topLocSelectedIndex=$j("#topLoc").attr("selectedIndex");topLocHtml=$j("#countryContainer").html();secondLocSelectedIndex=$j("#secondLoc").attr("selectedIndex");secondLocHtml=$j("#regionContainer").html();thirdLocSelectedIndex=$j("#thirdLoc").attr("selectedIndex");thirdLocHtml=$j("#locationContainer").html();$j("#topLoc").attr("disabled","disabled").empty();$j("#secondLoc").attr("disabled","disabled").empty();$j("#thirdLoc").attr("disabled","disabled").empty();
""!=searchLocationCode&&$j("#locationCode").val(searchLocationCode)});
$j("#searchFormType_locationPicker").click(function(){InitializeLocationContainers();""!=topLocHtml||""!=secondLocHtml||""!=thirdLocHtml?($j("#countryContainer").html(topLocHtml),$j("#regionContainer").html(secondLocHtml),$j("#locationContainer").html(thirdLocHtml)):""!=jsonTree&&populateTree(jsonTree);$j("#topLoc").attr("selectedIndex",topLocSelectedIndex);$j("#secondLoc").attr("selectedIndex",secondLocSelectedIndex);$j("#thirdLoc").attr("selectedIndex",thirdLocSelectedIndex);searchLocationCode=
$j("#locationCode").val();""!=selectedLocationCode&&$j("#locationCode").val(selectedLocationCode)});$j("#topLoc").live("change",function(){topLocSelectedIndex=$j(this).attr("selectedIndex");selectedLocationCode=$j(this).val()});$j("#secondLoc").live("change",function(){secondLocSelectedIndex=$j(this).attr("selectedIndex");selectedLocationCode=$j(this).val()});$j("#thirdLoc").live("change",function(){thirdLocSelectedIndex=$j(this).attr("selectedIndex");selectedLocationCode=$j(this).val()});
function InitializeLocationContainers(){$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>')};
