﻿var officeLocationClass = "officeLocation";
var OfficeLocationMainDiv = "officeLocation";
var officeLocationIntroText = "introduction";
var officeLocationHeaderText = "OfficeLocationHeader";
var officeLocationHeadOfficeText = "HeadOffice";

function OnloadOfficeLocation()
{	
	if (getElement(OfficeLocationMainDiv) != null)
	{
		//Get parameters from url
		var region = getURLParam("region");
		var country = getURLParam("country");
		var city = getURLParam("city");

		//No request parameters: display head office
		if (region == "" && country == "" && city == "") {
			displayHeadOffice();
		} 
		//Display by city
		else if (region != "" && country != "" && city != "") {
			displayByCity(region, country, city, false);
		}
		//Display by country
		else if (country != "" && city == "") {
			displayByCountry(region, country, false);
		}
	}
}

function hideAllOfficeLocations()
{
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
	// Hide all divs for office locations
	for(var i = 0; i < divs.length; i++) { 
		var divId = divs[i].id;
		if (divId && divId != officeLocationIntroText)
			displayOfficeLocation( divId, false);
	}
}

function displayHeadOffice()
{
    hideAllOfficeLocations();
    getElement(officeLocationHeaderText).innerHTML = 'Office Locations';
    displayOfficeLocation('Thailand-Bangkok', "true");
}

function displayByCountry(region, country, isFlashRequest)
{
    //alert('displayByCountry region=' + region + ' country=' + country + ' city=' + city);
    
    hideAllOfficeLocations();
    getElement(officeLocationHeaderText).innerHTML = 'Office Locations' + ' : ' + country;
    
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
	
    // Hide all divs for office locations
    for(var i = 0; i < divs.length; i++) { 
        var divId = divs[i].id;

        var searchText = country.replace(" ", "").toLowerCase();
        if (divId && divId.toLowerCase().indexOf(searchText) == 0) //the div id starts with the country name
            displayOfficeLocation( divId, true);
    }

    if (!isFlashRequest)
		ZoomIn(region, country, '-');
}

function displayByCity(region, country, city, isFlashRequest)
{
    hideAllOfficeLocations();
    getElement(officeLocationHeaderText).innerHTML = 'Office Locations' + ' : ' + city;
    
    //Get all div elements
    var divs = getElement(OfficeLocationMainDiv).getElementsByTagName("div");
    
    // Hide all divs for office locations
    for(var i = 0; i < divs.length; i++) { 
        var divId = divs[i].id;
        var searchText = "-" + city.replace(" ", "").toLowerCase();
        if (divId && divId.toLowerCase().indexOf(searchText) > -1) //the div id in "-[city name with no space]" format
            displayOfficeLocation( divId, true);
    }
    
    if (!isFlashRequest)
        ZoomIn(region, country, city);
}

function getElement ( whichElement ) 
{
  var elem;
  
  if( document.getElementById ) // this is the way the standards work    
    elem = document.getElementById( whichElement );  
  else if( document.all ) // this is the way old msie versions work      
    elem = document.all[whichElement];  
  else if( document.layers ) // this is the way nn4 works    
    elem = document.layers[whichElement];
    
  return elem;
}    

function displayOfficeLocation( whichLayer, isDisplayed ) 
{
    var oElement = getElement(whichLayer);
    if(oElement)
    {
        oElement.style.display = (isDisplayed) ? "block" : "none";
    }
}

function getURLParam(strParamName) 
{
  var strReturn = "";
  var strHref = window.location.href;
  
  if ( strHref.indexOf("?") > -1 ){
  
    //Get query string
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    
    //Remove anchor text from the string
    if ( strQueryString.indexOf("#") > -1 )
      strQueryString = strQueryString.substr(0, strQueryString.indexOf("#"));
      
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( aQueryString[iParam].toLowerCase().indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}  

//Function called by flash
//old (location, zone) 
function DisplayLocation(region, country, city) 
{
    //alert('DisplayLocation region=' + region + ' country=' + country + ' city=' + city);
    if (city != null && city != "")
        displayByCity(region, country, city, true)
    else
        displayByCountry(region, country, true)
}

//function displayFlash(region, country, city) 
function ZoomIn(region, country, city) 
{   //alert(country);
	//var flash = document.getElementById("locationSWF");
    var flash = getElement("locationSWF");

    region = region.replace(" ", "").toLowerCase();
    country = country.replace(" ", "").toLowerCase();
    city = city.replace(" ", "").toLowerCase();

    flash.doZoomIn(region,country,city);
}


