/* $Id: enf.js,v 1.9 2005/06/16 00:00:08 geoff Exp $ */
/* Copyright (c) 2000-2004 Peck Labs, Inc. All rights reserved. */

function PopFAA(url) {
    FAApopup = window.open('', 'FAApopup', 'scrollbars=1');
    FAApopup.location.href = url;
    if (FAApopup.opener === null) FAApopup.opener = window;
    FAApopup.focus();
}

function Pop(url) {
    popup = window.open('', 'popup', 'height=320,width=450,scrollbars=1');
    popup.location.href = url;
    if (popup.opener === null) popup.opener = window;
    popup.focus();
}

function PopTR() {
    popup = window.open('', 'TravelPopup', 'scrollbars=1');
    popup.location.href = 'http://www.travelstoremaker.com/cgit/porch?agentid=enflight';
    if (popup.opener === null) popup.opener = window;
    popup.focus();
}

function PopP(url) {
    popup = window.open('', 'Procedure', 'width=620,scrollbars=1');
    popup.location.href = url;
    if (popup.opener === null) popup.opener = window;
    popup.focus();
}

function PopH(url) {
    popup = window.open('', 'Help', 'width=500,height=500,scrollbars=1');
    popup.location.href = url;
    if (popup.opener === null) popup.opener = window;
    popup.focus();
}

function PopAPD(url) {
    popup = window.open('', 'AirportDiagram', 'width=620,scrollbars=1');
    popup.location.href = url;
    if (popup.opener === null) popup.opener = window;
    popup.focus();
}

function PopBig(url) {
    popup = window.open('', 'pop', 'height=520,width=720,scrollbars=1');
    popup.location.href = url;
    if (popup.opener === null) popup.opener = window;
    popup.focus();
}

function aopaDir (a) {
    var f;
    f = a.charAt(0).toUpperCase();
    if ( a.length == 4 && isalpha(a) && (f == 'K' || f == 'P' || f == 'C' || f == 'M')) {
	a = a.substring(1,4);
    }
    aopadir = window.open('', 'aopadir', 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=640,height=480');
    aopadir.location.href = 'http://www.aopa.org/members/airports/detail.cfm?identifier=' + a;
    if (aopadir.opener === null) aopadir.opener = window;
    aopadir.focus();
}

function aptDir (a) {
    var f;
    f = a.charAt(0).toUpperCase();
//    if ( a.length == 4 && isalpha(a) && (f == 'K' || f == 'P' || f == 'C' || f == 'M')) {
//	a = a.substring(1,4);
//    }
    dw = window.open('', 'aopadir', 'toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=660,height=480');
    dw.location.href = 'apt.php?p=1&id=' + a;
    if (dw.opener === null) dw.opener = window;
    dw.focus();
}

function isalpha(s) {
    var i;
    for ( i = 0 ; i < s.length ; i++ )
	if ( !((s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') || (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')) )
	    return false;
    return true;
}

function calcTimeOffset()
{
    var current = new Date();

    var offset = current.getTime() - " . time() * 1000 . "; 
    return offset;
}
var offset = calcTimeOffset();

function currentTime()
{
    var current = new Date();
    current = new Date(current.getTime() - offset);
    var utctime = current.toGMTString();
    utctime = '&nbsp;&nbsp;' + utctime.substring(0, utctime.length - 7) + ' UTC&nbsp;&nbsp;';
    if (document.all) {
	document.all.clockDiv.innerHTML = utctime;
	setTimeout('currentTime()', 1000);
    // } else if (document.layers) { 
	// This doesn't work in Netscape 4.x unless clockDiv is
	// positioned absolutely.
	//document.clockDiv.document.open()
	//document.clockDiv.document.write(utctime);
	//document.clockDiv.document.close()
	//setTimeout('currentTime()', 1000);
    // } else if (document.getElementById) {
	// Netscape 6
	// document.getElementById('clockDiv').innerHTML = utctime;
	// setTimeout('currentTime()', 1000);
    }
}

//#################################################

// wt moved this from support/utilities.js to here so it would
// be available w/o needing OpenLayers

// Get and set cookies

// The browser thinks cookies are strings.  So we've wrapped them in JSON
// serialize and de-serialize so we can pretend they are *actually* JS
// objects.

// Cookie management code via blazonry.com/javascript/cookies.php with flavor
// Expires behaves badly if your browser is set to flush all cookies at the
// end of the session It never records the expire date at all, just sets
// everything to "end of session" (AFAICT) Must wonder what this does to
// cookies that are set for, say, 3 seconds in the future?

// All date code in here now.  I don't want to think about the details.
// Everytime we write a cookie it will get a new expiredate

// http://stackoverflow.com/questions/1062963/how-do-browser-cookie-domains-work
// http://www.webreference.com/js/column8/functions.html


function setCookie(name, value) {
    // Setup a cookie expire date
    var exp = new Date();
    exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 7));     // 7 days ahead 
    var expc = (exp == null) ? "" : "; exp=" + exp.toGMTString();

    //  set cookie domain to either .enflight.com or .rho.net
    var host = window.location.host;
    var p = host.indexOf('.');
    var domain = host.substring( p, host.length );
    var domainc = "; domain=" + domain;

    //document.cookie = name + "=" + json.write(value) + 
    document.cookie = name + "=" + JSON.stringify(value) + 
        "; path=/" + expc + domainc;
}


// You must search for cookies by name, as someone was too lazy to provide
// this function pre-defined
// We pretend what is stored in cookies is Javascript Objects

function getCookie (name) {
    var dc = document.cookie;
    var cname = name + "=";

    if (dc.length > 0) {
        begin = dc.indexOf(cname);
        if (begin != -1) {
            begin += cname.length;
            end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            //return json.read(dc.substring(begin, end));
            return JSON.parse(dc.substring(begin, end));
        }
    }
    return null;
}

