/* The copy right is belong to idealhome.com.tw
	 Author= Arthur Chang	 (張榮庭)
	 this document created day 2008/06/26
 */
/*
 * prerequisite : GenericHTMLObjOrganizationFunc.js
 */



function SearchConditionController(usingMapSearch){
	if(usingMapSearch){
		this.usingMapSearch = true;
	}else{
		this.usingMapSearch = false;
	}
}

// default property value of this controller
SearchConditionController.prototype.keywordDefaultValue = "例如路名,租屋限制,其他您想要的關鍵字";
SearchConditionController.prototype.search_price_minDefaultValue= "最低租金";
SearchConditionController.prototype.search_price_maxDefaultValue= "最高租金";
SearchConditionController.prototype.valueOfOtherPrice = "others";

// default field id name 1
SearchConditionController.prototype.id_county = "Search_Address_County";
SearchConditionController.prototype.id_mrtLine = "Search_MRTLine";
SearchConditionController.prototype.id_mrtStationStart = "Search_MRTStart";
SearchConditionController.prototype.id_mrtStationEnd = "Search_MRTEnd";
SearchConditionController.prototype.id_school = "Search_School";
SearchConditionController.prototype.id_shoppingArea = "Search_ShoppingArea";
SearchConditionController.prototype.id_administrativeArea = "Search_Area";
// default field id name 2
SearchConditionController.prototype.id_price = "Search_Price";
SearchConditionController.prototype.id_priceInputArea = "price_input_area";
SearchConditionController.prototype.id_maxPrice = "search_price_max";
SearchConditionController.prototype.id_minPrice = "search_price_min";
SearchConditionController.prototype.id_keyword = "Search_Keyword";
SearchConditionController.prototype.id_houseType = "Search_Type";
SearchConditionController.prototype.id_houseSize = "Search_Size";


//condition elements partition id name
SearchConditionController.prototype.id_normalConditionDiv = "normalsearch";
SearchConditionController.prototype.id_advanceConditionDiv = "advance_search_conditions";
SearchConditionController.prototype.id_wholeCountySearchLinkDiv = "whole_county_search_lnks"; 
SearchConditionController.prototype.id_searchConditionSubmitBtnDiv = "search_condition_submit_btn_area"; 
SearchConditionController.prototype.id_mapAdDiv = "mapad";
SearchConditionController.prototype.id_hotRentAreaDiv = "hotrentarea";

//condition elements partition button id name
SearchConditionController.prototype.id_goRentHomeDivBtn = "search_go_home_btn";
SearchConditionController.prototype.id_normalConditionDivBtn = "normal_search_condition_btn";
SearchConditionController.prototype.id_advanceConditionDivBtn = "advance_search_condition_btn";
SearchConditionController.prototype.id_wholeCountySearchLinkDivBtn = "whole_county_search_lnk_btn";
SearchConditionController.prototype.id_mapSearchDivBtn = "map_search_btn";
SearchConditionController.prototype.id_hotRentAreaDivBtn = "hotrentarea_btn";

// search button id name 
SearchConditionController.prototype.id_houseSearchBtn = "search_condition_submit_btn";
SearchConditionController.prototype.id_houseSearchMapSearchBtn = "search_condition_submit_map_search_btn";

// search form id name
SearchConditionController.prototype.id_houseSearchForm = "house_search_condition_form";

SearchConditionController.prototype.changeSelectObjectDataForCountyChange = function(CountySelectObject, searchConditionController) {
	var index = CountySelectObject.selectedIndex;
	var value = CountySelectObject[index].value;
	getACountySchools(value, document.getElementById(searchConditionController.id_school), true, "不拘");
	getShoppingArea(value, document.getElementById(searchConditionController.id_shoppingArea), true, "不拘");
	getSearchArea(value, document.getElementById(searchConditionController.id_administrativeArea), true, "請選擇");
}

SearchConditionController.prototype.MRTLineChaged = function(MrtLineSelectObject, searchConditionController){
	var index = MrtLineSelectObject.selectedIndex;
	var value = MrtLineSelectObject[index].value;	
	getMRTStationsForALine(value, document.getElementById(searchConditionController.id_mrtStationStart), true, "");
	getMRTStationsForALine(value, document.getElementById(searchConditionController.id_mrtStationEnd), true, "");
}

SearchConditionController.prototype.adaptEvntHandlerForPartitionDiv = function(){
	var preShowDiv = document.getElementById(this.id_normalConditionDiv);
	var preShowDiv2 = document.getElementById(this.id_advanceConditionDiv);
	var preShowDiv3 = document.getElementById(this.id_wholeCountySearchLinkDiv);
	var preShowDiv4 =  document.getElementById(this.id_searchConditionSubmitBtnDiv);
	var preShowDiv5 =  document.getElementById(this.id_mapAdDiv);
	var preShowDiv6 =  document.getElementById(this.id_hotRentAreaDiv);
	
	if(document.getElementById(this.id_mapSearchDivBtn)){
		document.getElementById(this.id_mapSearchDivBtn).onclick = 
			function(){
			window.location.href = "?plugins=quick-search";
		}
	}
	
	var obj = document.getElementById(this.id_goRentHomeDivBtn);
	if(obj != null){
		document.getElementById(this.id_goRentHomeDivBtn).onclick= 
			function(){		
			window.location.href = "/rent";
		};
	}
	
	obj = document.getElementById(this.id_normalConditionDivBtn);
	if(obj != null){
		document.getElementById(this.id_normalConditionDivBtn).onclick= 
			function(){		
				controlDisplayForDIVObject(preShowDiv2,false);
				controlDisplayForDIVObject(preShowDiv3,false);
				if(preShowDiv5){
					controlDisplayForDIVObject(preShowDiv5,false);
				}
				if(preShowDiv6){
					controlDisplayForDIVObject(preShowDiv6,false);
				}
				controlDisplayForDIVObject(preShowDiv);
				controlDisplayForDIVObject(preShowDiv4);
		};
	}
	document.getElementById(this.id_advanceConditionDivBtn).onclick= 
		function(){
		controlDisplayForDIVObject(preShowDiv3,false);
		if(preShowDiv5){
			controlDisplayForDIVObject(preShowDiv5,false);
		}
		if(preShowDiv6){
			controlDisplayForDIVObject(preShowDiv6,false);
		}
		controlDisplayForDIVObject(preShowDiv);
		controlDisplayForDIVObject(preShowDiv2);
		controlDisplayForDIVObject(preShowDiv4);
	};
	document.getElementById(this.id_wholeCountySearchLinkDivBtn).onclick= 
		function(){
		controlDisplayForDIVObject(preShowDiv,false);
		controlDisplayForDIVObject(preShowDiv2,false);
		controlDisplayForDIVObject(preShowDiv4,false);
		if(preShowDiv5){
			controlDisplayForDIVObject(preShowDiv5,false);
		}
		if(preShowDiv6){
			controlDisplayForDIVObject(preShowDiv6,false);
		}
		controlDisplayForDIVObject(preShowDiv3);		
	};
	if(document.getElementById(this.id_hotRentAreaDivBtn)){
		document.getElementById(this.id_hotRentAreaDivBtn).onclick= 
			function(){
			controlDisplayForDIVObject(preShowDiv,false);
			controlDisplayForDIVObject(preShowDiv2,false);
			controlDisplayForDIVObject(preShowDiv3,false);
			controlDisplayForDIVObject(preShowDiv4,false);
			if(preShowDiv5){
				controlDisplayForDIVObject(preShowDiv5,false);
			}
			controlDisplayForDIVObject(preShowDiv6);		
		};
	}
	
	

}

SearchConditionController.prototype.adaptEvntHandlerSearchBtn = function(){
	if(this.usingMapSearch){
		controlDisplayForDIVObject(document.getElementById(this.id_houseSearchMapSearchBtn));
		controlDisplayForDIVObject(document.getElementById(this.id_houseSearchBtn), false);
		document.getElementById(this.id_houseSearchMapSearchBtn).searchConditionController = this;
		document.getElementById(this.id_houseSearchMapSearchBtn).onclick= function(){
			this.searchConditionController.mapSearchSubmit(false);
		}		
	}else{
		controlDisplayForDIVObject(document.getElementById(this.id_houseSearchBtn));
		controlDisplayForDIVObject(document.getElementById(this.id_houseSearchMapSearchBtn), false);
		document.getElementById(this.id_houseSearchBtn).searchConditionController = this;
		document.getElementById(this.id_houseSearchBtn).onclick= function(){
			this.searchConditionController.searchSubmit();
		}
	}
}

SearchConditionController.prototype.adaptEvntHandlerForActionBtn = function(){
	//
	document.getElementById(this.id_county).searchConditionController = this;
	document.getElementById(this.id_county).onclick = function(){
		this.searchConditionController.changeSelectObjectDataForCountyChange(this,this.searchConditionController);
	}
	
	document.getElementById(this.id_mrtLine).searchConditionController = this;
	document.getElementById(this.id_mrtLine).onchange = function(){
		this.searchConditionController.MRTLineChaged(this,this.searchConditionController);
	}
	
	//
	document.getElementById(this.id_price).searchConditionController = this;
	document.getElementById(this.id_price).onchange =function(){this.searchConditionController.search_price_select_changed(this, this.searchConditionController);};
	
	document.getElementById(this.id_minPrice).searchConditionController = this;
	document.getElementById(this.id_minPrice).onchange =function(){this.searchConditionController.search_price_min_changed(this);};
	document.getElementById(this.id_minPrice).onclick =function(){this.searchConditionController.search_price_min_clicked(this);};

	document.getElementById(this.id_maxPrice).searchConditionController = this;
	document.getElementById(this.id_maxPrice).onchange =function(){this.searchConditionController.search_price_max_changed(this);};
	document.getElementById(this.id_maxPrice).onclick =function(){this.searchConditionController.search_price_max_clicked(this);};
	
	document.getElementById(this.id_keyword).searchConditionController = this;
	document.getElementById(this.id_keyword).onfocus =function(){this.searchConditionController.clearDefaultValue(this,"keyword");};

	
}

SearchConditionController.prototype.search_price_min_clicked = function (source){
	if(source.value.match(source.searchConditionController.search_price_minDefaultValue)){
		source.value = "";	
	}
}

SearchConditionController.prototype.search_price_max_clicked = function(source){
	if(source.value.match(source.searchConditionController.search_price_maxDefaultValue)){
		source.value = "";	
	}
}

SearchConditionController.prototype.search_price_select_changed = function(source, searchConditionController){
	if(source.value == searchConditionController.valueOfOtherPrice){
		controlDisplayForDIVObject(document.getElementById(searchConditionController.id_priceInputArea), true);
	}else{
		controlDisplayForDIVObject(document.getElementById(searchConditionController.id_priceInputArea), false);
	}
}

SearchConditionController.prototype.search_price_min_changed = function (source){
	isNumberInputed(source, true, true);
}

SearchConditionController.prototype.search_price_max_changed = function (source){
	isNumberInputed(source, true, true);
}

SearchConditionController.prototype.getDefaultValue = function(index){
	var value;
	var source = document.getElementById(index);
	switch (index)
	{
	case "Search_Keyword": value = this.keywordDefaultValue; 
		break;
	case "search_price_min": value = this.search_price_minDefaultValue; 
		break;
	case "search_price_max": value = this.search_price_maxDefaultValue; 
		break;
	}
	source.value=value;
}

SearchConditionController.prototype.clearDefaultValue = function(source, index){
	var value;
	switch (index)
	{
	case "keyword": value = this.keywordDefaultValue; 
		break;
	}
	if(typeof(source)=="object"){
	 if(source.value == value){
		source.value='';
	 }
	}
}
var xmlRequest;
SearchConditionController.prototype.searchSubmit = function(){
	xmlRequest=GetXmlHttpObject();
	if (xmlRequest==null)
	{
		alert ("Browser does not support HTTP Request");
	} 
	var tmpId = "";
	var tmpValue = "";
	var inputStr = "";
	var search_params = null;
	search_params = new Array("Search_Address_County","Search_Area","Search_Size","Search_Type","Search_Price","Search_Keyword",
								"Search_Address","Search_School","Search_ShoppingArea","Search_MRTLine","Search_MRTStart","Search_MRTEnd",
								"search_price_min", "search_price_max");
	for (key in search_params) {
		tmpId = document.getElementById(search_params[key]);
		//Search_Address 已不再使用 2008/6/17
		if (tmpId == null ) {
			if(search_params[key]=="Search_Address"){
				tmpValue = "";
				inputStr = inputStr + search_params[key] + "=" + tmpValue + "&";
			}else{
				alert("quickSearch() element not found: "+search_params[key]);
			}
		} else {
			tmpValue = tmpId.value;
			inputStr = inputStr + search_params[key] + "=" + tmpValue + "&";
		}
	}

	var url="?plugins=quick-search&action=init-conditions";
	xmlRequest.onreadystatechange=this.getInitConditionResult;
	xmlRequest.open("POST",url,true,"","");
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlRequest.send(inputStr);		
}

SearchConditionController.prototype.getInitConditionResult = function(){
	if (xmlRequest.readyState==4 || xmlRequest.readyState=="complete")
	{
		var xmlDoc = xmlRequest.responseText;
		window.location.href= "?plugins=quick-search";
	}

}

SearchConditionController.prototype.mapSearchSubmit = function(clickArrangeBtn){
    var k3ReturnDefaultvalue = false;
	try{
		var keyword3 = document.getElementById(this.id_keyword);
		if (keyword3.value == this.keywordDefaultValue){
			keyword3.value = "";
			k3ReturnDefaultvalue= true;
		}
	}
	catch(e){
		alert(e);
	}
	xmlRequest=GetXmlHttpObject();
	if (xmlRequest==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	var tmpId = "";
	var tmpValue = "";
	var inputStr = "";
	var search_params = new Array("Search_Address_County","Search_Area","Search_Size","Search_Type","Search_Price","Search_Keyword",
								"Search_Address","Search_School","Search_ShoppingArea","Search_MRTLine","Search_MRTStart","Search_MRTEnd",
								"search_price_min", "search_price_max");
	for (key in search_params) {
		tmpId = document.getElementById(search_params[key]);
		//Search_Address 已不再使用 2008/6/17
		if (tmpId == null ) {
			if(search_params[key]=="Search_Address"){
				tmpValue = "";
				inputStr = inputStr + search_params[key] + "=" + tmpValue + "&";
			}else{
				alert("quickSearchAjax() element not found: "+search_params[key]);
			}
		} else {
			tmpValue = tmpId.value;
			inputStr = inputStr + search_params[key] + "=" + tmpValue + "&";
		}
	}
	if(thesouthwest_lat != ""){
		inputStr = inputStr + "southwest_lat=" + thesouthwest_lat + "&southweat_lng=" + thesouthweat_lng + "&northeast_lat=" + thenortheast_lat + "&northeast_lng=" + thenortheast_lng;
	}
	var url="?plugins=quick-search&action=map-quick-search";
	xmlRequest.onreadystatechange=getMapQuickSearchResults;
	xmlRequest.open("POST",url,true,"","");
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlRequest.send(inputStr);	
	if(clickArrangeBtn){
		changeBtnToMovingMap();
	}
	
	if(k3ReturnDefaultvalue){
		this.getDefaultValue("Search_Keyword");
	}
	showLoadingHouseMessage("搜尋中.....");
}

SearchConditionController.prototype.initial = function(){
	document.getElementById(this.id_houseSearchMapSearchBtn).searchConditionController = this;
	document.getElementById(this.id_houseSearchMapSearchBtn).onclick = function(){this.searchConditionController.mapSearchSubmit(false)};
	
	document.getElementById(this.id_county).searchConditionController = this;
	document.getElementById(this.id_county).onchange =function(){
			this.searchConditionController.changeSelectObjectDataForCountyChange(this,this.searchConditionController);
			if(this.searchConditionController.usingMapSearch){				
				this.searchConditionController.locateToMap(document.getElementById(this.searchConditionController.id_county), document.getElementById(this.searchConditionController.id_administrativeArea));
				deleteSelectedArea();
			}
		};

	document.getElementById(this.id_administrativeArea).searchConditionController = this;
	document.getElementById(this.id_administrativeArea).onchange =function(){
			if(this.searchConditionController.usingMapSearch){
				this.searchConditionController.locateToMap(document.getElementById(this.searchConditionController.id_county), document.getElementById(this.searchConditionController.id_administrativeArea));
				deleteSelectedArea();
			}
		};
		
	this.searchForm = document.getElementById(this.id_houseSearchForm);
	BuildFixedSelectOptions(document.getElementById(this.id_county), predefindCity, false, true, "請選擇");
	BuildFixedSelectOptions(document.getElementById(this.id_mrtLine), predefindMRTLines, false, true, "請選擇");
	this.getDefaultValue("Search_Keyword");
	this.getDefaultValue("search_price_min");
	this.getDefaultValue("search_price_max");
	this.adaptEvntHandlerForPartitionDiv();
	this.adaptEvntHandlerForActionBtn();
	this.adaptEvntHandlerSearchBtn();

}

SearchConditionController.prototype.searchUsingCounty = function(county){
	if(this.usingMapSearch){
	//	theGMap.currentMapLevel = 12;
	//	theGMap.showLocation(county);
		document.getElementById(this.id_county).value = county;
		launchAnEvent(document.getElementById(this.id_county), "change");
		this.mapSearchSubmit(false);
		return;
	}
	xmlRequest=GetXmlHttpObject();
	if (xmlRequest==null)
	{
		alert ("Browser does not support HTTP Request");
	} 
	var inputStr = "Search_Address_County=" + county;
	var url="?plugins=quick-search&action=init-conditions";
	xmlRequest.onreadystatechange=this.getInitConditionResult;
	xmlRequest.open("POST",url,true,"","");
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlRequest.send(inputStr);
}

SearchConditionController.prototype.searchUsingCountyCity = function(county, city){
	if(this.usingMapSearch){
	//	theGMap.currentMapLevel = 12;
	//	theGMap.showLocation(county);
		document.getElementById(this.id_county).value = county;
		launchAnEvent(document.getElementById(this.id_county), "change");
		document.getElementById(this.id_administrativeArea).value = city;
		launchAnEvent(document.getElementById(this.id_administrativeArea), "change");
		this.mapSearchSubmit(false);
		return;
	}
	xmlRequest=GetXmlHttpObject();
	if (xmlRequest==null)
	{
		alert ("Browser does not support HTTP Request");
	} 
	var inputStr = "Search_Address_County=" + county + "&" + "Search_Area=" + city;
	var url="?plugins=quick-search&action=init-conditions";
	xmlRequest.onreadystatechange=this.getInitConditionResult;
	xmlRequest.open("POST",url,true,"","");
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlRequest.send(inputStr);
}

SearchConditionController.prototype.setInitalConditions = function(conditions){
	this.setInitalConditionsForLocation(conditions);
	this.setInitalConditionsForPostRelated(conditions);
	this.setInitalConditionsForHouseRelated(conditions);
}

SearchConditionController.prototype.setInitalConditionsForLocation = function(conditions){
	if(conditions.county != ""){
		theGMap.currentMapLevel = 12;
		document.getElementById(this.id_county).value= conditions.county;
		this.changeSelectObjectDataForCountyChange(document.getElementById(this.id_county), this);
		var address = conditions.county;
		if(conditions.city != ""){
			theGMap.currentMapLevel = 13;	
			document.getElementById(this.id_administrativeArea).value= conditions.city;
			address += conditions.city;
		}
		theGMap.showLocation(address);	
	}
}

SearchConditionController.prototype.setInitalConditionsForPostRelated = function(conditions){
	document.getElementById(this.id_price).value= conditions.price;
	launchAnEvent(document.getElementById(this.id_price), "change");
	document.getElementById(this.id_minPrice).value= conditions.priceMin;
	document.getElementById(this.id_maxPrice).value= conditions.priceMax;
	document.getElementById(this.id_keyword).value= conditions.keywords;
	
}

SearchConditionController.prototype.setInitalConditionsForHouseRelated = function (conditions){
	document.getElementById(this.id_houseSize).value= conditions.size;
	document.getElementById(this.id_houseType).value= conditions.houseType;
	document.getElementById(this.id_shoppingArea).value= conditions.shoppingArea;
	document.getElementById(this.id_school).value= conditions.school;
	document.getElementById(this.id_mrtLine).value= conditions.mrtLine;
	getMRTStationsForALine(conditions.mrtLine, document.getElementById(this.id_mrtStationStart), true, "");
	getMRTStationsForALine(conditions.mrtLine, document.getElementById(this.id_mrtStationEnd), true, "");
	document.getElementById(this.id_mrtStationStart).value= conditions.mrtStationStart;
	document.getElementById(this.id_mrtStationEnd).value= conditions.mrtStationEnd;
	document.getElementById(this.id_school).value= conditions.school;	
}

SearchConditionController.prototype.locateToMap = function (county, city){
	var theLevel = 16;
	var selectedCounty = county.value;
	var selectedCity =  city.value;
	var thelocateAddres = selectedCounty + selectedCity;
	if(selectedCity == ""){
		 theLevel = 13; 
	}
	if(theGMap){
		theGMap.currentMapLevel = theLevel;
		theGMap.showLocation(thelocateAddres);	
		theGMap.afterAsynRequestDone = function(done, count, marker){
			if(done){
				theGMap.removeOverlay(marker);
			}
		}
	}
}

