

function log(message) {
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
    log.window_.document.body.scrollTop = log.window_.document.body.scrollHeight;
}


function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    
    //log("cookie=[" + document.cookie + "]");
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


function addShoppingList(username, hid, roomid) {
	
	if (username == "") {
		alert('請先加入會員');
	} else {
		xajax_addShoppingList(username, hid, roomid);
		alert('加入訂房完成');
	}
	
	
}


function removeShoppingList(username, hid, roomid) {
	if (username == "") {
		alert('請先加入會員');
	} else {
		xajax_removeShoppingList(username, hid, roomid);
		//alert('刪除訂房完成');
	}
}




function addEvaluation(username, hid) {
	
	if (username == "") {
		alert('請先加入會員，才能進行推薦');
	} else {
		xajax_addEvaluation(username, hid);
		//alert('感謝您的評價');
	}
	
}

function testReturn(t) {
	
	alert(t);
	return t;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function getSelectValue(selectObj) {
	if (!selectObj) 
		return "";
	var selectLength = selectObj.options.length;
	for(var i = 0; i < selectLength; i++) {
		if (selectObj.options[i].selected) {
			return selectObj.options[i].value;
		}
	}
	return "";
}



function locateViewPoint() {
	
	if (document.getElementById("map") != null) {
	
		var hotel_search = document.getElementById("hotel_search");
 		var idx = hotel_search.select_place.selectedIndex; 
 		// get the value of the selected option 
 		var place = hotel_search.select_place.options[idx].value; 
	 	
	 	var arg = place.split("|");
	 	var place = arg[0];
	  var coor = arg[1].split(",");
	  //log("place=" + place + " coor[0]=" + coor[0] + " coor[1]=" + coor[1]);
	
		var isShowMapBlowup = 1;
		if (place.charAt(0) == "P") {
  		isShowMapBlowup = 0;
  		place = place.substring(1);
  	}
  	// mark the place
  	var html = place;
	  var point = new GLatLng(coor[0], coor[1]);
	  map.setCenter(point, 13); 
	  map.panTo(point);
	  var viewIcon = new GIcon(G_DEFAULT_ICON);
	  viewIcon.image = "http://www.lifecitytech.com.tw/images/icons/flag_red.png";   
	  viewIcon.iconSize = new GSize(25, 25); 
	  // Set up our GMarkerOptions object
	  var markerOptions = { icon:viewIcon };
	  var marker = new GMarker(point, markerOptions);
	
	  // The new marker "mouseover" listener        
	  GEvent.addListener(marker,"mouseover", function() {
	    marker.openInfoWindowHtml(html);
	  });        
	               
	  //bounds.extend(point);
	  map.addOverlay(marker);
	  if (isShowMapBlowup == 1) {
	  	marker.showMapBlowup(place); 
	  }
  } else {
  	log("map is null");
  }

}

function placeChange() { 
 	// get the index of the selected option 
 	
 	var hotel_search = document.getElementById("hotel_search");
 	var idx = hotel_search.select_place.selectedIndex; 
 	// get the value of the selected option 
 	var place = hotel_search.select_place.options[idx].value; 
 	
 	var arg = place.split("|");
 	var place = arg[0];
  var coor = arg[1].split(",");
  //log("place=" + place + " coor[0]=" + coor[0] + " coor[1]=" + coor[1]);
  	
  
  
  var distance = hotel_search.setDistance.value;
  
  var isShowMapBlowup = 1;
  if (place.charAt(0) == "P") {
  	isShowMapBlowup = 0;
  	place = place.substring(1);
  } else {
    
	  var startPoint = new GLatLng(coor[0], coor[1]);
		var lefttop =  getDestPointByStart2D(startPoint, distance, -45);
		var rightbottom = getDestPointByStart2D(startPoint,distance,135);
		var searchArea = lefttop.lat().toFixed(4) +"," + lefttop.lng().toFixed(4)+ "|"+ rightbottom.lat().toFixed(4) +"," + rightbottom.lng().toFixed(4);  
		
		hotel_search.searchArea.value = searchArea; 
  }  
  
  
  if (document.getElementById("map") != null) {
  // mark the place
  	var html = place;
	  var point = new GLatLng(coor[0], coor[1]);
	  map.setCenter(point, 13); 
	  map.panTo(point);
	  var viewIcon = new GIcon(G_DEFAULT_ICON);
	  viewIcon.image = "http://www.lifecitytech.com.tw/images/icons/flag_red.png";   
	  viewIcon.iconSize = new GSize(25, 25); 
	  // Set up our GMarkerOptions object
	  var markerOptions = { icon:viewIcon };
	  var marker = new GMarker(point, markerOptions);
	  
	
	  // The new marker "mouseover" listener        
	  GEvent.addListener(marker,"mouseover", function() {
	    marker.openInfoWindowHtml(html);
	  });        
	               
	  //bounds.extend(point);
	  map.addOverlay(marker);
	  
	  if (isShowMapBlowup == 1) {
	  	marker.showMapBlowup(place); 
	  }
	  
	  var bounds = new GLatLngBounds();
		bounds.extend(lefttop);
		bounds.extend(rightbottom);
		var currentFocustRec = new GPolyline([
          new GLatLng(lefttop.lat(), lefttop.lng()),
          new GLatLng(lefttop.lat(), rightbottom.lng()),
          new GLatLng(rightbottom.lat(), rightbottom.lng()),
          new GLatLng(rightbottom.lat(), lefttop.lng()),
          new GLatLng(lefttop.lat(), lefttop.lng())
          ], "#007F00", 3);    
		
		
		// 只要顯示出結果，先不用顯示在地圖上
		map.addOverlay(currentFocustRec);  

		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());	  

	  
	  
	  

	  
  } else {
  	//log("map is null");
  }
  
	//log("searchArea=" + hotel_search.searchArea.value); 
 } 


function displayMingsu(currentpage, totalpage, isFirst) {
  
  if (isFirst == 0) {
		setMouseCursor('paginationid','wait');
	}
	
	if (isFirst == 1) {
		document.getElementById("hotel_display").innerHTML = "<p style='font-size:18px; color:#C0C0C0; text-indent:350px; '><img src='images/loading.gif' width=20 height=20 />...</p>";
	}
	var addr = readCookie("addr"); 
  if (addr == null) { addr = "";}
	xajax_queryResult(0,addr,currentpage,totalpage);
}


function displayAction(currentpage, totalpage) {
    
  //log("displayAction currentpage=" + currentpage + " totalpage=" + totalpage);
	xajax_queryActionResult(currentpage,totalpage);
}




function setMouseCursor(id, type) {
  //log("id=" + id + " type=" + type);
	document.getElementById(id).style.cursor = type;
}

//根據 搜尋清單中之checkbox來決定
function isExistedMap() {
	var r= document.getElementById("useMap").checked;
	return r;

}

function searchMingsuByHotelname(hotelname,hid){
    //log("hotelname=" + hotelname);
    xajax_addAccessCount(hid);
    
    window.location.href  = "#search-form";
		//document.getElementById("search_condition").value = hotelname;
		searchMingsu(1, 1, 1, 0, hotelname);
		//xajax_searchResult(1, 1 ,'', '',0, 0, 0, 0, 0, hotelname, LCKey, useShoppingCar, useMap, useInfo); 
  
}

function searchMingsu(currentpage, totalpage, isFirst, clearDisplay, hotelname) {
  
  //window.location.href  = "#search-form";

  currentpage = typeof(currentpage) != 'undefined' ? currentpage : 1;
  
 
  
 
  
  
  
	var place ="";
	var distance = 0;
	var searchpoints = "";
	var lbound ;
	var ubound ;
	
	// reset query result first
	//map.clearOverlays();
	
	//if (isFirst == 1) {
		if (clearDisplay == 1){
			document.getElementById("hotel_display").innerHTML = "";
		}
		document.getElementById("hotel_queryresult").innerHTML = "<p style='font-size:18px; color:#C0C0C0; text-indent:350px; '><img src='images/loading.gif' width=20 height=20 />...</p>";
		
	//}
	document.getElementById("link_queryresult").innerHTML = "";
	//document.getElementById("dopages").innerHTML = "";
	
	var hotel_search = document.getElementById("hotel_search");
	
	
	place = getSelectValue(hotel_search.select_place);
	
	

	distance = hotel_search.textfield_distance.value;
	//distance = 5;
	
	
	var time = getSelectValue(hotel_search.select_time);
	var price = getSelectValue(hotel_search.select_price);
  if (price == 0) {
    lbound = 0;
    ubound = 1000000;
  } else if (price == 1) {
    lbound = 0;
	  ubound = 999;
  } else if (price == 2) {
    lbound = 1000;
    ubound = 1999;
  } else if (price == 3) {
    lbound = 2000;
    ubound = 2999;
  } else if (price == 4) {
    lbound = 3000;
    ubound = 3999;
  } else if (price == 5) {
    lbound = 4000;
    ubound = 4999;
  } else if (price == 6) {
    lbound = 5000;
    ubound = 5999;
  } else if (price == 7) {
    lbound = 6000;
    ubound = 9999;
  } else if (price == 8) {
    lbound = 10000;
    ubound = 1000000;
  }

	
	var roomtype = getSelectValue(hotel_search.select_roomtype);
	
	var sortrule = getSelectValue(hotel_search.select_sortrule);
	
	// log("hotelname=" + hotelname);
	var search_condition;
	search_condition = hotel_search.search_condition.value;	
  
	  
  if ( (typeof(hotelname) != 'undefined') && ( hotelname != ''))  {
  	
  	search_condition = hotelname; 
  }
  
  //log("search_condition=" + search_condition);
	  
  //log("search_condition=" + search_condition);

	
	

	
	var useShoppingCar = hotel_search.useShoppingCar.checked;
	var useMap = hotel_search.useMap.checked;
	var useInfo = hotel_search.useInfo.checked;
	
	if (useShoppingCar) {
		useShoppingCar =1;	
	}else {
		useShoppingCar =0;	
	}
	
	if (useMap) {
		useMap =1;	
	}else {
		useMap =0;	
	}
	
	
	if (useInfo) {
		useInfo =1;	
	}else {
		useInfo =0;	
	}
	
	//log("useShoppingCar=" + useShoppingCar);

	//log("place=" + place + " distance=" + distance + " time=" + time + " price=" + price +   " lbound=" + lbound + " ubound=" + ubound + " roomtype=" + roomtype + " search_condition=" + search_condition);
	// 如果 place 屬於「鄉」「鎮」「市」，則直接搜尋不需定位。否則則進行定位運算
	
	var searchArea = ""; 

	var arg = place.split("|");
	var place = arg[0];
	
	
	
	//if (hasMap ==1) {
		var bounds = new GLatLngBounds();
	
		var coor = arg[1].split(",");
		var startPoint = new GLatLng(coor[0], coor[1]);

		var lefttop =  getDestPointByStart2D(startPoint, distance, -45);
			
	  var rightbottom = getDestPointByStart2D(startPoint,distance,135);
	  	
	  
	  if (useMap == 1) {
	  		bounds.extend(lefttop);
	  		bounds.extend(rightbottom);
	  		currentFocustRec = new GPolyline([
	            new GLatLng(lefttop.lat(), lefttop.lng()),
	            new GLatLng(lefttop.lat(), rightbottom.lng()),
	            new GLatLng(rightbottom.lat(), rightbottom.lng()),
	            new GLatLng(rightbottom.lat(), lefttop.lng()),
	            new GLatLng(lefttop.lat(), lefttop.lng())
	            ], "#3333FF", 5);    
	  		
	  		
	  		// 只要顯示出結果，先不用顯示在地圖上
	  		map.addOverlay(currentFocustRec);  
	  
	  		map.setZoom(map.getBoundsZoomLevel(bounds));
	  		map.setCenter(bounds.getCenter());    
	  
	  }
	  
	  searchArea = lefttop.lat().toFixed(4) +"," + lefttop.lng().toFixed(4)+ "|"+ rightbottom.lat().toFixed(4) +"," + rightbottom.lng().toFixed(4);  
	  //log("focusPoints=" + focusPoints);   
	  //log("before xajax_queryResult");
  //}
  
  
  if (useMap == "1") {
    if(document.getElementById("map") == null) {
  		xajax_showMap();
  	} 
  }
  
  var LCKey = readCookie("LCKey");
  xajax_searchResult(currentpage, totalpage ,place, searchArea, time, lbound, ubound, roomtype, sortrule, search_condition, LCKey, useShoppingCar, useMap, useInfo); 
  
  if (useInfo == "1") {
    //log("xajax_queryLinkResult()");
  	xajax_queryLinkResult(1, 0, searchArea);  
  }
    

}





function searchLink(currentpage, totalpage, searchArea) {
  
  //log("searchLink searchArea=" + searchArea);

  currentpage = typeof(currentpage) != 'undefined' ? currentpage : 1;
 
  
  xajax_queryLinkResult(currentpage, totalpage, searchArea);  

  
  //document.body.style.cursor = "default"; 
  //log("2 searchMingsu");
}






function openNewWindow(hid ,url) {
  var tempurl = encodeURI('displayMingsu.php?hid=' + hid + '&url=' + url);
	window.open(tempurl,'_blank', 'directories=1, menubar=1, toolbar=1, status=1, location=1, top=10, left=10, resizable=1');
}


var imageWin;

function openImageWin(url){ 
  //log("url=" + url);
  //var tempurl = encodeURI(url);
	imageWin =window.open(url , '顯示照片', config='width=800,height=500, top=0, left=0, toolbar=0, menubar=0, scrollbars=0, resizable=0, location=0, status=0') ;

  //document.onmousemove = getMouseXY;	
}

function hideImageWin(){ 
	imageWin.close();
}

  


function getErrorReport() {

	//var updateData=  document.getElementById('updateData');
	
	var value = getSelectValue(document.getElementById("select_time"));
	alert("error msg=["+ value + "]");
	//document.getElementById('showData').value = "謝謝您的協助";
}


function enableMap(enable) {

	if (enable) {
		xajax_showMap();
	} else {
		xajax_hideMap();
	}
}


function showMap(hid) {
	var tempurl = encodeURI('showMap.php?hid=' + hid);
	var w =window.open(tempurl,'map', config='width=800,height=500, top=0, left=0, toolbar=0, menubar=0, scrollbars=0, resizable=0, location=0, status=0');
	w.focus();
}

function showDiscount(hid) {
	var tempurl = encodeURI('showDiscount.php?hid=' + hid);
	var w =window.open(tempurl,'map', config='width=800,height=500, top=0, left=0, toolbar=0, menubar=0, scrollbars=0, resizable=0, location=0, status=0');
	w.focus();
}


function showCoupon(hid) {
	var tempurl = encodeURI('showCoupon.php?hid=' + hid);
	var w =window.open(tempurl,'map', config='width=800,height=700, top=0, left=0, toolbar=0, menubar=0, scrollbars=1, resizable=0, location=0, status=0');
	w.focus();
}



