/////////////////////////////////////////////////////////////////////////////////////////
////  ==============================================
////  E-Shopping Cart    byTom Beesley                      Version 1.2	         
////  All rights reserved.  Copyright 2001 Tom Beesley.		          
////  email info@chuchudesigns.co.uk				          
////  tel +44 (0)1670 788955					          
////  http://www.chuchudesigns.co.uk				         
////  ==============================================
////  cardpay_WorldPay  Version 1.0				          
////  ==============================================
/////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////                   Copyright             //////////////////////////
//======================================
// IMPORTANT:
//This Shopping Cart may not be copied or redistributed for any purposes 
//without prior written permission from Tom Beesley. Any inquiries regarding
//this may be sent to info@chuchudesigns.co.uk
//Permission given to use the script provided that this notice remains as is.
//
// CONFIGURATION:
//Please read the ReadMe file that accompanies this file for details of
//configuring this shopping cart
//======================================


//////////////////////////               Global Variables          /////////////////////////
//======================================
  var totalcount //counts number of items in shopping basket
  var i; //general counter
  var bolFound //boolean value for comparing
  var strProduct //element holding product - in this case I use the first box (four elements so can be found by i x 4 - 4)
  var baskettotal = getCookie("totalbasket");
  var sparecookie = getCookie("sparecookie");
  var browser //detects browser used
  i = 0;
  bolFound = false;
  if (sparecookie == null) sparecookie = "1";
  if (baskettotal == null) baskettotal = "0";
//======================================


//////////////////////////                   Functions             //////////////////////////
//======================================

if (parseInt(navigator.appVersion) >= 5 || navigator.appVersion.indexOf("MSIE 5") != -1) {
	browser="DOM";
} else if (navigator.userAgent.indexOf("Mozilla/4") != -1){
	if (navigator.appName.indexOf("Netscape") != -1)
		browser="NS4";
	if (navigator.appVersion.indexOf("MSIE 4") != -1)
		browser="IE4";
} else browser="Other";

//////////////////////////////
/////  open new windows in center of screen
//////////////////////////////
function newwindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//////////////////////////////
/////  view all cookies
/////  testing purposes only!
//////////////////////////////
function allcookies(){
newwindow("help.asp","Display","350","300","no");
}

//////////////////////////////
/////  view shopping cart  
//////////////////////////////
function viewcart(){
name="homewindow";
newwindow("Index.asp","Display","580","480","yes");
}

//////////////////////////////
/////  go to checkout 
//////////////////////////////
function gotocheckout(){
newwindow("Checkout.asp","Display","580","480","yes");
}

//////////////////////////////
/////   help  
//////////////////////////////
function viewhelp(){
window.alert('1. Choose the product you wish to purchase.\n2. Enter the number of items you require.\n3. Click "Add to Basket".\n\nContinue to add items from the online store, then \nclick on "Check Out" to proceed to the check out \npages.\n\nYou can view the contents of your shopping \nbasket at any time by clicking on "View Basket".\n\nPowered by E-Shopping Cart.\n                http://www.easil-e.com');
}

//////////////////////////////
/////   empty basket   
//////////////////////////////
function emptybasket(){
	if( confirm("Are you sure you want to empty your basket?") == true) {
		i = 1;
		for (i=1; i<16; i++) {
			deletecookie = "BasketItem" + i;
			document.cookie=deletecookie+'='+"" + ';'
		}
		document.cookie='totalbasket'+'='+"" + ';'
		document.cookie='sparecookie'+'='+"" + ';'
		window.location='Index.asp';
		return true;
	}
	else{
		return false;
	}
}

//////////////////////////////
/////   delete item  
//////////////////////////////
function deleteitem(IDtoDelete){
if (IDtoDelete == ""){
	window.alert('Please select an item to delete');
	return false;
}
else{
	if( confirm("Are you sure you want to delete this item?") == true) {
		document.cookie='BasketItem'+IDtoDelete+'='+''+';' 
		totalcount=getCookie('totalbasket');	
		totalcount = (totalcount - 1)
		document.cookie= "totalbasket" + '=' + totalcount;
		window.location='Index.asp';
		return true;
	}
	else{
	}
}
}

//////////////////////////////
/////  add product to basket
//linenumber is used for updating value 
//in this case I use frmDisplayProducts as the form name and
//the formula linenumber * 3 - 2 due to the buttons, this needs changing
//if the site uses a different form, or buttons appear in a different
//order (buttons are part of the form.element[i]
/////////////////////////////////////////////////////////////////////

function AddtoBasket(curPrice,intQuantity,linenumber,itemsinline,posquantity,curPostal,intPG){
	tempquantity = parseInt(intQuantity);
	thiselement = parseInt(linenumber) * parseInt(itemsinline);
	thiselement = parseInt(thiselement) - parseInt(itemsinline);
	strProduct = document.frmDisplayProducts.elements[thiselement].value;
	thiselement = thiselement + parseInt(posquantity);
	document.frmDisplayProducts.elements[thiselement].value = "";
	if (intQuantity == "" || isNaN(parseFloat(tempquantity)) || intQuantity == "0"){
		window.alert('Invalid Quantity.\n\nPlease re-enter using a number greater than zero.');
		return false;
	}
	else{
	}
	baskettotal ++;
	if (baskettotal <= 15){
		findduplicate(strProduct,curPrice,intQuantity,curPostal,intPG);
	}
	else{
		window.alert('Your Basket is full\n\nPlease visit the checkout, before adding any more items');
		return false;
	}
}

//////////////////////////////
/////  update cookie with stock Item
//////////////////////////////
function updatebasket(strProduct,curPrice,intQuantity,curPostal,intPG){
	document.cookie='BasketItem'+sparecookie+'=^'+strProduct+ '^'+curPrice+'^'+intQuantity+'^'+curPostal+'^'+intPG+';' 
	document.cookie= "totalbasket" + '=' + baskettotal;
	findnextavailable();	
	window.alert('The following item has been added to your basket:\n\n'+strProduct+'\n\nQuantity: '+intQuantity);
	window.location.reload();
}

//////////////////////////////
/////  left function
//////////////////////////////
function Left(str, n){
	if (n <= 0)    
	         return "";
	else if (n > String(str).length)  
	         return str;           
	else 
	         return String(str).substring(0,n);
	}

//////////////////////////////
/////  find duplicates
//////////////////////////////
function findduplicate(strProduct,curPrice,intQuantity,curPostal,intPG){
	i = 1;
	strProducta = "^"+strProduct;
	ken = strProducta.length
	while (i <= 16){
		if (i <= 15) {
			testcookie = "BasketItem" + i;
			boris = getCookie(testcookie);
			if (Left(boris,ken) == strProducta){
				if( confirm("This product already exists in your shopping cart.\n\nDo you want to add it again?") == true) {					
					updatebasket(strProduct,curPrice,intQuantity,curPostal,intPG);
					break;
				}
				else{
					return false;
					break;
				}
			}
			else{
				i++
			}
		}
		else{
			updatebasket(strProduct,curPrice,intQuantity,curPostal,intPG);
			break;
		}
	}
}

//////////////////////////////
/////  find next available cookie
//////////////////////////////
function findnextavailable(){
	i = 1;
	while (i < 16){
		testcookie = "BasketItem" + i;
		kitten = getCookie(testcookie);	
		if (kitten == null){
			document.cookie='sparecookie'+'='+ i + ';'
			break;
		}
		else{
			i++
		}
	}
}

//////////////////////////////
/////  find cookie value
//////////////////////////////
function getCookie (name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return null;
    }
//======================================


