function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function DeleteCookie(name, path, domain) {
    if (getCookie(name) != "") document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=" + Date();
}

function GetDomain() {
    var subDomain = '';
    var finalvalue = '';
    var host = document.domain;
    if (host.length > 2) {
        var last = host.lastIndexOf('.');
        var idx = host.lastIndexOf('.', last - 1);
        subDomain = host.substring(0, idx);
        finalvalue = host.replace(subDomain, '');
    }
    return finalvalue;
}

function loadCookieValues() {
   
  

    // Get Cookie values
    var oneway = getCookie("OneWay");
    var roundtrip = getCookie("RoundTrip");
    var multidestination = getCookie("MultiDestination");

    if (oneway != "") {
       
        //$("#Multidestination").hide();
        $("#OnewayRoundtrip").show();
        $('#OnewayRoundtrip').load('include/roundtrip.html', function(response, status, xhr) {
            if (status == "success") {

                ApplyCSSOneWay();
                ApplyCookieOneway(oneway);               
                $('#SearchTypeOneWay').attr("checked", "checked");
            }
        });

    }

    else if (roundtrip != "") {

        //$("#Multidestination").hide();
        $("#OnewayRoundtrip").show();
        $('#OnewayRoundtrip').load('include/roundtrip.html', function(response, status, xhr) {
            if (status == "success") {

                ApplyCSSRoundTrip();
                ApplyCookieRoundtrip(roundtrip);                
                $('#SearchTypeRoundTrip').attr("checked", "checked");
            }
        });         
      
    }

    else if (oneway == "" && roundtrip == "" && multidestination != "") {
   
        var data = multidestination.split('|');
        var i = 1;
        var l = 0;
       // $("#OnewayRoundtrip").hide();
        $("#Multidestination").show();
        $('#Multidestination').load('include/multidestination.html', function(response, status, xhr) {
            if (status == "success") {
                ApplyCSSMultidestination();
                ApplyCookieMultiDestination(multidestination);
                $('#SearchTypeMultiDestination').attr("checked", "checked");

            }
        });
           
     }
    else {
        loadDateText();
       
    }


}

function ApplyCookieOneway(oneway) {
    var strArrary, fromCity1, tocity1, departuredate;
    strArrary = oneway.split('|');
    fromCity1 = strArrary[0];
    tocity1 = strArrary[1];
    departuredate = strArrary[2];
    $("#fromCity").val(fromCity1)
    $("#toCity").val(tocity1);
    $("#flyDate_from").val(departuredate);
}

function ApplyCookieRoundtrip(roundtrip) {
    var strArrary, fromCity1, tocity1, departuredate, arrivalDate;
    strArrary = roundtrip.split('|');
    fromCity1 = strArrary[0];
    tocity1 = strArrary[1];
    departuredate = strArrary[2];
    arrivalDate = strArrary[3];
    $("#fromCity").val(fromCity1);
    $("#toCity").val(tocity1);
    $("#flyDate_from").val(departuredate);
    $("#flyDate_return").val(arrivalDate);
}

function ApplyCookieMultiDestination(multidestination) {
    var strArrary, fromCity1, tocity1, departuredate;
    var data = multidestination.split('|');
    var i = 1;
    var l = 0;
    for (l = 0; l < data.length; l = l + 3) {
        fromCity1 = data[l];
        tocity1 = data[l + 1];
        departuredate = data[l + 2];
        if (i >= 4) newaddSegment();
        $("#fromCity_" + i).val(fromCity1);
        $("#toCity_" + i).val(tocity1);
        $("#flyDate_" + i).val(departuredate);
        i++;
    }
}

function ApplyCSSOneWay() {
    $("#ReturnFlightTR3").hide();
    $("#ReturnFlightTR4").hide();
   $("#mid").show();

}

function ApplyCSSRoundTrip() {
    $("#ReturnFlightTR3").show();
    $("#ReturnFlightTR4").show();
   $("#mid").show();
}

function ApplyCSSMultidestination() {
    
    $("#mid").hide();
  

}

