// This will set the number of points to be shown on the legend
	var max_legend_pts = 10;
	var mutex_startStopStream = 0;
	var __islocal = 0;
	var __istreamtiming = 2; // this will make 2 sec timing 
	var __isStreaming =0;
	var jobidfilename="";
	var __streamingtime = 0;
	var __streamMaxLimit = 6120;
	var __streamingErrorTimeout =0;
	
	// Array for the clusters
	var arrclusterOrg = new Array();
	var arrclusterTemp = new Array()
	var pe = null ;
	for (i=0;i<8;i++)
		{
			arrclusterOrg[i] = "";
			arrclusterTemp[i]="";
		}
	var arrmarker = new Array()	
	var mutex =0 ;
	var isLegendDrawn = 0
	var mutex_count = 0 ;
	var	S1 ; // = $('svgPlaceHolder').down().getSVGDocument();
	var S2;
	var svgns = 'http://www.w3.org/2000/svg';
	
	var map=null; 
	var __features = null;
	
	
	
	function initS1()
	{
		
		S1 = $('clusters').down().getSVGDocument();
		canvas = S1.getElementById('canvas');
		canvas.setAttribute("background","black");
	}
	function initS2()
	{
		S2 = $('timeline').down().getSVGDocument();
		//S2.setAttribute("background-color","black");
		canvas = S2.getElementById('canvas');
		canvas.setAttribute("background-color","black");
	}
	
	function clusterclick(emer,size,label)
	{
		strval = "Emer : " + emer + " size : "+size +" Label : " + label ;
		//alert(strval)
	}
	
	function createRect(x,y,width,height,emer,size,label)
	{
				//S = $('svgPlaceHolder').down().getSVGDocument();
				var newRect = S1.createElementNS(svgns,"rect");
				newRect.setAttribute("x",x);	
				newRect.setAttribute("y",y);	
				newRect.setAttribute("width",width);
				newRect.setAttribute("height",height);
				newRect.setAttribute("stroke","black");
				newRect.setAttribute("stroke-width","2");
				//stroke="blue" stroke-width="2"
				//newRect.setAttribute("onclick","javascript:alert('hi');");
				newRect.setAttribute("onclick","parent.clusterclick("+emer+","+size+","+label+");");
				newRect.setAttribute("fill", "none");
				return newRect;
	}
	function createFillRectangle(x,y,width,height,color)
	{
				var newRect = S1.createElementNS(svgns,"rect");
				newRect.setAttribute("x",x);	
				newRect.setAttribute("y",y);	
				newRect.setAttribute("width",width);
				newRect.setAttribute("height",height);
				//newRect.setAttribute("stroke","black");
				//newRect.setAttribute("stroke-width","2");
					//stroke="blue" stroke-width="2"
				//newRect.setAttribute("onclick","javascript:alert('hi');");
				//newRect.setAttribute("onclick","parent.clusterclick("+emer+","+size+","+label +");");
				newRect.setAttribute("fill", color);
				newRect.setAttribute("z-index","-99999");
				return newRect;
		
	}
	function createCircle(x,y,r,emer,size,label)
	{

		var newCircle = S1.createElementNS(svgns,"circle");
		newCircle.setAttribute("cx",x);	
		newCircle.setAttribute("cy",y);	
		newCircle.setAttribute("r",r);
		newCircle.setAttribute("stroke","blue");
		newCircle.setAttribute("stroke-width","2");
		newCircle.setAttribute("onclick","parent.clusterclick("+emer+","+size+","+label + ");");
		newCircle.setAttribute("fill","none");
		//newCircle.setAttribute("onclick","javascript:alert('hi');");
		var zindex = (-1 * parseInt(r)).toString();
		newCircle.setAttribute("z-index",zindex);
		return newCircle;
	}
	function createPolyLine(points , color)
	{

		var newLine = S1.createElementNS(svgns,"polyline");
		newLine.setAttribute("points",points);	
		//newLine.setAttribute("cy",y);	
		//newLine.setAttribute("r",r);
		newLine.setAttribute("stroke",color);
		newLine.setAttribute("stroke-width","2");
		newLine.setAttribute("fill","none");
		//newLine.setAttribute("onmouseover","javascript:alert('hi');");
		return newLine;
	}
	function finishInit()
	{			
				
				canvas = S1.getElementById('canvas');
				objRect = createRect(12,20,200,200);
				canvas.appendChild(objRect);
				objcircle = createCircle(30,20,10);
				canvas.appendChild(objcircle);
				
				var S1 = null;
				if(S1==null)
				{					
					S1 = $('svgPlaceHolder1').down().getSVGDocument();	
				}
				canvas = S1.getElementById('canvas');
				objRect = createRect(12,20,200,200);
				canvas.appendChild(objRect);
				objcircle = createCircle(30,20,10);
				canvas.appendChild(objcircle);
				objcircle = createCircle(45,56,10);
				canvas.appendChild(objcircle);
	}
	
	function getLegendText(x,y,txt)
	{
		
		var new_txt = S1.createElementNS(svgns,"text");
		new_txt.setAttribute("x",x);
		new_txt.setAttribute("y",y);		
		new_txt.setAttribute("font-size","10");
		new_txt.setAttribute("fill","white");
		newtext=S2.createTextNode(txt);
		
		new_txt.appendChild(newtext);


		return new_txt;
		
		
	}
	function getLine(x1,y1,x2,y2,color)
	{
		var new_line = S1.createElementNS(svgns,"line");
		new_line.setAttribute("x1",x1);
		new_line.setAttribute("y1",y1);	
		new_line.setAttribute("x2",x2);
		new_line.setAttribute("y2",y2);
		new_line.setAttribute("fill","white");
		new_line.setAttribute("stroke",color);
		new_line.setAttribute("stroke-width","1");
		return new_line;
	}
	function setStartModule(maxnoofpts,interval) 
	{
		
		max_total_pts = parseInt(maxnoofpts);
		x_incr = (475-25)/max_legend_pts;
		x_start = 25;		
		TotalTime = max_total_pts * parseInt(interval);
		time_incr = TotalTime /max_legend_pts;
		time_incr = parseInt(time_incr);
	
		canvas2 = S2.getElementById('canvas');
		objsvg =  S2.getElementById('svgdoc');
		newobj1 = createFillRectangle(0,0,520,200,'black');
		canvas2.appendChild(newobj1);
		
		// Y -Axis ------------------
		var newLine = S1.createElementNS(svgns,"polyline");
		newLine.setAttribute("points",'25 0,25 170');	
		newLine.setAttribute("stroke","white");
		newLine.setAttribute("stroke-width","1");
		newLine.setAttribute("fill","none");
		canvas2.appendChild(newLine);
		
		// X-Axis ---------------------
		var newLine1 = S1.createElementNS(svgns,"polyline");
		newLine1.setAttribute("points",'25 170,512 170');	
		newLine1.setAttribute("stroke","white");
		newLine1.setAttribute("stroke-width","1");
		newLine1.setAttribute("fill","none");
		canvas2.appendChild(newLine1);
		
		if ( isLegendDrawn ==0 )
		{
				for (xaxis = 0 ;xaxis <= max_legend_pts ; xaxis++ )
				{ 
					x_val = x_start + (x_incr * xaxis);
					x_legendLine = x_val;
					if(xaxis != 0)
						x_legendLine = (x_val);
					newLegenLine = getLine(x_legendLine.toString(),'170',x_legendLine.toString(),'176','white');
					objsvg.appendChild(newLegenLine);
					new_txt = getLegendText(x_val.toString() , '190' , (xaxis * time_incr).toString());
					objsvg.appendChild(new_txt);
				 }
				 
				newLegenLine = getLine('19','2','25','2','white');
				objsvg.appendChild(newLegenLine);
				new_txt = getLegendText('10' , '10' , '1');
				objsvg.appendChild(new_txt);
				newLegenLine = getLine('19','170','25','170','white');
				objsvg.appendChild(newLegenLine);
				new_txt = getLegendText('10' , '170' , '0');
				objsvg.appendChild(new_txt);
				isLegendDrawn = 1;
		}
		
		
    	
	}
	
	
	
	
	function start(text)
 	{
 		
		
		// code for IE
		if (window.ActiveXObject)
		  {
		  var doc=new ActiveXObject("Microsoft.XMLDOM");
		  doc.async="false";
		  doc.loadXML(text);
		  }
		// code for Mozilla, Firefox, Opera, etc.
		else
		  {
		  var parser=new DOMParser();
		  var doc=parser.parseFromString(text,"text/xml");
		  
		  }
		
		// documentElement always represents the root node
		if(doc != null)
			{
			
				var x=doc.documentElement;
				divobj = document.getElementById("val");
				obj = x.getElementsByTagName("clusters");
				canvas1 = S1.getElementById('canvas');
				$('Errorval').innerHTML = "";
				objError = x.getElementsByTagName("errormessage");
				
				if(objError)
				{
				
				  if(objError[0].getAttribute("baseline_error").toString() != "")
				    $('Errorval').innerHTML += objError[0].getAttribute("baseline_error").toString();
				  if(objError[0].getAttribute("cluster_error").toString() != "")
				    $('Errorval').innerHTML += objError[0].getAttribute("cluster_error").toString(); 
				}
				
				while(canvas1.hasChildNodes()){
				 	canvas1.removeChild(canvas1.lastChild);
					}
				
				//------------------------------------------
				if(obj != null && obj.length > 0)
				{
				for (index=0; index < obj.length ;  index++)
				{
					 obj1 = obj[index].getElementsByTagName("cluster");
					
					for (i =0 ; i < obj1.length ;i++)
					{
						if(obj1[i].childNodes.length > 0 )
						{
							emer = obj1[i].getAttribute("emergent");
							size = obj1[i].getAttribute("size");
							label = obj1[i].getAttribute("label");
							val = obj1[i].childNodes[0].nodeValue;
							arr = val.split(",");
							var svgobj = null;
							if(parseInt(emer) == 0 )
								{
								  svgobj = createCircle(arr[0],200-arr[1],arr[2],emer,size,label);
								 }
							else
								{
									width = parseInt(arr[2]);
									width = width + width ;
									
									 svgobj = createRect(arr[0]-arr[2],200-arr[1]-arr[2],width.toString(),width.toString(),emer,size,label);	
								}
														
								canvas1.appendChild(svgobj);
								
								ilabel = parseInt(label);
								var strTmp =	emer+","+val;
								if (strTmp != arrclusterTemp[ilabel])
									{
										
										arrclusterOrg[ilabel] = arrclusterTemp[ilabel];
										arrclusterTemp[ilabel] = strTmp;
																									
									}
									if(__isStreaming == 1)
									{
									if(arrclusterOrg[ilabel] != "")
										{
											//alert('hi');
											val0 = arrclusterOrg[ilabel];
											arr0 = val0.split(",");
											arr1 = arrclusterTemp[ilabel].split(",");
											x0 = parseInt(arr0[1]);
											y0 = 200 - parseInt(arr0[2]);
											x1 = parseInt(arr1[1]);
											y1 = 200 - parseInt(arr1[2]);
											objCluster = getLine(x0.toString(),y0.toString(),x1.toString(),y1.toString(), 'gray');
											canvas1.appendChild(objCluster);
										}
									  }
							
						}
					}
								
				}
				}
				
				//--------------------------------------------------------------
				canvas2 = S2.getElementById('canvas');
				while(canvas2.hasChildNodes()){
				 	canvas2.removeChild(canvas2.lastChild);
					}
				
				baseObj = x.getElementsByTagName("baseline");
				var interval =	baseObj[0].getAttribute("interval");
				var max_no_points = baseObj[0].getAttribute("max_pts");
				var totalDuration = parseInt(interval) * parseInt(max_no_points);
				var incr = (475-25) / totalDuration * interval;
				setStartModule(max_no_points,interval);	
								
				//--------------------------------------------------------------
				polyObj = x.getElementsByTagName("node");
							
				if(polyObj != null && polyObj.length >0 )
				{
						//------------------------------------------
						for (index=0; index < polyObj.length ;  index++)
						{
							 obj1 = polyObj[index].getElementsByTagName("polyline");
							 var id = polyObj[index].getAttribute("id");
							 var lat = polyObj[index].getAttribute("lat");	
							 var long = polyObj[index].getAttribute("long");	 				
							 var city = polyObj[index].getAttribute("city");	 	
							 var postalcode = polyObj[index].getAttribute("postalCode");	
							 var score =  polyObj[index].getAttribute("score");					
							 var detector = polyObj[index].getAttribute("detector");	
							 var country = polyObj[index].getAttribute("country");			
							for (i =0 ; i < obj1.length ;i++)
							{
								//alert(obj[0].childNodes[i].nodeName);
								if(obj1[i].childNodes.length > 0 )
								{
									val = obj1[i].childNodes[0].nodeValue;
									var svgobj = null
									strval =""
									arrval = val.split(",");
									for (index1=0;index1<arrval.length;index1++)
									 {
									 	if(parseFloat(arrval[index1]) != -1)
									 	{
									 		strval += (incr * index1 + 25).toString() +" ";
										 	strval += parseInt(170 - parseFloat(arrval[index1] * 170)).toString();
									 	
										 	if(index1 != arrval.length -1 )
										 		strval += ",";
								 		}
									  }
									 // alert(strval);
									//$('val').innerHTML += strval + "<br>";
									svgobj = createPolyLine (strval , 'red');
									svgobj.setAttribute("onmouseover","parent.showinfo('"+id+"','"+lat+"','"+long+"','"+city+"','"+country+"','"+postalcode+"','"+score+"','"+detector+"');");
									
									//alert("node : " +  strval);
									//alert(svgobj);
									canvas2.appendChild(svgobj);
									if((parseFloat(lat) != 0) && (parseFloat(long) != 0))
										drawcircles(lat,long,'r', id, city,country, postalcode,score,detector)
									//alert(strval);
								} // if(obj1[i].childNodes.length > 0 ) 
							 } ////for (i =0 ; i < obj1.length ;i++)  
						}// for (index=0; index < polyObj.length ;  index++) 
				 } // if(polyObj != null && polyObj.length >0 ) 
				//--------------------------------------------------------------
				
				polyObj = x.getElementsByTagName("baseline");
				
				//------------------------------------------
				for (index=0; index < polyObj.length ;  index++)
				{
					
					 obj1 = polyObj[index].getElementsByTagName("polyline");
					
					for (i =0 ; i < obj1.length ;i++)
					{
						//alert(obj[0].childNodes[i].nodeName);
						if(obj1[i].childNodes.length > 0 )
						{
							//emer = obj1[i].getAttribute("emergent");
							//size = obj1[i].getAttribute("size");
							//label = obj1[i].getAttribute("label");
							val = obj1[i].childNodes[0].nodeValue;
							var svgobj = null
							//alert(val);
							strval =""
							arrval = val.split(",");
							for (index1=0;index1<arrval.length;index1++)
							 {
							 
							 	if(parseFloat(arrval[index1]) != -1)
							 	{
							 		strval += (incr * index1 + 25).toString() +" ";
							 		strval += parseInt(170 - parseFloat(arrval[index1] * 170)).toString();
							 	
							 									 	if(index1 != arrval.length -1 )
							 			strval += ",";
							 	}
							  }
							  //alert("baseline : " +  strval);
							//$('val').innerHTML += strval +"<br>";
							svgobj = createPolyLine (strval,'#00FF00');
							canvas2.appendChild(svgobj);							
						}
					}
				}
				//--------------------------------------------------------------
				
				
			}
			
	 		currentTime = new Date();
 		// $('val').innerHTML += " <br / > End : "+currentTime.toString() + "   " + currentTime.getMilliseconds();
 		 //$('val').innerHTML += " ------------------------<br/>";
 		 delete text;
		}
	function start1()
	{
		var text1 = document.getElementById("divxmlContent").innerHTML;
	 	start(text1);
	}
	
	function removeAll()
	{
		try
		{
				$('Errorval').innerHTML = "";
				$('val').innerHTML = "";
				$('val1').innerHTML = "";
				canvas1 = S1.getElementById('canvas');
				while(canvas1.hasChildNodes()){
						 	canvas1.removeChild(canvas1.lastChild);
							}
				canvas2 = S2.getElementById('canvas');
				while(canvas2.hasChildNodes()){
						 	canvas2.removeChild(canvas2.lastChild);
							}
				objsvg =  S2.getElementById('svgdoc');
				//alert(objsvg.childNodes.length);
				
			        if(objsvg.childNodes.length > 3)
				{
				  while (objsvg.childNodes.length > 3)
				  	objsvg.removeChild(objsvg.lastChild);
				  isLegendDrawn = 0;
				}
			}
			catch(e)
				{
				}
	}
	
	function getXMLAjaxCall()
	{
	//mutex_startStopStream = 1; 
	//mutex = 0;
	
			if ( mutex_startStopStream  == 1)
			{
						  if ( mutex == 0)
						  {
								  	mutex_count =0 ;
									//this.counter = this.counter ? this.counter + 1 : 1;
									//../PRODUCTION-webqueryv3/
								

								var URL = "";
								if(__islocal == 0)
								{
									 URL= "http://angle.ncdm.uic.edu/temp/";
									URL += jobidfilename+".xml";
								}
								else
								{
									  URL = jobidfilename+".xml"; 
								}
								new Ajax.Request(URL,
									  {
									    method:'get',
									    onSuccess: function(transport){
									      var response = transport.responseText;
									      mutex = 1;
									     __streamingErrorTimeout = 0;
									      removeAllMarkers();
									      start(response);
									      $('val').innerHTML="";
									      mutex=0;
									       __streamingtime++;
										 if(__streamingtime > __streamMaxLimit)
												 	startstopStream(0);
									      //alert("Success! \n\n" + response);
									    },
									    onFailure: function(){ mutex =0 ; 
									    	__streamingErrorTimeout++;
									    	if(__streamingErrorTimeout > 60)
									    		    		startstopStream(0);
									    	
									    	/*removeAll();*/ }
									  });
						   }
						   else
						   {
						   	if (mutex_count < 10 )
						   		mutex_count++;
						   	else
						   	{
						   		mutex = 0 ;
						   		mutex_count = 0;
						   	}
						   }
			   }

	}

	
	function init_start()
	{
		startstopStream(0);
		$('val').innerHTML="";
		
		
	}
	
	function startstopStream(val)
	{
		//alert(val);	
		var URL = "";
		var obj = document.getElementById("sfeaturesStream");
		for (i=0;i<8;i++)
			{
				arrclusterOrg[i] = "";
				arrclusterTemp[i]="";
			}
		//alert(obj.selectedValue);
		//alert(obj.selectedIndex);
		//alert(obj.options[obj.selectedIndex].text);
		if(val == 1) 
		{
			if(jobidfilename !="")
			  startstopStream(0);
			__streamingtime = 0;
	      __streamingErrorTimeout =0;
			
			$('val').innerHTML = " Stream started - please wait for the buffer to fill";
			mutex_startStopStream = 1;
			var ithres = 0.1;
			
			if(obj != null)
			{
				var val1 = obj.options[obj.selectedIndex].text;
				var thres = document.getElementById("thresholdStream").value;
				ithres = parseFloat(thres);
			}
			if (ithres < 1.0 && ithres >= 0)
				{
				 
				}
			else
				{
					ithres = 0.1;
					document.getElementById("thresholdStream").value = ithres.toString();
				}
							//http://angle.ncdm.uic.edu/servicesv3/streamcontrol.html?status=enable&feature=numberOfIps
					//URL = "http://angle.ncdm.uic.edu/servicesv3/streamcontrol.html?status=enable&feature="+val+"&threshold="+ithres.toString();
				if(__islocal == 1)
							URL = "job.dat"; 
				else
					{			
						URL = 	"http://angle.ncdm.uic.edu/servicesv3/streamcontrol.html?status=enable";
						URL +=  "&site="+document.getElementById("locationStream").options[document.getElementById("locationStream").selectedIndex].value;
						URL += "&threshold="+ document.getElementById("thresholdStream").value;
						URL += "&model="+document.getElementById("ddlModelStream").options[document.getElementById("ddlModelStream").selectedIndex].value;
						URL += "&feature="+document.getElementById("sfeaturesStream").options[document.getElementById("sfeaturesStream").selectedIndex].value;
						
						
				//http://angle.ncdm.uic.edu/servicesv3/streamcontrol.html?status=enable&site=1&threshold=0.1&model=52&feature=numberOfIps
					 }
		}	
		else
		{
		
			__streamingtime = 0;
	      __streamingErrorTimeout =0;
			if(jobidfilename!= "")
			{
				mutex_startStopStream =0 ;
				//URL = "http://angle.ncdm.uic.edu/servicesv3/streamcontrol.html?status=disable";
				if(__islocal == 1)
								URL = "job.dat"; 
					else
						{
							URL = "http://angle.ncdm.uic.edu/servicesv3/streamcontrol.html?status=disable";	
							URL += "&jobid="+jobidfilename;
						}
				removeAllMarkers();
				removeAll();
				$('val').innerHTML = " Streaming Inactive ";
				if(pe)
						{
					    pe.stop();
					    
					    }
				jobidfilename ="";
			}
		}
		new Ajax.Request(URL,
		  		{
				    method:'get',
				    onSuccess: function(transport1){
						//alert('stream started');

						if(val == 1 )
						{
							var txt = transport1.responseText;
							arrParent = txt.split('\n');
							//ERROR: max jobs reached	
							arrval = arrParent[0].replace(/^\s+|\s+$/g,"").split(':');
							if(arrval[0].replace(/^\s+|\s+$/g,"") != "ERROR")
							{
							jobidfilename = arrval[1].replace(/^\s+|\s+$/g,"");
							
							pe = new PeriodicalExecuter(getXMLAjaxCall, __istreamtiming);
							}
							else
							{
								$('Errorval').innerHTML = arrval[1];
							}
							
						}
				    },
				    onFailure: function(){ mutex =0 ; alert('Error in stream service URL '); }
				  }
				  );
				 
				 
			
				
		//alert(URL);
		
	

	}
	
	function showinfo(id,lat,long,city,country,postalcode,score,detector)
	{
		if( (parseInt(lat)!=0) && (parseInt(long) !=0 ))
		 {
	 	  strdisp = "Country :" + country + "<br>";
	 	  strdisp += "City :" + city + "<br>";
	     strdisp += "Postal Code : " + postalcode + "<br>";
	     strdisp += "IP : " + id +" <br>";
	     var strDetector = "";
	     switch(parseInt(detector))
	     {
	       case 1: strDetector = "uic2";
	           break;
	       case 2: strDetector ="uofc2";
	       				break;
	       case 4: strDetector ="anl2";
	       		break;
	       case 8: strDetector ="isi2";
	       		break;
	       case 15:  strDetector ="all";
	       			break;
	       default:  strDetector ="";
	       			break;
	     }
	     strdisp += "Detector : " + strDetector +"<br>";
	     strdisp += "Score : "+ score +"<br>";
	     //strdisp += "<a href='javascript:parent.callback();'> Map it</a> <br>";
	     map.openInfoWindowHtml(new GLatLng(parseFloat(lat), parseFloat(long)), strdisp);
	    }
	    
	}
	
	function drawcircles(lat,long,type, id, city, country , postalcode,score,detector)
	{
	 	  var Icon = new GIcon();	
		 // Icon.image = "images/circle-green.png";
		  //Icon.iconSize = new GSize(32,32);
		  Icon.image =  "images/circle.png";
		  Icon.shadow ="";
		  Icon.iconSize = new GSize(15,15);
		  markerOptions = { icon:Icon };
		 Icon.iconAnchor = new GPoint(7,7);    	 Icon.infoWindowAnchor = new GPoint(7,7);
		  Icon.enabled = true;
		  var point = new GLatLng(parseFloat(lat),parseFloat(long));
		  var marker = new GMarker(point, markerOptions);
		  

	   GEvent.addListener(marker, "click", function() {
			    /* strdisp = "IP : " + id +" <br>";
			     strdisp += "city :" + city + "<br>";
			     strdisp += "Postal Code : " + postalcode + "<br>";
			     strdisp += "Score : "+ score +"<br>";
			     strdisp += "Detector : " + detector +"<br>";
	   	 	  marker.openInfoWindowHtml(strdisp);
	   	 	  */
	   	 	 parent.showinfo(id,lat,long,city,country,postalcode,score,detector);
  			});
  			map.addOverlay(marker);
		arrmarker.push(marker);
	}
	
	function load() {
		document.getElementById("db").checked = 'true';
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(0, 0), 2);
        map.setCenter(new GLatLng(0, 0), 2);
	     map.addControl(new GSmallZoomControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10, 10)));
      }
		//init_start();     
		//startstopStream(1);
		//getXMLAjaxCall();
		
		document.getElementById("days").disabled = true;	
		document.getElementById("time").disabled = true;	
		var URL="";
		if(__islocal == 0)
		{
		 URL = "http://angle.ncdm.uic.edu/servicesv3/getdates.html?mode=months";
		}
		else
			{
		    URL ="tst.dat";
			}
		// Updating Time selector 
		new Ajax.Request(URL,
		  		{
				    method:'get',
				    onSuccess: function(transport1){
						var objMonths = document.getElementById("months");
						 for(i=objMonths.options.length-1;i>=0;i--)
							{
								objMonths.remove(i);
							}
						 
						
						var arrm = transport1.responseText.split('\n');
						
						var objddl = document.getElementById("months");
						var objslectoptions = document.createElement("OPTION");
						var txtnode = document.createTextNode("");
						objslectoptions.setAttribute("value","");
						objslectoptions.appendChild(txtnode);
						objddl.appendChild(objslectoptions);
						for (index =0 ; index < arrm.length ; index++)
						 {	
						   if( arrm[index].replace(/^\s+|\s+$/g,"") != "")
						     {		
							 		var arrs = arrm[index].split(',');
									var objslectoptions = document.createElement("OPTION");
	
									var txtnode = document.createTextNode(arrs[1]);
									objslectoptions.setAttribute("value",arrs[0]);
									objslectoptions.appendChild(txtnode);
									objddl.appendChild(objslectoptions);
									}
						 }
						
				    },
				    onFailure: function(){ alert("error getting months");}
				  }
				 );
		
		var URL_ModelList = "";
		if(__islocal == 0)
		{
		 URL_ModelList = "http://angle.ncdm.uic.edu/servicesv3/getmodels.html";
		}
		else
			{
		    URL_ModelList = "model.list";
		  }
		new Ajax.Request(URL_ModelList,
		  		{
				    method:'get',
				    onSuccess: function(transport1){
				 		var txt = transport1.responseText;
				 			var firstVal = setCollection(txt,'ddlCollection');
				 			setModel(firstVal , 'ddlModelDB')
							firstVal = setCollection(txt,'ddlCollectionStream');
							setModel(firstVal , 'ddlModelStream')
							//setModel(firstVal , 'ddlModel2')
							
				 		
				   },
				    onFailure: function(){ alert("error getting months");}
				  }
				 );
		var URL1 = ""
		if(__islocal == 0)
		 {
			  URL1 = "http://angle.ncdm.uic.edu/servicesv3/getversions.html" ; 
		 }
		else
		  { 
		    var URL1 ="features.dat";
		  }
		new Ajax.Request(URL1,
					{
					    method:'get',
					    onSuccess: function(transport1){
					     __features = transport1.responseText;
						  setfeatures(	__features ,0,'sfeaturesDB');	 
						  setfeatures(	__features ,0,'sfeaturesStream');	 
					},
			   onFailure: function(){ alert("error getting months");}
			 }
			 );
    }
    
    function setCollection(txt, ddlval)
    {
    	var arrmParent = txt.split('\n');
    	
					    var objCollection= document.getElementById(ddlval); 
							for(i=objCollection.options.length-1;i>=0;i--)
								{
													objCollection.remove(i);
								}
					    	
						   //var arrm = arrmParent[1].split(",");
							var firstVal = ""				
							for (index =0 ; index < arrmParent.length ; index++)
								 {	
								   
								   if( arrmParent[index].replace(/^\s+|\s+$/g,"") != "")
								     {		
									 		var arrm = arrmParent[index].split(':'); 
									 		var strval ="";
									 		for (  icombine =1  ; icombine < arrm.length ; icombine++)
									 		  {
									 		     strval += arrm[icombine];
									 		     if( icombine != arrm.length-1)
									 		      strval += ":";
									 			}
									 		if (index == 0) firstVal = strval;
									 		
											var objslectoptions = document.createElement("OPTION");
											var txtnode = document.createTextNode(arrm[0]);
											objslectoptions.setAttribute("value",strval);
											objslectoptions.appendChild(txtnode);
											objCollection.appendChild(objslectoptions);
																			
										}
								 }
							objCollection.selectedIndex =0 ;
							return firstVal;
    }
    
	function setModel(firstVal , ddlval)
	{
		var objModel = document.getElementById(ddlval);
							
							for(i=objModel.options.length-1;i>=0;i--)
								{
													objModel.remove(i);
								}
							
							var arrfirstval = firstVal.split(':');
							for (index =0 ; index < arrfirstval.length ; index++)
							 {
							
							    var arrindval = arrfirstval[index].split(',');
							    var val = arrindval[0].replace('\"',"").replace('\"',"");
							    var objslectoptions = document.createElement("OPTION");
								 var txtnode = document.createTextNode(val);
								 objslectoptions.setAttribute("value",arrindval[1]);
								 objslectoptions.appendChild(txtnode);
								 objModel.appendChild(objslectoptions);
							 }	
							 
							 objModel.selectedIndex=0;
	}
    
    function setfeatures(val, featureid, ddlname)
    {
    				
			    	var arrmParent1 = val.split('\n');
			    	
			    	var objfeatures = document.getElementById(ddlname);
					 for(i=objfeatures.options.length-1;i>=0;i--)
						{
								objfeatures.remove(i);
						}
			    
				    	var arrmParent = null 
				    	var isIdAvailable = 0;
				    	for(index1 =0 ; index1 < arrmParent1.length ; index1++)
				    	{
				    			arrmParent = arrmParent1[index1].split(':'); 
				    			if(arrmParent[0].replace(/^\s+|\s+$/g,"") == featureid )
				    			{
				    			  isIdAvailable =1 ;
				    			  break;
				    			 }
				    	}
				    	if( isIdAvailable == 0) 
				    	{
				    	 alert("No features available for the selected Model");
				    	  return;
				    	}
						var arrm = arrmParent[1].split(",");
									
									for (index =0 ; index < arrm.length ; index++)
									 {	
									   if( arrm[index].replace(/^\s+|\s+$/g,"") != "")
									     {		
										 		//var arrs = arrm[index].split(',');
										 		//alert(arrm[index]);
												var objslectoptions = document.createElement("OPTION");
												var txtnode = document.createTextNode(arrm[index].replace(/^\s+|\s+$/g,""));
												objslectoptions.setAttribute("value",arrm[index].replace(/^\s+|\s+$/g,""));
												objslectoptions.appendChild(txtnode);
												objfeatures.appendChild(objslectoptions);
																				
												}
									 }
									
					 
					 objfeatures.selectedIndex =0 ;

    }

	function removeAllMarkers()
	{
		
		while(arrmarker.length != 0)
		{
			val = arrmarker.pop();
			map.removeOverlay(val);
		}
		
	}
	
	function callback()
	{
		//alert("hi");
	}
	function showhide(val)
		{
			isLegendDrawn = 0;
			removeAllMarkers();
			removeAll();
			for (i=0;i<8;i++)
			{
				arrclusterOrg[i] = "";
				arrclusterTemp[i]="";
			}
		 
			if(val=="1") //Streaming 
				{
					__isStreaming = 1;
					//document.getElementById("divDBbtn").style.display ='none';
					document.getElementById("divDB").style.display = 'none';
					document.getElementById("divStream").style.display = 'block';
					 //startstopStream(1);
				}
			else
				{
					startstopStream(0);
				$('Errorval').innerHTML = "";
				$('val').innerHTML = "";
				$('val1').innerHTML = "";
						
					__isStreaming = 0;
					//document.getElementById("divDBbtn").style.display ='block';
					document.getElementById("divDB").style.display = 'block';
					document.getElementById("divStream").style.display = 'none';
					
				}
				return true;
		}
		
	function changedays(obj)
	{
			 var objdays = document.getElementById("days");
			 document.getElementById("time").disabled = true;
			 if (obj.selectedIndex == 0) 
			 	{
			 		alert("Please select a valid Entry");
			 		objdays.disabled = true;
			 		return;
			 	}
			
					 objdays.disabled = false;	 
					 
			 if( obj.selectedIndex >= 0)
			 {
			 //  document.getElementById("months").style.border= 'solid 1px black';
			   document.getElementById("months").className ='brdr';
			   document.getElementById("days").className ='brdrRed';
			  var selval = obj.options[obj.selectedIndex].value;
			  var objlocation = document.getElementById("location"); 
			  var sitevector = objlocation.options[objlocation.selectedIndex].value;
			  var URL ="";
			  if(__islocal == 0)
				{
			   URL = "http://angle.ncdm.uic.edu/servicesv3/getdates.html?mode=days&time="+selval+"&sitevector="+sitevector;
			   }
			   else
			    {
			      URL ="tst1.dat"
			     }
			  new Ajax.Request(URL,
				  		{
						    method:'get',
						    onSuccess: function(transport1){
							
								 for(i=objdays.options.length-1;i>=0;i--)
									{
										objdays.remove(i);
									}
								 
								
								var arrm = transport1.responseText.split('\n');
								
								//var objddl = document.getElementById("months");
								var objslectoptions = document.createElement("OPTION");
								var txtnode = document.createTextNode("");
								objslectoptions.setAttribute("value","");
								objslectoptions.appendChild(txtnode);
								objdays.appendChild(objslectoptions);
								for (index =0 ; index < arrm.length ; index++)
								 {		
								 					 		
								   if( arrm[index].replace(/^\s+|\s+$/g,"") != "")
								   {   		 		
								 		var arrs = arrm[index].split(',');
										var objslectoptions = document.createElement("OPTION");
										var txtnode = document.createTextNode(arrs[1]);
										objslectoptions.setAttribute("value",arrs[0]);
										objslectoptions.appendChild(txtnode);
										objdays.appendChild(objslectoptions);
										}
								 }
								
						    },
						    onFailure: function(){ alert("error getting days of the month");}
						  }
						  );
			 }
	}
	
	function changetime(obj)
	{
	 var objtime = document.getElementById("time");
	 if (obj.selectedIndex == 0) 
	 	{
	 		alert("Please select a valid Day");
	 		objtime.disabled = true;
	 		return;
	 	}
	
	 
	 
	 if( obj.selectedIndex > 0)
	 {
	     document.getElementById("days").className ='brdr';
	   document.getElementById("time").className ='brdrRed';
	 	 objtime.disabled = false;	
	  var selval = obj.options[obj.selectedIndex].value;
	  var objlocation = document.getElementById("location"); 
	  var sitevector = objlocation.options[objlocation.selectedIndex].value;
	 // http://angle.ncdm.uic.edu/servicesv3/getdates.html?mode=times&time=1190091600&sitevector=1
	 var URL ="";
	  if(__islocal == 0)
		{
	     URL = "http://angle.ncdm.uic.edu/servicesv3/getdates.html?mode=times&time="+selval+"&sitevector="+sitevector;
	     }
	    else
	     {
	       URL ="tst2.dat"
	      }
	  new Ajax.Request(URL,
		  		{
				    method:'get',
				    onSuccess: function(transport1){
					
						 for(i=objtime.options.length-1;i>=0;i--)
							{
								objtime.remove(i);
							}
						 
						
						var arrm = transport1.responseText.split('\n');
						
						//var objddl = document.getElementById("months");
						var objslectoptions = document.createElement("OPTION");
						var txtnode = document.createTextNode("");
						objslectoptions.setAttribute("value","");
						objslectoptions.appendChild(txtnode);
						objtime.appendChild(objslectoptions);
						for (index =0 ; index < arrm.length ; index++)
						 {		

						   if( arrm[index].replace(/^\s+|\s+$/g,"") != "")
						   {
						 		var arrs = arrm[index].split(',');
								var objslectoptions = document.createElement("OPTION");
								var txtnode = document.createTextNode(arrs[1]);
								objslectoptions.setAttribute("value",arrs[0]);
								objslectoptions.appendChild(txtnode);
								objtime.appendChild(objslectoptions);
								}
						 }
						
				    },
				    onFailure: function(){ alert("error getting time for the month");}
				  }
				  );
	 }
	}
	
	function datetimeset(obj)
	{
	  if(obj.selectedIndex > 0)
	    {
	     obj.className= 'brdr';
	    }
	}
	
	function locchange()
	{
	
	  document.getElementById("days").disabled = true;
	  document.getElementById("time").disabled = true;
	  document.getElementById("months").selectedIndex=0;
	  //document.getElementById("months").style.border= 'solid 2px red';
	  document.getElementById("months").className ='brdrRed';
	}
	
	function changecollection(obj)
	{
	  var val = obj.options[obj.selectedIndex].value;
	 
	  setModel(val, 'ddlModelDB');
  	  //setModel(val, 'ddlModel2');
	  setfeatures(__features,obj.options[obj.selectedIndex].text, 'sfeaturesDB')
	}
	
	function changecollectionStream(obj)
	{
		 var val = obj.options[obj.selectedIndex].value;
	 
	  setModel(val, 'ddlModelStream');
  	  //setModel(val, 'ddlModel2');
	  setfeatures(__features,obj.options[obj.selectedIndex].text, 'sfeaturesStream')
	}
	
	function updatedbstate()
	{
		var objtime = document.getElementById("time");
		if(document.getElementById("months").selectedIndex == 0) 
		{
		  alert("Select a valid Time");
		  return;
		 }
		if(document.getElementById("days").selectedIndex == 0) 
		{
		  alert("Select a valid Time");
		  return;
		 }
		if(objtime.selectedIndex == 0 )
		 {
		   alert("select a valid time");
		   return;
		 }
		var time =  objtime.options[objtime.selectedIndex].value;
		var objloc = document.getElementById("location");
		var location = objloc.options[objloc.selectedIndex].value;
		var objModel_1 = document.getElementById("ddlModelDB");
		var modelvalue_1 = objModel_1.options[objModel_1.selectedIndex].value;
		//var arrmodelvalue_1 = modelvalue_1.split(','); // "sample model 1",1182296400,1182297000,4
		/*
		var objModel_2 = document.getElementById("ddlModel2");
		var modelvalue_2 = objModel_2.options[objModel_2.selectedIndex].value;
		var arrmodelvalue_2 = modelvalue_2.split(','); // "sample model 1",1182296400,1182297000,4
		
		if(objModel_1.selectedIndex == objModel_2.selectedIndex)
		   {
		     alert("Select 2 different Models");
		     return;
		   }
		*/ 
		var objfeatures = document.getElementById("sfeaturesDB");
		var features  = objfeatures.options[objfeatures.selectedIndex].value;
		var objversion = document.getElementById("ddlCollection");
		var version = objversion.options[objversion.selectedIndex].text;
		var ithres = 0;
		var thres = document.getElementById("threshold").value;
		try
		{
		ithres = parseFloat(thres);
		}catch(e)
		{
		 ithres = 0.5
		 document.getElementById("threshold").value = 0.5;
		 alert("Invalid threshold value, hence reset to default value");
		}
		
		if ((ithres < 0.0) && (ithres >= 1.0))
		   {
		    alert("Invalid threshold value, hence reset to default value");
		     ithres = 0.5;
		     document.getElementById("threshold").value = 0.5;
		   }
		
		
		
		var Qstring = "t="+time+"&";
		Qstring += "l="+location+"&";
		/*
		Qstring += "mt1="+arrmodelvalue_1[2]+"&";
		Qstring += "ml1="+arrmodelvalue_1[3]+"&";
		Qstring += "pt1="+arrmodelvalue_1[1]+"&";
		
		Qstring += "mt2="+arrmodelvalue_2[2]+"&";
		Qstring += "ml2="+arrmodelvalue_2[3]+"&";
		Qstring += "pt2="+arrmodelvalue_2[1]+"&";
		*/
		Qstring += "mid="+modelvalue_1+"&";
		Qstring += "f="+features+"&";
		Qstring += "th="+ithres.toString()+"&";
		Qstring += "v="+version;
		
		//alert(Qstring);
	 // var URL = "http://angle.ncdm.uic.edu/servicesv3/webquery.html?t=1193859000&l=1&mt=1193859000&ml=1&pt=1193858400&pl=1&f=numberOfPackets&th=0.5&v=0";
	 $('val1').innerHTML = "Getting values please wait ...";
	 var URL="";
	 if( __islocal == 0 ) 
	  URL = "http://angle.ncdm.uic.edu/servicesv3/webquery.html?"+Qstring;
	 else
	  URL = "test_xml.xml";
	  new Ajax.Request(URL,
		  		{
				    method:'get',
				    onSuccess: function(transport1){
				    removeAllMarkers();
				    start(transport1.responseText);
				    $('val1').innerHTML = "";
				 },
				    onFailure: function(){ alert("Error in getting webquery xml");
				    	$('val1').innerHTML = "";
				    }
				  }
				  );
		 
	}
//var pe = new PeriodicalExecuter(getXMLAjaxCall, 4);











