| 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 | var statusfields = []; |
|---|
| 9 | function dateToUnixEpoch(date){ return (Math.round(1000*date.getTime())); } |
|---|
| 10 | function makeDate(val) { |
|---|
| 11 | var d = null; |
|---|
| 12 | if (val && val.length && val.length>0){ |
|---|
| 13 | try{ |
|---|
| 14 | d = Date.parse(val); |
|---|
| 15 | } |
|---|
| 16 | catch(e){ |
|---|
| 17 | d = invalidDate; |
|---|
| 18 | } |
|---|
| 19 | if(!d || d.toString == invalidDate){ |
|---|
| 20 | alert("You entered an invalid date: "+val); |
|---|
| 21 | return null; |
|---|
| 22 | } |
|---|
| 23 | } |
|---|
| 24 | return d; |
|---|
| 25 | } |
|---|
| 26 | function addBillingField( name /*optional type defaults to "textbox", optional flag status*/ ){ |
|---|
| 27 | var type = arguments.length >= 1 ? arguments[1] : "textbox"; |
|---|
| 28 | var status = arguments.length >= 2 ? arguments[2] : false; |
|---|
| 29 | var getSet = |
|---|
| 30 | (function(){ |
|---|
| 31 | var valueProp = "value"; |
|---|
| 32 | |
|---|
| 33 | if(type == "date"){ |
|---|
| 34 | return function (/*optional value*/){ |
|---|
| 35 | if(arguments.length == 0){ |
|---|
| 36 | var d = makeDate(this.$()[valueProp]); |
|---|
| 37 | if (d) return dateToUnixEpoch(d); |
|---|
| 38 | else return null; |
|---|
| 39 | } |
|---|
| 40 | else{ |
|---|
| 41 | var val = makeDate(arguments[0]); |
|---|
| 42 | if(!val){ |
|---|
| 43 | this.$()[valueProp] = null; |
|---|
| 44 | return null; |
|---|
| 45 | } |
|---|
| 46 | this.$()[valueProp] = val; |
|---|
| 47 | return val; |
|---|
| 48 | } |
|---|
| 49 | }; |
|---|
| 50 | } |
|---|
| 51 | //FOR EVERYTHING ELSE |
|---|
| 52 | if(type == "checkbox"){ |
|---|
| 53 | valueProp = "checked"; |
|---|
| 54 | } |
|---|
| 55 | return function (/*optional value*/){ |
|---|
| 56 | //alert(name+" : "+type+" "+valueProp); |
|---|
| 57 | if(arguments.length == 0){ |
|---|
| 58 | var val = (this.$())[valueProp]; |
|---|
| 59 | |
|---|
| 60 | if(typeof(val) == "string") val = val.trim(); |
|---|
| 61 | if(val)return val; |
|---|
| 62 | return null; |
|---|
| 63 | } |
|---|
| 64 | else{ |
|---|
| 65 | var val = arguments[0]; |
|---|
| 66 | (this.$())[valueProp] = val; |
|---|
| 67 | return val; |
|---|
| 68 | } |
|---|
| 69 | }; |
|---|
| 70 | })(); |
|---|
| 71 | billingfields[name] = { |
|---|
| 72 | "$" : function(){ |
|---|
| 73 | return document.getElementById(name); |
|---|
| 74 | }, |
|---|
| 75 | getval : getSet, |
|---|
| 76 | setval : getSet |
|---|
| 77 | }; |
|---|
| 78 | if (status){ |
|---|
| 79 | statusfields.push({ |
|---|
| 80 | name:name, |
|---|
| 81 | "$" : function(){ |
|---|
| 82 | return document.getElementById(name); |
|---|
| 83 | }, |
|---|
| 84 | getval : getSet, |
|---|
| 85 | setval : getSet |
|---|
| 86 | }); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | addBillingField("billable", "checkbox"); |
|---|
| 91 | addBillingField("unbillable", "checkbox"); |
|---|
| 92 | addBillingField("startdate", "date"); |
|---|
| 93 | addBillingField("startbilling", "dateselect"); |
|---|
| 94 | addBillingField("enddate", "date"); |
|---|
| 95 | addBillingField("endbilling", "dateselect"); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | var linkify = function ( atag, basehref ){ |
|---|
| 99 | var query = ""; |
|---|
| 100 | var haveAdded = false; |
|---|
| 101 | function addToQuery(str){ |
|---|
| 102 | query += haveAdded ? "&" : "?"; |
|---|
| 103 | query += str; |
|---|
| 104 | haveAdded = true; |
|---|
| 105 | } |
|---|
| 106 | //billable logic |
|---|
| 107 | addToQuery(billingfields["billable"].getval() || !(billingfields["unbillable"].getval()) |
|---|
| 108 | ? "BILLABLE=1" : "BILLABLE=0"); |
|---|
| 109 | addToQuery(billingfields["unbillable"].getval() || !(billingfields["billable"].getval()) |
|---|
| 110 | ? "UNBILLABLE=0" : "UNBILLABLE=1"); |
|---|
| 111 | |
|---|
| 112 | for(var i=0, f = null ; f = statusfields[i] ; i++){ |
|---|
| 113 | var val = f.name.toUpperCase().replace("_","", "g").replace(" ","","g")+"="; |
|---|
| 114 | if(f.getval()){ |
|---|
| 115 | val += f.name; |
|---|
| 116 | } |
|---|
| 117 | addToQuery(val); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | //startdate the date in the text box or the date in the dropdown or the first time |
|---|
| 121 | var startdate = billingfields["startdate"].getval() || billingfields["startbilling"].getval() || 0; |
|---|
| 122 | addToQuery("STARTDATE="+startdate); |
|---|
| 123 | //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 |
|---|
| 124 | // this will need a patch to continue working past this point |
|---|
| 125 | var enddate = billingfields["enddate"].getval() || billingfields["endbilling"].getval() || |
|---|
| 126 | 2000000000000000; |
|---|
| 127 | addToQuery("ENDDATE="+enddate); |
|---|
| 128 | |
|---|
| 129 | atag.href = basehref+query; |
|---|
| 130 | }; |
|---|
| 131 | //})() |
|---|