﻿var OPSrh=new Object();
OPSrh.doSearch=function()
{
    var pcatid=($(".searchContainer :input[name='pcatid']").get(0).value);
    var brandid=($(".searchContainer :input[name='pbrandid']").get(0).value);
    var key=$(".searchContainer :input[name='pkeyword']").get(0).value;
    if(!pcatid&&!brandid&&!key)
    {
        alert('Please enter Search Criteria');
        return;
    }
    var url="/products.aspx?pc={0}&pb={1}&q={2}";
    url=url.format(pcatid,brandid,encodeURIComponent(key));
    location.href=url;
}
OPSrh.bcCache=[];
OPSrh.getBrand=function()
{
    var pcatid=($(".searchContainer :input[name='pcatid']").get(0).value);
    if(!OPSrh.bcCache["pc"+pcatid])
    {
        adInput(false,".searchContainer");
        $.post('/ajax.aspx', 
        {'ajaxcmd':'GetBrandByCat','pc':pcatid}, 
        function(json) {
            adInput(true,".searchContainer");
            try
            {
                eval("var json="+json);
            }
            catch(e){json=[];}
            adInput(true,".searchContainer");
            OPSrh.fillSelOp("pbrandid",json,"By Manufacturer");
            OPSrh.bcCache["pc"+pcatid]=json;
        });  
    }
    else
    {
        OPSrh.fillSelOp("pbrandid",OPSrh.bcCache["pc"+pcatid],"By Manufacturer");
    }
}
OPSrh.getCat=function()
{
    var brandid=($(".searchContainer :input[name='pbrandid']").get(0).value);
    if(!OPSrh.bcCache["pb"+brandid])
    {
        adInput(false,".searchContainer");
        $.post('/ajax.aspx', 
        {'ajaxcmd':'GetCatByBrand','pb':brandid}, 
        function(json) {
            adInput(true,".searchContainer");
            try
            {
                eval("var json="+json);
            }
            catch(e){json=[];}
            OPSrh.fillSelOp("pcatid",json,"By Category");
            OPSrh.bcCache["pb"+brandid]=json;
        });
    }
    else
    {
        OPSrh.fillSelOp("pcatid",OPSrh.bcCache["pb"+brandid],"By Category");
    }
}

OPSrh.fillSelOp=function(selname,json,def)
{
    var jkv=$(".searchContainer select[name='"+selname+"']");
    var opsel=jkv.get(0);
    var cursel=opsel.value;    
    jkv.find("option").remove();
    opsel.options.add(new Option(def,"")); 
    for(var i=0;i<json.length;i++)
    {
        opsel.options.add(new Option(json[i]["Key"],json[i]["Value"])); 
    }
    for(var i=0;i< opsel.options.length;i++)
    {
        if(opsel.options[i].value==cursel)
        {
            opsel.selectedIndex=i;
            break;
        }
    }
}
