function fixDecimals (number,decimals) {

	decimals = (!decimals ? 2 : decimals);
	
	return Math.round(number*Math.pow(10,decimals))/Math.pow(10,decimals);
 
}

function returnCurrencyValue(unitprice, units) {

	units = ((units)? units : 1);
	thisVal = unitprice * units;
	thisVal = fixDecimals(thisVal, '2');

	// Fix "too few" decimals
	thisVal += "";
	 
	if (thisVal.indexOf(".") != -1) {
		if ((thisVal.length - (thisVal.indexOf(".") + 1)) < 2) {
			thisVal += "0";
		}
	} else {
		thisVal += ".00";
	}

	return thisVal;
}

function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;	
	this.ie4=(document.all && !this.dom)?1:0;
	this.ie=(this.ie4 || this.ie5 || this.ie6);
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}

bw=new checkBrowser();

function checkMandatories() {

if (document.form.mandatory) {
	var mandList = document.form.mandatory.value.split(".");

	for (i=0; i < mandList.length; i++) {
		thisCheck = mandList[i].split(":")[0];	
		thisMsg = mandList[i].split(":")[1];
		
		if(thisCheck) {
			thisOpt = eval('document.form.' + thisCheck + '.value');
			if(thisOpt == "") {
				alert(thisMsg);		
				return "0";
			}
		}
	}
}

return "1";
}

function addToBasket (url,code, title, price, pe) {

	if (pe == null) { pe = false; } 

    var value = 0;
	
	var chk = checkMandatories();
	if(chk == 0) {
		return;
	}

	var orderLine = "OL" + getNextBasketLine();


	var qty = document.form.quantity.value;
	price *= 1;
	price = price + checkOptions(price);

	value = qty * price;

	price = returnCurrencyValue(price, '1');
	value = returnCurrencyValue(value, '1');
    	
	if (Math.floor(qty)) {
	insertString = Math.floor(qty) + "-" + code + "-" + title + "-" + price + "-" + value + "-" + pe + "-" + getOptions();

	if(getCookie('basket')) {
    
	updateCookieChip('basket', orderLine, insertString);
	
	} else {
	
	setNewChipCookie('basket', orderLine, insertString);
	
	}

    
	  basketString = "Your Basket:";
     if(bw.ie) {	
	
		document.all.basketSummary.innerHTML = getBasketSummary(basketString,'<br>&nbsp;&nbsp;');
     } 

	} else {
	
	alert('Quantity should be a whole number');
	
	}
	
	
	viewBasketPopUp(url);
	basketWin.focus();
	
	return;

}


function getBasketSummary (headerText, sep) {

	var suffix = '';
	numBasketItems = 0;
	basketTotal = parseInt('0');
	

	numBasketItems = getBasketNumItems();
	basketTotal = returnCurrencyValue(getBasketValue());

    itemString = "Items:";
	valueString = "Value: &pound;"

	rtnMsg = headerText + sep + itemString + numBasketItems;
	rtnMsg += sep + valueString + basketTotal;
	return rtnMsg;
   
}

function getFrBasketSummary (headerText, sep) {

	var suffix = '';
	numBasketItems = 0;
	basketTotal = parseInt('0');
	

	numBasketItems = getBasketNumItems();
	basketTotal = returnCurrencyValue(getBasketValue());

	rtnMsg = headerText + sep + 'Articles: ' + numBasketItems;
	rtnMsg += sep + 'Prix: ' + basketTotal + ' &euro;';
	return rtnMsg;

}



function getBasketNumItems () {
 //
numBasketItems = 0;

	
	if(getCookie('basket')) {
	var basketArray = multiValueChipReturn('basket');

		var basketLines = basketArray.length;
		for (i = 0; i < basketArray.length; i++) {

			thisChip = basketArray[i].split("=")[1];
			thisQuant = thisChip.split("-")[0];
			numThisItem = basketArray[i].split("-")[0]
			numBasketItems += parseInt(numThisItem.split("=")[1]);

		}
	}

return numBasketItems
}

function getNextBasketLine () {

var numBasketLines = 0;

	
	if(getCookie('basket')) {
	

		var basketArray = multiValueChipReturn('basket');
		var basketLines = basketArray.length;

		for (i = 0; i < basketArray.length; i++) {

			thisLine = basketArray[i].split("=")[0];
			thisLineNum = thisLine.split("L")[1];
		
			if (thisLineNum > 0) {
				numBasketLines = thisLineNum;
			}
		}
	}
	
	numBasketLines++;

return numBasketLines;
}

function getBasketValue () {


	basketTotal = parseInt('0');
	
	if(getCookie('basket')) {
	var basketArray = multiValueChipReturn('basket');

		var basketLines = basketArray.length;
		for (i = 0; i < basketArray.length; i++) {

			thisLine = basketArray[i];
			thisChip = thisLine.split("=")[1];
			if(thisLine.indexOf("OL99") > -1) {
				thisVal = 0;
			} else {
				thisVal = fixDecimals(basketArray[i].split("-")[4]);
			}
			basketTotal += thisVal;
			
		}
	}


return basketTotal;

}


function getOptions(optList) {

	var optList = ""
	if(document.form.options) {
	optionList = document.form.options.value.split(" ");

	for (i=0;i < optionList.length; i++) {
		if (optionList[i]) {

				thisVal = eval('document.form.' + optionList[i] + '.value');

				if (thisVal != "") {
						optList += optionList[i] + "~" + thisVal + "~";
				}
					
		}
				
	}
	return optList
	} else {
		return "";
	}
}
	

function getCurrentBasket () {
	return getCookie('basket');
}


function emptyBasket () {

	deleteCookie('basket');
	
	
	if (rewriteOpener()) {
	    
	      opener.document.all.basketSummary.innerHTML = getBasketSummary('Your Basket:','<BR>&nbsp;&nbsp;');
	     }
	
	refreshBasket();
	return;
	
}






function rewriteOpener() {
if(document.form && bw.ie) {
	return (document.form.thisDoc.value == "viewbasketpopup" ? 1 : 0);
} else {
	return 0
}
}


function refreshBasket() {

	document.location.href = window.location;

}

function updateBasket() {


	var basketArray = multiValueChipReturn('basket');


	for ($i=0;$i<basketArray.length;$i++) {
	
		chipName = basketArray[$i].split("=")[0];

		chipValue = basketArray[$i].split("=")[1];
		
		thisCode = chipValue.split("-")[1];
		thisTitle = chipValue.split("-")[2];
		thisPrice = chipValue.split("-")[3];
		thisOptions = chipValue.split("-")[5];
		
		thisQuantity = eval('document.form.' + chipName + 'quantity.value');
		thisDeleteBox = eval('document.form.' + chipName + 'delete.checked');

		if ((thisQuantity && thisQuantity == 0) || thisDeleteBox) {
					
		deleteCookieChip('basket', chipName);	
		refreshBasket();	
		
		} else if (thisQuantity && Math.floor(thisQuantity)) {

		thisValue = Math.floor(thisQuantity) * thisPrice;
		thisValue = fixDecimals(thisValue);
		
		thisInsertString = Math.floor(thisQuantity) + '-' + thisCode + '-' + thisTitle + '-' + thisPrice + '-' + thisValue + "-" + thisOptions;

		updateCookieChip('basket', chipName, thisInsertString);

	
			

		} else {

		alert('Quantity must be a number');

		}
	


	}
	

	refreshBasket();

	 if (rewriteOpener()) {
	 	opener.document.all.basketSummary.innerHTML = getBasketSummary('Your Basket:','<br>&nbsp;&nbsp;');
	 } else {
	 	alert('reWriteOpener returned false');
	}
}


function viewBasketPopUp(url) {

	basketWinPopUpURL = "/vsite/vproduct/page/viewbasketpopup/1,11011,5061-194930-212153,00.html";
	//basketWin = window.open(basketWinPopUpURL,'ViewBasket','width=600,height=400,status=yes,location=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,directories=yes');
	basketWin = window.open(basketWinPopUpURL,'ViewBasket','width=600,height=550,resizable=yes,status=yes,location=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,directories=yes');
	basketWin.focus();
	
}

function goCheckOut() {


	var basket = getCookie('basket');
	
	if (!basket)
	{	 alert("You have no items in your basket");
		return
	}
	
	 
	
	checkOutURL = "https://www.sportcentric.com/vsite/vproductsite/page/checkout/1,11012,5061-194930-212153-checkout-front-start,00.html?basket=" + basket + ";";
	
	 checkoutWin = window.open(checkOutURL,'CheckOut','width=600,height=400,location=yes,menubar=yes,toolbar=yes,resizable=yes,status=yes,scrollbars=yes');
	checkoutWin.focus();
	
	

}

