/*
 * Code to Calulate Worksheet Results
 * for Germany Worksheet
 * 3/19/2008 by NNB
 */


/* Functions formatCommas, formatNumber, stripCommas, and numeralsOnly
 * from JavaScript & DHTML Cookbook, by Danny Goodman.
 * Sebastopol, CA: O'Reilly Media, Inc., 2003. 
 * ISBN 0-596-00467-2
 */

function formatCommas(numString) {
    var re = /(-?\d+)(\d{3})/;
    while (re.test(numString)) {
        numString = numString.replace(re, "$1,$2");
    }
    return numString;
}


function formatNumber (num, decplaces) {
    // convert in case it arrives as a string value
    num = parseFloat(num);
    // make sure it passes conversion
    if (!isNaN(num)) {
        // multiply value by 10 to the decplaces power;
        // round the result to the nearest integer;
        // convert the result to a string
        var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
        // exponent means value is too big or small for this routine
        if (str.indexOf("e") != -1) {
            return "Out of Range";
        }
        // if needed for small values, pad zeros
        // to the left of the number
        while (str.length <= decplaces) {
            str = "0" + str;
        }
        // calculate decimal point position
        var decpoint = str.length - decplaces;
        // assemble final result from: (a) the string up to the position of
        // the decimal point; (b) the decimal point; and (c) the balance
        // of the string. Return finished product.
        return formatCommas(str.substring(0,decpoint)) + "." + str.substring(decpoint,str.length);

    } else {
        return "NaN";
    }
}


function stripCommas(numString) {
    var re = /,/g;
    return numString.replace(re,"");
}


function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    /* Allow users to edit their entries (charCodes < 32), 0-9, ".", and "," */
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46 && charCode != 44) {
        alert("Enter numerals only in this field.");
        return false;
    }
    return true;
}


/* ==================================================================================================== */

/* 'Magic' numbers */
var hotelVATRate = 0.065;
var foodBevVATRate = 0.16;
var otherVATRate = 0.16; 


function CalcHotelTotal(form) {

 /* init */
 totalhotelvat = 0;

 perperson = stripCommas(form.perperson.value);
 nights = stripCommas(form.nights.value);
 people = stripCommas(form.people.value);
 totalhotel = stripCommas(form.totalhotel.value);

 totalcostperperson = perperson * nights;
 totalcost = totalcostperperson * people;

 if (totalcost != 0) {
	form.totalhotel.value = formatNumber(totalcost, 2);
	totalhotelvat = totalcost * hotelVATRate;
 } else if (totalhotel != 0) {
	totalhotelvat = totalhotel * hotelVATRate;
 }

 form.totalhotelvat.value = formatNumber(totalhotelvat, 2);

 return totalhotelvat;

}


function CalcFoodBevTotal(form) {

 /* init */
 totalfoodbevvat = 0;

 Mealperperson = stripCommas(form.Mealperperson.value);
 days = stripCommas(form.days.value);
 mealpeople = stripCommas(form.mealpeople.value);
 totalfoodbev = stripCommas(form.totalfoodbev.value);

 mealcostperperson = Mealperperson * days;
 totalmealcost = mealcostperperson * mealpeople;

 if (totalmealcost != 0) {
	form.totalfoodbev.value = formatNumber(totalmealcost, 2);
	totalfoodbevvat = totalmealcost * foodBevVATRate;
 } else if (totalfoodbev != 0) {
	totalfoodbevvat = totalfoodbev * foodBevVATRate;
 }

 form.totalfoodbevvat.value = formatNumber(totalfoodbevvat, 2);

 return totalfoodbevvat;

}


function CalcOtherTotal(form) {

 /* init */
 totalothervat = 0;

 meeting = stripCommas(form.meeting.value);
 av = stripCommas(form.av.value);
 entertainment = stripCommas(form.entertainment.value);
 ps = stripCommas(form.ps.value);
 exhibition = stripCommas(form.exhibition.value);
 totalother = stripCommas(form.totalother.value);

 meeting = (meeting == "") ? 0 : meeting;
 av = (av == "") ? 0 : av;
 entertainment = (entertainment == "") ? 0 : entertainment;
 ps = (ps == "") ? 0 : ps;
 exhibition = (exhibition == "") ? 0 : exhibition;
 totalother = (totalother == "") ? 0 : totalother;

 totalothercost = eval(meeting) + eval(av) + eval(entertainment) + eval(ps) + eval(exhibition);

 if (totalothercost != 0) {
	form.totalother.value = formatNumber(totalothercost, 2);
	totalothervat = totalothercost * otherVATRate;
 } else if (totalother != 0) {
	totalothervat = totalother * otherVATRate;
 }

 form.totalothervat.value = formatNumber(totalothervat, 2);

 return totalothervat;

}


function lastofit( form ) {

 /* init */
 totalreclaim = 0;

 perperson = form.perperson.value;
 nights = form.nights.value;
 people = form.people.value;
 totalhotel = form.totalhotel.value;
 Mealperperson = form.Mealperperson.value;
 days = form.days.value;
 mealpeople = form.mealpeople.value;
 totalfoodbev = form.totalfoodbev.value;
 meeting = form.meeting.value;
 av = form.av.value;
 entertainment = form.entertainment.value;
 ps = form.ps.value;
 exhibition = form.exhibition.value;
 totalother = form.totalother.value;

 totalhotelvat = CalcHotelTotal(form);
 totalfoodbevvat = CalcFoodBevTotal(form);
 totalothervat = CalcOtherTotal(form);

 totalreclaim = totalhotelvat + totalfoodbevvat + totalothervat;

 form.totalreclaim.value = formatNumber(totalreclaim, 2);

}


function clearHotelTotals( form ) {

	form.totalhotel.value = 0;
	form.totalhotelvat.value = 0;

	form.totalreclaim.value = 0;

}


function clearFoodBevTotals( form ) {

	form.totalfoodbev.value = 0;
	form.totalfoodbevvat.value = 0;

	form.totalreclaim.value = 0;

}


function clearOtherTotals( form ) {

	form.totalother.value = 0;
	form.totalothervat.value = 0;

	form.totalreclaim.value = 0;

}


function clearHotelItems( form ) {

	form.perperson.value = 0;
	form.nights.value = 0;
	form.people.value = 0;

}


function clearFoodBevItems( form ) {

	form.Mealperperson.value = 0;
	form.days.value = 0;
	form.mealpeople.value = 0;

}


function clearOtherItems( form ) {

	form.meeting.value = 0;
	form.av.value = 0;
	form.entertainment.value = 0;
	form.ps.value = 0;
	form.exhibition.value = 0;

}



