﻿//----------------------------------------------------------------------//
//                                                                      //
//                        余晓天    2009-3-9                            //
//                                                                      //
//                        企业站点搜索功能方法                          //
//                                                                      //
//                     修改原始方法请加备注时间和姓名                   //
//                                                                      //
//----------------------------------------------------------------------//
//页面初始函数
$(document).ready(function(){
    var inputsearch=$("#searchInput");
    $("#searchButton").click(function(){
        if($.trim(inputsearch.val())!="")
        {
            if($("select[@name='searchtype'] option[@selected]").val()=="2")
                window.location.href="/WebSite/"+$("#CmyID").val()+"/product_search.shtml?key="+inputsearch.val();
            else
                window.location.href="/WebSite/"+$("#CmyID").val()+"/news_search.shtml?key="+inputsearch.val();
        }
        else
        {
            alert("关键字不能为空，请输入!");
            inputsearch.focus();
        }
    });
    
    if(typeof(searchtype)!="undefined")
    {
        if(window.location.search.indexOf("key")>-1)
        {
            var keys=window.location.search.substring(window.location.search.indexOf("=")+1);
            $("#keyword").html(keys);
            WebSearch(searchtype,keys,"1","8");
        }
        if(searchtype=="2")
        {
               $("#searchtype")[0].selectedIndex =1;
        }
    }
  
});
//分页函数
function SearchPaging(stype,pageIndex)
{
    if(typeof(searchtype)!="undefined")
    {
        if(window.location.search.indexOf("key")>-1)
        {
            var keys=window.location.search.substring(window.location.search.indexOf("=")+1);
            WebSearch(stype,keys,pageIndex,"8");
        }
    }
}
//第一页函数
function StartPage(stype)
{
    var pageindex=parseInt($("#pageindex").html());
    if(pageindex==1)
    {
        alert("已经是第一页!");
    }
    else
        SearchPaging(stype,"1");
}
//上一页函数
function PrePage(stype)
{
    var pageindex=parseInt($("#pageindex").html());
    if(pageindex<2)
      {
        alert("没有上一页了!");
        return;
      }
    else
        pageindex=pageindex-1;
    SearchPaging(stype,pageindex);
}
//下一页函数
function NextPage(stype)
{
    var pageindex=parseInt($("#pageindex").html());
    var totalpage=parseInt($("#totalpage").html());
    if(pageindex+1>totalpage)
    {
        alert("没有下一页了!");
        return;
    }
    else
        pageindex=pageindex+1;
    SearchPaging(stype,pageindex);
}
//最后一页函数
function EndPage(stype)
{
    var pageindex=parseInt($("#pageindex").html());
    var totalpage=parseInt($("#totalpage").html());
    if(pageindex==totalpage)
    {
        alert("已经是最后一页!");
    }
    else
        SearchPaging(stype,totalpage);
}
//搜索AJAX处理函数
function WebSearch(stype,keyword,pageIndex,pageSize)
{
   
    if(searchtype=="1")
        $("#ulNewsList").html("<div style=\"text-align:center\"><img src=\"/images/ajax-loader.gif\" alt=\"等待中\"/></div>");
    else
        $("#searchProductList").html("<div style=\"text-align:center\"><img src=\"/images/ajax-loader.gif\" alt=\"等待中\"/></div>");
    $.ajax({
        type:"POST",
        url:"/Ajax/Search.ashx",
        data:{CmyId:$("#CmyID").val(),searchType:stype,key:keyword,datetime:new Date(),pageIndex:pageIndex,pageSize:pageSize},
        dataType:"json",
        success:function(data,textStatus)
        {
            if(data.result=="false")
            {
                if(searchtype=="1")
                    $("#ulNewsList").html("<p style=\"text-align:center;\">暂无数据,请换个关键字重试!!</p>");
                else
                    $("#searchProductList").html("<p style=\"text-align:center;\">暂无数据,请换个关键字重试!!</p>");
                $("#totalpage").html("1");
                $("#pageindex").html("1");
            }
            else
            {
               var htmllist="";
               if(searchtype=="1")
               {
                   for(var i=0;i<data.list.length;i++)
                   {
                        htmllist+="<li><a href=\""+data.list[i].url+"\" target=\"_blank\" title=\""+data.list[i].NewsTitle+"\">"+data.list[i].NewsTitle+" </a><span>["+data.list[i].CreateTime+"]</span></li>";
                        htmllist+="<b class=\"dashed02\"></b>";
                   }
                   $("#ulNewsList").html(htmllist);
                   $("#totalpage").html(data.totalpage);
                   $("#pageindex").html(data.pageindex);
               }
               else
               {
                   htmllist+="<div class=\"product_li_tit\"><span style=\"margin-left:20px;\">产品信息</span><span style=\"margin-left:453px;\">单价</span><span style=\"margin-left:88px;\">发布日期</span></div>";
                   for(var i=0;i<data.list.length;i++)
                   {
                         htmllist+="<div class=\"product_li\">";
        	             htmllist+=" <div class=\"product_li_c1\"><a href=\""+data.list[i].url+"\" target=\"_blank\" title=\""+data.list[i].url+"\"><img src=\""+data.list[i].ProductSmaImg+"\" width=\"112\" height=\"84\" onerror=\"this.onerror=null;this.src='/images/pic_no_112_84.gif'\" alt=\""+data.list[i].ProductName +"\" class=\"img02\" /></a></div>";
                         htmllist+="<div class=\"product_li_c2\" ><h1 class=\"h2\"><a href=\""+data.list[i].url+"\" target=\"_blank\" title=\""+data.list[i].ProductName +"\">"+data.list[i].ProductName +"</a></h1>";
                         htmllist+="<p>"+data.list[i].Remark.substring(0,500)+"</p></div><div class=\"product_li_c3\" >"+data.list[i].Price+"</div><div class=\"product_li_c4\" >"+data.list[i].CreateTime+"</div><br/></div>";
                   }
                   $("#searchProductList").html(htmllist);
                   $("#totalpage").html(data.totalpage);
                   $("#pageindex").html(data.pageindex);
               }
            }
        },
        error:function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert("搜索错误，请重新输入!");
        }
    });
}