function report_controller()
{
	this.getDatePickerFormatStr = function(datefmtStr){
		//http://docs.jquery.com/UI/Datepicker/formatDate
		return datefmtStr.replace("MMM", "M").replace("MM", "mm").replace("yy", "y").replace("yy", "y")
	}

	this.getDateTimePickerFormatStr = function(dateTimefmtStr){
		//need to revisit,since there is no proper timepicker plugin for Jquery UI datepicker
		//http://jsfiddle.net/hLTcB/8/
		return this.getDatePickerFormatStr(dateTimefmtStr).replace(" hh:mm a", "").replace(" HH:mm", "")
	}

	this.callback = function()
	{
		var controller = this;
		$('input[data-type=date]').datepicker({
            changeYear: true,
         	changeMonth: true,
            yearRange: '-60:+20',
            buttons: {
			    'Clear': function (sel) {
			        $.datepicker._clearDate(sel);
			    }
			},
            beforeShow: function(input, inst){
            	var currentWidget = $(input)
            	var datefmtStr = currentWidget.attr('data-format')
            	inst.settings["dateFormat"] = controller.getDatePickerFormatStr(datefmtStr)
            },
			onSelect: function(dateText, inst) {
				var currentWidget = $(this)
				var selectedDate = currentWidget.datepicker('getDate')
				var dateval = ""
				if(selectedDate){
		   			dateval = getExcelDateValue(selectedDate.getTime())
		   			//Math.ceil is specific to UB, getting 40806.958333333333 instead of 40807
		   			dateval=Math.ceil(dateval)
	   			}
			  var valueList = {}
			  valueList[dateval] = dateText
			  controller.setReportFilter(currentWidget.attr('name'),valueList)
			}
		});

		$('input[data-type=datetime]').datetimepicker({
     		changeYear: true,
         	changeMonth: true,
            yearRange: '-60:+20',
			buttons: {
			    'Clear': function (sel) {
			        $.datepicker._clearDate(sel);
			    }
			},
            beforeShow: function(input, inst){
            	var currentWidget = $(input)
            	var datefmtStr = currentWidget.attr('data-format')
            	inst.settings["dateFormat"] = controller.getDateTimePickerFormatStr(datefmtStr)
            	if(datefmtStr.indexOf('a') == -1)
            		inst.settings["clockType"] = 24
            },
         	onSelect: function( dateText, inst ) {
				var currentWidget = $(this)
				var selectedDate = currentWidget.datetimepicker('getDate')
				var dateval = ""
				if(selectedDate)
				{
					dateval = getExcelDateValue(selectedDate.getTime())
		   		}
		   	  var valueList = {}
		   	  valueList[dateText] = dateval
			  controller.setReportFilter(currentWidget.attr('name'),valueList)
			}
       	});
	}

	this.filter = function()
	{
		var controller = this;
		var options = new Array();
		var invokeFilterJson = {};
		var filterjsonval = this.getReportFilter();
		for(var name in filterjsonval)
		{
			if(filterjsonval[name] instanceof Object)
			{
				for(var key in filterjsonval[name])
					invokeFilterJson[name] = key;
			}
			else
			{
				invokeFilterJson[name] = filterjsonval[name];
			}
		}
		options["data"] =  invokeFilterJson;
		options["success"] =  function(list, dataObject, result)
		{
			if(result.root)
			{
				controller.graph = dataObject
			}else
			{
				//for aggreate report we need to revisit the filter api,
				//as of now filter api is only applicable for list report and inbox
				controller.graph = result
			}
			controller.render()
		}
		$(this.graph).osInvoke(this.id, 'filter', options);
	}

	this.setReportFilter = function(name, value)
	{
		this.filterJson[name] = value;
	}

	this.getReportFilter = function(name)
	{
		if(name)
		{
			var value =  this.filterJson[name];
			if(value instanceof Object)
			{
				for(var id in value)
					return value[id];
			}
			return value;
		}
		return this.filterJson;
	}

	this.masterLookup = function(elem,masterLookupUrl)
    {
		var currentElem = $(elem)
		var searchUrl = masterLookupUrl+"?mimetype=application/json";
        var controller = this;
        $.ajax({type:'GET', url:searchUrl, dataType: "json", async:true,
        success: function(data)
        {
            var searchData = data[data['root']]
            var mapList = []
            mapList.push({"label":"None","idval":""})
            for(key in searchData)
            {
                if(key.indexOf('ModifiedBy.') > -1)
                {
                    for(var item in searchData[key])
                    {
                        mapList.push({"label":searchData[key][item], "idval": item})
                    }
                    break;
                }
            }

            currentElem.autocomplete({
                source:mapList,
                minLength:0,
				select:function(event, ui){
	       				var currentElem = $(event.target)
	       				var valueList = {}
	       				valueList[ui.item.idval] = ui.item.label
				   		controller.setReportFilter(currentElem.attr('name'), valueList)
	       			}
            })
            currentElem.autocomplete("search","")
            currentElem.focus()
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            if(show)
            {
                 if(XMLHttpRequest && XMLHttpRequest.status==401){
                    alert("User session timed out. Please login again to continue...");
                    pageUrlPath = window.location.href
                    if(pageUrlPath)
                        location.href=pageUrlPath;
                 }else
                    jAlert('Error Failed to complete the search','Error',null,textStatus+"/"+errorThrown);
             }
        }
        });
   }

}
report_controller.prototype = new runtime_controller;
