﻿function Products(searchString) {
    if (searchString.length > 1) {
        $.ajax({
            type: 'POST',
            url: "search.asmx/pro",
            contentType: "application/json; charset=utf-8",
            dataType: 'json', data: '{ searchString: "' + searchString + '" }',
            success: function(response) {
                var c = eval(response.d);


                if (c.length != 0) {
                    $("#smartSearch").append("<ul id=smartsel>");


                    $("#smartSearch").append("<li class=srchtype>PRODUCTS FOUND</li>");
                    for (var i = 0; i < c.length; i++) {
                        $("#smartSearch").append("<li><a href='p-" + c[i].ProductID + "-" + c[i].ProductURL + ".aspx'>" + c[i].ProductName + "</a>");
                        $("#smartSearch").append("</li>");
                    }
                    $("#smartSearch").append("</ul>");

                    //$("#status li").removeClass("active").eq(i).addClass("active");
                    $("#smartSearch").removeClass("hideresults").eq(i).addClass("showresults");
                    //$("#smartSearch ul").removeClass("smartsel").eq(i).addClass("smartsel");
                    //$("#smartSearch li").removeClass("srchitem").eq(i).addClass("srchitem");
                };

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $("#error").html(XMLHttpRequest.status + "\n<hr />" + XMLHttpRequest.responseText);
            }


        }

                                 )
                                  ;

    }
    else if (searchString.length == 0) {
        $("#smartSearch").html('');
        $("#smartSearch").removeClass("hideresults").eq(0).addClass("hideresults");
    };
}
//CATEGORIES SEARCH
function Categories(searchString) {
    if (searchString.length > 1) {
        $.ajax({
            type: 'POST',
            url: "search.asmx/cat",
            contentType: "application/json; charset=utf-8",
            dataType: 'json', data: '{ searchString: "' + searchString + '" }',
            success: function(response) {
                var c = eval(response.d);

                if (c.length != 0) {
                    $("#smartSearch").append("<ul>");


                    $("#smartSearch").append("<li>CATEGORIES FOUND</li>");
                    for (var i = 0; i < c.length; i++) {
                        $("#smartSearch").append("<li><a href='c-" + c[i].ProductID + "-" + c[i].ProductURL + ".aspx'>" + c[i].ProductName + "</a>");
                        $("#smartSearch").append("</li>");
                    }
                    $("#smartSearch").append("</ul>");

                    //$("#status li").removeClass("active").eq(i).addClass("active");
                    $("#smartSearch").removeClass("hideresults").eq(i).addClass("showresults");
                    //$("#smartSearch ul").removeClass("smartsel").eq(i).addClass("smartsel");
                    //$("#smartSearch li").removeClass("srchitem").eq(i).addClass("srchitem");
                };

            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                $("#error").html(XMLHttpRequest.status + "\n<hr />" + XMLHttpRequest.responseText);
            }


        }

                                 )
                                  ;

    }
    //else if (searchString.length == 0) {$("#smartSearch").html('');$("#smartSearch").removeClass("hideresults").eq(0).addClass("hideresults");};
}

function searchAll(searchString) {
    $("#smartSearch").html('');
    Products(searchString);
    Categories(searchString);
}
