// style sheet functions
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 getActiveStyleSheet() {

  var i, a;

  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");

  }

  return null;

}



function getPreferredStyleSheet() {

  var i, a;

  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {

    if(a.getAttribute("rel").indexOf("style") != -1

       && a.getAttribute("rel").indexOf("alt") == -1

       && a.getAttribute("title")

       ) return a.getAttribute("title");

  }

  return null;

}



function createCookie(name,value,days) {

  if (days) {

    var date = new Date();

    date.setTime(date.getTime()+(days*24*60*60*1000));

    var expires = "; expires="+date.toGMTString();

  }

  else expires = "";

  document.cookie = name+"="+value+expires+"; path=/";

}



function readCookie(name) {

  var nameEQ = name + "=";

  var ca = document.cookie.split(';');

  for(var i=0;i < ca.length;i++) {

    var c = ca[i];

    while (c.charAt(0)==' ') c = c.substring(1,c.length);

    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

  }

  return null;

}



window.onload = function(e) {

  var cookie = readCookie("style");

  var title = cookie ? cookie : getPreferredStyleSheet();

  setActiveStyleSheet(title);

}



window.onunload = function(e) {

  var title = getActiveStyleSheet();

  createCookie("style", title, 365);

}



var cookie = readCookie("style");

var title = cookie ? cookie : getPreferredStyleSheet();

setActiveStyleSheet(title);



// end style sheet functions
function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );

}

function saveSelections()
{
	token = new String(Get_Cookie('amount'));
	tokens = token.split('^');
	tokens[0] = document.getElementById('amount').value;
	tokens[1] = document.getElementById('incomeneeded').value;
	var str = ""
	for(i = 0;i<42; i++)
	{
		str = str + tokens[i] + "^";
	}
	str = str + tokens[42] + "^" + tokens[43];
	Set_Cookie( 'amount',  str, 30, '/', '', '' );
}

function saveSelections1()
{
	token = new String(Get_Cookie('amount'));
	tokens = token.split('^');
	tokens[0] = document.getElementById('amount').value;
	tokens[1] = document.getElementById('incomeneeded').value;
	tokens[42] = document.getElementById('age1').value;
	tokens[43] = document.getElementById('age2').value;
	var str = ""
	for(i = 0;i<42; i++)
	{
		str = str + tokens[i] + "^";
	}
	str = str + tokens[42] + "^" + tokens[43];
	Set_Cookie( 'amount',  str, 30, '/', '', '' );
}

function saveSelections2()
{
	token = new String(Get_Cookie('amount'));
	tokens = token.split('^');
	howmuch = dollarFormat(frm(document.savings.expenses.value) - frm(document.savings.other.value));
	howmuch1 = dollarFormat(frm(document.savings.salary.value) - frm(document.savings.save.value) - frm(document.savings.other.value));
	if (0 < frm(howmuch)){
		tokens[1] = '$' + howmuch; }
	else if (0 < frm(howmuch1))
	{
		tokens[1] = '$' + howmuch1;
	}
	var str = ""
	for(i = 0;i<42; i++)
	{
		str = str + tokens[i] + "^";
	}
	str = str + tokens[42] + "^" + tokens[43];
	Set_Cookie( 'amount',  str, 30, '/', '', '' );
}

function saveSelections3()
{
	token = new String(Get_Cookie('amount'));
	tokens = token.split('^');
	tokens[0] = document.getElementById('amount').value;
	tokens[1] = document.getElementById('incomeneeded').value;
	tokens[42] = document.getElementById('age').value;
	var str = ""
	for(i = 0;i<42; i++)
	{
		str = str + tokens[i] + "^";
	}
	str = str + tokens[42] + "^" + tokens[43];
	Set_Cookie( 'amount',  str, 30, '/', '', '' );
}

function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}
	
function GetCookie()
{
  // cookies are separated by semicolons
  amount  = document.getElementById('amount');
  incomeneeded = document.getElementById('incomeneeded')
  if (Get_Cookie("amount")) {
  token = Get_Cookie("amount");
  tokens = token.split("^");
  amount.value = tokens[0];
  incomeneeded.value = tokens[1];
  }
}


function GetCookie1()
{
  // cookies are separated by semicolons
  amount  = document.getElementById('amount');
  incomeneeded = document.getElementById('incomeneeded');
  age1 = document.getElementById('age1');
  age2 = document.getElementById('age2');
  token = Get_Cookie("amount");
  tokens = token.split("^");
  amount.value = tokens[0];
  incomeneeded.value = tokens[1];
  if(tokens[42] != 'undefined')
  {
  	age1.value = tokens[42];
  }
  if(tokens[43] != 'undefined')
  {
  	age2.value = tokens[43];
  }
}

function GetCookie3()
{
  // cookies are separated by semicolons
  amount  = document.getElementById('amount');
  incomeneeded = document.getElementById('incomeneeded');
  age = document.getElementById('age');
  token = Get_Cookie("amount");
  tokens = token.split("^");
  amount.value = tokens[0];
  incomeneeded.value = tokens[1];
  if(tokens[42] != 'undefined')
  {
  	age.value = tokens[42];
  }
}