﻿// for year selection limitation
var startY=1930;
var endY=2019;
var today=new Date();
startY=today.getFullYear();
endY=today.getFullYear()+1;

function initTripType(){
	var tripType = getRadioValue('trip');
	changeTripType(tripType);
}
function changeTripType(tripType){
//alert('called:changeTripType! tripType='+tripType);
	if(tripType=='ONEWAY') {
		$('returnDateTextId').style.display='none';
		$('returnInputId').style.display='none';
	}else if(tripType=='ROUNDTRIP') {
		$('returnDateTextId').style.display='';
		$('returnInputId').style.display='';
	}
}
// 验证日期是否满足yyyy-MM-dd的样式
function isStrictDate(strDate){
	var result = /(\d{4})\-(\d{2})\-(\d{2})/.exec(strDate);
	if (!result) return false;
	var month = parseFloat(RegExp.$2);
	var day= parseFloat(RegExp.$3);
	if (month <= 0 || month > 12) return false;
	if (day <= 0 || day > 31) return false;
	return true;
}

function getRadioValue(radioName){
	var robj = document.getElementsByName(radioName);   
  	for(var i=0;i<robj.length;i++) 
      	if (robj[i].checked) return robj[i].value;   
		return "";
}

function SearchFlights(){
	var orgCity = $('orgCity');
	var dstCity = $('dstCity');
	if(orgCity.value==''){
		alert("请输入出发城市！");
		orgCity.focus();
		return false;
	}
	if(dstCity.value==''){
		alert("请输入到达城市！");
		dstCity.focus();
		return false;
	}
	var orgCityCode=findCityCode(orgCity.value);
	var dstCityCode=findCityCode(dstCity.value);
	if(!checkLine(orgCityCode, dstCityCode)){
		alert('没有开通从'+orgCity.value+'到'+dstCity.value+'的航线！');
		return false;
	}

	var takeoffDate = $('takeoffDate');
	if(takeoffDate.value==''){
		alert("请选择出发日期！");
		takeoffDate.focus();
		return false;
	}

	if(!(isStrictDate(takeoffDate.value))){
		alert("出发日期格式不对，请选择出发日期或按“2008-01-01”格式输入！");
		takeoffDate.focus();
		return false;
	}
    if(!compareDate(gettoday(),takeoffDate.value)){
       alert("出发日期不能小于当前日期！");
       return;
     } 
	var tripType = getRadioValue('trip');
	var returnDate = '';
	if(tripType=="ROUNDTRIP"){
		returnDate = $('returnDate');
		if(returnDate.value==''){
			alert("请选择返程日期！");
			returnDate.focus();
			return false;
		}
		if(!(isStrictDate(returnDate.value))){
			alert("返程日期格式不对，请选择返程日期或按“2008-01-01”格式输入！");
			returnDate.focus();
			return false;
		}
	
	    if(!compareDate(gettoday(),returnDate.value)){
	       alert("返程日期不能小于当前日期！");
	       return;
	     } 
	    if(!compareDate(takeoffDate.value,returnDate.value)){
	       alert("返程日期不能早于出发日期！");
	       return;
	     } 
	}
	var qType = getRadioValue('qType');

	var fobj1=document.SearchFlightsForm;
    fobj1.takeoffDate.value=takeoffDate.value;
	if(tripType=="ROUNDTRIP")fobj1.returnDate.value=returnDate.value;
	else fobj1.returnDate.value='';
    fobj1.tripType.value=tripType;
    fobj1.orgCity.value=orgCityCode;
    fobj1.dstCity.value=dstCityCode;
    fobj1.queryType.value=qType;
    fobj1.submit();
}
