var initialised = 0;

function initRegionAddition(){
    disable_add('regionarea');

    locationText = document.getElementById('id_location_text');
    locationSearchDiv = document.getElementById('locationSelect');

    createMainRegion();
    createRegion();
    createArea();

    addChangeHandler('id_location_main_region', 'regionShowHideAdd();');
    addChangeHandler('id_location_area', 'resetText();');
    addChangeHandler('id_location_main_region', 'disable_add(\'area\');');
    addChangeHandler('id_location_region', 'disable_add(\'regions\');');
    addChangeHandler('id_location_area', 'disable_add(\'all\');');

    mainRegionSelect = document.getElementById('id_location_main_region');
    mainRegionSelect.options.length = 0;

    for (var i=0; i<mainRegions.length; i++){
	    mainRegionSelect.options[i] = new Option(mainRegions[i][1], mainRegions[i][0]);
        if(mr_selected == mainRegions[i][0]){
            mainRegionSelect.options[i].selected = true
        }
    }
    resetArea();

    if(mr_selected != 0){
        regionShowHideAdd();
    }else{
        disable_add('regionarea');
    }

    addChangeHandler('id_location_main_region', 'disable_add(\'area\');');
    addChangeHandler('id_location_region', 'disable_add(\'regions\');');
    addChangeHandler('id_location_area', 'disable_add(\'all\');');

}

function disable_add(add_type){
    if(add_type == 'all'){
        $('new_main_region').disabled = true;
        $('new_region').disabled = true;
        $('new_area').disabled = true;
    }else if(add_type == 'area'){
        $('new_main_region').disabled = false;
        $('new_region').disabled = false;
        $('new_area').disabled = true;
    }else if(add_type == 'regions'){
        $('new_main_region').disabled = true;
        $('new_region').disabled = true;
        $('new_area').disabled = false;
    }else if(add_type == 'regionarea'){
        $('new_main_region').disabled = false;
        $('new_region').disabled = true;
        $('new_area').disabled = true;
    }
}

function initMainRegions(){
//  locationTextDiv = document.getElementById('location_text');
    locationText = document.getElementById('id_location_text');
    locationSearchDiv = document.getElementById('locationSelect');

    createMainRegion();
    createRegion();
    createArea();

    addChangeHandler('id_location_main_region', 'regionShowHide();');
    addChangeHandler('id_location_area', 'resetText();');

    mainRegionSelect = document.getElementById('id_location_main_region');
    mainRegionSelect.options.length = 0;

    for (var i=0; i<mainRegions.length; i++){
	    mainRegionSelect.options[i] = new Option(mainRegions[i][1], mainRegions[i][0]);
        if(mr_selected == mainRegions[i][0]){
            mainRegionSelect.options[i].selected = true
        }
    }
    resetArea();

    if(mr_selected != 0){
        regionShowHide();
    }
}
function createArea(){
    var aDiv = document.createElement("div");
    aDiv.className = "input";
    aDiv.setAttribute("id","area_select_div");
    var aSelect = document.createElement("select");
    aSelect.setAttribute("id","id_location_area");
    aSelect.setAttribute("name","location_area");
    aSelect.disabled = true;
    //var aLabel = document.createElement("label");
    //aLabel.setAttribute("for","id_location_area");
    //aLabel.setAttribute("id","area_label");
    //aStrong = document.createElement("strong");
    //aStrong.appendChild(document.createTextNode('Region'));
    //aLabel.appendChild(aStrong);
    //aDiv.appendChild(aLabel);
    aDiv.appendChild(aSelect);
    //insertAfter(aDiv, locationTextDiv);
    //locationSearchDiv.appendChild(aDiv);
    var advancedLink = document.getElementById('advancedButton');
    locationSearchDiv.insertBefore(aDiv, advancedLink);
}
function resetArea(){
    $('id_location_area').innerHTML = '';
    $('id_location_area').disabled = true;
}
function resetText(){
    if(initialised == 1){
        $('id_location_text').value = '';
    }
}
function createRegion(){
    var rDiv = document.createElement("div");
    rDiv.className = "input";
    rDiv.setAttribute("id","region_select_div");
    var rSelect = document.createElement("select");
    rSelect.setAttribute("id","id_location_region");
    rSelect.setAttribute("name","location_region");
    rSelect.disabled = true;
    //var rLabel = document.createElement("label");
    //rStrong = document.createElement("strong");
    //rStrong.appendChild(document.createTextNode('Select town/sub-region'));
    //rLabel.appendChild(rStrong);
    //rLabel.setAttribute("for","id_location_region");
    //rLabel.setAttribute("id","region_label");
    //rDiv.appendChild(rLabel);
    rDiv.appendChild(rSelect);
    //insertAfter(rDiv, locationTextDiv);
    //locationSearchDiv.appendChild(rDiv);
    var advancedLink = document.getElementById('advancedButton');
    locationSearchDiv.insertBefore(rDiv, advancedLink);
}
function createMainRegion(){
    var mrDiv = document.createElement("div");
    mrDiv.className = "input";
    var mrSelect = document.createElement("select");
    mrSelect.setAttribute("id","id_location_main_region");
    mrSelect.setAttribute("name","location_main_region");
    var mrLabel = document.createElement("label");
    mrLabel.setAttribute("for","id_location_main_region");
    mrLabel.setAttribute("id","main_region_label");
    mrStrong = document.createElement("strong");
    mrStrong.appendChild(document.createTextNode('Region'));
    mrLabel.appendChild(mrStrong);
    mrDiv.appendChild(mrLabel);
    mrDiv.appendChild(mrSelect);
    //insertAfter(mrDiv, locationTextDiv);
    //locationSearchDiv.appendChild(mrDiv);
    var advancedLink = document.getElementById('advancedButton');
    locationSearchDiv.insertBefore(mrDiv, advancedLink);
}


function addChangeHandler(id, scriptBlock){
    var element = document.getElementById(id);
	var oldonchange = element.onchange;
	var func = new Function(scriptBlock);

	if (typeof element.onchange != 'function') {
        element.onchange =  func;
	} else {
        element.onchange = function() {
			oldonchange();
		    func();
		}
	}
    //element.onchange = new Function(scriptBlock);
	if (typeof lettingWebMap != 'undefined') {
	    lettingWebMap.addFormEventHandlers();
	}
}

function regionShowHideAdd(){
    var regionSelectDiv = document.getElementById('region_select_div');
    $('id_location_region').innerHTML = '<option>Please wait...</option>';
    $('id_location_region').disabled = true;
    var mainRegionSelect = document.getElementById('id_location_main_region');
    selected = mainRegionSelect.value;
    var url = '/search/get_region_select/?selected_main_region='+selected+'&selected_region='+r_selected+'&rand='+new Date().getTime();
    new Ajax.Updater(regionSelectDiv, url, {
        method: 'get',
        onComplete: function() {
            addChangeHandler('id_location_region', 'areaShowHideAdd();');
            addChangeHandler('id_location_region', 'disable_add(\'regions\');')
            if ($('id_location_region').selectedIndex > 0) {
                areaShowHideAdd();
            }
        }
    });
    resetArea();
    resetText();
}

function regionShowHide(){
    var regionSelectDiv = document.getElementById('region_select_div');
    $('id_location_region').innerHTML = '<option>Please wait...</option>';
    $('id_location_region').disabled = true;
    var mainRegionSelect = document.getElementById('id_location_main_region');
    selected = mainRegionSelect.value;
    var url = '/search/get_region_select/?selected_main_region='+selected+'&selected_region='+r_selected+'&rand='+new Date().getTime();
    new Ajax.Updater(regionSelectDiv, url, {
        method: 'get',
        onComplete: function() {
            addChangeHandler('id_location_region', 'areaShowHide();');
            if ($('id_location_region').selectedIndex > 0) {
                areaShowHide();
            }
        }
    });
    resetArea();
    resetText();
}

function areaShowHideAdd(){
    var areaSelectDiv = document.getElementById('area_select_div');
    $('id_location_area').innerHTML = '<option>Please wait...</option>';
    $('id_location_area').disabled = true;
    var regionSelect = document.getElementById('id_location_region');
    selected = regionSelect.value;

    var url = '/search/get_area_select/?selected_region='+selected+'&selected_area='+a_selected+'&rand='+new Date().getTime();
    new Ajax.Updater(areaSelectDiv, url, {
        method: 'get',
        onComplete: function() {
            addChangeHandler('id_location_area', 'resetText();');
            addChangeHandler('id_location_area', 'disable_add(\'all\');');
            initialised = 1;
        }
    });
    resetText();
}

function areaShowHide(){
    var areaSelectDiv = document.getElementById('area_select_div');
    $('id_location_area').innerHTML = '<option>Please wait...</option>';
    $('id_location_area').disabled = true;
    var regionSelect = document.getElementById('id_location_region');
    selected = regionSelect.value;

    var url = '/search/get_area_select/?selected_region='+selected+'&selected_area='+a_selected+'&rand='+new Date().getTime();
    new Ajax.Updater(areaSelectDiv, url, {
        method: 'get',
        onComplete: function() {
            addChangeHandler('id_location_area', 'resetText();');
            initialised = 1;
        }
    });
    resetText();
}


function insertAfter(newElement, targetElement){
    var parent = targetElement.parentNode;
    if(parent.lastChild == targetElement){
        parent.appendChild(newElement);
    }
    else{
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}
