
var xmlHttp; 
var requestPage = "RefConverter.aspx";
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 	

function GetFromLatLong(lat, lng, isRoot)
{
	var latitude = document.getElementById(lat).value;
	var longitude = document.getElementById(lng).value;
	
	GetFromLatLong2(latitude, longitude, isRoot);
}

function GetFromLatLong2(latitude, longitude, isRoot)
{
	var latLng = new LatLng(latitude, longitude);
	var osRef = latLng.toOSRef();
	var easting = osRef.easting.toString().substring(0,6);
	var northing = osRef.northing.toString().substring(0,6);
	
	if(easting.length > 0 && northing.length > 0)
	{ 
		var osgridref = easting + " " + northing;
		var valToAppend = "osgridref=" + osgridref;
		show_data(valToAppend, isRoot);
	}
}
			
function GetFromOSGridRef(OSRefA, OSRefB, isRoot)
{
	var firstPart = document.getElementById(OSRefA).value;
	var secondPart = document.getElementById(OSRefB).value;
	
	if(firstPart.length > 0 && secondPart.length > 0)
	{ 
		var osgridref = firstPart + " " + secondPart;
		var valToAppend = "osgridref=" + osgridref;
		show_data(valToAppend, isRoot);
	}
}

function GetFromLandRangerGridRef(LRRefA, LRRefB, LRRefC, isRoot)
{
	var firstPart = document.getElementById(LRRefA).value;
	var secondPart = document.getElementById(LRRefB).value;
	var thirdPart = document.getElementById(LRRefC).value;
	
	if(firstPart.length > 0 && secondPart.length > 0 && thirdPart.length > 0)
	{ 
		var landRangerRef = firstPart + " " + secondPart + " " + thirdPart;
		var valToAppend = "landrangerref=" + landRangerRef;
		show_data(valToAppend, isRoot);
	}
}
		
function show_data(valToAppend, isRoot)
{ 
	if (valToAppend.length > 0)
	{ 
		//Build the url to query
		var url = ((isRoot) ? "" : "../") + requestPage + "?" + valToAppend;

        //Create the xmlHttp object to use in the request 
        xmlHttp = GetXmlHttpObject1(stateChangeHandler); 
             
        //Send the xmlHttp get to the specified url 
		xmlHttp_Get1(xmlHttp, url); 
	} 
}

function stateChangeHandler() 
{ 
	//readyState of 4 or 'complete' represents that data has been returned 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{ 
		//Gather the results from the callback 
        var str = xmlHttp.responseText;
		Populate(str);
	} 
} 

// XMLHttp send GET request 
function xmlHttp_Get1(xmlhttp, url) 
{ 
	xmlhttp.open('GET', url, true); 
    xmlhttp.send(null); 
} 

function GetXmlHttpObject1(handler) 
{ 
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
	if (is_ie)
	{ 
		//The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
             
        //Attempt to create the object 
        try
        { 
			objXmlHttp = new ActiveXObject(strObjName); 
            objXmlHttp.onreadystatechange = handler; 
		} 
		catch(e)
		{ 
            //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
         } 
	} 
    else if (is_opera)
    { 
		//Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
	} 
    else
    { 
		// Mozilla | Netscape | Safari 
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
	} 
         
	//Return the instantiated object 
	return objXmlHttp; 
} 
