function YMMSearchHelper(ContextHandler, Type)
{
    this.AjaxRoot       = "/shared/php/ajax";
    this.ContextHandler = ContextHandler;    
    this.Type           = Type;
    
    this.YMMSearchSetYear = function(YearObj)
    {     
        var Year            = YearObj.ExtraData ? YearObj.ExtraData : YearObj.options[YearObj.selectedIndex].value;
        var Parameters      = "event=ActionYMMSearchSetYear&YMMSearchYear="+Year+"&YMMSearchType="+this.Type;
        var objAjaxv2       = new Ajaxv2(this.AjaxRoot+"/ymm_search_helper.php", this, this.YMMSearchSetYearCallBack, "POST", Parameters, this.ContextHandler);
        objAjaxv2.EventData = null;
        objAjaxv2.GetPost(true);
    }
    
    this.YMMSearchSetYearCallBack = function(Success,Response,EventData)
    {
        this.GenericCallBack(Success, Response, EventData);        
    }
    
    this.YMMSearchSetMake=function(YearObj, MakeObj)
    {
        //kind of hacky way to allow for us to reset the model box with the css controls
        if (YearObj == null || MakeObj == null)
        {
            var Year = 0;
            var Make = 0;
        }
        else
        {
            var Year            = YearObj.ExtraData ? YearObj.ExtraData : YearObj.options[YearObj.selectedIndex].value;
            var Make            = MakeObj.ExtraData ? MakeObj.ExtraData : MakeObj.options[MakeObj.selectedIndex].value;        
        }
        
        var Parameters      = "event=ActionYMMSearchSetMake&YMMSearchYear="+Year+"&YMMSearchMakeID="+Make+"&YMMSearchType="+this.Type;
        var objAjaxv2       = new Ajaxv2(this.AjaxRoot+"/ymm_search_helper.php", this, this.YMMSearchSetMakeCallBack, "POST", Parameters, this.ContextHandler);
        objAjaxv2.EventData = null
        objAjaxv2.GetPost(true);
    }
      
    this.YMMSearchSetMakeCallBack = function(Success,Response,EventData)
    {
        this.GenericCallBack(Success,Response,EventData);        
    }
      
    this.YMMSearchSetModel=function(YearObj, MakeObj, ModelIDObj)
    {
        var Year            = YearObj.ExtraData ? YearObj.ExtraData : YearObj.options[YearObj.selectedIndex].value;
        var Make            = MakeObj.ExtraData ? MakeObj.ExtraData : MakeObj.options[MakeObj.selectedIndex].value;
        var ModelID         = ModelIDObj.ExtraData ? ModelIDObj.ExtraData : ModelIDObj.options[ModelIDObj.selectedIndex].value;        
        var Parameters      = "&event=ActionYMMSearchSetModel&YMMSearchYear="+Year+"&YMMSearchMakeID="+Make+"&YMMSearchModelID="+ModelID+"&YMMSearchType="+this.Type;
        var objAjaxv2       = new Ajaxv2(this.AjaxRoot+"/ymm_search_helper.php", this, this.YMMSearchSetModelCallBack, "POST", Parameters, this.ContextHandler);
        objAjaxv2.EventData = null;
        objAjaxv2.GetPost(true);
    }
    
    this.YMMSearchSetModelCallBack = function(Success,Response,EventData)
    {
        this.GenericCallBack(Success,Response,EventData);        
    }

    this.GenericCallBack = function(Success, ResponseObject, EventData)
    {
        if(Success)
        {
            var element;
            var event_array = Array();
            try
            {
                eval("var obj= "+ResponseObject.responseText);
                this.LastResponse=obj;
                if(EventData != null)
                {
                    if(EventData.constructor != Array)
                        event_array.push(EventData);
                    else
                        event_array = EventData;
                        
                    for(var c=0; c<event_array.length; c++)
                    {
                        element = document.getElementById(event_array[c]);
                        if(obj.Response != "")
                            element.innerHTML = obj.Response;
                    }
                }                
                
                if(obj.Javascript != null)            
                    window.eval(obj.Javascript);
            }
            catch(ex)
            {
                alert('Sorry an error has occured '+ex.toString());    
            }                
        }
    }
    
    this.ResetDropDown=function(ControlObj)
    {
        if(ControlObj != null)
        {
            ControlObj.selectedIndex=0;
            for(var c=ControlObj.length;c>0;c--)
            {
                ControlObj.remove(c);
            }
            //ControlObj.options[0] = new Option(this.DefaultValues[ControlID],'',true);            
            ControlObj.disabled=true;
            ControlObj.className='disabled_control';            
        }
    }
    
    this.ResetCSSDropDown=function(ControlObj)
    {
        if (ControlObj != null)
        {
            ControlObj.SelectedIndex=0;
            ControlObj.Length=1;
            for(var c=1;c<ControlObj.Length;c++)
            {
                ControlObj.Options.pop();
            }
            ControlObj.Visible=false;
            ControlObj.DisableHoverSelect=false;
            ControlObj.KeyStartTime=null;
            ControlObj.SearchString='';
            ControlObj.MouseX=0;
            ControlObj.MouseY=0;
            ControlObj.OnChangeEventHandler   = function(obj){};
            ControlObj.OnKeyDownEventHandler  = function(e){}
            ControlObj.OnMouseMoveEventHandler= function(e){}
            
            document.getElementById(ControlObj.ID+"_dropdown").className = 'css_select_disabled';
        }
    }
    
    this.DisableElement = function(ControlID)
    {
        if(document.getElementById(ControlID) != null)
        {
            document.getElementById(ControlID).disabled = true;
        }
    }
    
    this.EnableElement = function(ControlID)
    {
        if(document.getElementById(ControlID) != null)
        {
            document.getElementById(ControlID).disabled = false;
        }
    }
    
    this.ToggleDisabled = function(ControlID)
    {
        if(document.getElementById(ControlID) != null)
        {
            document.getElementById(ControlID).disabled = !(document.getElementById(ControlID).disabled);
        }
    }
    
    this.UpdateInput = function(ControlID, Value)
    {
        if(document.getElementById(ControlID) != null)
        {
            document.getElementById(ControlID).value = Value;
        }
    }
    
    this.SubmitForm = function(FormID, ModelID)
    {
        if (document.getElementById(ModelID) != null && document.getElementById(ModelID).value != '' && document.getElementById(FormID) != null)
        {
            document.getElementById(FormID).submit();
        }
    }
    
    this.ToggleDropdowns = function(ShowDropdowns)
    {
        DivDropdowns= document.getElementById("YMMSearchDropdowns");
        DivSelected = document.getElementById("YMMSearchSelected");
        DivHidden   = ShowDropdowns === true ? DivSelected : DivDropdowns;
        DivVisible  = DivHidden == DivSelected ? DivDropdowns : DivSelected;

        DivVisible.style.display = "block";
        DivVisible.style.visibility = "visible";
        DivHidden.style.display = "none";
        DivHidden.style.visibility = "hidden";
    }
    
    this.Reset = function()
    {
        var Parameters      = "event=ActionYMMSearchReset&YMMSearchType="+this.Type;
        var objAjaxv2       = new Ajaxv2(this.AjaxRoot+"/ymm_search_helper.php", this, this.YMMSearchSetYearCallBack, "POST", Parameters, this.ContextHandler);
        objAjaxv2.EventData = null;
        objAjaxv2.GetPost(true)
    }
}
