
var linkify = 
(function(){
   String.prototype.trim = 
      (function () {return this.replace(/^\s+/, "").replace(/\s+$/, "");});
   var invalidDate = new Date("invalid").toString()
   var billingfields= {}
   function dateToUnixEpoch(date){
      return Math.round(date.getTime()/1000) - (60 * date.getTimezoneOffset());
   }
   function addBillingField( name /*optional type defaults to "textbox"*/ ){
      var type = arguments.length > 1 ? arguments[1] : "textbox";
      var getSet = 
	 (function(){
	    var valueProp = "value";
	    
	    if(type == "date"){
	       return function (/*optional value*/){
		  if(arguments.length == 0){
		     var d = new Date(this.$()[valueProp]);
		     if(d.toString == invalidDate){
			alert("You entered an invalid "+name);
			return null;
		     }
		     return dateToUnixEpoch(d);
		  }
		  else{
		     var val = new Date(arguments[0]);
		     if(val.toString == invalidDate){
			this.$()[valueProp] = null;
			return null;
		     }
		     this.$()[valueProp] = val;
		     return val;
		  }
	       }
	    }
	    //FOR EVERYTHING ELSE
	    if(type == "checkbox"){
	       valueProp = "checked";
	    }
	    return function (/*optional value*/){
	       //alert(name+" : "+type+" "+valueProp);
	       if(arguments.length == 0){
		  var val = (this.$())[valueProp];
		  
		  if(typeof(val) == "string") val = val.trim();
		  if(val)return val;
		  return null;
	       }
	       else{
		  var val = arguments[0];
		  (this.$())[valueProp] = val;
		  return val
	       }
	    }
	 })()
      billingfields[name] = {
	 "$" : function(){
	    return document.getElementById(name);
	 },
         getval : getSet,
	 setval : getSet
      };
   }

   addBillingField("billable", "checkbox");
   addBillingField("unbillable", "checkbox");
   addBillingField("new", "checkbox");
   addBillingField("assigned", "checkbox");
   addBillingField("reopened", "checkbox");
   addBillingField("closed", "checkbox");
   addBillingField("startdate", "date");
   addBillingField("startbilling", "dateselect");
   addBillingField("enddate", "date");
   addBillingField("endbilling", "dateselect");
   

   return function linkify( atag, basehref ){
      var query = "";
      var haveAdded = false;
      function addToQuery(str){
	 query += haveAdded ? "&" : "?";
	 query += str;
	 haveAdded = true;
      }
      //billable logic
      addToQuery(billingfields["billable"].getval() || !(billingfields["unbillable"].getval())
		 ? "BILLABLE=1" : "BILLABLE=0");
      addToQuery(billingfields["unbillable"].getval() || !(billingfields["billable"].getval())
		 ? "UNBILLABLE=0" : "UNBILLABLE=1");

      addToQuery(billingfields["new"].getval() ? "NEW=new" : "NEW=");
      addToQuery(billingfields["assigned"].getval() ? "ASSIGNED=assigned" : "ASSIGNED=");
      addToQuery(billingfields["reopened"].getval() ? "REOPENED=reopened" : "REOPENED=");
      addToQuery(billingfields["closed"].getval() ? "CLOSED=closed" : "CLOSED=");

      //startdate the date in the text box or the date in the dropdown or the first time
      startdate = billingfields["startdate"].getval() || billingfields["startbilling"].getval() || 0;
      addToQuery("STARTDATE="+startdate);
      //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
      // this will need a patch to continue working  past this point
      enddate = billingfields["enddate"].getval() || billingfields["endbilling"].getval() || 2000000000;
      addToQuery("ENDDATE="+enddate);

      atag.href = basehref+query;
   }
})()
