// VisualCalc, Inc. copyright 2009
// www.visualcalc.com

// variables
var helpwin = null;
var detwin = null;
var szHelpDir = "help/";


// show the specified page
function showPage (name) {
    var helpURL = szHelpDir + name + ".html";
    
    if ( helpwin != null ) {
		helpwin.close ();
	}

	try {
		var sz = "width=450,height=200,resizable=yes,scrollbars=yes,screenX=100,screenY=100,top=100,left=100,menubar=no,status=no,location=no";
		helpwin = window.open (helpURL, "helpwin", sz);
		if ( !helpwin.opener) {
			helpwin.opener = self;
		}
	} catch (e) {
	}
}


// the script is used to workaround the IE "click to activate and use this control" KB912945 patch		
function write_control (text) {
	document.write (text);
}


// launch printable version of the savings solutions calculator (2nd pass)
function showDetails () {
	getInitData ();
    if ( (detwin != null) && (!detwin.closed) ) {
	    detwin.focus ();
    } else {
	    detwin = window.open ("accdetailspage.html", "detailswindow", "width=650,height=515,toolbar=no,status=no,menubar=no,resizable=yes,scrollbars=yes");
	    detwin.opener = this;
    }
}


// get the initialization data 
function getInitData () {
    var idx;
	
    window.loantot = document.vcapp.getData (1, 20, 0, 0);
    window.useIBR = document.vcapp.getData (1, 3, 0, 0);
    window.agi = document.vcapp.getData (1, 6, 0, 0);
    window.hhs = document.vcapp.getData (1, 7, 0, 0);
    window.state = document.vcapp.getData (1, 8, 0, 0);

    window.stdrow = getRowDataEx (12, 0);
    window.gradrow1 = getRowDataEx (12, 1);
    window.gradrow2 = getRowDataEx (12, 2);
    window.extrow = getRowDataEx (12, 3);
    window.ibrrow1 = getRowDataEx (12, 4);
    window.ibrrow2 = getRowDataEx (12, 5);
    window.ibrrow3 = getRowDataEx (12, 6);
}


// get table row data formatted for initialization
// Note: the returned string DOES end with ";"
function getRowData (dataid) {
    var retstr = "";
    var label = document.vcapp.getData (1, dataid, 7, 0) + ";";
    retstr = label + document.vcapp.getData (1, dataid, 3, 0) + ";";
    return retstr;
}


// get table row data formatted for initialization
// Note: the returned string DOES end with ";"
function getRowDataEx (dataid, row) {
    var retstr = "";
	var idx = row * 4;
	var label = document.vcapp.getData (1, dataid, 7, row) + ";";
    var eligible = document.vcapp.getData (1, dataid, 2, idx+0) + ";";
    var mopmt = document.vcapp.getData (1, dataid, 2, idx+1) + ";";
    var totpmt = document.vcapp.getData (1, dataid, 2, idx+2) + ";";
    var years = document.vcapp.getData (1, dataid, 2, idx+3) + ";";
    retstr = label + eligible + mopmt + totpmt + years;

    return retstr;
}


// write the HTML content for the main calculator applet
function writeCalcValue (index) {
	var calcval = "0.0";
	var validate = false;
	switch ( index ) {
		case 1:
			calcval = window.opener.loantot;
			break;
		case 2:
			calcval = window.opener.useIBR;
			break;
		case 3:
			calcval = window.opener.agi;
			validate = true;
			break;
		case 4:
			calcval = window.opener.hhs;
			validate = true;
			break;
		case 5:
			calcval = window.opener.state;
			validate = true;
			break;
	}
	if ( validate ) {
		if ( window.opener.useIBR == "No" ) {
			calcval = "N/A";
		}
	}
	document.open();
    document.write ( ' ' + calcval + ' ' );
	document.close();
}


function resetTable (row) {
	if ( row != tableidx ) {
		var tablestr = getTableText (row);
		getTableVals (tablestr);
		tableidx = row;
	}
}


function writeImageText () {
	var name = ( tablevals[1] == 0 ) ? "unchk.gif" : "chk.gif";
	document.open();
    document.write ( '<img src="images/' + name + '" width="12" height="12" border="0">' );
	document.close();
}


var tableidx = -1;
var tablevals = [" ", " ", " ", " ", " "];

function writeTableValue (row, col) {
	if ( row != tableidx ) {
		var tablestr = getTableText (row);
		getTableVals (tablestr);
		tableidx = row;
	}
	if ( col <= 5 ) {
		var tableval = tablevals[col - 1];
		document.open();
	    document.write ( tableval );
		document.close();
	}
}


function getTableText (table) {
	var text = "";
	switch ( table ) {
		case 1:
			text = window.opener.stdrow;
			break;
		case 2:
			text = window.opener.gradrow1;
			break;
		case 3:
			text = window.opener.gradrow2;
			break;
		case 4:
			text = window.opener.extrow;
			break;
		case 5:
			text = window.opener.ibrrow1;
			break;
		case 6:
			text = window.opener.ibrrow2;
			break;
		case 7:
			text = window.opener.ibrrow3;
			break;
	}
	return text;
}


function getTableVals (text) {
	var str = new String (text);
	var pos1 = 0;
	var pos2 = 0;
	var tidx = 0;

	while ( pos1 < str.length ) {
		pos2 = str.indexOf (";", pos1);
		var valstr = str.substring (pos1, pos2);
		tablevals[tidx] = valstr;

		if ( pos2 != -1 ) {
			pos1 = pos2 + 1;
		} else {
			pos1 = str.length;
		}
		tidx++;
	}
}





