var complexOptions = new Array();
var cityOptions = new Array();
var base_url = '';

function getSelectValue( selectNode )
{
	return selectNode.options[selectNode.selectedIndex].value
}

function showCities(){
	var state = getSelectValue(document.getElementById('state_options'));

	AjaxRequest.get(
			{
				'url':base_url+'/review/ajax_getCityOptions.jsp?state='+state,
				'onSuccess':function(req){ 
								try{
									eval('cityOptions = '+ req.responseText);
								}catch(err){cityOptions = false; complexOptions = false;}
									populateSelect(document.getElementById('city_options'));
									showComplexes();
								
							}
			}
		);
}

function showComplexes(){
	var city = getSelectValue(document.getElementById('city_options'));
	var state = getSelectValue(document.getElementById('state_options'));
	if(city == ''){ populateSelect(document.getElementById('city_options')); return;}
	AjaxRequest.get(
			{
				'url':base_url+'/review/ajax_getComplexNameOptions.jsp?state='+state+'&city='+city,
				'onSuccess':function(req){ eval('complexOptions = '+ req.responseText);
								populateComplexes(document.getElementById('complex_options'));
							}
			}
		);
}

function getInfo(){

	//selectNode.selectedIndex
	var iOption = document.getElementById('complex_options').selectedIndex;
	if(iOption == 0){ populateInfo('','');  return;}
	populateInfo(complexOptions[iOption-1].complex_address1, complexOptions[iOption-1].complex_phone, complexOptions[iOption-1].complex_id);
}

function showComplex(complex){

	var city = getSelectValue(document.getElementById('city_options'));
	var state = getSelectValue(document.getElementById('state_options'));
	if(city == ''){ populateSelect(document.getElementById('city_options')); return;}
	AjaxRequest.get(
			{
				'url':base_url+'/review/ajax_getComplexNameOptions.jsp?state='+state+'&city='+city,
				'onSuccess':function(req){ eval('complexOptions = '+ req.responseText);
								populateComplexes(document.getElementById('complex_options'));
								document.getElementById(complex).selected = true;
								getInfo();
							},
				'onFailure':function(req){	alert();
					
				}
			}
		);
}

function populateComplexes(selectInput){
	var mySelect = document.getElementById('complex_options');
	mySelect.disabled = true;
	mySelect.innerHTML = "";	
	//alert(list);	
	mySelect.options[0] = new Option('Select Apartment Name','');
	if(!complexOptions || complexOptions.length == 0) return;
	for(i = 0; i < complexOptions.length; i++){

		mySelect.options[i+1] = new Option( complexOptions[i].complex_name, complexOptions[i].complex_name);	
		mySelect.options[i+1].id = complexOptions[i].complex_name;
	}
	mySelect.disabled = false;
	populateInfo('','');
	//getInfo();
	
}

function populateSelect(selectInput){
	var mySelect = document.getElementById('city_options');
	mySelect.disabled = true;
	selectInput.innerHTML = "";	

	mySelect.options[0] = new Option('Select City','');
	if(!cityOptions || cityOptions.length == 0) return;
	for(i = 0; i < cityOptions.length; i++){

		mySelect.options[i+1] = new Option( cityOptions[i].city, cityOptions[i].city);
		mySelect.options[i+1].id = cityOptions[i].city;
	}
	mySelect.disabled = false;
	
	populateComplexes(selectInput);
	document.getElementById('complex_options').disabled = true;

}

function populateInfo(address1, phone, complex_id){

	document.getElementById("complex_address1").value = address1;
	document.getElementById("complex_phone").value = phone;
	document.getElementById("complex_id").value = complex_id;
}
