// JavaScript Document
function GetCookie(sName){
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++){
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]){
			if(aCrumb[1]){
				return unescape(aCrumb[1]);
			}
		}
	}
	return "";
}



var string = '';
string += '<div class="searchBox">';
string += '	<h4>China hotel search</h4>';
string += '	<form id="leftSearchForm" name="form1" method="get" action="/hotel/search.asp">';
string += '	<div>';
string += '		<ul>';
string += '			<li>';
string += '				Check in:&nbsp;&nbsp;&nbsp;';
string += '					<input id="checkin" class="text" type="text" name="checkin" readonly="readOnly"/>';
string += '					<a href="javascript:showCalendar(\'dimg1\',false,\'checkin\',null,\'setCheckInDate\')" onclick="event.cancelbubble=true;"><img id="dimg1" src="/hotel/images/icon/search-time.gif" /></a><br />';
string += '				Check out:';
string += '					<input id="checkout" class="text" type="text" name="checkout" readonly="readOnly" />';
string += '					<a href="javascript:showCalendar(\'dimg2\',false,\'checkout\',\'checkin\')" onclick="event.cancelbubble=true;"><img id="dimg2" src="/hotel/images/icon/search-time.gif" /></a>';
string += '			</li>';
string += '			<li class="alignLeft">';

var tempCity = "";
var tempString = "";
var cityNum=0;
for ( var i = 0;i<hotelArea.DB.length;i++){
	if (tempCity != hotelArea.DB[i].city){
		tempString = tempString + "<option vaIndex =\"" + i + "\" value =\"" + hotelArea.DB[i].city + "\">" + hotelArea.DB[i].city + "</option>";
		tempCity = hotelArea.DB[i].city;
		cityNum += 1;
	}
}

string += '				Destination: (all cities:' + cityNum + ')';
//string += '				<select id="selectSearchCity" onchange="citySelected(this);" name="c1">';
string += '				<select id="selectSearchCity" name="c1">';
string += '					<option value="">------ Please Select ------</option>';
string = string + tempString;
string += '				</select>';

/*
string += '				Near by';
string += '				<select id="selectCityArea" name="ca">';
string += '					<option value="">------ Please Select ------</option>';
string += '				</select>';
string += '				Star Rating';
string += '				<select id="star" name="s">';
string += '					<option value="">------ Please Select ------</option>';
string += '					<option value="5">5 star</option>';
string += '					<option value="4">4 star</option>';
string += '					<option value="3">3 star</option>';
string += '					<option value="2">2 star or less</option>';
string += '					<option value="14">Comfort Inn </option>';
string += '					<option value="13">Serviced Apartment</option>';
string += '					<option value="12">Chinese Courtyard</option>';
string += '					<option value="11">Hostel</option>';
string += '					<option value="15">Boutique Hotel</option>';
string += '				</select>';
*/

string += '				Hotel Name';
string += '				<input id="n" type="text" size="23" name="n" class="text2" />';
string += '			</li>';
string += '		</ul>';
string += '		<a href="javascript:dosubmit(\'leftSearchForm\');"><img src="/hotel/images/icon/search.gif" alt="" /></a>';
string += '	</div>';
string += '	</form>';
string += '</div>';
document.write (string);


var tmpCitystr = GetCookie("city");
var tmpcheckInstr = GetCookie("checkin");
var tmpcheckOutstr = GetCookie("checkout");
var tmpNamestr = UrlDecode(GetCookie("hotelname"));
var tmpRatingstr = GetCookie("rating");
var tmpCityAreastr = UrlDecode(GetCookie("cityarea"));

if(typeof(hCity)=="undefined")var hCity="";
if(typeof(theCheckIn)=="undefined")var theCheckIn="";
if(typeof(theCheckOut)=="undefined")var theCheckOut="";
if(tmpCitystr.length==0)tmpCitystr=hCity;
if(tmpcheckInstr.length==0)tmpcheckInstr=theCheckIn;
if(tmpcheckOutstr.length==0)tmpcheckOutstr=theCheckOut;

try{	
	var tmpCityField = document.getElementById("selectSearchCity");
	var tmpCityList = tmpCityField.getElementsByTagName("OPTION")
	for(var i = 0; i < tmpCityList.length; i ++){
		if(tmpCityList[i].value == tmpCitystr){
			tmpCityList[i].selected = true;
			break;
		}
	}
	if(tmpRatingstr.length>0)document.getElementById("star").value = tmpRatingstr;
	document.getElementById("n").value = tmpNamestr;

	document.getElementById("checkin").value = tmpcheckInstr;
	document.getElementById("checkout").value = tmpcheckOutstr;
	
   
	var selectcitystr = document.getElementById("selectSearchCity");

	if(selectcitystr.selectedIndex>0){
		//citySelected(selectcitystr);
		var tmpCityAreaField = document.getElementById("selectCityArea");
		var tmpCityListArea = tmpCityAreaField.getElementsByTagName("OPTION")
		for(var i = 0; i < tmpCityListArea.length; i ++){
			if(tmpCityListArea[i].value == tmpCityAreastr){
				tmpCityListArea[i].selected = true;
				break;
			}
		}
	}
}
catch(e){}



function citySelected(sel){
	var selIndex = sel.selectedIndex;
	var selectCityArea = document.getElementById("selectCityArea");
		selectCityArea.options.length = 0;
	
	var option = document.createElement("option");
		option.value = "";
		option.innerHTML = "------ Please Select ------";
		selectCityArea.appendChild(option);
			
	if(selIndex == 0){
		return false;
	}

	var starIndex = sel.options[selIndex].getAttribute("vaIndex");
	city = sel.value;
	for (var j = starIndex; j<hotelArea.DB.length;j++){
		if (city == hotelArea.DB[j].city){
			var option = document.createElement("option");
			option.value = hotelArea.DB[j].area;
			option.innerHTML = hotelArea.DB[j].area;
			selectCityArea.appendChild(option);
		}else{
			break;
		}
	}
}


function dosubmit(id){
	var obj1=document.getElementById("checkin").value;
	var obj2=document.getElementById("checkout").value;
	if(obj1=="" || obj1.length==0 || obj2=="" || obj2.length==0){
		alert("Sorry, your Check-in Date or Check-out Date is invalid.");
		return;
	}
	var d1=new Date(Date.parse(obj1.replace("-","/")));
	var d2=new Date(Date.parse(obj2.replace("-","/")));
	if(d1>=d2){
		alert("Sorry, your Check-out Date should be later than Check-in Date.");
		return;
	}
	var city=document.getElementById("selectSearchCity");
	if(city.value.length==0){
		alert("Sorry, please select the destination city first.");
		city.focus();
		return;
	}
	
	document.getElementById(id).submit();
}


function UrlDecode(str){
    var ret="";
    for(var i=0;i<str.length;i++){
        var chr = str.charAt(i);
        if(chr == "+"){
            ret+=" ";
        }else if(chr=="%"){
            var asc = str.substring(i+1,i+3);
            if(parseInt("0x"+asc)>0x7f){
                ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
                i+=5;
            }else{
                ret+=asc2str(parseInt("0x"+asc));
                i+=2;
            }
        }else{
            ret+= chr;
        }
    }
    return ret;
}

