quest training_mount begin
	state start begin
		function check_list(material, pc_level, horse_level)
			if pc.count_item(material) <= 0 then 
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._02_say)
				return 0
			elseif pc.level <  pc_level then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._01_say) 
				return 0
			elseif 	horse.get_level()< horse_level then
				say_title(""..mob_name(20349).."")
				say(string.format(gameforge.training_mount._33_say,horse_level)) 
				return 0
			else
				return 1
			end
		end
		
		function extraTime(typ)
			local n = number (1,100)
			if typ == 1 then
				if n <= 50 then
					return 86400
				elseif n <= 80 then
					return 86400*2
				elseif n <=95 then
					return 86400*3
				else
					return 86400*4
				end
			elseif typ == 2 then
				if n <= 30 then
					return 0
				elseif n <= 70 then
					return 86400
				elseif n <=90 then
					return 86400*2
				else
					return 86400*3
				end
			end
		end
		
		when 20349.chat.gameforge.training_mount._04_say begin
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._05_say)
			say("")
			wait()
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._06_say)
			say("")
			wait()
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._07_say)
			say("")
			set_state(main_point)
		end
	end

	state main_point begin
		when 20349.chat.gameforge.training_mount._08_say begin
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._09_say)
			say("")
			wait()
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._10_say)
			say("")
			wait()
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._11_say)

			local s=select(gameforge.training_mount._12_say,gameforge.training_mount._13_say)

			if 2==s then
				return
			end
			if pc.count_item(50067) >0  or pc.count_item(50068)>0 then 
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._14_say)
			else 
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._02_say)
				return
			end

			local s=select(gameforge.training_mount._15_say,gameforge.training_mount._15_2_say,gameforge.training_mount._16_say)
			if 1==s  then 
				if training_mount.check_list(50067,25,11) == 1 then 
					say_title(""..mob_name(20349).."")
					say(gameforge.training_mount._17_say)
				end
			elseif 2==s   then
				if training_mount.check_list(50068,45,20) == 1 then 
					say_title(""..mob_name(20349).."")
					say(gameforge.training_mount._18_say)
				end
			elseif 3==s then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._19_say)
				say("")
				return
			end
		end --when


		when 20349.take with 
                52001 <= item.vnum and item.vnum <= 52005 or 
                52016 <= item.vnum and item.vnum <= 52020 or 
                52031 <= item.vnum and item.vnum <= 52035 or 
                52046 <= item.vnum and item.vnum <= 52050 or 
                52061 <= item.vnum and item.vnum <= 52065 or 
                52076 <= item.vnum and item.vnum <= 52080 or 
                52091 <= item.vnum and item.vnum <= 52095 begin
			if pc.is_riding() == true then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._32_say)
				return
			end
			if count_item_range(38100, 38102) >= 1 then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._34_say)
				local choice = select(gameforge.training_mount._35_select_1, gameforge.training_mount._35_select_2, locale.cancel)
				if choice == 3 then
					return
				elseif choice == 1 then
					local option_select = select(gameforge.training_mount._36_select_1, gameforge.training_mount._36_select_2, gameforge.training_mount._36_select_3, locale.cancel)
					local information = {}
					if option_select == 4 then
						return
					end
					if option_select == 1 then
						if pc.count_item(38100) >= 1 then
							information = {86400*7,38100}
						else
							say_title(""..mob_name(20349).."")
							say(gameforge.training_mount._37_say)
							return
						end
					elseif option_select == 2 then
						if pc.count_item(38101) >= 1 then
							information = {86400*15,38101}
						else
							say_title(""..mob_name(20349).."")
							say(gameforge.training_mount._37_say)
							return
						end
					elseif option_select == 3 then
						if pc.count_item(38102) >= 1 then
							information = {86400*30, 38102}
						else
							say_title(""..mob_name(20349).."")
							say(gameforge.training_mount._37_say)
							return
						end
					end
					local remain_time =item.get_socket(0)
					item.set_socket(0,remain_time+information[1])
					pc.remove_item(information[2],1)
					return
				end
			end
			if training_mount.check_list(50067,25,11) == 1 then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._20_say)
				wait()
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._22_say)
				local remain_time =item.get_socket(0)
				local old_vnum =item.get_vnum()
				local new_vnum =item.get_vnum()+5
				local extra = training_mount.extraTime(1)
				pc.give_item2_select(new_vnum,1)
				item.set_socket(0,remain_time+extra)
				pc.remove_item(old_vnum,1)
				pc.remove_item(50067,1)
			end
		end

		when 20349.take with 52006 <= item.vnum and item.vnum <= 52010 or 
							52021 <= item.vnum and item.vnum <= 52025 or 
							52036 <= item.vnum and item.vnum <= 52040 or 
							52051 <= item.vnum and item.vnum <= 52055 or 
							52081 <= item.vnum and item.vnum <= 52085 or 
							52066 <= item.vnum and item.vnum <= 52070 or
							52096 <= item.vnum and item.vnum <= 52100 begin
			if pc.is_riding() == true then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._32_say)
				return
			end
			
			if count_item_range(38100, 38102) >= 1 then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._34_say)
				local choice = select (gameforge.training_mount._35_select_1, gameforge.training_mount._35_select_2, locale.cancel)
				if choice == 3 then
					return
				elseif choice == 1 then
					local option_select = select(gameforge.training_mount._36_select_1, gameforge.training_mount._36_select_2, gameforge.training_mount._36_select_3, locale.cancel)
					local information = {}
					if option_select == 4 then
						return
					end
					if option_select == 1 then
						if pc.count_item(38100) >= 1 then
							information = {86400*7,38100}
						else
							say_title(""..mob_name(20349).."")
							say(gameforge.training_mount._37_say)
							return
						end
					elseif option_select == 2 then
						if pc.count_item(38101) >= 1 then
							information = {86400*15,38101}
						else
							say_title(""..mob_name(20349).."")
							say(gameforge.training_mount._37_say)
							return
						end
					elseif option_select == 3 then
						if pc.count_item(38102) >= 1 then
							information = {86400*30, 38102}
						else
							say_title(""..mob_name(20349).."")
							say(gameforge.training_mount._37_say)
							return
						end
					end
					
					local remain_time =item.get_socket(0)
					item.set_socket(0,remain_time+information[1])
					pc.remove_item(information[2],1)
					return
				end
			end
			if training_mount.check_list(50068,45,20) == 1 then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._21_say)
				wait()
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._22_say)
				local remain_time =item.get_socket(0)
				local old_vnum =item.get_vnum()
				local new_vnum =item.get_vnum()+5
				local extra = training_mount.extraTime(1)
				pc.give_item2_select(new_vnum,1)
				item.set_socket(0,remain_time+extra)
				pc.remove_item (old_vnum,1)
				pc.remove_item (50068,1)
				
			end
		end

		when 20349.take with 52011 <= item.vnum and item.vnum <= 52015 or 
                            52026 <= item.vnum and item.vnum <= 52031 or
                            52041 <= item.vnum and item.vnum <= 52045 or
                            52056 <= item.vnum and item.vnum <= 52061 or
                            52086 <= item.vnum and item.vnum <= 52091 or
                            52071 <= item.vnum and item.vnum <= 52075 or
                            52101 <= item.vnum and item.vnum <= 52105 
                            begin
			if pc.is_riding() == true then
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._32_say)
				return
			end
			
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._38_say)
			local choice = select (gameforge.training_mount._35_select_1, locale.cancel)
			if choice == 2 then
				return
			elseif choice == 1 then
				local option_select = select(gameforge.training_mount._36_select_1, gameforge.training_mount._36_select_2, gameforge.training_mount._36_select_3, gameforge.training_mount._36_select_4, locale.cancel)
				local information = {}
				if option_select == 5 then
					return
				end
				if option_select == 1 then
					if pc.count_item(38100) >= 1 then
						information = {86400*7,38100}
					else
						say_title(""..mob_name(20349).."")
						say(gameforge.training_mount._37_say)
						return
					end
				elseif option_select == 2 then
					if pc.count_item(38101) >= 1 then
						information = {86400*15,38101}
					else
						say_title(""..mob_name(20349).."")
						say(gameforge.training_mount._37_say)
						return
					end
				elseif option_select == 3 then
					if pc.count_item(38102) >= 1 then
						information = {86400*30, 38102}
					else
						say_title(""..mob_name(20349).."")
						say(gameforge.training_mount._37_say)
						return
					end
				elseif option_select == 4 then
					local remain_time = item.get_socket(0)
					local run_time = remain_time-get_time()
					if (remain_time-get_time()) > 86400 then
						say_title(""..mob_name(20349).."")
						say(gameforge.training_mount._39_say)
						if  is_test_server()  then
							item.set_socket(0, get_time() + 86460)
						end
						return
					end
					if pc.count_item(50068) >= 1 and pc.get_gold() >= 200000 then
						information = {training_mount.extraTime(2),50068}
						pc.changegold(-200000)
					else
						say_title(""..mob_name(20349).."")
						say(gameforge.training_mount._40_say)
						return
					end
				end
				
				local remain_time = item.get_socket(0)
				item.set_socket(0,remain_time+information[1])
				pc.remove_item(information[2],1)
				return
			end
			
		end
		
		when 20349.chat.gameforge.training_mount._23_say  begin
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._24_say)
			wait()
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._25_say)
			wait()
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._26_say)
			local s=select(locale.yes,locale.no)
			if 2==s then
				return
			end
			say_title(""..mob_name(20349).."")
			say(gameforge.training_mount._27_say)
			wait()
			if pc.count_item(50068) >0  then
				if horse.get_level()>1 then 
					say_title(""..mob_name(20349).."")
					say(gameforge.training_mount._28_say)
					wait()
					say(gameforge.training_mount._29_say)
				else 
					say_title(""..mob_name(20349).."")
					say(gameforge.training_mount._30_say)
					return
				end
			else
				say_title(""..mob_name(20349).."")
				say(gameforge.training_mount._31_say)
				return
			end	
		end
	end
end
