
function resortArrayEntry(id, name, regionid, text1, text2, text3, text4, special, specialtext, specialexpiry, templateid, feature, pricingtext) {
    this.id = id;
    this.name = name;
    this.regionid = regionid;
    this.text1 = text1;
    this.text2 = text2;
    this.text3 = text3;
    this.text4 = text4;
    this.special = special;
    this.specialtext = specialtext;
    this.specialexpiry = specialexpiry;
    this.templateid = templateid;
    this.feature = feature;
    this.pricingtext = pricingtext;
}

function buildResortArray(countryid) {
    document.write("<table id='list' border='0' cellpadding='2' cellspacing='0'>");
    var j = 0;
    for (var i=0;i<resortArray.length-1;i++) {
        if (countryid == 0 || countryid == getCountryId(resortArray[i].regionid)) {
            j++;
            var resortName = resortArray[i].name;
            if (resortName.length > 20) {
                resortName = resortName.substring(0,20) + "..";
            }
            var regionName = getRegion(resortArray[i].regionid);
            if (regionName.length > 15) {
                regionName = regionName.substring(0,15) + "..";
            }
            document.write("<tr onClick=\"populateResortForm('" + resortArray[i].id + "')\" onMouseover=\"this.className='listDown'\" onMouseout=\"this.className='listUp'\">" + 
                           "<td width='*' nowrap>" + resortName + "</td>" + 
                           "<td width='100%' align='right' nowrap>" + regionName + "</td>" + 
                           "</tr>");
        }
    }
    if (j < 1) {
        document.write("<tr><td colspan='2' class='error'>There are no results to display</td></tr>");
    }
    document.write("</table>");
}

function buildResortSelectArray(id) {
    for (var i=0;i<resortArray.length-1;i++) {
        document.write("<option value='" + resortArray[i].id + "'");
        if (id != null && resortArray[i].id == id) {
            document.write(" selected");
        }
        document.write(">" + resortArray[i].name + "</option>");
    }
}

function populateResorts(id, type) {
    var k = 0;
    var prefix = "";

    if (type == "form") {
        var selector = document.getElementById('resortid');
        selector.options.length = 0;
        var option = new Option("Please select a Resort...", "0");
        selector.options[k++] = option;
    }
    if (type == "search") {
        var selector = document.getElementById('search_resortid');
        selector.options.length = 0;
        var option = new Option("Please select a Resort", "0");
        selector.options[k++] = option;
    }
    if (type == "list") {
        var selector = document.getElementById('nav_resortid');
        selector.options.length = 0;
        var option = new Option("Please select a Resort...", "0");
        selector.options[k++] = option;
        option.className = "header";
        prefix = "- ";
        document.getElementById('nav_country').className = "disabled";
        document.getElementById('nav_region').className = "disabled";
        document.getElementById('nav_resort').className = "active";
    }

    for (var i=0;i<resortArray.length-1;i++) {
        if (resortArray[i].regionid == id) {
            var option = new Option(prefix + resortArray[i].name, resortArray[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<resortArray.length-1;i++) {
        if (resortArray[i].id == id) {
            regionId = resortArray[i].regionid;
        }
    }
    return regionId;
}

function populateResortForm(id) {
    for (var i=0;i<resortArray.length-1;i++) {
        if (resortArray[i].id == id) {
            populateRegions(getCountryId(resortArray[i].regionid));
            document.getElementById('id').value = resortArray[i].id;
            document.getElementById('name').value = resortArray[i].name;
            document.getElementById('text1').value = resortArray[i].text1;
            document.getElementById('text2').value = resortArray[i].text2;
            document.getElementById('text3').value = resortArray[i].text3;
            document.getElementById('text4').value = resortArray[i].text4;
            document.getElementById('sp_text').value = resortArray[i].specialtext;

            var specialexpiry = resortArray[i].specialexpiry;
            if (specialexpiry.length > 10) {
                specialexpiry = specialexpiry.substring(0,10);
            }
            document.getElementById('sp_expiry').value = specialexpiry;
            document.getElementById('pricingtext').value = resortArray[i].pricingtext;

            for (j=0;j<document.getElementById('countryid').length;j++) {
                if (document.getElementById('countryid').options[j].value == getCountryId(resortArray[i].regionid)) {
                   document.getElementById('countryid').selectedIndex = j;
                }
            }
            for (j=0;j<document.getElementById('regionid').length;j++) {
                if (document.getElementById('regionid').options[j].value == resortArray[i].regionid) {
                   document.getElementById('regionid').selectedIndex = j;
                }
            }
            for (j=0;j<document.getElementById('templateid').length;j++) {
                if (document.getElementById('templateid').options[j].value == resortArray[i].templateid) {
                   document.getElementById('templateid').selectedIndex = j;
                }
            }

            if (resortArray[i].feature == 1) {
                document.getElementById('feature').checked = true;
            } else {
                document.getElementById('feature').checked = false;
            }
            if (resortArray[i].special == 1) {
                document.getElementById('special').checked = true;
            } else {
                document.getElementById('special').checked = false;
            }

            buildUploadArray(id);
            buildFacilityArray(id);
            // buttons
            document.getElementById('add').style.display = "none";
            document.getElementById('reset').style.display = "none";
            document.getElementById('update').style.display = "block";
            document.getElementById('delete').style.display = "block";
            document.getElementById('new').style.display = "block";
            // file upload
            //document.getElementById('file').style.display = "block";
            // pricing
            document.getElementById('pricingDiv1').style.display = "block";
            document.getElementById('pricingDiv2').style.display = "none";

            // resort link
            document.getElementById('resortlink').innerHTML = "<a href='resort.jsp?resortid=" + id + "'>View Resort (" + id + ")</a>";
            
            // delete uploads link
            if (document.getElementById('uploads').options[0].value.indexOf('/dp/') != -1) {
                uploadChar1 = document.getElementById('uploads').options[0].value.substring(15,16);
                document.getElementById('uploadslink').innerHTML = "<a href='admin/uploads.jsp?char1=" + uploadChar1 + "'>Click here to delete images</a>";
            }

        }
    }
}

function openPricingWindow() {
    var id = document.getElementById('id').value;
    window.open('resorts_pricing.jsp?resortid=' + id,'pricingtable','width=500, height=400, toolbars=no, scrolling=yes, scrollbars=yes, status=no, top=50, left=50');
}

function populatePrice(id, value) {
    document.forms['form' + id].price.value = value;
}

function openPeriodWindow() {
    var id = document.getElementById('id').value;
    window.open('resorts_periods.jsp?resortid=' + id,'pricingperiods','width=500, height=400, toolbars=no, scrolling=yes, scrollbars=yes, status=no, top=50, left=50');
}

function deletePricingTable() {
    if (confirm("Are you sure you want to delete the pricing table for this resort?")) {
        return true;
    } else {
        return false;
    }
}

function deletePeriodTable() {
    if (confirm("Are you sure you want to delete the pricing periods for this resort?")) {
        return true;
    } else {
        return false;
    }
}

function setResort(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(document.getElementById('countryid').value, 'form');
        populateNameValue('countryid');

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

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

function buildResortList(id) {
    var r = 0;
    var buffer = "";
    //if (id != null) {
        for (var k=0;k<resortArray.length;k++) {
            if (resortArray[k] != null && (id == null || id == resortArray[k].regionid)) {
                r++;
                buffer += "<a class=\"list\" href=\"" + url + "resort.jsp?id=" + resortArray[k].id + "\">" + resortArray[k].name.substring(0,26) + "</a>\n";
            }
        }
        if (r == 0) {
            buffer = "We are currently working on<br>this section of the website." + 
                     "<br><br>Please call us on 01608<br>738286 or complete our<br><a href='contact.jsp'>Contact Request</a> form for<br>more information.";
        }
    //} else {
    //    buffer = "Please select a country and<br>region to populate this list.";
    //}
    document.getElementById('nav_resort_list').innerHTML = buffer;
}