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

product = new Array ("Hawthorn Pan Rack 20 inch, 5 Hooks-MATT BLACK",
"Hawthorn Pan Rack 20 inch, 5 Hooks-BEESWAX",
"Hawthorn Pan Rack 30 inch, 7 Hooks-MATT BLACK",
"Hawthorn Pan Rack 30 inch, 7 Hooks-BEESWAX",
"Hawthorn Pan Rack 40 inch, 10 Hooks-MATT BLACK",
"Hawthorn Pan Rack 40 inch, 10 Hooks-BEESWAX",
"Maple Utensil Rack 20 inch, 5 Hooks-MATT BLACK",
"Maple Utensil Rack 20 inch, 5 Hooks-BEESWAX",
"Maple Utensil Rack 30 inch, 7 Hooks-MATT BLACK",
"Maple Utensil Rack 30 inch, 7 Hooks-BEESWAX",
"Maple Utensil Rack 40 inch, 10 Hooks-MATT BLACK",
"Maple Utensil Rack 40 inch, 10 Hooks-BEESWAX",
"Hazel Pan/Utensil Rack, 40 inch, 5 pan hooks, 5 utensil hooks-MATT BLACK",
"Hazel Pan/Utensil Rack, 40 in, 5 pan hooks, 5 utensil hooks-BEESWAX",
"Blackthorn Pan Rail, 25 inch, 5 hooks-MATT BLACK",
"Blackthorn Pan Rail, 25 inch, 5 hooks-BEESWAX",
"Blackthorn Pan Rail, 35 inch, 7 hooks-MATT BLACK",
"Blackthorn Pan Rail, 35 inch, 7 hooks-BEESWAX",
"Blackthorn Pan Rail, 45 inch, 10 hooks-MATT BLACK",
"Blackthorn Pan Rail, 45 inch, 10 hooks-BEESWAX",
"Elder Oval ceiling rack with bars - MATT BLACK+standard chain",
"Elder Oval ceiling rack with bars -BEESWAX+Standard Chain",
"Elder Oval ceiling rack with bars -MATT BLACK+Chain rods",
"Elder Oval ceiling rack with bars -BEESWAX+Chain Rods",
"Holly Oval ceiling rack with lattice - MATT BLACK+standard chain",
"Holly Oval ceiling rack with lattice -BEESWAX+standard chain",
"Holly Oval ceiling rack with lattice -MATT BLACK+chain rods",
"Holly Oval ceiling rack with lattice -BEESWAX+chain rods",
"Chestnut Rectangle ceiling rack with bars- MATT BLACK+standard chain",
"Chestnut Rectangle ceiling rack with bars  -BEESWAX+standard chain",
"Chestnut Rectangle ceiling rack with bars  -MATT BLACK+chain rods",
"Chestnut Rectangle ceiling rack with bars  -BEESWAX+chain rods",
"Oak Rectangle ceiling rack with lattices- MATT BLACK+standard chain",
"Oak Rectangle ceiling rack with lattice  -BEESWAX+standard chain",
"Oak Rectangle ceiling rack with lattice  -MATT BLACK+chain rods",
"Oak Rectangle ceiling rack with lattice  -BEESWAX+chain rods",
"Additional Hook Hawthorn",
"Additional Hook Blackthorn",
"Additional Hook Maple",
"Bracket for Blackthorn Range",
"Side hooks for ceiling rack",
"Under hooks for ceiling rack",
"Under hooks for ceiling rack",
"Standard Chain per meter ",
"Deluxe Chain rods per 4"
)

price = new Array(
3499 ,3499 ,4999 ,4999 ,6499 ,6499 ,  // Pan rack Hawthorn
3499 ,3499 ,4999 ,4999 ,6499 ,6499 ,	// Utensil rack 
6499 ,6499 ,							// Pan utensil rack hazel
3499 ,3499 ,4499 ,4499 ,4999 ,4999 ,	// Pan rail Blackthorn
13999 ,13999 ,17999 ,17999 ,16999 ,16999 ,20999 ,20999 ,	// Oval ceiling racks
13999 ,13999 ,17999 ,17999 ,16999 ,16999 ,20999 ,20999 ,	// Rectangular Ceiling Racks
400 ,400 ,400 ,700 ,400 ,400 ,400 ,600 ,2000 	// Extras
)

shipType = new Array(
1,1,1,1,1,1,
1,1,1,1,1,1,
1,1,
1,1,1,1,1,1,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
0,0,0,0,0,0,0,0,0)

shipCosts = new Array(295,695,1295)
shipUSACosts = new Array(2500,2500,4200)

pCount = product.length
quantity = new Array(pCount)
for(i=0; i<pCount; i++){quantity[i]=0}
shipping = 0;
shippingLow = 295;
shippingUK = 695;
shippingEurope = 2500;
shippingROW = 4000;
shippingUSA = 4200;

if (document.cookie != "") {
	if(cook = cookieVal("ki")) {
		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]
		alert(realname, telephone, email, address1)
	}
	if(!rememberme) {
		rememberme = "no"
	}
}

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])
		if(isNaN(quantity[i])){quantity[i]=0}
	}
	if(cookieVal("region")) {
		region = cookieVal("region")
	}
}
/* quantity = (1,0,1,1,1,2,2) /* THIS IS TEST DATA ? */

for( i=0; i<pCount; i++) { items+=quantity[i] }


/* 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
}

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

var re2 = /^\d+$/


function addItem2(i,count,size,finish, p3) {
//	alert(size+" "+finish+" "+p3)
	if(re2.test(count) != true) {
		alert("Please enter only numbers");
		window.location.reload();
		return false;
	}
	i += parseInt(size) + parseInt(finish) + parseInt(p3);
	alert("Adding "+product[i])
	quantity[i] += parseInt(count);
	if(quantity[i] < 0) {
		quantity[i] = 0
	}
	setQuantity(quantity);
//	window.location.reload();
	window.location = "../basket.html";
	return true;
}

function addItem(i,count) {
	if(re2.test(count) != true) {
		alert("Please enter only numbers");
		window.location.reload();
		return false;
	}
	quantity[i] += parseInt(count);
	if(quantity[i] < 0) {
		quantity[i] = 0
	}
	setQuantity(quantity);
//	window.location.reload();
	window.location = "../basket.html";
	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();

}

function clearItem(i) {
	quantity[i]=0;
	setQuantity(quantity);
	window.location.reload();
}
function setShipping() {
	if(region == "UK") {
		shipping = shippingUK
		if(total >= shipFree) {
			shipping=0
		}
	} else if (region == "Europe") {
		shipping = shippingEurope
	} else if (region == "USA") {
		shipping = shippingUSA
	} else if (region == "ROW") {
		shipping = shippingROW
	}
/*	alert("Shipping Set") */
}
function fill(param1) {
	itemPrice = param1;
	document.orderForm.itemPrice.value = "£"+itemPrice/100;
	}

function calcPrice(item) {
	fill(   price[item + parseInt(document.orderForm.length.value)+parseInt(document.orderForm.finish2.value) + parseInt(document.orderForm.inFill.value)])
}

function noBees() {
	
	if (document.orderForm.length.value == 0) {
		document.orderForm.finish2.value = 0;
		alert("Please note: Beeswax finish not avalailable with standard chains")
	}
}


