/**
 * Javascript file for the FreeMaps directions pages.
 */
var doc = null; // XML Document var
var format = new OpenLayers.Format.XML();
var node = null;

var browserName = OpenLayers.Util.getBrowserName();


/**
 * Function to set up the Route Server request and sent it to the Proxy Servlet
 * 
 * @param {Object} startLat - Starting address latitude value
 * @param {Object} startLon - Starting address longitude value
 * @param {Object} endLat - Destination address latitude value
 * @param {Object} endLon - Destination address longitude value
 */
function getIPCoord(){

    var gml = "<?xml version='1.0' encoding='UTF-8'?>"+
            "<XLS version='1.1' xmlns='http://www.opengis.net/xls' xmlns:gml='http://www.opengis.net/gml' "+
                "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"+
                    "<RequestHeader clientName='OpenGIS-XLS-Demo' clientPassword='OpenGIS-XLS-Demo'/>"+
                    "<Request methodName=\"IP_LOCATING\" version=\"1.1\" requestID=\"Freemaps_IP_Locating\">"+
                        "<SLIR>"+
							"<InputGatewayParameters>"+
								"<InputMSIDs>"+
									"<InputMSInformation msIDType=\"IPV6\" msIDValue=\"" + ipAddress + "\" />"+
								"</InputMSIDs>"+
							"</InputGatewayParameters>"+
                        "</SLIR>"+
                    "</Request>"+
                "</XLS>";

    /* 
     * OpenLayers way, but doesn't work until we can set 
     * the content-type to 'application/x-www-form-urlencoded'
     */
    data = "xls=" + gml;
    request = new OpenLayers.Ajax.Request("proxyServlet", 
                     {   method: 'post', 
                         contentType : 'application/x-www-form-urlencoded',
                         postBody: data,
                         onComplete: loadIpLocating, 
                         onFailure: failureIpLocating
                      }
                     );
}


/**
 * Function called when IP location fails.
 * 
 * Does nothing.
 * 
 * @return
 */
function failureIpLocating() { 
	//do nothing
}

 
/**
 * Function to get a cross-browser version of getElementsByTagName using namespaces
 * @param node
 * @param name
 * @param ns
 */
function customGetElementsByTagName (node, name, ns) {

    var elems;
    if (browserName == "msie") {
        elems =  node.getElementsByTagName(ns + ":" + name);
    } else {
        // Firefox3 is strict about full namespace name and not the abbrevation!
        // We assume here that all namespaces start with "http://www.opengis.net/
        elems = node.getElementsByTagNameNS("http://www.opengis.net/" + ns, name);
    }
    return elems;
}

 
/**
 * Function to complete the map and format the Route Server response
 * @param {string} doc - Route directions
 */
function loadIpLocating(response) {

    var doc = response.responseXML || response.responseText;

    //if doc is not a DOM element, turn it into one
    if(!doc.documentElement) {
        doc = format.read(doc);
    } 
    try {
		node = customGetElementsByTagName(doc, 'pos', 'gml')[0];
		if(node)
		{
			coords = node.firstChild.nodeValue.split(' ');
			point= new OpenLayers.LonLat(coords[1], coords[0]).transform(proj_4326, map.getProjectionObject());
	
			map.setCenter(point);
			map.zoomTo(10);
		}
    
    } catch (e) {
		//do nothing if theres an error
    }

    return true;
}

