var floating_tab = null;

String.prototype.trim = function() {	return this.replace(/^\s+|\s+$/g,"");}
String.prototype.ltrim = function() {	return this.replace(/^\s+/,"");}
String.prototype.rtrim = function() {	return this.replace(/\s+$/,"");}

  function selectImageToHolder(img, holderId, typeId){
      var holder = document.getElementById(holderId);
      if (null != img && null != holder){
          holder.value = img.id;
          
          var property = document.getElementById(typeId);
          if (null != property) {
            var type = holder.value.split('-');
            property.value = type[0].toLowerCase();
          }
      }
  }
  
  function markHomeSearchIcon(id, isFocus){
      var img = document.getElementById(id);
      if (null != img){
          var url = img.src;
          var tempArray = url.split("/");
          var imageName = tempArray[tempArray.length - 1];
          var newName;
          if(!isFocus){
              newName = "grey_" + imageName.replace("grey_", "");
          }else{
              newName = imageName.replace("grey_", "");
          }
          img.src = img.src.replace(imageName, newName);
      }
  }
  
  function setHomeSearchClass(id, isFocus){
      var e = document.getElementById(id);
      if (null != e){
          if (isFocus){
              e.className = "home-icon-selected";
          }else{
              e.className = "home-icon-unselected";
          }
      }
  }
  
  function setClass(e, className){
      if (null == e.id){
        e = document.getElementById(e);
      }
      if (null != e){
          e.className = className;
      }
  }
  
  function setColor(e, color){
      if (null == e.id){
        e = document.getElementById(e);
      }
      if (null != e){
          e.style.color = color;
      }
  }
  
  function setSearchArrow(selectedIcon, root, arrowId, searchBoxId, isIE, offsetX, offsetY){
      var selectedTriangleDiv = document.getElementById(arrowId);
      var searchBoxElement = document.getElementById(searchBoxId);
      var relTop = getAbsTop(searchBoxElement, isIE) ;          
      var relLeft = getAbsLeft(selectedIcon, isIE);
      var x, y;
      x = relLeft + selectedIcon.style.width.replace("px","") / 2 ;
      y = relTop ;
      if (null != offsetX){
          x = x + offsetX;
      }
      if (null != offsetY){
          y = y + offsetY;
      }
      selectedTriangleDiv.style.left = x + "px";
      selectedTriangleDiv.style.top = y + "px";
      
    }
    
  function mouseOnHomeSearchIcon(icon, holderId){
      var holder = document.getElementById(holderId);
      var selected = document.getElementById(holder.value);
      var typeStr = "CONDO-VILLA-OFFICE-SHOP-INDUSTRIAL-PARKING_SPACE";
      var typeArray = typeStr.split("-");
      var tempArray = icon.id.split("-");
      var name = tempArray[0];
      var selectedType = "";
      if (null != selected){
          selectedType = selected.id.split("-")[0];
      }
      for (var i=0; i < typeArray.length; i++){
          var type = typeArray[i];
          var isFocus = false;
          if (type == name || type == selectedType){
              isFocus = true;
          }
          var cellId = type + "-search-cell";
          var imgId = type + "-search-icon";
          var iconId = type + "-search-icon";
          setHomeSearchClass(cellId, isFocus);
          setHomeSearchClass(imgId, isFocus);
          markHomeSearchIcon(iconId, isFocus);
      }
  }
  
  function mouseOutHomeSearchIcon(icon, holderId){
      var holder = document.getElementById(holderId);
      var selected = document.getElementById(holder.value);
      var isFocus = false;
      if (selected.id == icon.id){
          isFocus = true;
      }
      var current_type = icon.id.split("-")[0];
      var cellId = current_type + "-search-cell";
      var imgId = current_type + "-search-icon";
      var iconId = current_type + "-search-icon";
      setHomeSearchClass(cellId, isFocus);
      setHomeSearchClass(imgId, isFocus);
      markHomeSearchIcon(iconId, isFocus);
  }
  
  function selectHomeSearchIcon(icon, rootId, holderId, arrowId, searchBoxId, isIE, offsetX, offSetY, typeId){
      selectImageToHolder(icon, holderId, typeId);
      mouseOnHomeSearchIcon(icon, holderId);
      var root = document.getElementById(rootId);
      setSearchArrow(icon, root, arrowId, searchBoxId, isIE, offsetX, offSetY);
  }
  
function switchPropertyType(parentId, currentId, imageFolder, containerId ){
    var element = document.getElementById(currentId);
	if (null != element){
		var parent = document.getElementById(parentId);
        if (null != parent){
             for(var i=0; i < parent.all.length; i++){
                var temp = parent.all(i);  
                if ("input" == temp.tagName.toLowerCase()){
                    var unselectedImagePath = 'url(' + imageFolder + "\\" + "button_" + temp.category + ".gif)";
                    temp.style.backgroundImage = unselectedImagePath;
                }
            }
        }
        var selectedImagePath = 'url(' + imageFolder + "\\" + "button_" + element.category + "_mouseon.png)";
        element.style.backgroundImage = selectedImagePath;
        var container = document.getElementById(containerId);
        if (null != container){
            container.style.backgroundImage = 'url(' + imageFolder + "\\" + "arrow_" + element.category + ".gif)";
        }
	}
}
// setFloatingTabVisitility('show', 'register-tab', this.id, -440)
function setFloatingTabVisitility(status, tabId, showButtonId, offsetX, offsetY){
    var tab = document.getElementById(tabId);
	var iframeMask = document.getElementById('iframeMask');
    if (null != tab){
        if ('show' == status){
			iframeMask.style.display = "";
            tab.style.display = "";
            if (null != floating_tab && tab != floating_tab){
                floating_tab.style.display = "none";
            }
            if (null != showButtonId){
                var showButton = document.getElementById(showButtonId);
                if (null != showButton){
                    var left = getAbsLeft(showButton);
                    var tempHeight = showButton.clientHeight;
                    if (0 == tempHeight){
                        tempHeight = 10;
                    }
                    var top = tempHeight + getAbsTop(showButton);
                    if (null != offsetX){
                        left += offsetX;
                    }
                    if (null != offsetY){
                        top += offsetY;
                    }
                    tab.style.zIndex = 50;
                    tab.style.position = "absolute";
                    if ( 0 <= left){
                        tab.style.left = left + "px";
                    }
                    if ( 0 <= top){
                        tab.style.top = top + "px";
                    }
                    floating_tab = tab;
                }
            }
        }
        else{
			iframeMask.style.display = "none";
            tab.style.display = "none";
            if (null != floating_tab){
                floating_tab.style.display = "none";
                floating_tab = null;
            }
        }
    }
}

function setFloatingProfileTabVisitility(status, tabId, containerId){
    var tab = document.getElementById(tabId);
    if (null != tab){
        if ('show' == status){
            tab.style.display = "";
            if (null != containerId){
                var container = document.getElementById(containerId);
                if (null != container){
                    var left = getAbsLeft(container);
                    var top = getAbsTop(container);
                    tab.style.zIndex = 10;
                    tab.style.position = "absolute";
                    tab.style.left = left + container.clientWidth;
                    tab.style.top = top;
                }
            }
        }
        else{
            tab.style.display = "none";
        }
    }
}

function getRelativeLeft(e, root){
    var result = 0;
    if (null != e){
          result = e.offsetLeft;
          while(null != e.offsetParent && (e != root || null == root)) 
          {  
            e = e.offsetParent; 
            result += e.offsetLeft; 
          }
    }
  return result;
}

function getRelativeTop(e, root){
    var result = 0;
    if (null != e){
          result = e.offsetTop;
          while(null != e.offsetParent  && (e != root || null == root)) 
          {  
            e = e.offsetParent;
            result += e.offsetTop; 
          }
    }
  return result;
}


function getAbsLeft(e, isIE){
    var result = 0;
    if (null != e){
      if (!isIE || null == isIE){
          result = e.offsetLeft;
          while(null != e.offsetParent) 
          {  
            e = e.offsetParent; 
            result += e.offsetLeft; 
          }
      }
      else{
        result = e.getBoundingClientRect().left;
      }
    }
  return result;
}

function getAbsTop(e, isIE){
    var result = 0;
    if (null != e){
        if (!isIE || null == isIE){
          result = e.offsetTop;
          while(null != e.offsetParent) 
          {  
            e = e.offsetParent;
            result += e.offsetTop; 
          }
        }
        else{
          result = e.getBoundingClientRect().left;
        }
    }
  return result;
}

function switchOtherLocationRow(selectId, rowId){
    var select = document.getElementById(selectId);
    var row = document.getElementById(rowId);
    if (null != select && null != row){
        if ("other" == select.value){
            row.style.display = "";
        }
        else{
            row.style.display = "none";
        }
    }
}

function showPanel(containerId, index){
    var table = document.getElementById(containerId);
    table.style.position = "relative";
    table.style.zIndex = index;
    table.marginBottom = "0px";
}

function switchDiv(selectedId, oldId, statusId){
    var selected = document.getElementById(selectedId);
    var old = document.getElementById(oldId);
    var status = document.getElementById(statusId);
    if (null != selected && null != old && null != status){
        selected.style.backgroundImage = "url(templates/macauabc/images/white_tab.gif)";
        old.style.backgroundImage = "url(templates/macauabc/images/grey_tab.gif)";
        status.value = selected.category;
    }
}

function switchDivContent(id1, id2, category, statusId){
    var temp1 = document.getElementById(id1);
    var temp2 = document.getElementById(id2);
    var status = document.getElementById(statusId);
    if(null != temp1 && null != temp2 ){
        var display1;
        var display2;
        if('individual' == category || 'corporate' == category){
            if("none" == temp1.style.display && 'individual' != category){
                display1 = "";
                display2 = "none";
            }else if('corporate' != category){
                display1 = "none";
                display2 = "";
            }
            if (null != display1 && null != display2){
                temp1.style.display = display1;
                temp2.style.display = display2;
            }
        }
    }
}

function setIndex(selectedId, originalId, tableId, startIndex){
    var selected = document.getElementById(selectedId);
    var original = document.getElementById(originalId);
    //var table = document.getElementById(tableId);
    if (null != selected && null != original){
        //original.style.zIndex = startIndex;
        //table.style.zIndex = startIndex + 1;
        //selected.style.zIndex = startIndex + 2;
        showPanel(selectedId, startIndex + 2);
        //showPanel(originalId, startIndex);
    }
}

function switchPanelZIndex(containerId1, containerId2){
    var container1 = document.getElementById(containerId1);
    var container2 = document.getElementById(containerId2);
    if (null != container1 && null != container2){
        var index1 = container1.style.zIndex;
        var index2 = container2.style.zIndex;
        showPanel(containerId1, index2);
        showPanel(containerId2, index1);
    }
}

function switchRow(parentId, statusId, imageButtonId, hidePath, showPath, keepStatus){
    var parent = document.getElementById(parentId);
    var status = document.getElementById(statusId);
    if (null != parent && null != status){
        var display;
        var path;
        if ('show' == status.value){
            display = "none";
            path = hidePath;
            if (null == keepStatus || !keepStatus) {
              status.value = "hide";
            }
        }else{
            display = "";
            path = showPath;
            if (null == keepStatus || !keepStatus) {
              status.value = "show";
            }
        }
        parent.style.display = display;
        if(null != imageButtonId){
            var image = document.getElementById(imageButtonId);
            if (null != image){
                image.src = path;
            }
        }
    }
}

function switchArrowUpDown(imgId, statusId){
    var img = document.getElementById(imgId);
    var status = document.getElementById(statusId);
    if (null != img && null != status){
        if ("hide" == status.value){
            img.src = "templates/macauabc/images/arrow_down.gif";
        }else{
            img.src = "templates/macauabc/images/arrow_up.gif";
        }   
    }
}

function setNextStatus(id){
    var item = document.getElementById(id);
    if ("hide" == item.value){
        item.value = "show";
    }else{
        item.value = "hide";
    }
}
function isIE(){
    var name = "Microsoft Internet Explorer";
    var result = false;
    if (window.navigator.appName == name){
        result = true;
    }
    return result;
}

function highlightTab(currentId, selected){
  var current = document.getElementById(currentId);
  var myArray = current.id.split("-");
  var table = document.getElementById(myArray[0] + "-" + myArray[1] + "-content");
  if (selected.id == current.id){
      current.className = "selected-tab";
      table.style.display = "";
  }else{
      current.className = "unselected-tab";
      table.style.display = "none";
  }
}

function selectTab(tab, name){
    highlightTab(name + "-CONDO-menu", tab);
    highlightTab(name + "-VILLA-menu", tab);
    highlightTab(name + "-OFFICE-menu", tab);
    highlightTab(name + "-SHOP-menu", tab);
    highlightTab(name + "-INDUSTRIAL-menu", tab);
    highlightTab(name + "-PARKING_SPACE-menu", tab);
}

function selectLeftTab(tab, total, statusHolderId){
    var statusHolder = document.getElementById(statusHolderId);
     if (null != statusHolder){
        for(var i = 1; i<= total; i++){
            var id = "hot-picks-select-tab-" + i;
            var tempTab = document.getElementById(id);
            if (null != tempTab){
                tempTab.className = "unselect-left-tab";
            }
        }
        var index;
        if(null == tab){
             index = statusHolder.value;
        }
        else{
            var tempArray = tab.id.split("-");
            index = tempArray[tempArray.length - 1];
        }
            
        var current_tab = document.getElementById("hot-picks-select-tab-" + index);
        current_tab.className = "select-left-tab";
        var tableId = "hot-picks-table-" + index;
        for(var i = 1; i<= total; i++){
            var tempTableId = "hot-picks-table-" + i;
            var tempTable = document.getElementById(tempTableId);
            if (null != tempTable){
                tempTable.style.display = "none";
            }
        }
        var table = document.getElementById(tableId);
        if (null != table){
            table.style.display = "";
        }
   
        statusHolder.value = index % total + 1;
    }
}    

function changeHotPicksAutomatically(id, timeout, max){
    var e = document.getElementById(id);
    if (null != e && 0 < timeout){
        selectLeftTab(null, max, 'hot-pick-current-index-holder');
        var tempArray = id.split("-");
        var currentStatus = document.getElementById('hot-pick-current-index-holder');
        var index;
        if (null != currentStatus && 0 < currentStatus.value.length){
            index = currentStatus.value;
        }else{
            index = tempArray[tempArray.length - 1];
        }
        var newIndex = index % max + 1;
        var newName = "";
        for (var i = 0; i < tempArray.length - 1; i++){
            newName = newName + tempArray[i] + "-";
        }
        newName = newName + newIndex;
        var handler = "changeHotPicksAutomatically('" + newName + "'," + timeout + "," + max + ")";
        setTimeout(handler, timeout);
    }
}

function selectPropertyType(e){
  var selectedTriangleDiv = document.getElementById("selectedTriangle");
  var searchBoxElement = document.getElementById("searchBox");
  var absTop = getAbsTop(searchBoxElement);          
  var absLeft = getAbsLeft(e);
  selectedTriangleDiv.style.left = absLeft + e.width / 2 - 4.5;
  selectedTriangleDiv.style.top = absTop - 10;
}

function selectImage(img){
  var divElement = img.parentNode;
  var bigImageElement = document.getElementById("demoImage");
  var images = divElement.getElementsByTagName("img");
  var imgCount = images.length;
  for(var i=0; i < imgCount; i++){
      var imgElement = images.item(i);
      imgElement.className = "unselected";
  }
  img.className = "selected";
  bigImageElement.src = img.src;
}

function switchDistrictDiv(parentId){
    var parent = document.getElementById(parentId);
    var child = document.getElementById("sub-" + parentId);
    var checkedStatus = "checked";
    
    if (null != parent && null != child){
        if (parent.checked){
            child.style.display = "";
        } else {
            checkedStatus = "";
        }
        
        var childCheckBox = child.getElementsByTagName("input");
        
        if (null != childCheckBox) {
            for (i = 0; i < childCheckBox.length; i++) {
                if ("checkbox" == childCheckBox[i].type) {
                    childCheckBox[i].checked = checkedStatus;
                }
            }
        }
    }
}

function checkStatus(parentId) {
  var parent = document.getElementById(parentId);
  var child = document.getElementById("sub-" + parentId);
  var checkedStatus = "";
  
  if (null != child) {
      var childCheckBox = child.getElementsByTagName("input");
      
      if (null != childCheckBox) {
          for (i = 0; i < childCheckBox.length; i++) {
              if ("checkbox" == childCheckBox[i].type && childCheckBox[i].checked) {
                  checkedStatus = "checked";
              }
          }
      }
  }
  
  if (null != parent && null != child) {
      parent.checked = checkedStatus;
  }
  
  if ("" == checkedStatus && null != child) {
      child.style.display = "none";
  }
}

function selectPropertyTypeImg(imgId, statusId){
    var img = document.getElementById(imgId);
    var status = document.getElementById(statusId);
    if (null != img && null != status){
        var images = img.parentNode.parentNode.getElementsByTagName("img");
        
        for( var i = 0; i < images.length; i++){
            images(i).parentNode.className = "unselected";
        }
        img.parentNode.className = "selected";
        var selected = document.getElementById(status.value);
        if (null != selected){
            selected.parentNode.className = "selected";
        }
    }
}

function switchTabBackgound(spanId, statusId){
    var span = document.getElementById(spanId);
    var status = document.getElementById(statusId);
    if (null != span && null != status){
        var individual_left = document.getElementById('individual-left-tab');
        var individual_content = document.getElementById('individual-content-tab');
        var individual_right = document.getElementById('individual-right-tab');
        var corporate_left = document.getElementById('corporate-left-tab');
        var corporate_content = document.getElementById('corporate-content-tab');
        var corporate_right = document.getElementById('corporate-right-tab');
        if( null != individual_left &&
            null != individual_content &&
            null != individual_right &&
            null != corporate_left &&
            null != corporate_content &&
            null != corporate_right ){
                var pic_individual_left;
                var pic_individual_content;
                var pic_individual_right;
                var pic_corporate_left;
                var pic_corporate_content;
                var pic_corporate_right;
                
                if("individual" == span.category){
                    pic_individual_left = "url(" + "images/whitetab_left.gif)";
                    pic_individual_content = "url(" + "images/whitetab_center.gif)";
                    pic_individual_right = "url(" + "images/whitetab_right.gif)";
                    pic_corporate_left = "url(" + "images/greytab_left.gif)";
                    pic_corporate_content = "url(" + "images/greytab_center.gif)";
                    pic_corporate_right = "url(" + "images/greytab_right.gif)";
                }else{
                    pic_corporate_left = "url(" + "images/whitetab_left.gif)";
                    pic_corporate_content = "url(" + "images/whitetab_center.gif)";
                    pic_corporate_right = "url(" + "images/whitetab_right.gif)";
                    pic_individual_left = "url(" + "images/greytab_left.gif)";
                    pic_individual_content = "url(" + "images/greytab_center.gif)";
                    pic_individual_right = "url(" + "images/greytab_right.gif)";
                }
                
                individual_left.style.backgroundImage = pic_individual_left;
                individual_content.style.backgroundImage = pic_individual_content;
                individual_right.style.backgroundImage = pic_individual_right;
                corporate_left.style.backgroundImage = pic_corporate_left;
                corporate_content.style.backgroundImage = pic_corporate_content;
                corporate_right.style.backgroundImage = pic_corporate_right;
                
                status.value = span.category;
        }
    }
}

function forwardMenu(forwardId, backId, currentShowMenuIds, nextShowMenuIds, name, direction){
    var forward = document.getElementById(forwardId);
    var back = document.getElementById(backId);
    var forwardVisibility, backVisibility;
    if ( "forward" == direction){
        backDisplay = "";
        forwardDisplay = "none";
    }else{
        forwardDisplay = "";
        backDisplay = "none";
    }
    forward.style.display = forwardDisplay;
    back.style.display = backDisplay;
    var array = currentShowMenuIds.split(",");
    for (var i = 0; i < array.length; i++){
        var temp = document.getElementById(array[i]);
        temp.style.display = "none";
    }
    
    var array = nextShowMenuIds.split(",");
    for (var i = 0; i < array.length; i++){
        var temp = document.getElementById(array[i]);
        temp.style.display = "";
        if (i == 0){
            selectTab(temp, name);
        }
    }
}

function getValidId(id, full_name, condition){
    var temp = document.getElementById(full_name);
    if (null != temp && condition != temp.style.display){
        if ( 0 != id.length){
            id = id + ",";
        }
        id = id + temp.id;
    }
    return id;
}

function getDisplayedMenuIds(name){
    var id = "";
    id = getValidId(id, name + "-CONDO-menu", "none");
    id = getValidId(id, name + "-VILLA-menu", "none");
    id = getValidId(id, name + "-OFFICE-menu", "none");
    id = getValidId(id, name + "-SHOP-menu", "none");
    id = getValidId(id, name + "-INDUSTRIAL-menu", "none");
    id = getValidId(id, name + "-PARKING_SPACE-menu", "none");
    return id;
}

function getNonDisplayedMenuIds(name){
    var id = "";
    id = getValidId(id, name + "-CONDO-menu", "");
    id = getValidId(id, name + "-VILLA-menu", "");
    id = getValidId(id, name + "-OFFICE-menu", "");
    id = getValidId(id, name + "-SHOP-menu", "");
    id = getValidId(id, name + "-INDUSTRIAL-menu", "");
    id = getValidId(id, name + "-PARKING_SPACE-menu", "");
    return id;
}

function login_user(emailId, passwordId, requestAddress, replyHandler){
    var email = document.getElementById(emailId);
    var password = document.getElementById(passwordId);
    if (null != email && null != password){
        var postData = "email=" + email.value + "&password=" + password.value;
        makeRequest(postData, requestAddress, replyHandler);
    }
}


var handleFailure = function(o){
  if(o.responseText !== undefined){
    alert(o.responseText);
  }
}

function makeRequest(postData, requestAddress, replyHandler, uploadHanlder, formId){
    var callback =
    {
      success:replyHandler,
      failure:handleFailure,
      upload:uploadHanlder
    };
    if ( null != formId) {
      var form = document.getElementById(formId);
      YAHOO.util.Connect.setForm(form, true, true);  
    }
  var request = YAHOO.util.Connect.asyncRequest('POST', requestAddress, callback, postData);
}


function switchDisplay(id1, id2){
    var e1 = document.getElementById(id1);
    var e2 = document.getElementById(id2);
    if (null != e1 && null != e2){
        var temp = e1.style.display;
        e1.style.display = e2.style.display;
        e2.style.display = temp;
    }
}

function setDisplay(id, display){
    var e = document.getElementById(id);
    if (null != e){
        e.style.display = display;
    }
}

function setArrayDisplays(array, display) {
  for( var i = 0 ; i < array.length; i++) {
    setDisplay(array[i], display)
  }
}

function setYearRange(selectId, min, max, selectedYear){
    var select = document.getElementById(selectId);
    if (null != select){
        for (var i = max; i >= min; i--){
            var e = document.createElement("OPTION");
            select.options.add(e);
            e.innerHTML = i;
            e.value = i;
            if (null != selectedYear && "" != selectedYear && i == selectedYear){
                e.selected = true;
            }
        }
    }
}

function validateEmail(emailId, address, handler){
    var email = document.getElementById(emailId);
    if (null != email){
        var postData = "email=" + email.value;
        makeRequest(postData, address, handler);
    }
}

function checkPasswordConsistence(id1, id2, errId, showErr){
    var e1 = document.getElementById(id1);
    var e2 = document.getElementById(id2);
    if (null != e1 && null != e2){
        if ( e1.value == e2.value){
            var err = document.getElementById(errId);
            if (null != err){
                err.style.display = "none";
            }
            return true;
        }
        else{
            var err = document.getElementById(errId);
            if (null != err && showErr){
                err.style.display = "";
            }
            return false;
        }
    }
    else{
        return false;
    }
}

function checkPasswordLength(id, length, errorId){
    var e = document.getElementById(id);
    var err = document.getElementById(errorId);
    if (null != e && null != err){
        if (length <= e.value.length){
            err.className = "";
            return true;
        }
        else{
            err.className = "error";
            return false;
        }
    }
    else {
      return false;
    }
}

function isDateValid(year, month, day){
  var e = new Date();
  e.setFullYear(year, month - 1, day);
  if (year != e.getFullYear() ||
      month - 1 != e.getMonth() ||
  day != e.getDate()){
    return false;
  }
  else{
    return true;
  }
}

function checkDate(yearId, monthId, dayId, errorId){
  var year = document.getElementById(yearId);
  var month = document.getElementById(monthId);
  var day = document.getElementById(dayId);
  var err = document.getElementById(errorId);
  if (null != year && 
      null != month && 
      null != day && 
      null != err){
    if ( isDateValid(year.value, month.value, day.value)){
      err.style.display = "none";
      return true;
    }
    else{
      err.style.display = "";
      return false;
    }
  }
  else {
    return false;
  }
}

function switchSpanDisplay(key, id1, id2){
  var e1 = document.getElementById(id1);
  var e2 = document.getElementById(id2);
  if (null != e1 && null != e2){
    var tempArray = e1.id.split("-");
    var display1, display2;
    if ( tempArray[2] == key){
      display1 = "";
      display2 = "none";
    }
    else{
      display2 = "";
      display1 = "none";
    }
    e1.style.display = display1;
    e2.style.display = display2;
  }
}

function isValidEmail(id){
  var input = document.getElementById(id);
  var reg = /^[\w-]+(\.[\w-]*)*@([\w-]+\.)+[.]{2,}$/i;
  return true; //reg.test(input.value);
}

function isInputEmpty(id){
  var input = document.getElementById(id);
  var temp = input.value.replace(/(^[\s]*)|([\s]*$)/g, "");
  if ( 0 == temp.length){
    return true;
  }
  else{
    return false;
  }
}

function hideError(id){
  var e = document.getElementById(id);
  if (null != e){
    e.style.display = "none";
    e.className = '';
  }
}

function checkMandatoryField(inputId, errorId, content){
  if (isInputEmpty(inputId)){
    showError(errorId, content);
    return false;
  }
  else{
    hideError(errorId);
    return true;
  }
}

function checkEmail(emailId, errorId, message){
  if (isValidEmail(emailId)) {
    hideError(errorId);
    return true;
  }
  else {
    showError(errorId, message);
    return false;
  }
}

function showError(id, content){
  var e = document.getElementById(id);
  if (null != e){
    e.style.display = "";
    e.className = 'error';
    if (content != null){
      e.innerHTML = content;
    }
  }
}

function resetDefaultValue(id, flag, value){
  var e = document.getElementById(id);
  if (null != e && e.value == flag) {
    e.value = value;
  }
}

function convertCheckboxArrayToString(array, token){
  var result = "";
  for (var i = 0; i < array.length; i++){
    var id = array[i];
    var e = document.getElementById(id);
    if ( null != e && e.checked){
      if ( 0 < result.length){
        result = result + token;
      }
      result = result + e.value;
    }
  }
  return result;
}

function getValue(id){
  var e = document.getElementById(id);
  return e.value;
}

function setValue(id, value) {
  var e = document.getElementById(id);
  e.value = value;
}

function clickButton(buttonId) {
  var button = document.getElementById(buttonId);
  button.click();
}

function clickCheckBoxes(ids, isChecked) {
  for ( var i = 0; i < ids.length; i++) {
    document.getElementById(ids[i]).checked = isChecked;
  }
}

function clickAllCheckBox(allId, ids) {
  var all = document.getElementById(allId);
  clickCheckBoxes(ids, all.checked);
}

function unclickAllCheckBox(id, allId){
  var e = document.getElementById(id);
  var all = document.getElementById(allId);
  if ( !e.checked ) {
    all.checked = false;
  }
}

function checkAll(nameItem, itemAll) {
    var a = document.getElementsByName(nameItem);
    var n = a.length;
    for (var i=0; i<n; i++) {
        a[i].checked = itemAll.checked;
    }
}

function returnNum(value) {
  var reg = /^\d+(\.\d+)?$/i;
  if (reg.test(value.trim())) {
    return value;
  }
  else {
    return '';
  }
}


function showArea(district_type) {
    var areaName = new Array('MACAU', 'TAIPA', 'COLOANE', 'COTAI_STRIP', 'ZHUHAI', 'ZHONGSHAN');
    for (var count=0; count<areaName.length; count++) {
        if (district_type == areaName[count]) {
            document.getElementById(areaName[count]).style.display = "";
        }
        else {
            document.getElementById(areaName[count]).style.display = "none";
        }
		//added for rmb and meter if zhuhai or zhongshan seleted
		if  (district_type == areaName[4] || district_type == areaName[5]) {
			document.getElementById("hkd").style.display = "none";
			document.getElementById("sqft").style.display = "none";
			document.getElementById("rmb").style.display = "";
			document.getElementById("meter").style.display = "";
			document.getElementById("price_rmb").value = "";
			document.getElementById("price").value = "";
			document.getElementById("area_sqft").value = "";
			document.getElementById("area_meter").value = "";
			document.getElementById("rmbcheck").setAttribute("class", "pricefloatformat");
			document.getElementById("hkcheck").setAttribute("class", "");
			document.getElementById("mcheck").setAttribute("class", "sqftintformat");
			document.getElementById("sqftcheck").setAttribute("class", "");
			
		}
		else {
            document.getElementById("hkd").style.display = "";
			document.getElementById("sqft").style.display = "";
			document.getElementById("rmb").style.display = "none";
			document.getElementById("meter").style.display = "none";
			document.getElementById("price_rmb").value = "";
			document.getElementById("price").value = "";
			document.getElementById("area_sqft").value = "";
			document.getElementById("area_meter").value = "";
			document.getElementById("hkcheck").setAttribute("class", "pricefloatformat");
			document.getElementById("rmbcheck").setAttribute("class", "");
			document.getElementById("mcheck").setAttribute("class", "");
			document.getElementById("sqftcheck").setAttribute("class", "sqftintformat");
        }
    }
}

function controlUploadDiv() {
    var tableStatus = document.getElementById('show_images');
    if ('none' == tableStatus.style.display) {
        tableStatus.style.display = "";
    }
    else {
        tableStatus.style.display = "none";
    }
}

function setBackgroundImage(id, url) {
  var e = document.getElementById(id);
  e.style.backgroundImage = url;
}

function clearFileUploader(parentFormId) {
  var parentForm = document.getElementById(parentFormId);
  if (null != parentForm && "FORM" == parentForm.tagName.toUpperCase()) {
    parentForm.reset();
  }
}

function clearInputControl(array) {
  for (var i = 0; i < array.length; i++) {
    var e = document.getElementById(array[i]);
    if (null != e) {
      var tagName = e.tagName.toUpperCase();
      if ("INPUT" == tagName && "TEXT" == e.type.toUpperCase()) {
        e.value = '';
      }
      else if ("INPUT" == tagName && "CHECKBOX" == e.type.toUpperCase()) {
        e.checked = false;
      }
      else if ("SELECT" == tagName && null != e.options && 0 < e.options.length) {
        e.options[0].selected = true;
      }
      else {
        // empty
      }
    }
  }
}

function setCookie(name, value) {
  document.cookie = name + "=" + escape(value);
}

function getCookieValue(sName) {
  var aCookie = document.cookie.split("; ");
  var value = null;
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
    value = unescape(aCrumb[1]);
  }
  return value;
}

function urlEncode(str){
  var ret="";
  var strSpecial="!#$%&'()*+,/:;<=>?[]^`{|}~%";
  for(var i=0;i<str.length;i++){
   var chr = str.charAt(i);
    var c=str2asc(chr);
    tt += chr+":"+c+"n";
    if(parseInt("0x"+c) > 0x7f){
      ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
    }else{
      if(chr==" ")
        ret+="+";
      else if(strSpecial.indexOf(chr)!=-1)
        ret+="%"+c.toString(16);
      else
        ret+=chr;
    }
  }
  return ret;
}


function urlEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += ch;
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function encodingPostData(postData) {
  var pairArray = postData.split('&');
  var result = '';
  for (var i = 0; i < pairArray.length; i++) {
    var keyPair = pairArray[i];
    var tempArray = keyPair.split('=');
    if ( 2 == tempArray.length) {
      var newKeyPair = tempArray[0] + '='+ urlEncode(tempArray[1]);
      if (0 < result.length) {
          result = result + '&';
      }
      result = result + newKeyPair;
    }
  }
  return result;
}

function getCuttedString(value, length) {
  var result = null;
  if ( length < value.length) {
    result = value.substring(0, length) + '...';
  }
  else {
    result = value;
  }
  return result;
}

function isUrl(url) {
  var reg = /^((http|https):\/\/([\w-]+\.)+[\w-]{2,4}(:\d+)?(\/[\w- .\/\?%&=]*)?)?$/;
  return reg.test(url.trim());
}

function checkUrl(url, errorId, error) {
  if (!isUrl(url)) {
    document.getElementById(errorId).innerHTML = error;
    showError(errorId, error);
    return false;
  }
  else {
    setDisplay(errorId, 'none');
    return true;
  }
}

function encodeHtml(value) {
  if (null != value && undefined != value) {
   value = value.replace(/</, '&lt;');
   value = value.replace(/>/, '&gt;');
  }
   return value;
 }
 
 function encodeUrl(value) {
   value = value.replace(/&/, '%26');
   value = value.replace(/=/, '%3D');
   return value;
 }
 
 function onPurposeChange(purpose_id, prefix) {
    var purpose = document.getElementById(purpose_id);
    var id1 = prefix + "-BOTH";
    var id2 = prefix + "-SALE";
    var id3 = prefix + "-RENT";
    var array = new Array(id1, id2, id3);
    for (var i =0; i< array.length; i++) {
      document.getElementById(array[i]).style.display = 'none';
    }
     var current_id = prefix + "-" + purpose.value;
     document.getElementById(current_id).style.display = '';
  }
  
  function getSelectValue(selectId) {
    var select = document.getElementById(selectId);
    if (null != select) {
      for (var i = 0; i < select.options.length; i++) {
          var option = select.options[i];
          if (true == option.selected) {
            return option.innerHTML;
          }
      }
    }
  }
