function LocationCardEditFindControls(ctrlid) {
    var ctrls = eval(ctrlid + "_ctrls");
    var ctrl = new Object();

    ctrl.rbDefaultCountry = findObj(ctrls[0]);
    ctrl.rbOtherCountry = findObj(ctrls[1]);
    ctrl.ddlOtherCountry = findObj(ctrls[2]);
    ctrl.mlLocationInfoId = ctrls[3];
    ctrl.hiCountryIndex = findObj(ctrls[4]);
    ctrl.hiEnabled = findObj(ctrls[5]);

    return ctrl;
}

function LocationCardEditGetCountry(ctrlid, countryindex) {
    var arrID = eval(ctrlid + "_country_id");
    var arrAC = eval(ctrlid + "_country_ac");
    var arrAS = eval(ctrlid + "_country_as");
    var arrZL = eval(ctrlid + "_country_zl");
    var arrPM = eval(ctrlid + "_country_pm");
    var arrZNO = eval(ctrlid + "_country_zno");
    var country = new Object();

    country.ID = arrID[countryindex];
    country.AllowCustom = arrAC[countryindex] == "0" ? false : true;
    country.AllowState = arrAS[countryindex] == "0" ? false : true;
    country.ZipLength = parseInt(arrZL[countryindex]);
    country.PhoneMask = arrPM[countryindex];
    country.ZipNumberOnly = arrZNO[countryindex] == "0" ? false : true;

    return country;
}

function LocationCardEditGetCurrentCountryIndex(ctrlid) {
    var ctrls = eval(ctrlid + "_ctrls");
    hiCountryIndex = findObj(ctrls[4]);
    return hiCountryIndex.value;
}

function LocationCardEditCountryChangeExecute(ctrlid, countryindex) {
    var arrScripts = eval(ctrlid + "_country_change");
    var country = LocationCardEditGetCountry(ctrlid, countryindex);

    for (m = 0; m < arrScripts.length - 1; m += 2) {
        func = new Function("ctrlid", "id", "ziplenght", "phonemask", "countryindex", arrScripts[m] + "(ctrlid,id,ziplenght,phonemask,countryindex);");
        func(arrScripts[m + 1], country.ID, country.ZipLength, country.PhoneMask, countryindex);
    }
}

function LocationCardEditSelectDefault(ctrlid, mlLocationInfoId, ddlOtherCountryId) {
    LocationCardEditChangeCountry(ctrlid, mlLocationInfoId, 0);

    var ddlOtherCountry = findObj(ddlOtherCountryId);
    ddlOtherCountry.disabled = "disabled";
}

function LocationCardEditSelectOther(ctrlid, mlLocationInfoId, ddlOtherCountryId) {
    var ddlOtherCountry = findObj(ddlOtherCountryId);
    var init = eval(ctrlid + "_init");
    hasDefaultCountry = init[1];
    indexOffset = init[2];

    index = ddlOtherCountry.selectedIndex - indexOffset;  //hasDefaultCountry ? ddlOtherCountry.selectedIndex+1 : ddlOtherCountry.selectedIndex;

    LocationCardEditChangeCountry(ctrlid, mlLocationInfoId, index);

    ddlOtherCountry.disabled = null;
}

function LocationCardEditChangeZipLength(ctrlid, layerIndex, zipLength, numberOnly) {
    var ctrls = eval(ctrlid + "_info_ctrls");
    LocationInfoEditSetZipLength(ctrls[layerIndex], zipLength, numberOnly);
}

function LocationCardEditGetLayerIndex(ctrlid, country) {
    if (!country.AllowCustom) return 0;
    if (!country.AllowState) return 1;
    return 2;
}

function LocationCardEditChangeCountry(ctrlid, mlLocationInfoId, countryindex) {
    var ctrls = eval(ctrlid + "_ctrls");
    var country = LocationCardEditGetCountry(ctrlid, countryindex);
    var layerIndex = LocationCardEditGetLayerIndex(ctrlid, country);

    changeLayer(mlLocationInfoId, layerIndex);
    LocationCardEditChangeZipLength(ctrlid, layerIndex, country.ZipLength, country.ZipNumberOnly);
    LocationCardEditCountryChangeExecute(ctrlid, countryindex);

    var hiCountryIndex = findObj(ctrls[4]);
    hiCountryIndex.value = countryindex;

    return layerIndex;
}

function LocationCardEditChangeEnabledInternal(ctrlid, enabled) {
    var ctrl = LocationCardEditFindControls(ctrlid);
    var init = eval(ctrlid + "_init");
    hasDefaultCountry = init[1];

    LocationInfoEditControlChangeEnabled(ctrl.rbDefaultCountry, enabled);
    LocationInfoEditControlChangeEnabled(ctrl.rbOtherCountry, enabled);

    if (hasDefaultCountry)
        LocationInfoEditControlChangeEnabled(ctrl.ddlOtherCountry, enabled && ctrl.hiCountryIndex.value != 0);
    else
        LocationInfoEditControlChangeEnabled(ctrl.ddlOtherCountry, enabled);

    ctrl.hiEnabled.value = enabled;
}

function LocationCardEditChangeEnabled(ctrlid, enabled) {
    var init = eval(ctrlid + "_init");

    LocationCardEditChangeEnabledInternal(ctrlid, init[0] && enabled);

    var ctrls = eval(ctrlid + "_info_ctrls");
    LocationInfoEditChangeEnabled(ctrls[0], init[0] && enabled);
    LocationInfoEditChangeEnabled(ctrls[1], init[0] && enabled);
    LocationInfoEditChangeEnabled(ctrls[2], init[0] && enabled);
}

function LocationCardEditStartUp(ctrlid, enabled) {
    var ctrl = LocationCardEditFindControls(ctrlid);
    var init = eval(ctrlid + "_init");

    LocationCardEditChangeCountry(ctrlid, ctrl.mlLocationInfoId, ctrl.hiCountryIndex.value);

    LocationCardEditChangeEnabledInternal(ctrlid, init[0] && enabled);

    var ctrls = eval(ctrlid + "_info_ctrls");
    LocationInfoEditStartUp(ctrls[0], init[0] && enabled);
    LocationInfoEditStartUp(ctrls[1], init[0] && enabled);
    LocationInfoEditStartUp(ctrls[2], init[0] && enabled);
}

function LocationCardEditExtractInfo(ctrlid) {
    var info_ctrls = eval(ctrlid + "_info_ctrls");
    var ctrl = LocationCardEditFindControls(ctrlid);
    var country = LocationCardEditGetCountry(ctrlid, ctrl.hiCountryIndex.value);
    var layerIndex = LocationCardEditGetLayerIndex(ctrlid, country);

    var info = new Array(ctrl.hiCountryIndex.value);

    return info.concat(LocationInfoEditExtractInfo(info_ctrls[layerIndex], layerIndex));
}

function LocationCardEditSetInfo(ctrlid, info) {
    var info_ctrls = eval(ctrlid + "_info_ctrls");
    var ctrl = LocationCardEditFindControls(ctrlid);
    var countryindex = parseInt(info.shift());
    var init = eval(ctrlid + "_init");
    hasDefaultCountry = init[1];
    indexOffset = init[2];

    var layerIndex = LocationCardEditChangeCountry(ctrlid, ctrl.mlLocationInfoId, countryindex);

    if (ctrl.rbDefaultCountry != null) ctrl.rbDefaultCountry.checked = countryindex == 0;
    if (ctrl.rbOtherCountry != null) ctrl.rbOtherCountry.checked = countryindex != 0;
    if (ctrl.ddlOtherCountry != null) {

        if (hasDefaultCountry) {
            ctrl.ddlOtherCountry.selectedIndex = countryindex != 0 ? countryindex + indexOffset : 0;
        } else {
            selectedIndex = countryindex + indexOffset;

            ctrl.ddlOtherCountry.selectedIndex = selectedIndex;
        }

        if (ctrl.hiEnabled.value == "true" && (!hasDefaultCountry || countryindex > 0)) {
            ctrl.ddlOtherCountry.disabled = null;
        } else {
            ctrl.ddlOtherCountry.disabled = "disabled";
        }
    }

    LocationInfoEditSetInfo(info_ctrls[layerIndex], info);
}

function OnCountryChangeTest(ctrlid, id, ziplenght, phonemask, countryindex) {
    alert(ctrlid + " " + phonemask);
}