
function propertyArrayEntry(id, name, regionid) {
    this.id = id;
    this.name = name;
    this.regionid = regionid;
}

function populateProperties(id, page) {
    var selector = document.getElementById('propertyid');
    selector.options.length = 0;
    k = 0;
    if (page != null && page == "form") {
        var option = new Option(genericPropertySelect, "0");
        selector.options[k++] = option;
    }
    for (var i=0;i<propertyArray.length-1;i++) {
        if (propertyArray[i].regionid == id) {
            var option = new Option(propertyArray[i].name, propertyArray[i].id);
            selector.options[k++] = option;
        }
    }
}

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

function getCountryId(id) {
    var name = 0;
    for (var i=0;i<regionArray.length-1;i++) {
        if (regionArray[i].id == id) {
            id = regionArray[i].countryid;
            break;
        }
    }
    for (var j=0;j<countryArray.length-1;j++) {
        if (countryArray[j].id == id) {
            name = countryArray[j].id;
            break;
        }
    }
    return name;
}

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

function getRegionId(id) {
    var regionId = 0;
    for (var i=0;i<propertyArray.length-1;i++) {
        if (propertyArray[i].id == id) {
            regionId = propertyArray[i].regionid;
        }
    }
    return regionId;
}

function setProperty(id) {
    if (id != null && id > 0) {
        var regionId = getRegionId(id);

        for (j=0;j<document.getElementById('countryid').length;j++) {
            if (document.getElementById('countryid').options[j].value == getCountryId(regionId)) {
               document.getElementById('countryid').selectedIndex = j;
            }
        }
        populateRegions('regionId', document.getElementById('countryid').value, regionId);
        populateNameValue('countryid');

        for (j=0;j<document.getElementById('regionid').length;j++) {
            if (document.getElementById('regionid').options[j].value == regionId) {
               document.getElementById('regionid').selectedIndex = j;
            }
        }
        populateProperties(document.getElementById('regionid').value, 'form');
        populateNameValue('regionid');

        for (j=0;j<document.getElementById('propertyid').length;j++) {
            if (document.getElementById('propertyid').options[j].value == id) {
               document.getElementById('propertyid').selectedIndex = j;
            }
        }
        populateNameValue('propertyid');
    }
}

function newPropertyLoad(id) {
      parent.frames[1].location.href = "uploadadmin.jsp?type=Property&id=" + id;
      parent.frames[2].location.href = "roomadmin.jsp?id=" + id;
      parent.frames[3].location.href = "infoadmin.jsp?id=" + id;
}

function changeMode() {
    if (document.getElementById('mode').checked) {
        document.getElementById('country').style.display = "block";
        document.getElementById('region').style.display = "block";
        document.getElementById('countryid').style.display = "none";
        document.getElementById('regionid').style.display = "none";
    } else {
        document.getElementById('country').style.display = "none";
        document.getElementById('region').style.display = "none";
        document.getElementById('countryid').style.display = "block";
        document.getElementById('regionid').style.display = "block";
    }
}

function changeMasterMode() {
    if (document.getElementById('mastermode').checked) {
        document.getElementById('mastercountry').style.display = "block";
        document.getElementById('masterregion').style.display = "block";
        document.getElementById('mastercountryid').style.display = "none";
        document.getElementById('masterregionid').style.display = "none";
    } else {
        document.getElementById('mastercountry').style.display = "none";
        document.getElementById('masterregion').style.display = "none";
        document.getElementById('mastercountryid').style.display = "block";
        document.getElementById('masterregionid').style.display = "block";
    }
}

function PropertyValidator() {
    var error = "Some of the details you have entered are incorrect. Please review the following information:\n";
    var mode = document.getElementById('mode').checked;

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

    if (mode) {
        var country = document.getElementById('country').value;
        if (country == null || country < 1) {
            error += "\nCountry is required";
        }
        var region = document.getElementById('region').value;
        if (region == null || region < 1) {
            error += "\nRegion is required";
        }
    } else {
        var countryid = document.getElementById('countryid').value;
        if (countryid == null || countryid < 1) {
            error += "\nCountry is required";
        }
        var regionid = document.getElementById('regionid').value;
        if (regionid == null || regionid < 1) {
            error += "\nRegion is required";
        }
    }

    var typeid = document.getElementById('typeid').value;
    if (typeid == null || typeid < 1) {
        error += "\nType is required";
    }

    var plotarea = document.getElementById('plotarea').value;
    if (!isNumeric(plotarea)) {
        error += "\nPlot Area must be numeric";
    }
    var builtarea = document.getElementById('builtarea').value;
    if (!isNumeric(builtarea)) {
        error += "\nBuilt Area must be numeric";
    }
//    var bedrooms = document.getElementById('bedrooms').value;
//    if (!isNumeric(bedrooms)) {
//        error += "\nBedrooms must be numeric";
//    }
    var bathrooms = document.getElementById('bathrooms').value;
    if (!isNumeric(bathrooms)) {
        error += "\nBathrooms must be numeric";
    }
    var price = document.getElementById('price').value;
    if (!isNumeric(price)) {
        error += "\nPrice must be numeric";
    }

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