
function regionArrayEntry(id, name, countryid, linkid) {
    this.id = id;
    this.name = name;
    this.countryid = countryid;
    this.linkid = linkid;
}

function userRegionArrayEntry(id, name, countryid) {
    this.id = id;
    this.name = name;
    this.countryid = countryid;
}

function buildRegionArray(id, array) {
    var countryId = 0;
    var lastCountryId = 0;
    for (i=0;i<regionArray.length-1;i++) {
        countryId = regionArray[i].countryid;
        var write = true;
        if (array != null) {
            for (j=0;j<array.length-1;j++) {
                if (array[j].id == regionArray[i].id) {
                    write = false;
                }
            }
        }
        if (write == true) {
            if (countryId != lastCountryId) {
                document.write("<option value='c_" + countryId + "'>" + getCountryName(countryId) + "</option>");
            }
            document.write("<option value='" + regionArray[i].id + "'");
            if (id != null && regionArray[i].id == id) {
                document.write(" selected");
            }
            document.write(">- " + regionArray[i].name + "</option>");
        }
        lastCountryId = countryId;
    }
}

function buildMasterRegionArray(id, array) {
    var countryId = 0;
    var lastCountryId = 0;
    for (i=0;i<masterRegionArray.length-1;i++) {
        countryId = masterRegionArray[i].countryid;
        var write = true;
        if (array != null) {
            for (j=0;j<array.length-1;j++) {
                if (array[j].id == masterRegionArray[i].id) {
                    write = false;
                }
            }
        }
        if (write == true) {
            if (countryId != lastCountryId) {
                document.write("<option value='c_" + countryId + "'>" + getCountryName(countryId) + "</option>");
            }
            document.write("<option value='" + masterRegionArray[i].id + "'");
            if (id != null && masterRegionArray[i].id == id) {
                document.write(" selected");
            }
            document.write(">- " + masterRegionArray[i].name + "</option>");
        }
        lastCountryId = countryId;
    }
}

function getRegionName(id) {
    var name = "";
    for (i=0;i<regionArray.length-1;i++) {
        if (regionArray[i].id == id) {
            name = regionArray[i].name;
        }
    }
    return name;
}

function getRegionCountryId(id) {
    var countryId = "";
    for (i=0;i<regionArray.length-1;i++) {
        if (regionArray[i].id == id) {
            countryId = regionArray[i].countryid;
        }
    }
    return countryId;
}

function buildUserRegionArray() {
    var buffer = "";
    var countryId = 0;
    var lastCountryId = 0;
    for (var i=0;i<userRegionArray.length-1;i++) {
        countryId = userRegionArray[i].countryid;
        if (countryId != lastCountryId) {
            document.write("<option value='c_" + countryId + "'>" + getCountryName(countryId) + "</option>");
        }
        document.write("<option value='" + userRegionArray[i].id + "'>- " + userRegionArray[i].name + "</option>");
        buffer += userRegionArray[i].id + ",";
        lastCountryId = countryId;
    }
    try {
        document.getElementById('regions').value = buffer;
    } catch(err) {}
}

function populateRegions(fieldid, id, regionid) {
    try {
        var selector = document.getElementById(fieldid);
        selector.options.length = 0;
        k = 0;

        if (id == 0) {
            var option = new Option(genericCountrySelect, "0");
        } else {
            var option = new Option(genericDropDownAll, "0");
        }
        selector.options[k++] = option;

        for (var i=0;i<regionArray.length-1;i++) {
            if (regionArray[i].countryid == id) {
                var option = new Option(regionArray[i].name, regionArray[i].id);
                selector.options[k] = option;
                if (regionid != null && regionArray[i].id == regionid) {
                    selector.options[k].selected = true;
                }
                k++;
            }
        }
    } catch(err) {}
}

function populateMasterRegions(fieldid, id, regionid) {
    try {
        var selector = document.getElementById(fieldid);
        selector.options.length = 0;
        k = 0;

        if (id == 0) {
            var option = new Option(genericCountrySelect, "0");
        } else {
            var option = new Option(genericDropDownAll, "0");
        }
        selector.options[k++] = option;

        for (var i=0;i<masterRegionArray.length-1;i++) {
            if (masterRegionArray[i].countryid == id) {
                var option = new Option(masterRegionArray[i].name, masterRegionArray[i].id);
                selector.options[k] = option;
                if (regionid != null && masterRegionArray[i].id == regionid) {
                    selector.options[k].selected = true;
                }
                k++;
            }
        }
    } catch(err) {}
}

function presetRegion(id) {
    populateRegions(document.getElementById('countryId').value, "property");
    if (id != null && id.length > 0) {
        for (i=0;i<document.getElementById('regionId').options.length;i++) {
            if (document.getElementById('regionId').options[i].value == id) {
                document.getElementById('regionId').selectedIndex = i;
            }
        }
    }
}

function RegionValidator() {
    var error = "Some of the details you have entered are incorrect. Please review the following information:\n";

    var name = document.getElementById('name').value;
    if (name == null || name.length < 1) {
        error += "\nName is required";
    }
    var countryid = document.getElementById('countryid').value;
    if (countryid == null || countryid < 1) {
        error += "\nCountry is required";
    }

    if (error != "Some of the details you have entered are incorrect. Please review the following information:\n") { 
        alert(error);
        return false;
    }
    return true;
}