﻿
/*
 This function wil check if page data is valid and then format the redirection URL accordingly
*/

function CheckSearchRedirect(SkeletonURL, DefaultLanguage, windowTitle, fromId, toId, departDateId, ariveDateId, MessageId, ErrMsgDepartdate, ErrMsgNeedAllFields) {
    
    
    // Start with given values
    var isValid = Boolean(true);
    var theURL = "" + SkeletonURL;      
    var theLanguage = "" + DefaultLanguage;

    // Get the values from the filed in data
    var fromLocation = document.getElementById(fromId);
    var toLocation = document.getElementById(toId);
    var departDate = document.getElementById(departDateId + '_txtInput');
    var ariveDate = document.getElementById(ariveDateId + '_txtInput');
    var xMsg = document.getElementById(MessageId);
    xMsg.innerHTML = '';

        
    // Reset previous errors
    fromLocation.style.backgroundColor = '#FFFFFF';
    toLocation.style.backgroundColor = '#FFFFFF';
    departDate.style.backgroundColor = '#FFFFFF';
    ariveDate.style.backgroundColor = '#FFFFFF';
    
    var fromLoc = String("");
    var ToLoc = String("");
    var depDateString = "";
    var ariveDateString = "";

    if ((fromLocation != undefined) && (fromLocation.value != '') && (isValid)) { fromLoc = fromLocation.value.toUpperCase(); }
    else { isValid = false; fromLocation.style.backgroundColor = '#E2DDD8'; }
    if ((toLocation != undefined) && (toLocation.value != '') && (isValid)) { ToLoc = toLocation.value.toUpperCase(); }
    else { isValid = false; toLocation.style.backgroundColor = '#E2DDD8'; }
    if ((departDate != undefined) && (departDate.value != '') && (isValid)) { depDateString = getDateInFormat(departDate.value); }
    else { isValid = false; departDate.style.backgroundColor = '#E2DDD8'; }
    if ((ariveDate != undefined) && (ariveDate.value != '') && (isValid)) { ariveDateString = getDateInFormat(ariveDate.value); }
    else { isValid = false; ariveDate.style.backgroundColor = '#E2DDD8'; }
    if (theLanguage == '') {isValid = false;}
    if (theURL == '') { isValid = false; }

    if (!isValid) { xMsg.innerHTML = "*" + ErrMsgNeedAllFields; }
    if  ((isValid) && (convertToDate(departDate.value)) > convertToDate(ariveDate.value)) {
        isValid = false;
        xMsg.innerHTML = "*" + ErrMsgDepartdate;
    }
 
    // Only continue if all values have been passed successfully
    if (isValid) {
        theURL = theURL.replace("<Language>", theLanguage);
        theURL = theURL.replace("<DepartureLocation>", fromLoc);
        theURL = theURL.replace("<ArivalLocation>", ToLoc);
        theURL = theURL.replace("<DepartureDate>", depDateString);
        theURL = theURL.replace("<ArivalDate>", ariveDateString);

        // open the new window if everything completed successfully
        var day = new Date();
        var id = day.getTime();
        var windowObjectReference = window.open(theURL, id, 'width=1024,height=768,scrollbars=yes,toolbar=yes,location=yes,status=yes');
    }
}

function getDateInFormat(datToConvert) {


    var theDay = "" + datToConvert.substring(0, 2);
    var theMonth = "" + datToConvert.substring(3,5);
    var theYear = "" + datToConvert.substring(6, 10);

    return "" + theYear + theMonth  + theDay + "0000";
   
}


function CheckExtensiveRedirect(SkeletonURL, DefaultLanguage, windowTitle)
{
    // Start with given values
    var isValid = Boolean(true);
    var theURL = "" + SkeletonURL;
    var theLanguage = "" + DefaultLanguage;

    if (theLanguage == '') { isValid = false }
    if (theURL == '') { isValid = false }

    if (isValid) 
    {
        theURL = theURL.replace("<Language>", theLanguage);

        // open the new window if everything completed successfully
        var day = new Date();
        var id = day.getTime();
        var windowObjectReference = window.open(theURL, id, 'width=1024,height=768,scrollbars=yes,toolbar=yes,location=yes,status=yes');
    }
}


function convertToDate(fromDate) {
    var theDay = "" + fromDate.substring(0, 2);
    var theMonth = "" + fromDate.substring(3, 5);
    var theYear = "" + fromDate.substring(6, 10);

    var datNew = new Date(theMonth + "/" + theDay + "/" + theYear);
    return datNew;
}