cartAr=new Array();
cartAr[0]=new Array(5);
cartAr[0][0]="empty";
cartAr[0][1]="empty";
cartAr[0][2]="empty";
cartAr[0][3]=0;
cartAr[0][4]=0;

paypalemail="";
nochexemail="";
orderemail="info@johnmenage.co.uk";
actionurlemail="http://www.johnmenage.co.uk/cgi-bin/cgiemail/orderemailtemplate.txt";

function emptytcart(){
setCookie("cart","blank");
deleteCookie("cart");
for (x=0;x<cartAr.length;x++){
cartAr[x][3]=0;
}
updateCart();
}

function parseCookie(){

thash="##";
tpipe="|";
tcomma="^";
var tmpparse = getCookie("cart");

if (!tmpparse || tmpparse=="blank"){
//alert("no cookie");
}else{
tmpmanip=tmpparse.split(thash);
yyy=tmpmanip[0];
//alert(yyy);
cartRegenerateA=yyy.split(tpipe);
//alert((cartRegenerateA.toString()));
for(ii=1;ii<cartRegenerateA.length;ii++){
ttt=cartRegenerateA[(ii-1)];
cartRegenerateB=ttt.split(tcomma);
//alert((cartRegenerateB.toString()));
cartAr[ii]=new Array(5);
cartAr[ii][0]=cartRegenerateB[0];
cartAr[ii][1]=cartRegenerateB[1];
cartAr[ii][2]=cartRegenerateB[2];
cartAr[ii][3]=parseInt(cartRegenerateB[3]);
cartAr[ii][4]=parseInt(cartRegenerateB[4]);
//alert(cartAr[ii][3]);
}
}

}// end func

function updateCart(){

tmptotalq=0;
tmptotal=0;
tmpnames="";
tmpdetails="List of Print/s ";
tmpnamedetails="List of Print/s ";
cartInfoStore="";
for (xx=0;xx<cartAr.length;xx++){
if (xx!=0 && cartAr[xx][3]!=0){
//
tmpcartArr=cartAr[xx][0]+"^"+cartAr[xx][1]+"^"+cartAr[xx][2]+"^"+cartAr[xx][3] +"^"+cartAr[xx][4] + "|";
cartInfoStore=cartInfoStore+tmpcartArr;
//
//alert("i "+xx+"p "+cartAr[xx][4]);
//alert("i "+xx+"q "+cartAr[xx][3]);
//
tmptotal=tmptotal +(cartAr[xx][3] * cartAr[xx][4]);
tmptotalq=tmptotalq +(cartAr[xx][3]);
//
tmpnames=tmpnames+"<br>"+cartAr[xx][3]+" of "+cartAr[xx][2]+" = £"+(cartAr[xx][3] * cartAr[xx][4]);
tmpdetails=tmpdetails+" | "+cartAr[xx][1]+ " x " + cartAr[xx][3];
tmpnamedetails=tmpnamedetails+" | "+cartAr[xx][0];
}
}
tmpnames=tmpnames+"<br>-------------------";
tmpnames=tmpnames+"<br>Total price £ "+tmptotal;
if(document.cart.noofitems.value)
{document.cart.noofitems.value=""+tmptotalq;
document.cart.totalprice.value="£ "+tmptotal;}
//
cartInfoStore=cartInfoStore + "##" + tmpnames; 
setCookie("cart",cartInfoStore,now);
}

function addToCart(posp,pname,pdesc,quanp,pprice){
fa=document.forms["q"+quanp];
tmpq=parseInt(fa.quantity.value);
//check quanp positive number else reject
if (tmpq==NaN || tmpq<=0){
alert ("Please enter a quantity.");
}else{
nocreate=0;

for (x=0;x<cartAr.length;x++){
if (posp == cartAr[x][0]){
//change quantity of products
cartAr[x][3]=tmpq;
//alert (cartAr[x][3]+" added to cart.");
if (location.href=="basket.php"){updateCart();}else{updateCart();location.href="basket.php";}
nocreate=1;
}else{
//
}

}
// end for

if (nocreate==0){
//create new product in cart
cartAr[x]=new Array(5);
cartAr[x][0]=posp;
cartAr[x][1]=pname;
cartAr[x][2]=pdesc;
cartAr[x][3]=tmpq;
cartAr[x][4]=pprice;
//alert (cartAr[x][3]+" added to cart.");
if (location.href=="basket.php"){updateCart();}else{updateCart();location.href="basket.php";}
}
//if
}
//end of function
}

function checkout(paymethod){
 if (!tmptotalq)
 {
 alert('Please select some prints');
 }else{
  if (tmptotalq!=0)
  {
   if (paymethod=='n'){
   processnochex(0,'none',0);
   }else if (paymethod=='e'){
   processByEmail();
   }else{
   processpaypal();
   }
  }else{
  alert('Please select some prints');
  }
 }
}

function removeFromCart(rposp,rformno){
fr=document.forms["q"+rformno];
fr.quantity.value=0;
incart=0;
//check item code
for (x=0;x<cartAr.length;x++){
if (rposp==cartAr[x][0]){
//add to array of products
cartAr[x][3]=0;
alert (cartAr[x][1] +" item removed.");
incart=1;
updateCart();
reload();
break;
}else{
//
}
}
if (incart==0){alert ("That item was not in your cart.");}
}

function addFormInfo(){
fe=document.forms["emf"];
fe.recipient.value=orderemail; 
fe.description.value=tmpdetails;
fe.ordernumber.value=tmpnamedetails;
fe.amount.value=tmptotal;
fe.submit();
}

function processByEmail(){
fe=document.forms["emf"];
emle=orderemail;
 if (fe.email.value == '')
 {
 alert('Please enter an email address');
 }else{
  fe.action=actionurlemail;
  fe.recipient.value=emle; 
  fe.description.value=tmpdetails;
  fe.ordernumber.value=tmpnamedetails;
  fe.amount.value=tmptotal;
  emptytcart();
  fe.submit();
 }
}

function processnochex(namount,ndescr,norderno){
fn=document.forms["nchx"];
eml=nochexemail;
fn.action="https://www.nochex.com/nochex.dll/checkout";
fn.email.value=eml; 
fn.description.value=tmpdetails;
fn.ordernumber.value=tmpnamedetails;
fn.amount.value=tmptotal;
emptytcart();
fn.submit();
}

function processpaypal(){
alert("Sorry, this method is temporarily unavailable.");
emlp=paypalemail;  
if (emlp == "dummy value"){
f=document.forms["payp"];
f.business.value=emlp; 
f.item_name.value=tmpdetails;
f.item_number.value=tmpnamedetails;
f.amount.value=tmptotal;
f.amount.quantity=tmptotalq;
f.action="https://www.paypal.com/cgi-bin/webscr";
emptytcart();
f.submit();
}
}

function cartDetails(){
var popurl="basket.php";
location.href=popurl;
}

function openCenteredWindow(url, height, width, name, parms) {
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (parms) { winParms += "," + parms; }
   var win = window.open(url, name, winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
   return win;
}

function displaycart(){
pgdata="";
pgdata=pgdata+'<table width="100%" class="tablebasket"><tr><td class="cellbasket"><span class="subheadertext">name of print</span></td>';
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">quantity</span></td>'; 
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">£</span></td>';
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">remove</span></td></tr>';
thash="##";
tpipe="|";
tcomma="^";
var tmpparse = getCookie("cart");
if (!tmpparse || tmpparse=="blank"){
//alert("no cookie");
}else{
tmpmanip=tmpparse.split(thash);
yyy=tmpmanip[0];
cartRegenerateA=yyy.split(tpipe);
for(ii=1;ii<cartRegenerateA.length;ii++){
ttt=cartRegenerateA[(ii-1)];
cartRegenerateB=ttt.split(tcomma);
cartAr[ii]=new Array(5);
cartAr[ii][0]=cartRegenerateB[0];
cartAr[ii][1]=cartRegenerateB[1];
cartAr[ii][2]=cartRegenerateB[2];
cartAr[ii][3]=parseInt(cartRegenerateB[3]);
cartAr[ii][4]=parseInt(cartRegenerateB[4]);
//alert(cartAr[ii][3]);
//
pgdata=pgdata+'<tr><td class="cellbasket">'+cartAr[ii][2]+'</td>'; 
pgdata=pgdata+'<td class="cellbasket">'; 
pgdata=pgdata+'<p><form name="q'+ii+'">'; 
pgdata=pgdata+'<input name="quantity" type="text" value="'+cartAr[ii][3]+'" size="3" maxlength="3">'; 
tmparg=' \''+cartAr[ii][0]+'\' , \''+cartAr[ii][1]+'\',\''+cartAr[ii][2]+'\','+ii+',\''+cartAr[ii][4]+'\'';
pgdata=pgdata+'&nbsp;[<a href="javascript:addToCart('+tmparg+');">Add</a>]';
pgdata=pgdata+'</form></p></td>'; 
//
pgdata=pgdata+'<td class="cellbasket">£'+(cartAr[ii][3] * cartAr[ii][4])+'</td>';
pgdata=pgdata+'<td class="cellbasket">[<a href="javascript:removeFromCart(\''+cartAr[ii][0]+'\',\''+ii+'\')">remove</a>]</td></tr>';
}
}
pgdata=pgdata+'</table>';
document.write (unescape(pgdata));
//
}
function displaystaticcart(){
pgdata="";
pgdata=pgdata+'<table width="100%" class="tablebasket"><tr><td class="cellbasket"><span class="subheadertext">name of print</span></td>';
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">quantity</span></td>'; 
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">£</span></td>';
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">&nbsp;</span></td></tr>';
//
thash="##";
tpipe="|";
tcomma="^";
tmptotz=0;
var tmpparse = getCookie("cart");
if (!tmpparse || tmpparse=="blank"){
//alert("no cookie");
}else{
tmpmanip=tmpparse.split(thash);
yyy=tmpmanip[0];
cartRegenerateA=yyy.split(tpipe);
for(ii=1;ii<cartRegenerateA.length;ii++){
ttt=cartRegenerateA[(ii-1)];
cartRegenerateB=ttt.split(tcomma);
cartAr[ii]=new Array(5);
cartAr[ii][0]=cartRegenerateB[0];
cartAr[ii][1]=cartRegenerateB[1];
cartAr[ii][2]=cartRegenerateB[2];
cartAr[ii][3]=parseInt(cartRegenerateB[3]);
cartAr[ii][4]=parseInt(cartRegenerateB[4]);
//alert(cartAr[ii][3]);
//
//
pgdata=pgdata+'<tr><td class="cellbasket">'+cartAr[ii][2]+'</td>'; 
pgdata=pgdata+'<td class="cellbasket">'+cartAr[ii][3]+'</td>'; 
//
pgdata=pgdata+'<td class="cellbasket">£'+(cartAr[ii][3] * cartAr[ii][4])+'</td>';
pgdata=pgdata+'<td class="cellbasket">&nbsp;</td></tr>';
tmptotz=tmptotz+(cartAr[ii][3] * cartAr[ii][4]);
}
}
pgdata=pgdata+'<tr><td class="cellbasket"><span class="subheadertext">&nbsp;</span></td>';
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">total cost:&nbsp;</span></td>'; 
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">£'+tmptotz+'</span></td>';
pgdata=pgdata+'<td class="cellbasket"><span class="subheadertext">&nbsp;</span></td></tr>';
pgdata=pgdata+'</table>';
document.write (unescape(pgdata));
//
}
//
function fnSetFocus ()
{
//
}

//
//********************************************************************
//
//<!--
// Copyright (c) 1996-1997 Tomer Shiran. All rights reserved.
// Permission given to use the script provided that this notice remains as is.
// Additional scripts can be found at http://www.geocities.com/~yehuda/
// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" + 
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}

// date - any instance of the Date object
// * you should hand all instances of the Date object to this function for "repairs"
// * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0){
date.setTime(date.getTime() - skew)
}
}

var now = new Date()
fixDate(now);
now.setTime(now.getTime() + 31 * 24 * 60 * 60 * 1000);
//
//*********************************************************************************
//
parseCookie();