
function ShowLocate()
{
    CloseAllPopups();
    document.getElementById("Locate").style.display = '';
    ResetLocate();
}
function CloseLocate()
{
    document.getElementById("Locate").style.display = 'none';
}
function ResetLocate() {
    $("#loc1").show();
    $("#loc2").hide();
    document.getElementById("txtYour").value = "";
    document.getElementById("txtuser").value = "";
    document.getElementById("txtLocateNum").value = "";
    document.getElementById("txtLocateNum0").value = "";
    document.getElementById("chkTerms").checked = "";
    document.getElementById("spnMsg").style.display = 'none';
}

var isCancelled = false;
var sNumber = "";
var friendID = "";
var userID = 0;
var _one = 0;
function ShowTNC()
{
//alert('Locate-A-Phone Terms and Conditions:\n===========================\n\n(1) Only Vodacom and MTN users supported. \n\n(2) You will be charged R 5.00 per phone lookup transaction.\n\n\(3) Phone location deemed reliable, but not guaranteed.');
	window.open('http://maps.afrigis.co.za/locateaphone/terms_and_conditions.htm');
}
function LocateMe()
{
    isCancelled = false;
    sNumber = document.getElementById("txtLocateNum").value;
    xmlHttp = GetXmlHttpObject();
    if (document.getElementById("chkTerms").checked) {


        if (xmlHttp == null) {
            alert("Your browser does not support AJAX!");
            return;
        }

        var query = "?clientID=1&clientPassword=1&locatorMSISDN=" + document.getElementById("txtYour").value + "&locatorName=" + document.getElementById("txtuser").value + "&locateeMSISDN=" + sNumber + "&num=" + Math.random();

        $.getJSON("quicklocate.ashx" + query, function(data) {
            if (data.status == false) {
                //document.getElementById("OTPmsg").innerHTML = data.message;
                return;
            }
            else {
                friendID = data.contactID;

                document.getElementById("spnMsg").style.display = '';
            }

        });
    }
    else {
        alert("Please accept terms & conditions");
    }
    
 }
 var t = 0
 function verifyPassword() {
             var Password = document.getElementById("txtLocateNum0").value;


             var query = "?clientID=1&clientPassword=1&locatorMSISDN=" + document.getElementById("txtYour").value + "&otp=" + Password + "&num=" + Math.random();

             $.getJSON("verifyuserpassword.ashx" + query, function(data) {
                 if (data.status == false) {
                     alert(data.message);
                     //    document.getElementById("OTPmsg").innerHTML = data.message;
                     return;
                 }
                 else {
                     userID = data.userID;
                     //document.getElementById("LocateLoader").style.display = '';
                     $("#loc1").hide();
                     $("#loc2").show();
                     $("#loggedInLbl").html(document.getElementById("txtuser").value + " (" + document.getElementById("txtYour").value + ")");
                     //t = setInterval("pollFriend()", 5000);


                     strLoc = "<div style='width:100%;text-align:center;font-size:10pt;font-weight:bold'>Locating phones</div><br/><table style='width:100%'><tr><td style='width:40%'>" + document.getElementById("txtLocateNum").value + "</td><td><div id='" + friendID + "div' style='color:yellow'>Waiting</div></td></tr></table>";
                     $("#locatingList").html(strLoc);
                     pollFriend();
                 }

             });
         }
         function AddToList() {
             var query = "?clientID=1&clientPassword=1&userID=" + userID + "&userPassword=" + document.getElementById("txtLocateNum0").value + "&locateMSISDN=" + $("#Text2").val() + "&num=" + Math.random();


             $.getJSON("requestlocatepermission.ashx" + query, function(data) {
                 if (data.status == false) {
                     strLoc = "<br/><table style='width:100%'><tr><td style='width:40%'>" + document.getElementById("Text2").value + "</td><td><div id='" + data.phoneID + "div' style='color:yellow'>Waiting</div></td></tr></table>";
                     document.getElementById("locatingList").innerHTML += strLoc;

                     if (friendID.length != "") {
                         friendID += "|" + data.contactID;
                         _one = 1;
                     }
                     else {
                         friendID += data.contactID;
                         _one = 0;
                         pollFriend();
                         $("#trAnother").hide();
                     }

                 }



             });
             
         }
         function pollFriend() {

             document.getElementById("btnCancel").style.display = '';
             if (_one > 0) {
                 var friendList = friendID.split('|');
              

                 for (var _icounter = 0; _icounter < friendList.length; _icounter++) {
                     var query = "?clientID=1&clientPassword=1&phoneID=" + friendList[_icounter] + "&num=" + Math.random();
                        
                     $.getJSON("canuserlocatephone.ashx" + query, function(data) {
                         if (data.status == false && data.message == "Waiting") {

                             document.getElementById(data.phoneID + "div").innerHTML = "Waiting For Authorisation.....";
                             
                         }
                         else {
                            
                             locateFriend(data.contactID);

                         }

                     });
                 }
            
                 setTimeout("pollFriend()", 5000);
             }
             else {
                 var query = "?clientID=1&clientPassword=1&phoneID=" + friendID + "&num=" + Math.random();

                 $.getJSON("canuserlocatephone.ashx" + query, function(data) {
                     if (data.status == false && data.message == "Waiting") {
                         document.getElementById(friendID + "div").innerHTML = "Waiting For Authorisation.....";
                         if (!isCancelled)
                             setTimeout("pollFriend()", 5000);
                     }
                     else {                         
                         locateFriend(friendID);
                         friendID = "";
                     }

                 });
             }
             
         }

         function locateFriend(id) {
             var query = "?clientID=1&clientPassword=1&phoneID=" + id + "&num=" + Math.random();

             $.getJSON("locatephone.ashx" + query, function(data) {
                 document.getElementById("btnCancel").style.display = 'none';
                 if (data.located == false) {
                     document.getElementById(id + "div").innerHTML = "Your Request was Denied.....";
                     document.getElementById(id + "div").style.color = 'Red';

                 }
                 else {

                     //alert("Lon: " + data.longitude + " Lat: " + data.latitude);
                     document.getElementById(id + "div").innerHTML = "<input onclick='ZoomLocateLocation(\"" + data.latitude + "\",\"" + data.longitude + "\");' value='View on map' type='button' />";
                     $("#trAnother").show();
                 }

             });
         }
         function ZoomLocateLocation(lat, lon) {
             
             var oLatLng = new AGCoord(parseFloat(lat), parseFloat(lon));
             imgObj = document.createElement("div");
             imgObj.innerHTML = "<img style='cursor:pointer' src ='images/Pin_Red_32x32_24bit.png' />";
             var myHTMLOptions = new AGHTMLOverlayOptions(new AGPoint(-5, -32));
             var myHTMLOverlay = new AGHTMLOverlay(imgObj, oLatLng, myHTMLOptions);
             g_map.addOverlay(myHTMLOverlay, "Point");

             g_map.centreAndScale(oLatLng, 18);
             CloseLocate();
         }
function receiveLocation()
{
    if (xmlHttp.readyState==4)
    {
        alert(xmlHttp.responseText);
//        if(arr_received == "DENIED")
//        {
//            alert('Invalid credentials');
//        }
//        else if (arr_received == "NONE")
//        {
//            alert('Number could not be located');
//        }
//        else
//        {
//          var Coord = arr_received.split("|");
//          //g_map.AddPoint("Images/Pin_Red_32x32_24bit.png", "1locate", parseFloat(Coord[0]),parseFloat(Coord[1]),-5,-32,"Location Result",sNumber);
//          var oLatLng = new AGCoord(parseFloat(Coord[0]),parseFloat(Coord[1]));
//          var oIcon = new AGIcon();
//          oIcon.image = "Images/Pin_Red_32x32_24bit.png";
//          //oIcon.iconSize = new AGSize(31, 32);


//        imgObj=document.createElement("div");
//        imgObj.innerHTML = "<img style='cursor:pointer' src ='images/Pin_Red_32x32_24bit.png' title='"+sNumber+"'/>";
//        var myHTMLOptions = new AGHTMLOverlayOptions(new AGPoint(-5, -32));
//        var myHTMLOverlay = new AGHTMLOverlay(imgObj, new AGCoord(parseFloat(oLatLng.latitude()), parseFloat(oLatLng.longitude()),myHTMLOptions));
//        g_map.addOverlay(myHTMLOverlay, "Point");    
//          
//          g_map.centreAndScale(oLatLng, 18);
        
        xmlHttp = null
    }
}

function CancelLocate() {
    isCancelled = true;
    CloseLocate();
}