
var chainID = 10316;
var locale = "en-US";
//&locale=zh-CN


var _hotels = {
	27667 : "Shanghai - Eaton Luxe",
	27425 : "Hong Kong - Eaton",
	27422 : "Koh Samui - Langham Place",
	27421 : "Hong Kong - Langham Place",
	27405 : "Los Angeles ¡V The Langham",
	27424 : "Auckland - The Langham",
	27423 : "Boston - The Langham",
	27404 : "Hong Kong - The Langham",
	27406 : "London - The Langham",
	27408 : "Melbourne - The Langham",
	27407 : "Shanghai-The Langham Yangtze",
	28382 : "Shanghai-The Langham Xintiandi",
	27897 : "Beijing Capital Airport - Langham Place"
};

var _ihotelers = {
	27667 : [27667,5811,2283,1],
	27425 : [4940,5831,2283,1],
	27422 : [73007,6531,2284,1],
	27421 : [4930,5831,2284,1],
	27405 : [71098,4498,2285,1],
	27424 : [4442,4513,2285,1],
	27423 : [5873,4497,2285,1],
	27404 : [5129,5831,2285,1],
	27406 : [4396,4495,2285,1],
	27408 : [4430,4512,2285,1],
	27407 : [72457,5811,2285,1],
	28382 : [28382,5811,2285,1],
	27897 : [27897,5811,2284,1]
};

var hotelName = {};

hotelName["27667"] = "ETSHA";
hotelName["27425"] = "ETHKG";
hotelName["27422"] = "LPSMU";
hotelName["27421"] = "LPHKG";
hotelName["27405"] = "TLPSA";
hotelName["27424"] = "TLAUC";
hotelName["27423"] = "TLBOS";
hotelName["27404"] = "TLHKG";
hotelName["27406"] = "TLLON";
hotelName["27408"] = "TLMEL";
hotelName["27407"] = "TLSHA";  
hotelName["28382"] = "TLXTD";   



function displayHotelWarning(type){
	var str = 'Please select one of the destinations first, and click &quot;';
	if(type == 1){
		str += 'Modify reservations';
	}else if(type == 2){
		str += 'More search options';
	}else{
		str += 'Search';
	}
	str += '&quot; again.';
	$('#sidepanel_booking .warning-bubble').show().html(str).ifixpng();
	
}
	
	
function getPopUpPath(){
	return "http://hongkong.langhamplacehotels.com";	
}


function getOldLink(DateIn,DateOut,Rooms,Adults,Hotel,Childs){
		
	hotelId = _ihotelers[Hotel][0];
	city = _ihotelers[Hotel][1];
	brands = _ihotelers[Hotel][2];					
	myDin  = DateIn.split("/");
	myDout = DateOut.split("/");

	night = daysElapsed(new Date(myDout[2],Number(myDout[0],10)-1,myDout[1]),new Date(myDin[2],Number(myDin[0],10)-1,myDin[1]));


//				if(hotelId == 72457){
//			        var link = "https://reservations.ihotelier.com/iStay.cfm?Children=0&HotelID=72457&DateIn="+DateIn+"&DateOut="+DateOut+"&Rooms="+Rooms+"&Adults="+Adults+"&Length="+night+"&LanguageID=11";
//				}else{
	var url = "http://reservations.ihotelier.com/areasearch/default.cfm"
	+"?PortalSymbol=lgm&ViewQty=0&SortBy=3"
	+"&CurrentPage=1&sType=1&sInput=HotelID&ChangeLevel=6&GeoID_6=ALL"
	+"&ChangeLevel=6&GeoID_5=ALL&ChangeLevel=6"
	+"&btnLevel=0&languageid=1"
	+"&GeoID_4="+city+""
	+"&HotelID="+hotelId+""
	+"&DateIn="+DateIn+""
	+"&DateOut="+DateOut+""
	+"&Rooms="+Rooms+""
	+"&Adults="+Adults+""
	+"&Children="+Childs+""
	+"&Channel=C_Room&RateType=&PromoCode="
	+"&ConID_1628="+brands
	//if(locale != 'en-US' || Hotel == '27423' || Hotel == '27405') url+="&locale="+locale;

//				}	
	return url;
}
	
function getNewLink(DateIn,DateOut,Rooms,Adults,Hotel,ip,Childs){

	if(Hotel == 27406 || Hotel == 27407 || Hotel == 27408 || Hotel == 27421 || Hotel == 27422 || Hotel == 27424 || Hotel == 28382  || Hotel == 27897) {
		// From mm/dd/yyyy to dd/mm/yyyy
		//_DateIn  = DateIn.split("/");
		//_DateOut = DateOut.split("/");
		
		//DateIn = _DateIn[1]+"/"+_DateIn[0]+"/"+_DateIn[2];
		//DateOut = _DateOut[1]+"/"+_DateOut[0]+"/"+_DateOut[2];
	}
	var url = "https://gc.synxis.com/rez.aspx?Hotel="+Hotel+"&Chain="+chainID+"&adult="+Adults+"&Rooms="+Rooms+"&Child="+Childs
	//if(Hotel == 27423 || Hotel == 27405 || locale!='en-US') url+="&locale="+locale;
	
	if(DateIn != "") url+= "&arrive="+DateIn;
	if(DateOut != "") url+= "&depart="+DateOut;
	
	if(ip != 1){
		url += "&start=1";
	}
	
	return url;
}


	

function submitBooking(frm,isMoreSearchOption) //more search option or not
{
	$('#sidepanel_booking .warning-bubble').hide();
	try{
		
	
		var DateIn = frm.DateIn.value;
		var DateOut = frm.DateOut.value;
		var Adults = frm.Adults.value;
		var Rooms = frm.Rooms.value;
		var hotel = frm.hotel.value;
		var Childs = frm.Childs.value;
		
		if(hotel == ''){
			displayHotelWarning(isMoreSearchOption == '1' ? 2: 0);
			
		}else{
			var url = getNewLink(DateIn,DateOut,Rooms,Adults,hotel,isMoreSearchOption,Childs);
			if(_ihotelers[hotel][3] == 0){
				
			  url = getOldLink(DateIn,DateOut,Rooms,Adults,hotel,Childs);
				
			}
			 window.open(url, '_blank');
		
		}
		
	}catch(e){
		try{
			console.error(e);
		}catch(e){
			
		}
	}
	return false;
}	

function layout(DateIn,DateOut,Adults,Rooms,isModify,isMoreSearchOption){
	var qs = "?DateIn="+DateIn+"&DateOut="+DateOut+"&Adults="+Adults+"&Rooms="+Rooms+"&isModify="+isModify+"&isMoreSearchOption="+isMoreSearchOption;
	var path = getPopUpPath() + "/popup_overlay/book_mask_overlay.htm"+qs;
	
	
	$.colorbox({href:path,iframe:true,innerWidth:320,innerHeight:300,speed:1000,opacity:0.65});

}	

var referLanguage=false;




// JavaScript Document
function bestGuaranteed(){
	
	var path = getPopUpPath() + "/popup_overlay/best_online_rate_guarantee_new.htm";
	$.colorbox({href:path,iframe:true,innerWidth:830,innerHeight:350,speed:1000,opacity:0.65});
	
}


function tripadvisor(){
	
	var path = "http://www.tripadvisor.com/WidgetEmbed-cdspropertydetail?locationId=305813&partnerId=AA1D6ECF95A1421D8767CA22D394870E&display=true&lang=en_US";
	$.colorbox({href:path,iframe:true,innerWidth:800,innerHeight:600});
	
}

function modifyReservation(DateIn,DateOut,Adults,Rooms,isModify,hotelName){
	
	$('#sidepanel_booking .warning-bubble').hide();
	
	var DateIn = frm.DateIn.value;
	var DateOut = frm.DateOut.value;
	var Adults = frm.Adults.value;
	var Rooms = frm.Rooms.value;
	var Childs = 0;
	var hotel = 0;
	
	for(var hotelID in _hotel){
		if(_hotel[hotelID] == hotelName){
			hotel = hotelID;
			break;
		}
	}
	
	if(!_ihotelers[hotel]){
		return;
	}
	
	if( _ihotelers[hotel][3] == 1 ){
		if(_ihotelers[hotel][0] != 27667 && _ihotelers[hotel][0] != 28382 && _ihotelers[hotel][0] != 27897){
var url = "https://gc.synxis.com/rez.aspx?Hotel="+hotel+"&Chain=10316&start=16"
//if(locale != 'en-US' || hotel == '27423' || hotel == '27405') url+="&locale="+locale;

			window.open(url,"_blank");
			
			return;

		}
	}

	calculate_overlay(1);
}


function modifyReservations(frm){
	
	$('#sidepanel_booking .warning-bubble').hide();
	
	var DateIn = frm.DateIn.value;
	var DateOut = frm.DateOut.value;
	var Adults = frm.Adults.value;
	var Rooms = frm.Rooms.value;
	var Childs = frm.Childs.value;
	var hotel = frm.hotel.value;
	
	// if no hotel selected, then display message and terminate the request
	if(frm.hotel.value.length < 1 ){
			displayHotelWarning(1);
			
			
		return;
	}
	
	if( _ihotelers[hotel][3] == 1 ){
		if(_ihotelers[hotel][0] != 27667 && _ihotelers[hotel][0] != 28382 && _ihotelers[hotel][0] != 27897){
var url = "https://gc.synxis.com/rez.aspx?Hotel="+hotel+"&Chain=10316&start=16";
//if(locale != 'en-US' || hotel == '27423' || hotel == '27405') url+="&locale="+locale;
			window.open(url,"_blank");
			
			return;

		}
	}

	calculate_overlay(1);
}

function calculate_overlay(isModify,isMoreSearchOption)
{
	var frm = document.getElementById("frmBooking");
	var DateIn = frm.DateIn.value;
	var DateOut = frm.DateOut.value;
	var Adults = frm.Adults.value;
	var Rooms = frm.Rooms.value;
	var Childs = frm.Childs.value;
	var hotel = frm.hotel.value;
	
	if(isModify!=1) isModify = 0;
	if(isMoreSearchOption!=1) isMoreSearchOption = 0;
	
	var url;
	if(hotel == ""){
			var type = 0;
			if(isModify == '1') type = 1;
			else if(isMoreSearchOption == '1') type = 2;
			displayHotelWarning(type);
			

	}else{
		if(isModify == 0 ){
			
			url = getNewLink(DateIn,DateOut,Rooms,Adults,hotel,isMoreSearchOption,Childs);
		
			if(_ihotelers[hotel][3] == 0){
				url = getOldLink(DateIn,DateOut,Rooms,Adults,hotel,Childs);
			}
			
			window.open(url,"_blank");
			
		}else{
			
			var url = "https://gc.synxis.com/rez.aspx?Hotel="+hotel+"&Chain="+chainID+"&start=16";
			//if(locale != 'en-US' || hotel == '27423' || hotel == '27405') url+="&locale="+locale;
			if(_ihotelers[hotel][3] == 0){
				url = "https://reservations.ihotelier.com/areasearch/cancel.cfm?Languageid=1&portalsymbol=lgm";
			}
			window.open(url,"_blank");	
		}
	}		
}		
function y2k(number) { return (number < 1000) ? number + 1900 : number; }


	
function daysElapsed(date1,date2) {
		var difference = Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0) - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
		return difference/1000/60/60/24;
}

