var purposeProperty = {'住宅':'jz','别墅':'jb','经济适用房':'jj','写字楼':'jx','商铺':'js'};
var arr_city = {'鞍山':'A 鞍山','保定':'B 保定','常州':'C 常州','东莞':'D 东莞','鄂尔多斯':'E 鄂尔多斯','佛山':'F 佛山','桂林':'G 桂林','合肥':'H 合肥','吉林':'J 吉林','昆山':'K 昆山','南宁':'N 南宁','泉州':'Q 泉州','日照':'R 日照','三亚':'S 三亚','台州': 'T 台州','乌鲁木齐':'W 乌鲁木齐','厦门':'X 厦门','宜昌':'Y 宜昌','中山':'Z 中山'};
var first_city = new Array('北京','上海','深圳','广州','重庆','天津','成都','武汉','南京','杭州','苏州');

//判断一个值是否在数组中
function in_array(needle, haystack) 
{
	var length_arr  = needle.length;
	for(i=0;i<length_arr;i++)
	{
	   if(haystack ==needle[i] )
	   {
		   return true;
	   }
	}
	return false;
}

function select_city()
{
  //城市处理
  var select_city =document.getElementById("city");
  //先处理前11个城市
 
  if(select_city.options.length < 2)
  { 
	 var city_num = 0;
	 var objOption=document.createElement("OPTION");
	 
	 for(city_num ; city_num<11;city_num++)
	 {      //alert(city_num);
	       var objOption=document.createElement("OPTION");
			select_city.options.add(objOption);
			city_f = first_city[city_num];
			 //alert(city_f);
			objOption.style.color ='#FF0000';
			objOption.text='　'+city_f;
			objOption.value=city_f;

	 }
     var d=0;   
     for (var city in SFNH.info)
     {   
		 if(city !='m')      //排除m，m为默认值
		 {  
				//关键
			
				if(in_array(first_city, city))
				{
					// alert(city);
					 continue;
				}
				var objOption=document.createElement("OPTION");
				select_city.options.add(objOption);
				if(city in arr_city)
				{
					 objOption.text=arr_city[city];
				}
				 else
				{
					objOption.text='　'+city;
				}
				objOption.value=city;
		}
     }
  }
}
//设置区县
function select_county()
{
  //清楚历史数据
  delselect('city');
  delselect('county');
  delselect('business');
  //清除商圈
 
  var s_city = document.getElementById("city").value;
  if(s_city=='')
  {
     //设置默认的价格范围(通过调用其方法)
     setprice();
	 return false;
  }
  //设置区县
  var arr_tmp =  SFNH.info[s_city]['q'];
  if(!arr_tmp)
  {
	 return false;
  }
  var i=0;
  var select_county =document.getElementById("county");
  for(i=0;i<arr_tmp.length;i++)
  {
    var objOption=document.createElement("OPTION");
	select_county.options.add(objOption);//关键
    objOption.text=arr_tmp[i];
    objOption.value=arr_tmp[i];
  }
  //设置价格
  setprice();
  l_type();
  //设置商圈
  return true;
}
//删除历史数据
function del(obj,type)
{
  try
   {  //ie
      var d = 0; 
      if(type=='price')
	  {
	    d = 2;
	  }
      for(var i=obj.options.length-1;i>d;i--) 
      {
          obj.options.remove(i);

      }
	}
	catch (E)
	{
	    //ff
		obj.options.length = 0;	
		var objOption=document.createElement("OPTION");
        obj.options.add(objOption);
		if(type=='county')
		{
          objOption.text='全部区县';
		}
		if(type=='business')
		{
          objOption.text='全部类型';
		}
		if(type == 'price')
		{
		  objOption.text='全部价格';
		  objOption.value='';
		  var objOption=document.createElement("OPTION");//添加横线
          obj.options.add(objOption);
		  objOption.style.color ='#E89B46';
		  objOption.text='-----------';
		  objOption.value='';

          var objOption=document.createElement("OPTION");//添加横线
          obj.options.add(objOption);
		  objOption.text='价格(元/㎡)';
		  objOption.value='';
		}
        objOption.value='';
	}
   return true;
}
function delselect(str)
{
  if(str =='city')
  {
    //当城市改变是需要注销区县和价格
    var select_price =document.getElementById("price");
	var select_county =document.getElementById("county");
    del(select_county,'county');
	del(select_price,'price');
  }
  //价格注销
  if(str =='county')
  {
    var select_price =document.getElementById("price");
	del(select_price,'price');
  }
  if( str == 'business')
  {
    var select_type =document.getElementById("type");
	del(select_type,'business');
  }
  return true;
}
function setprice()
{
  //设置价格范围
  var select_city =document.getElementById("city").value;
  
  var select_type =document.getElementById("type").value;
  //将类型转换成缩写字母
  var lei_py;
  if(select_type in purposeProperty)
  {
    lei_py = purposeProperty[select_type];
  }
  else
  {
    lei_py = '';
  }
  //alert(lei_py);
  //alert(select_city);
  //////////////////////////////////////////////////////////
  //特殊处理几个城市的经济适用房

  if(select_city == '苏州' && lei_py == 'jj' )
  {

     str = '2000-5000';
     var select_price =document.getElementById("price");
     var objOption=document.createElement("OPTION");
     select_price.options.add(objOption);//关键
     objOption.text='  '+str;
     objOption.value=str;
	 return true;
  }
   if(select_city == '广州' && lei_py == 'jj' )
  {

     str = '0-5000';
     var select_price =document.getElementById("price");
     var objOption=document.createElement("OPTION");
     select_price.options.add(objOption);//关键
     objOption.text='  '+str;
     objOption.value=str;
	 return true;
  }
   if(select_city == '深圳' && lei_py == 'jj' )
  {

     str = '0-6000';
     var select_price =document.getElementById("price");
     var objOption=document.createElement("OPTION");
     select_price.options.add(objOption);//关键
     objOption.text='  '+str;
     objOption.value=str;
	 return true;
  }
  if(select_city == '东莞' && lei_py == 'jj' )
  {

     str = '0-5000';
     var select_price =document.getElementById("price");
     var objOption=document.createElement("OPTION");
     select_price.options.add(objOption);//关键
     objOption.text='  '+str;
     objOption.value=str;
	 return true;
  }

//特殊处理结束
/////////////////////////////////////////////////////////////
  var arr_tmp_price = new Array();
  var arr_tmp_price_t = new Array();
  var type = false;
  //判断城市是否存在不存在则读取默认的值
  if(select_city=='')
  {
    arr_tmp_price = SFNH.info['m']['jz']; 
  }
  else
  {
    //选类型别墅
	
    if(lei_py=='jb' || lei_py == 'jx' || lei_py == 'js')
	{
		if(lei_py in SFNH.info[select_city])
		{
		  arr_tmp_price= SFNH.info[select_city][lei_py]['d'];
		  //添加套的读取
          //alert(lei_py);
		  if('t' in SFNH.info[select_city][lei_py])
		  {
		    arr_tmp_price_t = SFNH.info[select_city][lei_py]['t'];
		  }
		  if('z' in SFNH.info[select_city][lei_py])
		  {
		    arr_tmp_price_t = SFNH.info[select_city][lei_py]['z'];
		  }
		  if('y' in SFNH.info[select_city][lei_py])
		  {
		    arr_tmp_price_t = SFNH.info[select_city][lei_py]['y'];
			type = true;
		  }

		}
		else
		{
		  arr_tmp_price = false;
		  arr_tmp_price_t = false;
		}

	}
	//经济适用房
	if(lei_py == 'jj')
	{
	  arr_tmp_price = (lei_py in SFNH.info[select_city])?SFNH.info[select_city]['jj']:false;
	}
	//jx写字楼
     //js店铺 
	if(!arr_tmp_price || lei_py == '' || lei_py == 'jz')
	{
	  arr_tmp_price = SFNH.info[select_city]['jz'];
	  arr_tmp_price_t = false;
	  if(!arr_tmp_price)
	  {
	     arr_tmp_price = SFNH.info['m']['jz'];
	  }
	}
    //如果类型存在则读取
  }
  //价格更新
 var select_price =document.getElementById("price");
 var i=0,str;
 //价格计算
 for (i=0;i<=arr_tmp_price.length;i++)
 {
     str = '';
     if(i == 0)
	 {
	   str += '0—'+arr_tmp_price[i];
	 }
	 else
	 {
	     if(1 <= i && i < arr_tmp_price.length)
	    {
	      str += arr_tmp_price[i-1]+'—'+arr_tmp_price[i];
	    }
	    else
	    {
	     str += arr_tmp_price[i-1]+'以上';
	    }
	 }
	 //数据添加
     var objOption=document.createElement("OPTION");
     select_price.options.add(objOption);//关键
     objOption.text='  '+str;
     objOption.value=str;
 }
 //添加套价
 //alert(arr_tmp_price_t);
 if(arr_tmp_price_t && arr_tmp_price_t !='' )
 {
	 //添加横线（新加）
	var objOption=document.createElement("OPTION");//添加横线
    select_price.options.add(objOption);
	objOption.style.color ='#E89B46';
	objOption.text='----------------';
	objOption.value='';

    var s = '总价(万/套)';
	if(lei_py == 'jx' || lei_py == 'js')
	{
		if(type)
		{
		    s = '租价(元/㎡.月)';
		}
		else
		{
		   s = '租价(元/㎡.天)';
		}
	  
	}
	//var objoptgroup=document.createElement("optgroup");
   // objoptgroup.label=s;

    //select_price.appendChild(objoptgroup);
	 var objOption=document.createElement("OPTION");
      select_price.options.add(objOption);//关键
      objOption.text=s;
      objOption.value='';
	for (i=0;i<=arr_tmp_price_t.length;i++)
    {
	  //alert(i);
       str = '';
       if(i == 0)
	   {
	    str += '0—'+arr_tmp_price_t[i];
	   }
	   else
	   {
	     if(1 <= i && i < arr_tmp_price_t.length)
	    {
	      str += arr_tmp_price_t[i-1]+'—'+arr_tmp_price_t[i];
	    }
	    else
	    {
	     str += arr_tmp_price_t[i-1]+'以上';
	    }
	  }
	  //数据添加
      var objOption=document.createElement("OPTION");
      select_price.options.add(objOption);//关键
      objOption.text= '  '+str;
      objOption.value=str;
	}
 }
}
function select_type()
{
   //获取数据
   //注销价格
   delselect('county');
   setprice();
   return false;
}

//选择数据处理
function select_d_price(obj)
{
	d= obj.value;
	if(d=='' || d=='line')
	{
	   obj.options[0].selected="true";
	}
}

//设置类型选择
function l_type()
{
  var select_city =document.getElementById("city").value;
  var select_type =document.getElementById("type");
  var arr_tmp;
  if('l' in SFNH.info[select_city])
  {
   var arr_tmp =  SFNH.info[select_city]['l'];
  }
  else
  {
    var arr_tmp =  SFNH.info['m']['l'];
  }

  for (i=0;i<arr_tmp.length;i++)
  {
    var objOption=document.createElement("OPTION");
    select_type.options.add(objOption);//关键
    objOption.text= arr_tmp[i];
    objOption.value= arr_tmp[i];
  }  
  return true;
}