var iTotalRows = 0;
var stringArray = new Array();

function createXML(createXMLFor, XMLType, strQueryString) {
	//GLog.write("in createXML");
	var url = "./xml/createXML.aspx?createXMLFor="+createXMLFor;

    checkBounds();
    
  	var bounds = googleMap.getBounds();
	var zoom = googleMap.getBoundsZoomLevel(bounds);
		
	//send to createXML.aspx with the appropriate parameters to get the data
  	//then send the data to the appropriate function to parse and display
  	
  	var xmlMinX = minx;
  	var xmlMaxX = maxx;
  	var xmlMaxY = maxy;
  	var xmlMinY = miny; 
  	var modeType = "";
  		
  	if (XMLType == "trafficmarker") {
  		//for markers we pass in the checkboxes that are checked so we only grab data for layers that
  		//are active - will consist of incident, construction, and special event
  		modeType = "traffic";
  		url = url + "&" + strQueryString + "&modeType="+modeType;
  		url = url + "&minX="+xmlMinX+"&maxX="+xmlMaxX+"&minY="+xmlMinY+"&maxY="+xmlMaxY;
  	}
  	//*************************************************************** SK 1/06/2009
  	else if (XMLType == "trafficPmarker") {
  		//for markers we pass in the checkboxes that are checked so we only grab data for layers that
  		//are active - will consist of incident, construction, and special event
  		modeType = "trafficP";
  		url = url + "&" + strQueryString + "&modeType="+modeType;
  		url = url + "&minX="+xmlMinX+"&maxX="+xmlMaxX+"&minY="+xmlMinY+"&maxY="+xmlMaxY;
  	}
  	//****************************************************************************
  	else if (XMLType == "weather") {
//  	  url = "./xml/forecast.xml";
          url = "./xml/GetRegionLatLng.aspx";
  	}
	else {
	  url = url + "&minX="+xmlMinX+"&maxX="+xmlMaxX+"&minY="+xmlMinY+"&maxY="+xmlMaxY;
	}
	var request = GXmlHttp.create();
	      
    request.open("GET", url, true);
	
	request.onreadystatechange = function() {   
  		if (request.readyState == 4) {
  			
  			//alert(request.responseText);

			var xmlDoc = null;
			var elem = null;
			xmlDoc = request.responseXML;
			
			//alert(request.responseText);

			if (xmlDoc != null) {
				
			//GLog.write("XMLType: " + XMLType);
			
			//* SK 1/06/2009
			//if (XMLType == 'trafficmarker') {
			if (XMLType == 'trafficmarker'|| XMLType == 'trafficPmarker') {
				//var elem = xmlDoc.documentElement.getElementsByTagName("event");
				//var errElem = xmlDoc.documentElement.getElementsByTagName("error");
				var elem = xmlDoc.documentElement.getElementsByTagName("event");
				var errElem = xmlDoc.documentElement.getElementsByTagName("error");

                if (errElem.length > 0) {
                  alert("There was an error processing your request. Please try again.");
                  hidePleaseWaitScreen();
                }
                else {
				  if (elem.length > 0) {
				  //call function to plot markers
					var eventsExist = true; 
				 	createPopups(elem, xmlDoc, modeType);
				  }
				  else {
					//initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
                    //initializeEventMarkers(eventMarkers);
					
					//functions are in javascript/showHidePleaseWaitScreen.js
					hidePleaseWaitScreen();
					eventsExist = false;
				  }
				}
				//window.frames.showEvents.writeResults();
			}//end if XMLType == marker
			else if (XMLType == 'cctvs'){
			    //var elem = xmlDoc.documentElement.getElementsByTagName("cctv");
			    var elem = xmlDoc.documentElement.getElementsByTagName("cctv");
				if (elem.length > 0) {
				    //call function to plot markers
					cctvsExist = true;
					createCCTVPopups(elem, xmlDoc);
				}
				else {
					//initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
					initializeEventMarkers(cctvMarkers);
				    hidePleaseWaitScreen();
					cctvsExist = false;
				}
			}
			else if (XMLType == 'clusteredCCTVs'){
			    //var elem = xmlDoc.documentElement.getElementsByTagName("cctv");
			    var elem = xmlDoc.documentElement.getElementsByTagName("cctv");
				if (elem.length > 0) {
				    //call function to plot markers
					createClusteredCCTVMarkers(elem, xmlDoc);
				}
				else {
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'clusteredWinters'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("winterCluster");
				if (elem.length > 0) {
				    //call function to plot markers
					createClusteredWinterMarkers(elem, xmlDoc);
				}
				else {
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'clusteredSpeeds'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("speedCluster");
				if (elem.length > 0) {
				    //call function to plot markers
					createClusteredSpeedMarkers(elem, xmlDoc);
				}
				else {
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'crossings'){
				var elem = xmlDoc.documentElement.getElementsByTagName("crossing");
				if (elem.length > 0) {
				    //call function to plot markers
					crossingsExist = true;
					createCrossingPopups(elem, xmlDoc);
				}
				else {
				    //initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
				    initializeEventMarkers(crossingMarkers);
					crossingsExist = false;
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'VMS'){
				var elem = xmlDoc.documentElement.getElementsByTagName("VMS");
				if (elem.length > 0) {
				    //call function to plot markers
					vmsExist = true;
					createVMSPopups(elem, xmlDoc);
				}
				else {
					initializeEventMarkers(vmsMarkers);
					vmsExist = false;
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'weather'){
			  if (!weatherLoaded) {
			    var elem = xmlDoc.documentElement.getElementsByTagName("Region");
				if (elem.length > 0) {
				    //call function to plot markers
					weatherExist = true;
					createWeatherPopups(elem, xmlDoc);
				}
				else {
					initializeEventMarkers(weatherMarkers);
					weatherExist = false;
					hidePleaseWaitScreen();
				}
		      }
		      else {
		        //already loaded weather data, just set display true
		        if (weatherMarkers.length > 0) {
                  for (var i = weatherMarkers.length-1; i >=0; i--) { 
		            weatherMarkers[i].display(true); 
	              }
                }
                hidePleaseWaitScreen();
		      }
			}
			else if (XMLType == 'detectorStations'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("detectorStation");
				if (elem.length > 0) {
				    //call function to plot markers
					detectorsExist = true;
					createDetectorPopups(elem, xmlDoc);
				}
				else {
				    //initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
				    initializeEventMarkers(detectorMarkers);
					detectorsExist = false;
					hidePleaseWaitScreen();
				}
			}
			else {
				var elem = xmlDoc.documentElement.getElementsByTagName("link");
				//call function to parse XML and display on map
				if (elem.length > 0) {
				    getLinkData(elem, xmlDoc);
				    linkDataExists = true;
				}
				else {
				    drawingLinks = false;
				    hidePleaseWaitScreen();
				    linkDataExists = false;
				}
				//updateNoOfPoints(iTotalRows);
			}//end else - type is links
  		}//end if xmlDoc != null
    }//end if request.readyState = 4
	}//end request.onreadystatechange = function()
	
	request.send(null);

}//end function createXML


function createXMLforPlanned(createXMLFor, XMLType, strQueryString) {
	//GLog.write("in createXML");
 
	var url = "./xml/createXML.aspx?createXMLFor="+createXMLFor;

    checkBounds();
    
  	var bounds = googleMap.getBounds();
	var zoom = googleMap.getBoundsZoomLevel(bounds);
		
	//send to createXML.aspx with the appropriate parameters to get the data
  	//then send the data to the appropriate function to parse and display
  	
  	var xmlMinX = minx;
  	var xmlMaxX = maxx;
  	var xmlMaxY = maxy;
  	var xmlMinY = miny; 
  	var modeType = "";
  		
  	if (XMLType == "trafficPmarker") {
  		//for markers we pass in the checkboxes that are checked so we only grab data for layers that
  		//are active - will consist of incident, construction, and special event
  		modeType = "trafficP";
  		url = url + "&" + strQueryString + "&modeType="+modeType;
  		url = url + "&minX="+xmlMinX+"&maxX="+xmlMaxX+"&minY="+xmlMinY+"&maxY="+xmlMaxY;
  	}
  	else if (XMLType == "weather") {
  	  url = "./xml/forecast.xml";
  	}
	else {
	  url = url + "&minX="+xmlMinX+"&maxX="+xmlMaxX+"&minY="+xmlMinY+"&maxY="+xmlMaxY;
	}

	var request = GXmlHttp.create();

    request.open("GET", url, true);
	
	request.onreadystatechange = function() {   
  		if (request.readyState == 4) {
  			
  			//alert(request.responseText);
  				
			var xmlDoc = null;
			var elem = null;
			xmlDoc = request.responseXML;
			
			//alert(request.responseText);

			if (xmlDoc != null) {
				
			//GLog.write("XMLType: " + XMLType);
			if (XMLType == 'trafficPmarker' ) {
				var elem = xmlDoc.documentElement.getElementsByTagName("event");
				var errElem = xmlDoc.documentElement.getElementsByTagName("error");

                if (errElem.length > 0) {
                  alert("There was an error processing your request. Please try again.");
                  hidePleaseWaitScreen();
                }
                else {
				  if (elem.length > 0) {
				  //call function to plot markers
					var eventsExist = true; 
				 	createPopups(elem, xmlDoc, modeType);
				  }
				  else {
					//initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
                    //initializeEventMarkers(eventMarkers);
					
					//functions are in javascript/showHidePleaseWaitScreen.js
					hidePleaseWaitScreen();
					eventsExist = false;
				  }
				}
				//window.frames.showEvents.writeResults();
			}//end if XMLType == marker
			else if (XMLType == 'cctvs'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("cctv");
				if (elem.length > 0) {
				    //call function to plot markers
					cctvsExist = true;
					createCCTVPopups(elem, xmlDoc);
				}
				else {
					//initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
					initializeEventMarkers(cctvMarkers);
				    hidePleaseWaitScreen();
					cctvsExist = false;
				}
			}
			else if (XMLType == 'clusteredCCTVs'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("cctv");
				if (elem.length > 0) {
				    //call function to plot markers
					createClusteredCCTVMarkers(elem, xmlDoc);
				}
				else {
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'clusteredWinters'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("winterCluster");
				if (elem.length > 0) {
				    //call function to plot markers
					createClusteredWinterMarkers(elem, xmlDoc);
				}
				else {
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'clusteredSpeeds'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("speedCluster");
				if (elem.length > 0) {
				    //call function to plot markers
					createClusteredSpeedMarkers(elem, xmlDoc);
				}
				else {
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'crossings'){
				var elem = xmlDoc.documentElement.getElementsByTagName("crossing");
				if (elem.length > 0) {
				    //call function to plot markers
					crossingsExist = true;
					createCrossingPopups(elem, xmlDoc);
				}
				else {
				    //initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
				    initializeEventMarkers(crossingMarkers);
					crossingsExist = false;
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'VMS'){
				var elem = xmlDoc.documentElement.getElementsByTagName("VMS");
				if (elem.length > 0) {
				    //call function to plot markers
					vmsExist = true;
					createVMSPopups(elem, xmlDoc);
				}
				else {
					initializeEventMarkers(vmsMarkers);
					vmsExist = false;
					hidePleaseWaitScreen();
				}
			}
			else if (XMLType == 'weather'){
			  if (!weatherLoaded) {
			    var elem = xmlDoc.documentElement.getElementsByTagName("Region");
				if (elem.length > 0) {
				    //call function to plot markers
					weatherExist = true;
					createWeatherPopups(elem, xmlDoc);
				}
				else {
					initializeEventMarkers(weatherMarkers);
					weatherExist = false;
					hidePleaseWaitScreen();
				}
		      }
		      else {
		        //already loaded weather data, just set display true
		        if (weatherMarkers.length > 0) {
                  for (var i = weatherMarkers.length-1; i >=0; i--) { 
		            weatherMarkers[i].display(true); 
	              }
                }
                hidePleaseWaitScreen();
		      }
			}
			else if (XMLType == 'detectorStations'){
			    var elem = xmlDoc.documentElement.getElementsByTagName("detectorStation");
				if (elem.length > 0) {
				    //call function to plot markers
					detectorsExist = true;
					createDetectorPopups(elem, xmlDoc);
				}
				else {
				    //initialize any existing markers to display false - send type to display false
					//this is if there are no new markers to display and existing are off the screen, we need
					//to clear the existing from the map when hidden from view
				    initializeEventMarkers(detectorMarkers);
					detectorsExist = false;
					hidePleaseWaitScreen();
				}
			}
			else {
				var elem = xmlDoc.documentElement.getElementsByTagName("link");
				//call function to parse XML and display on map
				if (elem.length > 0) {
				    getLinkData(elem, xmlDoc);
				    linkDataExists = true;
				}
				else {
				    drawingLinks = false;
				    hidePleaseWaitScreen();
				    linkDataExists = false;
				}
				//updateNoOfPoints(iTotalRows);
			}//end else - type is links
  		}//end if xmlDoc != null
    }//end if request.readyState = 4
	}//end request.onreadystatechange = function()
	
	request.send(null);

}//end function createXML
