﻿// JScript 文件

/*-----------基础方法-------------*/
var $=function(id) {
   return document.getElementById(id);
}
//去掉字符窜的空格
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
//验证为正整数
function isNumber(s){
    var re = /^[1-9]+[0-9]*]*$/;    //判断正整数 /^[1-9]+[0-9]*]*$/   
    if (!re.test(s)){
        return false;
    }else{
        return true;
    }
}
//验证为数字
function IsNum(s){
	var Number = "0123456789";
	for (i = 0; i < s.length;i++){   
        var c = s.charAt(i);
        if (Number.indexOf(c) == -1){
           return false;
       }
    }
	return true
}
/*-----------经纬度编码解码-------------*/
METERS_PER_DEGREE = 109801.71;
//对经纬度编码
function codeLatLng(lat,lng){
    var originLng = 72;
	var originLat = 0 ;
	var grid10w = 72 ;
	var grid10h = 72 ;
	var grid9w = 24 ;
	var grid9h = 24 ;
	var code='';
	var d1=0;
	var d2=0;
	var offsetLng=(lng-originLng)*METERS_PER_DEGREE;
	var offsetLat=(lat-originLat)*METERS_PER_DEGREE;
	for(var i=4;i>=0;i--){
		code=code+Math.floor(offsetLng/(grid10w*Math.pow(10,i)))+Math.floor(offsetLat/(grid10h*Math.pow(10,i)));
		d1=(offsetLng/(grid10w*Math.pow(10,i)))-Math.floor(offsetLng/(grid10w*Math.pow(10,i)));
		offsetLng=d1*(grid10w*Math.pow(10,i));
		
		d2=(offsetLat/(grid10h*Math.pow(10,i)))-Math.floor(offsetLat/(grid10h*Math.pow(10,i)));
		offsetLat=d2*(grid10h*Math.pow(10,i));
	} 
	code=code+((Math.floor(offsetLng/grid9w)+1)+(Math.floor(offsetLat/grid9h)*3));
	return code;
}
//对经纬度解码
function enCodeLatLng(code){
    if(code.length!=11){
		return null;
	}
	var originLng = 72;
	var originLat = 0 ;
	var grid10w = 72 ;
	var grid10h = 72 ;
	var grid9w = 24 ;
	var grid9h = 24 ;
	var lat=null;
	var lng=null;
	var n=null;
	offsetLng=parseInt(code.charAt(0),10)*grid10w*Math.pow(10,4)+parseInt(code.charAt(2),10)*grid10w*Math.pow(10,3)+parseInt(code.charAt(4),10)*grid10w*Math.pow(10,2)+parseInt(code.charAt(6),10)*grid10w*10+parseInt(code.charAt(8),10)*grid10w;
	offsetLat=parseInt(code.charAt(1),10)*grid10h*Math.pow(10,4)+parseInt(code.charAt(3),10)*grid10h*Math.pow(10,3)+parseInt(code.charAt(5),10)*grid10h*Math.pow(10,2)+parseInt(code.charAt(7),10)*grid10h*10+parseInt(code.charAt(9),10)*grid10h;
	if(parseInt(code.charAt(10))==0){
		offsetLat=offsetLat+(grid10h/2);
		offsetLng=offsetLng+(grid10w/2);
	} else{
		if((parseInt(code.charAt(10))-3)/3<=0){
			n=0;
		}else if((parseInt(code.charAt(10),10)-3)/3<=1&&(parseInt(code.charAt(10),10)-3)/3>0){
			n=1;
		} else if((parseInt(code.charAt(10),10)-3)/3<=3&&(parseInt(code.charAt(10),10)-3)/3>1){
			n=2;
		} 
		offsetLat=offsetLat+grid9h*(n+1)-grid9h/2;
		offsetLng=offsetLng+(parseInt(code.charAt(10),10)-3*n)*grid9w-grid9w/2;
	} 
	lng=Math.round((((offsetLng/METERS_PER_DEGREE)+originLng)*100000))/100000;
	lat=Math.round((((offsetLat/METERS_PER_DEGREE)+originLat)*100000))/100000;
	
	var latlng = new Object();
    latlng.lat = parseFloat(lat);
    latlng.lng = parseFloat(lng);
	return latlng;
}
//判断是否为经纬度编码
function isValidLatLngCode(code){
    if(!code){return false;}
    if(code.length==11){
        var  rtcodeRegex;
		rtcodeRegex=/^\d{11}$/;
		if(!rtcodeRegex.test(code)){return false;};		
        return true;
    }
    return false;    
}

//判断是否为经纬度
function isLatAndLng(lat,lng){
    if(lat == "" || lat == "0"  || lat == 0) {
        return false;
    }
    if(lng == "" || lng == "0"  || lng == 0){
        return false;
    }
    return true;
}

/// <summary>
/// 分页
/// </summary>
/// <param name="thisPage">当前页码</param>
/// <param name="total">总记录数</param>
/// <param name="thisPageCount">每页显示的记录数</param>
/// <param name="pageCount">显示的页码数目（当前页显示在中间）</param>
/// <param name="cityID">城市ID</param>
/// <param name="keyword">关键字</param>
function getPage(thisPage, total, thisPageCount, pageCount, cityId, keyword){
    thisPage = parseInt(thisPage);
    total = parseInt(total);
    thisPageCount = parseInt(thisPageCount);
    pageCount = parseInt(pageCount);
    
    if (total <= 0){
        return "";
    }else{
        //当前页实际显示的记录数
        var count = thisPageCount;
        //总共分出来的页数
        var totalPage = 0;
        //限定最大页码（最多显示多少页）
//        var maxPage = 10;

        //计算出总共分出来的页数
        totalPage = Math.floor(total/thisPageCount); //向下取整
        if ((total % thisPageCount) != 0){
            totalPage = totalPage + 1; //向上取整
        }
//        if(maxPage != null && maxPage != "" && totalPage > maxPage ){
//            totalPage = maxPage;
//        }

        //当前页不在范围之内显示到第一页
        if (thisPage > totalPage || thisPage <= 0){
            thisPage = 1;
        }

        //计算出显示页码的起始页start
        var start = 1;
        if (totalPage <= pageCount){
            pageCount = totalPage;
        }else{
            if (thisPage <= Math.ceil(pageCount/2)){ //pageCount的一半向上取整
                start = 1;
            }else{
                if (thisPage > totalPage - Math.floor(pageCount/2) ){   //pageCount的一半向下取整
                    start = totalPage - pageCount + 1;
                }else{
                    start = thisPage - Math.floor(pageCount/2);
                }
            }
        }
        
        var pageHTML = "";
        pageHTML += "<div id='pagination' style='text-align:center; font-size:12;'>";

        if(totalPage > 1){
//            if(thisPage == 1){
//                pageHTML += "上页&nbsp;";
//            }else{
//                pageHTML += "<a style='text-decoration: none;color:#0000ff;'  href='javascript:void(0);' onclick='DoSearchPOI(\""+cityId+"\",\""+keyword+"\",\""+((thisPage - 2) * count)+"\",\""+count+"\")'>上页</a>&nbsp;";
//            }
            
            for (var iPoint = start; iPoint < (start + pageCount); iPoint++){
                if (iPoint == thisPage){
                    pageHTML += "<strong><span style='color:#E30502;'>[" + iPoint + "]</span></strong>&nbsp;";
                }else{
                    pageHTML += "<a style='text-decoration: none;color:#0000ff;' href='javascript:void(0);' onclick='DoSearchPOI(\""+cityId+"\",\""+keyword+"\",\""+((iPoint - 1) * count)+"\",\""+count+"\")'>[" + iPoint + "]</a>&nbsp;";
                }
            }
            
//            if(thisPage == totalPage){
//                pageHTML += "下页&nbsp;";
//            }else{
//                pageHTML += "<a style='text-decoration: none;color:#0000ff;' href='javascript:void(0);' onclick='DoSearchPOI(\""+cityId+"\",\""+keyword+"\",\""+(thisPage * count)+"\",\""+count+"\")'>下页</a>"
//            }
            pageHTML+= "&nbsp;<input type='text' id='pageKeyword' style='width:20px; height:15px;' maxlength='20' />&nbsp;<input type='button' style='width:25px; height:22px;' id='pageGo' size='3' value='go' onclick='pageGo(\""+cityId+"\",\""+keyword+"\",\""+count+"\")'/>";
        }
        pageHTML+= "</div>";
        return pageHTML;
    }
}



