/**** Private Js for hotel International ****/
/**** Author: Feifei ******/
/**** Create Date:2009-05-19***/

$r('domready' , function(){
	//init country and citylist
	var countryIndex = c[0];
	var cat = "";
	var queryFrm = document.forms[0];
	

	initCCList();
	

	function initData(){
		var url=location.href+"&";
		if(url.indexOf("?")==-1||url.indexOf("=")==-1)return null;
		var dataurl=url.split("?")[1].split("&");
		var initdata=new Array();
		for(var i=0,l=dataurl.length;i<l;i++){
			initdata[dataurl[i].split("=")[0].toString()]=dataurl[i].split("=")[1];
		}
		return initdata;
	}

	function initCCList(){
		getCountryList();
		var initdata = initData();
		if(initdata!=null){
			queryFrm.country.value=initdata["Country"];
			queryFrm.city.value = parseInt(initdata["City"]);
			queryFrm.oricity.value=parseInt(initdata["City"])+20000;
			$("starttime").value=initdata["starttime"]||"yyyy-mm-dd";
			$("deptime").value=initdata["deptime"]||"yyyy-mm-dd";
		}
		if(queryFrm.country.value!=""){
			var countryId = queryFrm.country.value;
			var cityId = queryFrm.oricity.value;
			setListSel($("countryList") , countryId);
			getCityList(countryId,false);
			setListSel($("cityList") , cityId);
		}
		
		$("countryList").onkeyup = $("cityList").onkeyup = function(e){
			e = $fixE(e);
			el = e.$target;
			k = e.keyCode;
			switch(k){
				case 38:
					el = el.previousSibling || el.parentNode.firstChild;
					el.focus();
					ccSel(this, el);
					break;
				case 40:
					el = el.nextSibling || el.parentNode.lastChild;
					el.focus();
					ccSel(this, el);
					break;
				default:
					if(k>=65 && k<=90){
						var c = String.fromCharCode(k);
						var divs = this.$("div");
						for(var i=0,l=divs.length;i<l;i++){
							 var inner = divs[i].innerHTML;
							 var reg = new RegExp("^"+c , "i");
							 if(reg.test(inner)){
								break;
							 }
						}
						if(i==divs.length) return;
						divs[i].parentNode.parentNode.scrollTop = i*divs[i].offsetHeight;
						divs[i].focus();
						ccSel(this, divs[i]);
					}
					break;
			}
		}
	}

	function ccSel(elp , el){
		if(elp == $("countryList")){
			var countryId = el.getAttribute("cid");
			setListSel(elp , countryId);
			queryFrm.country.value = countryId;
			getCityList(countryId,true);
			resetCityGroup();
		}
		if(elp == $("cityList")){
			var cityId = el.getAttribute("cid");
			setListSel(elp , cityId);
			setCityId(cityId);
		}
	}
	
	//show country list
	function getCountryList(){
		for(var i=0,l=countryIndex.length;i<l;i++){
			var ii = countryIndex[i];
			cat += "<div cid='"+ii+"' tabindex='1000'>"+n[ii].replace(/ /g , "&nbsp;")+"-></div>";
		}
		$('countryList').innerHTML = cat;
	}
	
	//show city list by country Id
	function getCityList(id,ischange){
		var cityIndex = c[id];
		var mouse = "";
		for(var i=0,l=cityIndex.length;i<l;i++){
			var oo = cityIndex[i];
			mouse += "<div cid='"+oo+"' tabindex='2000'>"+n[oo].replace(/ /g , "&nbsp;")+"</div>";
		}
		$('cityList').innerHTML = mouse;
		if(ischange){
			var ini = $('cityList').$('div')[0].getAttribute("cid");
			setListSel($('cityList') , ini);
			setCityId(ini);
		}
	}

	//set country list by country id
	function setListSel(el , id){
		if(id==-1) {
			var divs = el.$("div");
			for(var i=0,l=divs.length;i<l;i++){
				if(divs[i].className=="selected"){
					divs[i].className = "";
					break;
				}
			}
			return;
		}
		var divs = el.$("div");
		for(var i=0,l=divs.length;i<l;i++){
			if(divs[i].getAttribute("cid")==id){
				divs[i].className = "selected";
				divs[i].focus();
			}
			if(divs[i].className=="selected" && divs[i].getAttribute("cid")!=id){
				divs[i].className="";
			}
		}
	}


	//cityhotel radio click event
	$('hotelcitytd').onclick = function(e){
		var src = $fixE(e).$target;
		var tag = src.tagName;
		if(tag=="INPUT"){
			var cityvalue = src.getAttribute("value");
			queryFrm.city.value=cityvalue;
			queryFrm.country.value ="";
			setListSel($('countryList') , -1);
			setListSel($('cityList') , -1);
		}
	}
	
	//country list click event
	$('countryList').onclick = function(e){
		var src = $fixE(e).$target;
		var tag = src.tagName;
		if(tag=="DIV"){
			var countryId = src.getAttribute("cid");
			setListSel(this , countryId);
			queryFrm.country.value = countryId;
			getCityList(countryId,true);
			resetCityGroup();
		}
	}
	
	//city list click event
	$('cityList').onclick = function(e){
		var src = $fixE(e).$target;
		var tag = src.tagName;
		if(tag=="DIV"){
			var cityId = src.getAttribute("cid");
			setListSel(this , cityId);
			setCityId(cityId);
		}
	}
	
	//set city id to the hidden city value
	function setCityId(id){
		if (id > 0){
			queryFrm.oricity.value = id;
			if (id < 20000)
				queryFrm.city.value = id - 100;
			else
				queryFrm.city.value = id - 20000;
		}
	}
	
	//show no select on city group
	function resetCityGroup(){
		var inps = $('hotelcitytd').$("input");
		for(var i=0,l=inps.length;i<l;i++){
			if(inps[i].type=="radio" && inps[i].checked){
				inps[i].checked = false;
				break;
			}
		}
	}
	
	//get the selected Index from country list and city list
	function getListIndex(el){
		var divs = el.$('div');
		for (var i=0,l=divs.length ; i<l ; i++){
			if(divs[i].className=="selected"){
				break;
			}
		}
		return  (i==divs.length)?-1 : i;
	}
	

	//Star
	function starState(){
		for(var i=0,l=stars.length;i<l;i++){
			if(stars[i].checked=="") return false;
		}
		return true;
	}

	queryFrm.StarAll.onclick = function(){
		var substar = queryFrm.Star;
		for(var i=0,l=substar.length;i<l;i++){
			substar[i].checked = this.checked;
		}
	}
	
	var stars = queryFrm.Star;
	for(var i=0,l=stars.length;i<l;i++){
		stars[i].onclick = function(){
			queryFrm.StarAll.checked = "";
			if(starState())	 queryFrm.StarAll.checked = "checked";
		}
	}


	
	var url = "http://hotels.ctrip.com/domestic/SearchHotel.aspx?type=2";
	$('domesticLink').onclick = function(){
		url = url + "&starttime=" +  $('starttime').value;
		url = url + "&deptime=" + $('deptime').value;
		location.href = url;
	}
	
	queryFrm.onsubmit = function(){
		//check city
		//check cityGroup
		hcFlag = false;
		if(this.hotelcity){
			for(var i=0,l=this.hotelcity.length;i<l;i++){
				if(this.hotelcity[i].checked){
					hcFlag = true;
					break;
				}
			}
		}
	
		//check cityList
		if(!hcFlag){
			if(this.country.value=="" || this.city.value==""){
				$alert(this.hotelcity[0] , $s2t("请选择酒店所在的国家和城市"));
				return false;
			}
		}

		//check startDate and endDate
		if($('starttime').isNull()){
			$alert($('starttime') , $s2t("请输入入住时间"));
			return false;
		}
		var d_starttime = $('starttime').value.isDateTime();
		if(!d_starttime){
			$alert($('starttime') , $s2t("入住时间不符合格式规范或无效的日期"));
			return false;
		}
		var d_perdate = $('perdate').value.isDateTime();
		if(d_starttime<d_perdate){
			$alert($('starttime') , $s2t("入住时间不能早于"+$('perdate').value));
			return false;
		}
		var d_postdate = $('postdate').value.isDateTime();
		if(d_starttime>=d_postdate){
			$alert($('starttime') , $s2t("本系统不支持晚于"+$('postdate').value+"的网上预订，如需预订请致电800-820-6666"));
			return false;
		}
		
		if($('deptime').isNull()){
			$alert($('deptime') , $s2t("请输入离店时间"));
			return false;
		}
		var d_endtime = $('deptime').value.isDateTime();
		if(!d_endtime){
			$alert($('deptime') , $s2t("离店时间不符合格式规范或无效的日期"));
			return false;
		}
		if(d_endtime<=d_perdate){
			$alert($('deptime') , $s2t("离店时间不能早于或等于"+$('perdate').value));
			return false;
		}
		if(d_endtime<=d_starttime){
			$alert($('deptime') , $s2t("离店时间不能早于或等于"+$('starttime').value));
			return false;
		}
		if(d_endtime>d_postdate){
			$alert($('deptime') , $s2t("本系统不支持晚于"+$('postdate').value+"的网上预订，如需预订请致电800-820-6666"));
			return false;
		}

		if((d_endtime.getTime()-d_starttime.getTime())>2419200000){
			$alert($('deptime') , $s2t("入住时间段不能超过28天"));
			return false;
		}
		
		//price
		if (this.submittype.value==1)
		{
			var bprice = parseFloat($('BegPrice').value.replace(/[^\d]/g , ''));
			var eprice = parseFloat($('EndPrice').value.replace(/[^\d]/g , ''));

			if(bprice>0 && eprice>0 && bprice>eprice) 
			{
				$('BegPrice').value = eprice;
				$('EndPrice').value = bprice;
			}
		}

		//reserve room number
		if($('rooms').selectedIndex==0){
			$alert($('rooms') , $s2t("请选择预订房间数"));
			return false;
		}

		//other issues
		this.screensize.value = screen.width;
		if (this.city.value==58 || this.city.value==59 )
		{
			//this.action="http://hotels.ctrip.com/domestic/ShowHotelList.aspx";
			this.action=g_domain+"/domestic/ShowHotelList.aspx";
		}

		return true;
		
	}

});