﻿var map = null; //地圖物件
var geocoder = null; //搜尋

var RedrectStr="Map/MapSelectALLXML.ashx?Court=All&SelectClass=Auction,Sale"; //搜尋所得之XML
var IsStrat=false
//起始設定
function initialize()
{
    if(IsStrat==false)
    {
        if (GBrowserIsCompatible())
        {
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GSmallMapControl());
            geocoder = new GClientGeocoder();
        }
        GEvent.addListener(map, 'moveend', function()
        {
            PlotMarkAll(RedrectStr);
        });
        map.setCenter(new GLatLng(22.640356,120.315636), 15);
    }
    IsStrat=true;
}

function QSubmit(submitcase)
{
	var SelectClass=""
	if(aspnetForm.CheckboxAuction.checked)
	{
	    SelectClass+="Auction,"
	}
	if(aspnetForm.CheckboxSale.checked)
	{
	    SelectClass+="Sale,"
	}
	if(SelectClass==""){alert("請至少選擇一個搜尋項目!!");return;}
	
    RedrectStr="Map/MapSelectALLXML.ashx?Court=All&SelectClass="+ SelectClass.substring(0,SelectClass.length-1);
	
	var BKindStr = "";
	if (aspnetForm.CheckboxApartment.checked){BKindStr += escape("公寓") + ",";}
	if (aspnetForm.CheckboxBlock.checked){BKindStr += escape("大樓") + ",";}
	if (aspnetForm.CheckboxSigle.checked){BKindStr += escape("透天") + ",";}
	if (aspnetForm.CheckboxBungalow.checked){BKindStr += escape("平房") + ",";}
	if (aspnetForm.CheckboxShop.checked){BKindStr += escape("店面") + ",";}
	if (aspnetForm.CheckboxCottage.checked){BKindStr += escape("別墅") + ",";}
	if (BKindStr !="")
	{
	    BKindStr = "&BKind=" + BKindStr.substring(0,BKindStr.length-1);
	}
	
	var BSizeStr=""
	if((aspnetForm.BSizeMin.value!="") && (aspnetForm.BSizeMax.value!=""))
	{
		if(aspnetForm.BSizeMin.value==""){aspnetForm.BSizeMin.value=aspnetForm.BSizeMax.value}
		if(aspnetForm.BSizeMax.value==""){aspnetForm.BSizeMax.value=aspnetForm.BSizeMin.value}
		if(aspnetForm.BSizeMin.value-aspnetForm.BSizeMax.value>0)
		{
			var tmpvalue = aspnetForm.BSizeMin.value
			aspnetForm.BSizeMin.value=aspnetForm.BSizeMax.value
			aspnetForm.BSizeMax.value=tmpvalue
		}
		BSizeStr += "&BSizeMin=" + aspnetForm.BSizeMin.value + "&BSizeMax=" + aspnetForm.BSizeMax.value;
	}
	var PriceStr=""
	if((aspnetForm.ProPriceMin.value!="") && (aspnetForm.ProPriceMax.value!=""))
	{
		if(aspnetForm.ProPriceMin.value==""){aspnetForm.ProPriceMin.value=aspnetForm.ProPriceMax.value}
		if(aspnetForm.ProPriceMax.value==""){aspnetForm.ProPriceMax.value=aspnetForm.ProPriceMin.value}
		if(aspnetForm.ProPriceMin.value-aspnetForm.ProPriceMax.value>0)
		{
			var tmpvalue = aspnetForm.ProPriceMin.value
			aspnetForm.ProPriceMin.value=aspnetForm.ProPriceMax.value
			aspnetForm.ProPriceMax.value=tmpvalue
		}
		PriceStr += "&PriceMin=" + aspnetForm.ProPriceMin.value + "&PriceMax=" + aspnetForm.ProPriceMax.value;
	}
	var BuildStr=""
	if (aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListCity.value != "請選擇縣市"){BuildStr += escape(aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListCity.value)};
	if (aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListArea.value != "請選擇區域"){BuildStr += escape(aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListArea.value)};
	if (aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListRoad.value != "請選擇路名"){BuildStr += escape(aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListRoad.value)};

	//混合搜尋項目
	var MergeSearchStr =  BKindStr + BSizeStr + PriceStr

	switch(submitcase)
	{
		//總底價
		case "Price":
			if(PriceStr=="")
			{
				alert('總底價輸入錯誤');
				return;
			}else{
				RedrectStr += PriceStr
				break;
			}
		//建物總坪數
		case "BSize":
			if(BSizeStr=="")
			{
				alert('建物坪數輸入錯誤');
				return;
			}else{
				RedrectStr += BSizeStr;
				break;
			}
		//建物座落查詢
		case "Build":
			if (BuildStr=="")
			{
				alert('建物座落請至少輸入一項');
				return;
			}else{
			    var BuildStr=""
			    RedrectStr += BuildStr
				break;
			}
		case "AllMap":
		    RedrectStr += MergeSearchStr;
			break;
		case "ChangeClass":
		    RedrectStr += MergeSearchStr;
		    //alert(RedrectStr);
		    PlotMarkAll(RedrectStr);
			return;
	}
    if (geocoder)
    {
	    var BuildStr=""
	    if (aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListCity.value != "請選擇縣市"){BuildStr += aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListCity.value};
	    if (aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListArea.value != "請選擇區域"){BuildStr += aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListArea.value};
	    if (aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListRoad.value != "請選擇路名"){BuildStr += aspnetForm.ctl00$Content$TabContainerPush$TabPanelMap$ListBuild$DropDownListRoad.value};
        if (BuildStr!="")
        {
            geocoder.getLatLng(BuildStr,function(point)
            {
                if (!point)
                {
                    alert("搜尋不到此地址 "+ BuildStr);
                } else {
                    map.setCenter(point, 15);
                }
            });
        }else{
            map.setCenter(new GLatLng(22.640356,120.315636), 15);
        }
    }
}