﻿	function setAreaChange(SetSubChange)
	{
		$("#AreaSelect").change(function() {
			if($(this).val())
			{
				$.ajax({
					  url: "AjaxRepository.php?action=getLineListUI",
					  global: false,
					  type: "POST",
					  data: ({id : $(this).val()}),
					  dataType: "html",
					  async:false,
					  success: function(msg){
						 $("#LineSelect").parent().html(msg);
						 setLineChange(SetSubChange);
					  }
				   }
				);
			}
			else
			{
				$("#LineSelect").html('<option value="">請選擇</option>');
				$("#StationSelect").html('<option value="">請選擇</option>');
				if(SetSubChange){
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
		});	
	}
	
	function setFrontendAreaChange(SetSubChange)
	{
		$("#AreaSelect").change(function() {
			if($(this).val())
			{
				$.ajax({
					  url: "Backend/AjaxRepository.php?action=getLineListUIFrontend",
					  global: false,
					  type: "POST",
					  data: ({id : $(this).val()}),
					  dataType: "html",
					  async:false,
					  success: function(msg){
						$("#LineSelect").parent().html(msg);
						setFrontendLineChange(SetSubChange);
						GetFrontendLineChange($('#LineSelect').val());
						setFrontendLineChange();
					  }
				   }
				);
			}
			else
			{
				$("#LineSelect").html('<option value="">請選擇</option>');
				$("#StationSelect").html('<option value="">請選擇</option>');
				if(SetSubChange){
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
		});	
	}
	
	function setBwnetFrontendAreaChange(SetSubChange)
	{
		$("#AreaSelect").change(function() {
			if($(this).val())
			{
				$.ajax({
					  url: "../Backend/AjaxRepository.php?action=getLineListUIFrontend",
					  global: false,
					  type: "POST",
					  data: ({id : $(this).val()}),
					  dataType: "html",
					  async:false,
					  success: function(msg){
						$("#LineSelect").parent().html(msg);
						setFrontendLineChange(SetSubChange);
						GetFrontendLineChange($('#LineSelect').val());
						setFrontendLineChange();
					  }
				   }
				);	
			}
			else
			{
				$("#LineSelect").html('<option value="">請選擇</option>');
				$("#StationSelect").html('<option value="">請選擇</option>');
				if(SetSubChange){
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
		});	
	}	
	
	function setLineChange(SetSubChange)
	{
		$("#LineSelect").change(function() {
			if($(this).val())
			{
				$.ajax({
					  url: "AjaxRepository.php?action=getStationListUI",
					  global: false,
					  type: "POST",
					  data: ({id : $(this).val()}),
					  dataType: "html",
					  async:false,
					  success: function(msg){
						$("#StationSelect").parent().html(msg);
						if(SetSubChange){
							setStationChange();						
						}						
					  }
				   }
				);	
			}	
			else
			{
				$("#StationSelect").html('<option value="">請選擇</option>');
				if(SetSubChange){
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
		});		
	}
	
	function setFrontendLineChange(SetSubChange)
	{
		$("#LineSelect").change(function() {
			if($(this).val())
			{
				$.ajax({
					  url: "Backend/AjaxRepository.php?action=getStationListUIFrontend",
					  global: false,
					  type: "POST",
					  data: ({id : $(this).val()}),
					  dataType: "html",
					  async:false,
					  success: function(msg){
						$("#StationSelect").parent().html(msg);
						if(SetSubChange){
							setFrontendStationChange();
						}						
					  }
				   }
				);					
			}	
			else
			{
				$("#StationSelect").html('<option value="">請選擇</option>');
				if(SetSubChange){
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
		});		
	}
	
	function setBwnetFrontendLineChange(SetSubChange)
	{
		$("#LineSelect").change(function() {
			if($(this).val())
			{
				$.ajax({
					  url: "../Backend/AjaxRepository.php?action=getStationListUIFrontend",
					  global: false,
					  type: "POST",
					  data: ({id : $(this).val()}),
					  dataType: "html",
					  async:false,
					  success: function(msg){
						$("#StationSelect").parent().html(msg);
						if(SetSubChange){
							setFrontendStationChange();
						}						
					  }
				   }
				);					
			}	
			else
			{
				$("#StationSelect").html('<option value="">請選擇</option>');
				if(SetSubChange){
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
		});		
	}
	
	function setStationChange()
	{
		$("#StationSelect").change(function() {		
			if($(this).val())
			{
				checkStation($(this).val().split("|")[0]);
				if(check=="false")
				{
					$("#MetroStationCode").val($(this).val().split("|")[1]);
					$("#MetroStationId").val($(this).val().split("|")[0]);				
				}
				else
				{
					alert("此站點已建立");
					$("#StationSelect").val("");
					$("#MetroStationCode").val("");
					$("#MetroStationId").val("");
				}
			}
			else
			{
				$("#MetroStationCode").val("");
				$("#MetroStationId").val("");						
			}
		});	
	}

	function setFrontendStationChange()
	{
		$("#StationSelect").change(function() {		
			if($(this).val())
			{
			}
			else
			{
				$("#MetroStationCode").val("");
				$("#MetroStationId").val("");						
			}
		});	
	}
	
	var check = "";
	function checkStation(id)
	{
		$.ajax({
			  url: "AjaxRepository.php?action=ExistStation",
			  global: false,
			  type: "POST",
			  data: ({id : id}),
			  dataType: "html",
			  async:false,
			  success: function(msg){
				check = msg;					
			  }
		   }
		);				
		return check;
	}
	
	function fileUpload(num,site){
		if($('#fileToUpload').val() == ''){
			alert('請選擇圖片');
		}else{
			$.ajaxFileUpload ({ 
				url:'doajaxfileupload.php', 
				secureuri:false, 
				fileElementId:'fileToUpload'+num, 
				dataType: 'json', 
				success: function (data, status){ 
					if(typeof(data.error) != 'undefined'){ 
						if(data.error != ''){ 
							alert(data.error); 
						}else{
							alert("上傳成功!")
							$('#filePath'+num).val(data.msg);
							$('#src'+num).html(site+data.msg);
							$('#src'+num).attr("href",site+data.msg);							
						} 
					} 
				}, 
				error: function (data, status, e){ 
					alert('請選擇有效的檔案'); 
				} 
			});
		}
	}
	
	function ImgfileUpload(site){
		if($('#ImgfileToUpload').val() == ''){
			alert('請選擇圖片檔');
		}else{
			$.ajaxFileUpload ({ 
				url:'doajaxfileupload.php', 
				secureuri:false, 
				fileElementId:'ImgfileToUpload', 
				dataType: 'json', 
				success: function (data, status){ 
					if(typeof(data.error) != 'undefined'){ 
						if(data.error != ''){ 
							alert(data.error); 
						}else{
							alert("上傳成功!")
							$('#ImgPath').val(data.msg);
							$('#Imgsrc').html(site+data.msg);
							$('#Imgsrc').attr("href",site+data.msg);							
						} 
					} 
				}, 
				error: function (data, status, e){ 
					alert('請選擇有效的檔案'); 
				} 
			});
		}
	}
	
	function clearUpload(num){
		if (confirm("確定刪除環場檔?")){
			$('#fileToUpload'+num).val('');
			$('#filePath'+num).val('');
			$('#src'+num).html('');
			$('#src'+num).attr('');
		}	
	}
	
	function ImgclearUpload(){
		if (confirm("確定刪除圖片?")){
			$('#ImgfileToUpload').val('');
			$('#ImgPath').val('');
			$('#Imgsrc').html('');
			$('#Imgsrc').attr('');
		}						
	}
	
	function changePage(Page){
		$("#Page").val(Page);
		$("#mainform").submit();
	}
	
	function del(form)
	{
		if(confirm("確定要刪除"))
		{
			$("#"+form).submit();
		}
	}	
