/*********************************************************************
Author: Phil Scott
File Name: shop_functions.js
Project: WFOL
Purpose: global shopping functions
*********************************************************************/

var the_date = new Date(); // for url args added Jan 05
the_date.setMonth(the_date.getMonth() + 2);
var expiry_date=the_date.toGMTString();
var adDescriptor=location.search.substring(1);
 if (adDescriptor != "") {
   document.cookie='padvertiser='+adDescriptor+';expires='+expiry_date+';path=/';
   document.cookie='sadvertiser='+adDescriptor+';path=/';
 } 

wfolcopy="no";
winstat='Free mainland UK delivery on orders over £50';
if (window.focus) self.focus();
var fbegin=document.cookie.indexOf("wfolcopy=");
if (fbegin>-1) wfolcopy="yes";

function mouseDown(e) {
 var mouseClick=1;
 if (parseInt(navigator.appVersion)>3) {
	if (navigator.appName=="Netscape") mouseClick=e.which;
	else mouseClick=event.button;
 }
 if ((mouseClick!=1) && (wfolcopy=="no")) {
 alert('Please note that these pages are WFOL property and are protectd by copywright law.');
 wfolcopy="yes";
 document.cookie="wfolcopy="+escape('yes')+"; path=/";
 }
}

if (parseInt(navigator.appVersion)>3) {
 document.onmousedown = mouseDown;
 if (navigator.appName=="Netscape") document.captureEvents(Event.MOUSEDOWN);
}


//-------------------------------------------------------------------------------------

function purchase(newItem,newPrice,newCode,newSpecial,newQuantity) {

 if (newPrice<-1000.00) {
	alert("Out Of Stock: The item you have selected or a component of that item is currently out of stock.");
 } else {

 if(newQuantity<=0) {

 rc = alert('Invalid quantity entered');

 } else {

	if (confirm('Add '+newQuantity+' x '+newItem+' to basket? ')) {

		index=document.cookie.indexOf('ShoppingBasket');

		if (index>-1) {

			countbegin=(document.cookie.indexOf('=',index)+1);

			countend=document.cookie.indexOf(';',index);

			if(countend==-1) countend=document.cookie.length;

			datalist = document.cookie.substring(countbegin,countend);


 		} else datalist='';


 		changed = false;

 		newItemList=''; itemlist=0;

 		for (var i=0;i<=datalist.length;i++) {

			if (datalist.substring(i,i+1) == '[') {

				thisfield=1;

				itemstart=i+1;

				fullstart=i+1;

				theCode=0;

				special=0;

			} else if (datalist.substring(i,i+1) == ']') {

				itemend=i;

				thequantity=datalist.substring(itemstart,itemend);

				itemlist++;

				if (theItem==newItem ) {

					changed=true;

					tempquantity=eval(thequantity)+eval(newQuantity);

					newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theCode+'|'+special+'|'+tempquantity+']';

				} else {

					newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theCode+'|'+special+'|'+thequantity+']';

				}

			} else if (datalist.substring(i,i+1)=='|') {


				if (thisfield==1) theItem=datalist.substring(itemstart,i);

				if (thisfield==2) thePrice=datalist.substring(itemstart,i);

				if (thisfield==3) theCode=datalist.substring(itemstart,i);

				if (thisfield==4) special=datalist.substring(itemstart,i);

				thisfield++;itemstart=i+1;

			}

 		}

 		if (changed==false) newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+newCode+'|'+newSpecial+'|'+newQuantity+']';

 		index = document.cookie.indexOf('ShoppingBasket');

 		document.cookie='ShoppingBasket='+newItemList+'; path=/';

 		var childWindow=0;
 		if (window.name!="") {
			if (window.name.length>4) {
				if (window.name.substring(0,4)=='wfol') childWindow=1;
			}
 		}

 		if (childWindow==1) {

			mainWin=window.open("", "mainWindow");

			mainWin.location.href="../../shopping_basket/basket.htm";//level 3 link
		//       was before IE6      mainWin=window.open("../../shopping_basket/basket.htm", "mainWindow");

		 } else top.location="http://www.thebarrelmakers.co.uk/shopping_basket/basket.htm";

	}

 }

 } // newPrice<-1000.00

}

//-------------------------------------------------------------------------------------

function poundstring(MoneyValue) {

 if(MoneyValue<=0.9999) {

	Pounds='0';

 } else {

	Pounds=parseInt(MoneyValue);

 }

 if (MoneyValue>0) {

	MoneyValue=MoneyValue*1.0;

	Pence=Math.round((MoneyValue+.000008 - Pounds)*100);

	if (Pence==100) {
		Pounds=Pounds+1;
		Pence=0;
	}

 } else {

	Pence=0;

 }

 if (eval(Pence) <= 9) Pence='0'+Pence;

 newString='£' + Pounds + '.' + Pence + '';

 return (newString);

}

//-------------------------------------------------------------------------------------
function openWinUp(folder,dirName,winWidth,winHeight,withscroll) {
 if (withscroll==0) {
	myWin=window.open("../"+folder+"/"+dirName+"/index.html",dirName, "width="+winWidth+",height="+winHeight+",status=no,toolbar=no,menubar=no,scrollbars=no");
 } else myWin=window.open("../"+folder+"/"+dirName+"/index.html",dirName, "width="+winWidth+",height="+winHeight+",status=no,toolbar=no,menubar=no,scrollbars=yes");
}

function openWin(dirName,winWidth,winHeight,withscroll) {
 if (withscroll==0) {
	myWin=window.open(dirName+"/index.html",dirName, "width="+winWidth+",height="+winHeight+",status=no,toolbar=no,menubar=no,scrollbars=no");
 } else myWin=window.open(dirName+"/index.html",dirName, "width="+winWidth+",height="+winHeight+",status=no,toolbar=no,menubar=no,scrollbars=yes");
}
//-------------------------------------------------------------------------------------
function closeIt() {
 close();
}