﻿function PrintSalesRoomResults() {

    SendSalesRoomResultsEmail();

    var answer = confirm("Would you like to print a copy for your records?");
    if (answer) {
        window.print();
    }
    else {
        window.close();
    }
}


function FigureOutAI() {
    RemoveDollar();
    document.all.f44.value = Number(document.all.iwaterbottle.value) * 5;
    document.all.f46.value = 5.00 * Number(document.all.ibeer.value);
    document.all.f48.value = 5.00 * Number(document.all.idrink.value);
    document.all.fsubototal.value = (Number(document.all.f42.value) + Number(document.all.f43.value) + Number(document.all.f44.value) +
		Number(document.all.f45.value) + Number(document.all.f46.value) + Number(document.all.f47.value) + Number(document.all.f48.value) +
		Number(document.all.f49.value));

    document.all.ftips.value = document.all.fsubototal.value * 0.10;

    document.all.ftotaldaily.value = Number(document.all.fsubototal.value) + Number(document.all.ftips.value);
    document.all.ftotalx7.value = document.all.ftotaldaily.value * 7;
    document.all.ftotal4star.value = document.all.f4star.value * 7;
    document.all.ftotalx7_1.value = document.all.ftotalx7.value;
    document.all.ftotal4star_1.value = document.all.ftotal4star.value;
    document.all.ftotalvacation.value = Number(document.all.ftotalx7_1.value) + Number(document.all.ftotal4star_1.value);
    document.all.ftotalvacx7.value = document.all.fcost1day.value * 7;
    document.all.favgsavings.value = Number(document.all.ftotalvacation.value) - Number(document.all.fcost1day.value);
    document.all.weeks.value = roundNumber(document.all.fvacowncost.value / document.all.favgsavings.value);
    RoundDollar();

    return false;
}

function FigureOutSales() {
    RemoveDollar();
    document.all.fcurrentprice.value = (document.all.fstartprice.value - document.all.fbottomline.value);
    document.all.fnewcloserbottomline1.value = Number(document.all.fbottomline.value) + Number(document.all.faddon1.value) + Number(document.all.fnewbottomline1.value);
    document.all.fnewcloserbottomline2.value = Number(document.all.fbottomline.value) + Number(document.all.faddon2.value) + Number(document.all.fnewbottomline2.value);
    document.all.fnewcloserbottomline3.value = Number(document.all.fbottomline.value) + Number(document.all.faddon3.value) + Number(document.all.fnewbottomline3.value);
    document.all.fnewcloserbottomline4.value = Number(document.all.fbottomline.value) + Number(document.all.faddon4.value) + Number(document.all.fnewbottomline4.value);

    /*	document.all.fnewcloserbottomline2.value = (document.all.fbottomline.value + document.all.faddon2.value + document.all.fnewbottomline2.value );
    document.all.fnewcloserbottomline3.value = (document.all.fbottomline.value + document.all.faddon3.value + document.all.fnewbottomline3.value );
    document.all.fnewcloserbottomline4.value = (document.all.fbottomline.value + document.all.faddon4.value + document.all.fnewbottomline4.value );*/
    document.all.fmarginformanager1.value = document.all.fnewbottomline1.value;
    document.all.fmarginformanager2.value = document.all.fnewbottomline2.value;
    document.all.fmarginformanager3.value = document.all.fnewbottomline3.value;
    document.all.fmarginformanager4.value = document.all.fnewbottomline4.value;
    RoundDollarNoDecimals();
    return false;
}
function FigureOutBA() {
    RemoveDollar();
    document.all.ftotal3star1.value = (document.all.f3.value * 7);
    document.all.ftotal4star1.value = (document.all.f4.value * 7);
    document.all.ftotal5star1.value = (document.all.f5.value * 7);

    document.all.ftotal4star2.value = (document.all.f4.value * 7);
    document.all.fsavings.value = (Number(document.all.ftotal4star2.value) - 450);
    document.all.itotal.value = roundNumber(document.all.fownership.value / document.all.fsavings.value);
    RoundDollarNoDecimals();
    return false;
}

function roundNumber(number) {
    var numberField = number; // Field where the number appears
    var rnum = number;
    var rlength = 2; // The number of decimal places to round to
    if (rnum > 8191 && rnum < 10485) {
        rnum = rnum - 5000;
        var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
        newnumber = newnumber + 5000;
    } else {
        var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
    }
    return newnumber;
}


function FigureOut() {
    RemoveDollar();
    document.all.f121.value = document.all.i111.value * document.all.f112.value;
    document.all.f221.value = document.all.f121.value - document.all.f211.value;

    document.all.f321.value = (Number(document.all.f221.value) + Number(document.all.f311.value)) - 700;
    document.all.f322.value = (Number(document.all.f321.value) + Number(document.all.f312.value)) - 700;
    document.all.f323.value = (Number(document.all.f322.value) + Number(document.all.f313.value)) - 700;
    document.all.f324.value = (Number(document.all.f323.value) + Number(document.all.f314.value)) - 700;

    document.all.i411.value = document.all.i111.value;
    document.all.f412.value = document.all.i411.value * 400;
    document.all.f413.value = document.all.f211.value;
    document.all.f421.value = document.all.f412.value - document.all.f413.value;
    RoundDollar();
}


function FigureOut2(){
	RemoveDollar();
	document.all.i113.value = (document.all.i111.value * document.all.i112.value ) / 100;
	document.all.i114.value = document.all.i111.value - document.all.i113.value;
	document.all.f121.value = document.all.i114.value * document.all.f115.value;
	document.all.f213.value = document.all.f211.value - document.all.f212.value;
	document.all.i215.value = (document.all.i214.value * document.all.i114.value ) / 100;
	document.all.f221.value = document.all.i215.value * document.all.f213.value;
	document.all.i311.value = document.all.i114.value - document.all.i215.value;
	document.all.f314.value = document.all.f312.value - document.all.f313.value;
	document.all.i316.value = (document.all.i311.value * document.all.i315.value ) / 100;
	document.all.f321.value = document.all.f314.value * document.all.i316.value;
	document.all.f322.value = Number(document.all.f321.value) + Number(document.all.f221.value);
	document.all.f421.value = document.all.f121.value;
	document.all.f422.value = document.all.f322.value - document.all.f421.value;
	RoundDollar();
	return false;
//	MakeDollar();
}

function RemoveDollar() {
    for (i = 0; i < document.forms[0].elements.length; ++i) {
        myField = document.forms[0].elements[i];
        if (myField.type == "text" && isNaN(myField.value) && (myField.name.substr(0, 1) == "i" || myField.name.substr(0, 1) == "f")) {
            itemValue = myField.value;
            newValue = "";
            for (a = 0; a < itemValue.length; ++a) {
                if (!isNaN(itemValue.substr(a, 1)) || itemValue.substr(a, 1) == ".") {
                    newValue = newValue + itemValue.substr(a, 1);
                }
            }
            document.forms[0].elements[i].value = newValue;
        }
    }
    return false;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
	num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function formatCurrencyNoDecimals(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
	num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num);
}

function RoundDollar() {
    for (i = 0; i < document.forms[0].elements.length; ++i) {
        myField = document.forms[0].elements[i];
        if (myField.type == "text" && (myField.name.substr(0, 1) == "f")) {
            document.forms[0].elements[i].value = formatCurrency(myField.value);
        }
    }
    return false;
}
function RoundDollarNoDecimals() {
    for (i = 0; i < document.forms[0].elements.length; ++i) {
        myField = document.forms[0].elements[i];
        if (myField.type == "text" && (myField.name.substr(0, 1) == "f")) {
            document.forms[0].elements[i].value = formatCurrencyNoDecimals(myField.value);
        }
    }
}

