﻿function formatResult(row) { return row[1]; }
function ac(id) { initAuto(id); initRes(id); } 
function initAuto(id) { return $(id).autocomplete("/pages/airport.aspx", { width: 260, selectFirst: true, formatResult: formatResult }); }
function initRes(id) { return $(id).result(function(event, data, formatted) { if (data) { $(this).parent().next().find("input").val(data[1]); }}); }
function dp(id, vg, ct) { return $(id).datepicker({showOn: 'both', buttonImageOnly: true, buttonImage: '/images/cal.gif', minDate: 1, maxDate: 365, onSelect: function() { val(vg, ct) }}); }
function val(vg, ct)
{
    var pop = false;
    if (vg == 'Flights') {
        pop = true
        var radios = document.getElementById(ct).getElementsByTagName('input');      
        if (radios[0].checked) {
            vg = 'RoundTrip'
        } else if (radios[1].checked) {
            vg = 'OneWay'
        } else if (radios[2].checked) {
            vg = 'MultiCity'
        }
    }
    var res = Page_ClientValidate(vg);   
    if (res) {
        if (pop) { Pops(); }
        return true;
    } else {
        return false;
    }
}

// Sel Pages //

function BuildSlider(sliderid, min, max, displayid, filterid, format)
{
    $(displayid).text(displayFormat(min, format) + " to " + displayFormat(max, format));
    $(sliderid).slider({  
        min: min, 
        max: max,
        range: true,
        slide: function(e, ui) { 
            $(displayid).text(displayFormat($(this).slider('value', 0), format) + ' to ' + displayFormat($(this).slider('value', 1), format));
        },
        stop: function(e, ui) { 
            if (!ClearingFilters)
            {
                $("#ProgressDiv").dialog("open");
                ResultsTable.fnFilter($(this).slider('value', 0) + "|" + $(this).slider('value', 1), filterid);
                setTimeout('$("#ProgressDiv").dialog("close")',30);
            }
        }
    }); 
}

function Checks(name)
{
	$("input[@name=" + name + "]").each(function()
	{
		this.checked = true;
	});
}
function ChecksNot(name)
{
	$("input[@name=" + name + "]").each(function()
	{
		this.checked = false;
	});
}

function FilterString(string, column)
{
    $("#ProgressDiv").dialog("open");
    var sfilter = "";
    $("input[@name=" + string + "]").each(function()
	{
	    if (this.checked) {
		    sfilter = sfilter + this.value + "|";
		}
	});
    if (sfilter == "") { 
        sfilter = "--"; 
    } else {
    	sfilter = sfilter.substring(0, sfilter.length - 1);
    }
    ResultsTable.fnFilter(sfilter, column);
    setTimeout('$("#ProgressDiv").dialog("close")',30);
}

function displayFormat(num, format)
{
    if (format == "") {
        return num;
    } else if (format == "curr") {
        return formatCurrency(num);
    } else if (format == "dura") {
        return formatDuration(num)
    } else if (format == "time") {
        return formatTime(num);
    }
}

function formatCurrency(num) 
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function formatDuration(num)
{
    var Hours = Math.floor(num/60);
    var Minutes = Math.round(num%60);
    var Time = Hours + 'hr ' + Minutes + 'mn';

    return Time;
}
function formatTime(num)
{
    var M;
    var Hours = Math.floor(num/60);
    if (Hours >= 12) {
        if (Hours > 12)
            Hours = Hours - 12;
        M = ' pm'
    }else{
        if (Hours == 0)
            Hours = 12
        M = ' am'            
    }
    var Minutes = Math.round(num%60);
    if (Minutes < 10) {
        Minutes = '0' + Minutes;
    }
    
    var Time = Hours + ':' + Minutes + M;
    return Time;
}
function QuoteDetailPop(sURL) {
	nw=window.open(sURL,'QuoteDetail','width=760, height=495, resizable=yes, scrollbars=yes');
	nw.focus();
}    
// Traffic rescue popup functionality start

 

// whether or not user is leaving the site

      var leaving = true; 

      

// add the checkUrl click event to all buttons and anchor tags in order to prevent popup when clicking 

// internal links and radio buttons

 function addClickEvents()

 {

      // attach to all a tags

      var e = document.getElementsByTagName("a");           

      for(var i=0;i<e.length;i++) 

      {     

            

            // for non-ie

            if (e[i].addEventListener)

            {     

                  e[i].addEventListener("click",cancelExitPopup,false);

            }

            // for ie

            else if (e[i].attachEvent)

            {

                  e[i].attachEvent('onclick',cancelExitPopup);

            }

      };

      // attach to all buttons and radio buttons otherwise popup will occur on postbacks

      var buttons = document.getElementsByTagName("input")

      {

            for(var i=0;i<buttons.length;i++) 

            {

                  if(buttons[i].type == 'submit' || buttons[i].type =='radio')

                  {

                        // for non-ie

                        if (buttons[i].addEventListener)

                        {     

                              buttons[i].addEventListener("click",cancelExitPopup,false);

                        }

                        // for ie

                        else if (buttons[i].attachEvent)

                        {

                              buttons[i].attachEvent('onclick',cancelExitPopup);

                        }

                  }                       

            }           

      }           

} // add Click Events

      

// internal links and buttons fire this event to prevent popup

function cancelExitPopup(href)

{     

      leaving = false;

}
 
      

// display exit popup window if user is leaving the site

function showExitpopup(sPopupType, sQsParameters)

{     

      var sFeatures = 'menubar=no,resizable=yes,width=800,height=500,scrollbars=yes,status=no,toolbar=no';

      var sUrl = "/Pages/CYFRescue.aspx?type=" + sPopupType + "&SearchId=" + sQsParameters

      

      var bShowPopup = false;

      

      if(sPopupType != null && sPopupType != '')

      {

            // only show popup if we are on specific pages

            if(sPopupType == 'compareAirfare' || sPopupType == 'compareFare' || sPopupType == 'specialSavings')

            {

                  bShowPopup = true;

            }

      }

      

      if(leaving && bShowPopup) 

      {           

            window.open(sUrl,"",sFeatures);                 

      }

}

// Traffic rescue popup functionality end

 
  