
var rentCity,rentCityID,returnCity,returnCityID,rentStore,rentStoreID,returnStore,returnStoreID,selrentTime,selreturnTime,rentAddress,returnAddress,rentDate,returnDate;
var tt=0;
var tt2=0;

//加载城市列表
function loadCityList(obj){
	 
	 showCity=document.getElementById("show_City");
	 rentCity=document.getElementById("rentCity");   //取车城市
	 rentCityID=document.getElementById("rentCityID");  //取车城市ID
	 returnCity=document.getElementById("returnCity"); //还车城市
	 returnCityID=document.getElementById("returnCityID");//还车城市ID
	 rentStore=document.getElementById("rentStore");		//取车门店
	 rentStoreID=document.getElementById("rentStoreID");		//取车门店ID
	 returnStore=document.getElementById("returnStore");		//还车门店
	 returnStoreID=document.getElementById("returnStoreID"); //还车门店ID
	 selrentTime=document.getElementById("selrentTime");
	 selreturnTime=document.getElementById("selreturnTime");
	 rentAddress=document.getElementById("rentAddress");
	 returnAddress=document.getElementById("returnAddress");
	 rentDate=document.getElementById("rentDate");
	 returnDate=document.getElementById("returnDate");
	 
	var html="";
	var cityLen=1;
	html+="&nbsp;&nbsp;";
	for(var city in journeyCity){
		//alert(journeyCity[city].city);
		html+="&nbsp;<a href=\"javascript:setVal('"+obj+"',"+journeyCity[city].id+",'"+journeyCity[city].city+"');hideThis('#theLayer');\">"+journeyCity[city].city+"</a>&nbsp;";
		if(cityLen%4==0){			
			html+="<br/>&nbsp;&nbsp;";
			}
		
		cityLen++;
	}
	$("#show_City").html(html);
	var offset = $("#"+obj).offset();
    $("#theLayer").css({ "left": offset.left, "top": offset.top+20 });
    hideThis("#StoreList");
	showThis("#theLayer");
	
}
//选择城市的时候设置相关信息
function setVal(obj,id,city){
	var r=(obj=="rentCity") ? "rent" : "return";

	var today=new Date();
	today=today.valueOf();
	var runday=$("#rentDate").val();
	var timeArr=runday.split("-");
	var rentDate=new Date(timeArr[0],timeArr[1],timeArr[2]);
	rentDate=rentDate.valueOf();
	
	if($("#rentDate").val()=="日期..."){
		rentDate=today;
	}
	var td=0;
	if(r=="rent") td=today+1* 24 * 60 * 60 * 1000;
	else td=rentDate+1* 24 * 60 * 60 * 1000;
	td=new Date(td);
	var year=td.getFullYear();
	var month=td.getMonth()+1;
	if(r!="rent"&&$("#rentDate").val()!="日期...") month=td.getMonth();
	month=(month<10)?"0"+month:month;
	var day=td.getDate();
	day=(day<10)?"0"+day:day;
	var date=year+"-"+month+"-"+day;
	
	$("#"+r+"CityID").val(id);
	$("#"+obj).val(city);
	for(var store in storeList){
		if(storeList[store].city==id){
			//alert(storeList[store].storeID);
			$("#"+r+"Store").val(storeList[store].storeName);
			$("#"+r+"StoreID").val(storeList[store].storeID);
			$("#"+r+"Address").val(storeList[store].address);
			$("#"+r+"Date").val(date);
			if(r=="rent")var seltime=$("#selrentTime");else var seltime=$("#selreturnTime");
			var i=0;
			$(seltime).empty();
			for(var t in listOpenTime){ //取得门店的时间列表
				if(listOpenTime[t].storeID==storeList[store].storeID){
					//seltime[i]=new Option(listOpenTime[t].time,listOpenTime[t].time);
					$("<option value="+listOpenTime[t].time+">"+listOpenTime[t].time+"</option>").appendTo($(seltime));
					i++;
				}
			}
			break;
		}
	}
	if(tt!=1)$("#rentStore").click();
	if(tt2!=1)$("#returnStore").click();
	
}
//获取对应城市的门店列表及信息
function getStoreList(city_id,selName,selID){
	if($(city_id).val()=="")
		return;
	if(city_id=="#rentCityID")
		$("#selreturnTime").hide();
	var num=1;
	if(selName=="#rentStore"){num=1;}else{num=2;}
	var html="<div id=showStore ><table width=100% bgcolor=#FFFFFF><tr><td style=width:40%;vertical-align:top; ><ul>";
	for(var store in storeList){
		if(storeList[store].city==$(city_id).val()){
			html+="<li style=list-style-type:none><a href=\"javascript:setStore('"+storeList[store].storeName+"','"+storeList[store].storeID+"','"+storeList[store].address+"','"+selName+"','"+selID+"');changeTime("+num+");hideThis('#showStore');showThis('#selreturnTime');\" onmouseover=\"showStoreInfo('"+storeList[store].storeID+"');\" >"+storeList[store].storeName+"</a></li>";
		}
	}
	html+="</ul></td><td id=storeInfo></td></tr></table></div>";
	
	$("#StoreList").empty().html(html);
	var offset = $(selName).offset();
	$("#showStore").css({"position":"absolute","left": offset.left, "top": offset.top+20,"border":"2px solid #999999","width":"400px","z-index":"999"});
	showStoreInfo($(selID).val());
	hideThis("#theLayer");
	showThis("#StoreList");
}
function setStore(name,id,address,selname,selId){
	$(selname).val(name);
	$(selId).val(id);
	var addr=(selname.indexOf("rent")!=-1)?"#rentAddress":"#returnAddress";
	$(addr).val(address);
	
	if(selname=="#rentStore"&&tt!=1){
		rentDate.click();
		tt=1;
	}if(selname=="#returnStore"&&tt2!=1){
		returnDate.click();
		tt2=1;
	}
	
}

function showStoreInfo(storeId){
	for(var store in storeList){
		if(storeList[store].storeID==storeId){
			var storeInfoHTML="<h3>"+storeList[store].address+"</h3><hr/>";
			storeInfoHTML+="服务时间："+storeList[store].openningTime+"<br>";
			storeInfoHTML+="服务电话："+storeList[store].tel+"";
		}
	}
	$("#storeInfo").empty().html(storeInfoHTML);
	$("#storeInfo").css({"background-color":"#F2F2F2","padding-left": "10px","padding-top":"10px","padding-bottom":"10px","padding-right":"10px","vertical-align":"top"});
	
}
function over(text){
	$(text).css({"font-size":"14px"});
}
function currce(text){
	$(text).css({"font-size":"12px"});
}

//对日期时间的验证
function compareDate(rentDate,returnDate){
	
	var datetime1=$(rentDate).val();
	var datetime2=$(returnDate).val();
	var arr1=datetime1.split(" ");
	var arr2=datetime2.split(" ");
	var date1=arr1[0];
	var date2=arr2[0];
	var time1=arr1[1];
	var time2=arr2[1];
	
	var toDate=new Date();
	month=(toDate.getMonth()<10)?"0"+(toDate.getMonth()+1):toDate.getMonth()+1;
	var day=(toDate.getDate()<10)?"0"+toDate.getDate():toDate.getDate();
	var da=toDate.getFullYear()+"-"+month+"-"+day;
	var hours=parseInt(toDate.getHours());
	var minute=parseInt(toDate.getMinutes())+parseInt(hours*60);
	
	var a=date1.split("-");
	var sd=new Date(a[0],a[1],a[2]);
	
	a=date2.split("-");
	var ed=new Date(a[0],a[1],a[2]);
	
	var b=time1.split(":");
	var st=parseInt(b[0]*60)+parseInt(b[1]);
	b=time2.split(":");
	var et=parseInt(b[0]*60)+parseInt(b[1]);
	
	if(sd>ed){
		alert("取车时间不能晚于还车时间！");
		return false;
	}else if(date1==date2 && st>=et){
		alert("取车时间不能晚于还车时间！1");
		return false;
	}else if(date1==da&&st<=minute){
		alert("取车时间不能小于当前时间！");
		return false;
	}else if(date2==da&&et<=minute){
		alert("还车时间不能小于当前时间！");
		return false;
	}
	else
		return true;
}
//选择门店是重新得到该门店的时间列表
function changeTime(num){
	
	var storeId,selTime;
	if(num==1){
		storeId=$("#rentStoreID").val();
		selTime=$("#selrentTime");
	}
	if(num==2){
		storeId=$("#returnStoreID").val();
		selTime=$("#selreturnTime");
	}
	if(storeId!=""){
		$(selTime).empty();
		var i=0;
		for(var t in listOpenTime){
			if(listOpenTime[t].storeID==storeId){
				//selTime[i]=new Option(listOpenTime[t].time,listOpenTime[t].time);
				$("<option value="+listOpenTime[t].time+">"+listOpenTime[t].time+"</option>").appendTo($(selTime));
				i++;
			}
		}
	}
}

function showThis(obj) {
	$(obj).show();
}
function hideThis(obj) {
	$(obj).hide();
}


