var Airline = '';
var Class = '3';
var bNS = '0';
var bCP = '0';

var arrAL = new Array('|No Preference','EI |Aer Lingus','AM |Aeromexico','AC |Air Canada','CA |Air China','AF |Air France','JM |Air Jamaica','NZ |Air New Zealand','TP |Air Portugal','TN |Air Tahiti Nui','FL |AirTran Airways','AS |Alaska Airlines','AZ |Alitalia','NH |All Nippon Airways','AQ |Aloha Airlines','HP |America West','AA |American Airlines','OZ |Asiana Airlines','TZ |ATA','OS |Austrian Airlines','BD |bmi','BA |British Airways','CX |Cathay Pacific','CI |China Airlines','MU |China Eastern Airlines','CO |Continental','CM |Copa Airlines','OK |Czech Airlines','DL |Delta','LY |El Al','EK |Emirates','E0 |Eos Airlines','BR |EVA Airways','AY |Finnair','F9 |Frontier Airlines','HA |Hawaiian Airlines','IB |Iberia','FI |Icelandair','JL |Japan Airlines','KL |KLM','KE |Korean Air','LR |LACSA','LA |LAN Airlines','XL |LAN Ecuador','LP |LAN Peru','LO |LOT-Polish Airlines','LH |Lufthansa','LT |LTU','MH |Malaysia Airlines','MY |MAXjet','MX |Mexicana','YX |Midwest Airlines','NW |Northwest','OA |Olympic','QF |Qantas Airways','SK |SAS','SY |Sun Country Airlines','LX |Swiss International Air Lines','TA |TACA','JJ |Tam Brazilian Airlines','TG |Thai Air International','UA |United','US |US Airways','VS |Virgin Atlantic');
var arrAC = new Array('3|Economy / Coach','2|Business','1|First Class');

var minD = 1;
var maxD = 5;
var maxD1 = 3;
var aFCity = GetDefaultF('FCity').split(',');
var aTCity = GetDefaultF('TCity').split(',');
var aFDate = GetDefaultF('FDate').split(',');
var aFTime = GetDefaultF('FTime').split(',');

var pst = '';
var cst = '';
var bB = true;

//window.onload = I;
document.onclick = HCal;

function hw(id)
{
	if(id == ''){if(DE('r1').checked){id='1';}else{id='7';}}
	cst = DE('typ').options[DE('typ').selectedIndex].value;
	if ((id == pwid) && (pst == cst) && (bFlex == DE('flx').checked)){return;}
	cwid = id;
	DE('r' + id).checked = true;
    SO();
	SetSearchType();
	SetTeleMessage();
	if((cwid == '1' && cst == '1') || cwid == '7'){SH(DE('divFlex'),1);}else{SH(DE('divFlex'),0);}
	bFlex = DE('flx').checked;
	if(cwid == '1')
	{
	    SH(DE('divWrs'),1);
	    if(bFlex && cst == '1')
	    {
	        SH(DE('divWOptions'),0);
	    }
	    else
	    {
	        SH(DE('divWOptions'),1);
	    }
	}
	else if(cwid == '7')
	{
	    SH(DE('divWrs'),0);
	    if(bFlex)
	    {
	        SH(DE('divWOptions'),0);
	    }
	    else
	    {
	        SH(DE('divWOptions'),1);
	    }
	}
	else
	{
	    SH(DE('divWrs'),0);
	    SH(DE('divWOptions'),0);
	}
	SetST(cwid);
	SetCities()
	BuildFields();
	pwid = cwid;
	pst = cst;
}

function SetCities()
{
    var el;
    var t = '';
    if(!bFlex)
    {
		el = DE('fci');
		if(el != null)
		{
		    if(el.value != ''){aFCity[0] = el.options[el.selectedIndex].text;DE('aFCity').value = aFCity.join('|');FCity = aFCity[0];DE('FCity').value = FCity;}
		    el = DE('tci');
		    if(el.value != ''){aTCity[0] = el.options[el.selectedIndex].text;DE('aTCity').value = aTCity.join('|');TCity = aTCity[0];DE('TCity').value = TCity;}
		}
    }
}

function BuildFields()
{
	switch (pwid){case '4':case '5':switch (cwid){case '4':case '5':if(!bB){pwid = cwid;return;};break;}break;}
	if (bB){GetSavedValues();}
    SO();
	SetIH('divWFields',GetFieldsHTML());
	BuildRASC();
	SetCC();
	if (bB && DE('ao').value == '1'){SetAO();}
	bB = false;
}
function BuildRASC()
{
	if(pwid == cwid){return;}
	switch (pwid){case '1':case '4':case '5':switch (cwid){case '1':case '4':case '5':if (bB){return ''};break;}break;}
	var n = 0;
	switch (cwid)
	    {
	        case '1':case '7':n = 1;maxC=6;break;
	        case '4':case '5':case '6':n = 2;maxC=5;break;
	        default:n = 0;break;
	    }
	var s = true;
	maxR = 3;
	SetRASC(n,s);
}

function GetFieldsHTML()
{
    var t = ''; 
	switch (cwid)
	{
		case '1':case '7':
            if(cwid == '7'){cst='1';}
	        sb1.Reset();
	        switch (cst)
	        {
		        case '1':
		            if(bFlex)
		            {
			            t = GetFields('FL');break;
		            }
		            else
		            {
			            t = GetFields('F');break;
		            }
			        break;
		        case '2':
                    t = t + GetMDHTML(1);
			        break;
		        case '3':
	                if (DE('md').value == '1'){maxD1 = maxD;}
			        for(var i = minD; i < (maxD1 + 1); i++)
			        {
                        t = t + '<div class="bl2" style="color:#cc6600;font-weight:bold;">Flight ' + i + '</div>';
                        t = t + GetMDHTML(i);
			        }
			        if(maxD1 == 3)
			        {
                        t = t + '<div class="bl1" style="padding-bottom:12px;"><a id="amd" href="javascript:md();">Need more flights and destinations?</a></div>';
			        }
			        break;
	        }
		    break;
        case '4':case '5':t = GetFields('F');break;
		default:t = '';break;
	}
	return t;
}

function SetAO()
{
    DE('ao').value = '1';
	if(DE('divO1')){SetIH('divO1',GetAOHTML());}
}

function md()
{
    DE('md').value = '1';
    maxD1 = maxD;
    pwid = '';
    hw('1');
}

function GetMDHTML(i)
{
	var t = '';
	var n = '';
	var d1 = FCity;
	var d2 = TCity;
	var d3 = FDate;
	var d4 = FTime;
	if(i > 1){n = i;}
	d1 = aFCity[i - 1];
	d2 = aTCity[i - 1];
	d3 = aFDate[i - 1];
	d4 = aFTime[i - 1];
	t = t + '<div class="bl1">';
	t = t + '<div class="flmed">' + GetGL('fcy' + n,'Leaving from:') + '<br/>' + GetGI('fcy' + n,d1,'','',' autocomplete="off" onfocus="HCal();this.select();" onchange="Rem(this);"','100') + '</div>';
	t = t + '<div class="frmed">' + GetGL('tcy' + n,'Going to:') + '<br/>' + GetGI('tcy' + n,d2,'','',' autocomplete="off" onfocus="HCal();this.select();" onchange="Rem(this);"','100') + '</div>';
	t = t + '</div>';
	t = t + '<div class="bl1" style="padding-bottom:14px;">';
	t = t + '<div class="flmed">';
	t = t + '<div class="fl">' + GetGL('fdt' + n,'Departing:') + '<br/>' + GetGI('fdt' + n,d3,'small','',' onblur="Rem(this);" onclick="event.cancelBubble=true;SC(this);" onfocus="SC(this);"','12') + '</div>';
	t = t + '<div class="fr">' + GetGL('ftt' + n,'Time:') + '<br/>' + GetDD('ftt' + n,arrT1,d4,'small','',' onfocus="HCal();" onchange="Rem(this);"') + '</div>';
	t = t + '</div>';
	t = t + '</div>';
    t = t + '<div class="clearer"></div>';
    return t;
}

function GetAOHTML()
{
    tl = 'More info';
    ti = 'No preference search';
    te = 'A no preference search covers those airlines to which Expedia has access, which is most airlines. The flights displayed are those for which Expedia	was able to confirm availability. By searching for flights on just one airline, you may be able to find additional choices and possibly even lower prices.';
	sb.Reset();
    var s = 'margin-left:-1px;';
    if(document.all){s = 'margin-left:-4px;'};
	sb.Concat('<div class="bl2">Low fare note: To increase your chances of finding low fares, leave the following unchecked.</div>');
	sb.Concat('<div class="bl1" style="padding-bottom:0px;">' + GetCB('nos','','Nonstop only',bNS,'',s,' onchange="Rem(this);"') + '</div>');
	sb.Concat('<div class="bl1" style="padding-bottom:12px;">' + GetCB('chp','','Avoid most change penalties',bCP,'',s,' onchange="Rem(this);"') + '</div>');
	sb.Concat('<div class="bl3">');
	sb.Concat('<div class="flmed">' + GetGL('aln','Airline:') + '<br/>' + GetDD('aln',arrAL,Airline,'','',' onchange="Rem(this);"') + '</div>');
	sb.Concat('<div class="frmed">' + GetGL('acl','Class:') + '<br/>' + GetDD('acl',arrAC,Class,'','',' onchange="Rem(this);"') + '</div>');
	sb.Concat('</div>');
    sb.Concat('<div class="bl1">' + GetTT(tl,ti,te,250,'','',true) + '</div>');
    sb.Concat('<div class="clearer"></div>');
	return sb.GetValue();
}

function GetDefaultF(E)
{
	var t = '';
	switch (E)
	{
		case 'FCity':for (var i = minD; i < (maxD + 1); i++){t = t + ',';}break;
		case 'TCity':for (var i = minD; i < (maxD + 1); i++){t = t + ',';}break;
		case 'FDate':for (var i = minD; i < (maxD + 1); i++){t = t + 'mm/dd/yy,';}break;
		case 'FTime':for (var i = minD; i < (maxD + 1); i++){t = t + '362,';}break;
	}
	return t.substr(0,t.length-1);
}

function SetST(id)
{
    switch (id)
    {
        case '1':SText = 'Search for flights';break;
        case '4':SText = 'Search for flights + hotels';break;
        case '5':SText = 'Search for flights + hotels + cars';break;
        case '7':SText = 'Search for flights + cars';break;
    }
    SetSTG();
}

function Rem(el)
{
	RemG(el);
	var n = el.id.substr(3);
	if(n == ''){n = 1;}
	switch (el.id.substr(0,3))
	{
		case 'fcy':aFCity[n - 1] = el.value;DE('aFCity').value = aFCity.join('|');FCity = aFCity[0];DE('FCity').value = FCity;FTLA = FCity.toUpperCase();DE('FTLA').value = FTLA;break;
		case 'tcy':aTCity[n - 1] = el.value;DE('aTCity').value = aTCity.join('|');TCity = aTCity[0];DE('TCity').value = TCity;TTLA = TCity.toUpperCase();DE('TTLA').value = TTLA;break;
		case 'fdt':aFDate[n - 1] = el.value;DE('aFDate').value = aFDate.join('|');FDate = aFDate[0];DE('FDate').value = FDate;break;
		case 'ftt':aFTime[n - 1] = el.value;DE('aFTime').value = aFTime.join('|');FTime = aFTime[0];DE('FTime').value = FTime;break;
		
		case 'fct':aFCity[0] = el.options[el.selectedIndex].text;DE('aFCity').value = aFCity.join('|');FCity = aFCity[0];DE('FCity').value = FCity;break;
		case 'tct':aTCity[0] = el.options[el.selectedIndex].text;DE('aTCity').value = aTCity.join('|');TCity = aTCity[0];DE('TCity').value = TCity;break;
		
		case 'aln':Airline = el.value;DE('Airline').value = Airline;break;
		case 'acl':Class = el.value;DE('Class').value = Class;break;
		case 'nos':if(el.checked){bNS = '1';}else{bNS = '0';}DE('bNS').value = bNS;break;
		case 'chp':if(el.checked){bCP = '1';}else{bCP = '0';}DE('bCP').value = bCP;break;
	}
}

function GetSavedValues()
{
	GetSavedValuesG();
	var v;
	v = DE('aFCity').value;if (v != '') {aFCity = v.split('|');}aFCity[0] = FCity;
	v = DE('aTCity').value;if (v != '') {aTCity = v.split('|');}aTCity[0] = TCity;
	v = DE('aFDate').value;if (v != '') {aFDate = v.split('|');}aFDate[0] = FDate;
	v = DE('aFTime').value;if (v != '') {aFTime = v.split('|');}aFTime[0] = FTime;

	v = DE('Airline').value;if (v != '') {Airline = v;}
	v = DE('Class').value;if (v != '') {Class = v;}
	v = DE('bNS').value;if (v != '') {bNS = v;}
	v = DE('bCP').value;if (v != '') {bCP = v;}
}