//move here by zhy
//2010-4-22

var id_hash = {};
var $interface = {};

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('aspnetForm');	

	var flightWay = form[id_hash['FlightWay']];
	if(!flightWay) return;
	addEvent(flightWay, switchFlightWay);
	setTimeout(switchFlightWay, 10);
	
	//航程类型切换
	function switchFlightWay(){
		    //debugger;
		    //var tflightWay = $('ctl00_MainContentPlaceHolder_QuickSearch_drpFlightWay').value;
		    if(!$('div_dest1')){
			
			    $gl('depart_date_2').parentNode.style.display = getValue(flightWay)  == 'D' ? '' : 'none';
			    //$('ctl00_MainContentPlaceHolder_txtADatePeriod1').style.display = tflightWay == 'D' ? '' : 'none';
			    $gl('arrival_time_2').parentNode.style.display = getValue(flightWay)  == 'D' ? '' : 'none';
			    //$('ctl00_MainContentPlaceHolder_drpStartPeriod2').style.display = tflightWay == '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;
	}
	var nowTime = new Date();
	var NSTime = $("NSTime");
	if (NSTime){
		nowTime = new Date(NSTime.value);
	}
	
	//校验
	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 < nowTime.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;
			}
		}

		//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(!($$.status.version=="zh-cn"?/^1[358]\d{9}$/:/^((852|853)(5|6|8|9)\d{7}|1[358]\d{9}|659\d{7}|601[1-9]\d{7}|8869\d{8})$/)) 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);
		//var checkresult = "check fail";
		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('flt_list') && $gl('flt_list').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 longdis = selectedDate.getTime() - nowTime.getTime();
		var timedis = longdis/1000/60/60;
		if(timedis<=5){
			return $alert(box[i], '由于时间原因，此航班不接受online预定，如有疑问请致电400-820-6666',true,'rt','lt');
		}
		
		var t1 = makeTime(0, 20, 30);
		var t2 = makeTime(0, 8, 0);
		var t3 = nowTime >= t1 ? makeTime(1, 10, 0) : nowTime <= t2 ? makeTime(0, 10, 0) : null;
		if(t3 && selectedDate < t3){
			return $alert(box[i], '由于时间原因，此航班不接受online预定，如有疑问请致电400-820-6666',true,'rt','lt');
		}

		return true;
		
		function makeTime(d, h, m){
			var t = nowTime.dateValue();
			t.setDate(d + t.getDate());
			t.setHours(h);
			t.setMinutes(m);
			return t;
		}		
	};
};

$r("domReady",function(){
	if (id_hash['FlightWay'])
		new Validator(id_hash);
});

function showMore(obj, param){
	var tbody = obj.parentNode.parentNode.parentNode,
		caller = arguments.callee;
	if (!caller.tmpDiv) caller.tmpDiv = $('tmp');
	if (caller.last && caller.last.isExpanded && caller.last != obj) caller(caller.last);
	if (!obj.isInit) {
		$ajax($interface.moreFlights + '?' + param, null, function(txt){
			if (!txt) return;
			caller.tmpDiv.innerHTML = '<table><tbody>' + txt + '</tbody></table>';
			var ajaxList = caller.tmpDiv.firstChild.rows, l = ajaxList.length;
			for (i = 0; i < l; i++) tbody.appendChild(ajaxList[0]);
			obj.isInit = true;
			obj.innerHTML = obj.title = $s2t('更多价格');
			obj.className = "flight_more_up";
			obj.isExpanded = true;
			caller.last = obj;
		});
	} else {
		if (!obj.isExpanded) {
			for (var i = 1, rows = tbody.rows; i < rows.length; i++) rows[i].style.display = '';
			obj.innerHTML = obj.title = $s2t('更多价格');
			obj.className = "flight_more_up";
			obj.isExpanded = true;
			caller.last = obj;
		} else {
			for (var i = 1, rows = tbody.rows; i < rows.length; i++) rows[i].style.display = 'none';
			obj.innerHTML = obj.title = $s2t('更多价格');
			obj.className = "flight_more_down";
			obj.isExpanded = false;
		}
	}

}

//黄色浮出层提示框
//floatyntc模块
Ctrip.module.floatyntc = function(obj){
	obj.module.floatyntc = new function(){
		this.enabled = true;
		this.tip = obj.getAttribute("mod_notice_tip");
		this.check = function(){return true;};
		this.isNull = function(){
			return obj.value.trim() == "" || obj.value == obj.module.floatyntc.tip;
		}
	};

	obj.$r("focus" , function(){
		var gt = $("uid_inputtip");
		var fo = obj.module.floatyntc;
		if(fo.enabled == true && fo.isNull()){
			gt.style.display="";
			gt.innerHTML = fo.tip;
		}
	} , 30);
	
	obj.$r("blur" , function(){
		var gt = $("uid_inputtip");
		if(obj.module.floatyntc.enabled == true){
			gt.style.display="none";
		}
	} , 91);
}

$$.module.adFrame={
  clock:null,
  list:[],
  interval:800,
  timeout:1500,
  count:0
 };
 Ctrip.module.adFrame=function(obj){
  var $ref=$$.module.adFrame;
  function frameAdInit(){
   var ad=$ref.list.shift();
   var src=ad.getAttribute("mod_adFrame_src")||"about:blank";
   var style=ad.getAttribute("mod_adFrame_style")||"";
   ad.innerHTML="<iframe marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" scrolling=\"no\" style=\""+style+"\""+" src=\""+src+"\"><\/iframe>";
   if ($ref.list.length)
    setTimeout(arguments.callee,$ref.interval);
   else
    $ref.clock=null;
  }
  $ref.list.push(obj);
  if (!$ref.clock){
   if ($ref.count)
    frameAdInit();
   else
    $ref.clock=setTimeout(frameAdInit,$ref.timeout);
  }
  $ref.count++;
 };
