﻿logIn = 0
expireDate = new Date
expireDate.setFullYear(expireDate.getFullYear()+2)
quantityMeta = ""
realname = ""
telephone = ""
email = ""
address1 = ""
address2 = ""
address3 = ""
postcode = ""
country = "UK"
rememberme = "yes";
total = 0;
cook = "";
items = 0;

product = new Array ("Jarvis of St James Premier Cru Brut, single",
"Jarvis of St James Premier Cru Brut, case of 6",
"Jarvis of St James Premier Cru Brut, Gift Set",
"Jarvis of St James Premier Cru Ros&eacute;, single",
"Jarvis of St James Premier Cru Ros&eacute;, case of 6",
"Jarvis of St James Premier Cru Ros&eacute;, Gift Set",
"Jarvis of St James Vintage Premier Cru 2001, single",
"Jarvis of St James Vintage Premier Cru 2001, case of 6",
"Jarvis of St James Vintage Premier Cru 2001, Gift Set",
"Forget Brimont Gold Label Grand Cru Special Reserve, single",
"Forget Brimont Gold Label Grand Cru Special Reserve, case of 6",
"Forget Brimont Gold Label Grand Cru Special Reserve, Gift Set",
"Silver Plated Bottle Stopper",
"Box of Champagne Truffles")

productS = new Array ("Premier Cru Brut, single",
"Premier Cru Brut, case of 6",
"Premier Cru Brut, Gift Set",
"Premier Cru Rose, single",
"Premier Cru Rose, case of 6",
"Premier Cru Rose, Gift Set",
"Vintage Premier Cru, single",
"Vintage Premier Cru, case of 6",
"Vintage Premier Cru, Gift Set",
"Forget Brimont Gold Label, single",
"Forget Brimont Gold Label, case of 6",
"Forget Brimont Gold Label, Gift Set",
"Silver Plated Bottle Stopper",
"Box of Champagne Truffles")
quantity = new Array (0,0,0,0,0,0,0,0,0,0,0,0,0,0)
price = new Array(2100,12100,4000, 2300,13100, 4500, 3000,17800,5500,3500,19500,6000,900, 2500)
pCount = product.length
shipping = 1200;

logIn = cookieVal("logIn")
if(logIn == false) {	/* First Page - clear basket */
	logIn = true
	setQuantity(quantity)
	setLogin(logIn)
} else{					/* Load basket values */
	quantityMeta = cookieVal("quantity")
	for(i=0; i<pCount; i++) {
		quantity[i] = parseInt(quantityMeta.split(",")[i])
	}
}

for( i=0; i<pCount; i++) { items+=quantity[i] }
if (document.cookie != "") {
	if(cook = cookieVal("jofj")) {
		cook = unenc(cook)
		realname = cook.split("^")[0]
		telephone = cook.split("^")[1]
		email = cook.split("^")[2]
		address1 = cook.split("^")[3]
		address2 = cook.split("^")[4]
		address3 = cook.split("^")[5]
		postcode = cook.split("^")[6]
		country = cook.split("^")[7]
		rememberme = cook.split("^")[8]
	}
	if(!rememberme) {
		rememberme = "no"
	}
}

/* date check */
var expDate = new Date; var today = new Date;
expDate.setDate(11); expDate.setMonth(05); expDate.setFullYear(2019);
if(today > expDate) {window.location="http://www.theideasforge.co.uk/holding.html" }
remaining = Math.ceil((expDate-today)/86400000)

/* Functions below */

function cookieVal(cookieName) {
	thisCookie = document.cookie.split("; ")
	for (i=0; i<thisCookie.length; i++) {
       	if (cookieName == thisCookie[i].split("=")[0]) {
        	return thisCookie[i].split("=")[1]
        }
    }
return 0
}

function unenc(enc_str) {
	plain = ""
	if(enc_str != "") {
		for(i = 0; i < enc_str.length; i += 2) {
			meta = parseInt(enc_str.substr(i,[2])) + 23
			num_in = unescape('%' + meta.toString(16))
			plain += num_in
		}
	plain = unescape(plain)
 	}
return(plain)
}

function enc(plain) {
	enc_str = "";
	if(plain == "") {
		alert("No string")
	}
	else {
		pl_str = escape(plain);
		for(i = 0; i < pl_str.length; i++) {
			enc_str += pl_str.charCodeAt(i) - 23;
		}
	}
return(enc_str)
}

function setLogin(logIn) {
	document.cookie="logIn="+logIn+";;path=/"
	return true
}

function setQuantity(quantity) {
	document.cookie="quantity="+quantity+";;path=/"
	return true
}

var re2 = /^\d+$/

function addItem(i,count) {
	if(re2.test(count) != true) {
		alert("Please enter only numbers");
		window.location.reload();
		return false;
	}
	if(parseInt(count) > 99){
		alert("Please call for quote on quantities of 100 and over")
		window.location.reload();
		return false;
	}
	if( (items == 0) && (i == 12)) {
		alert("This item can only be ordered with cases of champagne");
		return false;
	}
	quantity[i] += parseInt(count);
	if(quantity[i] < 0) {
		quantity[i] = 0
	}
	setQuantity(quantity);
	window.location.reload();
	return true;
}
re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

function clearBasket() {
	for(i=0; i<quantity.length; i++){
		quantity[i]=0;
	}
	setQuantity(quantity)
	window.location.reload();
}