var objVal;
var splitIndex = 0, splitArray = new Object();
var passed = replace(replace(location.search.substring(1),"+"," "),"=","&");

function getReport() {

	document.retire.type='get';
	document.retire.action='http://www.fiscalagents.com/toolbox/cal/rrsp/rep_retplanner.shtml';
	document.retire.submit();

}

function roundDecimal(obj, places)
{
  tmpString = new String(obj);
  decimal = tmpString.indexOf(".");
  return (tmpString.slice(0, ( places + decimal ) ) );
};

function replace(string,text,by) {
    // Replaces text with by in string
    var i = string.indexOf(text), newstr = '';
    if ((!i) || (i == -1))
        return string;
    newstr += string.substring(0,i) + by;
    if (i+text.length < string.length)
        newstr += replace(string.substring(i+text.length,string.length),text,by);
    return newstr;
}

function split(string,text) {
    splitArray = string.split(text);
    splitIndex = splitArray.length;
}

function writeIn(objName) {
	split(passed,'&');
	for (var i=0; i < splitIndex; i=i+2) {
    if (splitArray[i] == objName)
        document.write('<font face="Courier New" size="1">' + unescape(splitArray[i+1]) + '</font>');
		objVal = unescape(splitArray[i+1]);
	}
}

function getVal(objName) {
	objVal=" ";
	split(passed,'&');
	for (var i=0; i < splitIndex; i=i+2) {
    if (splitArray[i] == objName) {
	objVal = unescape(splitArray[i+1]); }
	}
	return objVal;
}

function checkFor(objName, objResult) {
	split(passed,'&');
	for (var i=0; i < splitIndex; i=i+2) {
	    if (splitArray[i] == objName) {
			if (splitArray[i+1] == objResult) return true;
		}
	}
}

function countS1()
{
  if (document.retire["sec1row1"].value.length != 0 && document.retire["sec1row2"].value.length != 0)
  {
    document.retire["totsec1"].value = parseFloat(document.retire["sec1row1"].value) * parseFloat(document.retire["sec1row2"].value);
  }

  return false;
};

function sec1(){
	document.retire.sec1row1entry.value = document.retire.sec1row1.value;
  document.retire["sec1row1"].value = .75 * parseFloat(document.retire["sec1row1"].value);
  if (isNaN(document.retire["sec1row1"].value)) { document.retire["sec1row1"].value = "0" ; }
}

function countS2(name) {
	document.retire.sec2row1entry.value = document.retire.sec2row1.value;
  if (isNaN(document.retire["sec2row1"].value) || document.retire["sec2row1"].value > 100)
  {
      alert('Invalid Entry');
      document.retire["sec2row1"].focus();
      return false;
  }
  if (document.retire["totsec1"].value.length != 0 && document.retire["sec2row1"].value.length != 0)
  {
    document.retire["sec2row1"].value = (Math.round(100 * (document.retire["sec2row1"].value/100) * document.retire["totsec1"].value)) / 100;
    document.retire["totsec2"].value = parseFloat(document.retire["totsec1"].value) - parseFloat(document.retire["sec2row1"].value);
  }

  return false;
};

function countS3()
{
  if (document.retire["totsec2"].value.length != 0 && document.retire["sec3row1"].value.length != 0)
  {
    document.retire["sec3row2"].value = Math.round(100 * (parseFloat(document.retire["sec3row1"].value) * parseFloat(document.retire["totsec2"].value))) / 100;
    if (document.retire["sec3row3"].value.length != 0 && document.retire["sec3row4"].value.length != 0 )
    {
      document.retire["sec3row5"].value = parseFloat(document.retire["sec3row3"].value) * parseFloat(document.retire["sec3row4"].value);
      document.retire["totsec3"].value = Math.round(100 *( parseFloat(document.retire["sec3row2"].value) - parseFloat(document.retire["sec3row5"].value))) / 100;
    }
  }

  return false;
};

function countS4()
{
  if (document.retire["totsec3"].value.length != 0)
  {
    document.retire["totsec4"].value = Math.round(100*(parseFloat(document.retire["totsec3"].value/100) / parseFloat(document.retire["sec4row1"].value) * 100) / 100);
  }

  return false;
};

function TableValue(tablenum, val, refObj) {
	var cmd = 'document.retire.';
	if (tablenum == 1) { cmd += 'sec1row2.value="' + val + '"; countS1();'; }
	else if (tablenum == 2) { cmd += 'sec3row1.value="' + val + '"; countS3();'; }
	else if (tablenum == 3) { cmd += 'sec3row4.value="' + val + '"; countS3();'; }
	else if (tablenum == 4) { cmd += 'sec4row1.value="' + val + '"; countS4();'; }
	eval(cmd);
	refObj.focus();
}

function validNums(refVal) {
	var bValid=true;

	for (var i=0; i < refVal.value.length; i++) {
		var c = refVal.value.charCodeAt(i);
        if ((c > 57 || c < 48)) {
			bValid=false;
			}
	}
	if (bValid==false) {
		alert('You can only enter numbers into this field.');
//		refVal.value="";
		refVal.focus();
		refVal.select();
		return false;
		}
	else {
		return true; }
}

function validNumsDec(refVal) {
	var decm = false;
	var bDec = true;
	var bValid = true;
	for (var i=0; i < refVal.value.length; i++) {
		var c = refVal.value.charCodeAt(i);
        if ((c > 57 || c < 48)) {
			if (c==46) {
				if (decm == true) {
					var bDec = false; }
				else {
					decm = true; } }
			else if (c==44) { }
			else {
				bValid = false; }
			}
	}
	if (bValid==false || bDec==false) {
		alert('You can only enter numbers, commas and one decimal into this field.');
		refVal.focus();
		refVal.select();
		return false;
		}
	else return true;
}