/*********************************************************************
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.oak-barrel.com/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();
}

//--------------------------

function refreshOptions() { //needed if refresh page
  var i, radioValue, selObj, resObj;
  for (i = 0; i < document.itemsform.length; i++) {
    radioValue=document.itemsform[i].value
    if (document.itemsform[i].checked==true) {
	if (document.getElementById("OUTER_"+radioValue)) {
		selObj=document.getElementById("OUTER_"+radioValue)
		selObj.className = 'selected-outer';
	}
    } else {
	if (document.getElementById("OUTER_"+radioValue)) {
		resObj=document.getElementById("OUTER_"+radioValue)
		resObj.className = 'clear-outer';
	}
    }
  }
}

//--------------------------

function changeSelection(group,opt) {
  var i, radioValue, selObj, resObj;
  for (i = 0; i < document.itemsform[group].length; i++) {
    radioValue=document.itemsform[group][i].value;
    if (radioValue==opt) {
      document.itemsform[group][i].checked=true;
	if (document.getElementById("OUTER_"+opt)) {
		selObj=document.getElementById("OUTER_"+opt);
		selObj.className = 'selected-outer';
	}
    } else {
	if (document.getElementById("OUTER_"+radioValue)) {
		resObj=document.getElementById("OUTER_"+radioValue);
		resObj.className = 'clear-outer';
	}
    }
  }
}


function expandLink(contentSrc,enlrgHTML,fWidth,fHeight) {
	// Netscape 6 - does not like setting style to display:none and back to display block
	var eny, yScroll, enx, itopY, itopX;
	// Get screen height, viewport height;
	if (typeof(window.innerHeight) == 'number') {
		eny = window.innerHeight;
		yScroll = window.pageYOffset;
		enx = window.innerWidth;
	} else {
		if (typeof(document.body.clientHeight) == 'number') {
			eny = document.body.clientHeight;
			yScroll = document.body.scrollTop;;
			enx = document.body.clientWidth;
		} else {
			if (typeof(document.documentElement.clientHeight) == 'number') {
				eny = document.documentElement.clientHeight;
				yScroll = document.documentElement.scrollTop;
				enx = document.documentElement.clientWidth;
			} else {
					enx=0;
					eny=0;
					yScroll=0;
				}
		}
	}
	itopY = Math.floor((eny-fHeight)/2) + yScroll - 20;
	if (itopY<0) itopY=0;
	itopX = Math.floor((enx-fWidth)/2) -10;
	if (itopX<0) itopX=0;

	if ((document.getElementById('enlrg'))    ) {
		enDivObj=document.getElementById('enlrg')
		enDivObj.style.display = 'block';
		enDivObj.innerHTML = enlrgHTML;
		if (document.getElementById('enlrgIF')) {
			enIFObj=document.getElementById('enlrgIF')
			enDivObj.style.left = itopX;
			enDivObj.style.top = itopY;
			enDivObj.style.cursor='hand';
			enDivObj.style.display = 'block';
			enDivObj.onclick = function() {
				enDivObj.innerHTML = ""
			};
			enIFObj.style.width = fWidth+'px';
			enIFObj.style.height = fHeight+'px';
			enIFObj.style.position = 'relative';
			enIFObj.style.marginLeft = '10px';
			enIFObj.style.marginRight = '10px';
			enIFObj.style.display = 'block';
			enIFObj.src = contentSrc;
		}
	}
}


function viewIFrm(popUpSrc,fWidth,fHeight) {
	var enlrgHTML;
	enlrgHTML='<FONT SIZE="2">close</FONT><iframe src="../images/dot.gif" width="0" height="0" id="enlrgIF"></iframe><FONT SIZE="2">close</FONT>';
	expandLink(popUpSrc+'/index.html',enlrgHTML,fWidth,fHeight);
}


function viewImg(imgSrc,imgWidth,imgHeight) {
	var enlrgHTML;
	enlrgHTML='<FONT SIZE="2">close</FONT><img src="'+imgSrc+'" width="'+imgWidth+'" height="'+imgHeight+'" alt="expanded image" id="enlrgIF"><FONT SIZE="2">close</FONT>';
	expandLink(imgSrc,enlrgHTML,imgWidth,imgHeight);
}
