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

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

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

function getCountryName(id) {
    var retVal = "";
    for (x=0;x<countryArray.length-1;x++) {
        if (countryArray[x].id == id) {
            retVal = countryArray[x].name;
        }
    }
    return retVal;
}

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

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

function buildCountryTreeArray() {
    for (i=0;i<countryArray.length-1;i++) {
        var countryId = countryArray[i].id;
        document.write("<div class='country' id='country" + countryId + "' name='country" + countryId + "'>" + 
                       "<a href=\"javascript:toggleRegions('" + countryId + "')\">" + 
                       "<img id='countryimage" + countryId + "' name='countryimage" + countryId + "' src='images/show.gif' border='0'>" + countryArray[i].name + 
                       "</a>" + 
                       "<span class='region' id='regions" + countryId + "' name='regions" + countryId + "' style='display:none'>" + 
                       buildRegionTreeArray(countryId) + 
                       "</span>" + 
                       "</div>\n");
    }
}

function buildCountryListArray() {
    for (i=0;i<countryArray.length-1;i++) {
        var countryId = countryArray[i].id;
        document.write("<p class='header'>" + countryArray[i].name + "</p>" + buildRegionListArray(countryId) + "<br><br>\n");
    }
}

function buildRegionTreeArray(countryId) {
    var k = 0;
    var buffer = "";
    for (j=0;j<regionArray.length-1;j++) {
        if (regionArray[j].countryid == countryId) {
            k++;
            regionId = regionArray[j].id;
            buffer += "<a href='javascript:toggleResorts(\"" + regionId + "\")'>" + 
                      "<img id='regionimage" + regionId + "' name='regionimage" + regionId + "' src='images/show.gif' border='0'>" + regionArray[j].name + 
                      "</a>";
            buffer += "<span class='resort' id='resorts" + regionId + "' name='resorts" + regionId + "' style='display:none'>" + 
                      buildResortTreeArray(regionId) + 
                      "</span>\n";
        }
    }
    if (k < 1) {
        buffer = "There are no Regions to display for this Country";
    }
    return buffer;
}

function buildRegionListArray(countryId) {
    var buffer = "";
    for (j=0;j<regionArray.length-1;j++) {
        if (regionArray[j].countryid == countryId) {
            regionId = regionArray[j].id;
            buffer += "<br><b>" + regionArray[j].name + ":</b> " + buildResortListArray(regionId) + "\n";
        }
    }
    return buffer;
}

function buildResortTreeArray(regionId) {
    var m = 0;
    var buffer = "";
    for (l=0;l<resortArray.length-1;l++) {
        if (resortArray[l].regionid == regionId) {
            m++;
            buffer += "<a href='resort.jsp?resortid=" + resortArray[l].id + "'>" + 
                      "<img src='images/resort.gif' border='0'>" + resortArray[l].name + 
                      "</a>\n";
        }
    }
    if (m < 1) {
        buffer = "There are no Resorts to display for this Region";
    }
    return buffer;
}

function buildResortListArray(regionId) {
    var buffer = "";
    for (l=0;l<resortArray.length-1;l++) {
        if (resortArray[l].regionid == regionId) {
            buffer += "<a href='resort.jsp?id=" + resortArray[l].id + "' alt=\"" + resortArray[l].name + " - " + getRegionName(resortArray[l].regionid) + ", " + getCountryName(getRegionCountryId(resortArray[l].regionid)) + "\" title=\"" + resortArray[l].name + " - " + getRegionName(resortArray[l].regionid) + ", " + getCountryName(getRegionCountryId(resortArray[l].regionid)) + "\">" + resortArray[l].name + "</a>&nbsp;|\n";
        }
    }
    return buffer + "<a href='" + url + "search/search1.jsp?regionid=" + regionId + "' alt=\"" + getRegionName(regionId) + ", " + getCountryName(getRegionCountryId(regionId)) + "\" title=\"" + getRegionName(regionId) + ", " + getCountryName(getRegionCountryId(regionId)) + "\">View as Search</a>";
}

function toggleRegions(countryId) {
    if (document.getElementById('regions' + countryId).style.display == "none") {
        document.getElementById('regions' + countryId).style.display = "block";
        document.getElementById('countryimage' + countryId).src = "images/hide.gif";
    } else {
        document.getElementById('regions' + countryId).style.display = "none";
        document.getElementById('countryimage' + countryId).src = "images/show.gif";
    }
}

function toggleResorts(regionId) {
    if (document.getElementById('resorts' + regionId).style.display == "none") {
        document.getElementById('resorts' + regionId).style.display = "block";
        document.getElementById('regionimage' + regionId).src = "images/hide.gif";
    } else {
        document.getElementById('resorts' + regionId).style.display = "none";
        document.getElementById('regionimage' + regionId).src = "images/show.gif";
    }
}

function expandTree(countryId, regionId) {
    toggleRegions(countryId);
    toggleResorts(regionId);
}

function validateText(text) {
    text = text.replace("'"," ");
    return text;
}

