
//-----------------------------------------
//********* Timer to set city value for URL

var c=0;
var t;
var CityDateURL = "";

function IdentifyTimer(Param1)
{
  var aobj;
  try {
     aobj=parent.document.getElementById("CitiesFrame").contentWindow.document.getElementById("city");
  } catch (e) {aobj=null}
  
  if (aobj == null){
     c=c+1;
     t=setTimeout("IdentifyTimer(" + Param1 + ")",200);
     return;
  };
    
  clearTimeout(t);
  for(var i = 0; i < aobj.options.length; i++ ) {  
    if(aobj.options[i].value == Param1) {
       aobj.options[i].selected = true;	
       break;
    }
   }
}

//--------------------------------------
//********* Function for URL to Locator

function onLoadpage(Param1,Param2)
{
 if (Param1==""){
 LoadDefaultCity();
 }
 else {
 langext=findlang();
 var cities=parent.document.getElementById("CitiesFrame");//ref to CitiesFrame 
 cities.src="Search/Citiesearch.aspx?lang="  + langext; 
  
 try 
 {
 LoadKSACity(Param1,Param2);
 }catch(e){};

 IdentifyTimer("'" + Param1 + "'");
 }
}

//----------------------------------------------------------------------------------
//*********Loads DefaultImage & listboxes for cities & po's the first time app loads

function LoadDefaultCity()
{ 
  //initDHTMLAPI();
  langext=findlang();
  parent.document.getElementById("CitiesFrame").src="Search/Citiesearch.aspx?lang="  + langext; 
  parent.document.getElementById("MapContainer").src="DefaultImageMap" + parent.LanguageExtention + ".html";
  clearSrchresult();//clear search results from result frame 
  errorMessage();
  
  
  //********************************
  parent.document.getElementById("po_tab").style.display         = "block";
  parent.document.getElementById("SimpleSearchTR").style.display = "block";
  parent.document.getElementById("resultTR").style.display       = "block";

  parent.document.getElementById("DistrictFrame").src="Search/Districtsearch.aspx?city=" + city + "&lang=" + escape(lang_ext); 
  parent.document.getElementById("PostofficeFrame").src="Search/POsearch.aspx?City=" + city + "&lang=" + escape(lang_ext); 
  
 
  //------- clear all fields from Address search Tab
  //SelectionCtrl.disabled=true;//disable cities list box
  parent.document.getElementById("stext_buildingno").value ="";
  parent.document.getElementById("text_street").value      ="";
  parent.document.getElementById("text_zipcode").value     ="";
  parent.document.getElementById("text_addno").value       ="";
  parent.document.getElementById("stext_buildingno").focus();
  parent.document.getElementById("ResultFrame").src ="blank.htm"; 
}

//----------------------------------------------
//******* Define custom error Messages for users
function errorMessage() {

  langext=findlang();
  if (langext=="E")
  {
  font         = "<table cellpadding=0 cellspacing=0 border=0><tr><td><span style='font-size: 10pt; color: red; font-family:Tahoma'>";
  
  //---- for topic search
  altbuilding2 = "Building No or AddNo. must have only 4 digits!";
  altmsg       = "Invalid Search !";
  astreet_dist = "Street search with district is not yet available for this city !";
  altstreet    = "Search with street is not yet available for this city !";
  alertzip2    = "Zipcode must have only 5 digits!";
  
  //---- for service search
  alert1 = "<u>Search Service By District</u>[Select District => Select Category => Select Service];<BR><u>Search All Services</u>&nbsp[Select Category => Enter service name in the text box => Click Locate Button]";
  alert2 = "Please select a [Category] & enter [Service] name to search in all services!";
  }  
}

//--------------------------------------------------------------------------------------------------------------
//******* This function is triggred upon selecting a city from listbox or from map image. It passes the required
//******* cityname to bring the mapfile.

function LoadKSACity(x,Param2)
{ 
  var city=x;
  lang_ext = parent.LanguageExtention;
  errorMessage();

  //var aUrl = "Search/GetLastModifiedDate.aspx?city=" + city + "&lang=" + escape(lang_ext);
  //CityDateURL = aUrl;
  //alert("CityDateURL1 = " + CityDateURL);
  //getCityUpdateDate();
  
  /*hide(screenTR);
  show(po_tab);
  show(SimpleSearchTR);
  show(resultTR);*/
  //parent.document.getElementById("screenTR").style.display       = "none";
  parent.document.getElementById("po_tab").style.display         = "block";
  parent.document.getElementById("SimpleSearchTR").style.display = "block";
  parent.document.getElementById("resultTR").style.display       = "block";

  parent.document.getElementById("DistrictFrame").src="Search/Districtsearch.aspx?city=" + city + "&lang=" + escape(lang_ext); 
  parent.document.getElementById("PostofficeFrame").src="Search/POsearch.aspx?City=" + city + "&lang=" + escape(lang_ext); 
  parent.document.getElementById("MapContainer").src=aSPwmsURL+"?map=" + city + "&lang=" + parent.LanguageExtention + "&pin_points=" + Param2;// + "&sel_layer=districts&sel_ids=1,2,3,119";
  cities=parent.document.getElementById("CitiesFrame");//ref to CitiesFrame 
  SelectionCtrl=cities.contentWindow.document.getElementById("city");//ref to cities listbox in CitiesFrame
 // alert(SelectionCtrl);
  for(var i = 0; i < SelectionCtrl.options.length; i++ ) 
  {  
    if(SelectionCtrl.options[i].value == city) 
    { 
      SelectionCtrl.options[i].selected = true;	
       break;
    }
   }
   
  //------- Check for Geoweb menu buttons & enable
  /*if (parent.document.getElementById("menubutton1"))
  {
  parent.document.getElementById("menubutton1").disabled = false;
  parent.document.getElementById("menubutton2").disabled = false;
  }*/
  
  //------- clear all fields from Address search Tab
  //SelectionCtrl.disabled=true;//disable cities list box
  parent.document.getElementById("stext_buildingno").value ="";
  parent.document.getElementById("text_street").value      ="";
  parent.document.getElementById("text_zipcode").value     ="";
  parent.document.getElementById("text_addno").value       ="";
  parent.document.getElementById("stext_buildingno").focus();
  parent.document.getElementById("ResultFrame").src = "blank.htm";
}

//----------------------------------------------------------------------------------------------------------
//******* This function is triggred when a mapfile is already in the mapcontainer and a city is slected from
//******* listbox. It passes the required cityname to bring the mapfile.

function LoadKSACity2(avalue)
{
  city = avalue;
  lang_ext = parent.LanguageExtention; 
  parent.document.getElementById("DistrictFrame").src="Search/Districtsearch.aspx?city=" + city + "&lang=" + escape(lang_ext); 
  parent.document.getElementById("PostofficeFrame").src="Search/POsearch.aspx?City=" + city + "&lang=" + escape(lang_ext);
  parent.document.getElementById('MapContainer').contentWindow.mySetMap(city);
  
  //var aUrl = "GetLastModifiedDate.aspx?city=" + city + "&lang=" + escape(lang_ext);
  //CityDateURL = aUrl;
  //alert("CityDateURL2 = " + CityDateURL);
  //getCityUpdateDate();
  
  if (parent.document.getElementById("SimpleSearchTR").style.display == 'none')
  {
  parent.document.getElementById("latlongTR").style.display        = 'none';
  parent.document.getElementById("ServiceSearchTR").style.display  = 'none';
  parent.document.getElementById("SimpleSearchTR").style.display   = 'block';
  parent.document.getElementById("stext_buildingno").value ="";
  parent.document.getElementById("text_street").value      ="";
  parent.document.getElementById("text_zipcode").value     ="";
  parent.document.getElementById("text_addno").value       ="";
  parent.document.getElementById("stext_buildingno").focus();
   if (parent.LanguageExtention == "_A") 
   {parent.document.getElementById("ResultFrame").style.height="197";} //191
   else
   {parent.document.getElementById("ResultFrame").style.height="202";} //196
  }
  clrsel();//clear map selection
  parent.document.getElementById("ResultFrame").src ="blank.htm";
}

//----------------------------
//******* clear search results

function clearSrchresult()
{
 parent.document.getElementById("ResultFrame").src ="blank.htm";
}

//---------------------------------
//******* clear selection from maps

function clrsel()
{
 parent.document.getElementById('MapContainer').contentWindow.ClearAllSelectionLayers();
}

//----------------------------------------------------------------------------------------------
//******* determines the parent.language and sets the variable as A or E for respective langauge

function findlang()
{ 
  lang_ext = parent.LanguageExtention;
  if (lang_ext.substring(1)=="A") 
  {
  langext="A";
  }
  else 
  {
  langext="E";
  }
  return langext;
}

//-----------------------------------------
//******* functions for mouse cursor as hand

  function fnTrackMouse() { document.getElementById("banner").style.cursor = "hand"; }
  function fnStopTrackMouse() { document.getElementById("banner").style.cursor = "auto"; }
  
  function fnTrackMouse1()   {document.getElementById("dispsim_btn").style.cursor="hand";}
  function fnStopTrackMouse1()   {document.getElementById("dispsim_btn").style.cursor="auto";}

  function fnTrackMouse2()   {document.getElementById("displatlong_btn").style.cursor="hand";}
  function fnStopTrackMouse2()   {document.getElementById("displatlong_btn").style.cursor="auto";}

  function fnTrackMouse3()   {document.getElementById("dispserv_btn").style.cursor="hand";}
  function fnStopTrackMouse3()   {document.getElementById("dispserv_btn").style.cursor="auto";}
  
  
//----------------------------------------------------------------------------------------------------------
//======= show Welcome contents onloading application
function getContents() {

  msgDiv = document.getElementById("welcome");

  if (parent.LanguageExtention=="_A")
  {
    var message1 = "<B>عزيزي الزائر</B><br/><BR> مرحباً بك في المحدد السعودي  <BR><BR>يمكنك تحديد المواقع والبحث عن العناوين البريدية بكل يسر وسهولة باستخدام<BR> افضل سبل البحث <BR><BR> الرجاء اختيار المدينة<br> لاستعراض الخريطة و البحث عن المواقع";
    msgDiv.align = "right";
  }
  else 
  {
    var message1 = "<b>Dear Guest! </b><br/><br/>Welcome to the Saudi Locator. <br/><br/>Your address can be located easily using our search tools. <br/><br/>Please select a city for map navigation & search functionality.";
    msgDiv.align = "left";
  }
  msgDiv.innerHTML = message1;
  document.getElementById("screen").src = "images/screen" + parent.LanguageExtention + ".jpg";
   
}

//------------------------------------------------
//******* Brings help file on the map display area

function getHelp(aHelpFile)
{  
  if (parent.LanguageExtention == "_A" )
  //ps="left=704,top=312,height=470,width=558,status=no,toolbar=no,menubar=no,location=no,scrollbars=no"
  ps="left=250,top=150,height=600,width=1000,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,"
  else
  ps="left=250,top=250,height=600,width=1000,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes"
  window.open(aHelpFile,null,ps);
}


function getAbout()
{  
  
  if (parent.LanguageExtention == "_A" )
  {
     var languageParam = "Arabic";
     //ps="left=704,top=312,height=470,width=558,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes"
  }
  else
  {
     var languageParam = "English";
     //ps="left=7,top=312,height=470,width=558,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes"
  }
  
  window.open("http://www.sp.com.sa/" + languageParam + "/ProductsandServices/Locator/Pages/LocatorHome.aspx",null);
  //window.open("",,"fullscreen,"+s)     
//  if (parent.document.getElementById('MapContainer').contentWindow.myKaMap == null)
//  {
//   //window.open(aHelpFile,null,ps);
//   showContent(aHelpFile);
//  }
//  else
//  {
//   parent.document.getElementById('MapContainer').contentWindow.ShowUserHelp(aHelpFile);
// }
 
}


 function getFaq(aHelpFile)
{  
   alert('under Construction');  
}

//--------------------------------------
//======= Main menu functions for GeoWeb

function getMapRequest() {
parent.document.getElementById('MapContainer').contentWindow.sendMapRequest();
}

//=======
function openMapRequests() {
parent.document.getElementById('MapContainer').contentWindow.openMapRequestsWindow();
}


//function ShowMapUpDate() {

//    document.getElementById("MapUpdateDiv").innerHTML = MapUpdateDate;

//}

//*********************************
//*****A J A X   F u n c t i o n s 
//*********************************

//set the variables. Function to call from an event
function getResult(aUrl) {
    var link = aUrl;
    xmlhttpPost(link);
}

//Make the actual connection.
function xmlhttpPost(strURL) {
    //alert('strURL = ' + strURL);
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) { //ready state 4 means its complete.
            //alert("self.xmlHttpReq.responseText = " + self.xmlHttpReq.responseText);
            getData(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send("");
}

//set the query string to be sent
function getquerystring() {
    qstr = '';
    return qstr;
}


//put the data on the page.
function getCityUpdateDate() {
    try { getResult(CityDateURL); } catch (e) {}
}



//put the data on the page.
function getData(str) {

    if (str.length > 15) {
        var t1 = setTimeout("getCityUpdateDate()", 100);
        return false;
    }

    var MapUpdateDiv = parent.document.getElementById("MapUpdateDiv");
    if (parent.LanguageExtention == "_A") 
    {
        MapUpdateDiv.innerHTML = "<b>" + str + " : " + "آخر تحديث الخريطة</b>";
    } 
    else 
    {
        MapUpdateDiv.innerHTML = "<b>Map Last Updated : " + str + "</b>";
    }
    
    
}
