function Step5_PrepareAllRoomsData(button) {
	/*if(button.hasClass('no-checked')) {
		var MainBox = jQuery('div#room-forms');
		MainBox.find('div.one-room-order-form').each(function(i) {
			var CurrentRoom = i+1;
		});
		return false;
	} else {
		return true;
	}*/
}

function Step5_UpdateRoomInputsNamesByInc() {
	var MainBox = jQuery('div#room-forms');
	var Counter = jQuery('input[name=all_room_count]');
	var Count = 1;
	MainBox.find('div.one-room-order-form').each(function(i) {
		var CurrCount = i+1;
		var list = jQuery(this).find('select.room_drop_list');
		list.attr('name', 'rooms_list_' + CurrCount);
		list.change(function() { Step5_RoomDropListIsChange(jQuery(this), CurrCount); });
		Count++;
	});
	Counter.attr('value', parseInt(Count-1));
	jQuery('span#display-total-rooms').html(parseInt(Count-1));
}

function Step5_ShowHideDeleteLink() {
	var MainBox = jQuery('div#room-forms');
	var divBox = MainBox.find('div.one-room-order-form');
	divBox.each(function() { jQuery(this).find('a.x-delete').css('display', 'none'); });
	divBox.each(function(i) {
		if(i == (divBox.length-1)) { jQuery(this).find('a.x-delete').fadeIn('slow'); }
	});
}

function Step5_DeleteAnotherRoom(link) {
	link.css('display', 'none');
	var MainBox = link.parent().parent().parent();
	MainBox.fadeTo('slow', 0.1, function() {
		jQuery(this).slideUp('slow', function() {
			jQuery(this).remove();
			Step5_UpdateRoomInputsNamesByInc();
			Step5_ShowHideDeleteLink();
		});
	});
}

function Step5_AddAnotherRoom() {
	var Shablon = '';
	var MainBox = jQuery('div#room-forms');
	var Counter = jQuery('input[name=all_room_count]');
	Counter.attr('value', (parseInt(Counter.attr('value'))+1));
	
	
	Shablon += '<div class="one-room-order-form" style="display: none;">';
	Shablon += '	<div style="padding-top: 10px;">';
	Shablon += '		<h1 style="margin: 0px; padding: 0px; text-align: left; float: left; font-size: 19px; width: 78px; margin-left: 14px;">Room</h1>';
	Shablon += '		<select OnChange="Step5_RoomDropListIsChange(jQuery(this), ' + Counter.attr('value') + ');" class="room_drop_list" name="rooms_list_' + Counter.attr('value') + '" style="float: left; width: 260px; margin-right: 20px;">';
	Shablon += '			<option value="0"></option>';
	Shablon += '		</select>';
	Shablon += '		<div style="float: left;">';
	Shablon += '			<a class="x-delete" href="#JavaScript" OnClick="Step5_DeleteAnotherRoom(jQuery(this)); return false;">Delete</a>';
	Shablon += '		</div>';
	Shablon += '		<div class="clear"><!--Clear--></div>';
	Shablon += '	</div>';
	Shablon += '	<div>';
	Shablon += '		<div style="height: 10px;"><!--Null--></div>';
	Shablon += '		<table border="0" style="margin-left: 9px;">';
	Shablon += '			<tr>';
	Shablon += '				<td width="131">';
	Shablon += '					<b>First Name:</b>';
	Shablon += '				</td>';
	Shablon += '				<td width="130">';
	Shablon += '					<b>Last Name:</b>';
	Shablon += '				</td>';
	Shablon += '				<td width="31">';
	Shablon += '					<b>Age:</b>';
	Shablon += '				</td>';
	Shablon += '				<td>';
	Shablon += '					<b>Package:</b>';
	Shablon += '				</td>';
	Shablon += '			</tr>';
	Shablon += '		</table>';
	Shablon += '		<div class="add-form-inputs"><!--Null--></div>';
	Shablon += '	</div>';
	Shablon += '	<div style="height: 20px;"><!--Null--></div>';
	Shablon += '</div>';
	
	MainBox.append(Shablon);
	
	MainBox.find('div.one-room-order-form').each(function(i) {
		if(i == (parseInt(Counter.attr('value'))-1)) {
			
			jQuery(this).fadeTo(1, 0.1, function() {
				jQuery(this).css('display', 'block');
				jQuery(this).fadeTo('slow', 1.0, function() {
					jQuery('span#display-total-rooms').html(parseInt(Counter.attr('value')));
				});
			});
			
			var list = jQuery(this).find('select.room_drop_list');
			Step5_UpdateRoomsList(list, function() { Step5_UpdateRoomInputs(list, false, parseInt(Counter.attr('value'))); });
		}
	});
	Step5_ShowHideDeleteLink();
}

/*function Step5_SelectPrevValue(list, prev_value) {
	list.find('option').each(function() {
		if(jQuery(this).attr('value') == prev_value) {
			jQuery(this).attr('selected', 'selected');
		}
	});
}*/
 
function Step5_UpdateTourTypeThisList(room_id, age, list_to_update) {
	var prev_value = list_to_update.attr('value');
	list_to_update.html('<option>Loading...</option>');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-rooms.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_tour_types_for_this_age&age=' + age + '&room_id=' + room_id,
		success: function(data) {
			if(data != '0') {
				list_to_update.html(data);
				//Step5_SelectPrevValue(list_to_update, prev_value);
			} else {
				list_to_update.html('<option>Error!</option>');
			}
		}
	});
}

function Step5_UpdateTourTypePriceFromAge(input_age) {
	var room_id_list = input_age.parent().parent().parent().parent().parent().parent().parent().parent().find('select.room_drop_list');
	var tour_type_list = input_age.parent().parent().find('select');
	Step5_UpdateTourTypeThisList(room_id_list.attr('value'), input_age.attr('value'), tour_type_list);
}

function Step5_AgeIsChange(input) {
	if((input.attr('value') >= 1) & (input.attr('value') <= 120)) {
		input.css('color', 'green');
		if(input.hasClass('error')) { input.removeClass('error'); }
		Step5_UpdateTourTypePriceFromAge(input);
	} else {
		input.css('color', 'red');
		if(!input.hasClass('error')) { input.addClass('error'); }
	}
	//alert(input.attr('value'));
}

function Step5_UpdateRoomsList(list, callBack) {
	list.html('<option>Loading...</option>');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-rooms.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_room_list',
		success: function(data) {
			if(data != '0') {
				list.html(data);
			} else {
				list.html('<option>Not Found!</option>');
			}
			if(callBack) { callBack(); }
		}
	});
}

function Step5_UpdateRoomInputs(list, callBack, level) {
	var MainBlock = list.parent().parent();
	var InputsBlock = MainBlock.find('div.add-form-inputs');
	InputsBlock.html('<div style="margin-left: 11px;"><br /><b>Loading...</b></div>');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-rooms.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&action=get_room_inputs&room_id=' + list.attr('value') + '&level=' + level,
		success: function(data) {
			if(data != '0') {
				InputsBlock.html(data);
				InputsBlock.find('input:first').focus();
			} else {
				InputsBlock.html('<div style="margin-left: 11px;"><br /><b>Error!</b></div>');
			}
			if(callBack) { callBack(); }
		}
	});
}

function Step5_RoomDropListIsChange(list, level) {
	Step5_UpdateRoomInputs(list, false, level);
}

jQuery(document).ready(function() {
	var list = jQuery('select.room_drop_list');
	Step5_UpdateRoomsList(list, function() { Step5_UpdateRoomInputs(list, false, 1); });
});
