//global vars
var topNavArray = new Array ("about_dia", "managing", "community", "dia_news", "resources", "about_dlife");
var theImages   = new Array();

var currentSection = "";
var defFontSize    = 12;

var expDate = new Date (2050, 11, 11);

var curMenu = "";

function GA(){
    this.q = new Array ();
    this.isComplete = false;
    this.code = null;
}

GA.prototype.addPV = function (page) {
    try {
        if (this.isComplete) {
            pageTracker._trackPageview (page);
        } else {
            this.q[this.q.length] = function () {
                pageTracker._trackPageview (page);
            };
        }
    } catch (e) {}
};

GA.prototype.processq = function () {
    try {
        if (this.q.length > 0)
            for (i = 0; i < this.q.length; i ++)
                this.q [i] ();
    } catch (e) {}

    this.q = new Array();
    this.isComplete = true;
};

var dLifeGA = new GA();

/* variables for top nav menu */
// convert all characters to lowercase to simplify testing

var agt = navigator.userAgent.toLowerCase ();
var appVer = navigator.appVersion.toLowerCase ();

// *** BROWSER VERSION ***

var is_minor = parseFloat (appVer);
var is_major = parseInt (is_minor);

// Note: On IE, start of appVersion return 3 or 4
// which supposedly is the version of Netscape it is compatible with.
// So we look for the real version further on in the string

var iePos = appVer.indexOf ('msie');

if (iePos != -1) {
   is_minor = parseFloat (appVer.substring (iePos + 5, appVer.indexOf (';', iePos)));  //added missing ; - 030617 - bdn
   is_major = parseInt (is_minor);
}

var is_opera = (agt.indexOf ("opera") != -1);
var is_konq = false;

var kqPos   = agt.indexOf ('konqueror');
if (kqPos !=-1) {                 
   is_konq  = true;
   is_minor = parseFloat (agt.substring (kqPos+10, agt.indexOf (';', kqPos)));
   is_major = parseInt (is_minor);
}           

var is_safari = ((agt.indexOf ('safari') != -1) && (agt.indexOf ('mac') != -1)) ? true : false;
var is_khtml  = (is_safari || is_konq);
    
var is_ie    = ((iePos!=-1) && (!is_opera) && (!is_khtml));
var is_ie5up = (is_ie && is_minor >= 5);
var is_mac   = (agt.indexOf ("mac") != -1);

/* variables for message board */
var isMac   = navigator.userAgent.toLowerCase ().indexOf ("mac") != -1;
var isIE    = navigator.appName.indexOf ("Microsoft") != -1;
var isOpera = navigator.userAgent.toLowerCase().indexOf("opera")!=-1;

var is_addEventListener = window.addEventListener ? true : false;
var is_attachEvent      = window.attachEvent ? true : false;

function setCookie (name, value, expires, path, domain, secure)
{
    var cookie = name + "=" + escape(value);
    cookie += ((expires) ? "; expires=" + expires.toGMTString() : "");
    cookie += ((path) ? "; path=" + path : "");
    cookie += ((domain) ? "; domain=" + domain : "");
    cookie += ((secure) ? "; secure" : "");
    document.cookie = cookie;
}

function setActiveStyleSheet (title)
{
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName ("link") [i]); i++) {
        if (a.getAttribute ("rel").indexOf ("style") != -1 && a.getAttribute ("title")) {
            a.disabled = true;
            if (a.getAttribute ("title") == title) {
                a.disabled = false;
            }
        }
    }
}
    
function setStyles (size)
{
    if (document.getElementById ('mainContentSource')) {
        document.getElementById ('mainContentSource').style.fontSize = size + 'px';
    }
        
    if (document.getElementById ('footer')) {
        document.getElementById('footer').style.fontSize = size + 'px';
    }
        
    if (document.getElementById ('Left_Bucket_Containter')) {
        document.getElementById ('Left_Bucket_Containter').style.fontSize = size + 'px';
    }

    deleteCookie ("dLife_fontsize");
    setCookie ("dLife_fontsize", size, expDate, "/");
}
        
function getCookie (name)
{
    var dc     = document.cookie;
    var prefix = name + "=";
    var begin  = dc.indexOf("; " + prefix);
    
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) {
            return null;
        }
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf (";", begin);
    
    if (end == -1) {
        end = dc.length;
    }
    
    return unescape (dc.substring (begin + prefix.length, end));
}

function initStyles()
{   
    var style="";
    var fontSize = getCookie ("dLife_fontsize");
    
    style += '<style type="text/css">';
    
    if (fontSize == null) {
        fontSize = defFontSize;
    }
    
    style += '#navigation {font-size:' + fontSize + 'px}';
    style += '#mainContentSource {font-size:' + fontSize + 'px}';
    style += '#Left_Bucket_Containter {font-size:' + fontSize + 'px}';
    style += '#footer {font-size:' + fontSize + 'px}';
    style += '</style>';    

    return style;
}   
    
function deleteCookie (name, path, domain)
{
    if (getCookie (name)) {
        document.cookie = name + "=" +  ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function subForm (state, id1, id2)
{
    if (state) {
        document.getElementById ("newsForm").style.display = "inline";
    } else {
        if (document.getElementById (id1) != null) {
            if(!document.getElementById(id1).checked) {
                if(document.getElementById(id2) != null) {
                    if(!document.getElementById(id2).checked) {
                        document.getElementById("newsForm").style.display="none";
                     }
                }
            }
        }
    }
}

function showHideAddressForm (state)
{
    if (state) {
        document.getElementById ("billingAddressDiv").style.display = "inline";
    } else {
        document.getElementById ("billingAddressDiv").style.display = "none";
    }
}
    
function checkDisplay ()
{
    if (document.myProfileForm.newsletterSignup.checked) {
        document.getElementById ("newsForm").style.display = "inline";
    } else {
        document.getElementById ("newsForm").style.display = "none";
    }       
}

function goTo (loc)
{
    document.location.href = loc;
}
    
function changeFontPicker (newSize)
{
    
    var curSize = getCookie ("dLife_fontsize");
	
    if(curSize == null) {
        curSize = defFontSize;
    }
    
    var offExt = ".gif";
    var onExt  = "_on" + offExt;
    
    var onId = "fPick" + newSize;
    
    var offId =  "fPick" + curSize;
    
    var src = document.getElementById (offId).src; // fails

    src = src.replace(curSize+onExt, curSize+offExt);
    
    document.getElementById (offId).src = src;

    src = document.getElementById (onId).src;
    src = src.replace (newSize + offExt, newSize + onExt);
    
    document.getElementById (onId).src = src;    
}

function initFontPicker()
{   
    var offExt = ".gif";
    var onExt  = "_on" + offExt ;
    
    var curSize = getCookie ("dLife_fontsize");

    if (curSize == null) {
        curSize = defFontSize;
    }

    //Item to trun on
    var onId =  "fPick" + curSize;

    //turn item on 
    var src = document.getElementById (onId).src;
    src = src.replace (curSize + offExt, curSize + onExt);

    document.getElementById (onId).src = src;
}

var myHeight = 300;
var isResizable = false;

function createTarget (form) {
    var target = form.target;
    var colon  = target.indexOf (":");
    
    if (colon != -1) {
        form.target = target.substring (0, colon);
        form.args   = target.substring (colon + 1);
    } else if (typeof (form.args) == "undefined") {
        form.args = "";
    }
    
    if (form.args.indexOf ("{") != -1) {
        var args = form.args.split ("{");
        
        form.args = args [0];
        
        for (var i = 1; i < args.length; i ++) {
            args [i] = args [i].split ("}");
            form.args += eval (args [i] [0]) + args [i] [1];
        }
    }
    
    form.args = form.args.replace (/ /g, "");
    
    var win = window.open ('', form.target, form.args);
    
    if (typeof (focus) == "function") {
        _win.focus ();
    }
    
    return true;
}

function printThisPage ()
{
    var curUrl = (location.href);
    var printaGaga= window.open (thePrintVar, "newWin", "width=780,height=600,resizable=1,scrollbars=1");
}

function openWindow (location, height, width)
{
    var win = window.open (location, "newWin", "width=" + width + ",height=" + height + ",resizable=1,scrollbars=1");
}

/* limit number of char input for textarea */
function textCounter (field, cntfield, maxlimit)
{
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
        alert("Sorry! Your message has exceeded the character limit. The maximum number of characters\nthat can be entered is " + maxlimit + ".\n\n ");
    } else {
    	cntfield.value = maxlimit - field.value.length;
    }
}

var OAS_sitepage = 'www.dlife.com';

function getAdSitePage()
{
    var find = "/.*\/do\/ShowContent/g";
    var page = OAS_sitepage;
    var path = window.location.pathname;
    
    if (path.toLowerCase () == "/dlife/") {
    	return page + "/homepage";
    }
    
    if (path != "")  {
        page = page + path.replace (/.*\/do\/ShowContent/g, "");
                  
        if (page.indexOf (".html") == -1 && page.charAt (page.length - 1) != "/") {
            page = page + "/"; 
        }
        
        page = page.substring(0, page.lastIndexOf("/")); 
                 
        if (page == "www.dlife.com" || page == "www.dlife.com/") {
        	 page = "www.dlife.com/homepage";
        }
        
        if (page.indexOf ("www.dlife.com/dLife/do/food") > -1) {
        	page = "www.dlife.com/dlife/do/food"; 
        }
        
        if (page.indexOf ("www.dlife.com/dLife/do/recipe") > -1) {
        	page = "www.dlife.com/dlife/do/recipe";
        }
        
        if (page.indexOf ("www.dlife.com/dLife/do/expert") > -1) {
        	page = "www.dlife.com/dlife/do/expert"; 
        }
    }
    
    return page.toLowerCase ();
}

function showIFrameVideo (url)
{
    document.getElementById ('videoframe').src = url;
}

function popUp (URL)
{
    var day = new Date ();
    var id = day.getTime ();
    eval ("page" + id + " = window.open (URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=360,height=415');");
}

function popUpSeenOnTV (URL)
{
    var day = new Date ();
    var id = day.getTime ();

    eval ("page" + id + " = window.open (URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=360,height=415');");
}

function customPopUp (URL, name, width, height)
{
  var options = "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height;
  
  var popup = window.open (URL, name, options);
}

function get_query_variable (variable)
{
    var query = window.location.search.substring (1);
    var vars = query.split("&");
    
    for (var i=0; i < vars.length; i ++) {
        var pair = vars [i].split ("=");
        
        if (pair [0] == variable) {
            return pair [1];
        }
    } 
    
    return "";
}

function get_omni_events ()
{
    try {
        return omni_events.join (";");
    } catch (e) {
        return "";
    }
}

function add_omni_event (event_name)
{
    try {
        omni_events.push (event_name);
    } catch (e) {
        omni_events = new Array (event_name);    
    }
}

function newPop (URL)
{
    var day = new Date ();
    var id = day.getTime ();
    eval ("page" + id + " = window.open (URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=950,height=700');");
}