var searchMode, countryList, regionList, regionTxt, searchRadius, postalCode;

	function Initialize() {
		//these id's are injected from the server side
		searchMode =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('hdnSearchMode');	
		countryList =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(countryControlID);	
		regionList =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(regionControlID);
		regionTxt =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(txtRegionControlID);
		searchRadius =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(searchRadiusControlID);
		postalCode =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(postalCodeControlID);
	}

	function GetCurrCountry(returnType) {
		var selectedCountry =  _proxy_jslib_handle( _proxy_jslib_handle(countryList.options, (countryList.selectedIndex), 0, 0), 'value', '', 0, 0).split(SPLITCHAR);		
		var countryID = selectedCountry[0];	
		var countryCode = selectedCountry[1];

		if (returnType == 'code')
			return countryCode;	
		else
			return countryID;
	}
	
	function showRegion() {

		var currCountryCode = GetCurrCountry('code');	
		if(ContainsCountry(currCountryCode, regionCountries) == true)
		{
			//Call script proxy passing the input element data
			requestSimpleService = MySpace.Web.Services.GeoLocation.GetStates(
				GetCurrCountry(),   //param1
				currentCulture,		//param2
				OnComplete,			//Complete event
				OnTimeout  

 );
		}

		ToggleDisplayType();
        return false;
	}

	function OnComplete(results) {
		var optionObject;
		
		regionList.options.length = 0; //reset	
		
		if ( displayMode == 'browse' )
		{
			optionObject = new (Option)(defaultOptionForDropDown,"0");
			regionList.options[0]=optionObject;
		}
		else if ( displayMode == 'profile' )
		{
		    optionObject = new (Option)(defaultOptionForDropDown,"0");
			regionList.options[0]=optionObject;
		}
		
		for (var n in results)
		{
			optionObject = new (Option)(n,  _proxy_jslib_handle(results, (n), 0, 0));
			//this if statement is due to some funky stuff going on with safari
			if ( _proxy_jslib_handle(optionObject, 'value', '', 0, 0).indexOf('function') != 0)
				 _proxy_jslib_assign('', regionList.options, (regionList.options.length), '=', (optionObject));
		}
				
		ToggleDisplayType();		

	}

	function OnTimeout(result) {
		//alert("Timed out");
	}
	
	function IsDistanceSearchEnabled() {
	    var currCountryCode = GetCurrCountry('code');	
	    return ContainsCountry(currCountryCode, postalCountries);
	}

	function ValidatePostalCode() {	  		
		if ( _proxy_jslib_handle(searchMode, 'value', '', 0, 0) == 'distance')
		{
			if ( _proxy_jslib_handle(postalCode, 'value', '', 0, 0).length > 0 && searchRadius.selectedIndex == 0) 
			{
                alert(specifyDistance);
                return false;
            }					
            if (( _proxy_jslib_handle(postalCode, 'value', '', 0, 0).length < 1) && (searchRadius.selectedIndex > 0)) 
            {
				alert(specifyPostalCode);
				return false;
			}							
		}
		return true;
	}

	function ToggleDisplayType() {		
		var currCountryCode = GetCurrCountry('code');			
		var regionRowDD =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("regionRowDD");
		var regionRowTxt =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("regionRowTxt");
		var postalRow =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("postalRow");
		
		if (displayMode == 'browse')
		{
			//alert('browse');
			if(ContainsCountry(currCountryCode, postalCountries) == true && ContainsCountry(currCountryCode, regionCountries) == false)
			{
				 _proxy_jslib_assign('', searchMode, 'value', '=', ( "distance"));
				regionRowDD.style.display = 'none';
				regionRowTxt.style.display = 'none';
				postalRow.style.display = '';	
			}
			else if(ContainsCountry(currCountryCode, postalCountries) == false && ContainsCountry(currCountryCode, regionCountries) == true)
			{
				 _proxy_jslib_assign('', searchMode, 'value', '=', ( "region"));
				regionRowDD.style.display = '';
				regionRowTxt.style.display = 'none';
				postalRow.style.display = 'none';				
			} 
			else
			{
				 _proxy_jslib_assign('', searchMode, 'value', '=', ( "none"));
				regionRowDD.style.display = 'none';
				regionRowTxt.style.display = 'none';
				postalRow.style.display = 'none';		
			}
		}
		else if (displayMode == 'profile')
		{
			//alert(regionsFound);
			//if a culture other than en-US is selected on the ProfileEdit page, we need to populate the 'height' drop down list with centimeters instead of feet.
			if ( currentCulture == 'en-US' )
				showHeightDiv( 'FTIN' );
			else
				showHeightDiv( 'CM' );
			
			if(ContainsCountry(currCountryCode, regionCountries) == true && regionList.options.length > 0)
			{
			    //if regions are supplied in the database and they are set to show regions, the drop down of regions should appear
			     _proxy_jslib_assign('', searchMode, 'value', '=', ( "region"));
			    regionRowDD.style.display = '';
			    regionRowTxt.style.display = 'none';
			    postalRow.style.display = 'none';	
			}
			else
			{
				//if regions are not supplied in the database or they are set to not show regions, they need to enter it in manually so show the textbox
				 _proxy_jslib_assign('', searchMode, 'value', '=', ( "region"));
				regionRowTxt.style.display = '';
				regionRowDD.style.display = 'none';
				postalRow.style.display = 'none';	
			}
		}
	}
    
	function ContainsCountry(country, countries) {
	   for(x in countries)
	   {
			if(country ==  _proxy_jslib_handle(countries, (x), 0, 0))
			{
				return true;
			}
		}
		return false;
	}
	
	function showHeightDiv ( metric ) {
		
		var divUS;
		var divNonUS;
		
		divUS =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('USHeight');
		divNonUs =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('NonUSHeight');
		
		if ( metric == "CM" )
		{
			divNonUs.style.display = '';
			divUS.style.display = 'none';
		} else if ( metric == "FTIN" ) {
			divNonUs.style.display = 'none';
			divUS.style.display = '';
		}
		
	} ;
_proxy_jslib_flush_write_buffers() ;