/**
 * 本文件由addBusinessIndex.jsp和Index.jsp共用
 * 
 */
var aBusinessListener = null;
var markListener=null;
var addBusifinish = true;
var i=0,markList=new Array();
var geoMetryUrl = "http://58.63.71.44:8399/arcgis/rest/services/Geometry/GeometryServer";
var statrionURL = "http://58.63.71.44:8399/arcgis/rest/services/bufferServer/MapServer/2";
var statrionExitURL="http://58.63.71.44:8399/arcgis/rest/services/gmQueryData/MapServer/4";
function checkUserStatu(userID){
	if(userID===null){
		alert("请先登录！");
		showUserLogin();
	}else{
		addBussiness();
	}
}
function addBussiness() {
	alert("在地铁站点500米范围标示商铺");
	aBusinessListener = GEvent.addListener(gmap, 'click', handleAddBussiness);
}
function handleAddBussiness(overlay, latlng) {
	if (addBusifinish) {
		var marker = new GMarker(latlng);
		markList.push(marker);
		i+=1;
		marker.id=i;
		gmap.addOverlay(marker);
		/*
		var divInfo=document.createElement("iframe");
		document.
		divInfo.name="ifrAddbusiness";
		divInfo.frameBorder=0;
		divInfo.style.width="400px";
		divInfo.style.height="300px";
		divInfo.src=src="./addBusinessIndex.jsp?lat="+ latlng.lat() + "&lng=" + latlng.lng();
		document.appendChild(divInfo);
		marker.openInfoWindow(divInfo);
	
		
		GEvent.addListener(marker, "click", function() {
			//marker.openInfoWindowHtml(contents);
			//marker.openInfoWindow(divInfo);
			
			var tt=getIframe("ifrAddbusiness");alert(tt);
		});
		*/
		var contents = "<iframe id='ifrAddbusiness' name='ifrAddbusiness' src='./addBusinessIndex.jsp?lat="
				+ latlng.lat() + "&lng=" + latlng.lng()
				+ "' width='400px' height='300px' frameBorder='0'>";
		//marker.openInfoWindowHtml(contents,GInfoWinOpt);
		//openInfoWindowHtml(latlng:GLatLng, html:String, opts?:GInfoWindowOptions) 
		gmap.openInfoWindowHtml(latlng,contents);
		var Infowin=gmap.getInfoWindow();
		GEvent.addListener(Infowin, 'closeclick', function() {
			parent.focus();
		});
		markListener = GEvent.addListener(marker, 'click', function() {
			//marker.openInfoWindowHtml(contents,GInfoWinOpt);
/*			var bb=getIframe();alert(bb);
			marker.openInfoWindow(bb);*/
			gmap.openInfoWindowHtml(latlng,contents);
		});
		
		
		
		GEvent.removeListener(aBusinessListener);
		addBusifinish = false;
	} else {
		return;
	}
}
function getIframe(){
	var iframe=document.frames['ifrAddbusiness'].document.body;//document.all.iframeID;//window.frames[iframeID].document;//document.getElementById("ifrAddbusiness");
	return iframe;
}
// >>>>>>>>>>>>>根据点击的位置查询最近地铁站点>>>>>>>>>>
function init() {
	var lat = GetQueryString('lat');
	var lng = GetQueryString('lng');

	var gsvc = new esri.arcgis.gmaps.Geometry(geoMetryUrl);
	var buffParams = new esri.arcgis.gmaps.BufferParameters();
	buffParams.unit = esri.arcgis.gmaps.SRUnitType.DEGREE;// FOOT;
	buffParams.unionResults = true;// false; // union results. The default is
	// false
	var point = new GLatLng(lat, lng);
	buffParams.distances = [ 500 / 108000 ]; // distance in unit type
	// alert(point);
	buffParams.geometries = [ point ];// GLog.write(buffDistances/108000);
	gsvc.buffer(buffParams);
	GEvent.addListener(gsvc, "buffercomplete", bufferStationComplete);
}
function bufferStationComplete(results) {
	// alert(results.geometries.length);
	// Construct a 1-dimensional array of polygons to use in the
	// query.queryGeometry.
	var bufferPolys = [];
	for ( var x = 0; x < results.geometries.length; x++) {
		for ( var y = 0; y < results.geometries[x].length; y++) {
			bufferPolys.push(results.geometries[x][y]);
			// alert(results.geometries[x][y]);
		}
	}
	if (!bufferPolys[0]) {
		return;
	}
	// alert(bufferPolys[0]);
	esri.arcgis.gmaps.Config.proxyUrl = "./proxy.jsp";
	var queryNearStation = new esri.arcgis.gmaps.Query();
	queryNearStation.returnGeometry = true;// false;
	queryNearStation.where = "rownum<=1";
	queryNearStation.outFields = [ "STATION_ID","STATION_NA" ];
	var NearStationTask = new esri.arcgis.gmaps.QueryTask(statrionURL);
	queryNearStation.queryGeometry = bufferPolys[0];
	NearStationTask.execute(queryNearStation, false, qNearStationTaskComplete);

}
function qNearStationTaskComplete(fset) {
	// alert(fset.features.length)
	var inputStat = document.getElementById("input_stationName");
	if (!fset || fset.features.length == 0) {
		inputStat.value="获取地铁站点失败，请在地铁站点500米范围内打点。";
		inputStat.name="";
		parent.setAddBusifinish();
		return;
	}
	for ( var i = 0; i < 1; i++) {
		var Name = fset.features[i].attributes.STATION_NA;
		var stationID=fset.features[i].attributes.STATION_ID;
		// alert(Name);
		if(stationID!==null){
			inputStat.value = Name;
			inputStat.name=stationID;
			getStationExit(stationID);
		}else{
			inputStat.value="获取地铁站点失败，请在地铁站点500米范围内打点。";
			inputStat.name="";
			parent.setAddBusifinish();
		}
	}
}
// 获取URL中的参数
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>获取出入口>>>>>>>>>>>>>>>>>>>>>>>>
function getStationExit(stationId){
	var url = "./StationExitProcesser";
	var data = {
			stationID :stationId
	};
	parent.$.get(url, data, handleGetStationExit);
}
function handleGetStationExit(xmlDoc){
	var sel_exitO=document.getElementById("sel_station_exit");
	
	var stExitList=xmlDoc.getElementsByTagName("stationExit");
	for(var i=0;i<stExitList.length;i++){
		var tx=stExitList[i].childNodes[0].nodeValue;
		var varItem = new Option(tx, i+1);
		sel_exitO.options.add(varItem);
	}
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>>>>获取小类>>>>>>>>>>>>>>>>
function changetype(selectObj) {
	var correntText = selectObj.options[selectObj.selectedIndex].text;
	if (correntText == "请选择") {
		var selSmallType = document.getElementById("sel_small_type");
		selSmallType.options.length = 0;
		var varItem = new Option("请选择", 1);
		selSmallType.options.add(varItem);
		return;
	} else {
		var url = "./SmallTypeProcesser";
		var corretnId = selectObj.options[selectObj.selectedIndex].value;
		var data = {
			bigtypeId :corretnId
		};
		parent.$.get(url, data, handleChangetype);
	}

}
function handleChangetype(xmlDoc) {
	//alert(xmlDoc.xml);
	var smallTypeList = xmlDoc.getElementsByTagName("record");
	var selSmallType = document.getElementById("sel_small_type");
	selSmallType.options.length = 0;
	var varItem = new Option("请选择", 0);
	selSmallType.options.add(varItem);
	for ( var i = 0; i < smallTypeList.length; i++) {
		var smallTypeText = smallTypeList[i].getElementsByTagName("typeName").item(0).firstChild.nodeValue;
		var smallTypeId=smallTypeList[i].getElementsByTagName("typeId").item(0).firstChild.nodeValue;
		var varItem = new Option(smallTypeText, smallTypeId);
		selSmallType.options.add(varItem);
	}
}
// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

// >>>>>>>>>>>>>>提交数据到服务器>>>>>>>>>>>>>>>>>
function commitBusinss(data) {
	var url = "./AddBusinessProcesser";
	parent.$.post(url,data,parent.handlecommitBusinss);
}
function handlecommitBusinss(xml) {
	var message=xml.getElementsByTagName("message").item(0).firstChild.nodeValue;
	var sdiv=document.getElementById("divStatus");
	sdiv.innerHTML="<b>"+message+"</b>";
	setTimeout("coloseCommitStatus()",3000);
	addBusifinish = true;
	
	GEvent.removeListener(markListener);
	var curMark=markList[i-1];
	GEvent.addListener(curMark, "click", function() {
		//marker.openInfoWindowHtml(message);
		gmap.openInfoWindowHtml(curMark.getLatLng(),message);
	});
}
function checkBusinessData(userId) {
	//alert(userId);
	// 商家名称
	var nameO=document.getElementById("input_name");
	var nameV = nameO.value;
	// 商家地址
	var addressO = document.getElementById("input_address");
	var addressV = addressO.value;
	// 商家简介
	var introduceO = document.getElementById("textarea_introduce");
	var introduceV = introduceO.value;
	// 最近地铁站点
	var nearStationV = document.getElementById("input_stationName").name;
	// 最近地铁出口
	var selStationExit = document.getElementById("sel_station_exit");
	var stationExitV = selStationExit.options[selStationExit.selectedIndex].text;
	// 大类
	var selBigType = document.getElementById("sel_big_type");
	var bigTypeV = selBigType.options[selBigType.selectedIndex].value;
	// 小类
	var selSmallType = document.getElementById("sel_small_type");
	var smallTypeV = selSmallType.options[selSmallType.selectedIndex].value;
	// 营业开始时
	var workTimeHstart = document.getElementById("work_time_h_start");
	var startTimeHV = workTimeHstart.options[workTimeHstart.selectedIndex].text;
	// 营业开始分
	var workTimeMstart = document.getElementById("work_time_m_start");
	var startTimeMV = workTimeMstart.options[workTimeMstart.selectedIndex].text;
	// 营业结束时
	var workTimeHend = document.getElementById("work_time_h_end");
	var endTimeHV = workTimeHend.options[workTimeHend.selectedIndex].text;
	// 营业结束分
	var workTimeMend = document.getElementById("work_time_m_end");
	var endTimeMV = workTimeMend.options[workTimeMend.selectedIndex].text;

/*	alert(nameV + "|" + addressV + "|" + introduceV + "|" + nearStationV + "|"
			+ StationExitV + "|" + bigTypeV + "|" + smallTypeV + "|"
			+ opentimeV);*/

	if (nameV == "" || nameV == null) {
		alert("商家名称不能为空！");
		return false;
	}
	if (nameV.length > 46) {
		alert("商家名称不能超过50字！");
		return false;
	}
	if (addressV == "" || addressV == null) {
		alert("地址不能为空！");
		return false;
	}
	if (nearStationV == "" || nearStationV == null) {
		alert("地铁站名称不能为空！");
		return false;
	}
	if (stationExitV == "请选择出口") {
		stationExitV ="A";
	}
	if (bigTypeV == "" || bigTypeV == null) {
		alert("大类名称不能为空！");
		return false;
	}
	if (smallTypeV == "" || smallTypeV == null) {
		alert("小类名称不能为空！");
		return false;
	}
	if (startTimeHV == "时") {
		alert("请选择营业时间的开始小时！");
		return false;
	}
	if (startTimeMV == "分") {
		alert("请选择营业时间的开始分钟！");
		return false;
	}

	if (endTimeHV == "时") {
		alert("请选择营业时间的结束小时！");
		return false;
	}
	if (endTimeMV == "时") {
		alert("请选择营业时间的结束分钟！");
		return false;
	}
	if (introduceV == "" || introduceV == null) {
		alert("简介不能为空！");
		return false;
	} else if (introduceV.length > 1900) {
		alert("简介数字不能超过2000字！");
		return false;
	}
	var opentimeV = startTimeHV + ":" + startTimeMV + "-" + endTimeHV + ":"
			+ endTimeMV;
	var lat = GetQueryString('lat');
	var lng = GetQueryString('lng');
	//alert(nearStationV);
	//return;
	
	//提交数据
	var data={
			userID:userId,
			name:nameV,
			address:addressV,
			nearStationID:nearStationV,
			stationExit:stationExitV,//stationExitV
			bigTypeID:bigTypeV,
			smallTypeID:smallTypeV,
			opentime:opentimeV,
			introduce:introduceV,
			SLOC_X:lng,
			SLOC_Y:lat
			}
	//alert("/"+userId+"/"+nameV+"/"+nameV+"/"+nearStationV+"/"+stationExitV+"/"+bigTypeV+"/"+smallTypeV+"/"+opentimeV+"/"+introduceV+"/"+lng+"/"+lat);
	commitBusinss(data);
	parent.openCommitStatus();
	
	//设置页面控件不可用
	nameO.disabled="disabled";
	addressO.disabled="disabled";
	introduceO.disabled="disabled";
	selStationExit.disabled="disabled";
	selBigType.disabled="disabled";
	selSmallType.disabled="disabled";
	workTimeHstart.disabled="disabled";
	workTimeMstart.disabled="disabled";
	workTimeHend.disabled="disabled";
	workTimeMend.disabled="disabled";
	var buttonO=document.getElementById("but_submit");
	buttonO.disabled="disabled";
}

function openCommitStatus(){
	//提交状态显示
	var divCoverScreen = document.createElement("div");
	divCoverScreen.id="fullSreenDIV";
	document.body.appendChild(divCoverScreen); 
	
	var divStatus=document.createElement("div");
	divStatus.id="divStatus";
	document.body.appendChild(divStatus); 
	divStatus.innerHTML="<img src='./images/index/loading.gif' />&nbsp;发送中,请稍候...";
	var h = document.documentElement.clientHeight;
	var w = document.documentElement.clientWidth;
	divStatus.style.top = (h / 2 - 50) + "px";
	divStatus.style.left = (w / 2 - 100) + "px";
}
function coloseCommitStatus(){
	var divCoverScreen = document.getElementById("fullSreenDIV");
	document.body.removeChild(divCoverScreen); 
	
	var divStatus = document.getElementById("divStatus");
	document.body.removeChild(divStatus); 

}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function setAddBusifinish(){
	addBusifinish=true;
}
