NDAWN.irrSchedUpdateInProgress=false;NDAWN.irrSchedEmptyMapUnitTab="Map Unit Info";NDAWN.irrSchedEmptyFieldTab="Field Info";NDAWN.irrSchedInit=function(){NDAWN.irrSchedTabView=new YAHOO.widget.TabView("yui-tabs");var a=function(b){NDAWN.irrSchedSetTab(NDAWN.irrSchedTabView.getTabIndex(b.newValue));return true};NDAWN.irrSchedTabView.addListener("activeTabChange",a);if(YAHOO.widget.LogReader){new YAHOO.widget.LogReader()}$.ajax({type:"GET",url:"/irrsched-fields",dataType:"json",async:false,cache:false,error:function(d,c,b){alert("Unable to get initial fields: "+d.status)},success:function(b){NDAWN.irrSchedFieldInfo=b;NDAWN.irrSchedUpdateFields();if($("#preselectfield").val()!=null){$("#field option[value="+$("#preselectfield").val()+"]").attr("selected","selected")}else{$("#field option:first").attr("selected","selected")}}});$.ajax({type:"GET",url:"/cwu-get-crops",dataType:"json",async:false,cache:false,error:function(d,c,b){alert("Unable to get initial crop info: "+d.status)},success:function(b){NDAWN.irrSchedDefaultPltgMonths=b[0];NDAWN.irrSchedDefaultPltgDays=b[1];NDAWN.irrSchedDefaultEmerMonths=b[2];NDAWN.irrSchedDefaultEmerDays=b[3]}});NDAWN.irrSchedFieldChanged();$("#preselectfield").remove();$("#preselectyear").remove()};NDAWN.irrSchedUpdateFields=function(){var e=0;var a=new Array();for(var h in NDAWN.irrSchedFieldInfo){a[e++]=h}a.sort();var d="";for(var c=0;c<e;++c){var g=NDAWN.irrSchedFieldInfo[a[c]];var b=a[c].replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;");d+='<option value="'+g.id+'">'+b+"</option>"}$("#field").html(d)};NDAWN.irrSchedSetTab=function(a){if(a==0){$("#data").css("display","")}else{$("#data").css("display","none");if(a==1){NDAWN.irrSchedLoadFieldDetails()}else{NDAWN.irrSchedLoadMapUnitDetails()}}};NDAWN.irrSchedLoadTab=function(b,c,a){if($("#"+b).attr("innerHTML")==c){$("#"+b).load(a+"?field="+NDAWN.getSelectedField()["id"])}};NDAWN.irrSchedLoadFieldDetails=function(){NDAWN.irrSchedLoadTab("tabfield",NDAWN.irrSchedEmptyFieldTab,"/irrsched-get-field-details.html")};NDAWN.irrSchedLoadMapUnitDetails=function(){NDAWN.irrSchedLoadTab("tabmu",NDAWN.irrSchedEmptyMapUnitTab,"/irrsched-get-map-unit-details.html")};NDAWN.irrSchedToggleUnusedMapUnits=function(a){if(a.checked){$(".notused").hide()}else{$(".notused").show()}};NDAWN.irrSchedSetTableMessage=function(a){$("#data").hide().html('<p id="loading"><img alt="Busy updating table" src="/activity.gif"/><br/><span id="text">'+a+'</span><br/><img alt="Busy updating table" src="/activity.gif"/></p>').fadeIn("slow")};NDAWN.getSelectedField=function(){var a=$("#field").val();for(var b in NDAWN.irrSchedFieldInfo){if(NDAWN.irrSchedFieldInfo[b]["id"]==a){return NDAWN.irrSchedFieldInfo[b]}}};NDAWN.irrSchedFieldChanged=function(){var e=NDAWN.getSelectedField();var d=0;var c=new Array();for(var f in e.crops){c[d++]=f}c.sort().reverse();var b="";var a=null;for(var d=0;d<c.length;++d){var f=c[d];var h;var g=e.crops[f]["crop"];if(g==null){h=f}else{if(a==null){a=f}h=f+" ("+g+")"}b+='<option value="'+f+'">'+h+"</option>"}$("#year").html(b);if($("#preselectyear").val()!=null){$("#year option[value='"+$("#preselectyear").val()+"']").attr("selected","selected")}else{if(a!=null){$("#year option[value='"+a+"']").attr("selected","selected")}else{$("#year option:first").attr("selected","selected")}}NDAWN.irrSchedUpdateCropImageLink();$("#tabfield").attr("innerHTML",NDAWN.irrSchedEmptyFieldTab);$("#tabmu").attr("innerHTML",NDAWN.irrSchedEmptyMapUnitTab);NDAWN.irrSchedSetTab(NDAWN.irrSchedTabView.get("activeIndex"));NDAWN.irrSchedYearChanged(false)};NDAWN.irrSchedUpdateCropImageLink=function(){$.ajax({type:"GET",url:"/irrsched-get-crop-link",dataType:"text",cache:false,data:[{name:"field",value:NDAWN.getSelectedField()["id"]}],error:function(c,b,a){alert("Unable to retrieve crop link: "+c.status+(b==null?"":": "+b))},success:function(a){$("#tabcropimage").html(a)}})};NDAWN.irrSchedYearChanged=function(b){var a=NDAWN.getSelectedField()["crops"][$("#year").val()]["crop"];if(a==undefined){$("#crop option:contains('- Select crop -')").attr("selected","selected");$("#cropdeletebutton").attr("disabled","disabled");$("#data").html("");NDAWN.irrSchedCropChanged(false)}else{$("#cropdeletebutton").removeAttr("disabled");$("#crop option:contains('"+a+"')").attr("selected","selected");NDAWN.irrSchedCropChanged(false);NDAWN.irrSchedUpdateTable("Getting table...",b)}};NDAWN.irrSchedCropChanged=function(a){var d=$("#year").val();var b=$("#crop").val();var f=$("#year option:selected").text().match(/.*\((.+)\)/);if(f!=null){var e=f[1];if(b=="- Select crop -"){$("#crop option[text='"+e+"']").attr("selected","selected");return}if(a){if(e!=b){if(!confirm("You've already saved \""+e+'" as the crop for '+d+'.\n\nAre you sure you want to change it to "'+b+'"?\n')){$("#crop option[text='"+e+"']").attr("selected","selected");return}}}}$("#pdateyr").html(d);$("#edateyr").html(d);if(b=="- Select crop -"){$("#cropinfo tr:nth-child(3)").show();$("#pmonth").val("").attr("disabled","disabled");$("#pday").val("").attr("disabled","disabled");$("#emonth").val("").attr("disabled","disabled");$("#eday").val("").attr("disabled","disabled");$("#station").attr("disabled","disabled").empty();$("#cropsavebutton").attr("disabled","disabled");return}if(f==null){if(NDAWN.irrSchedDefaultPltgMonths[b]==null){$("#cropinfo tr:nth-child(3)").hide();$("#pmonth").val("");$("#pday").val("")}else{$("#cropinfo tr:nth-child(3)").show();NDAWN.irrSchedSetAndEnable("pmonth",NDAWN.irrSchedDefaultPltgMonths[b]);NDAWN.irrSchedSetAndEnable("pday",NDAWN.irrSchedDefaultPltgDays[b])}NDAWN.irrSchedSetAndEnable("emonth",NDAWN.irrSchedDefaultEmerMonths[b]);NDAWN.irrSchedSetAndEnable("eday",NDAWN.irrSchedDefaultEmerDays[b]);NDAWN.irrSchedUpdateStations();return}var c=NDAWN.getSelectedField()["crops"][d];NDAWN.irrSchedSetAndEnable("emonth",c.emonth);NDAWN.irrSchedSetAndEnable("eday",c.eday);if(NDAWN.irrSchedDefaultPltgMonths[b]==null){$("#cropinfo tr:nth-child(3)").hide();$("#pmonth").val("");$("#pday").val("");NDAWN.irrSchedUpdateStations();return}$("#cropinfo tr:nth-child(3)").show();NDAWN.irrSchedSetAndEnable("pmonth",c.pmonth);NDAWN.irrSchedSetAndEnable("pday",c.pday);NDAWN.irrSchedUpdateStations();if(f[1]==b){$("#cropsavebutton").attr("disabled","disabled")}else{$("#cropsavebutton").removeAttr("disabled")}NDAWN.irrSchedVerifyDates()};NDAWN.irrSchedUpdateStations=function(){$("#station").attr("disabled","disabled").html('<option selected="selected">Loading...</option>').css("color","#f0484c").css("font-weight","bold");$.ajax({type:"GET",url:"/nearest-stations",dataType:"json",async:false,cache:false,data:[{name:"field",value:NDAWN.getSelectedField()["id"]},{name:"year",value:$("#year").val()},{name:"pmonth",value:$("#pmonth").val()},{name:"pday",value:$("#pday").val()},{name:"emonth",value:$("#emonth").val()},{name:"eday",value:$("#eday").val()},{name:"crop",value:$("#crop").val()}],error:function(c,b,a){alert("Unable to retrieve nearest stations: "+c.status+(b==null?"":": "+b))},success:function(h){var c=h.list;var g=h.info;var a=document.getElementById("station");var d="";for(i in c){var b=c[i];d+='<option value="'+g[b]+'">'+b+"</option>"}$("#station").css("color","black").css("font-weight","normal").html(d);if(!NDAWN.irrSchedUpdateInProgress){$("#station").removeAttr("disabled")}var f=NDAWN.getSelectedField()["crops"][$("#year").val()]["sid"];var e=$("#station option[value='"+f+"']");if(e==null){e=$("#station option:first")}e.attr("selected","selected");NDAWN.irrSchedStationChanged()}})};NDAWN.irrSchedStationChanged=function(){NDAWN.irrSchedUpdateSaveButtonState()};NDAWN.irrSchedUpdateSaveButtonState=function(){var a=NDAWN.getSelectedField()["crops"][$("#year").val()];var b=a.crop;if(b==null){$("#cropsavebutton").removeAttr("disabled")}else{if(b!=$("#crop").val()){$("#cropsavebutton").removeAttr("disabled")}else{if(a.pmonth){if($("#pmonth").val()!=a.pmonth||$("#pday").val()!=a.pday){$("#cropsavebutton").removeAttr("disabled");return}}if($("#emonth").val()!=a.emonth||$("#eday").val()!=a.eday){$("#cropsavebutton").removeAttr("disabled")}else{if(a.sid==$("#station option:selected").val()){$("#cropsavebutton").attr("disabled","disabled")}else{$("#cropsavebutton").removeAttr("disabled")}}}}};NDAWN.irrSchedDeleteField=function(){if(confirm("Are you sure you want to remove this field?\n\nAll information you have entered for this field, such as\ncrops, rainfall, irrigation, will also be removed.")){NDAWN.irrSchedSetTableMessage("Removing field...");NDAWN.irrSchedUpdating();$.ajax({type:"POST",url:"/irrsched-delete-field",dataType:"json",cache:false,data:[{name:"field",value:NDAWN.getSelectedField()["id"]}],error:function(c,b,a){alert("Unable to delete field: "+c.status+(b==null?"":": "+b))},success:function(a){NDAWN.irrSchedFieldInfo=a;NDAWN.irrSchedUpdateFields();if(!$("#field:empty")){$("#field option:first").attr("selected","selected")}NDAWN.irrSchedFieldChanged();NDAWN.irrSchedDoneUpdating()}})}};NDAWN.irrSchedDeleteCrop=function(){if(confirm("Are you sure you want to remove this crop?\n\nAll information you have entered for this crop, such as\nrainfall and irrigation, will also be deleted.")){NDAWN.irrSchedSetTableMessage("Removing crop...");NDAWN.irrSchedUpdating();$.ajax({type:"POST",url:"/irrsched-delete-crop",dataType:"json",cache:false,data:[{name:"field",value:NDAWN.getSelectedField()["id"]},{name:"year",value:$("#year").val()}],error:function(c,b,a){alert("Unable to delete crop: status "+c.status)},success:function(a){NDAWN.irrSchedFieldInfo=a;NDAWN.irrSchedDoneUpdating();NDAWN.irrSchedFieldChanged()}})}};NDAWN.irrSchedSaveCrop=function(){$("#cropsavebutton").attr("disabled","disabled");NDAWN.irrSchedSetTableMessage("Saving crop...");NDAWN.irrSchedUpdating();$.ajax({type:"POST",url:"/irrsched-save-crop",dataType:"json",cache:false,data:{field:NDAWN.getSelectedField()["id"],year:$("#year").val(),crop:$("#crop").val(),pmonth:$("#pmonth").val(),pday:$("#pday").val(),emonth:$("#emonth").val(),eday:$("#eday").val(),station:$("#station").val()},error:function(c,b,a){alert("Unable to save crop: "+c.status+(b==null?"":": "+b))},success:function(a){NDAWN.irrSchedFieldInfo=a;$("#year option:selected").text($("#year").val()+" ("+$("#crop").val()+")");NDAWN.irrSchedUpdateTable("Crop saved! Getting table...",false)}})};NDAWN.irrSchedSetAndEnable=function(c,b){var a=document.getElementById(c);a.value=b;if(!NDAWN.irrSchedUpdateInProgress){a.disabled=false}};NDAWN.irrSchedVerifyDates=function(){var j={"border-color":"#ee2d34","border-width":"3px"};var e={"border-color":"","border-width":""};if(NDAWN.irrSchedDefaultPltgMonths[$("#crop").val()]==null){var g=$("#emonth").css(e).removeAttr("disabled").val();var b=$("#eday").css(e).removeAttr("disabled").val();var h=NDAWN.irrSchedCheckMonth(g);if(h<0){$("#emonth").css(j).focus();$("#eday").attr("disabled","disabled");$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}var d=NDAWN.irrSchedCheckDay(b);if(d<0){$("#eday").css(j).focus();$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}return true}var c=$("#pmonth").css(e).removeAttr("disabled").val();var a=$("#pday").css(e).removeAttr("disabled").val();var g=$("#emonth").css(e).removeAttr("disabled").val();var b=$("#eday").css(e).removeAttr("disabled").val();var k=NDAWN.irrSchedCheckMonth(c);if(k<0){$("#pmonth").css(j).focus();$("#pday").attr("disabled","disabled");$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}var h=NDAWN.irrSchedCheckMonth(g);if(h<0){$("#emonth").css(j).focus();$("#eday").attr("disabled","disabled");$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}if(k>h){$("#pmonth").css(j).focus();$("#emonth").css(j);$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}var f=NDAWN.irrSchedCheckDay(a);if(f<0){$("#pday").css(j).focus();$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}var d=NDAWN.irrSchedCheckDay(b);if(d<0){$("#eday").css(j).focus();$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}if(k==h&&f>d){$("#pday").css(j).focus();$("#eday").css(j);$("#station").attr("disabled","disabled");$("#cropdeletebutton").attr("disabled","disabled");$("#cropsavebutton").attr("disabled","disabled");return false}return true};NDAWN.irrSchedDateChanged=function(){if(NDAWN.irrSchedVerifyDates()){NDAWN.irrSchedUpdateStations();NDAWN.irrSchedUpdateSaveButtonState();return true}return false};NDAWN.irrSchedCheckMonth=function(a){if(a.length==0){return -1}if(!NDAWN.irrSchedAllDigits(a)){return -1}var b=parseInt(a);if(b<1||b>12){return -1}return b};NDAWN.irrSchedCheckDay=function(d,e){if(d.length==0){return -1}if(!NDAWN.irrSchedAllDigits(d)){return -1}var b=parseInt(d);var a=[31,28,31,30,31,30,31,31,30,31,30,31][e-1];if(e==2){var c=$("#year").val();if(!(c%4)&&c%100&&!(c%400)){a=29}}if(b<1||b>a){return -1}return b};NDAWN.irrSchedAllDigits=function(b){for(var a=0;a<b.length;++a){if(isNaN(parseInt(b.charAt(a)))){return false}}return true};NDAWN.irrSchedUpdateTable=function(b,a){if(NDAWN.irrSchedTabView.get("activeIndex")==0){NDAWN.irrSchedSetTableMessage(b)}NDAWN.irrSchedUpdating();$.ajax({type:"GET",url:"/irrsched-get-data",dataType:"html",cache:false,data:{field:NDAWN.getSelectedField()["id"],year:$("#year").val()},error:function(f,e,d){var c="";if(f!=null){if(f.status==400||f.status==500){c=f.responseText}else{c="Status "+f.status+": Unknown error."}}else{if(d!=null){c="Exception: "+d+"."}else{c="Unknown error."}}$("#data").html('<span id="error">'+c+"</span>")},success:function(c){$("#data").html(c)},complete:function(d,c){NDAWN.irrSchedDoneUpdating();if(a){$("#cropsavebutton").removeAttr("disabled")}else{$("#cropsavebutton").attr("disabled","disabled")}}})};NDAWN.irrSchedDoneUpdating=function(){NDAWN.irrSchedUpdateInProgress=false;$(".disablewhenupdating").removeAttr("disabled")};NDAWN.irrSchedUpdating=function(){NDAWN.irrSchedUpdateInProgress=true;$(".disablewhenupdating").attr("disabled","disabled")};