// STORE CURRENT TAB REF 
var curSelectedTab = 1; 
var coordState = ""
//function to get number of record to download, used in data download only

function getMessage(objValA){
	alert(objValA+' You are only allow to select 3 states...');
}

function getDownloadData(){

	document.getElementById('subTab'+curSelectedTab).style.display = 'none'; 
	document.getElementById('step'+curSelectedTab).style.display = 'block' ;
	if(curSelectedTab == 1){
			spVal = document.getElementById("speciesid").value
			toYear = document.getElementById("toYear1").value
			fromYear = document.getElementById("fromYear1").value

			stateVal = ""
			objRef=document.getElementById("regionSelect")
			totSelected = 0
			for (i=0; i<objRef.options.length; i++) {
				if (objRef.options[i].selected) {
					stateVal += objRef.options[i].value + ",";
					totSelected ++
				}
			}
			stateVal = stateVal.substring(0,stateVal.length-1)

		}else{
			stateVal = document.getElementById("region").value
			toYear = document.getElementById("toYear2").value
			fromYear = document.getElementById("fromYear2").value			
			spVal = ""
			objRef=document.getElementById("speciesidSelect")
			totSelected = 0
			for (i=0; i<objRef.options.length; i++) {
				if (objRef.options[i].selected) {
					spVal += objRef.options[i].value + ",";
					totSelected ++
				}
			}
			spVal = spVal.substring(0,spVal.length-1)

		}
	errorChk = 0
	if(toYear != -1 && fromYear == -1){
					errorChk = 1
					alert("Invalid year range");
		}

		if(toYear == -1 && fromYear != -1){
			alert("Invalid year range");
			errorChk = 1
		}
		if (fromYear > toYear){
				alert("ERROR!, Year From value is GREATER THAN to From");
				errorChk = 1
		}
		if(errorChk == 1){
				document.getElementById('step'+curSelectedTab).style.display = 'none'; 
				document.getElementById('subTab'+curSelectedTab).style.display = 'block' ; 
				return false;
		}
		urlObj = 'ajax/ajx_getTotCount.cfc?method=getdata&filter1=' + spVal +'&filter2=' +stateVal +'&filter3=' +fromYear +'&filter4=' +toYear+ '&key=' +Math.random(); 
		buildXMLObj(urlObj)
		// call this after 
		updateObjValue('getDownloadDataData')
		xmlhttp.onreadystatechange=checkAjaxState;
}

function getDownloadDataData(){

	document.getElementById('totSurvey'+curSelectedTab).innerHTML =  "Number of surveys selected:" +response;

}

//get states  for imultiple species
function getStatesForSpecies(objRef,objHeight){
		objVal = ""
		totSelected = 0
		for (i=0; i<objRef.options.length; i++) {
			if (objRef.options[i].selected) {
				objVal += objRef.options[i].value + ",";
				totSelected ++
				if(totSelected >3){
					alert(' You are only allow to select 3 states...')
					return false
			}

			}
		}
				objVal = objVal.substring(0,objVal.length-1)
				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getStates.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getStatesForSpeciesData')
				xmlhttp.onreadystatechange=checkAjaxState;
			}
			


			// update species select list for a selected state
			function getStatesForSpeciesData(){

							spListStr = '<select id="region" name="region" style="width:250px" size=" '+ listHeight +' " >	<option value="-1">Select All</option>'
							spListStr += 	response
							spListStr += '</select>'
							document.getElementById('stateDiv').innerHTML =  spListStr;
							getYearsForStatesSpecies(1)
			}




//get years list for slected state and species

function getYearsForStatesSpecies(objHeight){

				if(curSelectedTab == 1){
						spVal = document.getElementById("speciesid").value
						stateVal = ""
						objRef=document.getElementById("regionSelect")
						totSelected = 0
						for (i=0; i<objRef.options.length; i++) {
							if (objRef.options[i].selected) {
								stateVal += objRef.options[i].value + ",";
								totSelected ++
							}
						}
						stateVal = stateVal.substring(0,stateVal.length-1)
			
					}else{
						stateVal = document.getElementById("region").value
						spVal = ""
						objRef=document.getElementById("speciesidSelect")
						totSelected = 0
						for (i=0; i<objRef.options.length; i++) {
							if (objRef.options[i].selected) {
								spVal += objRef.options[i].value + ",";
								totSelected ++
							}
						}
						spVal = spVal.substring(0,spVal.length-1)
					}



				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getStateSpeciesYears.cfc?method=getdata&filter1=' + stateVal +'&filter2=' +spVal+ '&key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getYearsForStatesSpeciesData')
				xmlhttp.onreadystatechange=checkAjaxState;
			}
			


	// update  years list for slected state and species
	function getYearsForStatesSpeciesData(){
					eval(response)
					if(curSelectedTab == 1){
							toYear = document.getElementById("toYear1")
							fromYear = document.getElementById("fromYear1")
						}else{
							toYear = document.getElementById("toYear2")
							fromYear = document.getElementById("fromYear2")			
						}					

					fromListStr = '<select name="fromYear'+ curSelectedTab +'" id="fromYear'+ curSelectedTab +'" style="width:120px;" >	<option value="-1">All years</option>'
					fromListStr += listZ2A
					fromListStr += '</select>'

					toListStr = '<select name="toYear'+ curSelectedTab +'" id="toYear'+ curSelectedTab +'" style="width:120px;margin-left:8px" >	<option value="-1">All years</option>'
					toListStr += listZ2A
					toListStr += '</select>'

					document.getElementById('YearCont'+curSelectedTab).innerHTML =  fromListStr + toListStr;
/*					fromYear.length = 1
					fromYear.innerHTML += listZ2A
					
					toYear.length = 1
					toYear.innerHTML += listA2Z*/
}


// get species for multiple states
function getSpeciesForStates(objRef,objHeight){
		objVal = ""
		totSelected = 0
		for (i=0; i<objRef.options.length; i++) {
			if (objRef.options[i].selected) {
				objVal += objRef.options[i].value + ",";
				totSelected ++
				if(totSelected >3){
					alert(' You are only allow to select 3 states...')
					return false
				}
			}
		}
				objVal = objVal.substring(0,objVal.length-1)
				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getSpecies.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getSpeciesForStatesData')
				xmlhttp.onreadystatechange=checkAjaxState;
			}
			


			// update species select list for a selected state
			function getSpeciesForStatesData(){
							spListStr = '<select id="speciesid" name="speciesid" style="width:250px" size=" '+ listHeight +' " >	<option value="-1">Select All</option>'
							spListStr += 	response
							spListStr += '</select>'
							document.getElementById('spDiv').innerHTML =  spListStr;
							getYearsForStatesSpecies(1);
			}



//Species detection map functions
		// get species for a selected state
			function getSpecies(objVal,objHeight){
				if(objVal == ""){alert('Please select all required field'); return;	}
				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getSpecies.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getSpeciesData')
				xmlhttp.onreadystatechange=checkAjaxState;
			}
			


			// update species select list for a selected state
			function getSpeciesData(){

							spListStr = '<select id="speciesid" name="speciesid" style="width:250px" size=" '+ listHeight +' ">	'
							spListStr += 	response
							spListStr += '</select>'
							document.getElementById('spDiv').innerHTML =  spListStr;
			}
			
		// get SurveyYears for a selected state and selected species
			function getYears(objValA,objValB,objHeight){
				if(objValA == "" || objValB == ""){alert('Please select state and species'); return;	}
				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getYears.cfc?method=getdata&filterA=' +objValA+'&filterB=' +objValB+' &key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				//updateObjValue('getSpeciesData')
				updateObjValue('getYearsData')
				xmlhttp.onreadystatechange=checkAjaxState;
				alert(objValA+objValB+' You are only allow to select 3 states...')
			}
			


			// update species select list for a selected state
			function getSpeciesData(){

							spListStr = '<select id="speciesid" name="speciesid" style="width:250px" size=" '+ listHeight +' ">	'
							spListStr += 	response
							spListStr += '</select>'
							document.getElementById('spDiv').innerHTML =  spListStr;
			}

		// draw MAP data
		function getSpeciesDetection(){
					state = document.getElementById("region").value
					speciseid = document.getElementById("speciesid").value
					year = document.getElementById("year").value	
					 if (state == "" || speciseid == ""){alert("Please select all required fields") ; return;}
					removeAllMarker()
						mapTitleStr = ""
					 w = document.getElementById("region").selectedIndex;
					 mapTitleStr += document.getElementById("region").options[w].text +' Routes Surveyed: ';

					 w = document.getElementById("speciesid").selectedIndex;
					 mapTitleStr += document.getElementById("speciesid").options[w].text + ' in ';

					 w = document.getElementById("year").selectedIndex;
					 mapTitleStr += document.getElementById("year").options[w].text;

					document.getElementById("mapTitle").innerHTML = mapTitleStr

					urlObj = 'ajax/ajx_getSpeciesDetectionData.cfc?method=getdata&state=' +state+ '&speciseid=' + speciseid +'&year='+ year +' &key='+ Math.random(); 

					buildXMLObj(urlObj)
				 // call this after 
					updateObjValue('getSpeciesDetectionData')
					xmlhttp.onreadystatechange=checkAjaxState;				 
			}
			

		function getSpeciesDetectionData(){

				eval(response)
			if(dataArray.length == 0){alert("data not found"); return}
				var AstopIcon = new GIcon(G_DEFAULT_ICON);
				AstopIcon.iconSize = new GSize(22,29);
				AstopIcon.shadow = "";	
				AstopIcon.image = "images/frogYes.png";
				AstopIcon.printImage ="images/frogYes.png";
				AstopIcon.mozPrintImage ="images/frogYes.png";

				var VstopIcon = new GIcon(G_DEFAULT_ICON);
				VstopIcon.iconSize = new GSize(22,29);
				VstopIcon.shadow = "";	
				VstopIcon.image = "images/frogNoDetect.png";
				VstopIcon.printImage ="images/frogNoDetect.png";
				VstopIcon.mozPrintImage ="images/frogNoDetect.png";

				//dataArray array created by arrayStr 
					routeArray = new Array();
					var  marker = 0
					for (counter = 0; counter <= dataArray.length-1 ; counter++)	{
								lat = dataArray [counter][0]
								lon = dataArray [counter][1]
								details = dataArray [counter][2] 
								statusValue = dataArray [counter][3]
								maxCallIndex = dataArray [counter][4]
								if(statusValue == 1){
										useIcon = AstopIcon
										details +=  '<br /> Max call index =' + maxCallIndex
								}else
								{useIcon = VstopIcon}
								
								eval('var marker'+  counter  + '= new GMarker(new GLatLng(lat,lon),{  icon:useIcon });')
		
								eval('routeArray[counter] = marker'+counter)
								eval('GEvent.addListener(marker'+ counter+' , "click", function() {marker'+ counter+'.openInfoWindowHtml("' + details +'")  })')
								eval('map.addOverlay(marker'+ counter+');')
					}
						cntStop = Math.round(dataArray.length  /2)
						lat = dataArray [cntStop][0]
						lon = dataArray [cntStop][1]

						// hard coding state codes for different zoom level
						state = document.getElementById("region").value
						if (state == 21 || state == 47){
				       map.setCenter(new GLatLng(lat, lon), 7);
						}
						else{
				       map.setCenter(new GLatLng(lat, lon), 6);
						}
			}



//END -- Species detection map functions

//Route availability map 		

function getAvailiability(objVal,objHeight){
				if(objVal == ""){alert('Please select all required field'); return;	}
				removeAllMarker()
				var w = document.getElementById("region").selectedIndex;
				var selected_text = document.getElementById("region").options[w].text;
				document.getElementById("mapTitle").innerHTML = selected_text + ' route availability map'

				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getRouteAvailiability.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)

				updateObjValue('getRouteAvailiabilityData')
				xmlhttp.onreadystatechange=checkAjaxState;								

		}
		
		
	function getRouteAvailiabilityData(){
			eval(response)
			if(dataArray.length == 0){alert("data not found"); return}
			var AstopIcon = new GIcon(G_DEFAULT_ICON);
			AstopIcon.iconSize = new GSize(22,29);
			AstopIcon.shadow = "";	

			AstopIcon.image = "images/assign3.png";
			AstopIcon.printImage ="images/assign3.png";
			AstopIcon.mozPrintImage ="images/assign3.png";

			var VstopIcon = new GIcon(G_DEFAULT_ICON);
			VstopIcon.iconSize = new GSize(22,29);
			VstopIcon.shadow = "";	
			VstopIcon.image = "images/vacant3.png";
			VstopIcon.printImage ="images/vacant3.png";
			VstopIcon.mozPrintImage ="images/vacant3.png";

			//dataArray array created by arrayStr 
			routeArray = new Array();
			var  marker = 0
			for (counter = 0; counter <= dataArray.length-1 ; counter++)	{
						lat = dataArray [counter][0]
						lon = dataArray [counter][1]
						details = dataArray [counter][2] 
						statusValue = dataArray [counter][3]
						
						coordState = document.getElementById('region').value;
						if(statusValue != ""){	useIcon = AstopIcon	; details += " <br />This route already has an observer assigned. ";} else	{useIcon = VstopIcon; details += " <br />This route is not assigned to an observer. ";}
						
						//if(status = ""){useIcon = VstopIcon; details += " <br />This route is not assigned to an observer."} else	{useIcon = AstopIcon	; details += " <br />This route already has an observer assigned. "}
						
				
						eval('var marker'+  counter  + '= new GMarker(new GLatLng(lat,lon),{  icon:useIcon });')
						eval('routeArray[counter] = marker'+counter)
						eval('GEvent.addListener(marker'+ counter+' , "click", function() {marker'+ counter+'.openInfoWindowHtml('  + "'" + details + '<br /><a style="margin-top:5px" href="javascript:showCoordInfo(coordState)" title="show coordinator info.">show coordinator info.</a>' + "'" +')  })')

						eval('map.addOverlay(marker'+ counter+');')
			}
			cntStop = Math.round(dataArray.length  /2)
			lat = dataArray [cntStop][0]
			lon = dataArray [cntStop][1]

			// hard coding state codes for different zoom level
			state = document.getElementById("region").value.substring(0,2)
			if (state == 21 || state == 47){
				 map.setCenter(new GLatLng(lat, lon), 7);
			}
			else{
				 map.setCenter(new GLatLng(lat, lon), 6);
			}		

}		
		
// END --Route availability map 

// reults by state function
		function getChartState(objVal){
				if(objVal == ""){alert('Please select all required field'); return;	}
				urlObj = 'ajax/ajx_getChartRegion.cfc?method=getData&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getChartStateData')
				xmlhttp.onreadystatechange=checkAjaxState;		
			}
	

			function getChartStateData(){
							//update new values to HTMl object
							spListStr ='<select id="region" name="region" style="width:250px; border:0px" size="18">'
							spListStr += 	response
							spListStr += '</select>'
							//alert(spListStr)
							document.getElementById('spDiv').innerHTML =  spListStr;
			}
//END  get states for selected species



//results by Species Chart  functions

		// get species for a selected state
			function getchartSpecies(objVal,objHeight){
				if(objVal == ""){alert('Please select all required field'); return;	}
				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getChartSpecies.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getchartSpeciesData')
				xmlhttp.onreadystatechange=checkAjaxState;
			}
			

			// update species select list for a selected state
			function getchartSpeciesData(){
							spListStr = '<select id="speciesid" name="speciesid" style="width:250px" size=" '+ listHeight +' ">	'
							spListStr += 	response
							spListStr += '</select>'
							document.getElementById('spDiv').innerHTML =  spListStr;
			}

//END results by Species Chart  functions


//Route data summary
function getRoutes(objVal,objHeight){
			if(objVal == ""){alert('Please select all required field'); return;	}
			listHeight = (objHeight== null)?20:objHeight;
			urlObj = 'ajax/ajx_getRouteDataSummary.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
			buildXMLObj(urlObj)
		 // call this after 
			updateObjValue('getRouteDataSummarysData')
			xmlhttp.onreadystatechange=checkAjaxState;
			}
			


			// update species select list for a selected state
			function getRouteDataSummarysData(){
							spListStr = '<select id="route" name="route" style="width:250px" size=" '+ listHeight +' ">'
							spListStr += 	response
							spListStr += '</select>'
							document.getElementById('spDiv').innerHTML =  spListStr;
			}

//end Route data summary

// Species Richness map
function getRichness(objVal,objHeight){
				if(objVal == ""){alert('Please select all required field'); return;	}
				var w = document.getElementById("region").selectedIndex;
				var selected_text = document.getElementById("region").options[w].text;

				document.getElementById("mapTitle").innerHTML = selected_text + ' routes: species richness for all routes'
				document.getElementById("pHeader").innerHTML = "for " + selected_text
				removeAllMarker()
				listHeight = (objHeight== null)?20:objHeight;
				urlObj = 'ajax/ajx_getRichness.cfc?method=getdata&filter=' +objVal+' &key='+ Math.random(); 
				buildXMLObj(urlObj)

				updateObjValue('getRichnessData')
				xmlhttp.onreadystatechange=checkAjaxState;								

		}
		
		
	function getRichnessData(){
			eval(response)
			if(dataArray.length == 0){alert("data not found"); return}
			var AstopIcon = new GIcon(G_DEFAULT_ICON);
			AstopIcon.iconSize = new GSize(22,29);
			AstopIcon.shadow = "";	

			AstopIcon.image = "images/frogYes.png";
			AstopIcon.printImage ="images/frogYes.png";
			AstopIcon.mozPrintImage ="images/frogYes.png";

			var VstopIcon = new GIcon(G_DEFAULT_ICON);
			VstopIcon.iconSize = new GSize(22,29);
			VstopIcon.shadow = "";	
			VstopIcon.image = "images/frogNo.png";
			VstopIcon.printImage ="images/frogNo.png";
			VstopIcon.mozPrintImage ="images/frogNo.png";

			//dataArray array created by arrayStr 
			routeArray = new Array();
			//var  marker = 0
			for (counter = 0; counter <= dataArray.length-1 ; counter++)	{
						//if(counter >= dataArray.length){alert('what')}
						lat = dataArray [counter][0]
						lon = dataArray [counter][1]
						details = dataArray [counter][2]  + '<br /><br /><strong>Species list</strong><br />' +dataArray [counter][4] +'<br />' 
						statusValue = dataArray [counter][3]
						urlTodataSummary = ""
						if(statusValue != ""){	
								useIcon = AstopIcon	; 
								details +=  dataArray [counter][2]
								urlTodataSummary = '<br /><a style="margin-top:5px" href="index.cfm?fuseaction=app.routeDataSummary'+dataArray [counter][5] +' " title="go to route data summary page">route data summary</a>' 
						}else	{
								useIcon = VstopIcon; 
						}

						eval('var marker'+  counter  + '= new GMarker(new GLatLng(lat,lon),{  icon:useIcon });')
						eval('routeArray[counter] = marker'+counter)
						eval('GEvent.addListener(marker'+ counter+' , "click", function() {marker'+ counter+'.openInfoWindowHtml('  + "'" + details + urlTodataSummary + "'" +')  })')

						eval('map.addOverlay(marker'+ counter+');')
			}
			cntStop = Math.round(dataArray.length  /2)
			lat = dataArray [cntStop][0]
			lon = dataArray [cntStop][1]

			// hard coding state codes for different zoom level
			state = document.getElementById("region").value.substring(0,2)
			if (state == 21 || state == 47){
				 map.setCenter(new GLatLng(lat, lon), 7);
			}
			else{
				 map.setCenter(new GLatLng(lat, lon), 6);
			}
}		

// END species Richness Map

//check login
function chkLogin(){
			obsID = document.getElementById("obsID").value
			routeID = document.getElementById("routeID").value
				urlObj = 'ajax/ajx_chkLogin.cfc?method=getdata&filter1=' + obsID +'&filter2=' +escape(routeID)+ '&key='+ Math.random(); 
				buildXMLObj(urlObj)
			 // call this after 
				updateObjValue('getChkLoginData')
				xmlhttp.onreadystatechange=checkAjaxState;
			}
			

			// update species select list for a selected state
			function getChkLoginData(){
						//document.getElementById('spDiv').innerHTML =  spListStr;
						eval(response)
						if(dataArray == "notfound"){
								alert('invaild login')
						}
						else{
								document.getElementById("observerID").value = dataArray[0]
								document.getElementById("yearsService") .value=  dataArray[1]
								document.getElementById("name").value =   dataArray[2] + ' ' + dataArray[3]
								document.daForm.submit();
						}
			}
		

//END check login



