
//-----------------------------------------------------------------------------初始化数据

function generateForm()
{
	subList=getIndicators();
	str="<table border=\"1\" width='100%'><tr align=\"center\"><td width=\"20%\">用户</td><td width=\"20%\">锁定</td><td width=\"20%\">轨迹</td><td width='20%'>操作</td></tr>";
	for(i=0;i<subList.length;i++){
		subDesc=subList[i];
		str+="<tr align=\"center\"><td>"+subDesc[0]+"</td><td>";
		str+="<input type=\"checkbox\" name=\"cb_bind_"+subDesc[0]+"\" value=\"1\" onClick=\"bindIndicator('"+subDesc[0]+"',this.checked);\" ";
		if (subDesc[1]=="true")
			str+=" checked>";
		str+="</td><td>";
		str+="<input type=\"checkbox\" name=\"cb_track_"+subDesc[0]+"\" value=\"1\" onClick=\"trackIndicator('"+subDesc[0]+"',this.checked);\"";
		if (subDesc[2]=="true")
			str+=" checked>";
		str+="</td><td>";
		
		str+=" <a href='javascript:void(0)' onClick='deleteIndicator("+subDesc[0]+");' >移除</a>";
		str+="</td></tr>";
	}
	str+="</table>";
	document.getElementById("subsriberForm").innerHTML=str;
}

function getIndicators()
{
	if(mapview.getIndicators!=undefined)
		return mapview.getIndicators();
	else{
		alert("mapview.getIndicators is not defined");
		return new Array();	
	}
}

function subscriberUser(){
	subscribe(uid.value,token.value);	
}

//parent------------------------------------------index mothed 
var HOST = "/track";
var tUserName;
var tToken;
function subscribe(username,token)
{
		tUserName=username;
		tToken=token;
		ajaxSend(HOST+"/gps_subscriber.php?uid="+username+"&token="+token+"&t="+Math.random(),ajaxRecv);	
}


function ajaxSend(url,callback)
{
	if (window.XMLHttpRequest) 
		req = new XMLHttpRequest();
	else if (window.ActiveXObject) 
		req = new ActiveXObject("Microsoft.XMLHTTP");   
	 
	req.onreadystatechange = callback;
	//alert(url);
	req.open("GET", url, true);
	req.send(null);
};

function ajaxRecv() {
	if (req.readyState == 4 && req.status == 200) 
	{
		var rt=req.responseText.split("|");
		if (rt[0]=="subscribe")
		{
			onSubscribeResult(rt);
		}
		else if (rt[0] == "xy2ll")
		{
			onxy2ll(rt);
		}
		
	}
} 

function onSubscribeResult(rt)
{
	if (rt[1]=="succeed")
	{
		var subList=getIndicators();
		for(i=0;i<subList.length;i++){
			subDesc=subList[i];
			bindIndicator(subDesc[0],false);
		}
		addIndicator(tUserName,tToken);
		bindIndicator(tUserName,true);
		trackIndicator(tUserName,true)
		generateForm();
		tUserName="";
		tToken="";
	}
	else
		info_view.showInformation(rt[2]);	
}

function addIndicator(uid,token)
{
    mapview.createIndicator(uid,token,0,0);
}

function bindIndicator(uid,val){
	mapview.bindToIndicator(uid,val);
	generateForm();
}

function trackIndicator(uid,val){
	mapview.drawTrackForIndicator(uid,val);
}

function deleteIndicator(uid){
	//alert("2");
	mapview.deleteIndicatorById(uid);
	generateForm();
}

