﻿function onChangeCountry(ddlCountriesId, cultureId, ddlProvinceStateClientID, hdnStateItemsClienID,
                            stateLableClientID, otherStateClientID, hdnSelectedStateClientID,
                            ddlCityClientID, hdnCityItemsClienID, cityLableClientID, otherCityClientID,
                            hdnSelectedCityClientID, hdnSelectedCountryClienID,hdnChangedCoutry)
{
    var CountriesList = document.getElementById(ddlCountriesId);
    if (CountriesList != null)
    {
        saveSelectedCountry(CountriesList, document.getElementById(hdnSelectedCountryClienID));
        var country_id = CountriesList.options[CountriesList.selectedIndex].value;
        
        GeneralServices.GetProvinceState(country_id, cultureId, ddlProvinceStateClientID, hdnStateItemsClienID,
        stateLableClientID, otherStateClientID, hdnSelectedStateClientID, onStateCitySuccess);
        
        GeneralServices.GetCity(country_id, cultureId, ddlCityClientID, hdnCityItemsClienID,
        cityLableClientID, otherCityClientID, hdnSelectedCityClientID, onStateCitySuccess);
        if(hdnChangedCoutry != null)
        {
            hdnChangedCoutry.value = "true";
        }
    }
}

function onStateCitySuccess(result)
{
    if (result != null && result.length > 0)
    {
        var ddl = $get(result[result.length-1].split(";")[0]);
        var stateCityItems = $get(result[result.length-1].split(";")[1]);
        var stateCityLable = $get(result[result.length-1].split(";")[2]);
        var otherBox = $get(result[result.length-1].split(";")[3]);
        var selectedStateCity = $get(result[result.length-1].split(";")[4]);
        
        if (result.length <= 1)
        {
            ddl.style.display = "none";
            stateCityLable.style.display = "none";
            otherBox.style.display = "none";
            ddl.selectedIndex = -1;
            return;
        }
        
        stateCityItems.value = "";
        ddl.style.display = "block";
        stateCityLable.style.display = "block";
        otherBox.style.display = "block";
        ddl.options.length = 0;
        for (i = 0; i < result.length - 1; i++)
        {
            var item = result[i].split(";");
            var opt = document.createElement("option");
            ddl.options.add(opt);    
            opt.value = item[0];
            opt.text = item[1];
            if (stateCityItems != null)
            {
                stateCityItems.value += opt.text + "&" + opt.value + ";";
            }
        }
        saveSelectedStateCity(ddl, otherBox, selectedStateCity);
   }
}

function saveSelectedCountry(countriesList, selectedCountryHdn)
{
    if (countriesList != null && selectedCountryHdn != null)
    {
        selectedCountryHdn.value = countriesList.options[countriesList.selectedIndex].value;
    }
}

function saveSelectedStateCity(statesCitiesList, tbOther, selectedStateCityHdn)
{
    if (statesCitiesList != null)
    {
        var stateCity_value;
        if (statesCitiesList.selectedIndex == -1)
        {
            stateCity_value = "-1";
        }
        else
        {
            stateCity_value = statesCitiesList.options[statesCitiesList.selectedIndex].value;
        }
        
        if (selectedStateCityHdn != null)
        {
            selectedStateCityHdn.value = stateCity_value;
        }
        if (tbOther != null)
        {
            if (stateCity_value == "-1")
            {
                tbOther.style.display = "block";
            }
            else
            {
                tbOther.style.display = "none";
            }
            tbOther.value = "";
        }
    }
}

function SwitchOtherFieldVisibility(tbOther, ddlList, rfvValidator, enableValidator)
{
    if (tbOther != null && ddlList != null)
    {
        if (ddlList.value == "-1")
        {
            tbOther.style.display = "inline";
            if (rfvValidator != null && enableValidator == "True")
                ValidatorEnable(rfvValidator, true);
        }
        else
        {
            tbOther.style.display = "none";
            tbOther.value = "";
            if (rfvValidator != null)
                ValidatorEnable(rfvValidator, false);
        }
    }
}

function UpdateMapCityField_ddl(tbMapCity, ddlCity)
{
    if (tbMapCity != null && tbMapCity != "" && ddlCity != null)
        tbMapCity.value = (ddlCity.value == "-1") ? "" : ddlCity[ddlCity.selectedIndex].text;
}

function UpdateMapCityField_txt(tbMapCity, tbOtherCity)
{
    if (tbMapCity != null && tbMapCity != "" && tbOtherCity != null)
        tbMapCity.value = tbOtherCity.value;
}

function loadSelectedValues(ddlCountriesId, cultureId, ddlProvinceStateClientID, hdnStateItemsClienID,
                            stateLableClientID, otherStateClientID, hdnSelectedStateClientID,
                            ddlCityClientID, hdnCityItemsClienID, cityLableClientID, otherCityClientID,
                            hdnSelectedCityClientID, hdnSelectedCountryClienID)
{
    var CountriesList = document.getElementById(ddlCountriesId);
    if (CountriesList != null)
    {
        saveSelectedCountry(CountriesList, document.getElementById(hdnSelectedCountryClienID));
        var country_id = CountriesList.options[CountriesList.selectedIndex].value;
        
        GeneralServices.GetProvinceState(country_id, cultureId, ddlProvinceStateClientID, hdnStateItemsClienID,
        stateLableClientID, otherStateClientID, hdnSelectedStateClientID, onStateCityLoadSuccess);
        
        GeneralServices.GetCity(country_id, cultureId, ddlCityClientID, hdnCityItemsClienID,
        cityLableClientID, otherCityClientID, hdnSelectedCityClientID, onStateCityLoadSuccess);
    }
}


function onStateCityLoadSuccess(result)
{
    if (result != null && result.length > 0)
    {
        var ddl = $get(result[result.length-1].split(";")[0]);
        var stateCityItems = $get(result[result.length-1].split(";")[1]);
        var stateCityLable = $get(result[result.length-1].split(";")[2]);
        var otherBox = $get(result[result.length-1].split(";")[3]);
        var selectedStateCity = $get(result[result.length-1].split(";")[4]);
        
        if (result.length <= 1)
        {
            ddl.style.display = "none";
            stateCityLable.style.display = "none";
            otherBox.style.display = "none";
            ddl.selectedIndex = -1;
            return;
        }
        
        stateCityItems.value = "";
        ddl.style.display = "block";
        stateCityLable.style.display = "block";
        otherBox.style.display = "block";
        ddl.options.length = 0;
        for (i = 0; i < result.length - 1; i++)
        {
            var item = result[i].split(";");
            var opt = document.createElement("option");
            ddl.options.add(opt);    
            opt.value = item[0];
            opt.text = item[1];
            if (stateCityItems != null)
            {
                stateCityItems.value += opt.text + "&" + opt.value + ";";
            }
        }
        loadSelectedStateCity(ddl, otherBox, selectedStateCity);
        saveSelectedStateCity(ddl, otherBox, selectedStateCity);
   }
}

function loadSelectedStateCity(statesCitiesList, tbOther, selectedStateCityHdn)
{
    if (statesCitiesList != null)
    {
        var stateCity_value; 
        if (selectedStateCityHdn != null && tbOther != null)
        {
            if(selectedStateCityHdn.value != null)
            {
                statesCitiesList.value = selectedStateCityHdn.value;
                stateCity_value = selectedStateCityHdn.value;
            }
        }
        if (stateCity_value != null)
        {
            if (tbOther != null)
            {
                if (stateCity_value == "-1")
                {
                    tbOther.style.display = "block";
                }
                else
                {
                    tbOther.style.display = "none";
                }
                tbOther.value = statesCitiesList.options[statesCitiesList.selectedIndex].text;
            }
        }
    }
}

function ValidateCountrySelection(sender, arguments)
{
	var ddlCountries = newFind('ddlCountries');
	if (ddlCountries != null)
	{
		var iValue = new Number(ddlCountries[ddlCountries.selectedIndex].value);
		arguments.IsValid=(iValue != -2);
	}
	else
	{
		arguments.IsValid = false;
	}
}							
	

function newFind(name)
{
    var elems = document.getElementsByTagName('*'); 
    var num = elems.length; 
    for(var c=0;c<num;c++)
    {
        var val = elems[c].getAttribute('FISName'); 
        if(val!=null && val==name)
            return elems[c]; 
    }
}