function CountryCity (countryId, cityId, areaId)
{
    this.countryId = countryId;
    this.cityId = cityId;
    this.areaId = areaId;
    this.portalId = 0;
    
    this.countryCityEntity = 1; //Listing
    this.transactionTypeId = 0;

    this.defaultCountry = '';
    this.defaultCity = '';
    this.defaultArea = '';
    
    this.showAllCountry = true;
    this.showAllCity = true;
    this.showAllArea = true;
    this.showAllTextCountry = '';

    this.showAllTextCity = '';
    this.showAdditionalTextCity = '';
    this.showAllTextArea = '';
    this.showAdditionalTextArea = '';
    
    this.onClientChange = null;

    this.initialize = function(){            
	    if ((this.countryId) && jQueryObjectExists($("#" + this.countryId) ))
	    {
	        var countryCityTemp = this;
	        var callback = function(result){
	            eNekretnine.CountryCity.populateCountry(result.value, countryCityTemp);
	        }
	        
	        eNekretnine.CountryCity.bindEvents(countryCityTemp);
	        eNekretnine.CountryCity.getAllCountries(countryCityTemp, callback); 
	    }
    }
    
    this.getCountry = function(){
	    if ((this.countryId) && jQueryObjectExists($("#" + this.countryId) ))
	        return $("#" + this.countryId).val();
	    else
	        return '';        
    }
    this.getCity = function(){
	    if ((this.cityId) && jQueryObjectExists($("#" + this.cityId) ))
	        return $("#" + this.cityId).val();
	    else
	        return '';        
    }    
    this.getArea = function(){
	    if ((this.areaId) && jQueryObjectExists($("#" + this.areaId) ))
	        return $("#" + this.areaId).val();
	    else
	        return '';        
    }      
};

eNekretnine.CountryCity={
	getAllCountries:function(countryCity, callback)
	{
	    AIKON.eNekretnineLive.WebServices.WebService.CountryCity_GetCountries(countryCity.portalId, countryCity.countryCityEntity, countryCity.transactionTypeId, callback);
	},
	
    getCitiesByCountry:function(countryCity, country, callback)
	{
	    AIKON.eNekretnineLive.WebServices.WebService.CountryCity_GetCitiesByCountry(countryCity.portalId, countryCity.countryCityEntity, countryCity.transactionTypeId, country, callback);
	},
	
    getAreasByCountryCity:function(countryCity, country, city, callback)
	{
	    AIKON.eNekretnineLive.WebServices.WebService.CountryCity_GetAreasByCountryCity(countryCity.portalId, countryCity.countryCityEntity, countryCity.transactionTypeId, city, callback);
	},	
	
	populateCountry:function(result, countryCity)
	{
        eNekretnine.CountryCity.populateDDL(countryCity.countryId, result, countryCity.defaultCountry, countryCity.showAllCountry, countryCity.showAllTextCountry)

        //if city control exists        
	    if (countryCity.cityId)
	    {
	        var callbackPopulateCity = function(result){
	            eNekretnine.CountryCity.populateCity(result.value, countryCity);
	        }
            eNekretnine.CountryCity.getCitiesByCountry(countryCity, $("#" + countryCity.countryId).val(), callbackPopulateCity);               
        }
	},
	
	populateCity:function(result, countryCity)
	{
        eNekretnine.CountryCity.populateDDL(countryCity.cityId, result, countryCity.defaultCity, countryCity.showAllCity, countryCity.showAllTextCity, countryCity.showAdditionalTextCity)

        //if area control exists        
	    if (countryCity.areaId) {
	        var callbackPopulateArea = function(result){
	            eNekretnine.CountryCity.populateArea(result.value, countryCity);
	        }
            eNekretnine.CountryCity.getAreasByCountryCity(countryCity, $("#" + countryCity.countryId).val(), $("#" + countryCity.cityId).val(), callbackPopulateArea);              
        }        
        else {
            if (countryCity.onClientChange)
                countryCity.onClientChange();
        }
	},
	
	populateArea:function(result, countryCity)
	{
        eNekretnine.CountryCity.populateDDL(countryCity.areaId, result, countryCity.defaultArea, countryCity.showAllArea, countryCity.showAllTextArea, countryCity.showAdditionalTextArea)
        
        if (countryCity.onClientChange)
            countryCity.onClientChange();        
	},	

	bindEvents:function(countryCity)
	{        
        if (countryCity.cityId) eNekretnine.CountryCity.bindCountryEvent(countryCity);
        if (countryCity.areaId || countryCity.onClientChange) eNekretnine.CountryCity.bindCityEvent(countryCity);
    },
	
	bindCountryEvent:function(countryCity)
	{        
        var callback = function(){
            var innerCallback = function(result){
                eNekretnine.CountryCity.populateCity(result.value, countryCity);
            }
        
            eNekretnine.CountryCity.getCitiesByCountry(countryCity, $("#" + countryCity.countryId).val(), innerCallback);
        }
        
        //Common
        var delayed = new xDelayed(countryCity.countryId, callback, 500);
        $("#" + countryCity.countryId).unbind()
        $("#" + countryCity.countryId).bind("change", function(){
            delayed.touch();
        });      
        $("#" + countryCity.countryId).bind("keydown", function(){
            delayed.touch();
        });        
	},	
	
	
	bindCityEvent:function(countryCity)
	{       
	    var callback;
	    if (countryCity.areaId) {
            callback = function(){
                var innerCallback = function(result){
                    eNekretnine.CountryCity.populateArea(result.value, countryCity);
                }
            
                eNekretnine.CountryCity.getAreasByCountryCity(countryCity, $("#" + countryCity.countryId).val(), $("#" + countryCity.cityId).val(), innerCallback);
            }
        }
	    if (countryCity.onClientChange) {
            callback = function(){
                countryCity.onClientChange();
            }
        }        
        
        //Common
        var delayed = new xDelayed(countryCity.cityId, callback, 500);
        $("#" + countryCity.cityId).unbind()
        $("#" + countryCity.cityId).bind("change", function(){
            delayed.touch();
        });      
        $("#" + countryCity.cityId).bind("keydown", function(){
            delayed.touch();
        });        
	},			
	
	populateDDL:function(ddl, nameValues, defaultValue, showAll, showAllText, showAdditionalText)
	{
	    $("#" + ddl).empty();
	    
	    var listHTML = '';
	    
	    if (showAll)
	    {
	        listHTML += '<option value="">' + showAllText + '</option>'
	        if ((showAdditionalText) && (showAdditionalText != '') && nameValues && nameValues.length == 0)
	            listHTML += '<option value="">' + showAdditionalText + '</option>'
	    }
	    
	    if (nameValues){
	        for(var i=0; i< nameValues.length; i++)
	        {
	            var nameValue = nameValues[i];
    	        
	            listHTML += '<option value="' + nameValue.Value + '">' + nameValue.Name + '</option>'
	        }	
	    }
	    $("#" + ddl).append(listHTML);
	    
	    //Common
	    selectOptionByValue($("#" + ddl)[0], defaultValue);
	}
};
