/**
 * Crowfoot Liquor Store Alcohol Calculator
 * Used to perform a simple estimate on the amount of alcohol required and cost for an event.
 * Requires Prototype.js
 * Date: March 30, 2009
 * Author: Rob P.
 */

//Constants - configure here but please keep float precision on consumption rates, it matters for ceil calc.
var AVG_PERSON_BEER_CONSUMPTION_PER_HOUR = 1.3;
var AVG_PERSON_WINE_CONSUMPTION_PER_HOUR = 1.3;
var AVG_PERSON_SPIRITS_CONSUMPTION_PER_HOUR = 1.5;
var NUM_BEER_PER_CASE = 12; // 24 beer cans per case
var NUM_WINE_GLASSES_PER_BOTTLE = 5; // 750ml wine bottle yields approx 5, 5oz glasses.
var NUM_SHOTS_PER_BOTTLE = 17; //750ml bottle yields approx 17, 1 1/2oz shots.
var COST_PER_BEER_CASE = 25.59;
var COST_PER_WINE_BOTTLE = 15.00;
var COST_PER_SPIRIT_BOTTLE = 30.59;

/**
 * This function is used to validate the user's input and calculate
 * the quantities of cases of beer, bottles of wine, and bottles of spirits.
 */
function generateTotal(){    
    //User input
  	var durationOfEvent = $F('durationOfEvent');
    var numberOfGuests = $F('numberOfGuests');
    var beerPercentage = $F('beerPercentage');
    var winePercentage = $F('winePercentage');
    var spiritsPercentage = $F('spiritsPercentage');
    
    //Calculated totals
    var totalNumberOfCasesOfBeer = 0;
    var totalNumberOfBottlesOfWine = 0;
    var totalNumberOfBottlesOfSpirits = 0;
    
    //Calculated costs
    var totalCostOfCasesOfBeer = 0.00;
    var totalCostOfBottlesOfWine = 0.00;
    var totalCostOfBottlesOfSpirits = 0.00;
    
    //Error message display area
    var errorMessageDiv = $('errorMessage'); 
    var errorMessage;
    var resultAreaDiv = $('resultArea');
    var printButton = $('printButton');
    //Validation and total logic
    if(!isNaN(numberOfGuests) && numberOfGuests > -1){
        if(!isNaN(durationOfEvent) && durationOfEvent > -1){
            if(!isNaN(beerPercentage) && !isNaN(winePercentage) && !isNaN(spiritsPercentage) && beerPercentage > -1 && winePercentage > -1 && spiritsPercentage > -1){
                if ((beerPercentage * 1 + winePercentage * 1 + spiritsPercentage * 1) == 100.00){
                        totalNumberOfCasesOfBeer = Math.ceil(numberOfGuests * durationOfEvent * AVG_PERSON_BEER_CONSUMPTION_PER_HOUR / NUM_BEER_PER_CASE * (beerPercentage / 100.00));
                        totalNumberOfBottlesOfWine = Math.ceil(numberOfGuests * durationOfEvent * AVG_PERSON_WINE_CONSUMPTION_PER_HOUR / NUM_WINE_GLASSES_PER_BOTTLE * (winePercentage / 100.00));
                        totalNumberOfBottlesOfSpirits = Math.ceil(numberOfGuests * durationOfEvent * AVG_PERSON_SPIRITS_CONSUMPTION_PER_HOUR / NUM_SHOTS_PER_BOTTLE * (spiritsPercentage / 100.00));
                        totalCostOfCasesOfBeer = totalNumberOfCasesOfBeer * COST_PER_BEER_CASE;
                        totalCostOfBottlesOfWine = totalNumberOfBottlesOfWine * COST_PER_WINE_BOTTLE;
                        totalCostOfBottlesOfSpirits = totalNumberOfBottlesOfSpirits * COST_PER_SPIRIT_BOTTLE;
                        errorMessageDiv.hide();
                        errorMessageDiv.update();
                        var ulElement = $('resultsUl');
                        ulElement.update();
                        var beerLi = '<li><strong>' + totalNumberOfCasesOfBeer + '</strong> case(s) of beer costing $'+ totalCostOfCasesOfBeer.toFixed(2) +',</li>';
                        var wineLi = '<li><strong>' + totalNumberOfBottlesOfWine + '</strong> bottle(s) of wine costing $'+ totalCostOfBottlesOfWine.toFixed(2) +', &amp;</li>';
                        var spiritsLi = '<li><strong>' + totalNumberOfBottlesOfSpirits + '</strong> bottle(s) of spirits costing $'+ totalCostOfBottlesOfSpirits.toFixed(2) +'.</li>';
                        ulElement.update(beerLi + wineLi + spiritsLi);
                        resultAreaDiv.show();
                        printButton.show();
                } else {
                    errorMessage = 'Please make sure the percentages total 100%!';
                }
            } else {
                errorMessage ='Please enter valid percentage values!';
            }
        } else {
            errorMessage ='Please enter a valid duration for the event!';
        }
    } else {
        errorMessage ='Please enter a valid number of guests!';
    }
    
    if(errorMessage != null){
    	 resultAreaDiv.hide();
    	 printButton.hide();
       	 errorMessageDiv.update(errorMessage);
         errorMessageDiv.show();
    }
    return false;
}

/**
 * Used to reset the calculator form and return the result area, error message, and print button to default state.
 */
function resetForm(){
    $('resultArea', 'errorMessage', 'printButton').invoke('hide');
   	$('errorMessage').update();
    return true;
}

/**
 * Used to open a new window, inject the results of the calculator, and onload fire a print prompt.
 */
function openPrintPage(){
	var printWindow = window.open('','printWindow','width=500,height=500,scrollbars=1,resizable=1');
	var resultAreaDiv = $('resultArea');
	var html = '<html><head><title>Crowfoot Liquor Party Calculator</title><link rel=\"stylesheet\" type=\"text/css\" href=\"http://driveit.clickspace.com/ir/clients/crowfootliquor/css/driveit_crowfoot.css\" /></head><body onload =\"window.print();\"><img src=\"http://driveit.clickspace.com/ir/clients/crowfootliquor/i/banner.jpg\" /><br /><h1>Crowfoot Liquor Store &amp; Wine Boutique</h1>' + resultAreaDiv.innerHTML + '</body></html>';
	printWindow.document.open();
	printWindow.document.write(html);
	printWindow.document.close();
}
