//按id取得DOM元素
function $(id) {
	return document.getElementById(id);
}

//限制INPUT输入格式
function MaskEdit(Obj){
	var re = new RegExp(Obj.alt,"ig");
	arr=re.exec(Obj.value);
    if (arr==null)
		Obj.value=Obj.title;
	else
		Obj.title=Obj.value;
}

//限制TEXTAREA输入字符数量
function textCounter(field, maxlimit) { 
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit); 
	else 
		remLen.value = maxlimit - field.value.length;
}

//取得模板显示区内容
function getWorkSpaceItem(url,cat,pad){
	var ajax = new sack();
	if (cat===undefined) cat="workspace";
	if (pad===undefined) pad="workspace";
	ajax.setVar("category",cat);
	ajax.requestFile = url;
	ajax.method = "POST";
	ajax.element=pad;
	ajax.onCompletion = function(){
		if (typeof(this.response)==="string") {
			var re = new RegExp('<script[^>]+value="([^>]+)"','ig');
			if (re.test(this.response)) {
				re.exec(this.response);
	  			eval(RegExp.$1);
			}
		}
	};
	ajax.runAJAX();
}

//填充时间下拉列表框starttime
function fillTimeOptsend(prefix){

	var oHour=$(prefix+"_hour");
	var oMin=$(prefix+"_min");
	var d=new Date();
	for (i=0;i<24; i++){
		Opt=new Option();
		Opt.text=i;
		Opt.value=(i<=9?"0"+i:i);
		if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
			oHour.options.add(Opt);
		}else{
			oHour.add(Opt,null);
		}
		if (Opt.value==d.getHours() )
		{
			Opt.selected=true;
		}
	}

	for (i=0;i<60; i++){
		Opt=new Option();
		Opt.text=i;
		Opt.value=(i<=9?"0"+i:i);
		if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
			oMin.options.add(Opt);
		}else{
			oMin.add(Opt,null);
		}

		if (Opt.value==d.getMinutes())
		{
			Opt.selected=true;
		}
	}
}

//填充时间下拉列表框starttime
function fillTimeOpts(prefix){
	var oHour=$(prefix+"_hour");
	var oMin=$(prefix+"_min");
	for (i=0;i<24; i++){
		Opt=new Option();
		Opt.text=i;
		Opt.value=(i<=9?"0"+i:i);
		if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
			oHour.options.add(Opt);
		}else{
			oHour.add(Opt,null);
		}
	}
	for (i=0;i<60; i++){
		Opt=new Option();
		Opt.text=i;
		Opt.value=(i<=9?"0"+i:i);
		if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) {
			oMin.options.add(Opt);
		}else{
			oMin.add(Opt,null);
		}
	}
}

function checkTimeRange(beg,end,unit,range) {
	this.getMonthLen=function(time) {
		var oneDay = 1000 * 60 * 60 * 24;
		var thisMonth = new Date(time.getFullYear(), time.getMonth(), 1);
		var nextMonth = new Date(time.getFullYear(), time.getMonth()+1, 1);
		var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime())/oneDay);
		return len;
	}
	var i = 0;
	var unitName = new Array(); 
	unitName['SECOND']="秒";
	unitName['MINIUTE']="分钟";
	unitName['HOUR']="小时";
	unitName['DAY']="天";
	unitName['MONTH']="月";
	unitName['YEAR']="年";
	unit = unit.toUpperCase();
	var limit = new Date(beg.getTime());
	range=range/1;
	switch (unit) {
		case "YEAR":
			limit.setYear(limit.getFullYear()+range);
			break;
		case "MONTH":
			limit.setMonth(limit.getMonth()+range);
			break;
		case "DAY":
			limit.setDate(limit.getDate()+range);
			break;
		case "HOUR":
			limit.setHours(limit.getHours()+range);
			break;
		case "MINIUTE":
			limit.setMinutes(limit.getMinutes()+range);
			break;
		case "SECOND":
			limit.setSeconds(limit.getSeconds()+range);
			break;
		
	}
	//must caculate the LAST day of the month, if the unit is in the case of month or year.
	if (unit==="YEAR" || unit==="MONTH") {
		//end's month is February
		if (limit.getDate()<beg.getDate()) {
			limit.setMonth(limit.getMonth()-1);
			limit.setDate(this.getMonthLen(limit));
		}
		//beg's month is February
		if(beg.getDate()===this.getMonthLen(beg)) {
			limit.setDate(this.getMonthLen(limit));
		}
	}
	if (end.getTime()-beg.getTime()<=0)	{
		alert("结束时间必须晚于开始时间!");
		return false;
	}
	if (end-limit>0)
	{
		alert("时间范围限制于"+range+unitName[unit]+"内!");
		return false;
	}
	
	var nowdate="";
	nowdate=new Date();
	if ((nowdate-beg)/(3600*24*1000)>180)
	{
		alert("查询时间超范围!");
		return false;
	}

	return true;
}

//列表排序
function changeSortType(Obj){

	var st = new Array();
	st[0]='　';
	st[1]='▲';
	st[2]='▼';

	var tt = new Array();
	tt[0]='无排序';
	tt[1]='升序';
	tt[2]='降序';

	var sorter = null;
	//读出被点击TD中的排序项
	for (var i=0; i<Obj.childNodes.length; i++  )
	{
		var tag = ""+Obj.childNodes[i].tagName;
		if (tag.toLowerCase()==="input") {
			sorter = Obj.childNodes[i];
			break;
		}else sorter = null;
	}
	if (sorter===null) return false;
	//计算排序项新值
	var val = sorter.tabIndex;
	val=(val+1)%3;
	//初始化所有排序项
	var sorts = document.getElementsByName("sort");
	for(var i=0;i<sorts.length;i++){
		sorts[i].value=st[0];
		sorts[i].tabIndex=0;
		sorts[i].parentNode.title=tt[0];
	};
	//设置排序项新值
	sorter.value=st[val];
	sorter.tabIndex=val;
	Obj.title=tt[val];
}

var xsize = 600;
var ysize = 400;

//动态监控全部车辆
function popUpDynamic()
{
	var ajax = new sack();
	ajax.reset();
	ajax.requestFile = "timeout.php";
	ajax.method = "POST";
	ajax.onCompletion = function()
	{
		if (this.response==1)
		{
			var loc = "/webgis/dynamic.html";
			var tWindow = window.open(loc,"floating","toolbar=0,location=0,directories=0,status=1,top=" + 0 + ",left=" + 0 +
					",menubar=0,scrollbars=1,resizable=1,screenX=" + 400 + ",screenY=" + 300 + ",width=" + xsize + ",height=" + ysize);
			tWindow.focus( );
		}
		else
		{
			alert("登录超时，请重新登录！");
			window.location="logout.php";			
		}
	};
	ajax.runAJAX();
}

//轨迹回放
function popUpTrack(carno, carid, starttime, endtime)
{
	var ajax = new sack();
	ajax.reset();
	ajax.requestFile = "timeout.php";
	ajax.method = "POST";
	ajax.onCompletion = function()
	{
		if (this.response==1)
		{
			var title=carno;
			title=title.replace(/\-/g,"");
			title="floating"+title+"1";
			var loc = "/webgis/track.html?carno="+escape(carno)+"&carid="+escape(carid)+"&starttime="+starttime+"&endtime="+endtime;
			var tWindow = window.open(loc,title.replace("*","1"),"toolbar=0,location=0,directories=0,status=1,top=" + 0 + ",left=" + 0 +
				",menubar=0,scrollbars=1,resizable=1,screenX=" + 400 + ",screenY=" + 300 + ",width=" + xsize + ",height=" + ysize);
			tWindow.focus( );
		}
		else
		{
			alert("登录超时，请重新登录！");
			window.location="logout.php";			
		}
	};
	ajax.runAJAX();
}

//动态监控一辆车
function popUpMonitorOne(carno)
{
	var ajax = new sack();
	ajax.reset();
	ajax.requestFile = "timeout.php";
	ajax.method = "POST";
	ajax.onCompletion = function()
	{
		if (this.response==1)
		{
			var title=carno;
			title=title.replace(/\-/g,"");
			title="floating"+title+"2";
			var loc = "/webgis/dynamic.html?mode=2&carno=" + escape(carno);
			var tWindow = window.open(loc,title.replace("*","2"),"toolbar=0,location=0,directories=0,status=1,top=" + 0 + ",left=" + 0 +
				",menubar=0,scrollbars=1,resizable=1,screenX=" + 400 + ",screenY=" + 300 + ",width=" + xsize + ",height=" + ysize);
			tWindow.focus( );
		}
		else
		{
			alert("登录超时，请重新登录！");
			window.location="logout.php";			
		}
	};
	ajax.runAJAX();
}

function fixed(num,len)
{
	var a=parseFloat(num);
	a=a.toFixed(len);
	//alert(a);
	return a;	
}

