﻿function pageLoad() {
    $addHandler($get('leng'), 'blur', calcSQFootage);
    $addHandler($get('wdth'), 'blur', calcSQFootage);
    $addHandler($get('celht'), 'blur', calcSQFootage);
    $addHandler($get('airchng'), 'blur', setAirChng);
    $addHandler($get('outsidef'), 'blur', calcTempDif);
    $addHandler($get('insidef'), 'blur', calcTempDif);
}
function openDTempHelp() {
	var newWindow = window.open('design-temp-reference.aspx', 'help2', 'width=600,height=350,resizable=no,status=no,scrollbars=yes');
	if (!newWindow.opener) {
	    newWindow.opener = this;
	}
	newWindow.focus();
}
function openRFactorHelp() {
	var newWindow = window.open('r-factor-reference.aspx', 'help2', 'width=600,height=350,resizable=no,status=no,scrollbars=yes');
	if (!newWindow.opener) {
	    newWindow.opener = this;
	}
	newWindow.focus();
}
function calcSQFootage(evt) {
    var intLength
    var intWidth
    var intHeight
    var intSQFt 
	
	var leng = $get('leng');
	var wdth = $get('wdth');
	var celht = $get('celht');
	var tsqft = $get('tsqft');
	var tcbft = $get('tcbft');
	
    //Get calc values from form
    //Length
    if (leng.value != ""){
        if (!isNaN(leng.value)) {
	        intLength = leng.value;
        } else {
            alert(errorInvalidNumber);
	        leng.value='';
	        leng.focus();
	        return false;	
        }
    }
    //Width
    if (wdth.value != ""){
        if (!isNaN(wdth.value)){
	        intWidth = wdth.value;
        } else {
        alert(errorInvalidNumber);
	        wdth.value='';
	        wdth.focus();
	        return false;	
        }
    }
    //Height
    if (celht.value != ""){
        if (!isNaN(celht.value)){
	        intHeight = celht.value;
        } else {
            alert(errorInvalidNumber);
	        celht.value='';
	        celht.focus();
	        return false;	
        }
    }	
    //Calc sq footage and enter value
    if ((!isNaN(intLength)) && (!isNaN(intWidth))) {
        intSQFt = intLength * intWidth;
        tsqft.value = intSQFt;
    }
    //Calc cube footage and enter value
    if ((!isNaN(intLength)) && (!isNaN(intWidth))&& (!isNaN(intHeight))) {
        tcbft.value = intLength * intWidth * intHeight;
    }
    //Update air change
    setAirChng(null);

    return true;
}

function setAirChng(evt) {
    var airchng = $get('airchng');
	var tcbft = $get('tcbft');
	var airchng2 = $get('airchng2');
	var airchngloss = $get('airchngloss');

    if (!isNaN(parseInt(airchng.value)) && (tcbft.value != '') && (!isNaN(tcbft.value))) {
        airchng2.value = airchng.value * tcbft.value;
    }
    else {
        airchng2.value = '';
    }

    if (!isNaN(airchng2.value)) {
        airchngloss.value = airchng2.value * .019;
    } else {
        airchngloss.value = '';
    }
	
    setAirChngLoss(null);
	
    return true;
}

function setAirChngLoss(evt) {
    var airchngloss = $get('airchngloss');
    var tlosst = $get('tlosst');
    var tairchngloss = $get('tairchngloss');
    var tlosses = $get('tlosses');
    
    if ((airchngloss.value != '') && (!isNaN(airchngloss.value)) && (tlosst.value != '') && (!isNaN(tlosst.value))) {
        tairchngloss.value = parseFloat(tlosst.value) + parseFloat(airchngloss.value);
        tlosses.value = tairchngloss.value;
        calcTotals(null);
    } else {
        tairchngloss.value='';
    }	
	
    return true;
}
function calcTotals(evt) {
    var tlosses = $get('tlosses');
    var temprise = $get('temprise');
    var tbtu = $get('tbtu');
    var twatts = $get('twatts');

    if ((tlosses.value != '') && (!isNaN(tlosses.value)) && (temprise.value != '') && (!isNaN(temprise.value))) {
        tbtu.value = parseFloat(tlosses.value) * parseFloat(temprise.value);
        twatts.value = ' ' + parseFloat(tbtu.value) / 3.412;
    }
    return true;
}
function calcTempDif() {
    var intOutTemp
    var intInTemp
    var intTempDif
    
    var outsidef = $get('outsidef');
    var insidef = $get('insidef');
    var tempdif = $get('tempdif');
    var temprise = $get('temprise');
	
    //outside temp
    if (!isNaN(parseInt(outsidef.value))) {
        intOutTemp = outsidef.value;
    }

    //inside temp
    if (!isNaN(parseInt(insidef.value))) {
        intInTemp= insidef.value;
    }	

    //Calc diff and enter value
    if ((!isNaN(intOutTemp)) && (!isNaN(intInTemp))) {
        intTempDif = intInTemp - intOutTemp;
        tempdif.value = intTempDif;
        temprise.value = tempdif.value;
        calcTotals(null);
    }
    return true;
}
function calcItemBTU(txtbox, strType) {
    var flTLossT = 0
    var winb = $get('winb');
    var doorb = $get('doorb');
    var nwallb = $get('nwallb');
    var roofb = $get('roofb');
    var floorb = $get('floorb');
    var tlosst = $get('tlosst');
    var a, u, b;
    
    if (txtbox.value != '') {
        if (isNaN(parseInt(txtbox.value))) {
            alert(errorInvalidNumber);
            txtbox.value = '';
            txtbox.focus();
            return false;
        }
    } else { 
        return false;
    }
	
    switch(strType) {
        case 'win':
            a = $get('wina');
            u = $get('winu');
            b = winb;
	        break
			
        case 'door':
            a = $get('doora');
            u = $get('dooru');
            b = doorb;
	        break		

        case 'nwall':
	        a = $get('nwalla');
            u = $get('nwallu');
            b = nwallb;
	        break		
			
        case 'roof':
	        a = $get('roofa');
            u = $get('roofu');
            b = roofb;
	        break		
			
        case 'floor':
	        a = $get('floora');
            u = $get('flooru');
            b = floorb;
	        break					
    }
    if (!isNaN(parseInt(a.value)) && !isNaN(parseInt(u.value)) && !isNaN(a.value * (1/u.value)) && isFinite(a.value * (1/u.value))) {
        b.value = a.value * (1/u.value);
    } else {
        b.value = '';
    }

    if ((winb.value != '') && !isNaN(winb.value)) {
        flTLossT = flTLossT + parseFloat(winb.value);
    }

    if ((doorb.value != '') && !isNaN(doorb.value)){
        flTLossT = flTLossT + parseFloat(doorb.value);
    }

    if ((nwallb.value != '') && !isNaN(nwallb.value)){
        flTLossT = flTLossT + parseFloat(nwallb.value);
    }
	
    if ((roofb.value != '') && !isNaN(roofb.value)){
        flTLossT = flTLossT + parseFloat(roofb.value);
    }
	
    if ((floorb.value != '') && !isNaN(floorb.value)){
        flTLossT = flTLossT + parseFloat(floorb.value);
    }	
    if (!isNaN(flTLossT)){
        tlosst.value = flTLossT;
    } else {
        tlosst.value = '';
    }

    setAirChngLoss(null);
	
    return true;
}