// js functions for parsing xml and creating map calls
//onerror=handleErr;

 
function ChangeTabs(visibleDIV,invisibleDiv)
{
	var x;	  
	x = new getObj(visibleDIV);
	x.style.display = 'block';
	x = new getObj(invisibleDiv);
	x.style.display = 'none';
}
 

function getObj(name)
{
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
}


// dataStr input in form of: cityLongitude,cityLatitude _BusinessName,BusinessLongitude,BusinessLatitude _BusinessName,...
function BuildMap(dataStr, containerName)
{
	var output = "";
	dataStr = dataStr.replace("'", "\\'");

	// seperate city portion of string from businesses portion of string (seperated by first _ sign)
    var seperator = dataStr.indexOf("_");
    var cityStr = dataStr.substring(0, seperator);
    var busStr = dataStr.substring(seperator, dataStr.length);
    
    // extract cityLongitude and Latitude (in cityStr seperated by ,)
    seperator = cityStr.indexOf(",");
    var cityLong = parseFloat(cityStr.substring(0, seperator));
    var cityLat = parseFloat(cityStr.substring(seperator + 1, cityStr.length));
	
	// seperate businesses
	var business = new Array();
	var start = 0;
	var end = 0;
	var more = true;
	var i = 0;
	
	while (more)
	{
	    start = end + 1;
	    end = busStr.indexOf("_", start);
	    if (end == -1) 
	    {
	        // no more '_' this is the last record
	        end = busStr.length;
	        more = false
	    }
	    business[i] = busStr.substring(start, end);
	    i++;
	}
	
	var names = new Array();
    var lats = new Array();
    var longs = new Array();
    	
	// parse business array to extract business name & co-ords
	for (i = 0; i < business.length; i++) 
	{
	    var seperator1 = business[i].indexOf(",");
	    var seperator2 = business[i].indexOf(",", seperator1 + 1);
	    names[i] = business[i].substring(0, seperator1);
	    longs[i] = parseFloat(business[i].substring(seperator1 + 1, seperator2));
	    lats[i] = parseFloat(business[i].substring(seperator2 + 1, business[i].length));
	}
	
	
	// build javascript to create map and add markers from above gathered info
	output += "<script type=\'text/javascript\'>\n";
	    //output += "var mapPoint = new YGeoPoint(" + cityLat + ", " + cityLong + ");\n";
	    output += "var map = new YMap(document.getElementById(\"" + containerName + "\"));\n";
	    //output += "map.drawZoomAndCenter(mapPoint);\n";   // 3 might need to change also
	    
	    // create markers
	    output += "var markerPoints = new Array();\n";
	    output += "var marker = new Array();\n";
	    // iterate through the arrays holding the name and co-ords of the businesses
	    for (j = 0; j < names.length; j++)
	    {
	        output += "markerPoints[" + j + "] = new YGeoPoint(" + lats[j] + ", " + longs[j] + ");\n";
	        output += "marker[" + j + "] = new YMarker(markerPoints[" + j + "]);\n";
	        output += "marker[" + j + "].addAutoExpand(\'" + names[j] + "\');\n";
	        output += "map.addOverlay(marker[" + j + "]);\n";
	    }

        output += "var view = map.getBestZoomAndCenter(markerPoints);\n";
	    output += "map.drawZoomAndCenter(view.YGeoPoint, view.zoomLevel + 1);\n";   
	    
	    // add some extra controls
	    output += "map.addTypeControl();\n";
	    output += "map.setMapType(YAHOO_MAP_REG);\n";
	    output += "map.addPanControl();\n";
	    output += "map.addZoomLong();\n";
	
	// closing script tag
	output += "<\/script>\n"
	
	// return to be added to display page
    return output;
}


function ParseXMLtoMap(xmlDataStr, containerName)
{


  	var browser=navigator.appName;
        var b_version=navigator.appVersion;
        var version=parseFloat(b_version);
            
            
        if (browser == "Microsoft Internet Explorer") 
        {
		return ieParseXMLtoMap(xmlDataStr, containerName)
	} else {
		return firefoxParseXMLtoMap(xmlDataStr, containerName)
	}

}

function ieParseXMLtoMap(xmlDataStr, containerName)
{
	var output = "";
	
	// different versions of MS XML DOM
	var domVersions = [ "Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.5.0",
	                    "Msxml2.DOMDocument", "Microsoft.XmlDom" ] ;
	var xmldoc;
	
	for (i = 0; i < domVersions.length; i++)
	{
	    try 
	    {
	        xmldoc = new ActiveXObject(domVersions[i]);
	        break; // break out of loop on first sucessful creation of dom object
	    }
	    catch (oError) 
	    {
	        // this version didn't work, try next
	    }
	}
	
	xmldoc.loadXML(xmlDataStr);  // xmldoc.load(xmlDataStr); is for loading from a file
	xmldoc.async = false; // tells browser to wait for file to load before preceding
	if (xmldoc.parseError.errorCode != 0) 
	{
        alert("DOM Not Loaded: " + xmldoc.parseError.reason)
    }
	xmldoc.setProperty("SelectionLanguage", "XPath");

	// need to pull out the first co-ords for centering the map
	var cityLat = xmldoc.selectSingleNode("./SearchResults/SearchQuery/GeoLocation/Latitude").text;
	var cityLong = xmldoc.selectSingleNode("./SearchResults/SearchQuery/GeoLocation/Longitude").text;
	
	// then need to pull out co-ord and name of each business
	var busNodes = xmldoc.selectNodes("/SearchResults/BusinessCollection/Business");
	var names = new Array();
	var lats = new Array();
	var longs = new Array();
	
	for (i = 0; i < busNodes.length; i++)
	{
	    // pull out name and co-ords
	    names[i] = busNodes[i].selectSingleNode("./Name").text;
	    lats[i] = busNodes[i].selectSingleNode("./GeoLocation/Latitude").text;
	    longs[i] = busNodes[i].selectSingleNode("./GeoLocation/Longitude").text;
	}
	
	// build javascript to create map and add markers from above gathered info
	output += "<script type=\'text/javascript\'>\n";
	    output += "var mapPoint = new YGeoPoint(" + cityLat + ", " + cityLong + ");\n";
	    // should mapContainer be a param instead of hard coded like this?  it is required part of main page
	    output += "var map = new YMap(document.getElementById(\"" + containerName + "\"));\n";
	    output += "map.drawZoomAndCenter(mapPoint);\n";   // 3 might need to change also
	    
	    // create markers
	    output += "var markerPoints = new Array();\n";
	    output += "var marker = new Array();\n";
	    // iterate through the arrays holding the name and co-ords of the businesses
	    for (j = 0; j < names.length; j++)
	    {
	        output += "markerPoints[" + j + "] = new YGeoPoint(" + lats[j] + ", " + longs[j] + ");\n";
	        output += "marker[" + j + "] = new YMarker(markerPoints[" + j + "]);\n";
	        output += "marker[" + j + "].addAutoExpand(\'" + names[j] + "\');\n";
	        output += "map.addOverlay(marker[" + j + "]);\n";
	    }

        output += "var view = map.getBestZoomAndCenter(markerPoints);";
	    output += "map.drawZoomAndCenter(view.YGeoPoint, view.zoomLevel);\n";   // 3 might need to change also
	    
	    // add some extra controls
	    output += "map.addTypeControl();\n";
	    output += "map.setMapType(YAHOO_MAP_REG);\n";
	    output += "map.addPanControl();\n";
	    output += "map.addZoomLong();\n";
	
	// closing script tag
	output += "<\/script>"
	
	// return to be added to display page
	return output
}



function firefoxParseXMLtoMap(xmlDataStr, containerName)
{
	var output = "";
	
	var oParser = new DOMParser();
	var xmldoc = oParser.parseFromString(xmlDataStr, "text/xml");
	
//document.write("after parse text");

	// need to pull out the first co-ords for centering the map
	//var cityLat = xmldoc.selectSingleNode("./SearchResults/SearchQuery/GeoLocation/Latitude").nodeValue;
	//var cityLong = xmldoc.selectSingleNode("./SearchResults/SearchQuery/GeoLocation/Longitude").nodeValue;
	
	var cityLat = xmldoc.evaluate("./SearchResults/SearchQuery/GeoLocation/Latitude", xmldoc, null, XPathResult.NUMBER_TYPE, null).numberValue;
	var cityLong = xmldoc.evaluate("./SearchResults/SearchQuery/GeoLocation/Longitude", xmldoc, null, XPathResult.NUMBER_TYPE, null).numberValue;
	                            
//document.write("cityLat=" + cityLat + " cityLong=" + cityLong);
	
	// then need to pull out co-ord and name of each business
	var busNodes = xmldoc.evaluate("./SearchResults/BusinessCollection/Business", xmldoc, null, XPathResult.ANY_TYPE, null); 
	var names = new Array();
	var lats = new Array();
	var longs = new Array();
	
    var i = 0;
    var bNode = busNodes.iterateNext();
    while (bNode) 
    {
        names[i] = xmldoc.evaluate("./Name", bNode, null, XPathResult.STRING_TYPE, null).stringValue;
        lats[i] = xmldoc.evaluate("./GeoLocation/Latitude", bNode, null, XPathResult.NUMBER_TYPE, null).numberValue;
        longs[i] = xmldoc.evaluate("./GeoLocation/Longitude", bNode, null, XPathResult.NUMBER_TYPE, null).numberValue;
        //document.write(bNode.textContent);
//document.write(" i=" + i + " name=" + names[i]);
//document.writeln("lat=" + lats[i] + " long=" + longs[i]);
        bNode = busNodes.iterateNext();
        i++;
    }


	// build javascript to create map and add markers from above gathered info
	output += "<script type=\'text/javascript\'>\n";
	    output += "var mapPoint = new YGeoPoint(" + cityLat + ", " + cityLong + ");\n";
	    // should mapContainer be a param instead of hard coded like this?  it is required part of main page
	    output += "var map = new YMap(document.getElementById(\"" + containerName + "\"));\n";
	    output += "map.drawZoomAndCenter(mapPoint);\n";   // 3 might need to change also
	    
	    // create markers
	    output += "var markerPoints = new Array();\n";
	    output += "var marker = new Array();\n";
	    // iterate through the arrays holding the name and co-ords of the businesses
	    for (j = 0; j < names.length; j++)
	    {
	        output += "markerPoints[" + j + "] = new YGeoPoint(" + lats[j] + ", " + longs[j] + ");\n";
	        output += "marker[" + j + "] = new YMarker(markerPoints[" + j + "]);\n";
	        output += "marker[" + j + "].addAutoExpand(\'" + names[j] + "\');\n";
	        output += "map.addOverlay(marker[" + j + "]);\n";
	    }

        output += "var view = map.getBestZoomAndCenter(markerPoints);";
	    output += "map.drawZoomAndCenter(view.YGeoPoint, view.zoomLevel);\n";   // 3 might need to change also
	    
	    // add some extra controls
	    output += "map.addTypeControl();\n";
	    output += "map.setMapType(YAHOO_MAP_REG);\n";
	    output += "map.addPanControl();\n";
	    output += "map.addZoomLong();\n";
	
	// closing script tag
	output += "<\/script>"
	
	// return to be added to display page
	return output
}