﻿
//首頁前往地點選單
function UpdateMenuPlace() {
	var value = $("#ddlHatobusType").val();

    var goDate = $('#txtGoDate').val();
    var datestr="";
    if(goDate!=""){
        datestr = "&SD=" + goDate
    }
	var url = "HandleAction.aspx?Action=MP&HT=" + value + datestr + "&TF=" + new Date().getTime();
    
    $('#spMenuPlace').html('<select id="ddlMenuPlace" style="width:180px;"><option selected>Loading...</option></select>');

	$.ajax({
		type: "GET",
		url: url,
		success: function(msg) {
			$('#spMenuPlace').html(msg);
		},
		error: function(xhr, ajaxOptions, thrownError) {
		    alert('UpdateMenuPlace Error');
			alert(xhr.status);
			alert(thrownError);
		}
	});
}

//首頁出發日期選單
function UpdateDate(SelectedDate) {
	var value = $("#ddlMonth").val();

	var url = "HandleAction.aspx?Action=Date&Mon=" + value + "&SD=" + SelectedDate + "&TF=" + new Date().getTime();

	$.ajax({
		type: "GET",
		url: url,
		success: function(msg) {
			$('#spDate').html(msg);
			UpdateMenuPlace();
		},
		error: function(xhr, ajaxOptions, thrownError) {
		    alert('UpdateDate Error');
			alert(xhr.status);
			alert(thrownError);
		}
	});
}


//首頁查詢
function goQuery() {
	//出發日期, 月
	var yearMon = $("#ddlMonth").val();
	//出發日期, 日
	var date = $("#ddlDate").val();
	
	var qDate="";
	if(date!= "" && date!="= 不指定 ="){
		qDate = "&OutDate=" + date;
	}else{
		if( yearMon!="" && yearMon!="= 不指定 ="){
			qDate = "&OutDate=" + yearMon;
		}
	}
	
	//最低預算
	var MinPrice = parseInt($("#ddlMinPrice").val());
	//最高預算
	var MaxPrice = parseInt($("#ddlMaxPrice").val());
	
	if(MinPrice != 0 && MaxPrice != 0){
		if(MinPrice>MaxPrice == true){alert("預算上限金額不可小於下限金額!!"); return false;}
	}
	
	var qMinPrice="";
	if(MinPrice!=0 && (isNaN(MinPrice)) != true){qMinPrice = "&MinP=" + MinPrice;}
	
	var qMaxPrice="";
	if(MaxPrice!=0 && (isNaN(MaxPrice)) != true){qMaxPrice = "&MaxP=" + MaxPrice;}
	
	//行程種類
	var HatobusType = $("#ddlHatobusType").val();
	
	var qHatobusType = "";
	if(HatobusType!=""){qHatobusType = "&Hatobus=" + HatobusType;}
	
	//前往地點
	var MenuPlace = $("#ddlMenuPlace").val();
	
	var qMenuPlace="";
	if(MenuPlace!="" && MenuPlace !="Loading..." && MenuPlace!="= 請選擇 ="){qMenuPlace = "&MapMenuId=" + MenuPlace;}
	
	window.location.href="List.aspx?Type=HBus" + qDate + qMinPrice + qMaxPrice + qHatobusType + qMenuPlace ;
}



function SetMonthDayDDLWithDate(){
	var goDate = $('#txtGoDate').val();

	var today = new Date().format('yyyy/mm/dd');
	
	if(goDate < today){alert('出發日期不可早於今天!!'); return};

	var algoDate = goDate.split("/");
	
	var yearMon = algoDate[0] + "/" + algoDate[1];
	$('#ddlMonth').val(yearMon);
	
	UpdateDate(goDate);
}

function SetGoDateWithddlDate(){
	//出發日期, 日
	var goDate = $("#ddlDate").val();
	
	var today = new Date().format('yyyy/mm/dd');
	
	if(goDate < today){alert('出發日期不可早於今天!!'); return};

    $('#txtGoDate').val(goDate);
	UpdateMenuPlace();
}


//首頁-刪除考慮清單
function DelConsider(Index, ltpid) {
	var url = "HandleAction.aspx?Action=DelCons&Index=" + Index +"&t=" + new Date().getTime();

	$.ajax({
		type: "GET",
		url: url,
		success: function(msg) {
	        RefreshConsider();
		    
			if(msg == 'Succeed'){
				alert('行程' + ltpid + '已從考慮清單中刪除!');
				$('#divConsider'+ltpid).hide();
				$('#Consider_'+ltpid).html('<li class="add_btn"><a href="#" onclick="AddConsider('+ltpid+');return false;" title='+ltpid+'>加入考慮清單</a></li>');
			}else{
				alert('行程' + ltpid + '從考慮清單中刪除失敗!');
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			//alert(xhr.status);
			//alert(thrownError);
		}
	});
	
}

function RefreshConsider() {
    var url = "HandleAction.aspx?Action=RefCons&t=" + new Date().getTime();
	$.ajax({
		type: "GET",
		url: url,
		success: function(msg) {
			$('#ConsiderList').html(msg);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(thrownError);
		}
	});
}

//行程列表-加入考慮清單
function AddConsider(ltpid) {
	var url = "HandleAction.aspx?Action=AddCons&ltpid=" + ltpid +"&t=" + new Date().getTime();

	$.ajax({
		type: "GET",
		url: url,
		success: function(msg) {
	        RefreshConsider();
	        
			if(msg>5){alert('考慮清單只保留最後五個行程!!');}
			$('#Consider_'+ltpid).css({background: "url(/common/common-substance/add_btn_active.gif) left bottom no-repeat", height:"40px"});
			$('#Consider_'+ltpid).html('已加入考慮清單');
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(thrownError);
		}
	});
	
}
