﻿//script for Find.aspx

var target;
var target2;
var progress=0;
var timerId;
var requestWaiting = false;
var searchButton;

function GeneratePropertyCount(controlClicked)
{
    if(req==null)
    {
        var options = GenerateOptions(controlClicked);
        sendRequest("Ajax/PropertyCount.aspx" + options)
        progress=5;
        timerId = setInterval("showProgress()", 400);
        var img = document.getElementById("progress");
        img.style.display="none";
        showProgress();
    }
    else
    {
        searchButton = controlClicked;
        requestWaiting = true;
    }
}

function GetList()
{
    document.location.href="List.aspx" + GenerateOptions();
}

function GenerateOptions(controlClicked)
{
    var options = "";
    
    //location
    var option = GetAreasSelected();
    var locations = option.split(",");
    switch(locations.length)
    {
        case 12:     //total number of checkboxes minus 1 (dont include check all)
            if(!document.forms[0].SelectAll.checked)
            {
                document.forms[0].SelectAll.checked = true;
                return GenerateOptions(controlClicked);
            }
            break;
        case 11:    //if all but one are selected
            if(document.forms[0].SelectAll.checked)   //if check all is selected
            {
            if(locations[10]=="94")   // and outside area not in the last position (0 based)
                {
                    document.forms[0].SelectAll.checked = false;   //uncheck the check all checkbox
                    return GenerateOptions(controlClicked); 
                }
            }
            else if (locations[10]!="94")   //otherwise, if outside not selected
            {
                document.forms[0].SelectAll.checked = true;   // check the check all checkbox
                return GenerateOptions(controlClicked);
            }
            options+="&MXHLOC=" + option;
            break;
        default:
            if(document.forms[0].SelectAll.checked)
            {
                document.forms[0].SelectAll.checked = false;
                return GenerateOptions(controlClicked);
            }
            options+="&MXHLOC=" + option;
            break;
    }

 // property type
    option = "";
    var all = true;
    if(document.forms[0].MXHTYPEF.checked) option+=",F"; else all=false;
    if(document.forms[0].MXHTYPEH.checked) option+=",H"; else all=false;
    if(document.forms[0].MXHTYPEM.checked) option+=",M"; else all=false;
    if(document.forms[0].MXHTYPEB.checked) option+=",B"; else all=false;
    if(document.forms[0].MXHTYPES.checked) option+=",S";
    
    if(option.length=0) 
    {
        window.alert("You must select at least one type of property");
        controlClicked.checked = true;
        return GenerateOptions(controlClicked);
    }
    else if(!all)
    {
        options+="&MXHTYPE=" + option.substring(1);
    }

    //floor
    option = "";
    all = true;
    if(document.forms[0].MXHFLRB.checked) option+=",-1"; else all=false;
    if(document.forms[0].MXHFLR0.checked||document.forms[0].MXHTYPEH.checked||document.forms[0].MXHTYPEM.checked) option+=",0"; else all=false;
    if(document.forms[0].MXHFLR1.checked) option+=",1"; else all=false;
    if(document.forms[0].MXHFLR2.checked) option+=",2"; else all=false;
    if(document.forms[0].MXHFLR3.checked) option+=",3"; else all=false;
    if(option.length=0) 
    {
        window.alert("You must select at least one floor option");
        controlClicked.checked = true;
        return GenerateOptions(controlClicked);
    }
    else if(!all)
    {
        options+="&MXHFLR=" + option.substring(1);
    }

    //lift
    if(document.forms[0].MXHLIFT.checked) options+="&MXHLIFT=1";
    
    //heating
    option = "";
    all = true;
    if(document.forms[0].MXHHEATS.checked) option+=",S"; else all=false;
    if(document.forms[0].MXHHEATG.checked) option+=",G"; else all=false;
    if(document.forms[0].MXHHEATO.checked) option+=",O"; else all=false;
    if(option.length=0) 
    {
        window.alert("You must select at least one heating option");
        controlClicked.checked = true;
        return GenerateOptions(controlClicked);
    }
    else if(!all)
    {
        options+="&MXHHEAT=" + option.substring(1);
    }

    //bedrooms has
    option = "";
    all = true;
    if(document.forms[0].MXHBED0.checked) option+=",0"; else all=false;
    if(document.forms[0].MXHBED1.checked) option+=",1"; else all=false;
    if(document.forms[0].MXHBED2.checked) option+=",2"; else all=false;
    if(document.forms[0].MXHBED3.checked) option+=",3"; else all=false;
    if(document.forms[0].MXHBED4.checked) option+=",4"; else all=false;
    if(option.length=0) 
    {
        window.alert("You must select at least one bedrooms option");
        controlClicked.checked = true;
        return GenerateOptions(controlClicked);
    }
    else if(!all)
    {
        options+="&MXHBED=" + option.substring(1);
    }
     
    //bedrooms wants
    option = "";
    all = true;
    if(document.forms[0].MXWBED0.checked) option+=",0"; else all=false;
    if(document.forms[0].MXWBED1.checked) option+=",1"; else all=false;
    if(document.forms[0].MXWBED2.checked) option+=",2"; else all=false;
    if(document.forms[0].MXWBED3.checked) option+=",3"; else all=false;
    if(document.forms[0].MXWBED4.checked) option+=",4"; else all=false;
    if(option.length=0) 
    {
        window.alert("You must select at least one bedrooms option");
        controlClicked.checked = true;
        return GenerateOptions(controlClicked);
    }
    else if(!all)
    {
        options+="&MXWBED=" + option.substring(1);
    }                
    //garden
    var ctl = document.forms[0].MXHGARD;
    for(i=1;i<3;i++)
    {
        if(ctl[i].checked)
        {
            options+="&MXHGARD=" + ctl[i].value;
            break;
        }
    }

    //rent
    ctl = document.forms[0].MXHRENT;
    if(ctl.value.length>0) options+="&MXHRENT=" + ctl.value;
    
    if(options.length>0) options = "?" + options.substring(1);
    return options;
}

function onReadyStateChange()
{
    var ready = req.readyState;
    if(ready==READY_STATE_COMPLETE)
    {
        window.clearInterval(timerId);
        var img = document.getElementById("progress");
        img.style.display="inline";
        for(i=0;i<=5;i++)
        {
            img = document.getElementById("progress" + i);
            img.style.display="none";
        }
        target.innerHTML=req.responseText + " properties match your selection. ";
        target2.value = "get list of " + req.responseText + " properties";
        req = null;
        if(requestWaiting)
        {
            requestWaiting = false;
            GeneratePropertyCount(searchButton);
        }            
    }
}   

function showProgress()
{
    var img = document.getElementById("progress" + progress);
    img.style.display="none";
    progress++;
    if(progress>5) progress=0;
    img = document.getElementById("progress" + progress);
    img.style.display="inline";
}



