﻿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 = "";
orderRef = "";
items = 0;

product = new Array ("Powrtouch PowrActuator, self fit",
"Powrtouch PowrActuator, fitted",
"Powrtouch Compact Single, manual, self fit",
"Powrtouch Compact Single, Electronic, self fit",
"Powrtouch Compact Single, manual, fitted",
"Powrtouch Compact Single, Electronic, fitted",
"Powrtouch Heavy Duty Single, manual, self fit",
"Powrtouch Heavy Duty Single, Electronic, self fit",
"Powrtouch Heavy Duty Single, manual, fitted",
"Powrtouch Heavy Duty Single, Electronic, fitted",
"Powrtouch Twin, manual, self fit",
"Powrtouch Twin, Electronic, self fit",
"Powrtouch Twin, manual, fitted",
"Powrtouch Twin, Electronic, fitted",
"Powrtouch All Wheel Drive Twin, manual, self fit",
"Powrtouch All Wheel Drive Twin, Electronic, self fit",
"Powrtouch All Wheel Drive Twin, manual, fitted",
"Powrtouch All Wheel Drive Twin, Electronic, fitted",
"Powrtouch Evolution Auto, self fit",
"Powrtouch Evolution Auto, self fit",
"Powrtouch Evolution Auto, fitted",
"Powrtouch Evolution Auto, fitted")

pCount = product.length

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

price = new Array(17500,	//PowrActuator
25000,
69900,	// Compact Single
89500,
79900,
99500,
79900,	// Heavy Duty Single
99500,
84500,
109500,
109900,	// Twin
129500,
119900,
139500,
149500,	//AWD
187500,
169600,
207500,
99900,	//Evolution
99900,
105000,
105000
)


shipping = 1999;
shipBand = new Array(0,1999,1995,995)

fitting = new Array(1,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0)	

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]
	}
	if(!rememberme) {
		rememberme = "no"
	}
}
orderRef = cookieVal("orderRef")

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 = new Array (1,0,1,1,1,2,2) */
/* quantity[0] = 1; /* TEST DATA ONLY */

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 addItem(i,count) {
	if(re2.test(count) != true) {
		alert("Please enter only numbers");
		window.location.reload();
		return false;
	}
	if(parseInt(count) > 9){
		alert("Please call for quote on quantities of 10 and over")
		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();
}



