function GetType(){
	type_obj = document.getElementById('type');
	//if (type_obj != null){
		if (type_obj.options)
			var currType = type_obj.options[type_obj.selectedIndex].value;
		else
			var currType = type_obj.value;
	//}
	//else{
		//var currType = 'all';
	//}
	//alert(currType);
	return currType;
} 

function propTypeChange(){
	thisform=document.forms['frm_search'];

	thisform.elements['property_type'].options[0].selected=true;
	for (var i=thisform.elements['property_type'].length-1; i>0; i--) {
		thisform.elements['property_type'].options[i]=null ;
		}

	currIndex=thisform.elements['type'].selectedIndex;
	currType=thisform.elements['type'].options[currIndex].value;	

	if (currType=='sale')
		for (i=1;i<=option_for_sale_length;i++){
		thisform.elements['property_type'].options[i]=eval("option_for_sale_"+i);
		}
	else if (currType=='rent')
		for (i=1;i<=option_for_rent_length;i++){
			thisform.elements['property_type'].options[i]=eval("option_for_rent_"+i);
		}
	else	
		for (i=1;i<=option_for_all_length;i++){
			thisform.elements['property_type'].options[i]=eval("option_for_all_"+i);
		}
	;
	
	//countries refresh
	country_obj = document.getElementById('frm_country_id');
	currCountry=country_obj.options[country_obj.selectedIndex].value;
	country_obj.options.length = 0;
	o_count = 0;
	o_selected = 0;
	for (i=0;i<=listing_countries.length-1;i++)
    {
    	if (
	       		currType=='all' || 
	       		(currType=='sale' && listing_countries[i][2]>0) ||
	       		(currType=='rent' && listing_countries[i][3]>0)
	       	)
	       	
	    	{
		    	country_obj.options.length = o_count;
		    	if (listing_countries[i][0]==currCountry) o_selected=o_count;
				country_obj.options[o_count++] = new Option(listing_countries[i][1],listing_countries[i][0]);
			}
    };
    
    if(o_selected>0) country_obj.selectedIndex=o_selected;
	
	
	CountryChange();
}

function CountryChange(){
	country_obj = document.getElementById('frm_country_id');
	state_obj = document.getElementById('frm_state_id');
	city_obj = document.getElementById('city');
	state_obj.disabled = 0;
	
	currCountry = country_obj.options[country_obj.selectedIndex].value;
	currState = state_obj.options[state_obj.selectedIndex].value;	
	currType = GetType();

	if (currState.match(/(\d+),(\d+)/))
	{
		currState = RegExp.$1;
		currSubState = RegExp.$2;
	}
	else
	{
		currState = currState;
		currSubState = 0;
	}
		
	state_obj.options.length = 0;
	state_obj.options[0] = new Option("Any","0,0");
	
	
	if 	(currCountry == 0) {
		state_obj.disabled = 1;
	} else {
		o_count = 1;
		o_selected = 0;
		for (i=0;i<=listing_states.length-1;i++)
	    {
	    	if (
	    			listing_states[i][2]==currCountry	&& 
		       		(
		       			currType=='all' || 
		       			(currType=='sale' && listing_states[i][4]>0) ||
		       			(currType=='rent' && listing_states[i][5]>0)
		       		)
		       	)
		    	{
			    	state_obj.options.length = o_count;
			    	if (listing_states[i][0]==currState && listing_states[i][1]==currSubState) o_selected = o_count;
					state_obj.options[o_count++] = new Option(listing_states[i][3],listing_states[i][0]+','+listing_states[i][1]);
				}
	    };
	    if (o_selected>0) state_obj.selectedIndex = o_selected;
	    if (o_count==1) state_obj.disabled = 1;
	};
	
	StateChange();
}

function StateChange(){
	country_obj = document.getElementById('frm_country_id');
	state_obj = document.getElementById('frm_state_id');
	city_obj = document.getElementById('city');
	city_obj.disabled = 0;
	
	currState = state_obj.options[state_obj.selectedIndex].value;	
	currType = GetType();
	currCity = city_obj.options[city_obj.selectedIndex].value;
	currCountry = country_obj.options[country_obj.selectedIndex].value;
	
	if (currState.match(/(\d+),(\d+)/))
	{
		currState = RegExp.$1;
		currSubState = RegExp.$2;
	}
	else
	{
		currState = currState;
		currSubState = 0;
	}
	
	
	city_obj.options.length = 0;
	city_obj.options[0] = new Option("Any",0);	

	o_count = 1;
	o_selected = 0;
	for (i=0;i<=listing_cities.length-1;i++) 
	{
	    if (
	       		listing_cities[i][1]==currState && 
	       		listing_cities[i][4]==currCountry &&
	       		(
	       			currType=='all' || 
	       			(currType=='sale' && listing_cities[i][5]>0) ||
	       			(currType=='rent' && listing_cities[i][6]>0)
	       		)
	       	) 
				if (listing_cities[i][2]==currSubState||currSubState=='0') 
				{
	  			    city_obj.options.length = o_count;
	  			    if (listing_cities[i][0]==currCity) o_selected = o_count;
					city_obj.options[o_count++] = new Option(listing_cities[i][3],listing_cities[i][0]);
				}
		
	};
	if (o_selected>0) city_obj.selectedIndex = o_selected;
	if (o_count==1) city_obj.disabled = 1;
}

