// cart.js
//
// Kestrel Forestry Crime Stoppers Explorer Series Map Store
//
// referenced by:
//	map_index.htm
//	map_info.htm
//	checkout.htm
//	cart.htm
//	crimestopper.htm
//	return.htm
//	cancel_return.htm
//	shipping
//	full_map.htm
//	edit_cart.htm
//
// current tax rates:
//	pst 0.08
//	gst 0.05
//  hst 0.13
//
// 2008.09.05 - fd - update to change shipping calculations and add documentation
// 2009.01.19 - fd - updated shipping to keep up with cost increases
// 2010.06.23 - fd - updated for HST
// 2010.08.14 - fd - updated available maps/products
// 2010.09.21 - fd - updated available maps/products
// 2011.08.24 - fd - updated available maps/products (14 no longer flat/folded)
// 2011.09.02 - fd - updated available maps/products (13 no longer flat)
// 2011.09.12 - fd - 13 no longer folded

var maptotal = 29; // set this to one more than the max value in maparray(X)

// map array

// This map array is related to the map.swf created by Korkola Designs and needs to be updated to include zone 16 maps
// and the remainder (25-28) of the zone 15 maps.
var maparray = new Array()
	maparray[0]="";
	maparray[1]="15-1 Woodland Caribou";
	maparray[2]="15-2 Sydney Lake";
	maparray[3]="15-3 Kenora";
	maparray[4]="15-4 Lake of the Woods";
	maparray[5]="15-5 Big Traverse";
	maparray[6]="15-6 Red Lake";
	maparray[7]="15-7 Ear Falls";
	maparray[8]="15-8 Vermilion";
	maparray[9]="15-9 Sioux Narrows";
	maparray[10]="15-10 Fort Frances";
	maparray[11]="15-11 Birch Lake";
	maparray[12]="15-12 Lac Seul";
	maparray[13]="15-13 Dryden";
	maparray[14]="15-14 Manitou";
	maparray[15]="15-15 Rainy Lake";
	maparray[16]="15-16 Lac la Croix";
	maparray[17]="15-17 Cat River";
	maparray[18]="15-18 St. Raphael Lake";
	maparray[19]="15-19 Sioux Lookout";
	maparray[20]="15-20 Ignace";
	maparray[21]="15-21 Atikokan";
	maparray[22]="15-22 Quetico";
	maparray[23]="15-23 Lake St. Joseph";
	maparray[24]="15-24 Savant";
	maparray[25]="15-25 Brightsand";
	maparray[26]="15-26 Graham";
	maparray[27]="15-27 Lac des Milles Lacs";
	maparray[28]="15-28 Northern Lights Lake";
	
// description array

var descriptionarray = new Array()
    descriptionarray[0]="";
    descriptionarray[1]="Covers Woodland Caribou Park with portages and access points as far east as Pipestone Bay of Red Lake and south to Wrist/Haggart Lakes. Includes WMU&#39;s: 2 & 3. (Printed August 31, 2006)";
    descriptionarray[2]="Covers lower portion of Woodland Caribou Park north to Long Legged Lake, south to Umfreville Lake, Manitoba border east to Scotty and Dumpy Lakes. Includes WMU&#39;s: 2, 3 and 6. (Printed August 31, 2006)";
    descriptionarray[3]="UPDATED 2010 - Now with contours!<br />Covers from Manitoba border with Ontario, south to Northern Peninsula and Shoal Lake IR, east to Silver Lake and North to Separation/Tetu Lakes. Includes WMU&#39;s: 7B, 6.<br />Updated reprinted June 23 2010.<br />";
    descriptionarray[4]="Covers Alneau Peninsula, Bigsby Island in the south, Whitefish Bay, Shoal Lake and North to Western Peninsula. Includes WMU&#39;s: 7A & 7B. (Printed February 17, 2005)";
    descriptionarray[5]="Covers Bigsby Island to the north, East to Hwy 617, Rainy River and Border area county roads. Includes WMU&#39;s: 7B & 10. (Printed July 31, 2005)";
    descriptionarray[6]="Centres almost on the Town of Red Lake, covers Trout Lake to the east, west to Pipestone Lake, Gullrock Lake in the south and north to Nungesser River. Includes WMU&#39;s 2 & 3. (Printed August 31, 2006)";
    descriptionarray[7]="Covers Pakwash Lake in the north, east to Sawmill Bay of Lac Seul, and south to Perrault and Ball Lakes, and west to Longlegged Lake. Includes WMU&#39;s: 3, 4, 5, and 6. (Printed August 31, 2006)";
    descriptionarray[8]="UPDATED 2010 - Now with contours!<br />Covers Vermilion Bay/Eagle Lake in the south, Silver Lake/Dogtooth Lake in the west, Cedar Lake and English River IR in the north, east to Minnitaki. Includes WMU&#39;s: 5, 6, 7B & 8.<br />Updated map reprinted June 23 2010.";
    descriptionarray[9]="Covers Pipestone Lake in the South, Town of Sioux Narrows in the west, Eagle and Hillcock Lake in the North and Lower Manitou in the east. Includes WMU&#39;s: 7B, 8, 9A. (Printed September 14, 2005)";
    descriptionarray[10]="Covers in the west Sifton Twp to Rainy Lake in the east, south to the border area and north to Calliper/Pipestone Lakes. Includes WMU&#39;s: 7B, 9B, 10 and 11A. (Printed July 30, 2005)";
    descriptionarray[11]="Birch Lake: Covers Birch Lake in the North to Sesikinaga/Jeanette Lake in the East, west to Trout Lake as far as Hudson Bay Point and south to Whitemud Lake. Includes WMU&#39;s: 3, 4, & 16A. (Printed August 31, 2006)";
    descriptionarray[12]="Covers Vermilion River Road from Donch Lake and Wapesi Bay in the east, west to the intersection with Hwy XXX near Sawmill Bay of Lac Seul, south to Williams Bay and as far north as Bluffy Lake. Includes WMU&#39;s: 4 and 5. (Printed August 31, 2006)";
    descriptionarray[13]="Covers from Williams Bay in the North, south to Eagle/Wabigoon Lakes, west to Mafeking Lake, east to Basket Lake and Minnitaki Lake. Includes WMU&#39;s: 5 & 8. (Printed November 7, 2005)";
    descriptionarray[14]="Covers as far as Lower Manitou and Manitou Stretch in the west, Entwine Lake in the south, Dinorwic Lake in the north, Revell/Smirch/Dibble Lakes in the east. Includes WMU&#39;s 8, 9A, 12A. (Printed August 24, 2005)";
    descriptionarray[15]="Covers Mainville Lake and Northeast Bay in the west, Oak Point Island/Duff Lake in the south, Eltrut Lake in the north and to Hepburn Bay/Bennett Twp in the east. Includes WMU&#39;s: 9A, 9B, 11A, 12A, and 12B. (Printed July 29, 2005)";
    descriptionarray[16]="Covers the border area south to Loon Lake, Quetico Lake in the east, and Town of Mine Centre in the north and west to Halkirk Twp. Includes WMU&#39;s: 9B, 11A, and 12B. (Printed July 29, 2005)";
    descriptionarray[17]="Coming Soon - currently in review at Ontario Ministry of Natural Resources. Covers Lake St. Joseph in the east to Zionz Lake in the North, Sesikinaga/Jeanette Lake in the west to Root Lake in the South. Includes WMU&#39;s: 16A, 16B, & 4.";
    descriptionarray[18]="Covers eastern portion of Lac Seul and Vermilion River Road from Hwy 516 to Wapesi Bay in the west, south to intersection of Vermilion River Road and hwy 516, east to Hooker/Vincent Lakes and north to the loop of Vermilion River Road around Lac Seul. Includes WMU&#39;s: 4, 5 and 16B.  (Printed November 1, 2005)";
    descriptionarray[19]="Covers Minnitaki Lake to Southwest Bay in the west, east to Sturgeon Lake, south to Barrel Lake and north to intersection of Hwy 516 and Vermilion River Road. It includes the towns of Sioux Lookout and Silver Dollar. Includes WMU&#39;s 4, 5, 15A, and 16B. (Printed October 31, 2005)";
    descriptionarray[20]="Covers White Otter/ Sanford Lakes in the south, east to Reba/Chartrand Roads, north to Barrel Lake and west to Bending Lake/Hyndman Twp. Includes WMU&#39;s: 5, 9A, 12A, and 15A. (Printed August 26, 2005)";
    descriptionarray[21]="UPDATED 2010 - Now with contours!<br />Covers the top of Quetico Lake (Jean Lake) in the south, to Calm Lake in the west, Crooked Pine Lake in the east and North to White Otter Lake. Includes WMU&#39;s: 11A, 12A and 12B. (Printed July 30, 2005)<br />Updated map to be reprinted June 2010.<br />Now with contours and Quetico portages!";
    descriptionarray[22]="Covers are south to international border including Bayley Bay of Basswood Lake, west to Wolseley Lake and Neguaguon Lake IR (25D), north to Pickeral Lake and east to the edge of Quetico Park (Mack Lake). Includes WMU&#39;s: 11A. (Printed August 10, 2005)";
    descriptionarray[23]="Includes WMU&#39;s: 16A and 16B with contours.";
    descriptionarray[24]="Covers the area bisected by HWY 599 with the CNR rail line at the bottom of the map and Wabakimi Provincial Park on the eastern quarter. Includes WMU&#39;s: 16B with contours.";
    descriptionarray[25]="Includes WMU&#39;s: 15A and 15B with contours.";
    descriptionarray[26]="Includes WMU&#39;s: 12A, 15A, and 15B with contours.";
    descriptionarray[27]="Covers the area between HWY 17 and HWY 11 centered on Lac des Mille Lacs. Includes WMU&#39;s: 11B, 12A, 12B, and 15B with contours.";
    descriptionarray[28]="Covers the area east of Quetico Park to Whitefish Lake between HWY 11 and the border with the U.S.A. Includes WMU&#39;s: 11A, 11B, and 13 with contours.";
	
function getmap(map) { // get map
window.location = "map_info.htm?map="+ map;
}

// prices for maps

var f_price = "11.95"; // Folded and Flat
var l_price = "22.50"; // Laminated


// SHIPPING COSTS
// REGIONAL
// anything going to BC,AB,SK,MB,ON,QC
// FOLDED
var regfold = new Array()
    regfold[0]="";
    regfold[1]="2.50";
    regfold[2]="3.75";
    regfold[3]="4.15";
    regfold[4]="5.35";
    regfold[5]="8.50";
    regfold[6]="9.50";
    regfold[7]="9.50";
    regfold[8]="9.50";
    regfold[9]="9.50";
    regfold[10]="9.50";
    regfold[11]="12.00";
    regfold[12]="12.00";
    regfold[13]="12.00";
    regfold[14]="12.00";
    regfold[15]="12.00";
    regfold[16]="50.00";

// FLAT
var regflat = new Array()
    regflat[0]="";
    regflat[1]="12.65";
    regflat[2]="12.65";
    regflat[3]="12.65";
    regflat[4]="12.65";
    regflat[5]="14.20";
    regflat[6]="14.20";
    regflat[7]="14.20";
    regflat[8]="14.20";
    regflat[9]="16.85";
    regflat[10]="16.85";
    regflat[11]="16.85";
    regflat[12]="16.85";
    regflat[13]="18.00";
    regflat[14]="18.00";
    regflat[15]="18.00";
    regflat[16]="50.00";
	
// LAMINATED
var reglam = new Array()
    reglam[0]="";
    reglam[1]="14.65";
    reglam[2]="14.65";
    reglam[3]="14.65";
    reglam[4]="14.65";
    reglam[5]="17.50";
    reglam[6]="17.50";
    reglam[7]="17.50";
    reglam[8]="17.50";
    reglam[9]="20.60";
    reglam[10]="20.60";
    reglam[11]="20.60";
    reglam[12]="20.60";
    reglam[13]="23.60";
    reglam[14]="23.60";
    reglam[15]="23.60";
    reglam[16]="50.00";
	
// The rest of Canada shipping costs
// anything not going to BC,AB,SK,MB,ON,QC
// FOLDED
var canfold = new Array()
    canfold[0]="";
    canfold[1]="3.50";
    canfold[2]="4.25";
    canfold[3]="5.00";
    canfold[4]="5.75";
    canfold[5]="6.50";
    canfold[6]="7.25";
    canfold[7]="8.00";
    canfold[8]="8.75";
    canfold[9]="12.00";
    canfold[10]="12.50";
    canfold[11]="13.00";
    canfold[12]="13.50";
    canfold[13]="14.00";
    canfold[14]="14.50";
    canfold[15]="15.00";
    canfold[16]="50.00";

// FLAT
var canflat = new Array()
    canflat[0]="";
    canflat[1]="14.00";
    canflat[2]="14.00";
    canflat[3]="14.00";
    canflat[4]="14.00";
    canflat[5]="16.00";
    canflat[6]="16.00";
    canflat[7]="16.00";
    canflat[8]="16.00";
    canflat[9]="18.00";
    canflat[10]="18.00";
    canflat[11]="18.00";
    canflat[12]="18.00";
    canflat[13]="21.00";
    canflat[14]="21.00";
    canflat[15]="21.00";
    canflat[16]="50.00";
	
// LAMINATED
var canlam = new Array()
    canlam[0]="";
    canlam[1]="16.00";
    canlam[2]="16.00";
    canlam[3]="16.00";
    canlam[4]="16.00";
    canlam[5]="18.00";
    canlam[6]="18.00";
    canlam[7]="18.00";
    canlam[8]="18.00";
    canlam[9]="20.00";
    canlam[10]="20.00";
    canlam[11]="20.00";
    canlam[12]="20.00";
    canlam[13]="23.00";
    canlam[14]="23.00";
    canlam[15]="23.00";
    canlam[16]="50.00";
	
// USA shipping costs
// FOLDED
var usafold = new Array()
    usafold[0]="";
    usafold[1]="3.70";
    usafold[2]="4.50";
    usafold[3]="7.65";
    usafold[4]="7.65";
    usafold[5]="12.75";
    usafold[6]="13.45";
    usafold[7]="13.75";
    usafold[8]="14.05";
    usafold[9]="14.35";
    usafold[10]="14.55";
    usafold[11]="14.70";
    usafold[12]="14.95";
    usafold[13]="15.20";
    usafold[14]="15.95";
    usafold[15]="17.00";
    usafold[16]="50.00";

// FLAT
var usaflat = new Array()
    usaflat[0]="";
    usaflat[1]="16.00";
    usaflat[2]="16.50";
    usaflat[3]="17.00";
    usaflat[4]="17.50";
    usaflat[5]="18.00";
    usaflat[6]="18.50";
    usaflat[7]="19.00";
    usaflat[8]="19.50";
    usaflat[9]="20.00";
    usaflat[10]="20.50";
    usaflat[11]="21.00";
    usaflat[12]="21.50";
    usaflat[13]="22.00";
    usaflat[14]="22.50";
    usaflat[15]="23.00";
    usaflat[16]="50.00";
	
// LAMINATED
var usalam = new Array()
    usalam[0]="";
    usalam[1]="17.00";
    usalam[2]="17.50";
    usalam[3]="18.00";
    usalam[4]="18.50";
    usalam[5]="19.00";
    usalam[6]="19.50";
    usalam[7]="20.00";
    usalam[8]="20.50";
    usalam[9]="21.00";
    usalam[10]="22.75";
    usalam[11]="22.50";
    usalam[12]="23.25";
    usalam[13]="24.00";
    usalam[14]="24.75";
    usalam[15]="25.50";
    usalam[16]="50.00";
	

// OLD CODE REPLACE SEPTEMBER 2008
// outside shipping costs
// presently is for the rest of Canada and USA	
//var outside = new Array()
//    outside[0]="";
//    outside[1]="3.50";
//    outside[2]="5.00";
//    outside[3]="5.00";
//    outside[4]="8.30";
//    outside[5]="12.25";
//    outside[6]="24.00";
//    outside[7]="25.00";
//    outside[8]="12.00";
//    outside[9]="20.00";
//    outside[10]="30.00";
//    outside[11]="40.00";
	
// calculate shipping costs
// shiptype is either regional or outside
// SHIPTYPE
//  regfold
//  regflat
//  reglam
//  canfold
//  canflat
//  canlam
//  usafold
//  usaflat
//  usalam
// shipmap is 0 for folded and 1 for laminated or flat
function shippingcost(shiptype,count) {
	var s = 0;
	count = count*1;
	if (count < 1){
		s = 0;
	}
	if (count == 1){
		s = shiptype[1];
	} else if (count == 2){
		s = shiptype[2];
	} else if (count == 3){
		s = shiptype[3];
	} else if (count == 4){
		s = shiptype[4];
	} else if (count == 5){
		s = shiptype[5];
	} else if (count == 6){
		s = shiptype[6];
	} else if (count == 7){
		s = shiptype[7];
	} else if (count == 8){
		s = shiptype[8];
	} else if (count == 9){
		s = shiptype[9];
	} else if (count == 10){
		s = shiptype[10];
	} else if (count == 11){
		s = shiptype[11];
	} else if (count == 12){
		s = shiptype[12];
	} else if (count == 13){
		s = shiptype[13];
	} else if (count == 14){
		s = shiptype[14];
	} else if (count == 15){
		s = shiptype[15];
	} else {
		s = shiptype[16];
	}
	return s;
}

// Get URL function

function geturl(url) {
	window.location = url; 
}

// Get values of radio buttons

function getCheckedValue(radioObj) {
        if(!radioObj)
                return "";
        var radioLength = radioObj.length;
        if(radioLength == null)
                if(radioObj.checked)
                     return radioObj.value;
				else 
                        return "";
        for(var i = 0; i < radioLength; i++) {
                if(radioObj[i].checked) {
                        return radioObj[i].value;
                }
        }
        return "";
}

// Round currency to two decimal places

function CurrencyFormatted(amount)
{
        var i = parseFloat(amount);
        if(isNaN(i)) { i = 0.00; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if(s.indexOf('.') < 0) { s += '.00'; }
        if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
} 

// Read Cookie

function readCookie(name)
{
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

// cookie toolbox

var dbug = 0; function d_a(ary) {var beg = next_entry(ary) - 1; for (var i = beg ; i > -1; i--) {ary[i] = null;}} function init_array() {if (dbug) alert('init_cookie');  var ary = new Array(null); return ary;} function set_cookie(name,value,expires) {if (dbug) alert('set_cookie'); if (!expires) expires = new Date();
document.cookie = name + '=' + escape(value) + '; path=/';} function get_cookie(name) {if (dbug) alert('get_cookie'); 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;} function del_cookie(name) {if (dbug) alert('del_cookie');
document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';} function get_array(name, ary) {if (dbug) alert('get_array'); d_a(ary); var ent = get_cookie(name); if (ent) {i = 1; while (ent.indexOf('^') != '-1') {ary[i] = ent.substring(0,ent.indexOf('^')); i++;
ent = ent.substring(ent.indexOf('^')+1, ent.length);}}} function set_array(name, ary, expires) {if (dbug) alert('set_array'); var value = ''; for (var i = 1; ary[i]; i++) {value += ary[i] + '^';} set_cookie(name, value, expires);}
function del_entry(name, ary, pos, expires) {if (dbug) alert('del_entry');var value = ''; get_array(name, ary); for (var i = 1; i < pos; i++) {value += ary[i] + '^';} for (var j = pos + 1; ary[j]; j++) {value += ary[j] + '^';} set_cookie(name, value, expires);} function next_entry(ary) {if (dbug) alert('next_entry'); var j = 0; for (var i = 1; ary[i]; i++) {j = i} return j + 1;}
function debug_on() {dbug = 1;} function debug_off() {dbug = 0;} function dump_cookies() {if (document.cookie == '') document.write('No Cookies Found'); else {thisCookie = document.cookie.split('; '); for (i=0; i<thisCookie.length; i++) {document.write(thisCookie[i] + '<br \/>');}}}
function replace_entry(name, ary, pos, posq, expires) {if (dbug) alert('replace_entry'); var value = ''; get_array(name, ary); for (var i = 1; i < pos; i++) {value += ary[i] + '^';} value += posq + '^'; for (var j = pos + 1; ary[j]; j++) {value += ary[j] + '^';} set_cookie(name, value, expires);}

// end cookie toolbox

// Create cookie with values of 0

function newarray(){
var cookieName = 'cart';
var checkCookie = readCookie(cookieName);
if (checkCookie == null){
var timeToKeep = 7200000;
var expires = new Date();
expires.setTime(expires.getTime() + timeToKeep);
var cartarray = init_array();
for(var i=1; i<=maptotal; i++){
cartarray[i] = "0|0";
}
set_array(cookieName, cartarray, expires); 
}
}

function emptycart(){ // empty the cart
var confirmation = confirm ("Are you sure you want to empty the cart?");
if (confirmation) {
var cookieName = 'cart';
del_cookie(cookieName);
window.location = "cart.htm";
}
}

// Create cookie containing Postal / Zip Code then go to checkout

function shipping(form){
	
	var formvalid = "yes";
	
	if (form.first_name.value == ""){
		alert("Please enter your First Name");
		formvalid = "no";
	} else if (form.last_name.value == ""){
		alert("Please enter your Last Name");
		formvalid = "no";
	} else if (form.address1.value == ""){
		alert("Please enter your Address");
		formvalid = "no";
	} else if (form.city.value == ""){
		alert("Please enter your City");
		formvalid = "no";
	} else if (form.state.options[ form.state.selectedIndex].value == ""){
		if (lc == "US"){
			alert("Please select your State");
			} else {
			alert("Please select your Province");
			}
		formvalid = "no";
	} else if (form.zip.value == ""){
		if (lc == "US"){
			alert("Please enter your Zip Code");
			} else {
			alert("Please enter your Postal Code");
			}
		formvalid = "no";
	}
	
if (formvalid == "yes"){

// form is valid

if (form.address2.value == "") {
	var address2 = "none";
	} else if (form.address2.value == null) {
	var address2 = "none";
	} else {
	var address2 = form.address2.value;
	}
var cookieName = 'shipping';
var timeToKeep = 7200000;
var expires = new Date();
expires.setTime(expires.getTime() + timeToKeep);
var shippingarray = init_array();
shippingarray[1] = form.lc.options[form.lc.selectedIndex].value;
shippingarray[2] = form.first_name.value;
shippingarray[3] = form.last_name.value;
shippingarray[4] = form.address1.value;
shippingarray[5] = address2;
shippingarray[6] = form.city.value;
shippingarray[7] = form.state.options[form.state.selectedIndex].value;
shippingarray[8] = form.zip.value;
set_array(cookieName, shippingarray, expires);

if (formvalid == "yes"){
window.location = "checkout.htm";
}

}
else {

// form is invalid

}
}

// Add map values

function setit(form){
  if (form.map_id) {
   var mapno = form.map_id.value;
  }
  if (form.item_quantity) {
   var mapq = form.item_quantity.value;
  }
var maptype = getCheckedValue(form.map_type);
var timeToKeep = 7200000;
var expires = new Date();
expires.setTime(expires.getTime() + timeToKeep);
var cookieName = 'cart';
var cartarray = init_array(); // if it isn't already defined
mapno = mapno*1;
mapq = mapq + "|" + maptype;
replace_entry(cookieName, cartarray, mapno, mapq, expires);
}

// Edit values

// CHANGES HERE FOR AVAILABLE PRODUCTS
function editit(form){
	
	if (form.count) {
   var count = form.count.value;
  }
	for (var i = 1; i <= count; i++) {
var mapno = eval("form.map_id_"+ i +".value");
var mapq = eval("form.item_quantity_"+i+".value");
//if (mapno == 3 && mapno == 4){ // 3 and 4 only available folded
if (mapno == 4){ // 4 only available folded
	var maptype = "1";
} else if (mapno == 14 && mapno == 13){ // no folded or flat available
	var maptype = "3";
//} else if (mapno == 13 && eval("form.map_type_" + i + "[0].checked") == true)){
//	var maptype = "1";
//} else if (mapno == 13 && eval("form.map_type_" + i + "[2].checked") == true)){
//	var maptype = "3";
} else {
	if (eval("form.map_type_" + i + "[0].checked") == true){
		var maptype = "1";
	} else if (eval("form.map_type_" + i + "[1].checked") == true){
		var maptype = "2";
	} else if (eval("form.map_type_" + i + "[2].checked") == true){
		var maptype = "3";
	}
}
var timeToKeep = 7200000;
var expires = new Date();
expires.setTime(expires.getTime() + timeToKeep);
var cookieName = 'cart';
var cartarray = init_array(); // if it isn't already defined
mapno = mapno*1;
mapq = mapq + "|" + maptype;
replace_entry(cookieName, cartarray, mapno, mapq, expires);
}
}

// Delete map from cart

function mapdelete(map){
	
var confirmation = confirm ("Are you sure you want to delete " + maparray[map] + "?");
if (confirmation) {
	var mapno = map;
	var timeToKeep = 7200000;
	var expires = new Date();
	expires.setTime(expires.getTime() + timeToKeep);
	var cookieName = 'cart';
	var cartarray = init_array(); // if it isn't already defined
	mapno = mapno*1;
	mapq = "0|0";
	replace_entry(cookieName, cartarray, mapno, mapq, expires);
	var cookieName = 'cart';
	var init_count = 0;
	var cartarray = init_array();
	get_array(cookieName, cartarray);
	for (var i=1; i<next_entry(cartarray); i++) {
	var divide = cartarray[i].split("|");
	if (divide[0]>0){
		init_count = init_count + 1;
	}
	}
	if (init_count>0){
	window.location = "edit_cart.htm";
	} else {
	window.location = "cart.htm";
	}
}
else {
alert (maparray[map] + " was not deleted.");
}
}

// Show editable cart

function edit_cart(){
	var cookieName = 'cart';
	var init_count = 0;
	var counter = 0;
	var cartarray = init_array();
	get_array(cookieName, cartarray);
	for (var i=1; i<next_entry(cartarray); i++) {
		var divide = cartarray[i].split("|");
			if (divide[0]>0){
				init_count = init_count + 1;
		}
	}
	if (init_count>0){
		document.write('<form name="form2" action="cart.htm" onsubmit="editit(this);">');
		document.write('<table cellpadding="2" cellspacing="2" border="0"><tr><td><strong>Item</strong></td><td>&nbsp;</td><td><strong>Type</strong></td><td>&nbsp;</td><td align="center"><strong>Quantity</strong></td><td>&nbsp;</td><tr>');
		for (var i=1; i<next_entry(cartarray); i++) {
			var divide = cartarray[i].split("|");
			if (divide[0]>0){
				counter = counter + 1;
				var quantity = divide[0]*1;
				if (divide[1]=="1"){
					var type = "Folded";
					var type1 = '<input type="radio" name="map_type_'+ counter +'" value="1" checked>';
					var type2 = '<input type="radio" name="map_type_'+ counter +'" value="2">';
					var type3 = '<input type="radio" name="map_type_'+ counter +'" value="3">';
				} else if (divide[1]=="2"){
					var type = "Flat";
					var type1 = '<input type="radio" name="map_type_'+ counter +'" value="1">';
					var type2 = '<input type="radio" name="map_type_'+ counter +'" value="2" checked>';
					var type3 = '<input type="radio" name="map_type_'+ counter +'" value="3">';
				} else if (divide[1]=="3"){
					var type = "Laminated";
					var type1 = '<input type="radio" name="map_type_'+ counter +'" value="1">';
					var type2 = '<input type="radio" name="map_type_'+ counter +'" value="2">';
					var type3 = '<input type="radio" name="map_type_'+ counter +'" value="3" checked>';
				}
				document.write('<tr><td valign="top">'+ maparray[i]);
				document.write('<input type="hidden" name="map_id_'+ counter +'" value="'+ i +'"></td>');
				document.write('<td>&nbsp;</td>');
				//if (i !=14){ // 14 not available folded
				if (i !=0 && i !=14 && i !=13){ // 0 14 13 not available folded
					document.write('<td valign="top">' + type1);
					document.write(' Folded - $'+ f_price +' CAD<br>');
				}
				if (i != 4){ // 4 only available folded
					//if (i != 8 && i != 3 && i != 4){ // 8 3 4 not available flat
					if (i != 4 && i != 14 && i != 13){ // 4 and 14 not available flat
						document.write(type2);
						document.write(' Flat - $'+ f_price +' CAD<br>')
					}
					document.write(type3);
					document.write(' Laminated - $'+ l_price +' CAD');
				}
				document.write('</td>');
				document.write('<td>&nbsp;</td>');
				document.write('<td valign="top" align="center"><input name="item_quantity_'+ counter +'" type="text" class="border" size="2" value="'+ quantity +'" >');
				document.write('</td>');
				document.write('<td>&nbsp;</td>');
				document.write('<td valign="top"><input type="button" name="delete" value="Delete" onClick="javascript:mapdelete('+ i +')"></td>');
				document.write('</tr>');
			}
		}
		document.write('<tr><td colspan="6"><table border="0" cellpadding="2" cellspacing="2"><tr><td><input type="hidden" name="count" value="'+ counter +'"><input type="submit" value="Update Cart"></td><td><input type="button" value="Empty Cart" onClick="javascript:emptycart()"></td></tr></table></td></tr></table>');
		document.write('</form>');
	} else {
		document.write('<p>The shopping cart is empty.</p>');
	}
}


// Show cart with totals calculated

function show_cart(){
	var cookieName = 'cart';
	var init_count = 0;
	var sum = 0;
	var cartarray = init_array();
	get_array(cookieName, cartarray);
for (var i=1; i<next_entry(cartarray); i++) {
	var divide = cartarray[i].split("|");
	if (divide[0]>0){
		init_count = init_count + 1;
	}
}
if (init_count>0){
document.write('<form>');
document.write('<table width="100%" cellpadding="2" cellspacing="2" border="0"><tr><td><strong>Item</strong></td><td><strong>Type</strong></td><td><strong>Price</strong></td><td>&nbsp;</td><td align="center"><strong>Quantity</strong></td><td>&nbsp;</td><td><strong>Total</strong></td><tr>');
	for (var i=1; i<next_entry(cartarray); i++) {
var divide = cartarray[i].split("|");
	if (divide[0]>0){
	var quantity = divide[0]*1;
	if (divide[1]=="1"){
	var price = f_price*1;
	var type = "Folded";
	} else if (divide[1]=="2"){
	var price = f_price*1;
	var type = "Flat";
	} else if (divide[1]=="3"){
	var price = l_price*1;
	var type = "Laminated";
	}
	price = CurrencyFormatted(price);
	var totalprice = price * quantity;
	totalprice = CurrencyFormatted(totalprice);
	sum = (sum * 1) + (totalprice * 1);
	sum = CurrencyFormatted(sum);
	document.write('<tr><td>'+ maparray[i] +'</td>');
	document.write('<td>'+ type +'</td>');
	document.write('<td>$'+ price +'</td>');
	document.write('<td>x</td>');
	document.write('<td align="center">'+ quantity +'</td>');
	document.write('<td>=</td>');
	document.write('<td>$'+ totalprice +'</td></tr>');
}
}
document.write('<tr><td colspan="6">&nbsp;</td><td>$'+ sum +' CAD</td></tr>');
document.write('<tr><td colspan="7"><table border="0" cellpadding="2" cellspacing="2"><tr>');
document.write('<td><input type=\"button\" name=\"edit\" value=\"Edit Cart\" onClick=\"javascript:geturl(\'edit_cart.htm\')\"></td>');
var cookieName = 'shipping';
var checkCookie = readCookie(cookieName);
if (checkCookie != null){
document.write("<td><input type=\"button\" name=\"checkout\" value=\"Shipping Information\" onClick=\"javascript:geturl(\'shipping.htm\')\"></td><td><input type=\"button\" name=\"checkout\" value=\"Check Out\" onClick=\"javascript:geturl(\'checkout.htm\')\"></td>");
} else {
document.write("<td><input type=\"button\" name=\"checkout\" value=\"Check Out\" onClick=\"javascript:geturl(\'shipping.htm\')\"></td>");
}
document.write('</tr></table></td></tr>');
document.write('</table>');
document.write('</form>');
} else {
document.write('<p>The shopping cart is empty.</p>');
}
}

// Show final cart with totals and calculated

function checkout(){
	var cookieName = 'cart';
	var init_count = 0;
	var counter = 0;
	var unitcount = 0;
	var sum = 0;
	var shipmaptype = 0;
	var cartarray = init_array();
	get_array(cookieName, cartarray);
for (var i=1; i<next_entry(cartarray); i++) {
	var divide = cartarray[i].split("|");
	if (divide[0]>0){
		init_count = init_count + 1;
	}
}
if (init_count>0){
document.write('<form action="https://www.paypal.com/cgi-bin/webscr" method="post">');
document.write('<input type="hidden" name="cmd" value="_ext-enter">');
document.write('<input type="hidden" name="redirect_cmd" value="_cart">');
document.write('<input type="hidden" name="upload" value="1">');
document.write('<input type="hidden" name="business" value="sales@kestrelforestry.com">');
document.write('<input type="hidden" name="currency_code" value="CAD">');
document.write('<input type="hidden" name="return" value="http://www.kestrelforestry.com/return.htm">');
document.write('<input type="hidden" name="cancel_return" value="http://www.kestrelforestry.com/cancel_return.htm">');
document.write('<table width="100%" cellpadding="2" cellspacing="2" border="0"><tr><td><strong>Item</strong></td><td><strong>Type</strong></td><td><strong>Price</strong></td><td>&nbsp;</td><td align="center"><strong>Quantity</strong></td><td>&nbsp;</td><td><strong>Total</strong></td><tr>');
	for (var i=1; i<next_entry(cartarray); i++) {
var divide = cartarray[i].split("|");
	if (divide[0]>0){
	var quantity = divide[0]*1;
	if (divide[1]=="1"){
	var price = f_price*1;
	var typecode = "t1";
	var type = "Folded";
	} else if (divide[1]=="2"){
	var price = f_price*1;
	var typecode = "t2";
	if (shipmaptype == 0){
	var shipmaptype = 1;}
	var type = "Flat";
	} else if (divide[1]=="3"){
	var price = l_price*1;
	var typecode = "t3";
	if (shipmaptype != 2){
	var shipmaptype = 2;}
	var type = "Laminated";
	}
	counter = counter + 1;
	price = CurrencyFormatted(price);
	var totalprice = price * quantity;
	totalprice = CurrencyFormatted(totalprice);
	unitcount = (unitcount * 1) + (quantity * 1);
	sum = (sum * 1) + (totalprice * 1);
	sum = CurrencyFormatted(sum);
	document.write('<tr><td>'+ maparray[i]);
	document.write('<input type="hidden" name="item_name_'+ counter +'" value="'+ maparray[i] +'">');
	document.write('<input type="hidden" name="item_number_'+ counter +'" value="'+ i + typecode +'"></td>');
	document.write('<td>'+ type);
	document.write('<input type="hidden" name="on0_'+ counter +'" value="Type">');
	document.write('<input type="hidden" name="os0_'+ counter +'" value="'+ type +'"></td>');
	document.write('<td>$'+ price);
	document.write('<input type="hidden" name="amount_'+ counter +'" value="'+ price +'"></td>');
	document.write('<td>x</td>');
	document.write('<td align="center">'+ quantity);
	document.write('<input type="hidden" name="quantity_'+ counter +'" value="'+ quantity +'"></td>');
	document.write('<td>=</td>');
	document.write('<td>$'+ totalprice +'</td></tr>');
}
}
document.write('<tr><td colspan="3" rowspan="5" valign="bottom">');
document.write('<table border="0" cellpadding="4" cellspacing="4"><tr><td><input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit"></td>');
document.write('<td><input type="button" name="edit" value="Edit Cart" onClick="javascript:geturl(\'edit_cart.htm\')"></td>');
//document.write('</tr><tr><tb colspan="2">Pay at PayPal</td>');
document.write('</tr><tr><td colspan="2"><img src="images/credit_cards.gif" width="133" height="27" /></td>');
document.write('</tr></table></td>');
document.write('<td colspan="2" align="right">Total</td><td>=</td><td>$'+ sum +'</td></tr>');
	var shippingCookie = 'shipping';
	var shippingarray = init_array();
	get_array(shippingCookie, shippingarray);
	var country = shippingarray[1];
	var provstat = shippingarray[7]; // added 2008.03.08 to fix pst on shipping
	var address2 = shippingarray[5];
	if (address2 == "none"){
		address2 = '&nbsp;';
	} else {
		address2 = '<input type="hidden" name="address2" value="'+ shippingarray[4] +'">';
	}
	var code = shippingarray[8];

// Determine where the maps are going and what maptype in order to figure out postage.
	if (shipmaptype == 0){
	// FOLDED
		if (country == "CA" || country == "Canada"){
		        if (provstat == "ON" || provstat == "BC" || provstat == "AB" || provstat == "SK" || provstat == "MB" || provstat == "QC"){
		        // REGIONAL
		        // anything going to BC,AB,SK,MB,ON,QC
				shiptype = regfold;
		        } else {
		        // The rest of CANADA
			// anything in Canada not going to BC,AB,SK,MB,ON,QC
		        	shiptype = canfold;
		        }
		} else {
			shiptype = usafold;
		}
	} else {
		if (shipmaptype == 1){
		// FLAT
			if (country == "CA" || country == "Canada"){
			        if (provstat == "ON" || provstat == "BC" || provstat == "AB" || provstat == "SK" || provstat == "MB" || provstat == "QC"){
					shiptype = regflat;
			        } else {
			        	shiptype = canflat;
			        }
			} else {
				shiptype = usaflat;
			}
		} else {
		// LAMINATED
			if (country == "CA" || country == "Canada"){
			        if (provstat == "ON" || provstat == "BC" || provstat == "AB" || provstat == "SK" || provstat == "MB" || provstat == "QC"){
					shiptype = reglam;
			        } else {
			        	shiptype = canlam;
			        }
			} else {
				shiptype = usalam;
			}
		}
	}
		

// ORIGINAL CODE REPLACE SEPTEMBER 2008
//	var startswith = code.charAt(0);
//	if (startswith == "p" || startswith == "r" || startswith == "P" || startswith == "R"){
//		shiptype = regional;
//	} else {
//		shiptype = outside;
//	}


//var shipcost = shippingcost(shiptype,shipmaptype,unitcount);
var shipcost = shippingcost(shiptype,unitcount);
shipcost = CurrencyFormatted(shipcost);
sum = (sum * 1) + (shipcost * 1);
sum = CurrencyFormatted(sum);
document.write('<tr><td colspan="2" align="right"><input type="hidden" name="lc" value="'+ shippingarray[1] +'"><input type="hidden" name="first_name" value="'+ shippingarray[2] +'"><input type="hidden" name="last_name" value="'+ shippingarray[3] +'"><input type="hidden" name="address1" value="'+ shippingarray[4] +'">'+ address2 +'<input type="hidden" name="city" value="'+ shippingarray[6] +'"><input type="hidden" name="state" value="'+ shippingarray[7] +'"><input type="hidden" name="zip" value="'+ shippingarray[8] +'"><input type="hidden" name="shipping_1" value="'+ shipcost +'">Shipping Cost</td><td>=</td><td>$'+ shipcost +'</td></tr>');
	if (country == "CA" || country == "Canada" || country == ""){ // calculate hst
	var hst = sum * 0.13;
	hst = CurrencyFormatted(hst);
	var tax = (hst * 1)
	tax = CurrencyFormatted(tax);
	document.write('<tr><td colspan="2" align="right"><input type="hidden" name="tax_cart" value="'+ tax +'">HST</td><td>=</td><td>$'+ hst +'</td></tr>');
	sum = (sum * 1) + (tax * 1);
	sum = CurrencyFormatted(sum);
	} else {
		// do nothing
	var hst = 0;
	hst = CurrencyFormatted(hst);
	}
if (country == "CA" || country == "Canada" || country == ""){
document.write('<tr><td colspan="2" align="right">Order Total</td><td>=</td><td>$'+ sum +' CAD</td></tr>');
} else {
document.write('<tr><td colspan="2" rowspan="3" valign="top" align="right">Order Total</td><td>=</td><td>$'+ sum +' CAD</td></tr>');
}
document.write('</table>');
document.write('</form>');
} else {
document.write('<p>The shopping cart is empty.</p>');
}
}

// end


