﻿/*
Script Dependencies: common.js
*/

user_reg = {

init : function() 
    {
        if( document.getElementById( 'continent' ).options[document.getElementById( 'continent' ).options.selectedIndex].value != 'All_Continents' )
        {
            mapLevel = 2;

	        if( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value != 'All_Countries' )
	        {
                mapLevel = 3;

                if( document.getElementById( 'region' ).options[document.getElementById( 'region' ).options.selectedIndex].value != 'All_Regions' )
                {
                    mapLevel = 4;
                }
	        }
        }
    }
}

// Run the init method for the following
DOMhelp.addEvent( window, 'load', user_reg.init, false );


// The variable http will hold our new XMLHttpRequest object.
var http2 = CreateRequestObject(); 
var http3 = CreateRequestObject(); 

var mapLevel = 1;
var countryId = '';
var dropDownFlag = false;
var userAction

function GetCountries( continent )
{
	http2.open('get', baseDirectory + '/fill_countries.php?continent=' + continent );
	http2.onreadystatechange = FillCountries; 
	http2.send(null);
}

function FillCountries()
{
    if( http2.readyState == 4 )
    {
        var response = http2.responseText;

        // If our response text contains data update the featured property list
		if( response.length != 0 )
		{
			if( document.getElementById( 'country' ))
			{
                select_innerHTML( document.getElementById( 'country' ), response );
           }
		}
    }
}

function GetRegions( country )
{
	http3.open('get', baseDirectory + '/fill_regions.php?country=' + country );
	http3.onreadystatechange = FillRegions; 
	http3.send(null);
}

function FillRegions()
{
    if( http3.readyState == 4 )
    {
        var response = http3.responseText;

        // If our response text contains data update the featured property list
		if( response.length != 0 )
		{
			if( document.getElementById( 'region' ))
			{
                select_innerHTML( document.getElementById( 'region' ), response );
            }
		}
    }
}

function GetCities( country, region )
{
	http2.open('get', baseDirectory + '/fill_cities.php?country=' + country + '&region=' + region );
	http2.onreadystatechange = FillCities; 
	http2.send(null);
}

function FillCities()
{
    if( http2.readyState == 4 )
    {
        var response = http2.responseText;

        // If our response text contains data update the featured property list
		if( response.length != 0 )
		{
			if( document.getElementById( 'city' ))
			{
                select_innerHTML( document.getElementById( 'city' ), response );
            }
		}
    }
}

function ContinentChanged()
{
    select_innerHTML( document.getElementById( 'country' ), '<option value="All_Countries">Loading...</option>' );

  
    
    self.setTimeout( 'AsyncContinentChanged()', 0 );
    
}

function AsyncContinentChanged()
{
    dropDownFlag = true;
    
    if( document.getElementById( 'flash_map' ))
    {
        // Change the flash map to the appropriate continent
        if( document.getElementById( 'continent' ).options[document.getElementById( 'continent' ).options.selectedIndex].value != 'All_Continents' )
        {
            fmAreaZoomIn( document.getElementById( 'continent' ).options[document.getElementById( 'continent' ).options.selectedIndex].value );
            mapLevel = 2;
        }
        else
        {
            fmInitialView();
            mapLevel = 1;
        }


    }
    
    UpdateContinents();
               
               //// IE FIX
               select_innerHTML( document.getElementById( 'region' ), '<option value="All_Countries">All State/Province</option>' );

               //// IE FIX
               select_innerHTML( document.getElementById( 'city' ), '<option value="All_Countries">All Cities</option>' );


}

function UpdateContinents()
{
    GetCountries( document.getElementById( 'continent' ).options[document.getElementById( 'continent' ).options.selectedIndex].value );
    document.getElementById( 'region' ).innerHTML = '<option value="All_Regions">All State/Province</option>';
                   //// IE FIX
               select_innerHTML( document.getElementById( 'region' ), '<option value="All_Countries">All State/Province</option>' );


}

function CountryChanged( ind )
{
    select_innerHTML( document.getElementById( 'region' ), '<option value="All_Regions">Loading...</option>' );
    
    self.setTimeout( 'AsyncCountryChanged()', 0 );
}

function AsyncCountryChanged()
{
    dropDownFlag = true;
    
    if( document.getElementById( 'flash_map' ))
    {
        if( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value == 'All_Countries' )
        {
            fmMapBackLevel();
            mapLevel = 2;
        }
        else
        {
            if( mapLevel > 3 )
            {   
                fmAreaBackAndCenter( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value );
                mapLevel = 3;
            }
            else
            {
                fmAreaCenter( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value );
                mapLevel = 3;
            }
        }
    }
    
    UpdateCountries();
 
                   //// IE FIX
               //select_innerHTML( document.getElementById( 'city' ), '<option value="All_Countries">All Cities</option>' );

}

function UpdateCountries()
{
    GetRegions( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value );

    if( document.getElementById( 'city' ) )
    {
        //document.getElementById( 'city' ).innerHTML = '<option value="All_Cities">City</option>';
    }
}

function RegionChanged( ind )
{
    dropDownFlag = true;

    if( document.getElementById( 'city' ) )
    {
        select_innerHTML( document.getElementById( 'city' ), '<option value="All_Cities">Loading...</option>' );
    }
    
    if( !ind && document.getElementById( 'flash_map' ))
    {
        if( document.getElementById( 'region' ).options[document.getElementById( 'region' ).options.selectedIndex].value == 'All_Regions' )
        {
            fmMapBackLevel();
            mapLevel = 3;
        }
        else
        {
            fmAreaCenter( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value + '_' + document.getElementById( 'region' ).options[document.getElementById( 'region' ).options.selectedIndex].value );
            mapLevel = 4;
        }
    }
    
    UpdateRegions();

}

function UpdateRegions()
{
    if( document.getElementById( 'city' ) )
    {
        GetCities( document.getElementById( 'country' ).options[document.getElementById( 'country' ).options.selectedIndex].value, document.getElementById( 'region' ).options[document.getElementById( 'region' ).options.selectedIndex].value );
    }
}

function listLoad(level_str, areas_str) 
{
    if( level_str == 1 )
    {
        SelectOptionInList( document.getElementById( 'continent' ), 'All_Continents' );
        
        if( !dropDownFlag )
        {
            UpdateContinents();
        }
    }
    else if( level_str == 2 )
    {
        SelectOptionInList( document.getElementById( 'continent' ), areas_str );

        if( !dropDownFlag )
        {
	        UpdateContinents();
	    }
	}
    else if( level_str == 3 )
    {
        SelectOptionInList( document.getElementById( 'country' ), areas_str.substr( 3, 2 ));

        if( !dropDownFlag )
        {
            UpdateCountries( true );
        }
    }
    else if( level_str == 4 )
    {
        SelectOptionInList( document.getElementById( 'region' ), areas_str.substr( 9, 2 ));

        if( !dropDownFlag )
        {
	        UpdateRegions( true );
        }
	}
	
    dropDownFlag = false;
}


function select_innerHTML(objeto,innerHTML)
{
    objeto.innerHTML = "";    
    var selTemp = document.createElement("micoxselect");
    var opt;    
    selTemp.id="micoxselect1";    
    document.body.appendChild(selTemp);    
    selTemp = document.getElementById("micoxselect1");    
    selTemp.style.display="none";    
    
    if(innerHTML.toLowerCase().indexOf("<option")<0)
    {
        //se n?o ? option eu converto        
        innerHTML = "<option>" + innerHTML + "</option>";    
    }    
    
//    innerHTML = innerHTML.toLowerCase().replace(/<option/g,"<span").replace(/<\/option/g,"</span");    
    innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span");    
    selTemp.innerHTML = innerHTML;              
    
    for(var i=0;i<selTemp.childNodes.length;i++)
    {  
        var spantemp = selTemp.childNodes[i];          
        
        if(spantemp.tagName)
        {                 
            opt = document.createElement("OPTION");       
            
            if(document.all)
            { 
                //IE    
                objeto.add(opt);   
            }
            else
            {    
                objeto.appendChild(opt);   
            }              
            
            //getting attributes   
            
            for(var j=0; j<spantemp.attributes.length ; j++)
            {    
                var attrName = spantemp.attributes[j].nodeName;    
                var attrVal = spantemp.attributes[j].nodeValue;    
                if(attrVal)
                {     
                    try
                    {      
//                        opt.setAttribute(attrName,attrVal);      
//                        opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));     
                    }
                    catch(e){}    
                }   
            }   
            
            //getting styles   
            if(spantemp.style)
            {    
                for(var y in spantemp.style)
                {     
                    try
                    {
//                        opt.style[y] = spantemp.style[y];
                    }
                    catch(e){}   
                }   
             }   
             
             //value and text   
             opt.value = spantemp.getAttribute("value");   
             opt.text = spantemp.innerHTML;   
             
             //IE   
             opt.selected = spantemp.getAttribute('selected');   
//             opt.className = spantemp.className;  
       }  
   }     
   
   document.body.removeChild(selTemp);
   selTemp = null;
}


// -------------------------------------------------------------------------
//  Name: SelectOptionInList
//  Abstract: Given a select list and an ID search the list for the option with
//                  the matching ID and select it.
// -------------------------------------------------------------------------
function SelectOptionInList( lstSelectList, intID )
{
      try
      {
            var intIndex = 0;
            // Loop through all the options
            for( intIndex = 0; intIndex < lstSelectList.options.length; intIndex++ )
            {
                  // Is this the ID we are looking for?
                  if( lstSelectList.options[intIndex].value == intID )
                  {
                        // Select it
                        lstSelectList.selectedIndex = intIndex;
                        // Yes, so stop searching
                        break;
                  }
            }
      }
      catch( expError )
      {
            alert( "ClientUtilities1.js::SelectOptionInList( ).\n" +
                        "Error:" + expError.number + ", " + expError.description );
      }
} // SelectOptionInList
      