// Ajax settings
AjaxPro.timeoutPeriod = 60000;



AjaxPro.onError = function(err) {
    if (err.Type == 'ConnectFailure') {
        document.getElementById('apartments').innerHTML = 'Connection failure';
        return;
    }

    if (err.Type == 'System.NullReferenceException') {
        window.location.reload();
        return;
    }

    apartmentsapart.Modules.AjaxApartmentList.SendError(err.Message, err.Type, err.Status, null);
}

function $Get(id) { return document.getElementById(id); }
// global variables

var als = null;
var newPage = false;
var apts = null;
var listID;
var filterExpanded = true;
var html;
// Ajax wrappers

function Load(ID) {

    if (document.cookie == '') {
        return;
    }
    if ((version == 3 || version == 4) && window.location.toString().indexOf("anyedit") < 0) // turn off ajax on landing apartments list
        return;

    listID = ID;
    try {
        document.getElementById('paging').style.display = 'none';
    } catch (error) { }
    Build(als, true);
}


function Reload() {
    try {
        document.getElementById('paging').style.display = 'none';
        document.getElementById('apartments').innerHTML = '<center><img src="http://cachefly.warsawshotel.com/wh/img_modules/progress.gif" /></center>';
        document.getElementById('sortingloading').style.display = 'block';
        document.getElementById('pagingloading').style.display = 'block';
    } catch (exc) { }
    Build(als, false);

}

function getPage(pageNo) {
    if (pageNo >= 0 && pageNo < als.noPages) {
        als.currentPage = pageNo;
        newPage = true;
        Reload();
    }
}

function details(id, link) {
    window.location = apartmentsapart.Modules.AjaxApartmentList.saveToCookies(id, listID, link).value;

}

function hideApartment(aptID, cityID) {
    if (typeof (FilterRestrictions) == "undefined") {
        var buttons = document.getElementsByTagName('span');
        for (i = 0; i < buttons.length; ++i) {
            if (buttons.item(i).className == 'close')
                buttons.item(i).style.display = 'none';
        }
    }
    else {
        //var answer = confirm(listphrases["dont_show_apt"]);
        //if (answer) {
            if (document.getElementById('hiddenApts') != null)
                document.getElementById('hiddenApts').style.display = 'none';

            FilterRestriction('ApartmentHide', aptID + "," + cityID, null);
            GetFilterForServer();
            //Build(this.als, false);
        //}
    }
}


function Redirect(res) {
    window.location.href = res.value;
}


function sort() {
    var newOrder = document.sortform.sortdd.options[document.sortform.sortdd.selectedIndex].value;

    if (newOrder != als.sortOrderBy) {
        if (newOrder == 'CustomersRating' || newOrder == 'Quality')
            als.sortOrderByDesc = true;
        else
            als.sortOrderByDesc = false;
    }
    else
        als.sortOrderByDesc = !als.sortOrderByDesc;

    als.sortOrderBy = newOrder;
    als.currentPage = 0;

    Reload();
}

function changeCurrency() {
    als.currency = document.sortform.currencydd.options[document.sortform.currencydd.selectedIndex].value;
    Reload();
}

function changeUnit() {
    als.unit = document.sortform.unitdd.options[document.sortform.unitdd.selectedIndex].value;
    Reload();
}

function showFilterAlertForCity() {
    if (document.getElementById('HiddenAptsPlaceHolder') == undefined || document.getElementById('HiddenAptsPlaceHolder').style.display == 'none')
        return false;
    else
        return true;
        
        
}

// Html code buildiers

function Build(als, firstTime) {


    try {

        if (als == null)
            this.als = apartmentsapart.Modules.AjaxApartmentList.getListState(listID).value;
        else
            this.als = als;
            
        var h = '';

        if ((this.als.noPages != 0 || (typeof(FilterRestrictions) != "undefined" &&  FilterRestrictions.Count > 0))) {



            if (document.getElementById('pagingloading') != null)
                document.getElementById('pagingloading').style.display = 'none';
            if (document.getElementById('pagingseo') != null)
                document.getElementById('pagingseo').style.display = 'none';
            apts = $Get('apartments');
            
            //if (FilterRestrictions.Count == 0)
            //    GetFilterForServer();
            //if (FilterRestrictions.Count > 0)
            //    h = "<div class=\"filterAlert\">" + listphrases["filtered_results"] + "</div>";
            //GetFilterForServer();
            if ((h == '' || h == null) && typeof(FilterRestrictions) != "undefined" && FilterRestrictions.Count > 0) {
                if (FilterRestrictions.Count == 1 && FilterRestrictions.GetFilter('ApartmentHide') != null && GetFilterApartmentHideCount() == 0)
                { }
                else if (GetFilterApartmentHideCount() == 0)
                    h = "<div class=\"filterAlert\">" + listphrases["filtered_results"] + "</div>";
                else {
                    var hid = "<div class=\"hidcontainer\" onclick=\"showHiddenApartments(event)\"><div class=\"back\"><b>" + phrases["hidden_apartments"] + "</b></div><div class=\"rev\"></div></div><div style=\"clear:both;\"></div>";
                    h = "<div class=\"filterAlert\">" + listphrases["filtered_results"] + hid + "</div>";
                }
            }
            
            h += apartmentsapart.Modules.AjaxApartmentList.getHtml(this.als, firstTime).value;

          //  else if (h == '' || h == null)
          //      throw "Error";
          //  else
                apts.innerHTML = h;

            if (!this.als.landingtoprated) {

                if (this.als.noPages != 0) {
                    BuildPaging();
                    $Get('paging').style.display = 'block';
                }
                else if (typeof(FilterRestrictions) != "undefined" &&  FilterRestrictions.Count > 0);
                else {
                    $Get('paging').style.display = 'none';
                    $Get('apartments').innerHTML = noresult;
                }
                if ($Get('sorting').style.display != 'block') {
                    BuildSorting();
                }
                document.getElementById('sortingloading').style.display = 'none';

            }

            if (this.newPage) {
                this.nextPage = false;
                window.location.href = '#listtop';
            }
        }
        else {
            $Get('apartments').innerHTML = noresult;
        }
    } catch (errr) {
        window.location.reload();
        return;
    }
}
function Buil(res) {

    if (res.error != null)
        return;

    apts.innerHTML = res.value;
}
function BuildSorting() {
    var className;
    if (als.sortOrderByDesc)
        className = 'down';
    else
        className = 'up';
    document.getElementById('sortorder').className = className;
    document.sortform.sortdd.value = als.sortOrderBy;

    BuildDD();

    document.getElementById('sorting').style.display = 'block';
}

function BuildDD() {
    var html = '<select name="currencydd" onchange="changeCurrency();">';
    for (var i = 0; i < currencyNames.length; i++) {
        var selected = (currencyNames[i] == als.currency) ? ' selected="selected"' : '';
        html += '<option value="' + currencyNames[i] + '"' + selected + '>' + currencyNames[i] + '</option>';
    }
    html += '</select>';

    document.getElementById('currencydd').innerHTML = html;

    document.sortform.unitdd.selectedIndex = als.unit;
}


function BuildPaging() {
    var split = 8;
    if (als.currentPage + split >= 100)
        split = 6;
    var left = als.currentPage - split;
    var right = als.currentPage + split;

    if (left < 0) {
        right += Math.abs(left);
        left = 0;
    }

    if (right > als.noPages - 1) {
        left -= right - als.noPages + 1;
        right = als.noPages - 1;
    }

    if (left < 0)
        left = 0;

    var html = '';
    if (left > 0) {
        html += '<a id="ApartListPaging0" class="unavaliable" href="javascript:void(0);" onclick="getPage(0);">1</a>\n';
        if (left > 1)
            html += '<span class="more">...</span>\n';
    }
    for (var i = left; i <= right; i++) {
        html += '<a id="ApartListPaging' + i + '" class="unavaliable" href="javascript:void(0);" onclick="getPage(' + i + ');">' + (i + 1) + '</a>\n';
    }
    if (right < als.noPages - 1) {
        if (right < als.noPages - 2)
            html += '<span class="more">...</span>\n';
        html += '<a id="ApartListPaging' + (als.noPages - 1) + '" class="unavaliable" href="javascript:void(0);" onclick="getPage(' + (als.noPages - 1) + ');">' + als.noPages + '</a>\n';
    }

    document.getElementById('ApartListPaging').innerHTML = html;


    $Get('ApartListPagingPrev').className = 'unavaliable';


    if (als.currentPage == 0) {
        $Get('ApartListPagingPrev').className = 'unavaliable';
        $Get('ApartListPagingPrev').href = 'javascript:void(0)';
    }
    else {
        $Get('ApartListPagingPrev').className = '';
        $Get('ApartListPagingPrev').href = 'javascript:void(0)';
    }

    if (als.currentPage == als.noPages - 1) {
        $Get('ApartListPagingNext').className = 'unavaliable';
        $Get('ApartListPagingNext').href = 'javascript:void(0)';
    }
    else {
        $Get('ApartListPagingNext').className = '';
        $Get('ApartListPagingNext').href = 'javascript:void(0)';
    }

    $Get('ApartListPaging' + als.currentPage).className = '';
}

function numbersOnly(event) {
    if (!event) event = window.event;
    var charCode = (event.charCode != undefined) ? event.charCode : event.keyCode;
    if (charCode == 0) return true; //enter, escape, tab
    var keychar = String.fromCharCode(charCode);


    reg = /\d/;
    return reg.test(keychar);
}

function onlyFloat(event) {
    if (!event) event = window.event;

    var charCode = (event.charCode != undefined) ? event.charCode : event.keyCode;
    if (charCode == 0 || charCode == 46) return true; //enter, escape, tab, '.'
    var keychar = String.fromCharCode(charCode);

    reg = /[\d+|\.]/;
    return reg.test(keychar);


}
function floatValidation(element) {
    var text = $Get(element).value;
    reg = /^\d{1,2}([\.]\d{1,2})?$/;
    if (!reg.test(text)) {
        n = text.indexOf(".");
        $Get(element).value = text.substring(0, n);

    }
} 



