﻿function validateLogin(userCode,password,checkCode){if(userCode.val()==""){alert("用户名不能为空");userCode.focus();return false}if(password.val()==""){alert("密码不能为空");password.focus();return false}if(checkCode.val()==""){alert("验证码不能为空");checkCode.focus();return false;}return true}
function login(){var userCode=$("input#userCode");var password=$("input#password");var checkCode = $("input#yzm");$.ajax({type:"POST",url: basePath + "member/login.do",data:"userCode="+userCode.val()+"&password="+password.val()+"&checkCode="+checkCode.val()+"&d="+new Date().getTime(),beforeSend:function(){$("div#login_body1").block({message:null,overlayCSS:{backgroundColor:'#eee'}});if(!validateLogin(userCode,password,checkCode)){$("div#login_body1").unblock();$.ajaxStop()}},dataType:"json",error:function(er){alert("ERROR:"+er.responseText)},success:function(result){if(result.success){$("#userInfo_code").text(result.data.userCode);$("span#memberCoin").text(result.data.memberCoin);$("span#memberDegree").text(result.data.memberDegree);$("div#login_body2").show();$("div#login_body1").hide();$("div#login_body1").unblock();location.href= basePath + "member/manageMember.do"}else{alert(result.data);$("div#login_body1").unblock()}}})}
function logout(){$.ajax({type:"POST",url: basePath + "member/logout.do",data:"d="+new Date().getTime(),beforeSend:function(){$("div#login_body2").block({message:null,overlayCSS:{backgroundColor:'#eee'}})},dataType:"json",error:function(er){alert("ERROR:"+er.responseText)},success:function(result){if(result.success){$("#userInfo_code").text(result.data.userCode);$("div#login_body1").show();$("div#login_body2").hide();$("div#login_body2").unblock()}else{alert(result.data);$("div#login_body2").unblock()}}})}
function loadAuotCityList(){$.ajax({type:"POST",url: basePath + "ticket/getTargetStationList.do",data:"cityId="+$("#beginCityId").val(),dataType:"json",error:function(er){},success:function(result){if(result.success){apdata=eval(result.data)}else{}}})}
function toFindPassword(){location.href= basePath + "member/toFindPassword.do?userCode="+$('#userCode').val()}
function CityList(){};CityList.From=CityList.From={"options":[{"value":"0","label":"杭州地区","options":[{"value":"330101","label":"杭州市"}]},{"value":"1","label":"宁波地区","options":[{"value":"330201","label":"宁波市"}]},{"value":"2","label":"绍兴地区","options":[{"value":"330601","label":"绍兴市"}]},{"value":"3","label":"舟山地区","options":[{"value":"330901","label":"舟山市"}]}]};
function InitOptions(){}InitOptions.initCityFrom=function(parentId,childId,parentValue,childValue){Select.create(parentId,CityList.From,parentValue);Select.create(childId,CityList.From.options[parentValue?parentValue:0],childValue);$("#"+parentId).change(function(){Select.create(childId,CityList.From.options[$(this).val()])})}
InitOptions.initCityFrom1=function(parentId,childId,parentValue,childValue){Select.setInitOption=false;Select.create(parentId,CityList.From,parentValue);Select.setInitOption=true;Select.create(childId,CityList.From.options[parentValue?parentValue:0],childValue);$("#"+parentId).change(function(){Select.create(childId,CityList.From.options[$(this).val()])})}
InitOptions.initDateBegin=function(selectId,dateJson){Select.create(selectId,dateJson)}
InitOptions.initDepotBegin=function(selectId,dateJson,selectedValue){Select.clear(selectId);Select.addOption(selectId,"","不限","");Select.add(selectId,dateJson,selectedValue)}
function Select(){};Select.setInitOption=false;Select.initOptionValue="";Select.initOptionLabel="不限";
Select.create=function(selectId,json,selectedValue){Select.clear(selectId);Select.add(selectId,json,selectedValue)};
Select.add=function(selectId,json,selectedValue){try{if(!json.options)return;if(Select.setInitOption)Select.addOption(selectId,Select.initOptionValue,Select.initOptionLabel);for(var i=0;i<json.options.length;i++){Select.addOption(selectId,json.options[i].value,json.options[i].label,selectedValue)}}catch(ex){base.alert('设置select错误：指定的JSON对象不符合Select对象的解析要求！')}};
Select.createOption=function(value,label,selectedValue){var opt=document.createElement('option');opt.setAttribute('value',value);if(selectedValue&&selectedValue==value){opt.setAttribute('selected','selected')}opt.innerHTML=label;return opt};
Select.addOption=function(selectId,value,label,selectedValue){var opt=Select.createOption(value,label,selectedValue);document.getElementById(selectId).appendChild(opt);return opt};
Select.clear=function(selectId,iterator){if(typeof(iterator)!='function'){document.getElementById(selectId).length=0}else{var slt=document.getElementById(selectId);for(var i=slt.options.length-1;i>=0;i--){if(iterator(slt.options[i])==true)slt.removeChild(slt.options[i])}}};
var autoCity=null;function initAutoCity(inputId){autoCity=new AutoCity();autoCity.init(inputId)}function gE(x){return document.getElementById(x)}function gETop(x){var t=0;while(x){t+=x.offsetTop;x=x.offsetParent}return t}function gEBottom(x){return(x.offsetHeight+gETop(x))}function gELeft(x){var l=0;while(x){l+=x.offsetLeft;x=x.offsetParent}return l}function gERight(x){return(x.offsetWidth+gELeft(x))}function isUpDownKey(kc){if(kc==40||kc==38||kc==33||kc==34){return true}else{return false}}function isCtrlKey(kc){if(kc==16||kc==17||kc==18){return true}else{return false}}
var apdata="";function AutoCity(){var inputobj=null;var sugdiv=null;var focusBySelect=false;var currInputObj=null;var prevInputValue=null;var showarr=null;var showfocus=-1;var FOCity=null;var onDocumentClick=null}AutoCity.prototype.init=function(inputId){if(this.sugdiv==null){this.sugdiv=document.createElement('DIV');this.sugdiv.style.display='none';this.sugdiv.className='sug';this.sugdiv.style.zIndex=999;this.sugdiv.innerHTML='';this.sugdiv.onscroll=OnSugScroll;var bs=document.getElementsByTagName('BODY');bs[0].appendChild(this.sugdiv)}this.focusBySelect=false;var obj=gE(inputId);obj.onkeydown=OnInputKeyDown;obj.onfocus=OnInputFocus}
function OnSugScroll(){if(autoCity.currInputObj==null){return}autoCity.focusBySelect=true;autoCity.currInputObj.focus()}function OnDocClick(e){var x,y,t,b,l,r;var d;if(window.event){e=window.event;x=e.clientX+document.body.parentNode.scrollLeft;y=e.clientY+document.body.parentNode.scrollTop}else{x=e.pageX;y=e.pageY}d=autoCity.currInputObj;if(!d){document.onclick=autoCity.onDocumentClick;return}t=gETop(d);l=gELeft(d);b=gEBottom(d);r=gERight(d);if(x>=l&&x<r&&y>=t&&y<b){return}d=autoCity.sugdiv;t=gETop(d);l=gELeft(d);b=gEBottom(d);r=gERight(d);if(x>=l&&x<r&&y>=t&&y<b){return}autoCity.select()}var global_focus_flag=false;function OnInputFocus(){global_focus_flag=true;autoCity.currInputObj=this;autoCity.prevInputValue=null;if(autoCity.focusBySelect){autoCity.focusBySelect=false;return}autoCity.focusBySelect=false;InputChecking()}function OnInputBlur(){return}function InputChecking(){if(autoCity.currInputObj==null){return}var v=autoCity.currInputObj.value;if(autoCity.prevInputValue==null||autoCity.prevInputValue!=v){if(v!=""){autoCity.update(v)}else{autoCity.hide()}}setTimeout("InputChecking()",10)}
function OnInputKeyDown(e){if(autoCity.currInputObj==null){autoCity.currInputObj=this;autoCity.prevInputValue=null;InputChecking()}var bCtrlDown=0;if(!e&&window.event){e=window.event}if(window.event){bCtrlDown=e.ctrlKey}else{bCtrlDown=(e.modifiers&Event.CONTROL_MASK)>0}if(bCtrlDown){return true}var kc=e.keyCode;if(isCtrlKey(kc)){return true}return(InputRealKeyDown(kc))}
function InputRealKeyDown(kc){global_focus_flag=false;if(isUpDownKey(kc)){if(kc==38){autoCity.move(1,0)}else if(kc==40){autoCity.move(1,1)}else if(kc==33){autoCity.move(0,0)}else if(kc==34){autoCity.move(0,1)}return false}else if(kc==13){autoCity.select();return false}else if(kc==9){autoCity.select();return true}else if(kc==27){autoCity.hide();return true}return true}function OnSugClick(){autoCity.select()}
AutoCity.prototype.select=function(){if(this.sugdiv.style.display=='none'){return false}if(this.showfocus>=0&&this.showarr!=null&&this.showfocus<this.showarr.length&&this.currInputObj!=null){var i=this.showarr[this.showfocus];this.currInputObj.value=apdata[i][0];this.focusBySelect=true;this.currInputObj.focus();if(window.event){var r=this.currInputObj.createTextRange();r.move("character",r.text.length);r.select()}else{var v=apdata[i][0].length;this.currInputObj.setSelectionRange(v,v)}}this.hide()}
AutoCity.prototype.highlight=function(idx){if(this.sugdiv.style.display=='none'){return false}if(idx==this.showfocus){return true}var i;var ls=this.sugdiv.getElementsByTagName('LI');if(this.showfocus>=0&&this.showfocus<ls.length){ls[this.showfocus].className=''}if(idx>=0&&this.showfocus<ls.length){ls[idx].className='hover'}this.showfocus=idx}
AutoCity.prototype.move=function(m,n){var a=this.showfocus;if(m==1&&n==0){a--}else if(m==1&&n==1){a++}else if(m==0&&n==0){a-=5}else if(m==0&&n==1){a+=5}if(a<0){a=0}else if(a>=this.showarr.length){a=this.showarr.length-1}var i,j,k;i=(this.sugdiv.scrollTop)/20;if(a<=i){this.sugdiv.scrollTop=a*20}else if(a>=i+8){this.sugdiv.scrollTop=(a-7)*20}this.highlight(a)}
AutoCity.prototype.hide=function(v){if(this.sugdiv.style.display!='none'){this.sugdiv.style.display='none';document.onclick=this.onDocumentClick}}
AutoCity.prototype.update=function(v){this.prevInputValue=v;v=v.toLowerCase();this.showfocus=-1;if(this.showarr==null){this.showarr=new Array()}else{this.showarr.length=0}var i,j,s;if(v.length!=0){for(i=0;i<apdata.length;i++){for(j=0;j<apdata[i].length;j++){if(apdata[i][j].toLowerCase().indexOf(v)==0){this.showarr[this.showarr.length]=i;break}}}}if(this.showarr.length<=0||(this.showarr.length==1&&v==apdata[this.showarr[0]][0])){this.hide();return}if(global_focus_flag){return}this.sugdiv.style.top=gEBottom(this.currInputObj)+"px";this.sugdiv.style.left=gELeft(this.currInputObj)+"px";this.sugdiv.style.width=this.currInputObj.offsetWidth;s='<ul>';for(i=0;i<this.showarr.length;i++){s+='<li'+(i==0?' class="hover"':'')+' onmousemove="autoCity.highlight('+i+')" onclick="OnSugClick();">'+apdata[this.showarr[i]][0]+'('+apdata[this.showarr[i]][1]+')</li>'}s+='</ul>';this.sugdiv.innerHTML=s;this.showfocus=0;this.sugdiv.style.zIndex=999;this.sugdiv.scrollTop=0;this.sugdiv.style.display='block';this.onDocumentClick=document.onclick;document.onclick=OnDocClick}
function SetString(str,len){var strlen=0;var s="";for(var i=0;i<str.length;i++){if(str.charCodeAt(i)>128)strlen+=2;else strlen++;s+=str.charAt(i);if(strlen>=len)return s+"..."}return s}function MM_swapImgRestore(){var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)x.src=x.oSrc}function MM_preloadImages(){var d=document;if(d.images){if(!d.MM_p)d.MM_p=new Array();var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0;i<a.length;i++)if(a[i].indexOf("#")!=0){d.MM_p[j]=new Image;d.MM_p[j++].src=a[i]}}}function MM_findObj(n,d){var p,i,x;if(!d)d=document;if((p=n.indexOf("?"))>0&&parent.frames.length){d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p)}if(!(x=d[n])&&d.all)x=d.all[n];for(i=0;!x&&i<d.forms.length;i++)x=d.forms[i][n];for(i=0;!x&&d.layers&&i<d.layers.length;i++)x=MM_findObj(n,d.layers[i].document);if(!x&&d.getElementById)x=d.getElementById(n);return x}function MM_swapImage(){var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;for(i=0;i<(a.length-2);i+=3)if((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc)x.oSrc=x.src;x.src=a[i+2]}}
$(document).ready(function(){
	$("#loginBtn").click(function(){login()});
	$("a#logout").click(function(){logout()});
	$("#password").keydown(function(event){if(event.keyCode==13){event.returnValue=false;event.cancel=true;$("img#loginBtn").click()}});
	InitOptions.initCityFrom("beginAreaId","beginCityId");initAutoCity("endStationName");
	loadAuotCityList();
	$("#endStationName").Watermark("汉字或拼音首字母");
	$("#beginCityId").change(function(){loadAuotCityList()});
	$("#beginAreaId").bind("change",function change(){$("#beginCityId").change()});
	if(!$.browser.msie){$(".guideListSpan").each(function(){$(this).text(SetString($(this).text(),28))})};
	$("#searchBusBtn").click(function(){var endCity=$("#endStationName").val();
	if($("#leaveDate").val()==""){alert("请填写出发日期");$("#leaveDate").focus();return false;}
	if(endCity=="汉字或拼音首字母"||endCity==""){alert("请填写目的城市");$("#endStationName").focus();return false;}
	document.forms['searchBusForm'].submit.click()});
	$("#menuDiv table td span").mouseover(function() {
		var oldClass = $(this).attr("class");
		var newClass = oldClass.replace("menuIco0","menuIco1");
		$(this).attr("class", newClass);
	}).mouseout(function(){
		var oldClass = $(this).attr("class");
		var newClass = oldClass.replace("menuIco1","menuIco0");
		$(this).attr("class", newClass);
	});
	// 绑定日期
	$("#leaveDate").bind('focus',function(){
		WdatePicker({minDate:'%y-%M-%d',maxDate:'%y-%M-{%d+15}',isShowClear:false,isShowToday:false});
	});
	
	// 根据cityId绑定日期区间
	$("#beginCityId").change(function(){
		//$("#leaveDate").addClass('Wdate2');
		//alert($(this).val());
		$("#leaveDate").addClass('Wdate_loading');
		$.ajax({
			type: "POST",
			url: "../ticket/getSaleDateByCityId.do?date="+new Date().getTime(),
			data: "cityId="+$(this).val(),
			dataType:"json",
			error: function(er) {
				//alert("ERROR:" + er.responseText);
				$("#leaveDate").addClass('Wdate_loading');
			},
			success: function(result){
				//alert(result.minDate+"   "+result.maxDate);
				$("#leaveDate").val(result.minDate).unbind('focus').bind('focus',function(){	
					//WdatePicker({minDate:'%y-%M-%d',maxDate:maxdate});
					WdatePicker({minDate:result.minDate,maxDate:result.maxDate,isShowClear:false,isShowToday:false});
				}).removeClass('Wdate_loading');
				
			}
		});
	}).change();
});