var id_hash={};

window.Validator=function(id_hash){
	//by cdchu
	var submitBtn=_.$("btnSearchFlight",true)||_.$("btnSearch",true);
	var scheduleBtn=_.$("btnFlightSchedule",true);
	if (submitBtn){
		submitBtn.disabled=false;
		submitBtn.style.display="";
	}
	if (scheduleBtn){
		scheduleBtn.disabled=false;
		scheduleBtn.style.display="";
	}
	//end
	
	var el_hash = {};
	
	var $gl = function(id){
		return el_hash[id] || (el_hash[id] = document.getElementById(id_hash[id] || id));
	};
	
	var $alert = (function(fn){
		return function(){
			var arg=Array.prototype.slice.call(arguments,0);
			arg[1]=$s2t(arg[1]);
			fn.apply(_,arg);
			return false;
		};
	})(window.$alert);
	
	var form = document.getElementById('form_search_1');	

	var flightWay = form[id_hash['FlightWay']];
	if(!flightWay) return;
	addEvent(flightWay, switchFlightWay);
	setTimeout(switchFlightWay, 10);
	
	//航程类型切换
	function switchFlightWay(){
		//debugger;
		if(!$('div_dest1')){
			$('bdate').style.display = getValue(flightWay) == 'D' ? '' : 'none';
			$('ctl00_MainContentPlaceHolder_txtADatePeriod1').style.display = getValue(flightWay) == 'D' ? '' : 'none';
			$('btime').style.display = getValue(flightWay) == 'D' ? '' : 'none';
			$('ctl00_MainContentPlaceHolder_drpStartPeriod2').style.display = getValue(flightWay) == 'D' ? '' : 'none';
		}
		else{
			$gl('div_dest1').style.display = getValue(flightWay) == 'D' ? '' : 'none';
		}
	}

	//成人儿童切换
	var ageType = form[id_hash['PType']];
	ageType.onchange = switchAgeType;
	setTimeout(switchAgeType, 10);
	function switchAgeType(){
		$gl('age_type_tip').style.display = ageType.value == 'CHD' ? '' : 'none';
	}

	//是否为国内城市
	function isDomesticCity(name){
		var re = new RegExp('\\|'+name+'\\(');
		return re.test($$.module.address.source.fltInternational_cn);
	}
	
	//private
	function getValue(el){
		if(el.options) return el.value;
		for(var i = 0; i < el.length; i++) if(el[i].checked) return el[i].value;
	}
	function setValue(el, val){
		if(el.options) el.value = val;
		else for(var i = 0; i < el.length; i++) if(el[i].value == val) el[i].checked = true;
	}
	function addEvent(el, fn){
		if(el.options) el.onchange = fn;
		else for(var i = 0; i < el.length; i ++) el[i].onclick = fn;
	}

	//校验
	window.validateQuery = function(){
		//出发城市
		var el = $gl('home_city');
		if(el.isNull()) return $alert(el, '请选择出发城市');
		var ct1 = el.value;

		//出发日期
		var el = $gl('depart_date_1');
		if(el.isNull()) return $alert(el, '请选择出发日期');
		var dt1 = el.value.isDateTime();
		if(!dt1) return $alert(el, '日期格式有误，请检查');
		if(dt1 < new Date().dateValue()) return $alert(el, '对不起，出发日期不能在今天之前');

		//目的城市
		var el = $gl('dest_city_1');
		if(el.isNull()) return $alert(el, '请选择目的城市');
		var ct2 = el.value.replace(/\(.+$/g,'');
		if(ct2 == ct1) return $alert(el, '您选择的目的城市和出发城市相同，请重新选择');
		if(isDomesticCity(ct1) && isDomesticCity(ct2)) return $alert(el, '出发城市和目的城市不能同为国内城市，请检查');

		//返回日期
		var el = $gl('depart_date_2');
		if(getValue(flightWay) == 'D'){
			if(el.isNull()){
				setValue(flightWay, 'S');
				switchFlightWay();
			}else{
				var dt2 = el.value.isDateTime();
				if(!dt2) return $alert(el, '日期格式有误，请检查');
				if(dt2 < dt1) return $alert(el, '对不起，返回日期不能早于出发日期');
			}
		}

		//送票城市
		var el = $gl('ticket_city');
		if(el.isNull()){
			
			if (el.module.notice)
				el.module.notice.check();
			if(/720|617|59|58/.test(document.getElementById($gl('home_city').getAttribute('mod_address_reference')).value)){
				document.getElementById(el.getAttribute('mod_address_reference')).value = "2";
				el.value = "上海";
			}
			else{
				document.getElementById(el.getAttribute('mod_address_reference')).value = 
				document.getElementById($gl('home_city').getAttribute('mod_address_reference')).value;
				el.value = $gl('home_city').value;
			}
		}

		if($$.module.loading){
			$$.module.loading.show();
		}else{
			//by cdchu
			if (submitBtn)
				setTimeout(function(){
					submitBtn.disabled=true;
				},1);
			if (scheduleBtn)
				setTimeout(function(){
					scheduleBtn.disabled=true;
				},1);
			$r("beforeunload",function(){
				if (submitBtn)
					submitBtn.disabled=false;
				if (scheduleBtn)
					scheduleBtn.disabled=true;
			});
			//end
		}

		return true;
	}

	window.validateMobile = function(url){
		if(!validateChoice()) return false;
		var mob = $gl('mobile');
		if(!mob) return true;
		if(mob.value == '') return $alert(mob, '请填写手机号');
		if(!/^0?1(3|5)\d{9}$/.test(mob.value)) return $alert(mob, '您填写的手机号码有误，请重新填写。',false,"lb","lt");
		mob.value = mob.value.replace(/^0/,'');
		if(url) {
			form.action = url;
		}
		var checkresult = $ajax("SubPage/ValidateUserMobile.aspx?uid=" + mob.value);
		if(checkresult.indexOf("check fail")>=0){
			var uuid = $gl('uid');
			var pwd = $gl('password');
			uuid.value = mob.value;
			uuid.module.notice.check();
			if (confirm("您的手机号已在携程注册，请在会员区登录。\n是否需要将您的登录密码发送到您手机上？")){
				var CurrentDomain = $gl("CurrentDomain");
				if(!CurrentDomain){CurrentDomain= document.domain;}
				window.open("http://" + CurrentDomain + "/Member/ConfirmName.asp?hdnUid="+mobilestring+"&hdnSendMode=SMS","tipForm","width=800,height=500,top=0,left=0");
			}
			return $alert(pwd,'您已经注册过，请输入密码登录');
		}
		return true;
	};

	window.validateLogin = function(url){
		if(!validateChoice()) return false;
		if(url) form.action = url;
		var uid = $gl('uid');
		var pwd = $gl('password');
		if(!uid) return false;
		if(uid.isNull()) return $alert(uid, '请输入用户名');
		if(pwd.value == '') return $alert(pwd, '请输入密码');
		return true;
	};

	window.validateCorp = function(url){
		if(!validateChoice()) return false;
		if(url) form.action = url;
		return true;
	};
	
	window.validateChoice = function(){
		var box = $gl('divFlights') && $gl('divFlights').getElementsByTagName('input');
		if(!box) return true;
		for(var i = 0; i < box.length; i++) {
			if(box[i].checked) break;
		}
		if(i >= box.length) return $alert(box[0], '请选择航班');

		//时间是否在五小时以后
		if($gl('selectedDate').value==""){return true;}
		var selectedDate = $gl('selectedDate').value;

		selectedDate = new Date(Date.parse(selectedDate.replace(/^(\d+)-(\d+)-(\d+)/, '$2/$3/$1')));
		var nowtime = new Date();
		var longdis = selectedDate.getTime() - nowtime.getTime();
		var timedis = longdis/1000/60/60;
		if(timedis<=5){
			return $alert(box[i], '请选择5小时以后出发的航班<br/>如有疑问可致电携程800-820-6666',true,'rt','lt');
		}

		return true;
	};
};

function redirectQuery(url){
	document.forms[0].action = url;
	return validateQuery();
}

$r("domReady",function(){
	if (id_hash['FlightWay'])
		new Validator(id_hash);
	if($$.module.loading){
		$$.module.loading.source = '||';
		$$.module.loading.preload(9000);
	}
});