function additem(frombox,tobox) {
    move(frombox,tobox);
    removeitembyname(tobox,"None Selected")
    docount(tobox);
    return false;
}


function docount(box) {
    if ((townsareloaded) && (box.id=='thetowns')) {

        var selct=box.options.length;
        if (selct==1) {

            if (box[0].text == "None Selected") selct=0;
        }
        document.getElementById('selectionscount').innerHTML = '<p>Selections - ' + selct + '</p>';
    }
    // document.getElementById('selectionscount').innerHTML = '<p>Selections:  ' + selct + '</p>';
    // alert("x" + document.getElementById('selectionscount').innerHTML);

    //$('selectionscount').update('<p>Selections - ' + selct + '</p>');
    // $('exampleDiv').update('Div content updated!');


}
function additembyname(tobox, zName) {
    // if (tobox.options.length == 0) {
    var bct=tobox.length;
            var newoption = new Option();
            newoption.value = zName;
            newoption.text = zName;
            tobox.options[bct] = newoption ;
            tobox.length++;
    // }
    KillSpace(tobox);
    docount(tobox);
    return false;
}
function addtownbyname(tobox, name, id) {
    var bct=tobox.length;
            var newoption = new Option();
            newoption.value = id;
            newoption.text = name;
            tobox.options[bct] = newoption ;
            tobox.length++;
    KillSpace(tobox);
    docount(tobox);
}
function removeitem(frombox) {
    for(var i=0; i<frombox.options.length; i++) {
        if(frombox.options[i].selected && frombox.options[i].value != "") {
            frombox.options[i].value = "";
            frombox.options[i].text = "";
           }
    }
    KillSpace(frombox);
    if (frombox.options.length<1){
        additembyname (frombox,"None Selected")
    }
    docount(frombox);
    return false;
}
function removeallitems(frombox) {
    for(var i=0; i<frombox.options.length; i++) {
        frombox.options[i].value = "";
        frombox.options[i].text = "";
    }
    KillSpace(frombox);
    if (frombox.id == 'thetowns') {
        additembyname (frombox,"None Selected")
    }
    docount(frombox);
    return false;
}
function KillSpace(box)  {
    for(var i=0; i<box.options.length; i++) {
        if(box.options[i].value == "")  {
            for(var j=i; j<box.options.length-1; j++)  {
                box.options[j].value = box.options[j+1].value;
                box.options[j].text = box.options[j+1].text;
            }
            var ln = i;
            break;
        }
    }
    if(ln < box.options.length)  {
        box.options.length -= 1;
        KillSpace(box);
   }
}
sortem = 1;  //true
function SortThem(lbox)  {
    // alert ("Sorting");
    var t_opts = new Array();
    var temp = new Object();
    for(var i=0; i<lbox.options.length; i++)  {
        t_opts[i] = lbox.options[i];
    }
    for(var x=0; x<t_opts.length-1; x++)  {
        for(var y=(x+1); y<t_opts.length; y++)  {
            if(t_opts[x].text > t_opts[y].text)  {
                temp = t_opts[x].text;
                t_opts[x].text = t_opts[y].text;
                t_opts[y].text = temp;
                temp = t_opts[x].value;
                t_opts[x].value = t_opts[y].value;
                t_opts[y].value = temp;
            }
           }
    }
    for(var i=0; i<lbox.options.length; i++)  {
        lbox.options[i].value = t_opts[i].value;
        lbox.options[i].text = t_opts[i].text;
   }
}

function xaddallitems(frombox,tobox) {
    if (frombox.options.length > 400) {
        // alert('We\'re sorry, you may only select up to 200 towns at one time.\nYou have selected ' + frombox.options.length + ' towns\nIf you want ALL towns, then do not select any towns.');
        alert ("too long");
        return false;
    }
    for(var i=0; i<frombox.options.length; i++) {
    }
     // if (sortem) SortThem(tobox);
    docount(tobox);
    return false;
}







function addallitems(frombox,tobox) {
    removeitembyname(tobox, "None Selected");
    // alert(frombox.name + " " + frombox.options.length);
    // return;
    if (frombox.options.length > 200) {
        alert('We\'re sorry, you may only select up to 200 towns at one time.\nYou have selected ' + frombox.options.length + ' towns\nIf you want ALL towns, then do not select any towns.');
        //alert ("too long");
        return false;
    }
    for(var i=0; i<frombox.options.length; i++) {
        if(frombox.options[i].value != "") {
            var newoption = new Option();
            newoption.value = frombox.options[i].text;
            newoption.text = frombox.options[i].text;
            // check that this entry doesn't already exist.  If it doesn't, then add it
            var len = tobox.length;
            var found = false;
            for(var count = 0; count < len; count++) {
                if (tobox.options[count] != null) {
                    if (newoption.text == tobox.options[count].text) {
                    found = true;
                    break;
                    }
                }
            }
            if (found != true) {
                tobox.options[tobox.options.length] = newoption;
                len++;
            }
           }
    }
     // if (sortem) SortThem(tobox);
    docount(tobox);
    return false;
}
function removeitembyname(frombox, zName) {
    // alert(zName);
    for(var i=0; i<frombox.options.length; i++) {
        // alert(frombox.options[i].text);
        if(frombox.options[i].text == zName) {
            frombox.options[i].value = "";
            frombox.options[i].text = "";
           }
    }
    KillSpace(frombox);
    docount(frombox);
    return false;
}
function move(frombox,tobox) {
    for(var i=0; i<frombox.options.length; i++) {
        if(frombox.options[i].selected && frombox.options[i].value != "") {
            // alert(frombox.options[i].value);
            var newoption = new Option();
            newoption.value = frombox.options[i].text;
            newoption.text = frombox.options[i].text;
            // alert ("V: " + frombox.options[i].text+ "T: " + frombox.options[i].text);
            // check that this entry doesn't already exist.  If it doesn't, then add it
            var len = tobox.length;
            var found = false;
            for(var count = 0; count < len; count++) {
                if (tobox.options[count] != null) {
                    if (newoption.text == tobox.options[count].text) {
                    found = true;
                    break;
                    }
                }
            }
            if (found != true) {
                tobox.options[tobox.options.length] = newoption;
                len++;
            }
           }
    }
     if (sortem) SortThem(tobox);
}
// show the local market area by default...
function checktowns() {
    // go through the selected town list and select all in it
    //  alert ("checktowns");
    // document.getElementById('searchform').thetowns
    var athetowns=document.getElementById('searchform').thetowns;
    selecttowns(athetowns);
    var athetowns=document.getElementById('searchform').townbox;
    deselecttowns(athetowns);

}
function selecttowns(box) {
    // selects all towns in the thetowns list
    for(var i=0; i<box.options.length; i++) {
        if (box.options[i].value != "None Selected") {
            box.options[i].selected=true;
        }
    }
}

function deselecttowns(box) {
    // selects all towns in the thetowns list
    for(var i=0; i<box.options.length; i++) {
    box.options[i].selected=false;
    }
}

function clearsearch(theform) {
    theform.MinPrice.value=0;
    theform.MaxPrice.value=0;
    theform.MinBedrooms.value=0;
    theform.MaxBedrooms.value=0;
    theform.MinBathrooms.value=0;
    theform.MaxBathrooms.value=0;
    theform.MinLotSize.value=0;
    theform.MaxLotSize.value=0;
    theform.MinLivingArea.value=0;
    theform.MaxLivingArea.value=0;
    theform.MinAge.value=0;
    theform.MaxAge.value=0;
    theform.MinGarage.value=0;
    theform.wf.checked=false;
    theform.xdays.value=3;
    theform.days.checked=false;
    //towns
    removeallitems(theform.thetowns);
    var i;
    for (i=0;i<6;i++) {
        theform.PClass[i].checked=false;
    }
    theform.PClass[0].checked=true;
    return false;
}

function showLMA() {
    var townbox=document.getElementById("searchform").townbox;
    var bct=townbox.length;
    removeallitems(townbox);
    // now go and get the new items from the jsarray and drop them in..
    //thisarray = document.getElementById(thiscounty);
    for (x in LMA) {
         addtownbyname(townbox,LMA[x].name,LMA[x].name)
    }
    changecountyname('Local Market Area');
    SortThem(townbox);
}
function showcounty(name) {
     // alert('showcounty');
     // changecountyname('Local Market Area...');
    showcountynew(name);
}

