﻿
Type.registerNamespace("ScriptLibrary");

ScriptLibrary.AvailabilityCalendarContainer = function() {
    this._calendars = new Hashtable();
    this._isRunning = false;
    this._runningCalendar = null;
    this._zpCalendar = null;
    this._data = new Hashtable();
    this._tooltips = new Hashtable();
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.addCalendar = function(cal) {
    this._calendars.put(cal._id, cal);
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.findByCalId = function(id) {
    var cals = this._calendars.getValues();
    for (var i = 0; i < cals.length; i++) {
        if (cals[i]._calendarId == id) {
            return cals[i];
        }
    }
    return null;
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.findByTextDateId = function(id) {
    var cals = this._calendars.getValues();
    for (var i = 0; i < cals.length; i++) {
        if (cals[i]._txtDateId == id) {
            return cals[i];
        }
    }
    return null;
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.put = function(propertyUnitId, mm, data) {
    if (!this._data.get(propertyUnitId)) {
        this._data.put(propertyUnitId, new Hashtable());
    }
    this._data.get(propertyUnitId).put(mm, data);
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.putTooltip = function(propertyUnitId, y, index, t) {
    if (!this._tooltips.get(propertyUnitId)) {
        this._tooltips.put(propertyUnitId, new Hashtable());
    }
    if (!this._tooltips.get(propertyUnitId).get(y)) {
        this._tooltips.get(propertyUnitId).put(y, new Hashtable());
    }
    this._tooltips.get(propertyUnitId).get(y).put(index, t);
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.get = function(propertyUnitId, y, m) {
    var data = this._data.get(propertyUnitId);
    if (data) {
        var key1 = y + (m < 10 ? ('0' + m) : ('' + m));
        return data.get(key1);
    }
    return data;
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.getTooltip = function(propertyUnitId, y, index) {
    if (this._tooltips.get(propertyUnitId)) {
        if (this._tooltips.get(propertyUnitId).get(y)) {
            return this._tooltips.get(propertyUnitId).get(y).get(index);
        }
    }
    return '';
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.LoadDatesForCheckIn = function(propertyUnitId, text) {
    var xmlDoc = null;
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(text);
    }
    catch (e) {
        try //Firefox, Mozilla, Opera, etc.
        {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(text, "text/xml");
        }
        catch (e) { alert(e.message) }
    }
    if (xmlDoc != null && typeof (xmlDoc) != 'undefined') {
        this.LoadDatesFromXml(propertyUnitId, xmlDoc);
    }
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.LoadDatesFromXml = function(propertyUnitId, result) {
    var elements = result.getElementsByTagName('root');
    if (typeof (elements) != 'undefined') {
        var root = elements.item(0);
        if (typeof (root) != 'undefined') {
            for (var i = 0; i < root.childNodes.length; i++) {
                var node = root.childNodes.item(i);
                if (node.nodeName == 'a') {
                    var m = node.getAttribute('m');
                    var h1 = new Hashtable();
                    for (var j = 0; j < node.childNodes.length; j++) {
                        var data = new Object();
                        var dn = node.childNodes.item(j);
                        data.day = dn.getAttribute('d');
                        data.css = dn.getAttribute('css');
                        data.tooltip = dn.getAttribute('tt');
                        h1.put(data.day, data);
                    }
                    this.put(propertyUnitId, m, h1);
                }
                if (node.nodeName == 'tt') {
                    var y = node.getAttribute('y');
                    for (var j = 0; j < node.childNodes.length; j++) {
                        var tnode = node.childNodes.item(j);
                        var ttIndex = tnode.getAttribute('i');
                        var ttText = '';
                        if (typeof (tnode.text) != 'undefined') {
                            ttText = tnode.text;
                        } else {
                            ttText = tnode.textContent;
                        }
                        this.putTooltip(propertyUnitId, y, ttIndex, ttText);
                    }
                }
            }
        }
    }
}
ScriptLibrary.AvailabilityCalendarContainer.prototype.GetDatesForCheckInCallback = function(result) {
    this.LoadDatesFromXml(this._runningCalendar._propertyUnitId, result);
    window.setTimeout('if(window.calendar) {window.calendar.refresh();}', 0);
    this.stop();
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.isRunning = function() {
    return this._isRunning;
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.start = function(acal, zpcal) {
    this._isRunning = true;
    this._runningCalendar = acal;
    this._zpCalendar = zpcal;
    if (loadingAnimation) {
        loadingAnimation.show(0);
    }
}

ScriptLibrary.AvailabilityCalendarContainer.prototype.stop = function() {
    this._isRunning = false;
    this._runningCalendar = null;
    this._zpCalendar = null;
    if (loadingAnimation) {
        loadingAnimation.reset();
    }
}

ScriptLibrary.AvailabilityCalendarContainer.registerClass('ScriptLibrary.AvailabilityCalendarContainer', null);
window.AvailabilityCalendarContainer = new ScriptLibrary.AvailabilityCalendarContainer();


Type.registerNamespace("ScriptLibrary");
ScriptLibrary.AvailabilityCalendar = function(id, txtDateId, btnDateId, pmAccountId, propertyId, propertyUnitId, type) {
    this._id = id;
    this._calendarId = id + '_cal';
    this._txtDateId = txtDateId;
    this._btnDateId = btnDateId;
    this._propertyId = propertyId;
    this._propertyUnitId = propertyUnitId;
    this._pmAccountId = pmAccountId;
    this._type = type;

    this._checkOutCalendar = null;
    this._checkInDate = null;
    this._click;
}

ScriptLibrary.AvailabilityCalendar.prototype.put = function(mm, data) {
    AvailabilityCalendarContainer.put(this._propertyUnitId, mm, data);
}

ScriptLibrary.AvailabilityCalendar.prototype.get = function(y, m) {
    return AvailabilityCalendarContainer.get(this._propertyUnitId, y, m);
}

ScriptLibrary.AvailabilityCalendar.prototype.getTooltip = function(y, index) {
    return AvailabilityCalendarContainer.getTooltip(this._propertyUnitId, y, index);
}

ScriptLibrary.AvailabilityCalendar.prototype.setCheckOutCalendar = function(c) {
    this._checkOutCalendar = c;
}

ScriptLibrary.AvailabilityCalendar.prototype.setCheckInDate = function(checkInDate) {
    this._checkInDate = checkInDate;
}

ScriptLibrary.AvailabilityCalendar.prototype.setEnable = function(enable) {
    document.getElementById(this._btnDateId).onclick = (enable == true) ? this._click : '';
    document.getElementById(this._txtDateId).readOnly = !enable;
}

ScriptLibrary.AvailabilityCalendar.prototype.setEnableKeyDate = function(enable) {
    document.getElementById(this._txtDateId).readOnly = !enable;
}

ScriptLibrary.AvailabilityCalendar.prototype.onblurTxtDate = function(evt) {
    var c = AvailabilityCalendarContainer.findByTextDateId(this.id);
    if (typeof (c) != 'undefined') {
        if (c._type == 'CheckIn') {
            var myDate = null;
            var bValid = isDateValid(this.value);
            if (bValid) {
                var parsedDate = Date.parseDate(this.value, '%m/%d/%Y');
                if (parsedDate != null) {
                    myDate = parsedDate;
                }
                c._checkOutCalendar._checkInDate = myDate;
                c._checkOutCalendar.createZPCalendar(myDate);
                c._checkOutCalendar.setEnable(true);
            } else {
                c._checkOutCalendar._checkInDate = null;
                c._checkOutCalendar.setEnable(false);
            }
        }
    }
}

ScriptLibrary.AvailabilityCalendar.prototype.onDateSelected = function(cal, date) {
    checkInDate = cal.date;
    var acal = AvailabilityCalendarContainer.findByCalId(cal.id);
    document.getElementById(acal._txtDateId).value = date;
    if (acal._type == 'CheckIn' || acal._type == 'NormalStart') {
        acal._checkOutCalendar.setCheckInDate(checkInDate);
        acal._checkOutCalendar.createZPCalendar(checkInDate);
        acal._checkOutCalendar.setEnable(true);
    }
    if (window.calendar) {
        window.calendar.hide();
    }
}

ScriptLibrary.AvailabilityCalendar.prototype.getDateStatusFunc = function(date, y, m, d) {
    var now = new Date();
    /*
    if (date <= now)
    return true;
    */
    var r = 'Avaliable';
    var acal = AvailabilityCalendarContainer.findByCalId(this.id);
    if (acal != null) {
        if (!AvailabilityCalendarContainer.isRunning()) {
            var mm = acal.get(y, (m + 1));
            if (typeof (mm) == 'undefined') {
                AvailabilityCalendarContainer.start(acal, this);
                GetDatesForCheckIn(acal._id, acal._pmAccountId, acal._propertyId, acal._propertyUnitId, y, m + 1);
            }
            else {
                var data = mm.get('' + (d));
                if (typeof (data) != 'undefined') {
                    r = data.css;
                }
            }
            if (acal._type == 'CheckOut' && acal._checkInDate != null) {
                if (date <= acal._checkInDate) {
                    r = 'disabled ' + r;
                }
            }
        }
    }

    return r;
}

ScriptLibrary.AvailabilityCalendar.prototype.getDateToolTipFunc = function(date) {
    var y = date.getFullYear();
    var m = date.getMonth();
    var d = date.getDate();

    var acal = AvailabilityCalendarContainer.findByCalId(this.id);
    if (acal != null) {
        if (!AvailabilityCalendarContainer.isRunning()) {
            var mm = acal.get(y, (m + 1));
            if (typeof (mm) != 'undefined') {
                var data = mm.get('' + (d));
                if (typeof (data) != 'undefined') {
                    var tt = acal.getTooltip(y, data.tooltip);
                    if (typeof (tt) != 'undefined' && tt != null) {
                        return tt;
                    }
                }
            }
        }
    }
    return date.print("%m/%d/%Y");
}

ScriptLibrary.AvailabilityCalendar.prototype.createZPCalendar = function(visibleDate) {
    if (this._type == 'Normal') {
        var r = Zapatec.Calendar.setup({
            id: this._calendarId,
            button: this._btnDateId,
            inputField: this._txtDateId,
            ifFormat: '%m/%d/%Y',
            weekNumbers: false,
            step: 1,
            multiple: false,
            numberMonths: 1,
            noHelp: true,
            showOthers: true,
            date: visibleDate
        });
    }
    else if (this._type == 'NormalStart' || this._type == 'NormalEnd') {
        var r = Zapatec.Calendar.setup({
            id: this._calendarId,
            button: this._btnDateId,
            inputField: this._txtDateId,
            ifFormat: '%m/%d/%Y',
            weekNumbers: false,
            step: 1,
            multiple: false,
            numberMonths: 1,
            noHelp: true,
            showOthers: true,
            date: visibleDate,
            onSelect: this.onDateSelected
        });

        this._click = r.click;
        document.getElementById(this._txtDateId).onblur = this.onblurTxtDate;

        AvailabilityCalendarContainer.addCalendar(this);

    }
    else {
        var r = Zapatec.Calendar.setup({
            id: this._calendarId,
            button: this._btnDateId,
            inputField: this._txtDateId,
            ifFormat: '%m/%d/%Y',
            weekNumbers: false,
            step: 1,
            multiple: false,
            numberMonths: 1,
            noHelp: true,
            showOthers: true,
            date: visibleDate,
            dateStatusFunc: this.getDateStatusFunc,
            dateToolTipFunc: this.getDateToolTipFunc,
            onSelect: this.onDateSelected
        });

        this._click = r.click;
        document.getElementById(this._txtDateId).onblur = this.onblurTxtDate;

        AvailabilityCalendarContainer.addCalendar(this);
    }
}

ScriptLibrary.AvailabilityCalendar.registerClass('ScriptLibrary.AvailabilityCalendar', null);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 