//for pop-up windows

function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
//if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function openFrameless(urlPop) {

var windowW=214 // width
var windowH=398 // high
var windowX = 260 // from left
var windowY = 100 // from top
//var urlPop = 
var title =  "Program Description"
// set this to true if the popup should close
// upon leaving the launching page; else, false
var autoclose = true

s = "width="+windowW+",height="+windowH;
var beIE = document.all?true:false

if (beIE) {
NFW = window.open("","popFrameless","fullscreen,"+s);
NFW.blur();


window.focus();
NFW.resizeTo(windowW,windowH);
NFW.moveTo(windowX,windowY);
var frameString=""+
"<html>"+
"<head>"+
"<title>"+title+"</title>"+
"</head>"+
"<frameset rows='*,0' framespacing=0 border=1 frameborder=1>"+
"<frame name='top' src='"+urlPop+"' scrolling=auto>"+
"<frame name='bottom' src='about:blank' scrolling='no'>"+
"</frameset>";//+
//"</html>";
NFW.document.open();
NFW.document.write(frameString);
NFW.document.close();
}
else {
NFW = window.open(urlPop,"popFrameless","scrollbars,"+s);
NFW.blur();
window.focus();
NFW.resizeTo(windowW,windowH);
NFW.moveTo(windowX,windowY);
}
NFW.focus();
if (autoclose) {
window.onunload = function(){NFW.close();}
   }
}
/*
The following functions are called in process_address.asp
The first function validateSelect is used to ensure that a user selects an address from the pre and post address dropdowns
The second function getAddress pulls the selected address text from the select box, which is displayed (eventually) on savings.asp.
*/

function validateSelect()
{
	var bSelected = true;
	if (document.address_criteria.new_pre_building_id)
	{
		bSelected = false;
		for (i=0; i < document.address_criteria.new_pre_building_id.length; i++)
		{
			if (document.address_criteria.new_pre_building_id.options[i].selected)
			{
				bSelected = true;
				getAddress(document.address_criteria.new_pre_building_id.options[i].text, 'pre')
			}
		}
	}
	if (document.address_criteria.new_post_building_id)
	{
		bSelected = false;
	 
		for (i=0; i < document.address_criteria.new_post_building_id.length; i++)
		{
			if (document.address_criteria.new_post_building_id.options[i].selected)
			{
	 
				bSelected = true;
				getAddress(document.address_criteria.new_post_building_id.options[i].text, 'post')
			}
		}
	}
	if (bSelected == false)
	{	alert('You must select a valid address in order to continue.') }
	return bSelected;
}

function getAddress(address,select)
{
	if (select == 'pre'){
		document.address_criteria.pre_address_detail.value = address
	}
	if (select == 'post'){
		document.address_criteria.post_address_detail.value = address
	}
}

function resetForm()
{
//There are 45 elements on the page, but we don't want to reset hidden form element values (address Info)f
	for (i=0; i<33; i++)
		{
			document.criteria.elements[i].value = ''
		}
}

function CalculateNPV(){
var vNPV
vNPV = 0
	with( document.npv ){
		vNPV =  year1.value / Math.pow( ( 1 + ( rate.value / 100 ) ), 1 )
		vNPV += year2.value / Math.pow( ( 1 + ( rate.value / 100 ) ), 2 )
		vNPV += year3.value / Math.pow( ( 1 + ( rate.value / 100 ) ), 3 )
		vNPV += year4.value / Math.pow( ( 1 + ( rate.value / 100 ) ), 4 )
		vNPV += year5.value / Math.pow( ( 1 + ( rate.value / 100 ) ), 5 )
		 
		tnpv.value = FormatDecimal(vNPV, 2, 1)
	}
}

function FormatDecimal( Amount, decPlaces, bComma ){
	var isNegative = false;	
	var sAmount = parseFloat( Amount );
	
	if( isNaN( sAmount )){
		return "0";
	}
	
	if( sAmount < 0 ){
		isNegative = true;
	}
	
	sAmount = "" + ( Math.round( Math.abs( sAmount ) * Math.pow( 10, decPlaces ) ) / Math.pow( 10, decPlaces ) );
	
	if( bComma ){
		var decLocation = sAmount.indexOf( "." );
		var comNum = 0;
		
		if( decLocation == -1 ){
			decLocation = sAmount.length;
		}
		
		for( var i = decLocation; i > 1; i-- ){
			comNum++;
			if( comNum == 3 ){
				sAmount = sAmount.substring( 0, i - 1 ) + "," + sAmount.substring( i - 1 );
				comNum = 0;
			}
		}	
	}
	
	if( isNegative ){
		sAmount = "(" + sAmount + ")";
	}
	
	return sAmount;
}

