var weekStartsOnSunday = false;  // true = Start the week on Sunday, false = start the week on Monday

// Format of current day at the bottom of the calendar
// [dayString] = day of week (examle: mon, tue, wed...)
// [UCFdayString] = day of week (examle: Mon, Tue, Wed...) ( First letter in uppercase)
// [day] = Day of month, 1..31
// [monthString] = Name of current month
// [year] = Current year

/**
 * Variables to define the number of days for calendar
 */
//days to set on flight booking calendar
var flightBookingDays = 336;
//days to set on hotel calendar
var hotelBookingDays = 480;

var pathToImages = '/tridion/images/';
var pathToImagesFromGG = 'http://www.virgin-atlantic.com/tridion/images/';

var calendar_offsetTop = 0;
var calendar_offsetLeft = 0;
var ifgg = false;

function getPathToImages() {
    if (ifgg) {
        return pathToImagesFromGG;
    }
    else {
        return pathToImages;
    }
}

var calendarDiv;
var calendarId;
var calendarToCloseId;
// represents the input date field
var inputField;
var noDateToDisplay;
var triedToSetReturnDateToDepartDate = false;
var wasBefore;
/**
 * The first active date of the current calendar.
 */
var startDate;
/**
 * The last active date of the current calendar.
 */
var endDate;

var MSIE = false;
var Opera = false;
//For userAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en) AppleWebKit/522.15.5 (KHTML, like Gecko) Version/3.0.3 Safari/522.15.5
var Safari = false;
//For userAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/419.2 (KHTML, like Gecko) Safari/419.3
//For useAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1
var Safari_Macintosh = false;
if (navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0)MSIE = true;
if (navigator.userAgent.indexOf('Opera') >= 0)Opera = true;
if (navigator.userAgent.indexOf('WebKit') >= 0)Safari = true;
if (navigator.userAgent.indexOf('Safari/312') >= 0 || navigator.userAgent.indexOf('Safari/417') >= 0
        || navigator.userAgent.indexOf('Safari/419') >= 0)Safari_Macintosh = true;

var monthArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var dayArray = ['M','T','W','T','F','S','S'];
var closeText = 'Close';
var diffReturnDepart;

if (weekStartsOnSunday) {
    var tempDayName = dayArray[6];
    for (var theIx = 6; theIx > 0; theIx--) {
        dayArray[theIx] = dayArray[theIx - 1];
    }
    dayArray[0] = tempDayName;
}

var daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
var currentMonth;
var currentYear;
var tmpDay;
var calendarContentDiv;
var returnDateTo = 'dd/mm/yy';
var returnFormat;
var regionalFormat;
var activeSelectBoxMonth;
var activeSelectBoxYear;

var iframeObj = false;
//// fix for EI frame problem on time dropdowns 09/30/2006
var iframeObj2 = false;

//global variables to identify the departure/arrival ids of the html elements involved
var departureElementId = 'departureDate';
var arrivalElementId = 'returnDate';

function initElementIds(departureId, arrivalId) {
    departureElementId = departureId;
    arrivalElementId = arrivalId;
}

function EIS_FIX_EI1(where2fixit)
{

    if (!iframeObj2)return;
    iframeObj2.style.display = 'block';
    iframeObj2.style.height = document.getElementById(where2fixit).offsetHeight + 1;
    iframeObj2.style.width = document.getElementById(where2fixit).offsetWidth;
    iframeObj2.style.left = getleftPos(document.getElementById(where2fixit)) + 1 - calendar_offsetLeft;
    iframeObj2.style.top = getTopPos(document.getElementById(where2fixit))
            - document.getElementById(where2fixit).offsetHeight - calendar_offsetTop;
}

function EIS_Hide_Frame()
{
    if (iframeObj2)iframeObj2.style.display = 'none';
}
//// fix for EI frame problem on time dropdowns 09/30/2006

var inputYear;
var inputMonth;
var inputDay;
var calendarDisplayTime = false;

var selectBoxMovementInProgress = false;

function cancelCalendarEvent()
{
    return false;
}
function isLeapYear(inputYear)
{
    if (inputYear % 400 == 0 || (inputYear % 4 == 0 && inputYear % 100 != 0)) return true;
    return false;

}

/*
 * Verifies if one of the calendar's arrows should not be displayed. This means that
 * will be no navigation to the previous month if the left arrow is not displayed.
 */
function displayArrows() {
    var d = new Date();
    var left = document.getElementById('leftLink' + calendarId);
    var right = document.getElementById('rightLink' + calendarId);
    var leftImg = document.getElementById('leftImg' + calendarId);
    var rightImg = document.getElementById('rightImg' + calendarId);
    if (((currentYear / 1) > (endDate.getFullYear())) || ((currentYear / 1) == (endDate.getFullYear())
            && currentMonth >= endDate.getMonth())) {
        rightImg.style.visibility = 'hidden';
        right.style.display = 'none';
    }
    else {
        rightImg.style.visibility = 'visible';
        right.style.display = 'inline';
    }

    if (((currentYear / 1) < startDate.getFullYear()) || (currentMonth <= startDate.getMonth() && currentYear
            == startDate.getFullYear())) {
        leftImg.style.visibility = 'hidden';
        left.style.display = 'none';
    }
    else {
        leftImg.style.visibility = 'visible';
        left.style.display = 'inline';
    }
}


/*
 * Called when the calendar's left arrow is pressed. Goes back with a month
 */
function switchMonthLeft() {
    var d = new Date();
    if (currentMonth >= 0) {
        if ((currentMonth == 0)) {
            currentMonth = 11;
            currentYear = currentYear / 1 - 1;
        }
        else {
            currentMonth = currentMonth - 1;
        }
    }
    writeCalendarContent();
    displayArrows();
}

/*
 * Called when the calendar's right arrow is pressed. Goes ahead with a month
 */
function switchMonthRight() {
    var d = new Date();
    if (currentMonth >= 0) {
        if ((currentMonth < 11)) {
            currentMonth = currentMonth + 1;
        }
        else {
            currentMonth = 0;
            currentYear = currentYear / 1 + 1;
        }
    }
    writeCalendarContent(calendarId);
    displayArrows();
}

/**
 * Close calendar
 */
function closeCalendar(calendarToClose) {
    if (calendarToClose) {
        calendar = document.getElementById('calendarDiv' + calendarId);
        if (calendar) {
            //In this case we have the other calendar opened and we will close it
            calendar.style.display = 'none';
        }
    }
    else {
        if (calendarDiv) {
            calendarDiv.style.display = 'none';
        }
    }
    if (iframeObj) {
        iframeObj.style.display = 'none';
        //// //// fix for EI frame problem on time dropdowns 09/30/2006
        EIS_Hide_Frame();
    }
    //Without this check, on Safari the calendar doesn't close anymore
    if (!Safari) {
        if (!inputField.disabled) {
            try {
                inputField.focus();
            }
            catch(e) {
            } // suppress focus errors  - necessary for IEs
        }
    }
}

/**
 * Closes the calendar with the id given as parameter.
 */
function closeCalendarById(calendarToClose) {
    if (calendarToClose) {
        if (calendarToClose == calendarId) {
            closeCalendar(calendarToClose);
        }
    }
}

/*
 Function used to create the top bar which containd the left arrow, month and year and right arrow
 */
function writeTopBar(calendarId) {
    var topBar = document.createElement('TABLE');
    topBar.id = 'topBar';
    topBar.width = '100%';
    calendarDiv.appendChild(topBar);

    var topBody = document.createElement('TBODY');
    topBar.appendChild(topBody);

    var row = topBody.insertRow(-1);
    row.style.width = '100%';
    // Left arrow
    var leftDiv = row.insertCell(-1);
    leftDiv.id = 'leftArrow' + calendarId;
    leftDiv.align = 'left';
    leftDiv.style.width = '15%';
    leftDiv.style.paddingLeft = '4px';

    var leftLink = document.createElement('A');
    leftLink.id = 'leftLink' + calendarId;
    leftLink.className = 'calLink';
    leftLink.href = 'javascript:switchMonthLeft()';

    var img = document.createElement('IMG');
    img.id = 'leftImg' + calendarId;
    img.src = getPathToImages() + 'booking_cal_prev_tcm4-808133.gif';
    img.style.height = '10px';
    img.style.width = '6px';

    leftLink.appendChild(img);
    leftDiv.appendChild(leftLink);
    if (Opera)leftDiv.style.width = '34px';

    //Month field
    var monthDiv = row.insertCell(-1);
    monthDiv.id = 'monthSelect' + calendarId;
    monthDiv.className = 'monthSpan';
    monthDiv.style.width = '70%';
    var span = document.createElement('SPAN');
    span.id = 'calendar_month_txt' + calendarId;
    monthDiv.appendChild(span);

    // Right arrow
    var rightDiv = row.insertCell(-1);
    rightDiv.id = 'rightArrow' + calendarId;
    rightDiv.align = 'right';
    rightDiv.style.width = '15%';
    rightDiv.style.marginRight = '1px';
    rightDiv.style.paddingRight = '4px';

    var rightLink = document.createElement('A');
    rightLink.id = 'rightLink' + calendarId;
    rightLink.className = 'calLink';
    rightLink.href = 'javascript:switchMonthRight()';

    var imgRight = document.createElement('IMG');
    imgRight.id = 'rightImg' + calendarId;
    imgRight.src = getPathToImages() + 'booking_cal_next_tcm4-808123.gif';
    imgRight.style.height = '10px';
    imgRight.style.width = '6px';

    rightLink.appendChild(imgRight);
    rightDiv.appendChild(rightLink);
    if (Opera)rightDiv.style.width = '34px';
}

/*
 * Creates the table with the calendar content
 */
function writeCalendarContent()
{
    var calendarContentDivExists = true;
    calendarContentDiv = document.getElementById('content' + calendarId);
    if (!calendarContentDiv) {
        calendarContentDiv = document.createElement('DIV');
        calendarContentDiv.id = 'content' + calendarId;
        calendarDiv.appendChild(calendarContentDiv);
        calendarContentDivExists = false;
    }

    var d = new Date();
    d.setHours(0, 0, 0, 0);
    if (!currentYear) {
        currentMonth = d.getMonth();
        currentYear = d.getFullYear();
    }
    else {
        var extendedStartDate = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
        var firstCalendarDate = new Date(currentYear, currentMonth, 1);
        if ((firstCalendarDate - extendedStartDate < 0) || (endDate - firstCalendarDate < 0)) {
            currentMonth = startDate.getMonth();
            currentYear = startDate.getFullYear();
        }
    }
    currentMonth = currentMonth / 1;
    d.setDate(1);
    d.setMonth(currentMonth);
    d.setFullYear(currentYear);

    var dayStartOfMonth = d.getDay();
    if (! weekStartsOnSunday) {
        if (dayStartOfMonth == 0)dayStartOfMonth = 7;
        dayStartOfMonth--;
    }

    document.getElementById('calendar_month_txt' + calendarId).innerHTML = monthArray[currentMonth] + " " + currentYear;

    var existingTable = calendarContentDiv.getElementsByTagName('TABLE');
    if (existingTable.length > 0) {
        calendarContentDiv.removeChild(existingTable[0]);
    }

    var calTable = document.createElement('TABLE');
    calTable.className = 'calTablePopup';
    calTable.width = '100%';
    calTable.cellSpacing = '0';
    calTable.cellpadding = '3';
    calendarContentDiv.appendChild(calTable);

    var calTBody = document.createElement('TBODY');
    calTable.appendChild(calTBody);

    //Insert the first row of the table, the one with the week days letters
    var row = calTBody.insertRow(-1);
    row.className = 'calHead';
    for (var no = 0; no < dayArray.length; no++) {
        var cell = row.insertCell(-1);
        cell.innerHTML = dayArray[no];
    }

    var row = calTBody.insertRow(-1);
    row.className = 'calRow';

    for (var no = 0; no < dayStartOfMonth; no++) {
        var cell = row.insertCell(-1);
        cell.innerHTML = '&nbsp;';
    }

    var colCounter = dayStartOfMonth;
    var daysInMonth = daysInMonthArray[currentMonth];
    if (daysInMonth == 28) {
        if (isLeapYear(currentYear))daysInMonth = 29;
    }
    var rowNo = 0;
    for (var no = 1; no <= daysInMonth; no++) {
        d.setDate(no);
        if (colCounter > 0 && colCounter % 7 == 0) {
            row = calTBody.insertRow(-1);
            row.className = 'calRow';
            rowNo++;
        }
        var cell = row.insertCell(-1);

        if ((d - startDate >= 0) && (endDate - d >= 0)) {
            var cellLink = document.createElement('A');
            cellLink.innerHTML = no;
            cellLink.href = 'javascript:void(0)';
            cellLink.title = no;
            cellLink.onclick = pickDateAndClose;
            cell.appendChild(cellLink);
        }
        else {
            cell.innerHTML = no;
        }
        colCounter++;
    }
    //Add the remaining empty cells
    while (colCounter % 7 != 0) {
        var cell = row.insertCell(-1);
        cell.innerHTML = '&nbsp;';
        colCounter++;
    }

    if (!document.all) {
        if (calendarContentDiv.offsetHeight)
            document.getElementById('topBar').style.top = calendarContentDiv.offsetHeight
                    + document.getElementById('topBar').offsetHeight - 1 + 'px';
        else {
            document.getElementById('topBar').style.top = '';
            document.getElementById('topBar').style.bottom = '0px';
        }
    }

    if (iframeObj) {
        if (!calendarContentDivExists)setTimeout('resizeIframe()', 350);
        else setTimeout('resizeIframe()', 10);
    }
}

function resizeIframe()
{
    iframeObj.style.width = calendarDiv.offsetWidth + 'px';
    iframeObj.style.height = calendarDiv.offsetHeight + 'px';
}

/*
 *Verifies if the date field is the default value
 */
function verifyIfDefaultDate(elementId) {
    var field = document.getElementById(elementId);
    if (field != null && field.value == regionalFormat.toUpperCase()) {
        return true;
    }
    else {
        return false;
    }
}

function pickDateAndClose(e, inputDate) {
    if (!inputDate && this) {
        day = this.innerHTML;
    }
    else {
        day = inputDate;
    }
    pickDate(day);
    closeCalendar();
}

function pickDate(day) {
    var month = currentMonth / 1 + 1;
    if (month < 10)month = '0' + month;
    if (day / 1 < 10)day = '0' + day;
    if (returnFormat) {
        returnFormat = returnFormat.replace('dd', day);
        returnFormat = returnFormat.replace('mm', month);
        var displayYear = "" + currentYear;
        if (displayYear.length == 4) {
            //In this case the date is selected from the calendar
            returnFormat = returnFormat.replace('yy', displayYear.substring(2));
        }
        else {
            //In this case the date is typed
            returnFormat = returnFormat.replace('yy', currentYear);
        }

        returnDateTo.value = returnFormat;
        if (calendarToCloseId == arrivalElementId) {
            var departureDate = parseDateFromString(returnFormat);
            if ((departureDate != null) && (departureDate - startDate >= 0) && (departureDate - endDate <= 0)) {
                var returnDateSpan = document.getElementById('calendar_month_txt' + calendarToCloseId);
                if (!returnDateSpan && verifyIfDefaultDate(arrivalElementId)) {
                    var returnDateField = document.getElementById(arrivalElementId);
                    if (!returnDateField.disabled) {
                        if (diffReturnDepart != null) {
                            var returnDate = departureDate;
                            returnDate.setDate(returnDate.getDate() + diffReturnDepart);
                            var returnDateString = getDateAsString(returnDate);
                            returnDateField.value = returnDateString;
                        }
                        else {
                            returnDateField.value = returnFormat;
                        }
                    }
                }
            }
        }
    }
}

function getDateAsString(date) {
    var dateString = regionalFormat;
    dateString = dateString.replace('yy', getYearString(date));
    dateString = dateString.replace('mm', getMonth(date));
    dateString = dateString.replace('dd', getDay(date));
    return dateString;
}

function getYearString(date) {
    var yearString = "" + date.getFullYear();
    var yearString2 = yearString.substring(2);
    return yearString2;
}

function getMonth(date) {
    var month = date.getMonth() / 1 + 1;
    if (month < 10)month = '0' + month;
    return month;
}

function getDay(date) {
    var day = date.getDate();
    if (day / 1 < 10)day = '0' + day;
    return day;
}

function writeCloseBar(calendarDiv) {
    var closeDiv = document.createElement('DIV');
    closeDiv.id = 'closeBar';
    //closeDiv.style.position = 'absolute';
    //closeDiv.style.margin-bottom = '0px';
    var link = document.createElement('A');
    link.href = 'javascript:closeCalendar()';
    link.innerHTML = closeText;

    closeDiv.appendChild(link);
    calendarDiv.appendChild(closeDiv);
}

function getTopPos(inputObj) {
    if (ifgg) {
        return calendar_offsetTop;
    }

    var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
    while ((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
    return returnValue + calendar_offsetTop;
}

function getleftPos(inputObj)
{
    var returnValue = inputObj.offsetLeft;
    while ((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
    if (ifgg) {
        return returnValue - 208;
    }
    return returnValue + calendar_offsetLeft;
}

function positionCalendar(inputObj, calendarDiv)
{
    calendarDiv.style.left = getleftPos(inputObj) + 'px';
    calendarDiv.style.top = getTopPos(inputObj) + 'px';
    if (iframeObj) {
        iframeObj.style.left = calendarDiv.style.left;
        iframeObj.style.top = calendarDiv.style.top;
        //// fix for EI frame problem on time dropdowns 09/30/2006
        iframeObj2.style.left = calendarDiv.style.left;
        iframeObj2.style.top = calendarDiv.style.top;
    }
}

function initCalendar(calendarId)
{
    if (MSIE) {
        iframeObj = document.createElement('IFRAME');
        iframeObj.style.filter = 'alpha(opacity=0)';
        iframeObj.style.position = 'absolute';
        iframeObj.border = '0px';
        iframeObj.style.border = '0px';
        iframeObj.style.backgroundColor = '#FF0000';
        //// fix for EI frame problem on time dropdowns 09/30/2006
        iframeObj2 = document.createElement('IFRAME');
        iframeObj2.style.position = 'absolute';
        iframeObj2.border = '0px';
        iframeObj2.style.border = '0px';
        iframeObj2.style.height = '1px';
        iframeObj2.style.width = '1px';
        //// fix for EI frame problem on time dropdowns 09/30/2006
        // Added fixed for HTTPS
        iframeObj2.src = '/images/spacer.gif';
        iframeObj.src = '/images/spacer.gif';
        document.body.appendChild(iframeObj2);  // gfb move this down AFTER the .src is set
        document.body.appendChild(iframeObj);
    }

    calendarDiv = document.createElement('DIV');
    calendarDiv.id = 'calendarDiv' + calendarId;
    calendarDiv.className = 'calendarDiv';
    calendarDiv.style.zIndex = 1000;

    document.body.appendChild(calendarDiv);
    writeTopBar(calendarId);
    writeCalendarContent();
    writeCloseBar(calendarDiv);
    displayArrows();
}

function calendarSortItems(a, b)
{
    return a / 1 - b / 1;
}

/**
 * Add number of days to myDate.
 * Return the new date.
 * @param myDate
 * @param days
 */
function addDaysToDate(startDate, days) {
    return new Date(startDate.getTime() + days * 24 * 60 * 60 * 1000);
}

/**
 * Added an extra parameter - numDays - which represents the number of days to add to the startDate to build the endDate.
 * @param inputFieldVar
 * @param format
 * @param calendarId1
 * @param calendarToClose
 * @param diffReturnDepart1
 * @param fromGG
 * @param offsetTop
 * @param endDateMonths
 */
function displayCalendar(inputFieldVar, format, calendarId1, calendarToClose, diffReturnDepart1, fromGG, offsetTop,
                         numDays) {
    diffReturnDepart = diffReturnDepart1;
    ifgg = fromGG;
    inputField = inputFieldVar;
    regionalFormat = format;
    if (offsetTop != null) {
        calendar_offsetTop = offsetTop;
    }
    triedToSetReturnDateToDepartDate = false;
    //If the other calendar is opened, than it must be closed
    closeCalendar(calendarToClose);
    calendarId = calendarId1;
    calendarToCloseId = calendarToClose;

    //determine the last active date displayed in the current calendar
    if (!numDays || isNaN(numDays)) {
        numDays = flightBookingDays;
    }
    endDate = new Date();
    endDate.setHours(0, 0, 0, 0);
    endDate = addDaysToDate(endDate, numDays);

    //determine the first active date displayed in the current calendar
    startDate = null;
    var departureDate = parseDateFromString(document.getElementById(departureElementId).value);
    if (departureDate != null) {
        //Set with the selected departure date values
        currentMonth = departureDate.getMonth();
        currentYear = departureDate.getFullYear();
    }
    else {
        currentMonth = null;
        currentYear = null;
    }
    if (calendarToCloseId == departureElementId) {
        var departureDateCalendar = document.getElementById(calendarToCloseId);
        //var departureDate = parseDateFromString(departureDateCalendar.value);
        var currentDate = new Date();
        currentDate.setHours(0, 0, 0, 0);
        if ((departureDate != null) && (departureDate - currentDate >= 0) && (departureDate - endDate <= 0)) {
            startDate = departureDate;
            if (diffReturnDepart != null) {
                startDate.setDate(startDate.getDate() + diffReturnDepart);
                endDate.setDate(endDate.getDate() + diffReturnDepart);
            }
        }
    }
    if (startDate == null) {
        startDate = new Date();
        startDate.setHours(0, 0, 0, 0);
    }

    createDate(inputField, format);

    //This is done for the calendar displaying
    if (currentYear) {
        if (currentYear.length == 2) {
            currentYear = 20 + currentYear;
        }
        if (currentYear.length == 1) {
            currentYear = 200 + currentYear;
        }
    }
    calendarDiv = document.getElementById('calendarDiv' + calendarId);
    if (!calendarDiv) {
        initCalendar(calendarId);
    }
    else {
        if (calendarDiv.style.display == 'block') {
            closeCalendar();
            return false;
        }
        writeCalendarContent();
        displayArrows();
    }

    returnFormat = format;
    returnDateTo = inputField;
    positionCalendar(inputFieldVar, calendarDiv);
    calendarDiv.style.visibility = 'visible';
    calendarDiv.style.display = 'block';
    if (iframeObj) {
        iframeObj.style.display = '';
        iframeObj.style.height = '140px';
        iframeObj.style.width = '195px';
        iframeObj2.style.display = '';
        iframeObj2.style.height = '140px';
        iframeObj2.style.width = '195px';
    }
}

/*
 * Takes the input data value and creates the data
 */
function createDate(inputFieldVar, format) {
    if (inputFieldVar.value.length > 0) {
        if (!format.match(/^[0-9]*?$/gi)) {
            var items = inputFieldVar.value.split(/[^0-9]/gi);
            //In IE if the date is set the items length is 3, and if the default is "DD/MM/YY" the length is 0
            //In Mozilla if the date is set the items length is 3, and if the default is "DD/MM/YY" the length is 9
            if (items.length == 3) {
                var positionArray = new Array();
                positionArray['m'] = format.indexOf('mm');
                if (positionArray['m'] == -1)positionArray['m'] = format.indexOf('m');
                positionArray['d'] = format.indexOf('dd');
                if (positionArray['d'] == -1)positionArray['d'] = format.indexOf('d');
                positionArray['y'] = format.indexOf('yy');

                var positionArrayNumeric = Array();
                positionArrayNumeric[0] = positionArray['m'];
                positionArrayNumeric[1] = positionArray['d'];
                positionArrayNumeric[2] = positionArray['y'];

                positionArrayNumeric = positionArrayNumeric.sort(calendarSortItems);
                var itemIndex = -1;
                for (var no = 0; no < positionArrayNumeric.length; no++) {
                    if (positionArrayNumeric[no] == -1)continue;
                    itemIndex++;
                    if (positionArrayNumeric[no] == positionArray['m']) {
                        currentMonth = items[itemIndex] - 1;
                        if (currentMonth) {
                            if (currentMonth.length == 1) {
                                currentMonth = 0 + currentMonth;
                            }
                        }
                        continue;
                    }
                    if (positionArrayNumeric[no] == positionArray['y']) {
                        currentYear = items[itemIndex];
                        continue;
                    }
                    if (positionArrayNumeric[no] == positionArray['d']) {
                        tmpDay = items[itemIndex];
                        if (tmpDay && tmpDay.length == 1) {
                            tmpDay = 0 + tmpDay;
                        }
                        continue;
                    }
                }

                currentMonth = currentMonth / 1;
                tmpDay = tmpDay / 1;
                if (currentYear == 0) {
                    noDateToDisplay = true;
                }

                //Verify if the date is valid
                if (currentMonth < 12 && currentMonth >= 0 && tmpDay != 0) {
                    var daysInMonth = daysInMonthArray[currentMonth];
                    if (daysInMonth == 28) {
                        if (isLeapYear(currentYear))daysInMonth = 29;
                    }
                    if (tmpDay > daysInMonth) {
                        setTodaysDate();
                        noDateToDisplay = true;
                    }
                }
                else {
                    setTodaysDate();
                    noDateToDisplay = true;
                }
            }
            else {
                noDateToDisplay = true;
                //This means that the input value is DD/MM/YY"
                if (calendarToCloseId == departureElementId && !triedToSetReturnDateToDepartDate) {
                    var inputdate = document.getElementById(calendarToCloseId);
                    if (!verifyIfDefaultDate(calendarToCloseId)) {
                        triedToSetReturnDateToDepartDate = true;
                        createDate(inputdate, format);
                    }
                }
            }
        }
        else {
            var monthPos = format.indexOf('mm');
            currentMonth = inputFieldVar.value.substr(monthPos, 2) / 1 - 1;
            var yearPos = format.indexOf('yy');
            currentYear = inputFieldVar.value.substr(yearPos, 4);
            var dayPos = format.indexOf('dd');
            tmpDay = inputFieldVar.value.substr(dayPos, 2);
        }
    }
    else {
        noDateToDisplay = true;
        var d = new Date();
        currentMonth = d.getMonth();
        currentYear = d.getFullYear();
        tmpDay = d.getDate();
    }

    inputYear = currentYear;
    inputMonth = currentMonth;
    inputDay = tmpDay / 1;
}

/*
 * This function is used to change the return date, if is not set, with the value entered in the departure date field
 */
function changeDate(dateField, format) {
    //This check is made for solving the IE issue in order to move cursor in the date field
    if (wasBefore && wasBefore == dateField.value) {
        return;
    }
    wasBefore = dateField.value;
    noDateToDisplay = false;
    currentYear = 0;
    createDate(dateField, format);
    if (!noDateToDisplay) {
        returnFormat = format;
        pickDate(tmpDay);
    }
}

/**
 * Sets only the date input field to be selected. This will call the displayCalendar function
 */
function displayCalendarOnClick(inputFieldVar, format, calendarId1, calendarToClose, diffReturnDepart1, fromGG,
                                offsetTop, noOfDays) {
    diffReturnDepart = diffReturnDepart1;
    inputFieldVar.select();
    //This is a fixed for Safari/419.3(Without this the calendar is not displayed anymore)
    //The check inputFieldVar.value == '' is done to fix the IE issue that when the input field is empty String the calendar is not displayed anymore
    if ((inputFieldVar.value == '') || Safari_Macintosh) {
        displayCalendar(inputFieldVar, format, calendarId1, calendarToClose, diffReturnDepart1, fromGG, offsetTop, noOfDays);
    }
}

function setTodaysDate() {
    var d = new Date();
    currentMonth = d.getMonth();
    currentYear = d.getFullYear();
    tmpDay = d.getDate();
}

/**
 * Parses the date from the given string. The expected format is DD/MM/YY. Returns null in case the given string
 * does not contain a valid date.
 */
function parseDateFromString(depDateStr) {

    var items = depDateStr.split(/[^0-9]/gi);

    //In IE if the date is set the items length is 3, and if the default is "DD/MM/YY" the length is 0
    //In Mozilla if the date is set the items length is 3, and if the default is "DD/MM/YY" the length is 9
    if (items.length == 3) {

        var dayPosition = regionalFormat.indexOf('dd') / 3;
        var monthPosition = regionalFormat.indexOf('mm') / 3;
        var yearPosition = regionalFormat.indexOf('yy') / 3;

        var dateFields = depDateStr.split('/');
        if (dateFields.length != 3) {
            //invalid date format
            return null;
        }
        if ((dateFields[0] / 1 != dateFields[0]) || (dateFields[1] / 1 != dateFields[1]) || dateFields[2] / 1
                != dateFields[2]) {
            //some of the date fields are not numeric
            return null;
        }

        var day = dateFields[dayPosition] / 1;
        var month = dateFields[monthPosition] / 1 - 1;
        var year = dateFields[yearPosition] / 1;
        if (dateFields[2].length < 4) {
            var tmpDate = new Date();
            var century = tmpDate.getFullYear() - (tmpDate.getFullYear() % 1000);
            year = century + year;
        }
        var result = new Date(year, month, day);
        if ((result.getFullYear() != year) || (result.getMonth() != month) || (result.getDate() != day)) {
            //invalid date
            return null;
        }
    }
    else {
        return null;
    }
    return result;
}




