﻿/* NAS COMMON START */
var NasCommon;

if (!NasCommon) NasCommon = {};

NasCommon.GetElement = function(elementID) {
    var element;
    if (elementID && typeof elementID == "string") {
        if (document.getElementById) {
            // this is the way the standards work
            element = document.getElementById(elementID);
        }
        else if (document.all) {
            // this is the way old msie versions work
            element = document.all[elementID];
        }
        else if (document.layers) {
            // this is the way nn4 works
            element = document.layers[elementID];
        }
    }

    return element;
}

NasCommon.SetElementClass = function(elementId, elementClassName) {
    var element = NasCommon.GetElement(elementId);
    if (element) {
        element.className = elementClassName;
    }
}

NasCommon.GoToElementValue = function(element) {
    if (element) {
        document.body.style.cursor = "wait";
        element.style.cursor = "wait";
        document.location.href = element.value;

    }
}

NasCommon.ShowHideElement = function(element) {
    if (element) {
        element.style.display = element.style.display == 'block' ? 'none' : 'block';
    }
}


NasCommon.SetInnerHtml = function(elementId, elementValue) {
    var element = NasCommon.GetElement(elementId);
    if (element) {
        element.innerHTML = elementValue;
    }
}

NasCommon.SubmitForm = function(form, action) {
    form.action = action;
    form.submit();
}

NasCommon.DoPostBack = function(clientId, argument) {
    __doPostBack(clientId, argument);
}

NasCommon.ResetScrollPosition = function() {
    setTimeout("window.scrollTo(0,0)", 0);
}

NasCommon.WindowOpen = function(url, width, height) {
    InfoWindow = window.open(url, 'InfoWindow', 'width=' + width + ',height=' + height + ',resizable,scrollbars');
    InfoWindow.focus();
}

NasCommon.ConfirmMessage = function(message) {
    return confirm(message);
}

NasCommon.ElementHasValue = function(element) {
    if (element && element.value != "") {
        return true;
    }
    return false;
}

NasCommon.SetWaitStyle = function() {
    if (!theForm) {
        return;
    }
    theForm.style.cursor = "wait";
}

/*** NAS COMMON END ***/

/*** RESERVATION  ***/

function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

function ShowHideTravelDetails(showHideId, toggleId, imgRootFolder) {
    var showHideElement = NasCommon.GetElement(showHideId);
    if (showHideElement) {
        NasCommon.ShowHideElement(showHideElement);
        var toggleElement = NasCommon.GetElement(toggleId);
        if (toggleElement) {
            var collapseIcon = '<img src="' + imgRootFolder + '/icons/treeview_minus.gif" border="0" alt="[-]" width="9" height="9" />';
            var expandIcon = '<img src="' + imgRootFolder + '/icons/treeview_plus.gif" border="0" alt="[+]" width="9" height="9" />';
            toggleElement.innerHTML = showHideElement.style.display == 'block' ? collapseIcon : expandIcon;
        }
    }
}

function CheckCreditCard(bnCardNumber, ccElement, infoElementId) {
    var infoElement = NasCommon.GetElement(infoElementId);
    if (bnCardNumber != null && ccElement != null && infoElement != null) {

        if (bnCardNumber.length <= ccElement.value.length) {
            if (ccElement.value.indexOf(bnCardNumber) == 0) {
                infoElement.style.display = "block";
            }
            else {
                infoElement.style.display = "none";
            }
        }
        else {
            infoElement.style.display = "none";
        }
    }
}
/*** RESERVATION  END ***/

/*** FARECALENDAR theForm = aspNetForm***/
function FareCal_OnClick(dayElement, mydate, fare, direction) {
    if (!theForm) {
        return false;
    }

    FareCal_Reset(direction);
    /* select calendar day */
    if (dayElement) {
        dayElement.className = dayElement.className + 'Selected';
    }

    if (!theForm.SelectedDate_OutboundFareCalendar) {
        return false;
    }
    if (direction == 'Outbound') {
        theForm.SelectedDate_OutboundFareCalendar.value = mydate;
        theForm.SelectedFare_OutboundFareCalendar.value = fare;
    }

    if (!theForm.SelectedDate_ReturnFareCalendar) {
        return false;
    }


    if (direction == 'Inbound') {
        theForm.SelectedDate_ReturnFareCalendar.value = mydate;
        theForm.SelectedFare_ReturnFareCalendar.value = fare;
    }

}

function FareCal_OnMouseHoover(dayElement, mydate, style, direction) {
    if (!theForm) {
        return false;
    }
    if (!theForm.SelectedDate_OutboundFareCalendar) {
        return false;
    }
    if (direction == 'Outbound' && theForm.SelectedDate_OutboundFareCalendar.value != mydate) {
        dayElement.className = style;
    }
    if (!theForm.SelectedDate_ReturnFareCalendar) {
        return false;
    }
    if (direction == 'Inbound' && theForm.SelectedDate_ReturnFareCalendar.value != mydate) {
        dayElement.className = style;
    }
}

/**
* Resets all elements in the calendar which isn't the currently selected date.
**/
function FareCal_Reset(direction) {
    if (!theForm) {
        return false;
    }
    if (document.getElementsByTagName) {
        var allDivs = document.getElementsByTagName("DIV");
        for (var i = 0; i < allDivs.length; i++) {
            var trgDiv = allDivs[i];
            /* reset calendar day */
            if (trgDiv.id.indexOf(direction) != -1 && trgDiv.className.indexOf('fareCalDay') != -1) {
                if (trgDiv.className.indexOf('MouseOver') != -1) {
                    var lastIndex = trgDiv.className.lastIndexOf('MouseOver');
                    trgDiv.className = trgDiv.className.substring(0, lastIndex);
                }

                if (trgDiv.className.indexOf('Selected') != -1) {
                    var lastIndex = trgDiv.className.lastIndexOf('Selected');
                    trgDiv.className = trgDiv.className.substring(0, lastIndex);
                }

            }
        }
    }
}


function CheckFareCalSelection(tripType, eOutboundId, eInboundId, errOutbound, errOutboundReturn, errDates) {
    eOutbound = document.getElementById(eOutboundId);
    if (!eOutbound) {
        return true;
    }

    var outboundDate = eOutbound.value;
    if (tripType == 1) {
        if (outboundDate == "") {
            alert(errOutbound);
            return false;
        }
        return true;
    }
    eInbound = document.getElementById(eInboundId);
    if (!eInbound) {
        return true;
    }
    var inboundDate = eInbound.value;
    if (outboundDate == "" || inboundDate == "") {
        alert(errOutboundReturn);
        return false;
    }

    var oDay = outboundDate.substr(0, 2);
    var oMonth = outboundDate.substr(2, 6);
    var rDay = inboundDate.substr(0, 2);
    var rMonth = inboundDate.substr(2, 6);
    if (!ValidateDates(oDay, oMonth, rDay, rMonth)) {
        alert(errDates);
        return false;
    }
    return true;
}

function ValidateDates(oday, omonth, rday, rmonth) {
    if ((1 * omonth) < (1 * rmonth)) {
        return true;
    }
    else if ((1 * omonth) == (1 * rmonth) && (oday <= rday)) {
        return true;
    }
    return false
}
/*** FARECALENDAR END***/

// From/to text change
function ChangeToFrom (toId, fromId) {

    var elementTo = document.getElementById(toId);
    var elementFrom = document.getElementById(fromId);

    if (elementTo.style.display == '') {

        document.getElementById(toId).style.display = 'none';
        document.getElementById(fromId).style.display = '';
    }
    else {
        document.getElementById(toId).style.display = '';
        document.getElementById(fromId).style.display = 'none';
    }

//    if (value == 'Departure')
//    {
//        document.getElementById(toId).style.display = 'none';
//        document.getElementById(fromId).style.display = 'block';
//    }
//    else
//    {
//        document.getElementById(toId).style.display = 'block';
//        document.getElementById(fromId).style.display = 'none';
//    }
}