﻿// JScript 文件
var oldcondition="";
document.onkeyup=function()
{
   
   if(event.srcElement.id=="txtCondition")
   {
       if(event.keyCode!=13)
        {
            //preSearch();
        }
        else
        {
            goSearch();
        }
   }
   else
   {
        return;
   }
}

//window.onresize=function()
//{
//    var e = document.getElementById("txtCondition");
//    var left=e.offsetLeft;    
//    var top=e.offsetTop;
//    var width=e.offsetWidth;
//    var toptemp=e.offsetHeight;
//    while (e=e.offsetParent){
//            left += e.offsetLeft;
//            top += e.offsetTop; };     
//   top +=toptemp;
//   $("#div1").css("top",top);
//   $("#div1").css("left",left);
//}

function goSearch()
{
    var condition=$("#txtCondition").val();
    condition=$.trim(condition);
    if(condition.length>0)
    {
        Search("txtCondition",condition);
    }
}

function preSearch()
{
    var condition=$("#txtCondition").val();
    condition=$.trim(condition);
    if(condition.length==0)
    {
        $("#search_div").empty();
        oldcondition="";
        $("#search_div").hide();
    }
    else if(condition==oldcondition)        //和以前的值相等，就不需重复提交
    {
        
        //$("#div1").empty();
    }
    else
    {
        oldcondition=condition;
        $.ajax({
            type:"POST",
            cache:false,
            async: false,
            url:"Ajax/AutoCompleteSearch.ashx?Candition="+escape(condition)+"&tmp="+(new Date().getTime().toString(36)),
            success:function(msg){rtn_preSearch(msg);},
            error:function(){alert("occur error")}
            });
    }
}

function rtn_preSearch(responsetext)
{
    var result=responsetext.split("|");
    if(responsetext.length>0)
    {
        ShowResult1(result);
    }
    else if(responsetext=="")
    {
         $("#search_div").hide();
    }
    
}

function ShowResult1(result)
{
    var temp="";
    var conditionId="";
    for(var i=0;i<result.length;i++)
    {
        //temp=temp+"<div class=\"search_kang\" style=\"cursor:hand;\"onclick=\"itemclick();\"><a href=\"#\">"+result[i].toString()+"</a><\/div>";
        var infos=result[i].split(',');
        conditionId="condition"+i.toString();
        temp=temp+"<div class=\"search_kang\" style=\"width:183px; overflow:hidden; cursor:hand; height:22px; \"onclick=\"itemclick('"+conditionId+"');\" line-height:22px;\"><a href=\"#\"><span class=\"condition\" id=\""+conditionId+"\" style=\"display:block; width:140px; height:22px; line-height:22px; float:left;\">"+infos[0]+"</span><span class=\"result\" style=\"display:block; width:43px; color:Green; height:22px; line-height:22px; float:right;\">"+infos[1]+"结果</span></a></div>";
    }
    $("#search_div").empty();
    $("#search_div").append(temp);
    
    var e = document.getElementById("txtCondition");
    var left=e.offsetLeft;    
    var top=e.offsetTop;
    var width=e.offsetWidth;
    var toptemp=e.offsetHeight;
    while (e=e.offsetParent){
            left += e.offsetLeft;
            top += e.offsetTop; };     
   top +=toptemp;
   $("#search_div").css("top",0);
   $("#search_div").css("left",50); 
   $("#search_div").show();
   //alert(top);
   //alert(left);
}

function itemclick(id)
{
//    var objEvent = window.event || arguments.callee.caller.arguments[0];
//    var srcElement = objEvent.srcElement;
//    if (!srcElement)
//    {
//        srcElement = objEvent.target;
//    }
    var srcElement=$("#"+id);
    $("#txtCondition").val($(srcElement).text());
    $("#search_div").empty();
    $("#search_div").hide();
    
}

function userediting()
{
    if($("#txtCondition").val()=="关键字间用空格分开")
    {
        $("#txtCondition").val("");
        $("#txtCondition").removeClass("txtCondition");
    }
}

function endedit()
{
    if($.trim($("#txtCondition").val())=="")
    {
        $("#txtCondition").val("关键字间用空格分开");
        $("#txtCondition").addClass("txtCondition");
    }
}