BrowserIE=navigator.appVersion.indexOf("MSIE")!=-1?true:false;
BrowserIEVer=navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE")+5, 1);

function FullHeight() {
	var Stage=document.getElementById('StageI');
	var Footer=document.getElementById('Footer');
	var WindowHeight=0;
	if (typeof(window.innerHeight)=='number') {	//Non-IE
		WindowHeight=window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {	//IE 6+ in 'standards compliant mode'
		WindowHeight=document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {	//IE 4 compatible
		WindowHeight=document.body.clientHeight;
	};
	var StageHeight=Stage.scrollHeight;
	if (!StageHeight)
		StageHeight=Stage.clientHeight;
	if (WindowHeight>StageHeight+Footer.scrollHeight) {
		Stage.style.height=(WindowHeight-Footer.scrollHeight-11)+'px';
	} else {
		Stage.style.height='auto';
	};
	Footer.style.visibility='visible';
};
//window.onresize=FullHeight;
//window.onload=FullHeight;

function ShowBigImg(Img) {
	if (Img.id=='ActiveImg') {
		return;
	};
	if (document.getElementById('ActiveImg')) {
		document.getElementById('ActiveImg').id='';
//		FadeSwap('ThisBigImg', 'images/loading.gif');
	} else
		document.getElementById('ImgBig').innerHTML='<img src="images/loading.gif" id="ThisBigImg" />';
	Img.id='ActiveImg';
	var NewImg=new Image();
	var NewImgURL=Img.src.replace(/\/thumbs\//, '/resized/').replace(/(\/\d+\.\d)t_/, '$1_');
	NewImg.onload=function () {FadeSwap('ThisBigImg', this.src);};
	NewImg.src=NewImgURL;
};

var Features=false;
var LastFeature=0; var CurFeature=0; var SFTimer=false; var SFInterval=10; // seconds
var FeatureDIV=false; var FeatureLink=false; var FeatureIMG=false;

function InitFeatures() {
	FeatureLink=document.getElementById('FeatureLink');
	FeatureDIV=FeatureLink.parentNode;
	FeatureIMG=FeatureLink.firstChild;

	// start preloading images
	PreloadFeatureImg();

	SFTimer=setTimeout('ShowFeature();', SFInterval*1000);
};
var Preloading=0; var Cache=new Array();
function PreloadFeatureImg() {
	Preloading++;
	if (Cache[Preloading]==undefined && Preloading<Features.length) {
		Cache[Preloading]=new Image();
		Cache[Preloading].onload=PreloadFeatureImg;
		Cache[Preloading].src='images/features/'+Features[Preloading]['idx']+'.jpg';
	} else
		Preloading=-1;
};

function ShowFeature(thisEvent, ForceIndex) {
	clearTimeout(SFTimer);
	var ToShow=CurFeature;
	if (ForceIndex==undefined)
		ToShow++;
	else	ToShow=ForceIndex;
	if (ToShow>=Features.length)
		ToShow%=Features.length;
	SFTimer=setTimeout('ShowFeature();', SFInterval*1000);
	if (ToShow==CurFeature)
		return;
	clearTimeout(FSTimer);
	LastFeature=CurFeature;
	CurFeature=ToShow;
	FadeSwap(ToShow);
};

var FSTimer=false; var ThisStep=0;
function FadeSwap(NewFeatureI, Timeout, NewImgURL) {
	if (FSTimer) {
		clearTimeout(FSTimer);
	};
	if (Timeout==undefined) {
		Timeout=50;
	};
	if (NewImgURL==undefined) {
		NewImgURL='';
		if (Timeout!=undefined && !Timeout.toString().match(/^\d+$/)) {
			NewImgURL=Timeout;
			Timeout=50;
		};
	};
	var IsFeature=NewFeatureI.toString().match(/^\d+$/);
	if (!IsFeature) {
		FeatureIMG=document.getElementById(NewFeatureI);
	};
	var TargetOpacity=0;
	switch (ThisStep) {
	case 0:	// fade out
		TargetOpacity=0.7;
		break;
	case 1:
		TargetOpacity=0.4;
		break;
	case 2:
		TargetOpacity=0.1;
		break;
	case 3: // swap image
		if (IsFeature) {
			document.getElementById('FNActive').id='';
			FeatureIMG.src='images/features/'+Features[NewFeatureI]['idx']+'.jpg';
			FeatureIMG.title=Features[NewFeatureI]['summary'];
			FeatureLink.href=Features[NewFeatureI]['link'];
			document.getElementById('FeatureNav').childNodes[NewFeatureI].id='FNActive';
		} else {
			FeatureIMG.src=NewImgURL;
		};
		break;
	case 4:	// fade in
		TargetOpacity=0.4;
		break;
	case 5:
		TargetOpacity=0.7;
		break;
	case 6:
		TargetOpacity=1;
		break;
	};
	if (TargetOpacity>0) {
		if (!BrowserIE)
			FeatureIMG.style.opacity=TargetOpacity;
		else	FeatureIMG.style.filter='alpha(opacity='+TargetOpacity*100+')';
	}
	if (ThisStep<6) {
		FSTimer=setTimeout('FadeSwap(\''+NewFeatureI+'\', '+Timeout+', "'+NewImgURL+'");', Timeout);
		ThisStep++;
	} else {
		ThisStep=0;
		FSTimer=false;
	};
};

function FormatPrice(Price) {
	var PriceHTML=typeof(Price)=='number'?(Math.round(Price*100)/100).toString():parseFloat(Price).toString();
	if (PriceHTML.indexOf('.')==-1) {
		PriceHTML+='.00';
	} else if ((PriceHTML.length-PriceHTML.indexOf('.')-1)==1) {
		PriceHTML+='0';
	};
	var PriceParts=PriceHTML.split('.');
	var CommaDollars='';
	while (PriceParts[0]!='') {
		var Matches=PriceParts[0].match(/(\d{1,3})$/);
		CommaDollars=','+Matches[1]+CommaDollars;
		PriceParts[0]=PriceParts[0].replace(/(\d{1,3})$/, '');
	};
	return CommaDollars.replace(/^,*/, '')+'.'+PriceParts[1];
};

var BNChosen=new Array(); var BNChosenCount=0;
var CustOrderPage=0;
function ReduceQty(Field) {
	var Value=parseInt(Field.value, 10)-1;
	if (Value>0) {
		Field.value=Value;
	} else {
		Field.value=0;
	};
	if (!CustOrderPage) {
		var Category='cat'+CurrentCat.toString();
		var Img=new Image();
		if (Value>0) {
			BNChosen[Category][Field.name]=Field.value;
			Img.src='/buynowajax.php?sid='+SessionID+'&c='+Category+'&b='+Field.name+'&v='+Field.value;
		} else if (BNChosen[Category]!=undefined) {
			delete BNChosen[Category][Field.name];
			Img.src='/buynowajax.php?sid='+SessionID+'&c='+Category+'&b='+Field.name+'&v=0';
		};
		UpdateSubTotal();
	} else {

	};
};
function AddQty(Field, AddBy) {
	var Category='cat'+CurrentCat.toString();
	if (AddBy==undefined)
		AddBy=1;
	else if (Field.value==0) {
		RemoveBeer(false, Category, Field.name);
		return;
	};
	Field.value=parseInt(Field.value, 10)+AddBy;
	if (!CustOrderPage) {
		if (BNChosen[Category]==undefined) {
			BNChosen[Category]=new Array();
		};
		if (BNChosen[Category][Field.name]==undefined) {
			BNChosen[Category][Field.name]=new Array();
		};
		BNChosen[Category][Field.name]=Field.value;
		var Img=new Image();
		Img.src='/buynowajax.php?sid='+SessionID+'&c='+Category+'&b='+Field.name+'&v='+Field.value;
		UpdateSubTotal();
	} else {

	};
};
function RemoveBeer(DIV, Category, FieldName) {
	delete BNChosen[Category][FieldName];
	var Img=new Image();
	Img.src='/buynowajax.php?sid='+SessionID+'&c='+Category+'&b='+FieldName+'&v=0';
//	DIV.parentNode.removeChild(DIV);
	var Form=document.getElementById('BNBeersForm');
	if (Form.elements[FieldName]!=undefined) {
		Form.elements[FieldName].value=0;
	};
	UpdateSubTotal();
};
var FreeDeliveryMinValue=200;
function UpdateSubTotal() {
	var i=0; var Total=0; BNChosenCount=0;
	var BNListing='';
	var PriceFactor=BottlesN/StdBottlesN;
	for (Cat in BNChosen) {
		var RealCat=Cat.substr(3);
		for (B in BNChosen[Cat]) {
			var BSplit=B.split('_');	// 0=voucher/beer, 1=b['idx'], 2=b['ml']
			var Beer=BNBeers[RealCat][BSplit[1]];
			var Name=Beer['name'];
			var PriceHTML=Beer['price'];
			if (Beer['ml']!=BSplit[2]) {
				var VOLs=Beer['ml'].split(',');
				var Prices=Beer['price'].split(',');
				for (i in VOLs) {
					if (VOLs[i]==BSplit[2]) {
						Name+=' ('+VOLs[i]+'ml)';
						PriceHTML=Prices[i];
						break;
					};
				};
			};
			Price=parseFloat(PriceHTML)*PriceFactor;
			Total+=Price*BNChosen[Cat][B];
			BNListing+='<div class="Row BNSummaryLine"><a href="#" onclick="RemoveBeer(this.parentNode, \''+Cat+'\', \''+B+'\');return false;"><img src="/images/remove.png" /></a><span class="Name">'+Name+'</span><span>'+BNChosen[Cat][B]+' x S$'+FormatPrice(Price)+'</span><span>S$'+FormatPrice(Price*BNChosen[Cat][B])+'</span><input type="hidden" name="'+B+'" value="'+BNChosen[Cat][B]+'" /></div>';
			BNChosenCount++;
		};
	};
	if (BNListing=='') {
		BNListing='<div class="Row BNSummaryLine">You have not selected any beers to buy.</div>';
	};
	var DetailsAddressTA=document.getElementById('BNDetailsAddress');
	var DetailsField=DetailsAddressTA.lastChild;
	var DetailsPrompt=DetailsAddressTA.childNodes[1];
	if (Total<FreeDeliveryMinValue) {
		if (DetailsPrompt.innerHTML.indexOf('else')==-1 && DetailsField.value=='') {
			DetailsPrompt.innerHTML='Leave blank if self-collecting at The Drinking Partners.<br/>Free island-wide delivery for orders above S$'+FreeDeliveryMinValue+'.00. Else, a S$20.00 delivery charge applies.';
		};
		if (DetailsField.value!='') {
			BNListing+='<div class="Row BNSummaryLine"><a>&nbsp;</a><span class="Name">Delivery Surcharge</span><span>1 x S$20.00</span><span>S$20.00</span></div>';
			Total+=20;
		};
	} else if (DetailsField.value=='') {
		DetailsPrompt.innerHTML='Entitled to free delivery, so let us have your address and we\'ll bring the beer right to your doorstep!';
	};
	document.getElementById('BuyForm').ordervalue.value=Total;
	document.getElementById('BNList').innerHTML=BNListing;
	document.getElementById('SubTotal').innerHTML=FormatPrice(Total);
/*	var Form=document.getElementById('BuyForm');
	var Total=0;
	for (i=0; i<Form.length; i++) {
	if (Form.elements[i].name.match(/_qty$/) && parseInt(Form.elements[i].value, 10)>0) {
		var parts=Form.elements[i].name.split('_');
		Total+=parseInt(Form.elements[i].value, 10)*parts[1];
	};
	};
	Total=Math.round(Total*100)/100;
	var TotalP=Total.toString().split('.');
	if (TotalP[1]==undefined)
		TotalP[1]='00';
	else if (TotalP[1].length==1)
		TotalP[1]+='0';
	document.getElementById('SubTotal').innerHTML=TotalP[0]+'.'+TotalP[1];
*/
};

function ShowBNScroller() {
	document.getElementById('BNScroller').innerHTML='<a href="#" onclick="return false;" onmouseover="SideScrollDIV(\'BNDisplay\', -1);" onmouseout="SideScrollCancel();" onmousedown="SideScrollFactor=2;" onmouseup="SideScrollFactor=1;">&#9650;</a><a href="#" onclick="return false;" id="Bottom" onmouseover="SideScrollDIV(\'BNDisplay\', 1);" onmouseout="SideScrollCancel();" onmousedown="SideScrollFactor=2;" onmouseup="SideScrollFactor=1;">&#9660;</a>';
};
function HideBNScroller() {
	document.getElementById('BNScroller').innerHTML='';
};
var SideScrollTimer=false; var SideScrollFactor=1;
function SideScrollDIV(DIVID, LeftRight, Pixels, Timeout) {
	var DIV=document.getElementById(DIVID);
	if (Pixels==undefined) {
		Pixels=10;
	};
	if (Timeout==undefined) {
		Timeout=50;
	};
	if (LeftRight==-1) { // left/up
		if (DIV.scrollTop<=Pixels*SideScrollFactor) {
			DIV.scrollTop=0;
		} else {
			DIV.scrollTop-=Pixels*SideScrollFactor;
			SideScrollTimer=setTimeout('SideScrollDIV("'+DIVID+'", '+LeftRight+', '+Pixels+', '+Timeout+');', Timeout);
		};
	} else { // right/down
		var CurrentScroll=DIV.scrollTop;
		DIV.scrollTop+=Pixels*SideScrollFactor;
		if (CurrentScroll!=DIV.scrollTop) {
			SideScrollTimer=setTimeout('SideScrollDIV("'+DIVID+'", '+LeftRight+', '+Pixels+', '+Timeout+');', Timeout);
		};
	};
};
function SideScrollCancel() {
	clearTimeout(SideScrollTimer);
	SideScrollTimer=false;
	SideScrollFactor=1;
};

var BNBeers=new Array();
var StdBottlesN=24; var BottlesN=24;
var CurrentCat=-1;
var QtyReduce='<a href="#" onclick="ReduceQty(this.nextSibling);return false;">&nbsp;&nbsp;&#8722;&nbsp;&nbsp;</a>';
var QtyAdd='<a href="#" onclick="AddQty(this.previousSibling);return false;">&nbsp;&nbsp;+&nbsp;&nbsp;</a>';
function ShowBNCat(Cat) {
	if (CurrentCat==Cat)
		return;
	if (CurrentCat!=-1) {
		document.getElementById('Cat'+CurrentCat).className='';
	};
	var PriceFactor=BottlesN/StdBottlesN;
	document.getElementById('Cat'+Cat).className='Active';
	CurrentCat=Cat;
	var i; var Beers='';
	for (i in BNBeers[Cat]) {
		var VOLs=new Array(); var Prices=new Array();
		if (BNBeers[Cat][i]['ml'].indexOf(',')!=-1) {
			VOLs=BNBeers[Cat][i]['ml'].split(',');
			Prices=BNBeers[Cat][i]['price'].split(',');
		} else {
			VOLs[0]=BNBeers[Cat][i]['ml'];
			Prices[0]=BNBeers[Cat][i]['price'];
		};
		var i2;
		for (i2=0; i2<VOLs.length; i2++) {
			if (parseFloat(Prices[i2])>0) {
				var ThisCaseQty=BNBeers[Cat][i]['casesize']*PriceFactor;
				Prices[i2]*=PriceFactor;
				var IdvPrice=Math.round(parseFloat(Prices[i2])/BNBeers[Cat][i]['casesize']*100)/100;
/*				if (IdvPrice.toString().indexOf('.')==-1) {
					IdvPrice=IdvPrice.toString()+'.00';
				} else {
					var DecPlaces=IdvPrice.toString().length-IdvPrice.toString().indexOf('.')-1;
					if (DecPlaces==1) {
						IdvPrice=IdvPrice.toString()+'0';
					};
				};*/
				var Name=BNBeers[Cat][i]['type']+'_'+BNBeers[Cat][i]['idx']+'_'+VOLs[i2];
				Beers+='<div class="BNBeer"><img src="/'+BNBeers[Cat][i]['img']+'" class="'+BNBeers[Cat][i]['type']+'" title="'+BNBeers[Cat][i]['name']+'" /><b>'+BNBeers[Cat][i]['name']+'</b><br/>';
				if (BNBeers[Cat][i]['type']!='voucher') {
					Beers+='<span>'+VOLs[i2]+'ml &ndash; '+BNBeers[Cat][i]['abv']+'% ABV<br/>S$'+FormatPrice(Prices[i2])+' ('+ThisCaseQty+' x S$'+FormatPrice(IdvPrice)+')</span>';
				} else {
					Beers+='S$'+FormatPrice(Prices[i2]);
				};
				Beers+='<br/><div class="qtyselector">'+QtyReduce+'<input type="text" class="text small" name="'+Name+'" onkeyup="CheckInputNumbersOnly(this);" onchange="AddQty(this, 0);" onblur="UpdateSubTotal();" value="'+(BNChosen['cat'+Cat]==undefined || BNChosen['cat'+Cat][Name]==undefined?0:BNChosen['cat'+Cat][Name])+'" onfocus="this.select();" />'+QtyAdd+'</div></div>';
			};
		};
	};
	document.getElementById('BNBeers').lastChild.innerHTML=Beers;
	if (document.getElementById('BNBeers').lastChild.scrollHeight>document.getElementById('BNBeers').lastChild.clientHeight) {
		ShowBNScroller();
	} else {
		HideBNScroller();
	};
	document.getElementById('BNDisplay').scrollTop=0;
};

