﻿function showRegionList(strroot, strShihHsienID) {
    if ((strShihHsienID == null) || (strShihHsienID == '') || (strShihHsienID == '-1')) { alert('請選擇一縣市'); return false; }
    ShowPopUpDetail(strroot + 'RegionList_IF.aspx?cid=' + strShihHsienID, 610, 360, false);
}
//---TextBox必須有值
function TextBoxMustFill(strControlID, strErrorMsg) {
    var strControlValue = '';
    strControlValue = document.getElementById(strControlID).value;
    strControlValue = strControlValue.replace(/^[\s　]+/g, "");
    strControlValue = strControlValue.replace(/[\s　]+$/g, "");
    strControlValue = strControlValue.replace(/[\s　]+/g, " ");
    if (strControlValue.length == 0) {
        return strErrorMsg + '\n\n';
    }
    return ('');
}

//---小視窗位置在瀏覽器中央
function OpenPopUpWindow(theURL,winName,intWidth,intHeight,strFeatures) { 
		var intTop=(screen.availHeight - intHeight) / 2 ;
		var intLeft=(screen.availWidth - intWidth) / 2 ;
		strFeatures += ',top=' + intTop + ',left=' + intLeft + ',width=' + intWidth + ',height=' + intHeight ;
		window.open(theURL,winName,strFeatures);
}


//---Block background div fill full all screen
function SetBGDIVWH(){
    document.getElementById('divBlockBackGround').style.width=((document.documentElement.clientWidth>document.documentElement.scrollWidth)?document.documentElement.clientWidth:document.documentElement.scrollWidth)+'px';
    document.getElementById('divBlockBackGround').style.height=((document.documentElement.clientHeight>document.documentElement.scrollHeight)?document.documentElement.clientHeight:document.documentElement.scrollHeight)+'px';
}

//window.onresize=function(){
//    SetBGDIVWH();
//}


function setPopUpPosition(strPopUpDIVID) {
    var yScroll;
	if (self.innerHeight) {
		windowHeight = self.innerHeight;
	}	else {
		windowHeight = document.documentElement.clientHeight;
	}
	
	
	if (typeof document.body.style.maxHeight != "undefined") {////Mozilla, Safari,...IE7
        yScroll = 0;
    } else {//ie6
        yScroll = document.documentElement.scrollTop;
    }

	
	var posObj=document.getElementById(strPopUpDIVID);	
	posObj.style.left = (document.documentElement.clientWidth - parseInt(posObj.style.width))/2 + 'px';
	posObj.style.top = yScroll + (windowHeight - parseInt(posObj.style.height))/2 + 'px';	
}
        
function tmpFromAction(url){
    var fm=document.forms['frmTmp'];
    fm.action=url;
    fm.submit();        
}


function closePopUpDiv(){
    document.getElementById('divBlockBackGround').style.display='none';
    document.getElementById('divPopUpMsg').style.display='none';
    document.getElementById('divPublicPopUp').style.display='none';
    document.getElementById('divPublicPopUp').innerHTML='';
}

function slideStringSub(ul, delay, speed, lh) {
			var slideBox = (typeof ul == 'string')?document.getElementById(ul):ul;
			var delay = delay||1000, speed=speed||20, lh = lh||20;
			var tid = null, pause = false;
			var start = function() {
				tid=setInterval(slide, speed);
			}
			var slide = function() {
				if (pause) return;
				        slideBox.scrollTop += 2;
				        if (slideBox.scrollTop % lh == 0) {
					        clearInterval(tid);
					        slideBox.appendChild(slideBox.getElementsByTagName('li')[0]);
					        slideBox.scrollTop = 0;
					        setTimeout(start, delay);
				        }
			}
			slideBox.onmouseover=function(){pause=true;}
			slideBox.onmouseout=function(){pause=false;}
			setTimeout(start, delay);
}


function AddFavorite(url, title, homepage){//FF
   if( window.sidebar && window.sidebar.addPanel ) {  
      window.sidebar.addPanel( title, url+homepage, '' );
   }
   else if( window.external ) {// IE
      window.external.AddFavorite( url, title );
   }
   else { // Other browsers
      alert('抱歉,無法為您的瀏覽器中加入最愛功能,自行新增一個最愛聯結');
   }
}



function CityRegionCascade(strRegionFirstText, strCityDropID, strRegionDropID){
        var intSelectCityValue=document.getElementById(strCityDropID).value;
        var ddlRegion=document.getElementById(strRegionDropID);
        for (var intI=ddlRegion.options.length-1; intI>-1; intI--) {
            ddlRegion.remove(intI);
        }
        var objOption1=new Option(strRegionFirstText, '-1');
        ddlRegion.options[0]=objOption1;            
        for (intI=0; intI<arystrRegion.length; intI++) {
            if (parseInt(arystrRegion[intI].strCityID)==parseInt(intSelectCityValue)){
                var objOption=new Option(arystrRegion[intI].strText, arystrRegion[intI].strRegID);
                ddlRegion.options[ddlRegion.options.length]=objOption;
            }
        }
}

//---open, close adv search
function advHouseSearch(){
    if (document.getElementById('divAdvSearch').style.display=='none'){
        document.getElementById('divAdvSearch').style.display='block';
        document.getElementById('aAdvSearch').innerHTML='關閉搜尋條件';
    }else{document.getElementById('divAdvSearch').style.display='none';document.getElementById('aAdvSearch').innerHTML='開啟更多搜尋條件';}
    document.getElementById('ddlHouseYearS').selectedIndex=0;
    document.getElementById('ddlRoomCountS').selectedIndex=0;
    document.getElementById('ddlDirection').selectedIndex=0;
    document.getElementById('ddlNoneBaffleEnvironment').selectedIndex=0;
    FromToSub('ddlHouseYearS', 'ddlHouseYearBetween', 'ddlHouseYearE');
    FromToSub('ddlRoomCountS', 'ddlRoomCountBetween', 'ddlRoomCountE');
}

//---至, 以上, 以下
function FromToSub(startId, betweenId, endId){
    if (document.getElementById(startId).value=='-1'){
        document.getElementById(betweenId).style.display='none';
        document.getElementById(endId).style.display='none';
    }else{
        document.getElementById(betweenId).style.display='inline';
        if (document.getElementById(betweenId).value=='between'){
            document.getElementById(endId).style.display='inline';
        }else{document.getElementById(endId).style.display='none';}                
    }
}

//---收集已打勾的CheckBox
function GetWSSelectCheckBox(strKeyWord){
    var arystrSelectCheckID = new Array();
    var intJ=0;
    for (var intI = 0 ; intI < document.form1.length ; intI++) {
            if (document.form1.elements[intI].type == 'checkbox') {
                var strCheckID=document.form1.elements[intI].id ;
                if (strCheckID.indexOf(strKeyWord,0) != '-1'){
                    if (document.getElementById(strCheckID).checked==true){
                        arystrSelectCheckID[intJ]=strCheckID;
                        intJ+=1;
                    }
                }
            }
      }
      return arystrSelectCheckID;
}

function compareSub(blnIsRoot){
          var arystrSelectCheckID;
          arystrSelectCheckID=GetWSSelectCheckBox('chk_');
          if (arystrSelectCheckID == null){alert('請選擇2個物件至4個物件，才能進行比較');return false;}
          if (arystrSelectCheckID != null){
                  if ((arystrSelectCheckID.length>4)||(arystrSelectCheckID.length<2)){
                    alert('請選擇2個物件至4個物件，才能進行比較');
                    return false;
                  }
                  var strPara='';
                  for (var intI = 0 ; intI <= arystrSelectCheckID.length-1 ; intI++) {
                    if (intI!=0){strPara +='_';}
                    strPara += arystrSelectCheckID[intI].replace('chk_','');
                  }
                  if(blnIsRoot==true){
                    tmpFromAction("Compare/Compare.aspx?para=" + strPara);
                  }else{tmpFromAction("../Compare/Compare.aspx?para=" + strPara);}
                  
          }
}

document.oncontextmenu = function(){return false;}
document.onselectstart = function(){return false;}
document.oncopy = function(){return false;}
document.oncut = function(){return false;}
document.ondrag = function(){return false;}



function PerfectSmallWinWidth(dblRatio) {
    var dblWidth = parseInt(document.documentElement.clientWidth * dblRatio);
    return dblWidth
}

function PerfectSmallWinHeight(dblRatio) {
    var dblHeight = parseInt(document.documentElement.clientHeight * dblRatio);
    return dblHeight
}


function ShowPopUpDetail(path, blnNeedButton){
        document.getElementById('divBlockBackGround').style.display='block';
        document.getElementById('divPublicPopUp').style.display='block';
        document.getElementById('divPublicPopUp').style.width = PerfectSmallWinWidth(0.7) + 'px';
        document.getElementById('divPublicPopUp').style.height = PerfectSmallWinHeight(0.8) + 'px';
        setPopUpPosition('divPublicPopUp');
        SetBGDIVWH();
        document.getElementById('divPublicPopUp').innerHTML='';
        if(document.getElementById('ifPublicPopUp')==null){
            document.getElementById('divPublicPopUp').innerHTML="<iframe name='ifPublicPopUp' id='ifPublicPopUp' width='100%' height='92%' frameborder='0' scrolling='auto'></iframe>";
        }
        if(blnNeedButton==true){document.getElementById('divPublicPopUp').innerHTML +="<div class='butt'><input type ='button' id='btnClose' value='關閉視窗' onclick='parent.BackGroundDIVClick();' style='width:80px;'/></div>";}
        document.getElementById('ifPublicPopUp').src=path;         
}

function BackGroundDIVClick(){
        document.getElementById('divBlockBackGround').style.display='none';
        document.getElementById('divPublicPopUp').style.display='none';
        document.getElementById('ifPublicPopUp').src='';
}

//---TextArea計數器
function TextAreaCounter(objTextArea, strRemainLabelID, intMaxLimit) {
  if (objTextArea.value.length > intMaxLimit) {
    objTextArea.value = objTextArea.value.substring(0,intMaxLimit);
  }
  else {
    document.getElementById(strRemainLabelID).innerHTML='尚能輸入 ' + (intMaxLimit - objTextArea.value.length) + ' 個字';
  }
}


//---check email format
function EmailFormat(strEmailControlID, strAlertMsg){
            strEmail=document.getElementById(strEmailControlID).value ;
	        strEmail = strEmail.replace(/^[\s　]+/g, "");
            strEmail = strEmail.replace(/[\s　]+$/g, "");
	        if (strEmail != ''){
		        var intEmailLength = strEmail.length;
		        for(var intI=0;intI<intEmailLength;intI++){ 
			        var strEmailChar= strEmail.charAt(intI);
			        if(!((strEmailChar>="A"&&strEmailChar<="Z")||(strEmailChar>="a"&&strEmailChar<="z")||(strEmailChar>="0"&&strEmailChar<="9")||(strEmailChar=="-")||(strEmailChar=="_")||(strEmailChar==".")||(strEmailChar=="@")))
				        return strAlertMsg+'\n';
		        }
		        if((strEmail.indexOf("@")==-1)||(strEmail.indexOf("@")==0)||(strEmail.indexOf("@")==(intEmailLength-1)))
			        return strAlertMsg+'\n\n';
    				
		        if((strEmail.indexOf("@")!=-1)&&(strEmail.substring(strEmail.indexOf("@")+1,intEmailLength).indexOf("@")!=-1))
			        return strAlertMsg+'\n\n';
    				
		        if((strEmail.indexOf(".")==-1)||(strEmail.indexOf(".")==0)||(strEmail.lastIndexOf(".")==(intEmailLength-1)))
			        return strAlertMsg+'\n\n';
    				
		        return '';
	        }
	        else{
		        return '';
	        }
}
//---去左右空白
function TrimTextBoxValue(strControlValue){
	strControlValue = strControlValue.replace(/^[\s　]+/g, "");
	strControlValue = strControlValue.replace(/[\s　]+$/g, "");
	strControlValue = strControlValue.replace(/[\s　]+/g, " ");
	return strControlValue;
}


 //---radio起碼要打一個勾       
function RadioMustSelectOne(strKeyWord, strMsg){
    var blnSelected=false;
    for (var intI = 0 ; intI < document.form1.length ; intI++) {
            if (document.form1.elements[intI].type == 'radio') {
                var strRadioID=document.form1.elements[intI].id ;
                if (strRadioID.indexOf(strKeyWord,0) != '-1'){
                    if (document.getElementById(strRadioID).checked==true){
                        blnSelected=true;
                        return '';
                    }
                }
            }
      }
      if (blnSelected==false){
            return strMsg+'\n\n';
      }
}

//---不允許有不合法字句
function FormStopWord(strErrorMsg, arystrStopWord){
    for (var intI = 0 ; intI < document.form1.length ; intI++) {
            if ((document.form1.elements[intI].type == 'text')||(document.form1.elements[intI].type == 'textarea')) {
                var strTextBoxValue=document.getElementById(document.form1.elements[intI].id).value;
                strTextBoxValue = strTextBoxValue.replace(/^[\s　]+/g, "");
                strTextBoxValue = strTextBoxValue.replace(/[\s　]+$/g, "");
                strTextBoxValue = strTextBoxValue.replace(/[\s　]+/g, " ");
                if (strTextBoxValue.length != 0){
                        strTextBoxValue=strTextBoxValue.toLowerCase();
                        for (var intJ = 0 ; intJ < arystrStopWord.length ; intJ++) {
                                var strStopWord=arystrStopWord[intJ].toLowerCase();
                                if (strTextBoxValue.indexOf(strStopWord,0)!=-1){
	                                return strErrorMsg + '\n\n';                    
	                                break;
                                }
                        }
                }
            }
      }
      return '';
  }
  function closeShunDownDiv() {
      document.getElementById('divBlockBackGround').style.display = 'none';
      document.getElementById('divShunDown').style.display = 'none';
  }
	
    
    function openDisableControl(){
        document .getElementById ('divSearch').style.display='';
        document .getElementById ('divSortType').style.display='';
    }
    function openFaceBook() {
        void (open('http://www.facebook.com/share.php?u=' + encodeURIComponent(location.href) + '&amp;t=' + encodeURIComponent(document.title.split('@')[0].replace(/([\s]*$)/g, ''))));
    }
    function openPlurk() {
        var url = "http://plurk.com/?status=" + encodeURIComponent(location.href) + " " + "(" + ((document.title) ? encodeURIComponent(document.title.replace(/^\s*|\s*$/g, '')) : "") + ")";
        void (window.open(url));
    }
    function openTwitter() {
        var url = "http://twitthis.com/twit?url=" + encodeURIComponent(location.href) + "&title=" + ((document.title) ? encodeURIComponent(document.title.replace(/^\s*|\s*$/g, '')) : "");
        void (window.open(url));
    }
