﻿// JScript File
function makeOneWay() {
    $("#" + txtToDate).datepicker("disable");
}
function makeRoundTrip() {
    $("#" + txtToDate).datepicker("enable");
}
function makeDomestic() {
    $("#" + ddlFromStation).attr("style", "display:inline");
    $("#" + ddlToStation).attr("style", "display:inline");
    $("#" + txtFromStation).attr("style", "display:none");
    $("#" + txtToStation).attr("style", "display:none");
    $("#rowAirlineList").show();
    document.getElementById('AirDetails').value = 'D';
}
function makeInternational() {
    $("#" + ddlFromStation).attr("style", "display:none");
    $("#" + ddlToStation).attr("style", "display:none");
    $("#" + txtFromStation).attr("style", "display:inline");
    $("#" + txtToStation).attr("style", "display:inline");
    if(($('input[id$="txtFromStation"]').val().toUpperCase().indexOf('INDIA')!=-1)
        &&  ($('input[id$="txtToStation"]').val().toUpperCase().indexOf('INDIA')!=-1)){
            $("#rowAirlineList").show();
    }else{
            $("#rowAirlineList").hide();
    }
    document.getElementById('AirDetails').value = 'I';
}
$(function () {
    var cache = {};
    var fromCity = document.getElementById('ctl00_CPBody_TCOneWay_txtFromCity');
    var toCity = document.getElementById('ctl00_CPBody_TCOneWay_txtToCity');
    $("#" + txtFromStation).autocomplete('GetCityNames.ashx', {
        minChars: 3,
        cacheLength:10,
        autoFill: false,
        mustMatch: true,
        selectFirst: true,
        forceSelection: true
    })
    $("#" + txtToStation).autocomplete('GetCityNames.ashx', {
        minChars: 3,
        cacheLength: 10,
        autoFill: false,
        mustMatch: true,
        selectFirst: true,
        forceSelection: true
    })
    if(fromCity!=null && fromCity!='undefined')
    {
     $("#" + txtFromCity).autocomplete('GetCityNames.ashx', {
        minChars: 3,
        cacheLength:10,
        autoFill: false,
        mustMatch: true,
        selectFirst: true,
        forceSelection: true
    })
    }
    if(toCity!=null && toCity!='undefined')
    {
    $("#" + txtToCity).autocomplete('GetCityNames.ashx', {
        minChars: 3,
        cacheLength: 10,
        autoFill: false,
        mustMatch: true,
        selectFirst: true,
        forceSelection: true
    })
    }
    $("#" + txtFromDate).datepicker({
        showAnim: '',
        dateFormat: 'dd/mm/yy',
        showOn: 'both',
        buttonImage: 'images/calendar_icon.gif',
        buttonImageOnly: true,
        prevText: '',
        nextText: '',
        minDate: 0,
        maxDate: '+1Y',
        numberOfMonths: 2
    });
    $("#" + txtToDate).datepicker({
        showAnim: '',
        dateFormat: 'dd/mm/yy',
        showOn: 'both',
        buttonImage: 'images/calendar_icon.gif',
        buttonImageOnly: true,
        prevText: '',
        nextText: '',
        minDate: 0,
        maxDate: '+1Y',
        numberOfMonths: 2
    });
    //    if ($("#" + rbDomestic)[0].checked) {
    //        makeDomestic();
    //    } else {
    //        if ($("#" + rbInternational)[0].checked) {
    //            makeInternational();
    //        } else {
    //            makeDomestic();
    //        }
    //    }
    makeInternational();
    if ($("#" + rdOne)[0].checked) {
        makeOneWay();
    } else {
        if ($("#" + rdRound)[0].checked) {
            makeRoundTrip();
        } else {
            makeOneWay();
        }
    }
    $("#" + rbDomestic).bind("click", makeDomestic)
    $("#" + rbInternational).bind("click", makeInternational)
    $("#" + rdOne).bind("click", makeOneWay)
    $("#" + rdRound).bind("click", makeRoundTrip)
});

function setCount(LCount,ICount)
{
document.getElementById("hdnLccCount").value = LCount; 
document.getElementById("hdnIATAcount").value = ICount;
if ((parseInt(LCount) < 1) || (parseInt(LCount) > 0 && parseInt(ICount) > 0))
    {
    //selectIATAItems();
    }
else
    {
    selectLCCItems();
    } 
}
function fnAirlineChecked(obj, isLCC) {
    //debugger;
    var LccCount = document.getElementById("hdnLccCount");
    var IATACount = document.getElementById("hdnIATAcount");
    if (isLCC) {
        if (obj.checked) {
            //LCC Checked Event
            LccCount.value = parseInt(LccCount.value) + 1;
            if ((LccCount.value == 1) && (IATACount.value < 1)) {
                selectLCCItems();
            }
        } else {
            //LCC Unchecked Event
            LccCount.value = parseInt(LccCount.value) - 1;
            if (LccCount.value < 1 && IATACount.value < 1) {
                selectIATAItems();
            }
        }
    } else {
        if (obj.checked) {
            //IATA Checked Event
            IATACount.value = parseInt(IATACount.value) + 1;
            if (IATACount.value == 1 && LccCount.value > 0) {
                selectIATAItems();
            }
        } else {
            //IATA Unchecked Event
            IATACount.value = parseInt(IATACount.value) - 1;
            if (IATACount.value < 1 && LccCount.value > 0) {
                selectLCCItems();
            }
        }
    }
    return false;
}
function selectLCCItems() {
    //debugger;
    var ddlADULT = document.getElementById('ddlAdult');
    var ddlCHILD = document.getElementById('ddlChild');
    var ddlINFANT = document.getElementById('ddlInfant');
    addChkItms(ddlADULT, ddlADULT.options.length + 1, ddlADULT.options.length + 3);
    addChkItms(ddlCHILD, ddlCHILD.options.length, ddlCHILD.options.length + 2);
    addChkItms(ddlINFANT, ddlINFANT.options.length, ddlINFANT.options.length + 1);
}
function selectIATAItems() {
    var ddlADULT = document.getElementById('ddlAdult');
    var ddlCHILD = document.getElementById('ddlChild');
    var ddlINFANT = document.getElementById('ddlInfant');
    removeChkItms(ddlADULT, 3);
    removeChkItms(ddlCHILD, 3);
    removeChkItms(ddlINFANT, 2);
}
function removeChkItms(objChk, numEle) {
    for (var i = objChk.options.length - 1; numEle > 0; numEle--) {
        objChk.remove(i--);
    }
}
function addChkItms(objChk, startIndex, numEle) {
    for (var i = startIndex; i <= numEle; i++) {
        var opt = document.createElement('OPTION');
        opt.text = (i.length > 1) ? i : '0' + i;
        opt.value = (i.length > 1) ? i : '0' + i;
        objChk.options.add(opt);
    }
}
function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}
function Validation() {
    //debugger;
    // regular expression to match required date format (dd/mm/yyyy)
    var regExp = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    var AirDetails = document.getElementById('AirDetails');
    var ddlADULT = document.getElementById('ddlAdult');
    var ddlCHILD = document.getElementById('ddlChild');
    var ddlINFANT = document.getElementById('ddlInfant');
    //Added by Priyanka 12/10/10
    var hdnSpe = document.getElementById('hdnSpe');
    if (AirDetails.value == 'I') {
        var txtToStation1 = $("#" + txtToStation)[0];
        var txtFromStation1 = $("#" + txtFromStation)[0];
        var splitToStation = ("" + txtToStation1.value).split("(");
        var splitFromStation = ("" + txtFromStation1.value).split("(");
        if (txtFromStation1.value == "" || txtFromStation1.value == "Enter City Name" || txtFromStation1.value == "No Match Found") {
            alert("Enter From City");
            txtFromStation1.focus();
            return false;
        }
        if (splitFromStation.length < 2) {
            txtFromStation1.focus();
            alert("From City Name Must Be With City Code");
            return false;
        }
        if (txtToStation1.value == "" || txtToStation1.value == "Enter City Name" || txtToStation1.value == "No Match Found") {
            alert("Enter To City");
            txtToStation1.focus();
            return false;
        }
        if (splitToStation.length < 2) {
            txtToStation1.focus();
            alert("To City Name Must Be With City Code");
            return false;
        }
        if (txtToStation1.value == txtFromStation1.value) {
            alert("Enter From and To City names are different.");
            txtToStation1.focus();
            return false;
        }
        //var fromCountry = trim(txtFromStation1.value.split(",")[1].toString())
        //var toCountry = trim(txtToStation1.value.split(",")[1].toString())
        //if (fromCountry == toCountry && fromCountry.toUpperCase() == "INDIA") {
        //   alert("Please select atleast one airport outside INDIA or go for domestic search.")
        //    return false;
        //}
    }
    else {
        var ddlFromStation1 = $("#" + ddlFromStation)[0];
        var ddlToStation1 = $("#" + ddlToStation)[0];
        var intFromStation = ddlFromStation1.selectedIndex;
        var intToStation = ddlToStation1.selectedIndex;
        if (ddlFromStation1.options[intFromStation].value == "Select") {
            alert("Select From Station.");
            ddlFromStation1.focus();
            return false;
        }
        if (ddlFromStation1.options[intFromStation].value == "") {
            alert("Select From Station.");
            ddlFromStation1.focus();
            return false;
        }
        if (ddlToStation1.options[intToStation].value == "Select") {
            alert("Select To Station.");
            ddlToStation1.focus();
            return false;
        }
        if (ddlToStation1.options[intToStation].value == "") {
            alert("Select To Station.");
            ddlToStation1.focus();
            return false;
        }
        if (ddlFromStation1.options[intFromStation].value == ddlToStation1.options[intToStation].value) {
            alert("Enter From and To City names are different.");
            ddlToStation1.focus();
            return false;
        }
    }
    var rdRound1 = $("#" + rdRound)[0];
    var txtdate = $("#" + txtFromDate)[0];
    if (txtdate) {
        var valDate = trim(txtdate.value);
        if (txtdate.value == "" || txtdate.value == "Enter Date") {
            alert("Enter 'Depart On' Date");
            txtdate.focus();
            return false;
        }
        else if (!valDate.match(regExp)) {
            alert("Enter Valid 'Depart On' Date. (Format: dd/mm/yyyy)");
            txtdate.focus();
            return false;
        }
        else {
            var strFrom = txtdate.value.split('/');
            if (parseInt(strFrom[0], 10) <= 0 || parseInt(strFrom[0], 10) > 31) {
                alert("Enter Day Value From 1 To 31 In 'Depart On' Date. (Format: dd/mm/yyyy)");
                txtdate.focus();
                return false;
            }
            if (parseInt(strFrom[1], 10) <= 0 || parseInt(strFrom[1], 10) > 12) {
                alert("Enter Month Value From 1 To 12 In 'Depart On' Date. (Format: dd/mm/yyyy)");
                txtdate.focus();
                return false;
            }
            var dtToday = new Date();
            if (parseInt(strFrom[2], 10) < dtToday.getFullYear() || parseInt(strFrom[2], 10) >= (dtToday.getFullYear() + 2)) {
                alert("Year Value In 'Depart On' Date Must Be Current Year Or Next Year Only. (Format: dd/mm/yyyy)");
                txtdate.focus();
                return false;
            }
            if (isDate(parseInt(strFrom[0], 10), parseInt(strFrom[1], 10), parseInt(strFrom[2], 10)) == false) {
                alert("Enter Valid 'Depart On' Date. (Format: dd/mm/yyyy)");
                txtdate.focus();
                return false;
            }
            var dtFrom = new Date(parseInt(strFrom[2], 10), parseInt(strFrom[1], 10) - 1, parseInt(strFrom[0], 10));
            //var dtFrom  = new Date(strFrom[1]+'/'+strFrom[0]+ '/' + strFrom[2]);
            dtFrom = formatDate(dtFrom, 'yyyy-MM-dd');
            dtToday = formatDate(dtToday, 'yyyy-MM-dd');
            if (dtFrom < dtToday) {
                alert("'Depart On' Date Cannot Be Less Than Today's Date.");
                txtdate.focus();
                return false;
            }
        }
    }
    else {
        alert("Enter 'Depart On' Date");
        return false;
    }
    //Added by Priyanka 12/10/10
    if (rdRound1.checked == true || hdnSpe.value=="1") {
        var txtReturnOn = $("#" + txtToDate)[0];
        var valDate = trim(txtReturnOn.value);
        if (txtReturnOn.value == "" || txtReturnOn.value == "Enter Date") {
            alert("Enter 'Return On' Date");
            txtReturnOn.focus();
            return false;
        }
        else if (!valDate.match(regExp)) {
            alert("Enter Valid 'Return On' Date. (Format: dd/mm/yyyy)");
            txtReturnOn.focus();
            return false;
        }
        var strFrom = txtdate.value.split('/')
        var strTo = txtReturnOn.value.split('/')
        if (parseInt(strTo[0], 10) <= 0 || parseInt(strTo[0], 10) > 31) {
            alert("Enter Day Value From 1 To 31 In 'Return On' Date. (Format: dd/mm/yyyy)");
            txtReturnOn.focus();
            return false;
        }
        if (parseInt(strTo[1], 10) <= 0 || parseInt(strTo[1], 10) > 12) {
            alert("Enter Month Value From 1 To 12 In 'Return On' Date. (Format: dd/mm/yyyy)");
            txtReturnOn.focus();
            return false;
        }
        var dtToday = new Date();
        if (parseInt(strTo[2], 10) < dtToday.getFullYear() || parseInt(strTo[2], 10) >= (dtToday.getFullYear() + 2)) {
            alert("Year Value In 'Return On' Date Must Be Current Year Or Next Year Only. (Format: dd/mm/yyyy)");
            txtReturnOn.focus();
            return false;
        }
        if (isDate(parseInt(strTo[0], 10), parseInt(strTo[1], 10), parseInt(strTo[2], 10)) == false) {
            alert("Enter Valid 'Return On' Date. (Format: dd/mm/yyyy)");
            txtReturnOn.focus();
            return false;
        }
        var dtFrom = new Date(parseInt(strFrom[2], 10), parseInt(strFrom[1], 10) - 1, parseInt(strFrom[0], 10));
        var dtTo = new Date(parseInt(strTo[2], 10), parseInt(strTo[1], 10) - 1, parseInt(strTo[0], 10));
        dtFrom = formatDate(dtFrom, 'yyyy-MM-dd');
        dtTo = formatDate(dtTo, 'yyyy-MM-dd');
        //var dtToday = new Date();
        dtToday = formatDate(dtToday, 'yyyy-MM-dd');
        if (dtTo < dtToday) {
            alert("'Return On' Cannot Be Less Than Today's Date.");
            txtReturnOn.focus();
            return false;
        }
        if (dtFrom > dtTo) {
            alert("'Depart On' Date Must Be Smaller Than 'Return On' Date.");
            txtReturnOn.focus();
            return false;
        }
    }
    
    //debugger;
    if (ddlADULT.options[ddlADULT.selectedIndex].value < ddlINFANT.options[ddlINFANT.selectedIndex].value) {
        alert("You can't Book a greater number of Infants than Adults.");
        ddlINFANT.focus();
        return false;
    }
    var AdultValue = ddlADULT.options[ddlADULT.selectedIndex].value;
    var ChildValue = ddlCHILD.options[ddlCHILD.selectedIndex].value;
    if (AdultValue == '08') {
        AdultValue = '8';
    }
    else if (AdultValue == '09') {
        AdultValue = '9';
    }
    if (ChildValue == '08') {
        ChildValue = '8';
    }
    else if (ChildValue == '09') {
        ChildValue = '9';
    }
    var AdultChild = parseInt(AdultValue) + parseInt(ChildValue);
    var validCnt = 0;
    var LccCount = document.getElementById("hdnLccCount");
    var IATACount = document.getElementById("hdnIATAcount");
    (LccCount.value > 0 && IATACount.value < 1) ? validCnt = 9 : validCnt = 6;
    if (parseInt(AdultChild) > validCnt) {
        alert("Maximum " + validCnt + "  Passengers Are Allowed For Booking.");
        ddlCHILD.focus();
        return false;
    }
    SetSearchAirlinesDet();
}
function y2k(number) {
    return (number < 1000) ? number + 1900 : number;
}
function isDate(day, month, year) {
    // checks if date passed is valid
    // will accept dates in following format:
    // isDate(dd,mm,ccyy), or
    // isDate(dd,mm) - which defaults to the current year, or
    // isDate(dd) - which defaults to the current month and year.
    // Note, if passed the month must be between 1 and 12, and the
    // year in ccyy format.
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()) : year);
    month = ((!month) ? today.getMonth() : month - 1);
    if (!day) return false
    var test = new Date(year, month, day);
    if ((y2k(test.getYear()) == year) && (month == test.getMonth()) && (day == test.getDate())) return true;
    else return false
}
function formatDate(date, format) {
    format = format + "";
    var result = "";
    var i_format = 0;
    var c = "";
    var token = "";
    var y = date.getYear() + "";
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var E = date.getDay();
    var H = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var yyyy, yy, MMM, MM, dd, hh, h, mm, ss, ampm, HH, H, KK, K, kk, k;
    // Convert real date parts into formatted versions
    var value = new Object();
    if (y.length < 4) {
        y = "" + (y - 0 + 1900);
    }
    value["y"] = "" + y;
    value["yyyy"] = y;
    value["yy"] = y.substring(2, 4);
    value["M"] = M;
    value["MM"] = LZ(M);
    value["MMM"] = MONTH_NAMES[M - 1];
    value["NNN"] = MONTH_NAMES[M + 11];
    value["d"] = d;
    value["dd"] = LZ(d);
    value["E"] = DAY_NAMES[E + 7];
    value["EE"] = DAY_NAMES[E];
    value["H"] = H;
    value["HH"] = LZ(H);
    if (H == 0) {
        value["h"] = 12;
    }
    else if (H > 12) {
        value["h"] = H - 12;
    }
    else {
        value["h"] = H;
    }
    value["hh"] = LZ(value["h"]);
    if (H > 11) {
        value["K"] = H - 12;
    } else {
        value["K"] = H;
    }
    value["k"] = H + 1;
    value["KK"] = LZ(value["K"]);
    value["kk"] = LZ(value["k"]);
    if (H > 11) {
        value["a"] = "PM";
    }
    else {
        value["a"] = "AM";
    }
    value["m"] = m;
    value["mm"] = LZ(m);
    value["s"] = s;
    value["ss"] = LZ(s);
    while (i_format < format.length) {
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        if (value[token] != null) {
            result = result + value[token];
        }
        else {
            result = result + token;
        }
    }
    return result;
}
function LZ(x) {
    return (x < 0 || x > 9 ? "" : "0") + x
}
var MONTH_NAMES = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var DAY_NAMES = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
function SetSearchAirlinesDet() {
    //debugger
    var TBLSAL = document.getElementById('rowAirlineList');
    var strAirlines = "";
    if (TBLSAL) {
        if (TBLSAL.style.display != 'none') {
            for (var i = 0; i < TBLSAL.childNodes.length; i++) {
                var objTblTD = TBLSAL.childNodes[i];
                if (objTblTD) {
                    for (var j = 0; j < objTblTD.childNodes.length; j++) {
                        var objTbl = objTblTD.childNodes[j];
                        if (objTbl.tagName == "TABLE") {
                            for (var j = 0; j < objTbl.rows.length; j++) {
                                var tblRows = objTbl.rows[j];
                                for (var k = 0; k < tblRows.cells.length; k++) {
                                    var tblCells = tblRows.cells[k];
                                    for (var t = 0; t < tblCells.childNodes.length; t++) {
                                        var objChk = tblCells.childNodes[t];
                                        if (objChk) {
                                            if (objChk.tagName == "INPUT" && objChk.type == "checkbox") {
                                                if (objChk.checked == true) {
                                                    strAirlines = strAirlines + tblCells.childNodes[t + 1].innerHTML + ",";
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (strAirlines != "") {
        strAirlines = strAirlines.substring(0, strAirlines.length - 1);
    }
    $("#" + txtSearchAirlines)[0].value = strAirlines;
    return true;
}
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
