﻿// JScript File

// CallWebServiceMethods.js

// This function calls the Web service method without 
// passing the callback function. 
function GetNoReturn()
{
    Imap.WebServices.ImapSvc.GetServerTime();
    alert("This method does not return a value.");
    
}


// This function calls the Web service method and 
// passes the event callback function.  
function GetTime()
{
    Imap.WebServices.ImapSvc.GetServerTime(
    SucceededCallback);
    
}


// This function calls the Web service method 
// passing simple type parameters and the 
// callback function  
function Add(a,  b)
{
    Imap.WebServices.ImapSvc.Add(a, b, 
    SucceededCallback);
}

// This function calls the Web service method 
// that returns an XmlDocument type.  
function GetXmlDocument() 
{
    Imap.WebServices.ImapSvc.GetXmlDocument(
        SucceededCallbackWithContext, FailedCallback,
        "XmlDocument")
}

// This function calls a Web service method that uses
// GET to make the Web request.
function MakeGetRequest() 
{

    Imap.WebServices.ImapSvc.EchoStringAndDate(
        new Date("1/1/2007"), " Happy",
        SucceededCallback, 
        FailedCallback, "HappyNewYear");

}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object, the user context, and the 
// calling method name as parameters.
function SucceededCallbackWithContext(result, userContext, methodName)
{
    var output;
    
    // Page element to display feedback.
    var RsltElem = document.getElementById("ResultId");
    
    var readResult;
    if (userContext == "XmlDocument")
	{
	
	    if (document.all) 
	        readResult = 
		        result.documentElement.firstChild.text;
		else
		    // Firefox
		   readResult =
		        result.documentElement.firstChild.textContent;
		
	     RsltElem.innerHTML = "XmlDocument content: " + readResult;
	}
    
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallback(result, eventArgs)
{
    // Page element to display feedback.
    var RsltElem = document.getElementById("ResultId");
    RsltElem.innerHTML = result;
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallback(error)
{
    // Display the error.    
    var RsltElem = 
        document.getElementById("ResultId");
    RsltElem.innerHTML = 
    "Service Error: " + error.get_message();
}

// This function calls the Web service method 
// passing simple type parameters and the 
// callback function  
function GetIP(IP)
{
    var mapcount = getMapCount();
    if (mapcount == 0)
    {
       alert( "Please select atleast one map for making this search." );
       return;
    } 
    if (IP.length == 0)
    {
       alert( "Please enter the IP to find its originating location." );
       return;
    }
    if (!validateIP())
    {
    alert(IP + "  is not a valid IP/ IP will be in the format [xxx.xxx.xxx.xxx]");
    return;
    }
    
    document.getElementById("imgIPLookup").style.visibility="visible";
    Imap.WebServices.ImapSvc.GetIP(IP, SucceededIPResults,handleFailedIPLookup);
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededIPResults(result, eventArgs)
{
   
    // Page element to display feedback.
  /**  var RsltElem = document.getElementById("ResultId");
    RsltElem.innerHTML = result;*/
       document.getElementById("imgIPLookup").style.visibility="hidden";  

    if (result == null)
    {
       alert( "There were no results for the specified IP Location."  );
       return;
    }  
    MarkOnMap(result.latitude,result.longitude ,result.city,result.countryCode,result.countryName,result.region);
}



function GetLocationDetailsByPostCode(postCode)
{
            if (postCode.length == 0)
            {
               alert( "Please enter the Postcode to find its location." );
               return;
            }
 	    var mapcount =getMapCount();
	    if (mapcount == 0)
        {
           alert( "Please select atleast one map for making this search." );
           return;
        }             
            document.getElementById("imgPostCodeSearch").style.visibility="visible";
            var PostcodeExactMatch = document.getElementById("rdoExact");
             var PostcodeLike = document.getElementById("rdoLike");
            var CountryCode = document.getElementById("countriesDropDown").value;
            if (PostcodeExactMatch != null)
            {
                if(PostcodeExactMatch.checked)
                {
                    Imap.WebServices.GeonamesSvc.GetLocationDetailsByPostcode(postCode, CountryCode,0, SucceededpostCodeResults,handleFailedPostCodeSearch);
                    if (CountryCode == 'IN' && document.getElementById("rdoPODb").checked)
                    Imap.WebServices.GeonamesSvc.GetIndianLocationDetailsByPostcode(postCode, 0, SucceededIndianpostCodeResults,handleIndianFailedPostCodeSearch);
                 }
                else if(PostcodeLike.checked)
                {
                    Imap.WebServices.GeonamesSvc.GetLocationDetailsByPostcode(postCode, CountryCode, 1, SucceededpostCodeResults,handleFailedPostCodeSearch);
                    if (CountryCode == 'IN' && document.getElementById("rdoPODb").checked)
                    Imap.WebServices.GeonamesSvc.GetIndianLocationDetailsByPostcode(postCode, 1, SucceededIndianpostCodeResults,handleIndianFailedPostCodeSearch);
                    
                 }
                else 
                {
                    Imap.WebServices.GeonamesSvc.GetLocationDetailsByPostcode(postCode, CountryCode, 2, SucceededpostCodeResults,handleFailedPostCodeSearch);
                    if (CountryCode == 'IN' && document.getElementById("rdoPODb").checked)
                    Imap.WebServices.GeonamesSvc.GetIndianLocationDetailsByPostcode(postCode, 2, SucceededIndianpostCodeResults,handleIndianFailedPostCodeSearch);
                    
                    
                 }
                    
            }
            else
            {
                    Imap.WebServices.GeonamesSvc.GetLocationDetailsByPostcode(postCode, CountryCode, 2, SucceededpostCodeResults,handleFailedPostCodeSearch);
                    if (CountryCode == 'IN')
                    Imap.WebServices.GeonamesSvc.GetIndianLocationDetailsByPostcode(postCode, 2, SucceededIndianpostCodeResults,handleIndianFailedPostCodeSearch);
                 
            }
    
}
// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededpostCodeResults(result, eventArgs)
{
    document.getElementById("imgPostcodeLookup").style.visibility="hidden";
    document.getElementById("imgPostCodeSearch").style.visibility="hidden";
   
    DisplayPostecodeResults(result);
}
function SucceededIndianpostCodeResults(result, eventArgs)
{
    document.getElementById("imgPostcodeLookup").style.visibility="hidden";
    document.getElementById("imgPostCodeSearch").style.visibility="hidden";
   
    DisplayIndianPostecodeResults(result);
}

function GetPostCodeDetailsByLocation(lat, lon)
{
 	    var mapcount =getMapCount();
	    if (mapcount == 0)
        {
           alert( "Please select atleast one map for making this search." );
           return;
        } 
    if (lat.length == 0)
    {
       alert( "The Latitude is zero, Please click on the map to select the postcode location." );
       return;
    }
    if (lon.length == 0)
    {
       alert( "The Longitude is zero, Please click on the map to select the postcode location." );
       return;
    }    
     if (!IsNumeric(lat))
    {
       alert( "Please enter a integer value for Latitude." );
       return;
    }  
      if (!IsNumeric(lon))
    {
       alert( "Please enter a integer value for Longitude." );
       return;
    }       
      
    
  document.getElementById("imgPostcodeLookup").style.visibility="visible"; 
  Imap.WebServices.GeonamesSvc.GetPostCodesByLocation(lat, lon, SucceededpostCodeResults,handleFailedPostCodeLookup);

    
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function handleFailedPostCodeLookup(error)
{
    // Display the error.    
    document.getElementById("imgPostcodeLookup").style.visibility="hidden";     
   alert("Post Code Lookup Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}
function handleIndianFailedPostCodeSearch(error)
{
    // Display the error.    
    document.getElementById("imgPostCodeSearch").style.visibility="hidden";     
   alert("Indian database Post Code Lookup Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function handleFailedPostCodeSearch(error)
{
    // Display the error.    
    document.getElementById("imgPostCodeSearch").style.visibility="hidden";     
   alert("Global Post Code Search Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}

// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function handleFailedIPLookup(error)
{
    // Display the error.    
    document.getElementById("imgIPLookup").style.visibility="hidden";     
   alert("IP Lookup Service Connection Error: " + error.get_message() + '. Please submit your query again.');
}


  function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }



if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

