function init_jquery()
{
	$("#agent_cities").change(
		function()
		{
			var city = (this).value;
			$.getJSON(
				selfURL+'?page=get_agents', {'city' : city},
				function(data)
				{			
					
					$("#agents").empty();
					if (data.get_agents != "")
					{
						var k = 1;
						var str = "";
						$.each(data.get_agents, function(i,item)
							{							
								if (k == 1) str += "<tr>";
								str += "<td><b>"+item.name+"</b><br/>Phone: "+item.phone+"</td>";
								if (k == 3) {str += "</tr>"; k  = 0;}
								k++;
							}
						);
					}
					
					if (k == 2) str += "<td></td><td></td></tr>";
					else if (k == 3) str += "<td></td></tr>";
					
					$("#agents").append(str);				
				}
			);
			
		}
	)
	
	
	$("#add_other_photo").click(
		function()
		{
			other_photo_count++;
			$("#op_tbl").append("<tr id=\"op_row_"+other_photo_count+"\"><td>Large photo "+other_photo_count+"</td><td><div class=\"file_box\"><div id=\"div_other_photo_"+other_photo_count+"\" class=\"file_name\"></div><div title=\"Select File\" class=\"fakeButton\"></div><input id=\"file_other_photo_"+other_photo_count+"\" type=\"file\" name=\"other_photo_"+other_photo_count+"\" class=\"customFile\" onchange=\"HandleChanges('file_other_photo_"+other_photo_count+"','div_other_photo_"+other_photo_count+"');\"/><div></td><td rowspan=\"2\" style=\"vertical-align: middle;\"><img id=\"del_"+other_photo_count+"\" src=\"../resources/images/button-delete.gif\" alt=\"delete\" title=\"Delete Small and Large photo "+other_photo_count+"\" onmousemove=\"this.style.cursor='pointer'\" onclick=\"deleteTwoOtherPhoto('op_row_"+other_photo_count+"', 'small_op_row_"+other_photo_count+"')\"/></td></tr><tr id=\"small_op_row_"+other_photo_count+"\"><td>Small photo "+other_photo_count+"</td><td><div class=\"file_box\"><div id=\"div_small_other_photo_"+other_photo_count+"\" class=\"file_name\"></div><div title=\"Select file\" class=\"fakeButton\"></div><input id=\"file_small_other_photo_"+other_photo_count+"\" type=\"file\" name=\"small_other_photo_"+other_photo_count+"\" class=\"customFile\" onchange=\"HandleChanges('file_small_other_photo_"+other_photo_count+"','div_small_other_photo_"+other_photo_count+"');\"/></div></td></tr>");

			document.hotel.other_photo_count.value = other_photo_count;
			}
		)

		$("#add_timeslot").click(
		function()
		{
			var last_date_id = 'end_date_'+timeslot_count;
			var last_date = document.getElementById(last_date_id).value;
			var next_date = "";
			if (last_date) {
				var myregexp = /([0-9]{2})\/([0-9]{2})\/([0-9]{4})/;
				var _date = myregexp.exec(last_date);
				last_date = _date[2]+"/"+_date[1]+"/"+_date[3].substr(2);
				var converted = Date.parse(last_date) + 24*60*60*1000;
				next_date = new Date(converted);
				var _day = next_date.getDate().toString();
				var _month = (next_date.getMonth()+1).toString();
				var _year = next_date.getFullYear()+100;
				next_date = (_day.length < 2 ? ("0"+_day):_day)+"/"+(_month.length < 2 ? ("0"+_month):_month)+"/"+_year;
			}
			timeslot_count++;

			var myDate = new Date();
			var cur_year = myDate.getFullYear();
			var end_year = cur_year+3;
			var year_select = "";
			for (i=cur_year; i<=end_year; i++)
			{
				var str_year = new String(i);
				var short_year = str_year.substring(2,4);
				year_select += "<option value='"+i+"'>"+short_year+"</option>";
			}

			$("#timeslot_tbl").append("<tr id='timeslot_row_"+timeslot_count+"'><td style=\"width: 150px; vertical-align: middle;\">Availability timeslot #"+timeslot_count+"</td><td style=\"width: 500px;\"><input id=\"start_date_"+timeslot_count+"\" style=\"width: 70px;\" type=\"text\" name=\"timeslot["+timeslot_count+"][start_date]\" value=\""+next_date+"\" maxlength=\"16\" /> <img src=\"../resources/images/calendar.gif\" id=\"start_trigger_"+timeslot_count+"\" style=\"cursor: pointer; vertical-align: middle;\" title=\"Select a date\" alt=\"Select Date\"/><script type=\"text/javascript\">Calendar.setup({inputField : \"start_date_"+timeslot_count+"\", button : \"start_trigger_"+timeslot_count+"\", ifFormat : \"%d/%m/%Y\"});</script> <input id=\"end_date_"+timeslot_count+"\" style=\"width: 70px;\" type=\"text\" name=\"timeslot["+timeslot_count+"][end_date]\" value=\""+next_date+"\" maxlength=\"16\" /> <img src=\"../resources/images/calendar.gif\" id=\"end_trigger_"+timeslot_count+"\" style=\"cursor: pointer; vertical-align: middle;\" title=\"Select a date\" alt=\"Select Date\"/><script type=\"text/javascript\">Calendar.setup({inputField : \"end_date_"+timeslot_count+"\", button : \"end_trigger_"+timeslot_count+"\", ifFormat : \"%d/%m/%Y\"});</script> Price <input type='text' style='width: 50px;' name='timeslot["+timeslot_count+"][price]' value=''/> Price extra night <input type='text' style='width: 50px;' name='timeslot["+timeslot_count+"][extra_price]' value=''/></td><td style=\"vertical-align: middle;\"><img src='../resources/images/button-delete.gif' alt='delete' title='Delete Availability timeslot #"+timeslot_count+"' onmousemove='this.style.cursor=\"pointer\"' onclick='deleteTimeslot(\"timeslot_row_"+timeslot_count+"\", \"\")'/></td></tr>");

			document.hotel.timeslot_count.value = timeslot_count;

			}
		)

		return false;
}

//=================================================================================================
function deleteTwoOtherPhoto(op_id, small_op_id)
{
	$("#"+op_id).remove();
	$("#"+small_op_id).remove();
	other_photo_count--;

	document.hotel.other_photo_count.value = other_photo_count;
}

//=================================================================================================
function deleteTimeslot(timeslot_id, price_chart_id)
{
	if (price_chart_id)
	{
		if (confirm("The Availability timeslot will be deleted from the Data Base.\nAre you sure?") == true)
		{
			document.hotel.cmd.value = "timeslot_delete";
			document.hotel.delete_timeslot_id.value = price_chart_id;
			document.forms[0].submit();

		}
	}
	else
	{
		$("#"+timeslot_id).remove();
		timeslot_count--;
		if (!timeslot_count) {
			timeslot_count++;

			$("#timeslot_tbl").append("<tr id='timeslot_row_"+timeslot_count+"'><td style=\"width: 150px; vertical-align: middle;\">Availability timeslot #"+timeslot_count+"</td><td style=\"width: 500px;\"><input id=\"start_date_"+timeslot_count+"\" style=\"width: 70px;\" type=\"text\" name=\"timeslot["+timeslot_count+"][start_date]\" value=\"\" maxlength=\"16\" /> <img src=\"../resources/images/calendar.gif\" id=\"start_trigger_"+timeslot_count+"\" style=\"cursor: pointer; vertical-align: middle;\" title=\"Select a date\" alt=\"Select Date\"/><script type=\"text/javascript\">Calendar.setup({inputField : \"start_date_"+timeslot_count+"\", button : \"start_trigger_"+timeslot_count+"\", ifFormat : \"%d/%m/%Y\"});</script> <input id=\"end_date_"+timeslot_count+"\" style=\"width: 70px;\" type=\"text\" name=\"timeslot["+timeslot_count+"][end_date]\" value=\"\" maxlength=\"16\" /> <img src=\"../resources/images/calendar.gif\" id=\"end_trigger_"+timeslot_count+"\" style=\"cursor: pointer; vertical-align: middle;\" title=\"Select a date\" alt=\"Select Date\"/><script type=\"text/javascript\">Calendar.setup({inputField : \"end_date_"+timeslot_count+"\", button : \"end_trigger_"+timeslot_count+"\", ifFormat : \"%d/%m/%Y\"});</script> Price <input type='text' style='width: 50px;' name='timeslot["+timeslot_count+"][price]' value=''/> Price extra night <input type='text' style='width: 50px;' name='timeslot["+timeslot_count+"][extra_price]' value=''/></td><td style=\"vertical-align: middle;\"><img src='../resources/images/button-delete.gif' alt='delete' title='Delete Availability timeslot #"+timeslot_count+"' onmousemove='this.style.cursor=\"pointer\"' onclick='deleteTimeslot(\"timeslot_row_"+timeslot_count+"\", \"\")'/></td></tr>");
		}
		document.hotel.timeslot_count.value = timeslot_count;
	}
}

//=================================================================================================
function DeleteOtherPhoto(photo_id,photo_type,photo_num)
{
	if (confirm("The Large Photo will be deleted from the Data Base.\nAre you sure?") == true) {
		var hotel_id = $("#hotel_id").val();
		if (hotel_id) {
			$.ajax({
				type: "POST",
				dataType: "text",
				data: "hotel_id="+hotel_id+"&photo_type="+photo_type+"&photo_id="+photo_id,
				url: baseURL+"/index.php?page=del_photo",
				error: function() {
					alert("Sorry, the script was not executed. Please try again.");
				},
				success: function(data, textStatus){
					$("#img_"+photo_type+"_"+photo_num).remove();
					if (!document.getElementById("img_large_photo_"+photo_num) && !document.getElementById("img_small_photo_"+photo_num))
						document.getElementById("del_"+photo_num).style = "display: inline;";
				}
			});
		}
	}
}

//=================================================================================================

function confirm_delete(offer_id,photo_type) {
	if (confirm("Are you sure?") == true) {
		var offer_id = $("#offer_id").val();
		var city_id = $("#city_id").val();
		var hotel_id = $("#hotel_id").val();
		if (offer_id) {
			$.ajax({
				type: "POST",
				dataType: "text",
				data: "offer_id="+offer_id+"&photo_type="+photo_type,
				url: baseURL+"/index.php?page=del_photo",
				error: function() {
					alert("Sorry, the script was not executed. Please try again.");
				},
				success: function(data, textStatus){
					$("#img_"+photo_type).remove();
					$("#del_"+photo_type).remove();
				}
			});
		}
		else
			if (city_id) {
				$.ajax({
					type: "POST",
					dataType: "text",
					data: "city_id="+city_id+"&photo_type="+photo_type,
					url: baseURL+"/index.php?page=del_photo",
					error: function(data) {
						alert(data+"Sorry, the script was not executed. Please try again.");
					},
					success: function(data, textStatus){
						$("#img_"+photo_type).remove();
						$("#del_"+photo_type).remove();
					}
				});
			}
			else
				if (hotel_id) {
					$.ajax({
						type: "POST",
						dataType: "text",
						data: "hotel_id="+hotel_id+"&photo_type="+photo_type,
						url: baseURL+"/index.php?page=del_photo",
						error: function(data) {
							alert(data+"Sorry, the script was not executed. Please try again.");
						},
						success: function(data, textStatus){
							$("#img_"+photo_type).remove();
							$("#del_"+photo_type).remove();
						}
					});
				}
	}
}
