/**
 * Registration form dropdowns
 */

function changeBuildings( selected, id, empty_value ) {
	
	var element = $( id );
	
	element.options.length = 0;
	$( 'building_index' ).value = 0;
	
	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}
	
	
	// make ajax call
	if ( selected && selected != 0 ) {
		getAvailableBuildings( id, empty_value );
	}
	
	
	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
	
	
	// reset floors if building or area is changed
	changeFloors( 0, 'floors_select', empty_value );
}


function getAvailableBuildings( id, empty_value ) {
	
	var room_count = $( 'reg_room_count' ).options[$( 'reg_room_count' ).selectedIndex].value;
	var area = $( 'reg_area' ).options[$( 'reg_area' ).selectedIndex].value;
	var interior = $( 'reg_interior' ).options[$( 'reg_interior' ).selectedIndex].value;
	
	var params = '';
	
	if ( room_count != 0 ) {
		params += 'room_count=' + room_count;
	}
	
	if ( area != 0 ) {
		params += '&area=' + area;
	}
	
	if ( interior != '' ) {
		params += '&interior=' + interior;
	}
	
	var ajaxRequest = new Ajax.Request(
		filter_villas_url, {
			method: 'get',
			parameters: params,
			onSuccess: function ( req ) {
				
				if( req.responseText && req.responseText != '' ) {
					
					var building_names = new String( req.responseText ).split( '|' );
					var iterator = 1;
					
					for ( var i in building_names ) {
						if ( building_names[i] && typeof building_names[i] == 'string' ) {
							$( id ).options[iterator++] = new Option( building_names[i].toUpperCase(), building_names[i] );
						}
					}
					
					if ( tmpBuildingIndex && tmpBuildingIndex != '' ) {
						$( id ).selectedIndex = tmpBuildingIndex;
						$( 'building_index' ).value = tmpBuildingIndex;
						tmpBuildingIndex = '';
						
						// display floors
						callChangeFloors();
					}
				}
				
				showHideTerrace();
			}
		}
	);
}


function changeFloors( selected, id, empty_value ) {
	
	var element = $( id );
	
	element.options.length = 0;
	$( 'floor_index' ).value = 0;
	
	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}
	
	
	// make ajax call
	if ( selected && selected != 0 ) {
		getAvailableFloors( id, empty_value );
	}
	
	
	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
}


function getAvailableFloors( id, empty_value ) {
	
	var room_count = $( 'reg_room_count' ).options[$( 'reg_room_count' ).selectedIndex].value;
	var area = $( 'reg_area' ).options[$( 'reg_area' ).selectedIndex].value;
	var building_name = $( 'buildings_select' ).options[$( 'buildings_select' ).selectedIndex].value;
	
	var params = '';
	
	if ( room_count != 0 ) {
		params += 'room_count=' + room_count;
	}
	
	if ( area != 0 ) {
		params += '&area=' + area;
	}
	
	if ( building_name != '' ) {
		params += '&building_name=' + building_name;
	}
	
	var ajaxRequest = new Ajax.Request(
		filter_floors_url, {
			method: 'get',
			parameters: params,
			onSuccess: function ( req ) {
				
				if( req.responseText && req.responseText != '' ) {
					
					var floors = new String( req.responseText ).split( '|' );
					var iterator = 1;
					
					for ( var i in floors ) {
						if ( floors[i] && typeof floors[i] == 'string' ) {
							$( id ).options[iterator++] = new Option( floors[i], floors[i] );
						}
					}
					
					if ( tmpFloorIndex && tmpFloorIndex != '' ) {
						$( id ).selectedIndex = tmpFloorIndex;
						$( 'floor_index' ).value = tmpFloorIndex;
						tmpFloorIndex = '';
					}
				}
				
				showHideTerrace();
			}
		}
	);
}


function changeArea( selected, id, empty_value ) {
	
	var element = $( id );
	var iterator = 1;
	
	element.options.length = 0;
	
	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}
	
	
	for ( var i in roomQuantities[selected] ) {
		
		if ( roomQuantities[selected][i] && typeof roomQuantities[selected][i] == 'string' ) {
			
			element.options[iterator] = new Option( roomQuantities[selected][i], i );
			iterator = iterator + 1;
		}
	}
	
	
	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
}


function changeInteriors( selected, id, empty_value ) {
	
	var element = $( id );
	var iterator = 1;
	
	element.options.length = 0;
	
	if( empty_value != '' ) {
		element.options[0] = new Option( empty_value, '' );
	}
	
	
	for ( var i in roomInteriorsRel[selected] ) {
		
		if ( roomInteriorsRel[selected][i] && typeof roomInteriorsRel[selected][i] == 'string' ) {
			
			element.options[iterator] = new Option( roomInteriorsRel[selected][i], i );
			iterator = iterator + 1;
		}
	}
	
	
	if( empty_value != '' ) {
		element.options[0].selected = true;
	} else {
		element.options[0] = null;
	}
}


function showHideTerrace() {
	
	var room_count = document.getElementById('reg_room_count').options[document.getElementById('reg_room_count').selectedIndex].value;
	var area = document.getElementById('reg_area').options[document.getElementById('reg_area').selectedIndex].value;
	var building = document.getElementById('buildings_select').options[document.getElementById('buildings_select').selectedIndex].value;
	var floor = document.getElementById('floors_select').options[document.getElementById('floors_select').selectedIndex].value;
	
	var params = '';
	
	if ( room_count != 0 ) {
		params += 'room_count=' + room_count;
	}
	
	if ( area != 0 ) {
		params += '&area=' + area;
	}
	
	if ( building != 0 ) {
		params += '&building=' + building;
	}
	
	if ( floor != 0 ) {
		params += '&floor=' + floor;
	}
	
	var ajaxRequest = new Ajax.Request(
		filter_terrace_url, {
			method: 'get',
			parameters: params,
			onSuccess: function ( req ) {
				
				if( req.responseText && req.responseText == 1 ) {
					$( 'terrace_block' ).style.display = 'block';
				} else {
					$( 'terrace_block' ).style.display = 'none';
					$( 'terrace_checkbox' ).checked = false;
				}
			}
		}
	);
}
