// (c) 2004 Ron Jourard

function calcBAC(){if((personalInfo.gender=="")||(personalInfo.weight=="")){alert("Please enter your personal information before\ncalculating your blood alcohol content.");showPersonalLayer();return}if(arDrinks.length==0){alert("Please enter your drinking history information before\ncalculating your blood alcohol content.");showHistoryLayer();return}if(document.fHistory.tbEndTime==""){alert("Please enter the time you finished the last drink\nbefore calculating your blood alcohol content.");showHistoryLayer();var oBox=findObj('tbEndTime');oBox.focus();return}fetchEndDate();if(endDrinkDate.getTime() <arDrinks[arDrinks.length-1].oDate.getTime()){alert("The time the last drink was finished is before\nthe time the last drink was started.\nPlease correct the time(s).");showHistoryLayer();var oBox=findObj('tbEndTime');oBox.focus();return}if(validateTime(document.fResults.tbCalcTime,document.fResults.rbCalcTime)){fetchCalcDate()}else{return}if(endDrinkDate.getTime()> calcDrinkDate.getTime()){alert("The time for which you\nwant to calculate BAC\nis before the time you finished\nyour last drink.\nPlease correct the time(s).");var oBox=document.fResults.tbCalcTime;oBox.focus();return}var oBAC=findObj('tbBAC');var oRemarks=findObj('tbRemarks');var sRemarks="";var nTotalAlcoholML=0;var nAlcoholML=0;var nBAC=0;var nTotalBAC=0;var nFactor=(personalInfo.gender=="M")?0.35:0.30;var nWeightLBS=(personalInfo.isLbs())?personalInfo.weight:personalInfo.weight*2.2;var nRate=personalInfo.rate;var nTimeDifference=0;if(arDrinks.length> 0){for(var i=0;i <arDrinks.length;i++){if(i <arDrinks.length-1){arDrinks[i].nTime=(arDrinks[i+1].oDate.getTime()-arDrinks[i].oDate.getTime())/iOneHour;nTimeDifference=arDrinks[i].nTime}else{arDrinks[i].nTime=(endDrinkDate.getTime()-arDrinks[i].oDate.getTime())/iOneHour;nTimeDifference=arDrinks[i].nTime}nAlcoholML=(arDrinks[i].sUnits=="oz")?arDrinks[i].nAmount:arDrinks[i].nAmount/28.41;nAlcoholML=nAlcoholML*(arDrinks[i].nPercent/0.40);arDrinks[i].nAlcoholML=nAlcoholML;nTotalAlcoholML+=nAlcoholML;nBAC=(nAlcoholML/(nWeightLBS*nFactor))*1000;nTotalBAC=nTotalBAC+nBAC;nTotalBAC=(nTotalBAC-((nRate)*nTimeDifference));if(nTotalBAC <0){nTotalBAC=0}}if((calcDrinkDate.getTime()-endDrinkDate.getTime()) <(20*iOneMinute)){alert("NOTE:\n\nThe alcohol might not have been completely\nabsorbed into your bloodstream.\nIt can take 20 minutes or longer for full\nabsorption depending on your drinking pattern.")}nTotalBAC=nTotalBAC-(((calcDrinkDate.getTime()-endDrinkDate.getTime())/iOneHour)*(nRate));if(nTotalBAC <0){nTotalBAC=0}oBAC.value=(Math.ceil(nTotalBAC))+"";sRemarks="";sRemarks="You are a "+((personalInfo.gender=="M")?"male":"female")+" and weigh ";sRemarks+=Math.round(personalInfo.weight*10)/10;sRemarks+=(personalInfo.isLbs())?" lbs":(" kgs("+Math.round(personalInfo.weight*2.2*10)/10+" lbs)");sRemarks+=".\n";sRemarks+="You have selected an elimination rate of "+nRate+" mg of alcohol per 100 ml of blood per hour.\n";sRemarks+="You had "+arDrinks.length+" drink";if(arDrinks.length> 1){sRemarks+="s"}sRemarks+=" beginning on ";sRemarks+=arDrinks[0].oDate.getDateString()+" at ";sRemarks+=arDrinks[0].oDate.getDisplayTimeString()+".";var currentDate=arDrinks[0].oDate.getDateString();for(var i=0;i <arDrinks.length;i++){if(arDrinks[i].oDate.getDateString()!=currentDate){currentDate=arDrinks[i].oDate.getDateString();sRemarks+="\nOn "+currentDate+":"}sRemarks+="\nYou began drink "+(i+1)+" ["+arDrinks[i].sName;sRemarks+="] at "+arDrinks[i].oDate.getTimeString()+" "+(arDrinks[i].oDate.isPM()?"PM":"AM")+"."}sRemarks+="\nYou finished your last drink on "+endDrinkDate.getDateString()+" at "+endDrinkDate.getDisplayTimeString()+".";var sRemarks2="\nYour BAC on "+calcDrinkDate.getDateString()+" at "+calcDrinkDate.getDisplayTimeString();if (calcDrinkDate.getTime() <new Date().getTime()){sRemarks2+=" was "}else{sRemarks2+=" will be "}sRemarks2+=oBAC.value+" (mgs of alcohol per 100 ml of blood)."}else{oBAC.value="****";sRemarks="No Drinking History."}oRemarks.value=sRemarks+sRemarks2;var overUnder=(oBAC.value <80)?"UNDER":((oBAC.value> 80)?"OVER":"AT");overUnderMsg="\n\nYour blood alcohol concentration is <b>"+overUnder;overUnderMsg+="</b> the legal limit in Canada. ";overUnderMsg+="\nIt is a crime to operate or have ";overUnderMsg+="the care or control of a motor vehicle \nif your BAC ";overUnderMsg+="exceeds 80 mgs of alcohol in 100 ml of blood.";if (oBAC.value <=80){overUnderMsg+="\n<b>Warning:</b> your ability to operate a motor vehicle ";overUnderMsg+="may be impaired by alcohol even if you are under ";overUnderMsg+="the legal limit."}sRemarks=sRemarks+"\n<b>"+sRemarks2+"</b>"+overUnderMsg;var WarningMsg='\n\n<table border="1" cellpadding="5" width=\"400\" bordercolor="#000000" style="border:thin;"><tr><td>';WarningMsg+="<p><font size=2>WARNING AND DISCLAIMER:\n";WarningMsg+="The blood alcohol concentration determined by the calculator may not ";WarningMsg+="reflect physiological and other ";WarningMsg+="factors such as age, disease states and meal times.<br><br>";WarningMsg+="It is dangerous ";WarningMsg+="to drive a vehicle or operate machinery after drinking alcohol. ";WarningMsg+="If you do so, you assume full risk of injury and death to yourself and others.</font></p>";WarningMsg+='</td></tr></table>';WarningMsg+="\nCalculator results are provided courtesy of Toronto,Ontario criminal\n";WarningMsg+="lawyer Ron Jourard and forensic toxicologist Gerry Kupferschmidt.\n\n";WarningMsg+="Mr. Jourard can be reached at (416) 398-6685 or toll free \n";WarningMsg+="(Canada and U.S.) 1-888-257-0002,or by email at ";WarningMsg+="<a href=\"mailto:jourard@defencelaw.com\">jourard@defencelaw.com</a>.\n";WarningMsg+="Visit his website at <a href=\"http://www.defencelaw.com\" target=\"homesite\">http://www.defencelaw.com</a>.\n\n";WarningMsg+="Mr. Kupferschmidt can be reached at ";WarningMsg+="<a href=\"mailto:services@forcon.ca\">services@forcon.ca</a>.\n";WarningMsg+="Visit his website at <a href=\"http://www.forcon.ca\">http://www.forcon.ca</a>.";sRemarks=sRemarks+WarningMsg;sResults=sRemarks.replace(/\n/g,"<BR>");var resultsMessage=findObj("resultsMessage");resultsMessage.innerHTML=overUnderMsg;showHidePrintButton("show")}