function getBeercosts() {
	var path = rootpath + '/diversions/functions/ajx/ajx_beercost.php';
	sndReq('get',path,'method=get','beercosts',null,null,'<img src='+String.fromCharCode(34)+rootpath+'/images/loading.gif'+String.fromCharCode(34)+'>');
}

function saveBeercost(bid,name,cont,sz,numCont,tcost,costperoz,costpergal,comm,city,state,country) {
	var path = rootpath + '/diversions/functions/ajx/ajx_beercost.php';
	sndReq('get',path,'method=save&name=' + encodeURIComponent(name) + '&bid=' + encodeURIComponent(bid) + '&cont=' + encodeURIComponent(cont) + '&sz=' + encodeURIComponent(sz) + '&numCont=' + encodeURIComponent(numCont) + '&tcost=' + encodeURIComponent(tcost) + '&costperoz=' + encodeURIComponent(costperoz) + '&costpergal=' + encodeURIComponent(costpergal) + '&comm=' + encodeURIComponent(comm) + '&city=' + encodeURIComponent(city) + '&state=' + encodeURIComponent(state) + '&country=' + encodeURIComponent(country),'saveresult',null,null,'<img src='+String.fromCharCode(34)+rootpath+'/images/wait.gif'+String.fromCharCode(34)+'>');
	getBeercosts();
}

function saveBeerComment(bid,comm) {
	var path = rootpath + '/diversions/functions/ajx/ajx_beercost.php';
	sndReq('get',path,'method=comm&bid=' + encodeURIComponent(bid) + '&comm=' + encodeURIComponent(comm),'commlink_'+bid,null,null,'<img src='+String.fromCharCode(34)+rootpath+'/images/wait.gif'+String.fromCharCode(34)+'>');
	sndReq('get',path,'method=getOne&bcID=' + encodeURIComponent(bid),'comm_'+bid,null,null,null);
}

function flagComm(level,id,comm) {
	var path = rootpath + '/diversions/functions/ajx/ajx_beercost.php';
	sndReq('get',path,'method=flagcomm&bid=' + encodeURIComponent(id) + '&level=' + encodeURIComponent(level) + '&comm=' + encodeURIComponent(comm),'flag_'+level+'_'+id,null,null,'<img src='+String.fromCharCode(34)+rootpath+'/images/wait.gif'+String.fromCharCode(34)+'>');
}

function showComm(id,show) {
	var ih = '<a href="javascript:showComm(' + id + ',true);">Comment on this</a>';
	if (show) ih='<form class="frm_comments" name="frm_' + id + '"><textarea name="txt_' + id + '" id="txt_' + id + '"></textarea><br /><input type="button" value="cancel" onClick="showComm(' + id + ',false);" /><input type="button" value="submit" onClick="saveBeerComment('+id+',this.form.txt_'+id+'.value);" />';

	hideAllCommTxt();

	setDivText('commlink_'+id,ih);
	if (show) eval('document.frm_'+id+'.txt_'+id+'.focus();');
}

function hideAllCommTxt() {
	var pgt = document.getElementsByTagName("*");
	for (i=0; i<pgt.length; i++) {
		if (pgt[i].id.substring(0,4)=='txt_') {
			var id = pgt[i].id.split("_")[1];
			var ih = '<a href="javascript:showComm(' + id + ',true);">Comment on this</a>';
			setDivText('commlink_'+id,ih);
		}
	}
}

function calcBeerGal(frm,save) {
	var bid=frm.beerName[frm.beerName.selectedIndex].value;
	var nam=frm.beerName[frm.beerName.selectedIndex].title;
	var cont=frm.beerContainer[frm.beerContainer.selectedIndex].value;
	var sz=frm.szContainer.value.trim();
	var numCont=frm.numCont[frm.numCont.selectedIndex].value;
	var onum=frm.onumCont.value.trim();
	var tcost=frm.totalcost.value.trim();
	var comm=frm.beerComments.value.trim();
	if (numCont=='other') numCont=onum;	

	var valText=validateBeerCost(sz,numCont,tcost);
	if (save) {
		var city=frm.city.value.trim();
		var state=frm.hsState[frm.hsState.selectedIndex].value;
		var country=frm.country.value.trim();
		valText+= validateLoc(city,state,country);
	}

	if (valText=='') {
		var totaloz=numCont*sz;
		var costperoz=tcost/totaloz;
		var costpergal=costperoz*128;
		costperoz=costperoz.toFixed(3);
		costpergal=costpergal.toFixed(2);
	
		var str = '';
		if (nam!='') str+= nam + ':<br/>';
		str+=numCont + '(' + cont + ') x ' + sz + '(oz each) = ' + totaloz + ' oz per ' + numCont + ' ' + cont + '<br />';
		str+='Cost = $' + tcost + ' = $' + costperoz + ' per oz = <span class="result">$' + costpergal + ' per gallon</span><br /><br />' + comm;
	
		setDivText('output',str);
		if (!save) {
			setDiv('sharebtn',null,null,'block');
			setDiv('share',null,null,'none');
		}
		else {	//ajax save and reload
			saveBeercost(bid,nam,cont,sz,numCont,tcost,costperoz,costpergal,comm,city,state,country);
		}
	}
	else {
		if (!save) {
			setDivText('output',valText);
			setDiv('sharebtn',null,null,'none');
			setDiv('share',null,null,'none');
		}
		else {
			setDivText('saveresult',valText);
		}
	}
}

function validateBeerCost(sz,numCont,tcost) {
	var retVal='';

	//sz should be a postive integer
	if (sz=='' || !isNumeric(sz, true, 0)) retVal+='<li>The Ounce per ... must be a positive integer.</li>';

	//numCont should be a positive integer
	if (numCont=='' || !isNumeric(numCont, true, 0)) retVal+='<li>The number purchased must be a positive integer.</li>';

	//tcost should be a numeric dollar and cents amount
	if (tcost=='' || !isNumeric(tcost, false, 0)) retVal+='<li>The total cost must be a whole number or decimal (e.g. 13 or 12.99).</li>';

	if (retVal!='') retVal='<ul class="err">'+retVal+'</ul>';
return retVal;
}

function validateLoc(city,state,country) {
	var retVal='';

	//city should not be blank
	if (city=='') retVal+='<li>City should not be blank.</li>';
	//state should not be blank
	if (state=='') retVal+='<li>State should not be blank.</li>';
	//country should not be blank
	if (country=='') retVal+='<li>Country should not be blank.</li>';
return retVal;
}

function toggleQty(blnShow) {
	var hs='none';
	if (blnShow) hs='block';
	setDiv('othernum',null,null,hs);
	if (blnShow) document.frmBeerCost.onumCont.focus();
}

function changeLabels(t) {
	var n=t;
	var f=document.frmBeerCost;
	if (t=="keg") {
		f.szContainer.value="1984";
		f.numCont.selectedIndex=0;
		t="keg";
		n="Kegs";
	}
	else if (t=="half barrel keg") {
		f.szContainer.value="1984";
		f.numCont.selectedIndex=0;
		t="keg";
		n="Kegs";
	}
	else if (t=="pony keg") {
		f.szContainer.value="992";
		f.numCont.selectedIndex=0;
		t="keg";
		n="Kegs";
	}
	else if (t=="bottles") {
		f.szContainer.value="12";
		f.numCont.selectedIndex=2;
		t='bottle';
		n="Bottles";
	}
	else if (t=="cans") {
		f.szContainer.value="12";
		f.numCont.selectedIndex=2;
		t='can';
		n="Cans";
	}
	else {
		f.numCont.selectedIndex=0;
		t='item';
		n="Items";
	}
	setDivText('ozper',t);
	setDivText('sp_numCont',n);
}

function sharedata() {
	var nam=document.frmBeerCost.beerName[document.frmBeerCost.beerName.selectedIndex].value.trim();
	if (nam=='') {
		alert('Please select a beer from the Beer list');
	}
	else {
		setDiv('share',null,null,'block');
		setDiv('sharebtn',null,null,'none');
	}
}

