function ValidatePrice(form)
{
    var isValid = false;
    var errorMsg = document.getElementById('priceError');
    if(form.MaxPrice.value  == '' || form.MinPrice.value  == '' || parseInt(form.MinPrice.value) < parseInt(form.MaxPrice.value))
    {
        isValid = true;
        errorMsg.style.display = 'none';
    }
    else
    {
        errorMsg.style.display = 'inline';
    }
    return isValid;    
}

function ValidateNeighborhood(form,currentState)
{
    var isValid = false;
    var errorMsg = document.getElementById('hoodError');
    //if there is no currentNode or currentState, then this is a non columnview search
    if(typeof(currentNode) == 'undefined' || !currentNode || currentNode.selectable || currentState)
    {
        isValid = true;
        if (errorMsg) errorMsg.style.display = 'none';
    }
    else
    {
        if (errorMsg) errorMsg.style.display = 'inline';
    }
    return isValid; 
}

function ValidateTypeId(form)
{
    var isValid = false;
    var errorMsg = document.getElementById('typeIdError');
    if(form.TypeID)
    {
        for(i=0;i<form.TypeID.length;i++)
        {
            if(form.TypeID[i].checked)
            {
                isValid = true;
                if (errorMsg) errorMsg.style.display = 'none';
                break;
            }
        }
    }
    else
    {
        isValid = true;
    }
    if (!isValid)
    {
        if (errorMsg) errorMsg.style.display = 'inline';
    }
    return isValid; 
}

function ValidateStatus(form)
{
	var isValid = false;
    var errorMsg = document.getElementById('statusError');
    for(i=0;i<form.Status.length;i++)
    {
        if(form.Status[i].checked)
        {
            isValid = true;
            if (errorMsg) errorMsg.style.display = 'none';
            break;
        }
     }
    if (!isValid)
        if (errorMsg) errorMsg.style.display = 'inline';
    return isValid; 
}
   
function ValidateSearchForm(form)
{     
    var isValid = (ValidatePrice(form) && ValidateNeighborhood(form) && ValidateTypeId(form) && ValidateStatus(form));
    if(!isValid) alert('Please complete missing or incorrect information before submitting your search');
    return isValid;			            
}

function parseIdFromPath(path) {
    var _id = path;
    if(_id.indexOf('-')) {
        _id = _id.substring(_id.lastIndexOf('-')+1,_id.length);
    }
    return _id;
}

var lastPreSearch;
function PreSearch(form)
{
    var obj = document.getElementById('presearch');
    if (!obj) return;

    obj.innerHTML = '';
    var isValid = (ValidatePrice(form) && ValidateNeighborhood(form) && ValidateTypeId(form) && ValidateStatus(form));
    if (!isValid) return;
    
    if (typeof(lastPreSearch) != 'undefined' && lastPreSearch)
        AbortInitScript(lastPreSearch);
        
    var params = '';
    var geoAreaID = '';
    //var form = document.getElementsByName('GeoAreaID')[0].form;
    if(form.GeoAreaID.value == '') {
        geoAreaID = form.SGeoAreaID.value;
    } else {
        geoAreaID = form.GeoAreaID.value;
    }
    params += 'GeoAreaID=' + geoAreaID;
    params += '&XGeoAreaID=' + form.XGeoAreaID.value;
    params += '&MinPrice=' + form.MinPrice.value;
    params += '&MaxPrice=' + form.MaxPrice.value;
    params += '&MinBedroomCount=' + form.MinBedroomCount.value;
    params += '&MinBathroomCount=' + form.MinBathroomCount.value;
    if(form.TypeID)
        for(var i=0;i<form.TypeID.length;i++)
            if(form.TypeID[i].checked)
                params += '&TypeID=' + form.TypeID[i].value;
    if(form.Status)
        for(var j=0;j<form.Status.length;j++)
            if(form.Status[j].checked)
                params += '&Status=' + form.Status[j].value;
    lastPreSearch = '/Origin/Listing/SearchCountV2.ashx?' + params;

    GetInitScript(lastPreSearch, function(data) { if (data && data != '') document.getElementById('presearch').innerHTML = data + ' Listings found' });
}

function LoadSearch(savedSearch,form)
{
	var values = [];
	if(savedSearch && savedSearch != '')
	{
	    var nameValueRegex = /([^&=]+)=([^&=]+)/g;
	    var nameValue;
        while ((nameValue = nameValueRegex.exec(savedSearch)) != null)
            values[nameValue[1]] = nameValue[2];
		SavedSearch = new Search(values['g'],values['xg'],values['minP'],values['maxP'],values['bd'],values['bt'],values['t'],values['s']);
	    FillSearchForm(form,SavedSearch);
	}	
};

function FillSearchForm(form, savedSearch)
{
    var _TypeID = savedSearch.TypeID.split(',');
    var _Status = savedSearch.Status.split(',');
    
    for(var x=0;x<form.MinPrice.options.length;x++)
    {
        if(form.MinPrice.options[x].value == savedSearch.MinPrice)
        {
            form.MinPrice.options[x].selected = true;
            break;
        }
    }
    for(x=0;x<form.MaxPrice.options.length;x++)
    {
        if(form.MaxPrice.options[x].value == savedSearch.MaxPrice)
        {
            form.MaxPrice.options[x].selected = true;
            break;
        }
    }
    for(x=0;x<form.MinBedroomCount.options.length;x++)
    {
        if(form.MinBedroomCount.options[x].value == savedSearch.MinBedroomCount)
        {
            form.MinBedroomCount.options[x].selected = true;
            break;
        }
    }
    for(x=0;x<form.MinBathroomCount.options.length;x++)
    {
        if(form.MinBathroomCount.options[x].value == savedSearch.MinBathroomCount)
        {
            form.MinBathroomCount.options[x].selected = true;
            break;
        }
    }
    for(var j=0;j<form.TypeID.length;j++)
    {
        form.TypeID[j].checked = false;
        for(var i=0;i<_TypeID.length;i++)
            if(form.TypeID[j].value == _TypeID[i]) 
                form.TypeID[j].checked = true;
    }
    for(var j=0;j<form.Status.length;j++)
    {
        form.Status[j].checked = false;
        for(var i=0;i<_Status.length;i++)
            if(form.Status[j].value == _Status[i]) 
                form.Status[j].checked = true;
    }
}

function SaveListingSearch(form)
{
    var typeid = new Array();
    var status = new Array();
    SavedSearch = new Search();
    SavedSearch.GeoAreaID = form.GeoAreaID.value;
    if(!SavedSearch.GeoAreaID || SavedSearch.GeoAreaID == '') {
        form.GeoAreaID.value = form.SGeoAreaID.value;
        SavedSearch.GeoAreaID = form.SGeoAreaID.value;
    }
    SavedSearch.XGeoAreaID = form.XGeoAreaID.value;
    SavedSearch.MinPrice = form.MinPrice.value;
    SavedSearch.MaxPrice = form.MaxPrice.value;
    SavedSearch.MinBedroomCount = form.MinBedroomCount.value;
    SavedSearch.MinBathroomCount = form.MinBathroomCount.value;
    if(form.TypeID)
    {
        for(var i=0;i<form.TypeID.length;i++)
        {
            if(form.TypeID[i].checked)
            {
                typeid.push(form.TypeID[i].value);
            }
        }
        SavedSearch.TypeID = typeid;
    }
    if(form.Status)
    {
        for(var j=0;j<form.Status.length;j++)
        {
            if(form.Status[j].checked)
            {
                status.push(form.Status[j].value);
            }
        }
        SavedSearch.Status = status;
    }
    var foo = SavedSearch.ToString();
	document.cookie = 'ss=' + SavedSearch.ToString() + '; expires=Sun, 17-Jan-2038 18:00:00 GMT; path=/;';
};

function Search(geoAreaId,xGeoAreaId,minPrice,maxPrice,beds,baths,typeID,status)
{
	this.GeoAreaID = geoAreaId;
	this.XGeoAreaID = xGeoAreaId;
	this.MinPrice = minPrice;
	this.MaxPrice = maxPrice;
	this.MinBedroomCount = beds;
	this.MinBathroomCount = baths;
	this.TypeID = typeID;
	this.Status = status;
	
	this.ToString = function() 
	{
	    return 'g=' + this.GeoAreaID + '&xg=' + this.XGeoAreaID + '&minP=' + this.MinPrice + '&maxP=' + this.MaxPrice + '&bd=' + this.MinBedroomCount + '&bt=' + this.MinBathroomCount +  '&t=' + this.TypeID +  '&s=' + this.Status;
	};
}




