﻿if (typeof (HC) == 'undefined') {
    var HC = {};
    HC.gLanguageCode = 'EN';
    if (typeof (gLanguageCode) != 'undefined') {
        HC.gLanguageCode = gLanguageCode;
    }
}

function maintainInfoText(obj, defaultValue) {
    if (obj.value == defaultValue) {
        obj.value = '';
    }
    else if (obj.value == '') {
        obj.value = defaultValue;
    }
}

function ValidateDates(checkin, checkout) {
    var inDate = getDate(checkin);
    var outDate = getDate(checkout);
    if (!inDate) {
        alert("Checkin date is not valid");
        return false;
    }
    if (!outDate) {
        alert("Checkout date is not valid");
        return false;
    }
    var currentDate = new Date();

    //validate checkin - checkout difference (date range too big)
    if ((outDate - inDate) / 86400000 >= 31) {  //86400000 is one days in milliseconds
        alert('Your period of stay should be no longer than 30 nights');
        return false;
    }

    // validate checkout <= checkin
    if (outDate - inDate <= 0) {
        alert('Please ensure that the check-out date is after the check-in date');
        return false;
    }

    //validate checkin/checkout is less than one year in advance
    if ((outDate - currentDate) / 86400000 >= 363) {
        alert('You cannot book more than 1 year in advance');
        return false;
    }

    return true
}

function getDate(dateString) {
    var year = dateString.substr(0, 4);
    var month = dateString.substr(5, 2) - 1;  // 0 - 11
    var day = dateString.substr(8, 2);
    if (isNaN(year) || isNaN(month) || isNaN(day)) {
        return null;
    }
    return new Date(year, month, day);
}

function formdateDateToString(date) {
    var dd = date.getDate();
    var mm = date.getMonth() + 1; //January is 0!
    var yyyy = date.getFullYear();
    if (dd < 10) { dd = '0' + dd }
    if (mm < 10) { mm = '0' + mm }
    return yyyy + "-" + mm + "-" + dd;
}

function getObj(objID) {
    if (document.getElementById) {
        var returnObj = document.getElementById(objID);
        try {
            if (returnObj == null) {
                for (var i = 0; i < window.frames.length; i++) {
                    returnObj = window.frames[i].document.getElementById(objID);
                    if (returnObj != null) {
                        break;
                    }
                }
            }
        }
        catch (err) {
            return returnObj;
        }
        return returnObj;
    }
    else if (document.all) { return document.all[objID]; }
    else if (document.layers) { return document.layers[objID]; }
    return null;
}

function getXY(obj) {
    var paddingBottom = isNaN(parseInt($(obj).css("padding-bottom"))) ? 0 : parseInt($(obj).css("padding-bottom"));
    var paddingTop = isNaN(parseInt($(obj).css("padding-top"))) ? 0 : parseInt($(obj).css("padding-top"));
    var marginBottom = isNaN(parseInt($(obj).css("margin-bottom"))) ? 0 : parseInt($(obj).css("margin-bottom"));
    var marginTop = isNaN(parseInt($(obj).css("margin-top"))) ? 0 : parseInt($(obj).css("margin-top"));
    var curleft = $(obj).offset().left;
    var curtop = $(obj).offset().top + $(obj).height() + paddingBottom + marginBottom + paddingTop + marginTop + 1;

    return { 'x': curleft, 'y': curtop };
}

function getStyle(obj, styleProp) {
    if (obj.currentStyle)
        return obj.currentStyle[styleProp];
    else if (window.getComputedStyle)
        return document.defaultView.getComputedStyle(obj, null).getPropertyValue(styleProp);
    return null;
}

function isChild(s, d) {
    while (s) {
        if (s == d)
            return true;
        s = s.parentNode;
    }
    return false;
}

//Custom function to turn strings into ellipsis
String.prototype.ellipsisString = function(length) {

    var result = this;
    if (this.length > length + 1) {
        result = result.substring(0, length);
        result = result.substring(0, result.lastIndexOf(' '));
        var lastChar = result.charAt(result.length - 1);
        if (lastChar == "." || lastChar == "," || lastChar == "-") {
            if (result.length > 1) {
                result = result.substring(0, result.length - 1);
            }
        }
        return result + "...";
    } else {
        return this;
    }

    return this + length;
}

