// global flag
var isIE = false;

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
	try {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);    	    	   	
	}
     catch(exr) {
       alert("Erro"+exr);
     };
 
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
    //    	alert("Content"+req.responseText);
        	
        				var resp = req.responseText;
									html = getHtml(resp);
									buildStations(resp);
									unloadL();	
									map2.openInfoWindowHtml(markerpoint,html, new GSize(0,0));								
                                    var mm = new GMarkerManager(map2);	 						  
                                    var point_marker = new GMarker(markerpoint, iconr);
                                    mm.addMarker(point_marker,0,17); 	   
                                    hasClicked=0;

         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}



	var schtype = document.schedule.schtype;
	
	var point_marker;
	var heads = document.schedule.heads;
	var gallons = document.schedule.gallons;
	var datei = document.schedule.datei;
	var datef = document.schedule.datef;
	var raina = document.schedule.raina;
	var stress = document.schedule.stress;
	
    var loading = new Image( 500, 500 );
	var loadingLayer = document.getElementById("ilayer");	
	loading.src="/gmapkast/imgs/loading.gif";
			
	var hasClicked = 0
	var html;
    var ihtml = new Array;
	
	
	var iconb = new GIcon();		   
    iconb.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
    iconb.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    iconb.iconSize = new GSize(12, 20);
    iconb.shadowSize = new GSize(22, 20);
    iconb.iconAnchor = new GLatLng(6, 20);
    iconb.infoWindowAnchor = new GLatLng(5, 1);
		   
	var icony = new GIcon();		   
    icony.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
    icony.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icony.iconSize = new GSize(12, 20);
    icony.shadowSize = new GSize(22, 20);
    icony.iconAnchor = new GLatLng(6, 20);
    icony.infoWindowAnchor = new GLatLng(5, 1);
			   
	var iconr = new GIcon();		   
    iconr.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    iconr.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    iconr.iconSize = new GSize(12, 20);
    iconr.shadowSize = new GSize(22, 20);
    iconr.iconAnchor = new GLatLng(6, 20);
    iconr.infoWindowAnchor = new GLatLng(5, 1);
				   	
    var markerpoint = map2.getCenter();


    if (imarker){
       var mm = new GMarkerManager(map2);	
       var marker = new GMarker(markerpoint);
       mm.addMarker(marker,0,17); 	    	   
	}
  
	//String.prototype.trim = function(){
	//     var reSpaces=/^\s+(.*?)\s+$/;
	//	 return this.replace(reSpaces, "$1");
	//}
		
    function unloadL(){
	    try {
	      loadingLayer.src="/gmapkast/imgs/nibtr.gif";
		} catch (exception){
		  alert("Error unloadL: "+exception.message);
		};
		 
    }			

    function loadL(){
	    try {
	      loadingLayer.src=loading.src;
		} catch (exception){
		  alert("Error loadL: "+exception.message);
		};
		 
    }
			
    GEvent.addListener(map2, 'click', popinfo);
	
	function popinfo(overlay, point) {

	 if (datei.value < "20090301" ){
	     alert("Date must be Between: 2009-03-01 to 2010-09-30");
		 return;
	 } 

	 if (datef.value > "20100930") {
	     alert("Date must be Between: 2009-03-01 to 2010-09-30");
		 return;
	  }


      if (overlay) {
				map2.removeOverlay(point);
	   }
	  else {
	    if (hasClicked==0){
		   hasClicked=1;
		} else {
		   return;
		}


	    try {		
			try{
				map2.removeOverlay(point);
			} catch (exception){};
											
			loadL();
			if (overlay) {
			    markerpoint = overlay.point;
			} else if (point) {  							
				markerpoint = point;
			}

		//	alert("Lat:"+markerpoint.x+"  Lon:"+markerpoint.y);

			//var requestA = AjaxRequest();
	
		
			
			var respA = "";
		/*	AJAX Test Marcel.
			AjaxRequest.get(
                       {
                         'url':'pop.jsp?latitude='+markerpoint.y+'&longitude='+markerpoint.x+'&datei='+datei.value+'&datef='+datef.value+'&schtype='+schtype.options[schtype.selectedIndex].value+'&sizex=380&sizey=280&raina='+raina.value+'&stress='+stress.options[stress.selectedIndex].value ,
                         'onSuccess':function(req){ respA = req.responseText; },
                         'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
                        }
            );
			
			alert("Ajax Resp"+respA);
		*/	
	//		var request = GXmlHttp.create();
			
			
			
			//alert("GXmlHttp"+typeof(request));
			//document.write("pop.jsp?latitude="+markerpoint.y+"&longitude="+markerpoint.x+"&datei="+datei.value+"&datef="+datef.value+"&schtype="+schtype.options[schtype.selectedIndex].value+"&sizex=500&sizey=500");
			//document.write("pop.jsp?latitude="+markerpoint.y+"&longitude="+markerpoint.x+"&datei="+datei.value+"&datef="+datef.value+"&schtype="+schtype.options[schtype.selectedIndex].value+"&sizex=380&sizey=280&raina="+raina.value+"&stress="+stress.options[stress.selectedIndex].value);
//			request.open("GET", "pop.jsp?latitude="+markerpoint.y+"&longitude="+markerpoint.x+"&datei="+datei.value+"&datef="+datef.value+"&schtype="+schtype.options[schtype.selectedIndex].value+"&sizex=380&sizey=280&raina="+raina.value+"&stress="+stress.options[stress.selectedIndex].value, true);



           var request = loadXMLDoc("pop.jsp?latitude="+markerpoint.y+"&longitude="+markerpoint.x+"&datei="+datei.value+"&datef="+datef.value+"&schtype="+schtype.options[schtype.selectedIndex].value+"&sizex=380&sizey=280&raina="+raina.value+"&stress="+stress.options[stress.selectedIndex].value);
           
      		

						
		} catch (exception){
		    alert("Error addListener: "+exception.message);
		};
		
		};

    }
	
	function getHtml(resp, markerpoint){
	    return resp.substring(0,resp.indexOf("||||"));
	}
	
	function buildStations(resp){
	
		map2.clearOverlays();	

	    var idx = resp.indexOf("||||");
		if (resp.length<=(idx+6)) {
		  return "";
		}
		

	    var stpoint = resp.substring(idx+6).split("|");
    //    alert("building stations: "+stpoint);
		if (stpoint==null || stpoint.length==0){
		
		   
		} else {
			for (var b=0; b <  stpoint.length ; b++){
			   var stcoord = stpoint[b].split("/");
			   if (stcoord==null || stcoord.length<2){
				  return "";
			   }
			   
			   var st_point =  new GLatLng(stcoord[0], stcoord[1]);
//	           alert("building stations: "+stpoint);
			   
               var ms = new GMarkerManager(map2);						   
			   if (b==0){
				   var st_marker = new GMarker(st_point,icony);
//				   map2.addOverlay(st_marker);
                   ms.addMarker(st_marker,0,17); 	  			   			   			   
			   } else {
				   var st_marker = new GMarker(st_point,iconb);
//				   map2.addOverlay(st_marker);
                   ms.addMarker(st_marker,0,17); 	   				   
			   }
			}
		}
		
	}	
	
	


