//var rbcHref = "http://communitycorner.mapanswer.com/";
var rbcHref = "/cgi-bin/findingyourhome/";

function trim(str) {
    return str.replace(/^\s*|\s*$/g,"");
}
function resetSearchDistance() {
    var distances = document.getElementById('searchDistance');
    if (distances) {
        distances.selectedIndex = 2;
    }
}
function setRadioBtn(proximityType) {
    var radioBtns = document.getElementsByName('proximityType');
    if (proximityType == 'WORKPLACE') {
        clearTextElement('currentPostalCode');
        document.getElementById('amenitySelection').options.selectedIndex = 0;
    }
    if (proximityType == 'AMENITIES') {
        radioBtns[1].click();
        clearTextElement('currentPostalCode');
    }
    if (proximityType == 'NEIGHBOURHOOD') {
        radioBtns[2].click();
        document.getElementById('amenitySelection').options.selectedIndex = 0;
    }
    if (proximityType == 'NEW_SCHOOL') {
        document.getElementById('amenitySelection').options.selectedIndex = 0;
    }
    if (proximityType == 'PUBLIC_TRANSIT') {
        document.getElementById('amenitySelection').options.selectedIndex = 0;
    }
}
function clearTextElement(textId) {
    var textElement = document.getElementById(textId);
    if (textElement) {
        textElement.value = '';
    }
}
function setHousingType(housingType) {
    var radioBtns = document.getElementsByName('housingType');
    var sel;
    if (housingType == 'BUY') {
        radioBtns[0].click();
        sel = document.getElementById('rentPaymentRange');
    }
    if (housingType == 'RENT') {
        radioBtns[1].click();
        sel = document.getElementById('houseValueRange');
    }
    sel.options.selectedIndex = 0;
}
function clearProvinceAndCity(selectId, cityId){
    var sel = document.getElementById(selectId);
    if (sel) {
        sel.options.selectedIndex = 0;
    }
    sel = document.getElementById(cityId);
    if (sel) {
        clearOptions(sel);
    }
}
function clearPostalCode(elId){
    var el = document.getElementById(elId);
    if (el) {
        el.value = '';
    }
}
function clearAllFields(btn, defaultText) {
    var aform = btn.form;
    for (var i=0; i<aform.elements.length; i++) {
        var el = aform.elements[i];
        if (el.className && el.className.indexOf("clearable") != -1 ) {
            if (el.type == 'select-one') {  // or select-multiple
                el.options.selectedIndex = 0;
            } else if (el.type ==  'text') { // or textarea
                if (defaultText) {
                    el.value = defaultText;
                } else {
                    el.value = '';
                }
            } else if (el.type == 'radio'){
                el.click();
            } else if (el.type == 'checkbox' && el.checked) {
                el.click();
            }
        }
    }
    clearCityLists();
    resetSearchDistance();
}
function clearCityLists() {
    var cityEl = document.getElementById('city1');
    if (cityEl) {
        clearOptions(cityEl);
    }
    cityEl = document.getElementById('city2');
    if (cityEl) {
        clearOptions(cityEl);
    } 
    cityEl = document.getElementById('city3');
    if (cityEl) {
        clearOptions(cityEl);
    } 
}
function clearOptions(cityEl) {
    var firstOption = new Option('','');
    firstOption.selected;
    cityEl.length=1;
    cityEl.options[0]=firstOption;
    cityEl.options.selectedIndex = 0;
    cityEl.value = '';   
}
function updateMunicipalityDropDown(provinceSelectId, citySelectId) {
    var provinceSelect = document.getElementById(provinceSelectId);
    var provinceCode = provinceSelect.options[provinceSelect.options.selectedIndex].value;
    if (provinceCode == "") {        
        var citySelect = document.getElementById(citySelectId);
        citySelect.options.length=1;
        var option = new Option();
        option.value = '';
        option.text = '';
        citySelect.options[0]=option;
        citySelect.selectedIndex=0;
    } else {
        var getMunicipalitiesRequest = createXmlHttpRequest();
        getMunicipalitiesRequest.open("GET", rbcHref + "GetMunicipalities.cgi?provinceCode=" + provinceCode,false);
        getMunicipalitiesRequest.send(null);
        
        var citySelect = document.getElementById(citySelectId);
        citySelect.options.length=0;
        
        var dataOptions = getMunicipalitiesRequest.responseText.split('|');
        for(i=0;i<dataOptions.length;i++){
            var option = new Option();
            
            option.value = dataOptions[i++];
            option.text = dataOptions[i];
            citySelect.options[citySelect.length]=option;
        }  
    }
}

function initializeMunicipalityDropDown(citySelectId, provinceCode, selectedValue) {
    if (provinceCode == "") {        
        var citySelect = document.getElementById(citySelectId);
        citySelect.options.length=0;
    } else {
        var getMunicipalitiesRequest = createXmlHttpRequest();
        getMunicipalitiesRequest.open("GET", rbcHref + "GetMunicipalities.cgi?provinceCode=" + provinceCode,false);
        getMunicipalitiesRequest.send(null);
        var citySelect = document.getElementById(citySelectId);
        citySelect.options.length=0;
        
        var dataOptions = getMunicipalitiesRequest.responseText.split('|');
        for(i=0;i<dataOptions.length;i++){
            var option = new Option();
            
            option.value = dataOptions[i++];
            option.text = dataOptions[i];
            if(selectedValue != null && selectedValue == option.value){
                option.selected = true;
            }    
            citySelect.options[citySelect.length]=option;
        }  
    }
}