| 1 | |
|---|
| 2 | var linkify = |
|---|
| 3 | (function(){ |
|---|
| 4 | String.prototype.trim = |
|---|
| 5 | (function () {return this.replace(/^\s+/, "").replace(/\s+$/, "");}); |
|---|
| 6 | var invalidDate = new Date("invalid").toString() |
|---|
| 7 | var billingfields= {} |
|---|
| 8 | function dateToUnixEpoch(date){ |
|---|
| 9 | return Math.round(date.getTime()/1000) - (60 * date.getTimezoneOffset()); |
|---|
| 10 | } |
|---|
| 11 | function addBillingField( name /*optional type defaults to "textbox"*/ ){ |
|---|
| 12 | var type = arguments.length > 1 ? arguments[1] : "textbox"; |
|---|
| 13 | var getSet = |
|---|
| 14 | (function(){ |
|---|
| 15 | var valueProp = "value"; |
|---|
| 16 | |
|---|
| 17 | if(type == "date"){ |
|---|
| 18 | return function (/*optional value*/){ |
|---|
| 19 | if(arguments.length == 0){ |
|---|
| 20 | var d = new Date(this.$()[valueProp]); |
|---|
| 21 | if(d.toString == invalidDate){ |
|---|
| 22 | alert("You entered an invalid "+name); |
|---|
| 23 | return null; |
|---|
| 24 | } |
|---|
| 25 | return dateToUnixEpoch(d); |
|---|
| 26 | } |
|---|
| 27 | else{ |
|---|
| 28 | var val = new Date(arguments[0]); |
|---|
| 29 | if(val.toString == invalidDate){ |
|---|
| 30 | this.$()[valueProp] = null; |
|---|
| 31 | return null; |
|---|
| 32 | } |
|---|
| 33 | this.$()[valueProp] = val; |
|---|
| 34 | return val; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | //FOR EVERYTHING ELSE |
|---|
| 39 | if(type == "checkbox"){ |
|---|
| 40 | valueProp = "checked"; |
|---|
| 41 | } |
|---|
| 42 | return function (/*optional value*/){ |
|---|
| 43 | //alert(name+" : "+type+" "+valueProp); |
|---|
| 44 | if(arguments.length == 0){ |
|---|
| 45 | var val = (this.$())[valueProp]; |
|---|
| 46 | |
|---|
| 47 | if(typeof(val) == "string") val = val.trim(); |
|---|
| 48 | if(val)return val; |
|---|
| 49 | return null; |
|---|
| 50 | } |
|---|
| 51 | else{ |
|---|
| 52 | var val = arguments[0]; |
|---|
| 53 | (this.$())[valueProp] = val; |
|---|
| 54 | return val |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | })() |
|---|
| 58 | billingfields[name] = { |
|---|
| 59 | "$" : function(){ |
|---|
| 60 | return document.getElementById(name); |
|---|
| 61 | }, |
|---|
| 62 | getval : getSet, |
|---|
| 63 | setval : getSet |
|---|
| 64 | }; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | addBillingField("billable", "checkbox"); |
|---|
| 68 | addBillingField("unbillable", "checkbox"); |
|---|
| 69 | addBillingField("new", "checkbox"); |
|---|
| 70 | addBillingField("assigned", "checkbox"); |
|---|
| 71 | addBillingField("reopened", "checkbox"); |
|---|
| 72 | addBillingField("closed", "checkbox"); |
|---|
| 73 | addBillingField("startdate", "date"); |
|---|
| 74 | addBillingField("startbilling", "dateselect"); |
|---|
| 75 | addBillingField("enddate", "date"); |
|---|
| 76 | addBillingField("endbilling", "dateselect"); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | return function ( atag, basehref ){ |
|---|
| 80 | var query = ""; |
|---|
| 81 | var haveAdded = false; |
|---|
| 82 | function addToQuery(str){ |
|---|
| 83 | query += haveAdded ? "&" : "?"; |
|---|
| 84 | query += str; |
|---|
| 85 | haveAdded = true; |
|---|
| 86 | } |
|---|
| 87 | //billable logic |
|---|
| 88 | addToQuery(billingfields["billable"].getval() || !(billingfields["unbillable"].getval()) |
|---|
| 89 | ? "BILLABLE=1" : "BILLABLE=0"); |
|---|
| 90 | addToQuery(billingfields["unbillable"].getval() || !(billingfields["billable"].getval()) |
|---|
| 91 | ? "UNBILLABLE=0" : "UNBILLABLE=1"); |
|---|
| 92 | |
|---|
| 93 | addToQuery(billingfields["new"].getval() ? "NEW=new" : "NEW="); |
|---|
| 94 | addToQuery(billingfields["assigned"].getval() ? "ASSIGNED=assigned" : "ASSIGNED="); |
|---|
| 95 | addToQuery(billingfields["reopened"].getval() ? "REOPENED=reopened" : "REOPENED="); |
|---|
| 96 | addToQuery(billingfields["closed"].getval() ? "CLOSED=closed" : "CLOSED="); |
|---|
| 97 | |
|---|
| 98 | //startdate the date in the text box or the date in the dropdown or the first time |
|---|
| 99 | startdate = billingfields["startdate"].getval() || billingfields["startbilling"].getval() || 0; |
|---|
| 100 | addToQuery("STARTDATE="+startdate); |
|---|
| 101 | //the date in the enddate text box or the date in the enddate billing box or real close to the end of integer unix epoch time |
|---|
| 102 | // this will need a patch to continue working past this point |
|---|
| 103 | enddate = billingfields["enddate"].getval() || billingfields["endbilling"].getval() || 2000000000; |
|---|
| 104 | addToQuery("ENDDATE="+enddate); |
|---|
| 105 | |
|---|
| 106 | atag.href = basehref+query; |
|---|
| 107 | } |
|---|
| 108 | })() |
|---|