
function trim(str) 
{
    var x = str;
    x = x.replace(/^\s*(.*)/, "$1");
    x = x.replace(/(.*?)\s*$/, "$1");
    return x;
}

function submit_form(name)
{ 
    f = document.getElementById(name);
    if (f) { 
        f.submit();
    }

    return true;
}

function fill_estate_agents(init)
{
    e = document.getElementById('estate_agent_id');
    if (!e) { 
        alert('unable to find estate_agent');
        return;
    }

    if (init) { 
        l = e.options.length;
        
        for (i = 0; i < l; i++) { 
            if (e.options[i].value == selected_estate_agent) { 
                e.options[i].selected = true;
                break;
            }
        }
    }

    id = parseInt(e.options[e.selectedIndex].value);    

    // Populate sub offices.
    so = document.getElementById('sub_office');
    if (!so) { 
        alert('unable to find sub_office');
        return;
    }
        
    // backup 'No' (lang changes etc)
    text = so.options[0].text;

    // clear
    so.options.length = 0;
    olen = 0;
    so.options[olen] = new Option(text, '');

    if (estate_agents[id]) { 
        l = estate_agents[id].length;
        
        for (i = 0; i < l; i++) { 
            if (estate_agents[id][i]) { 
                o = new Option(estate_agents[id][i], i);
                // preincrement
                so.options[++olen] = o;

                if (id == selected_estate_agent && i == selected_sub_office) { 
                    so.options[olen].selected = true;
                }

            }
        }
    }
}

function update_advert(id)
{
    errors = new Array();

    // Check name
    e = document.getElementById('name');
    if (!e) { 
        alert('could not find name');
        return;
    }
    
    n = trim(e.value);
    if (!n.length) { 
        errors.push('- you must enter a name');
    }

    // check box count
    e = document.getElementById('box_count');
    if (e) { 
        // Box count does not exist when there is only one section, so
        // it is alright to continue
        
        bc = parseInt(e.value);
        
        if (bc == 0) { 
            errors.push('- box count must be greater than 0');
        }
    }

    ttotal = 0;

    // Check box counts and final total
    e = document.getElementById('sections');
    if (e) { 
        sects = e.value.split(',');
        
        zero_count = 0;

        for (i in sects) { 
            key = 'box_count_' + sects[i];
            
            e = document.getElementById(key);
            if (!e) { 
                alert('could not find ' + key);
                return false;
            }
        
            t = parseInt(e.value);
            if (t == 0) { 
                zero_count++;
                e.style.backgroundColor = '#CC3333';
                e.style.color = '#ffffff';
            } else if (t > 0) { 
                e.style.backgroundColor = '#ffffff';
                e.style.color = '#000000';
            }

            ttotal += t;
        }

        if (zero_count > 0) { 
            errors.push('- box count must be greater than 0');
        }

        if (sects.length > 1) { 
            // More than one section, make sure it all adds up.
            e = document.getElementById('box_count');
            current_total = parseInt(e.value);

            if (current_total != ttotal) { 
                toggle_total(true);
                errors.push('- box counts must add up');
            } else {
                toggle_total(false);
            }
        }
    }

    if (errors.length > 0) { 
        txt = "Sorry, the following errors where found...\n" + 
               errors.join("\n");
    } else {
        e = document.getElementById('neweditadvert');
        if (!e) {
            alert('could not find neweditadvert');
            return;
        }

        e.submit();
    }
}

function toggle_total(bad) 
{
    if (bad) { 
        // Does not add up, mate
        e = document.getElementById('total');
        if (!e) { 
            alert('unable to find total');
            return;
        }
        
        e.style.backgroundColor = '#CC3333';

        e = document.getElementById('total_text');
        if (!e) {
            alert('unable to find total_text');
            return;
        }
        
        e.style.color = '#ffffff';
        e.style.fontWeight = 'bold';
    } else {
        e = document.getElementById('total');
        if (!e) { 
            alert('unable to find total');
            return;
        }
        
        e.style.backgroundColor = '#ffffff';

        e = document.getElementById('total_text');
        if (!e) {
            alert('unable to find total_text');
            return;
        }
        
        e.style.color = '#000000';
        e.style.fontWeight = 'normal';
    }
}

function check_totals()
{
    e = document.getElementById('sections');
    if (!e) { 
        return true;
    }
   
    sects = e.value.split(',');
    
    if (sects.length == 1) {
        return true;
    }
    
    new_total = 0;
    
    for (i in sects) { 
        key = 'box_count_' + sects[i];
        
        e = document.getElementById(key);
        if (!e) { 
            alert('could not find ' + key);
            return false;
        }
        
        t = parseInt(e.value);
        if (t == 0) { 
            e.style.backgroundColor = '#CC3333';
            e.style.color = '#ffffff';
        } else if (t > 0) { 
            e.style.backgroundColor = '#ffffff';
            e.style.color = '#000000';
        }

        new_total += parseInt(e.value);
    }

    // Get what the system thinks the total box count
    e = document.getElementById('box_count');
    current_total = parseInt(e.value);

    if (current_total != new_total) { 
        toggle_total(true);
        return;
    } else {
        toggle_total(false);
    }
}

max = 180;

function check_property_form()
{
    desc = document.getElementById('description');
    if (!desc) { 
        return;
    }

    clc = document.getElementById('charsleftchars');
    if (!clc) { 
        return;
    }

    len = desc.value.length;

    bcw = document.getElementById('boxcountwarning');

    cl = document.getElementById('charsleft');
    if (len > max) { 
        cl.style.backgroundColor = '#aa0000';
        if (bcw) { 
            bcw.style.display = 'block';
        }
    } else {
        cl.style.backgroundColor = '#00aa00';
        if (bcw) { 
            bcw.style.display = 'none';
        }
    }           

    var new_txt = document.createTextNode(desc.value.length);
    clc.replaceChild(new_txt, clc.childNodes[0]);
}

function another_image_upload()
{
    upper = document.getElementById('fileuploadcont');
    if (!upper || !upper.childNodes) {
        // oh dear, give up then
        return;
    }
	
    l = upper.childNodes.length;
    
    // Found out how many upload boxes we already have.
    highest = 0;

    for (i = 0; i < l; i++) { 
        o = upper.childNodes[i];

        name = o.nodeName.toLowerCase();
        
        if (name == 'div') { 
            a = parseInt(o.id.substring(5));
            if (a > highest) { 
                highest = a;
            }
        }
    }

    // Create new upload box container
    elem = document.createElement('div');
    elem.setAttribute('id', 'idiv_' + (parseInt(highest) + 1));
    elem.className = 'uploadcont';
    // Create new upload element
    file = document.createElement('input');
    file.setAttribute('type', 'file');
    file.setAttribute('id', 'image_' + (parseInt(highest) + 1));
    file.setAttribute('name', 'image_' + (parseInt(highest) + 1));
    // Add upload element to container
    elem.appendChild(file);
    // Add container to master container
    upper.appendChild(elem);

    return;
}

function remove_section(section, publication)
{
    if (confirm('Are you sure you want to remove this section?')) { 
        self.location.href = 'publication-population.php?publication=' + publication +
                             '&remove-section=' + section;
    }
}

function remove_advert(advert, publication)
{
    if (confirm('Are you sure you want to remove this advert?')) { 
        self.location.href = 'publication-population.php?publication=' + publication + 
                             '&remove-advert=' + advert;
    }
}

function remove_property(property, section, bounce)
{
    if (confirm('Are you sure you want to remove this property?')) { 
        
        if (bounce == 'estate-agent') { 
            self.location.href = 'new-estate-agent.php';
        } else { 
            self.location.href = 'property-populate.php?section=' + section +
                                 '&remove-property=' + property;
        }
    }
}


