// JavaScript Document
function GetCookie(sName)
{
// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
	// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]){
			if(aCrumb[1]){
				return unescape(aCrumb[1]);
			}
		}
	}
// a cookie with the requested name does not exist
return "";
}



var string = "<p class=\"searchT\">Hotel Search</p><div class=\"searchmod\">  <form action=\"/hotel/search.asp\" method=\"get\" name=\"form1\" id=\"leftSearchForm\">    <table width=\"162\" border=\"0\"><tr>  <td width=\"64\">Check-in </td>  <td width=\"88\">";
	string +="<input type=\"text\" class=\"ins1\" id=\"checkin\" readOnly name=\"checkin\" ";
	string +=" /> <A onclick=event.cancelbubble=true; href=\"javascript:showCalendar('dimg1',false,'checkin',null,'setCheckInDate')\"><IMG id=dimg1 src=\"/hotel/images/icon/search-time.gif\" /></A></td></tr><tr>  <td>Check-out</td>  <td>";
	string +="<input id=\"checkout\" readonly=\"readOnly\"  name=\"checkout\" type=\"text\" class=\"ins1\" ";
	string +=" />   <a  onclick=\"event.cancelbubble=true;\" href=\"javascript:showCalendar('dimg2',false,'checkout','checkin')\"><img id=\"dimg2\"  src=\"/hotel/images/icon/search-time.gif\" /></a></td></tr><tr><td colspan=\"2\">Destination (all cities:113)</td></tr><tr>  <td colspan=\"2\"><select id=\"selectSearchCity\" name=\"c1\" onchange=\"citySelected(this);\">    <option value=\"\">------ Select ------</option> ";
	
 var tempCity = "";
 var tempString = "";
 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;
	}
 }
string = string + tempString;

string +="  </select>  </td></tr><tr>  <td>Nearby</td>  <td>&nbsp;</td></tr><tr>  <td colspan=\"2\"><select name=\"ca\" id=\"selectCityArea\">    <option value=\"\">------ Select ------</option>  </select></td></tr><tr>  <td>Star Rating </td>  <td>&nbsp;</td></tr><tr>  <td colspan=\"2\"><select id=\"star\" name=s>  <option value=0 selected>-------------All-------------</option>  <option value=5>5 star</option>  <option value=4>4 star</option>  <option value=3>3 star</option>  <option value=2>2 star or less</option><option value=\"14\">Comfort Inn </option><option value=\"13\">Serviced Apartment</option>				<option value=\"12\">Chinese Courtyard</option>				<option value=\"11\">Hostel</option><option value=\"15\">Boutique Hotel</option></select>  </td></tr><tr>  <td>Hotel Name </td>  <td>&nbsp;</td></tr><tr>  <td colspan=\"2\"><input name=\"n\" type=\"text\" id=\"n\" size=\"23\"/></td></tr>    </table>    <p><a href=\"javascript:dosubmit('leftSearchForm');\"><img src=\"/hotel/images/icon/search-button.gif\" /></a></p>  </form></div><p class=\"searchBgB1\"></p>";
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;
		}
	}

	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;
	}
	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;
}

