﻿window.onresize = setMainDivPosition;
//======================
//The Width Of The Div.
var divWidth = 925;
//The Height Of The Div.
var divHeight = 750;
//======================
var selectedArrival;
var selectedDeparture;
var senderControl = "";
//======================
var isResizing = false;
var isSilverlightInstaledOnMachine = isSilverlightInstalled();
setupPage();
function setupPage() {
    try {
        //Setup the screen sizes.
        document.getElementById("mainDiv").style.width = String(divWidth) + "px";
        document.getElementById("mainDiv").style.height = String(divHeight) + "px";
        //Set the initial position of the main screen
        setMainDivPosition();
        //Load default values for dates etc...
        loadDefaultValues();
        if (!isSilverlightInstaledOnMachine) {
            displaySilverlightWarning();
        }
    }
    catch (ex) {
        alert("ERROR : setupPage() " + String(ex));
    }
}
function loadDefaultValues() {
    try {
        selectedArrival = new Date();
        selectedDeparture = new Date();
        selectedDeparture.setDate(selectedArrival.getDate() + 1);
        document.getElementById("arriveDate").innerHTML = returnDate(selectedArrival);
        document.getElementById("departDate").innerHTML = returnDate(selectedDeparture);
        document.getElementById("noNights").innerHTML = "1";
    }
    catch (ex) {
        alert(String(ex));
    }
}
function displaySilverlightWarning() {
    try{
        var w = GetWidth();
        var h = GetHeight();
        document.getElementById("silverlightDiv").style.top = ((h / 2) - 110) + "px";
        document.getElementById("silverlightDiv").style.left = ((w / 2) - 160) + "px";
        document.getElementById("blurDiv").style.display = '';
        document.getElementById("silverlightDiv").style.display = '';
    }
    catch (ex) {
        alert(String(ex));
    }
}
function setMainDivPosition() {
    try {
        if (!isResizing) {
            isResizing = true;
            var w = GetWidth();
            var h = GetHeight();
            if (((h / 2) - ((divHeight / 2) - 15)) > 0)
                document.getElementById("mainDiv").style.top = ((h / 2) - (divHeight / 2)) + "px";
            else
                document.getElementById("mainDiv").style.top = "-15px";
            if (((w / 2) - ((divWidth / 2) - 57)) > 0)
                document.getElementById("mainDiv").style.left = ((w / 2) - (divWidth / 2)) + "px";
            else
                document.getElementById("mainDiv").style.left = "-57px";
            isResizing = false;
        }
    }
    catch (ex) {
    }
}
function GetWidth() {
    var x = 0;
    if (self.innerHeight) {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}
function GetHeight() {
    var y = 0;
    if (self.innerHeight) {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) {
        y = document.body.clientHeight;
    }
    return y;
}
function returnDate(cDate) {
    try {
        cDay = String(cDate.getDate());
        if (cDay.length == 1)
            cDay = "0" + cDay;
        cMonth = String(cDate.getMonth() + 1);
        if (cMonth.length == 1)
            cMonth = "0" + String(cMonth);
        cYear = String(cDate.getFullYear());
        return cDay + "/" + cMonth + "/" + cYear;
    }
    catch (ex) {
        alert(String(ex));
        return "";
    }
}
function applyBookingButton_Click() {
    try {
        document.getElementById("popupTitle").innerHTML = "Request Booking";
        document.getElementById("popupStatus").innerHTML = "Please Wait For Booking Control To Load.";
        document.getElementById("popupContent").innerHTML = "";
        var s = new Array();
        //--- Arrival Date Decoded
        var aDate = document.getElementById("arriveDate").innerHTML.split("/");
        //-- Departure Date Decoded
        var dDate = document.getElementById("departDate").innerHTML.split("/");
        s[s.length] = "<iframe src=\"http://leisurelodgemotorinnnelson.web-rooms.co.nz/home.html?cid=" +
            aDate[0] + "&cimy=" + aDate[1] + "-20" + aDate[2] +
            "&cod=" + dDate[0] + "&comy=" + dDate[1] + "-20" + dDate[2] + 
            "&cat=&go=GO\" height=\"100%\" width=\"100%\" onload=\"setDonePopupStatus()\"></iframe>\n";
        document.getElementById("popupContent").innerHTML = s.join("");
        document.getElementById("blurDiv").style.display = '';
        document.getElementById("zoomDiv").style.display = '';
    }
    catch (ex) {
        alert(String(ex));
    }
}
function blurDiv_Click() {
    try{
        if(document.getElementById("zoomDiv").style.display == '')
            document.getElementById("zoomDiv").style.display = 'none';
        if (document.getElementById("calendarDiv").style.display == '')
            document.getElementById("calendarDiv").style.display = 'none';
        if (document.getElementById("silverlightDiv").style.display == '')
            document.getElementById("silverlightDiv").style.display = 'none';
        document.getElementById("blurDiv").style.display = 'none';   
    }
    catch (ex) {
        alert(String(ex));
    }
}
function zoomCloseButton_Click() {
    try {
        document.getElementById("zoomDiv").style.display = 'none';
        document.getElementById("blurDiv").style.display = 'none';
    }
    catch (ex) {
        alert(String(ex));
    }
}
//--- DATE CONTROL ---
function openDateControl(sender) {
    try {
        document.getElementById("calendarDiv").style.left = (tempX + 20) + "px";
        document.getElementById("calendarDiv").style.top = (tempY - 240) + "px";
        senderButton = sender;
        document.getElementById("blurDiv").style.display = '';
        senderControl = sender;
        var caption = ""
        if (sender == "departDate") {
            caption = "Select Departure Date";            
        }
        else {
            caption = "Select Arrival Date";
        }
        var str = document.getElementById(sender).innerHTML;
        loadCalendar(str, "NZ",caption);
        document.getElementById("calendarDiv").style.display = '';
    }
    catch (ex) {
        alert(String(ex));
    }
}
function calanderControlClose_Click() {
    try{
        document.getElementById("calendarDiv").style.display = 'none';
        document.getElementById("blurDiv").style.display = 'none';
    }
    catch (ex) {
        alert(String(ex));
    }
}
function submitButton_Click() {
    try {
        var dte = "";
        if (senderControl == "arriveDate") {
            if (String(paramDay).length == 1)
                dte += "0" + String(paramDay) + "/";
            else
                dte += String(paramDay) + "/";
            if (String(paramMonth).length == 1)
                dte += "0" + String(paramMonth) + "/";
            else
                dte += String(paramMonth) + "/";
            dte += String(paramYear);
            document.getElementById("arriveDate").innerHTML = dte;
            dte = "";
            var nDate = new Date();
            nDate.setDate(paramDay);
            nDate.setMonth(paramMonth - 1);
            nDate.setFullYear(paramYear);
            //--------------------------------------------------------------------------
            var dDate = new Date();
            var s = String(document.getElementById("departDate").innerHTML).split("/");
            dDate.setDate(s[0]);
            dDate.setMonth(s[1] - 1);
            dDate.setFullYear(s[2]);
            if (dDate <= nDate) {
                dDate = nDate.addDays(1);
                if (String(dDate.getDate()).length == 1)
                    dte += "0" + String(dDate.getDate()) + "/";
                else
                    dte += String(dDate.getDate()) + "/";
                if (String(dDate.getMonth() + 1).length == 1)
                    dte += "0" + String(dDate.getMonth() + 1) + "/";
                else
                    dte += String(dDate.getMonth() + 1) + "/";
                dte += String(dDate.getFullYear());
                document.getElementById("departDate").innerHTML = dte;
                document.getElementById("noNights").innerHTML = "1";
            }
            else {
                dDate = new Date();
                s = String(document.getElementById("departDate").innerHTML).split("/");
                dDate.setDate(s[0]);
                dDate.setMonth(s[1] - 1);
                dDate.setFullYear(s[2]);
                var dif = dayDiff(nDate,dDate);
                document.getElementById("noNights").innerHTML = String(dif);
            }
        }
        else {
            var aDate = new Date();
            s = String(document.getElementById("arriveDate").innerHTML).split("/");
            aDate.setDate(s[0]);
            aDate.setMonth(s[1] - 1);
            aDate.setFullYear(s[2]);   

            nDate = new Date();
            nDate.setDate(paramDay);
            nDate.setMonth(paramMonth - 1);
            nDate.setFullYear(paramYear);

            if (nDate < aDate.addDays(1)) {
                alert("ERROR - The Departure Date Must Be The Day After The Arrival Date At Least.");
            }
            else {
                if (String(nDate.getDate()).length == 1)
                    dte += "0" + String(nDate.getDate()) + "/";
                else
                    dte += String(nDate.getDate()) + "/";
                if (String(nDate.getMonth() + 1).length == 1)
                    dte += "0" + String(nDate.getMonth() + 1) + "/";
                else
                    dte += String(nDate.getMonth() + 1) + "/";
                dte += String(nDate.getFullYear());
                document.getElementById("departDate").innerHTML = dte;
                dif = dayDiff(aDate, nDate);
                document.getElementById("noNights").innerHTML = String(dif + 1);
            }             
        }
        document.getElementById("calendarDiv").style.display = 'none';
        document.getElementById("blurDiv").style.display = 'none';
    }
    catch (ex) {
        alert(String(ex));
    }
}
//Add Days To A Date!!
Date.prototype.addDays = function (days) {
    this.setDate(this.getDate() + days);
    return this;
}
function dayDiff(d1,d2) {
    var d = Math.abs(d2 - d1);
    return Math.floor(d / (24 * 60 * 60 * 1000));
}


//Rotator Control Functions
function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }

    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    if (errorType == "ImageError" || errorType == "MediaError") {
        return;
    }

    var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}

function changeImagePage(tab) {
    try {
        var control = document.getElementById("rotatorControl");
        control.Content.Page.loadRunList(Number(tab));
    }
    catch (ex) {
        alert(String(ex));
    }
}

//--- TABS CONTROL ---

var tabCaptions = new Array();
var tabContents = new Array();
var tabSlideShowIndexs = new Array();
var tabZoomAvailable = new Array();

loadData();

function loadData() {
    try {
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        }
        else {
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET", "./Data/Tabs.xml", false);
        xhttp.send("");
        xmlDoc = xhttp.responseXML;
        var x = xmlDoc.getElementsByTagName("TAB");
        var s = new Array();
        for (var i = 0; i < x.length; i++) {
            tabCaptions[tabCaptions.length] = x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
            tabContents[tabContents.length] = x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue;
            tabSlideShowIndexs[tabSlideShowIndexs.length] = x[i].getElementsByTagName("SLIDESHOW")[0].childNodes[0].nodeValue;
            if (x[i].getElementsByTagName("ZOOM")[0].childNodes[0].nodeValue == "true")
                tabZoomAvailable[tabZoomAvailable.length] = true;
            else
                tabZoomAvailable[tabZoomAvailable.length] = false;
        }        
    }
    catch (ex) {
        alert(String(ex));
    }
}


//--------------------------------------

//--- DEFAULT VALUE ---
var currentTab = 0;

//=== IF THE PAGE HAS PARAMETERS THEN SET THEN DEFAULT TO THAT PAGE ===
setParameterTab();

function setParameterTab() {
    try
    {
        var parameters = location.search.substring(1).split("?");
        if (parameters != "") {
            var page = parameters[0].split(":");
            if (page[0] == "go") {
                for (var i = 0; i < tabCaptions.length; i++) {
                    if (tabCaptions[i].toUpperCase().trim() == page[1].toUpperCase().trim()) {
                        currentTab = i;
                    }
                }   
            }           
        }
    }
    catch(ex){
        alert(String(ex));
    }
}


displayTabs(currentTab,true);
//--------------------------------------
function displayTabs(tabID,isFirst) {
    try {
        var s = new Array();
        if (isFirst) {
            for (var i = 0; i < tabContents.length; i++) {
                s[s.length] = "<div id=\"" + tabCaptions[i] + "Div\" style=\"position: absolute; top: 0px; left: 0px; right: 0px; bottom: 0px; display: none;\" ></div>\n";
            }
            document.getElementById("contentsBodyInternalDiv").innerHTML = s.join("");   
        }
        s = new Array();
        s[s.length] = "<ol id=\"toc\">\n";
        for(var i = 0;i < tabCaptions.length;i++){
            if (i == tabID) {
                if (document.getElementById(tabCaptions[i] + "Div").innerHTML == "") {
                    document.getElementById(tabCaptions[i] + "Div").innerHTML = "<iframe width=\"100%\" height=\"100%\" src=\"./Content/" + tabContents[i] + "\"></iframe>\n";
                }
                s[s.length] = "<li class=\"current\"><a><span>" + tabCaptions[i] + "</span></a></li>\n";
            }
            else {
                if(document.getElementById(tabCaptions[i] + "Div").style.display == '')
                    document.getElementById(tabCaptions[i] + "Div").style.display = 'none';
                s[s.length] = "<li><a><span onclick=\"displayTabs(" + i + ",false)\">" + tabCaptions[i] + "</span></a></li>\n"; /// <reference path="../Images/tabs.gif" />

            }            
        }
        s[s.length] = "</ol>\n";
        document.getElementById("contentsControlDiv").innerHTML = "";
        document.getElementById("contentsControlDiv").innerHTML = s.join("");
        if (document.getElementById(tabCaptions[tabID] + "Div").style.display == 'none')
            document.getElementById(tabCaptions[tabID] + "Div").style.display = '';
        if (!isFirst) {
            changeImagePage(tabSlideShowIndexs[tabID]);
        }
        currentTab = tabID;
    }
    catch (ex) {
        alert(String(ex));
    }
}

function zoomButtonClick() {
    try {
              
    }
    catch (ex) {
        alert(String(ex));
    }
}

function setDonePopupStatus() {
    try {
        document.getElementById("popupStatus").innerHTML = "Done.";
    }
    catch (ex) {
        alert(String(ex));
    }
}

function zoomButtonMouseDown() {
    try {
        document.getElementById("contentsBodyZoomDiv").style.borderTop = "solid 2px Black";
        document.getElementById("contentsBodyZoomDiv").style.borderLeft = "solid 2px Black";
        document.getElementById("contentsBodyZoomDiv").style.borderRight = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderBottom = "solid 2px Gray";
    }
    catch(ex){
        alert(String(ex));    
    }
}

function zoomButtomMouseUp() {
    try {
        var zoomUrl = tabContents[currentTab];
        if (tabZoomAvailable[currentTab]) {
            zoomUrl = tabContents[currentTab].replace(".htm", "_zoom.htm");
        }
        document.getElementById("popupTitle").innerHTML = tabCaptions[currentTab] + " (Popup Mode)";
        document.getElementById("popupStatus").innerHTML = "Please Wait For Page To Load.";
        document.getElementById("popupContent").innerHTML = "";
        var s = new Array();
        s[s.length] = "<iframe src=\"./Content/" + zoomUrl + "\" height=\"100%\" width=\"100%\" onload=\"setDonePopupStatus()\"></iframe>\n";
        document.getElementById("popupContent").innerHTML = s.join("");
        document.getElementById("popupStatus").innerHTML = "Done.";
        document.getElementById("blurDiv").style.display = '';
        document.getElementById("zoomDiv").style.display = '';  
        document.getElementById("contentsBodyZoomDiv").style.borderTop = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderLeft = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderRight = "solid 2px Black";
        document.getElementById("contentsBodyZoomDiv").style.borderBottom = "solid 2px Black";    
    }
    catch (ex) {
        alert(String(ex));
    }
}

//Load a zoom in of the google map
function loadGoogleMap() {
    try {
        document.getElementById("popupTitle").innerHTML = "Show Location (Popup Mode)";
        document.getElementById("popupStatus").innerHTML = "Please Wait For Page To Load.";
        document.getElementById("popupContent").innerHTML = "";
        var s = new Array();
        s[s.length] = "<iframe id=\"popupFrame\" src=\"./Content/googlemap.htm\" height=\"100%\" width=\"100%\" onload=\"setDonePopupStatus()\"></iframe>\n";
        document.getElementById("popupContent").innerHTML = s.join("");
        document.getElementById("popupStatus").innerHTML = "Done.";
        document.getElementById("blurDiv").style.display = '';
        document.getElementById("zoomDiv").style.display = '';
        document.getElementById("contentsBodyZoomDiv").style.borderTop = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderLeft = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderRight = "solid 2px Black";
        document.getElementById("contentsBodyZoomDiv").style.borderBottom = "solid 2px Black"; 
        
    }
    catch (ex) {
        alert(String(ex));
    }
}

function launchEnquiry() {
    try {
        document.getElementById("popupTitle").innerHTML = "Enguiry Form (Popup Mode)";
        document.getElementById("popupStatus").innerHTML = "Please Wait For Page To Load.";
        document.getElementById("popupContent").innerHTML = "";
        var s = new Array();
        s[s.length] = "<iframe id=\"popupFrame\" src=\"./Content/enquiry.htm\" height=\"100%\" width=\"100%\" onload=\"setDonePopupStatus()\"></iframe>\n";
        document.getElementById("popupContent").innerHTML = s.join("");
        document.getElementById("popupStatus").innerHTML = "Done.";
        document.getElementById("blurDiv").style.display = '';
        document.getElementById("zoomDiv").style.display = '';
        document.getElementById("contentsBodyZoomDiv").style.borderTop = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderLeft = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderRight = "solid 2px Black";
        document.getElementById("contentsBodyZoomDiv").style.borderBottom = "solid 2px Black"; 
    }
    catch (ex) {
        alert(String(ex));
    }
}

function sendMail() {
    try {
        var link = "mailto:stay@leisurelodge.co.nz&subject=Stay At Leisure Lodge&body=Dear Reception";
        window.location.href = link;
    }
    catch (ex) {
        alert(String(ex));
    }
}

function returnBrowser() {
    try {
        var res = new Array();
        res[0] = navigator.appName;
        res[1] = browserVersion=navigator.appVersion
        return res;
    }
    catch (ex) {
        alert(String(ex));
        return null;
    }
}

function customPopup(popupUrl) {
    try {
        var zoomUrl = tabContents[currentTab];
        if (tabZoomAvailable[currentTab]) {
            zoomUrl = tabContents[currentTab].replace(".htm", "_zoom.htm");
        }
        document.getElementById("popupTitle").innerHTML = tabCaptions[currentTab] + " (Popup Mode)";
        document.getElementById("popupStatus").innerHTML = "Please Wait For Page To Load.";
        document.getElementById("popupContent").innerHTML = "";
        var s = new Array();
        s[s.length] = "<iframe src=\"./Content/" + popupUrl + "\" height=\"100%\" width=\"100%\" onload=\"setDonePopupStatus()\"></iframe>\n";
        document.getElementById("popupContent").innerHTML = s.join("");
        document.getElementById("popupStatus").innerHTML = "Done.";
        document.getElementById("blurDiv").style.display = '';
        document.getElementById("zoomDiv").style.display = '';
        document.getElementById("contentsBodyZoomDiv").style.borderTop = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderLeft = "solid 2px Gray";
        document.getElementById("contentsBodyZoomDiv").style.borderRight = "solid 2px Black";
        document.getElementById("contentsBodyZoomDiv").style.borderBottom = "solid 2px Black";
    }
    catch (ex) {
        alert(String(ex));
    }
}

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}
