// czhao ess
var enable_alert = false;

var numArray = new Array("","一","二","三","四","五","六","七","八");

function genNumOptions(id,end,blank,any){
	var selectObj = document.getElementById(id);
	if (selectObj != null){
		selectObj.options.length = 0 ; 
		var i = 0 ;
		if (blank == true){
			selectObj.options[i] = new Option("","");
			i++;
		}
		for ( ; i <= end; i++){
			selectObj.options[i] = new Option(numArray[i],i);
		}
		if (any == true){
			selectObj.options[i] = new Option("不拘",0);
		}
	}
	else{
		ess_alert("no such select : "+id);
	}
}


function isInt(x) {
   var y=parseInt(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 } 

// This will pass if first value is 0
function isInt1(x) {
	if (isNaN(x)) return false;
	return true;
}

function isFloat(x) {
   var y=parseFloat(x);
   if (isNaN(y)) return false;
   return x==y && x.toString()==y.toString();
 } 

function ess_alert(value){
	if (enable_alert){
		alert(value);
	}
}

function clearGraph(id){
	var graphObj = document.getElementById(id);
	if (graphObj == null){
		ess_alert("no such graph : "+id);
	}
	else{
		graphObj.src = "images/loading.gif";
	}
}

function setTextValue(id,value){
	var textObj = document.getElementById(id);
	if (textObj == null){
		ess_alert("no such element : "+id);
	}
	else{
		// Checks for browser's feature support and use the correct property 
		if (document.all) {
			textObj.innerText = value;
		} else {
			textObj.textContent = value;		// for Firefox
		}
	}
}

function setTextValueNone(id,value){
	if (value == "" || value == " ") {
		setTextValue(id,"無");
	} else {
		setTextValue(id,value);
	}
}

function setTextValueNoConstraint(id,value){
	if (value == "" || value == " ") {
		setTextValue(id,"不拘");
	} else {
		setTextValue(id,value);
	}
}

function setHtmlValue(id,value){
	var textObj = document.getElementById(id);
	if (textObj == null){
		ess_alert("no such element : "+id);
	}
	else{
		textObj.innerHTML = value;
	}
}

function setAnchorHref(id,href){
	var anchor = document.getElementById(id);
	if (anchor == null){
		ess_alert("no such anchor : "+id);
	}
	else{
		anchor.href = href;
	}
}

/* for set input box value with debug info on error */
function setInputValue ( fieldName, value ){
	var input = document.getElementById(fieldName);
	if (input == null){
		ess_alert("no such input element: "+fieldName);
	}
	else{
		input.value = value;
	}
}

//czhao ess
/* for set list object selected value and provide debug info on error */
function setSelectValue ( fieldName, value ) {
	var select = document.getElementById(fieldName);
	if (select == null ){
		ess_alert ("no such select element: "+fieldName);
	}
	else{
		for (i=0;i < select.length;i++)
		{
				if (select.options[i].value == value ){
					select.options[i].selected=true;
					break;
				}
				
		}
	}
}
//czhao ess
/* for set radio object checked value and provide debug info on error */
function setRadioValue ( fieldName, value ){
	var radio = document.getElementsByName(fieldName);
	if (radio == null){
		ess_alert("no such radio element: "+fieldName);
	}
	else{
		for (i = 0; i < radio.length; i++){
			if (radio[i].value == value){
				radio[i].checked = true;
			}
			else {
				radio[i].checked = false;
			}
		}
	}
}

//czhao ess
/* for set checkbox object checked value and provide debug info on error */
function setCheckboxValue ( fieldName, value ){
	var checkbox = document.getElementById(fieldName);
	if (checkbox == null){
		ess_alert("no such checkbox element: "+fieldName);
	}
	else{
		if (value != ""){
			checkbox.checked = true;
		}
	}
}

function setDivDisplay( fieldName, value ) {
	var divId = document.getElementById(fieldName);
	if (divId == null) {
		ess_alert("no such div block element: "+fieldName);
	}
	else {
		if (value != "") {
			divId.style.display = "block";
		}
		else {
			divId.style.display = "none";
		}
	}
}

function getDivDisplay( fieldName ) {
	var divId = document.getElementById(fieldName);
	if (divId == null) {
		ess_alert("no such div block element: "+fieldName);
	}
	else {
		if (divId.style.display == "none") {
			return false;
		}
		else {
			return true;
		}
	}
}

function toggleDivDisplay(id1, id2) {
	if ( getDivDisplay(id1) ) {
		setDivDisplay(id1, false);
		setDivDisplay(id2, true);
	}
	else {
		if ( getDivDisplay(id2) ) {
			setDivDisplay(id2, false);
			setDivDisplay(id1, true);
		}
		else {
			// Default if both ids' display are none, turn on id1.
			setDivDisplay(id2, false);
			setDivDisplay(id1, true);
		}
	}
}

function setStyleVisibility( fieldName, value ) {
	var elem = document.getElementById(fieldName);
	if (elem == null) {
		ess_alert("setVisibility() no such element: "+fieldName);
	}
	else {
		if (value != "") {
			elem.style.visibility = "visible";
		}
		else {
			elem.style.visibility = "hidden";
		}
	}
}


//chzao ess

/**
Current index structure (Tenant and Landlord):
one page, many content divs
for tenant, currently there are 6 of them, and each div has one or more iframes inside.
we use following javascript functions to control dynamically display of divs upon user selection
this is to avoid refreshing the whole index page.

NOTE: in the vm (html)page, all iframe's src property is set to blank, so there will not be redundant requests
iframe src properties are recorded in tenant_frame and landlord_frame arrays

the key function is changeTenantDiv(value) and changeLandlordDiv(value)
input parameter value : id of the div to be displayed
the functions does following things,
1. on first call, record div's innerHtml in tenant_div or landlord_div array
2. change display property of the selected div to block, and set its innerHtml from _div array.<b> 
3. change display properties of other divs to none, and set its innerHtml to ""
4. change iframe src property to respective pre-recorded value in _frame arrays

However, there are some special requests that can't be handled by these two general functions

a. to submit a form in iframe, triggered by an event in the main window
	functions start with submit:
		submitDeleteFavors()
		submitDeleteApartments()
		...etc.
b. to select all checkbox in an iframe, triggered by an event in the main window (by check or uncheck a checkbox in main window)
	functions start with checkAll
		checkAllFavors()
		...etc.
c. to dynamically set iframe src property, (message_id, object_id, post_id)
	functions start with change
		changeTenantInLandlord()
		...etc
d. to submit a form in main window but, action in iframe (smart search)
	submitSmartSearch()
	submitRentPrice()
	
once these functions are working, instead of using hyperlinks in the main window, we shall use onclick="submitRentPrice()" style 
 
*/

var tenant_div = new Array();
//this array is to store tenant iframe urls
//it is tested that we better set iframe src property to blank when load the page
var tenant_frame = new Array();

var t1 = "tenant_top_10";
var t2 = "tenant_edit";
var t3 = "tenant_favor";
var t4 = "tenant_in_landlord";
var t5 = "tenant_search";
var t6 = "tenant_message";
var t7 = "tenant_subscribe_list";
var t8 = "tenant_subscribe_add";
tenant_frame[t1] = new Array();
tenant_frame[t2] = new Array();
tenant_frame[t3] = new Array();
tenant_frame[t4] = new Array();
tenant_frame[t5] = new Array();
tenant_frame[t6] = new Array();
tenant_frame[t7] = new Array();
tenant_frame[t8] = new Array();
tenant_frame[t1][0] = "?plugins=tenant&action=list";
tenant_frame[t2][0] = "?plugins=tenant-edit-register";
tenant_frame[t3][0] = "?plugins=manage-favor&action=iframe";
tenant_frame[t4][0] = "?plugins=view-post&post_id=";
tenant_frame[t5][0] = "";
tenant_frame[t6][0] = "?plugins=tenant-message&action=list";
tenant_frame[t6][1] = "?plugins=tenant-message&action=iframe&post_id=";
tenant_frame[t7][0] = "?plugins=tenant-subscribe-list&action=iframe";
tenant_frame[t8][0] = "?plugins=tenant-subscribe-add&action=iframe";


function changeTenantDiv(value){

	var divs = new Array();
	divs[t1] = document.getElementById(t1);
	divs[t2] = document.getElementById(t2);
	divs[t3] = document.getElementById(t3);
	divs[t4] = document.getElementById(t4);
	divs[t5] = document.getElementById(t5);
	divs[t6] = document.getElementById(t6);
	divs[t7] = document.getElementById(t7);
	divs[t8] = document.getElementById(t8);
	
	
	
	for (key in divs) {
		if (divs[key] == null){
			ess_alert("no such div: "+key);
		}
		else{
			if (key == value){
				divs[key].style.display = "block";
				if (tenant_div[key] == null || tenant_div[key] == ""){
					tenant_div[key] = divs[key].innerHTML;
				}
				divs[key].innerHTML = "";
				divs[key].innerHTML = tenant_div[key];
				var iframes = divs[key].getElementsByTagName("iframe");
				if (iframes == null){
					ess_alert ("no iframe found for "+key);
				}
				else{
					for(var i = 0 ; i < iframes.length; i ++){
						var iframe = iframes[i];
						iframe.src = tenant_frame[key][i];
					}
				} 
			}
			else{
				divs[key].style.display = "none";
				if (tenant_div[key] == null || tenant_div[key] == ""){
					tenant_div[key] = divs[key].innerHTML;
				}
				divs[key].innerHTML = "";
			}
		}
	}
	
	// Handle dynamic turning on of tenant_in_landlord blog header 
	var divAd = document.getElementById("header_ad");
	var divBlog = document.getElementById("blog");
	if (divAd == null) {
		ess_alert("no div block found for " + divAd);
	}
	else {
		if (divBlog == null) {
			ess_alert("no div block found for " + divBlog);
		}
		else {
			if (value == t4) {
				divAd.style.display = "none";
				divBlog.style.display = "block";
			} else {
				divAd.style.display = "block";
				divBlog.style.display = "none";
			}
		}
	}
	
}

function checkAllFavors(checked){
	checkAll(checked,t3,0);
}
function submitDeleteFavors(){
	submitDelete(t3,0);
}

function checkAllSubscribes(checked){
	checkAll(checked,t7,0);
}
function submitDeleteSubscribes(){
	submitDelete(t7,0);
}

function changeTenantIframe(object_id, header,index){
	changeTenantDiv(header);
	var div = document.getElementById(header);
	if (div == null){
		ess_alert ("no such div: "+header);
	}
	else{
		var iframe = div.getElementsByTagName("iframe")[index];
		if (iframe == null){
			ess_alert ("no iframe found for "+header);
		}
		else{
			iframe.src = tenant_frame[header][index]+object_id;
		}
	}

}

function changeTenantInLandlord(post_id){
	changeTenantIframe(post_id,t4,0);
}

function changeTenantMessage(post_id){
	changeTenantIframe(post_id,t6,1);
	
}

function changeTenantMessageDetail(post_id){
	var div = document.getElementById(t6);
	if (div == null){
		ess_alert ("no such div: "+t6);
	}
	else{
		var iframe = div.getElementsByTagName("iframe")[1];
		if (iframe == null){
			ess_alert ("no iframe found for "+t6);
		}
		else{
			iframe.src = tenant_frame[t6][1]+post_id;
		}
	}
}

function changeTenantMessageList(){
	changeTenantIframe("",t6,0);
	/*
	var div = document.getElementById(t6);
	if (div == null){
		ess_alert ("no such div: "+t6);
	}
	else{
		var iframe = div.getElementsByTagName("iframe")[0];
		if (iframe == null){
			ess_alert ("no iframe found for "+t6);
		}
		else{
			iframe.src = tenant_frame[t6][0];
		}
	}
	*/
}

/* this part of code moved to tenant_index.vm file
function submitSmartSearch(){
	var search_county = document.getElementById("Address_County");
	var search_area = document.getElementById("Search_Area");
	var search_type = document.getElementById("Search_Type");
	var search_size = document.getElementById("Search_Size");
	var search_rent = document.getElementById("Search_Rent");
	
	if (search_county.value == "" || search_type.value == "" || search_size.value == "" || search_rent.value == ""){
		ess_alert("資料不完整");
		return;
	}
	changeTenantDiv(t5);
	var search_form = document.getElementById("matchForm");
	search_form.target = "smart_search_iframe";
	search_form.action = "?plugins=tenant-smart-search";
	search_form.submit();
}
*/

var landlord_div = new Array();
var landlord_frame = new Array();

var l1 = "landlord_home";
var l2 = "landlord_publish_list";
var l3 = "landlord_apartment_list";
var l4 = "landlord_apartment_add";
var l5 = "landlord_apartment_modify";
var l6 = "landlord_publish_add";
var l7 = "landlord_publish_modify";
var l8 = "landlord_edit";
var l9 = "lanlord_message_list";
var l10 = "landlord_room_list";
var l11 = "landlord_room_add";
var l12 = "landlord_room_modify";
var l13 = "landlord_studio_list";
var l14 = "landlord_studio_add";
var l15 = "landlord_studio_modify";
var l16 = "landlord_rentprice";
var l17 = "landlord_blog";
var l18 = "landlord_republish_add";
var l19 = "landlord_history_publish_list";

landlord_frame[l1] = new Array();
landlord_frame[l2] = new Array();
landlord_frame[l3] = new Array();
landlord_frame[l4] = new Array();
landlord_frame[l5] = new Array();
landlord_frame[l6] = new Array();
landlord_frame[l7] = new Array();
landlord_frame[l8] = new Array();
landlord_frame[l9] = new Array();
landlord_frame[l10] = new Array();
landlord_frame[l11] = new Array();
landlord_frame[l12] = new Array();
landlord_frame[l13] = new Array();
landlord_frame[l14] = new Array();
landlord_frame[l15] = new Array();
landlord_frame[l16] = new Array();
landlord_frame[l17] = new Array();
landlord_frame[l18] = new Array();
landlord_frame[l19] = new Array();

landlord_frame[l1][0] = "?plugins=landlord&action=list";
landlord_frame[l2][0] = "?plugins=publish-house&action=list-iframe";
landlord_frame[l3][0] = "?plugins=maintain-apartment-list&action=iframe";
landlord_frame[l4][0] = "?plugins=maintain-apartment-add&action=iframe";
landlord_frame[l5][0] = "?plugins=maintain-apartment-modify&action=iframe&ObjectID=";
landlord_frame[l6][0] = "?plugins=publish-house&action=add-iframe&ObjectID=";
landlord_frame[l7][0] = "?plugins=publish-house&action=modify-iframe&ObjectID=";
landlord_frame[l8][0] = "?plugins=landlord-edit-register";
landlord_frame[l9][0] = "?plugins=landlord-message&action=list&ObjectID=";
landlord_frame[l10][0] = "?plugins=maintain-room-list&action=iframe";
landlord_frame[l11][0] = "?plugins=maintain-room-add&action=iframe";
landlord_frame[l12][0] = "?plugins=maintain-room-modify&action=iframe&ObjectID=";
landlord_frame[l13][0] = "?plugins=maintain-studio-list&action=iframe";
landlord_frame[l14][0] = "?plugins=maintain-studio-add&action=iframe";
landlord_frame[l15][0] = "?plugins=maintain-studio-modify&action=iframe&ObjectID=";
landlord_frame[l16][0] = "?plugins=landlord-rentprice&action=iframe";
landlord_frame[l17][0] = "?plugins=landlord-blog&action=iframe";
landlord_frame[l18][0] = "?plugins=publish-house&action=readd-iframe&post_id=";
landlord_frame[l19][0] = "?plugins=publish-house&action=history-list-iframe";

function changeLandlordDiv(value){

	var divs = new Array();
	divs[l1] = document.getElementById(l1);
	divs[l2] = document.getElementById(l2);
	divs[l3] = document.getElementById(l3);
	divs[l4] = document.getElementById(l4);
	divs[l5] = document.getElementById(l5);
	divs[l6] = document.getElementById(l6);
	divs[l7] = document.getElementById(l7);
	divs[l8] = document.getElementById(l8);
	//divs[l9] = document.getElementById(l9);
	divs[l10] = document.getElementById(l10);
	divs[l11] = document.getElementById(l11);
	divs[l12] = document.getElementById(l12);
	divs[l13] = document.getElementById(l13);
	divs[l14] = document.getElementById(l14);
	divs[l15] = document.getElementById(l15);
	divs[l16] = document.getElementById(l16);
	divs[l17] = document.getElementById(l17);
	divs[l18] = document.getElementById(l18);
	divs[l19] = document.getElementById(l19);
	
	for (key in divs) {
		if (divs[key] == null){
			ess_alert("no such div: "+key);
		}
		else{
			if (key == value){
				divs[key].style.display = "block";
				if (landlord_div[key] == null || landlord_div[key] == ""){
					landlord_div[key] = divs[key].innerHTML;
				}
				divs[key].innerHTML = "";
				divs[key].innerHTML = landlord_div[key];
				var iframes = divs[key].getElementsByTagName("iframe");
				if (iframes == null){
					ess_alert ("no iframe found for "+key);
				}
				else{
					for(var i = 0 ; i < iframes.length; i ++){
						var iframe = iframes[i];
						iframe.src = landlord_frame[key][i];
					}
				} 
			}
			else{
				divs[key].style.display = "none";
				if (landlord_div[key] == null || landlord_div[key] == ""){
					landlord_div[key] = divs[key].innerHTML;
				}
				divs[key].innerHTML = "";
			}
		}
	}
}

function submitRentPrice(){
	var search_form = document.getElementById("priceForm");
	search_form.target = "rentprice_iframe";
	search_form.action = "?plugins=landlord-rentprice&action=show";
	search_form.submit();
}


function changeLandlordIframe(object_id, header,index){
	changeLandlordDiv(header);
	var div = document.getElementById(header);
	if (div == null){
		ess_alert ("no such div: "+t1);
	}
	else{
		var iframe = div.getElementsByTagName("iframe")[index];
		if (iframe == null){
			ess_alert ("no iframe found for "+header);
		}
		else{
			iframe.src = landlord_frame[header][index]+object_id;
		}
	}

}

function changeApartmentModify(object_id){
	changeLandlordIframe(object_id,l5,0);
}

function changeRoomModify(object_id){
	changeLandlordIframe(object_id,l12,0);
}

function changeStudioModify(object_id){
	changeLandlordIframe(object_id,l15,0);
}


function changePublishModify(object_id){
	changeLandlordIframe(object_id,l7,0);
}

function changeRepublishAdd(post_id){
	changeLandlordIframe(post_id,l18,0);
}

function changeDirectPublishAdd(object_id){
	changeLandlordIframe(object_id,l6,0);
}

function changeLandlordMessageList(object_id){
	changeLandlordIframe(object_id,l9,0);
}

function checkAllCheckboxes(check_name,checked){
	var check_boxes = document.getElementsByName(check_name);
	if (check_boxes == null){
		ess_alert("no such checkbox elements: "+check_name);
	}
	else{
		for ( i = 0; i < check_boxes.length ; i ++ ){
			check_boxes[i].checked = checked;
		}
	}
}

function checkAll(checked, header,index){
	var t1 = header;
	var div = document.getElementById(t1);
	if (div == null){
		ess_alert("no such div element: "+t1);
	}
	else{
		var iframe = div.getElementsByTagName("iframe")[index];
		if (iframe == null){
			ess_alert ("no iframe found for "+t1);
		}
		else{
			iframe.contentWindow.checkAllCheckboxes("delete",checked);
		}
	}
}
function submitDelete(header,index){
	if (!confirm ("確定要刪除嗎？")){
		return false;
	}
	var t1 = header;
	var div = document.getElementById(t1);
	if (div == null){
		ess_alert("no such div element: "+t1);
	}
	else{
		var iframe = div.getElementsByTagName("iframe")[index];
		if (iframe == null){
			ess_alert ("no iframe found for "+t1);
		}
		else{
			iframe.contentWindow.document.getElementsByTagName("form")[0].submit();
		}
	}
}

function checkAllApartments(checked){
	checkAll(checked,"landlord_apartment_list",0);
}
function submitDeleteApartments(){
	submitDelete("landlord_apartment_list",0);
}

function checkAllRooms(checked){
	checkAll(checked, "landlord_room_list",0);
}
function submitDeleteRooms(){
	submitDelete("landlord_room_list",0);
}

function checkAllStudios(checked){
	checkAll(checked, "landlord_studio_list",0);
}
function submitDeleteStudios(){
	submitDelete("landlord_studio_list",0);
}

function checkAllPosts(checked){
	checkAll(checked,"landlord_publish_list",0);
}

function submitDeletePosts(){
	submitDelete("landlord_publish_list",0);
}

function randomizeImageUrl(image){
	var img = document.getElementById(image);
	if (img == null){
		ess_alert("no such image element: "+image);
	}
	else{
		img.src = img.src + "?" + Math.random();
	}
}
function createHtmlAttribute(name, value) {
   var attribute = document.createAttribute(name);
   attribute.nodeValue = value;
   return attribute;
}
function createHiddenInput(frmId,name,value){
	var formElement = document.getElementById(frmId);
	if (formElement != null){
		var inputElement = document.createElement("input");
		inputElement.setAttributeNode(createHtmlAttribute("type", "hidden"));
		inputElement.setAttributeNode(createHtmlAttribute("name", name));
		inputElement.setAttributeNode(createHtmlAttribute("id", name));
		inputElement.setAttributeNode(createHtmlAttribute("value", value));
		formElement.appendChild(inputElement);
	}
	else{
		ess_alert("no such form object: "+frmId);
	}
}





// Functions below are used by new ver 4 UI




// Function to return the message 早安 午安 晚安 based on current time

function getGreeting() {

	var now = new Date();
	var hour = now.getHours();
	var minute = now.getMinutes();

	var hrmin = hour * 100 + minute;

	// 4:00am - 12:00pm = morning
	var greeting = "早安";

	// 6:00PM - 4:00AM = night
	if (hrmin >= 1800 || hrmin <= 400) {
	  greeting = "晚安";
	}

	// 12:00pm - 6:00pm = afternoon
	if (hrmin >= 1200 && hrmin < 1800) {
	  greeting = "午安";
	}
	return greeting;
}

function doLogoutConfirm() { //v2.0
  	var yesLogout =confirm("確定登出?");
	if (yesLogout){
		self.location = "?plugins=index&action=logout";
	}else{
		alert('歡迎繼續使用，謝謝');
	}
}

function setImgOnClickEvent(elementId, functionToInvoke) {
	var img = document.getElementById(elementId);
	if (img == null) {
		ess_alert("setImgOnClickEvent() element not found: "+elementId);
	} else {
		img.onclick = functionToInvoke;
	}
}

function setOnChangeEvent(elementId, functionToInvoke) {
	var elem = document.getElementById(elementId);
	if (elem == null) {
		ess_alert("setOnChangeEvent() element not found: "+elementId);
	} else {
		elem.onchange = functionToInvoke;
	}
}

function setOnKeyupEvent(id, func) {
	var elem = document.getElementById(id);
	if (elem == null) {
		ess_alert("setOnKeyupEvent element not found: "+id);
	} else {
		elem.onkeyup = func;
	}
}

function setOnErrorEvent(id, func) {
	var elem = document.getElementById(id);
	if (elem == null) {
		ess_alert("setOnErrorEvent element not found: "+id);
	} else {
		elem.onerror = func;
	}
}

function setOnLoadEvent(id, func) {
	var elem = document.getElementById(id);
	if (elem == null) {
		ess_alert("setOnLoadEvent element not found: "+id);
	} else {
		elem.onload = func;
	}
}

function setFormAction(elementId, action) {
	var elem = document.getElementById(elementId);
	if (elem == null) {
		ess_alert("setFormAction() element not found: "+elementId);
	} else {
		elem.action = action;
	}
}

function setDivInnerTextValue(elementId, value) {
	var divId = document.getElementById(elementId);
	if (divId == null) {
		ess_alert("setDivInnerTextValue() element not found: "+elementId);
	}
	else {
		// Checks for browser's feature support and use the correct property 
		if (document.all) {
			divId.innerText = value;
		} else {
			divId.textContent = value;		// for Firefox
		}
	}
}


// Blog banner update functions

function setFontStyle(elementId, style, value) {
	var myElem = document.getElementById(elementId);
	if (myElem == null) {
		ess_alert("setFontStyle element not found: "+elementId);
	}
	else {
		switch(style) {
			case "size":	myElem.style.fontSize=value; break;
			case "family":	myElem.style.fontFamily=value; break;
			case "style":	myElem.style.fontStyle=value; break;
			case "color":	myElem.style.color=value; break;
			case "text":    
				// Checks for browser's feature support and use the correct property 
				if (document.all) {
					myElem.innerText = value;
				} else {
					myElem.textContent = value;		// for Firefox
				}
				break;
		}
	}
}

function setBgImage(elementId, value) {
	var myElem = document.getElementById(elementId);
	if (myElem == null) {
		ess_alert("setBgImage element not found: "+elementId);
	}
	else {
		var newBg = 'url(' + value + '?' + Math.random() + ')';
		myElem.style.background=newBg;
	}
}

function setPicImage(elementId, value) {
	var myElem = document.getElementById(elementId);
	if (myElem == null) {
		ess_alert("setPicImage element not found: "+elementId);
	}
	else {
		document.getElementById(elementId).src=value;
	}
}


// AJAX functions

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}

	return objXMLHttp;
}

// The one should go into Andy's js files.

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function getInputElementValue(elementId){
	var element = document.getElementById(elementId);
	if (element!=null){
		return element.value;
	}
	else{
		ess_alert("getInputElementValue() : element not found: "+elementId);
	}
}
function setVisibility(id,value){
	var element = document.getElementById(id);
	if (element != ""){
		if (value == true){
			element.style.visibility = "visible";
		}
		else{
			element.style.visibility = "hidden";
		}
	}
	else{
		ess_alert("setVisibility() : element not found: "+id);
	}
}

function setDisplay(id,value){
	var element = document.getElementById(id);
	if (element != ""){
		if (value == true){
			element.style.display = "block";
		}
		else{
			element.style.display = "none";
		}
	}
	else{
		ess_alert("setVisibility() : element not found: "+id);
	}
}

function setOnchangeEventHandle(id, value){
	var element = document.getElementById(id);
	if (element != ""){
		element.onchange = value;
	}
	else{
		ess_alert("setOnchangeEventHandle() : element not found: "+id);
	}
}

function setOnclickEventHandle(id, value){
	var element = document.getElementById(id);
	if (element != ""){
		element.onclick = value;
	}
	else{
		ess_alert("setOnclickEventHandle() : element not found: "+id);
	}
}


// This is used by landlord and user during registration and edit registration
function validateUserForm(mandatories, integers, oneWillDo){
	result = true;
	error = "";
	if(mandatories!=null){
		for (word in mandatories){
			var inputItem = document.getElementById(word);
			if (inputItem == null){
				ess_alert("no such element: "+word);
				error += "沒有以下element: "+word;
				result = false;
			}
			else{
				if (inputItem.type == "radio"){
					var hasValue = false;
					var radioItem = document.getElementsByName(inputItem.name);
					for (i = 0; i < radioItem.length; i++){
						if (radioItem[i].checked){
							hasValue = true;
							break;
						}
					}
					if(!hasValue){
						error += "必須填寫"+mandatories[word]+"\n";
						result = false;
					}
				} else {
					if (inputItem.value == ""){
						error += "必須填寫"+mandatories[word]+"\n";
						result = false;
					}
				}
			}
		}
	}
	if (integers!=null){
		for (word in integers){
			var inputItem = document.getElementById(word);
			if (inputItem == null){
				ess_alert("no such element: "+word);
				error += "沒有以下element: "+word;
				result = false;
			}
			else{
				if (inputItem.value != "" && !isInt1(inputItem.value)){
					error += integers[word]+"必須是數字\n";
					result = false;
				}
			}
		}
	}

	if (oneWillDo!=null) {	
		var allBlanks = true;
		for (word in oneWillDo){
			var inputItem = document.getElementById(word);
			if (inputItem == null){
				ess_alert("no such element: "+word);
				result = false;
			}
			else{
				if (inputItem.value != ""){
					allBlanks = false;
				}
			}
		}
		if (allBlanks) {
			result = false;
			error += "必須填寫其一:";
			for (word in oneWillDo){
				error += oneWillDo[word]+"  ";
			}
		}
	}
	
	if (!result){
		alert(error);
	}
	return result;
}


// This is used by landlord and normal-user during landlord rent price
function validateLandlordRentPriceSelectBox(){
	var mandatories = {
		"Address_County":"區域1:縣市",
		"LocalArea":"區域1:行政區"
		};
	result = true;
	error = "";
	if(mandatories!=null){
		for (word in mandatories){
			var inputItem = document.getElementById(word);
			if (inputItem == null){
				ess_alert("no such element: "+word);
				error += "沒有以下element: "+word;
				result = false;
			}
			else{
				if (inputItem.value == ""){
					error += "必須選擇"+mandatories[word]+"\n";
					result = false;
				}
			}
		}
	}
	if (!result){
		alert(error);
	}
	return result;
}


function checkPasswords(){
	var result = true;
	var password =  document.getElementById('Password').value;
	if ( password  == document.getElementById('ConfirmPassword').value){
		if(password.length > 8 || password.length < 1){
	        result = false;	        
			alert("密碼最少1碼，最多8碼!");
		}
	}
	else{
		alert("密碼不一致，請重新輸入");
		result = false;
	}
	return result;
}

function getCheckedCheckBoxes(name){
	var values = new Array();
	try{
		var elements = document.getElementsByName(name);
		for (var i = 0 ; i < elements.length; i ++){
			var element = elements[i];
			if (element.checked){
				values[values.length] = element.value; 				
			}
		}
	}
	catch(e){
		ess_alert(e.description);
	}
	return values;
}

function getSkypeOut(countryCode,areaCode,phoneNum) {
	var values = countryCode;
	if (areaCode.indexOf('0')==0) {
		values += areaCode.substr(1);
	} else {
		values += areaCode;
	}
	if (phoneNum.indexOf('09')==0) {
		values += phoneNum.substr(1);
	} else {
		values += phoneNum;
	}
	return values;
}

// Given the password, return us a string equal to the same length as original but replaced with asterisks 
function getMaskedPassword(pw) {
	var value = "";
	if (pw != null) {
		for (i = 0; i < pw.length; i++) {
			value += '*';
		}
	}
	return value;
}

function showHideElement(id){
	try{
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = "";
		}
		else{
			document.getElementById(id).style.display = "none";
		}
	}
	catch(e){ess_alert(e.description);}
}

// This is used primarily by house detail page when non-login user hits on button that only logged user can access

function alertNeedLogin() {
	alert('您必須加入會員才能使用此功能，謝謝!');
}

function alertNeedTenant() {
	alert('此功能只有房客才能使用，謝謝!');
}

function alertNeedLandlord() {
	alert('此功能只有房東才能使用，謝謝!');
}


function checkIsPopout() {
	if (window.opener) {
		window.opener.focus();
		window.close();
	}
}

function limitTextDisplay(text, limit) {
	if (text.length > limit) {
		return text.slice(0,limit)+"...";
	} else {
		return text;
	}
}
