﻿var strMain = "";
var strBug = "|";
var strUni = "|";
var strReg = "|";
var strFix = "|";
var strRom = "|";
var strRef = "|";
var strDat = "|";
var strInv = "|";
var strNam = "";
var strPageIndex = "1";
var strPageType = "T";	//T:Pic Z:Word
var strMapType = "S";
var strOrderType = 0;
var SearchMainType = "";
var housetype = "";
var BuyRent = "";
var DTDis = "300";
var defX = 13522000;
var defY = 3641125;
var Cid = 0;

function CutMain(s)
{
    var re = /X/g;
	s = s.trim().replace(re, '');
	
    if (s.length > 14)
    {
        s = s.substring(0, 14) + "…";
    }
    else
    {
        if (s.lastIndexOf('|') + 1 == s.length)
        {
            s = s.substr(0, s.length - 1);
        }
    }
    return s;
}

function CutStr(s)
{
    s = s.trim();
    if (s.length > 14)
    {
        s = s.substring(0, 14) + "…";
    }
    else
    {
        if (s.lastIndexOf('|') + 1 == s.length)
        {
            s = s.substr(0, s.length - 1);
        }
    }
    if (s == '')
    {
        s = "不限";
    }
    return s;
}

function CutStr2(s1, s2, s3)
{
    var s = "";
    if (s1 == "")
    {
        s = s2 + s3 + "以下";
    }
    else if (s2 == "")
    {
        s = s1 + s3 + "以上";
    }
    else
    {
        s = s1 + "-" + s2 + s3;
    }
    if (s == '')
    {
        s = "不限";
    }    
    return s;
}

function ShowMainText()
{
	var strMainText = "";
	jQuery('.SelectedWordsList span').each(function(){
		    strMainText += jQuery(this).text() + "|";
	})
	if (strMainText == "")
	{
		strMainText = "不限";	
	}
	if (strMain.indexOf('QY') >=0)
	{
		strMainText = "区域" + strMainText;
	}
	else if (strMain.indexOf('DT') >=0)
	{
		strMainText = "地铁" + strMainText;
	}
	else if (strMain.indexOf('XX') >=0)
	{
		strMainText = "学校" + strMainText;
	}
	else
	{
	    strMainText = "区域" + strMainText;
	}
	
    jQuery('#SSpanMain').html(CutMain(strMainText));
}

function ShowBugText()
{
    var strText = "";
    jQuery('#ddBug :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanBug').html('预算' + CutStr(strText));
}

function ShowUniText()
{
    var strText = "";
    jQuery('#ddUni :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanUni').html('单价' + CutStr(strText));
}

function ShowRegText()
{
    var strText = "";
    jQuery('#ddReg :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanReg').html('面积' + CutStr(strText));
}

function ShowFixText()
{
    var strText = "";
    jQuery('#ddFix :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanFix').html('装修' + CutStr(strText));
}

function ShowRomText()
{
    var strText = "";
    jQuery('#ddRom :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanRom').html('房型' + CutStr(strText));
}

function ShowRefText()
{
    var strText = "";
    jQuery('#ddRef :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanRef').html('用途' + CutStr(strText));
}

function ShowDatText()
{
    var strText = "";
    jQuery('#ddDat :radio').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanDat').html('日期' + CutStr(strText));
}

function ShowInvText()
{
    var strText = "";
    jQuery('#ddInv :checkbox').each(function(){
        if (jQuery(this).attr("checked")==true){
		    strText += jQuery(this).parent().text() + "|";
		}
	})
    
    jQuery('#SSpanInv').html('投报率' + CutStr(strText));
}

function GoPage(p)
{
    strPageIndex = p;
    
    LoadData(0);
}

function ChangeType(t)
{  
    strPageType = t;
    strPageIndex = "1";
    
    if (strMapType == "S")
    {
        LoadData(0);
    }
}

function SetBug()
{
    if (isNaN($get("bugB").value) || isNaN($get("bugT").value))
    {
        alert('请填写正确的数字');
    }
    else
    {
        if ($get("bugB").value != '' || $get("bugT").value != '')
        {
            jQuery('.SearchMain input[name="chkBug"]:checkbox').each(function(){
                jQuery(this).attr("checked",false);
            })
        
            strBug = "_" + $get("bugB").value + "-" + $get("bugT").value;
            if (BuyRent == "Buy")
            {
                jQuery('#SSpanBug').html('预算' + CutStr2($get("bugB").value, $get("bugT").value, '万'));
                jQuery('#DefinedYsValue').html('自定义预算：' + CutStr2($get("bugB").value, $get("bugT").value, '万'));
            }
            else
            {
                jQuery('#SSpanBug').html('预算' + CutStr2($get("bugB").value, $get("bugT").value, ''));
                jQuery('#DefinedYsValue').html('自定义预算：' + CutStr2($get("bugB").value, $get("bugT").value, ''));
            }
            
            jQuery('#DefinedYsValue').show();
            
            LoadData(1);
        }
    }
    
    return false;
}

function SetReg()
{
    if (isNaN($get("regB").value) || isNaN($get("regT").value))
    {
        alert('请填写正确的数字');
    }
    else
    {
        if ($get("regB").value != '' || $get("regT").value != '')
        {
            jQuery('.SearchMain input[name="chkReg"]:checkbox').each(function(){
                jQuery(this).attr("checked",false);
            })
            
            strReg = "_" + $get("regB").value + "-" + $get("regT").value;
            jQuery('#SSpanReg').html('面积' + CutStr2($get("regB").value, $get("regT").value, '平米'));
            
            jQuery('#DefinedMjValue').html('自定义面积：' + CutStr2($get("regB").value, $get("regT").value, '平米'));
            jQuery('#DefinedMjValue').show();
            
            LoadData(1);
        }
    }
    
    return false;
}

function ResetBug()
{
    $get("bugB").value = "";
    $get("bugT").value = "";
    
    return false;
}

function ResetReg()
{
    $get("regB").value = "";
    $get("regT").value = "";
    
    return false;
}

function ChangeOrder()
{
    strOrderType = $get("selOrder").selectedIndex;
    
    LoadData(1);
}

function ChangeOrderBy_Title(OrderBy)
{
    switch (OrderBy)
    {
        case "register":
            if (strOrderType == 2)
            {
                $get("selOrder").selectedIndex = 3;
            }
            else
            {
                $get("selOrder").selectedIndex = 2;
            }
            break;
        case "totalprice":
            if (strOrderType == 6)
            {
                $get("selOrder").selectedIndex = 7;
            }
            else
            {
                $get("selOrder").selectedIndex = 6;
            }
            break;
        case "unitprice":
            if (strOrderType == 4)
            {
                $get("selOrder").selectedIndex = 5;
            }
            else
            {
                $get("selOrder").selectedIndex = 4;
            }
            break;                        
        case "datetime":
            $get("selOrder").selectedIndex = 1;
            break;            
        case "click":
            if (strOrderType == 8)
            {
                $get("selOrder").selectedIndex = 9;
            }
            else
            {
                $get("selOrder").selectedIndex = 8;
            }
            break;             
    }
    
    ChangeOrder();
}

function ClearDefault()
{
    if($get("txtNam").value == "请输入小区或路名")
    {
        $get("txtNam").value = "";
    }
}

function SearchButton()
{
    if($get("txtNam").value == "请输入小区或路名")
    {
        strNam = "";
    }
    else
    {
        strNam = $get("txtNam").value;
    }
       
    LoadData(1);
    
    return false;
}

function OMO(X, Y, a) {
    var iframe1 = document.getElementById("iMap");
    iframe1.src = "/Maps/ListMap.aspx?X=" + X + "&Y=" + Y;
}

function AddToFav(ID)
{
    if(jQuery(ID).val() != ""){
        if(jQuery(ID).attr("checked")){
            memberhouseWS.AddToFav(housetype,
                    jQuery(ID).val(),
                    AddToFavResult);
        }
        else{
            if(confirm("确定要删除选中的房源吗？")){
                memberhouseWS.DeleteFav(housetype,
                jQuery(ID).val(),
                DeleteFavResult);
            }
        }
    }
}

function AddToFavResult(result){
    switch(result){
        case "InvalidUsername":
            ShowMsg("无效用户");break;
        case "WrongParameters":
            ShowMsg("参数错误");break;
        case "Error":
            ShowMsg("系统错误请联系管理员");break;
        case "Success":
            GetMemberFavForDetailPage();break;
        default:
            ShowMsg("系统错误请联系管理员");break;
            break;
    }
}

function DeleteFav(ID){
    if(ID != ""){
        if(confirm("确定要删除选中的房源吗？")){
            memberhouseWS.DeleteFav(housetype,
            ID,
            DeleteFavResult);
        }
    }
}

function DeleteFavResult(result){
    switch(result){
        case "InvalidUsername":
            ShowMsg("无效用户");break;
        case "Success":
            GetMemberFavForDetailPage();break;
        case "Error":
            ShowMsg("系统错误请联系管理员");break;
    }
}

function DelCondition(ID){
    if(ID != ""){
        if(confirm("确定要删除选中的搜索条件吗？")){
            SearchConditionWS.DeleteCon(ID,
            DelConditionResult);
        }
    }
}

function DelConditionResult(result){
    switch(result){
        case "InvalidUsername":
            ShowMsg("无效用户");break;
        case "Success":
            GetCondition();break;
        case "Error":
            ShowMsg("系统错误请联系管理员");break;
    }
}
            
jQuery(function(){
    jQuery('.SearchMm :radio').each(function(){
        jQuery(this).click(function(){
            if(jQuery(this).attr("checked")==true){
                strMain = strMain.replace("DT|" + DTDis + "|", "DT|" + jQuery(this).attr("value") + "|"); 
                DTDis = jQuery(this).attr("value");                
            }
            
        LoadData(1);
        })
    })
    
    jQuery('.SearchMain :radio').each(function(){
        jQuery(this).click(function(){
            if(jQuery(this).attr("checked")==true){
                if (jQuery(this).attr("value") != ""){
                    switch(jQuery(this).attr("name")){
                    case "chkDat":
                            strDat = "|" + jQuery(this).attr("value") + "|";
                            ShowDatText();
                            break;
                    }
                }
                else{
                    switch(jQuery(this).attr("name")){
                    case "chkDat":
                            strDat = "|";
                            ShowDatText();
                            break;   
                    }
                }
            }
            else{
                if (jQuery(this).attr("value") != ""){
                    switch(jQuery(this).attr("name")){
                    case "chkDat":
                            strDat = strDat.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowDatText();
                            break;
                    }
                }
            }
            
        LoadData(1);
        })
    })
    
    jQuery('.SearchMain :checkbox').each(function(){
        jQuery(this).click(function(){
  
            if(jQuery(this).attr("checked")==true){
                if (jQuery(this).attr("value") != ""){
                                        
                    jQuery('.SearchMain input[name="' + jQuery(this).attr("name") + '"]:checkbox').each(function(){
                        if (jQuery(this).attr("value") == ""){
                            jQuery(this).attr("checked",false);
                        }
                    })
                    
                    switch(jQuery(this).attr("name")){
                    case "chkInv":
                            strInv += jQuery(this).attr("value") + "|";
                            ShowInvText();
                            break;
                    case "chkRef":
                            strRef += jQuery(this).attr("value") + "|";
                            ShowRefText();
                            break;
                    case "chkRom":
                            strRom += jQuery(this).attr("value") + "|";
                            ShowRomText();
                            break;
                    case "chkFix":
                            strFix += jQuery(this).attr("value") + "|";
                            ShowFixText();
                            break;
                    case "chkReg":
                            if (strReg.substring(0,1) == "|"){
                                strReg += jQuery(this).attr("value") + "|";
                            }
                            else{
                                strReg = "|" + jQuery(this).attr("value") + "|";
                            }
                            ShowRegText();
                            jQuery('#DefinedMjValue').hide();
                            break;
                    case "chkUni":
                            strUni += jQuery(this).attr("value") + "|";
                            ShowUniText();
                            break;
                    case "chkBug":
                            if (strBug.substring(0,1) == "|"){
                                strBug += jQuery(this).attr("value") + "|";
                            }
                            else{
                                strBug = "|" + jQuery(this).attr("value") + "|";
                            }
                            ShowBugText();
                            jQuery('#DefinedYsValue').hide();
                            break;     
                    }
                }
                else{
                    jQuery('.SearchMain input[name="' + jQuery(this).attr("name") + '"]:checkbox').each(function(){
                        if (jQuery(this).attr("value") != ""){
                            jQuery(this).attr("checked",false);
                        }
                    })
                    
                    switch(jQuery(this).attr("name")){
                    case "chkInv":
                            strInv = "|";
                            ShowInvText();
                            break;
                    case "chkRef":
                            strRef = "|";
                            ShowRefText();
                            break;
                    case "chkRom":
                            strRom = "|";
                            ShowRomText();
                            break;
                    case "chkFix":
                            strFix = "|";
                            ShowFixText();
                            break; 
                    case "chkReg":
                            strReg = "|";
                            ShowRegText();
                            jQuery('#DefinedMjValue').hide();
                            break;
                    case "chkUni":
                            strUni = "|";
                            ShowUniText();
                            break;
                    case "chkBug":
                            strBug = "|";
                            ShowBugText();
                            jQuery('#DefinedYsValue').hide();
                            break;                                                         
                    }
                }               
            }
            else{
                if (jQuery(this).attr("value") != ""){
                    switch(jQuery(this).attr("name")){
                    case "chkInv":
                            strInv = strInv.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowInvText();
                            break;
                    case "chkRef":
                            strRef = strRef.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowRefText();
                            break;
                    case "chkRom":
                            strRom = strRom.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowRomText();
                            break;
                    case "chkFix":
                            strFix = strFix.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowFixText();
                            break; 
                    case "chkReg":
                            strReg = strReg.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowRegText();
                            break;
                    case "chkUni":
                            strUni = strUni.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowUniText();
                            break;
                    case "chkBug":
                            strBug = strBug.replace("|" + jQuery(this).attr("value") + "|", "|");
                            ShowBugText();
                            break;                                                         
                    }
                }
            }
        
        LoadData(1);
        })
    })
})

function SetMainStr(v)
{
    strMain = v;
}

function SetBugStr(v)
{
    strBug = v;
}

function SetUniStr(v)
{
    strUni = v;
}

function SetRegStr(v)
{
    strReg = v;
}

function SetFixStr(v)
{
    strFix = v;
}

function SetRomStr(v)
{
    strRom = v;
}

function SetRefStr(v)
{
    strRef = v;
}

function SetDatStr(v)
{
    strDat = v;
}

function SetInvStr(v)
{
    strInv = v;
}

function SetNamStr(v)
{
    jQuery('#txtNam').val(v);
    strNam = v;
}

function SetCid(v)
{
    Cid = v;
}
