// JavaScript Document
/* <![CDATA[ */
function changeInputType(oldElm,iType,iValue,noFocus) {
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) newElm.name = oldElm.name;
  if(oldElm.id) newElm.id = oldElm.id;
  newElm.onfocus = function() {
    if(this.hasFocus) return;
    var newElm = changeInputType(this,'password',
      (this.value.toLowerCase()=='password')?'':this.value);
    if(newElm) newElm.hasFocus=true;
  }
  newElm.onblur = function() {
    if(this.hasFocus)
    if(this.value=='' || this.value.toLowerCase()=='password') {
      changeInputType(this,'text','password',true);
    }
  }
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(iValue) newElm.value = iValue;
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}

window.onload = function() {
  // Example 1
  var allowInputTypeChange = true;

  var ua = navigator.userAgent.toLowerCase();
  if(!((ua.indexOf('konqueror')!=-1) && (document.all || 
    (ua.indexOf('khtml/3.4')!=-1))) && !(((ua.indexOf('safari')!=-1) && 
    !window.print) || (document.defaultCharset && !window.print)))
      changeInputType(document.login.password,'text','password',true);
}

function savingsCalc(){
	var rate = document.calc.increase_cost.value;
	var interval = 12;
	var payments_five = 60;
	var payments_ten = 120;
	var initial = parseFloat(convertIt(document.calc.avg_bill.value));
	var sqFt = parseInt(convertIt(document.calc.sq_ft.value));
	var floors = parseInt(convertIt(document.calc.floors.value));
	var wallFeet = ( Math.sqrt(sqFt / floors) * 32 * floors );
	var retroCost = wallFeet * 2;
	for (var i=0; i < document.calc.savings.length; i++){
		if (document.calc.savings[i].checked){
	  		var savings = document.calc.savings[i].value;
		}
	}
	
	var total_five = futureValue(rate, interval, payments_five, initial);
	var total_ten = futureValue(rate, interval, payments_ten, initial);
	var save_five = futureValue(rate, interval, payments_five, (initial * (1 - savings)));
	var save_ten = futureValue(rate, interval, payments_ten, (initial * (1 - savings)));
	document.getElementById('five_years').firstChild.nodeValue=addCommas(Math.round(total_five - save_five - retroCost));
	document.getElementById('ten_years').firstChild.nodeValue=addCommas(Math.round(total_ten - save_ten - retroCost));
}

function futureValue(rate, interval, payments, initial){
	var increase=(rate/interval);
	var numerator=(Math.pow((1+increase),payments) - 1);
	var denominator=increase;
  	var total = Math.round(initial * ( numerator / denominator));
	return total	
}

function convertIt(_a) {
  var _c = _a;
  var _x = _a.indexOf("$");
  if (_x == -1 ) _x = _a.indexOf(",");
  if (_x != -1) {
    var _p1 = _a.substr(0,_x);
	var _p2 = _a.substr(_x+1,_a.length);
    _c = convertIt(_p1+_p2);
  }
  return (_c);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
			
/* ]]> */
