function hide(mailbox) {
  document.write('<a href="mailto:' + mailbox + '@' + window.emailDomain +'">');
}

function popup_subscribe() {
  subscribewin = window.open('','_blank','location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,width=350,height=500,top=50,left=100');
  window.focus();
  subscribewin.location = 'http://' + document.location.hostname + '/popup/';
}

function popup_special() {
  specialwin = window.open('http://' + document.location.hostname + '/special_popup.html','','location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no,width=350,height=500,top=50,left=150');
}

function popup_letter() {
  letterwin = window.open('','_blank','location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');
  window.focus();
  letterwin.location = 'http://' + document.location.hostname + '/?alternate_index=1';
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
  endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
  var j = i + alen;
  if (document.cookie.substring(i, j) == arg)
  return getCookieVal (j);
  i = document.cookie.indexOf(" ", i) + 1;
  if (i == 0) break;
  }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : '/';
  var domain = (argc > 4) ? argv[4] : '.simplesavings.com.au';
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  ((path == null) ? "" : ("; path=" + path)) +
  ((domain == null) ? "" : ("; domain=" + domain)) +
  ((secure == true) ? "; secure" : "");
}

function onEntry() {
  var expDays = 1; // number of days the cookie should last
  var exp = new Date();
  exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
  var count = GetCookie('popped');
  if (count == null) {
    count=1;
    SetCookie('popped', count, exp);
    if(! GetCookie('subscriber')) {
      popup_letter();
      popup_subscribe();
      window.focus();
    }
  } else {
    count++;
    SetCookie('popped', count, exp);
  }

//  var scount = GetCookie('spec_popped');
//  if (scount == null) {
//    scount=1;
//    SetCookie('spec_popped', scount, exp);
//    if(! GetCookie('member'))
//      popup_special();
//  } else {
//    scount++;
//    SetCookie('spec_popped', scount, exp);
//  }

}

function isSubscriber() {
  var expDays = 365; // number of days the cookie should last
  var exp = new Date();
  exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
  SetCookie('subscriber', 'yes', exp);
}

votedArray = new Array();
votedNum = 0;


function voteHint(direction, id) {
    dovote = true;
    for (n=0; n<votedArray.length; n++) {
        if (id == votedArray[n]) {
            dovote = false;
        }
    }
    if (dovote) {
        if (direction == "up") {
            otherDirection = "down";
        } else {
            otherDirection = "up";
        }
        document.images[direction + "_" + id].src = "vault/?vote=" + direction + "&hint_id=" + id;
        document.images[otherDirection + "_" + id].src = "images/vote-blank.gif";
        document.getElementById(otherDirection + "_" + id).style.color = '#fff';

        votedArray[votedNum] = id;
        votedNum ++;
    } else {
        alert("You have already voted on this hint.");
    }
}


/**
 * Code to dynamically resize the number of rows in a textarea field.
 *
 * Based on code from the Trac project, which has been changed to
 * +- the rows, rather than a set size.
 */
function resizeTextArea (id, rows)
{
    var textarea = document.getElementById (id);
    if (!textarea || (typeof(textarea.rows) == "undefined")) {
        return;
    }
    if ((textarea.rows + rows) > 0) {
        textarea.rows = parseInt(textarea.rows) + parseInt(rows);
    }
    else {
        textarea.rows = 3;
    }
}


/**
 * Code to populate the state combo box a linked country/state combobox.
 *
 * @param countryCombo combo box object
 * @param stateCombo combo box object
 * @param defaultState code of the default state
 * @param optional is the state field optional?
 *
 * This function is based on work by Brian Reeves (ketapillar'ayat'hotmail.com)
 * found at www.planet-source-code.com.
 *
 * The file location.n.js contains country/state data required by this
 * function.
 */
function populateState (countryCombo, stateCombo, defaultState, mandatory)
{
    var sDefaultState, state, isDefault;

    stateCombo.options.length = 0;
    var j = 0;
    if (typeof mandatory == "boolean" && !mandatory) {
        stateCombo.options[j] = new Option ("-- all --", " ");
        j = 1;
    }

    if (locationStates [countryCombo.selectedIndex].length > 0) {
        states = locationStates[countryCombo.selectedIndex].split ("|")
        for (i = 0; i < states.length; i++){
            state = states [i].split ("#");

            if (defaultState == state [0]) {
                isDefault=true;
                if (navigator.appName=="Microsoft Internet Explorer") {
                    stateCombo.focus ();
                }
            }
            else {
                isDefault=false;
            }

            if (isDefault) {
                stateCombo.options[i + j] = new Option (state [1], state [0],
                                                   isDefault, isDefault);
            }
            else {
                stateCombo.options[i + j] = new Option (state [1], state [0]);
            }
        }
    }
}


function canEatCookies() {
    SetCookie("ssCookieTest", "tseTeikooCss");
    var test = GetCookie('ssCookieTest');
    if (test == 'tseTeikooCss') {
        return true;
    } else {
        return false;
    }
}

function cookieCheckOnLogin() {
    if (!canEatCookies()) {
        alert("To login to Simple Savings, you must have cookies enabled.");
        return false;
    } else {
        return true;
    }
}


var logWin ;
function openLogWindow (name, title)
{
    logWin = window.open('', name, 'toolbar=no,scrollbars,width=600,height=400,screenX=20');
    logWin.autoClear;
    if (logWin.autoClear == true) {
        logWin.document.body.innerHTML = '';
    }
    logWin.document.writeln('<html>');
    logWin.document.writeln('<head>');
    logWin.document.writeln('<title>' + title + '</title>');
    logWin.document.writeln('<style type="text/css">');
    logWin.document.writeln('body { font-family: monospace; font-size: 8pt; }');
    logWin.document.writeln('td,th { font-size: 8pt; }');
    logWin.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
    logWin.document.writeln('td,th { border-right: #999999 solid 1px; }');
    logWin.document.writeln('</style>');
    logWin.document.writeln('</head>');
    logWin.document.writeln('<body>');
    logWin.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
    logWin.document.writeln('<tr><th>Time</th>');
    //$identHeader
    logWin.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
    logWin.document.writeln('<tr><td colspan="3"><button style="font-size:60%" ');
    logWin.document.writeln('onClick="document.body.innerHTML=\'\';">clear</button>');
    logWin.document.writeln(' <label><input id="ckAutoClear" type="checkbox" onClick="autoClear = this.checked;">');
    //if (logWin.autoClear == true) {
        logWin.document.getElementById('ckAutoClear').checked = logWin.autoClear;
    //}
    logWin.document.writeln(' auto-clear</label>');
    logWin.document.writeln('</td></tr>');
}


function writeLogWindow (message)
{
    logWin.document.writeln ('<tr align=\"left\" valign=\"top\"><td style=\"background-color: aqua;\">js</td><td style=\"background-color: aqua;\">Info</td><td style=\"background-color: white;\">');
    logWin.document.writeln (message);
    logWin.document.writeln ('</td></tr>');
}

function formatCurrency(num)
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) {
        num = "0";
    }
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if (cents<10) {
        cents = "0" + cents;
    }
    return ('$' + num + '.' + cents);
}

function replaceTextInNode(nodeId, newText)
{
    node = document.getElementById(nodeId);
    while (child = node.lastChild) {
        node.removeChild(child);
    }
    node.appendChild(document.createTextNode(newText));
}
