
// CA stuff Below

var item01  = new Array (0, 17.94, 18.05, 18.67, 25.05, 25.31, 25.41);
var item02  = new Array (0, 20.56, 20.71, 21.60, 27.82, 28.18, 28.71);
var item03  = new Array (0, 35.34, 36.28, 39.63, 47.78, 48.29, 51.44);
var item04  = new Array (0, 15.52, 15.67, 15.88, 22.15, 22.31, 22.41);
var itembyzone  = new Array (0,item01, item02,item03,item04);

var zip = "ZZZ";
var zone;
var amt;
var quantity = 1;
var itemval;
var shipval;
var thisitem;
var zn = 0;   // ZIP, zone entries index
var zne;
var zipl = new Array ();   // ZIP entry low
var ziph = new Array ();   // ZIP entry high
var zone = new Array ();   // corresponding zone for ZIP


function addToBasket_CA(intProduct) {

zip=GetZIP();
// alert ("Zip from cookie is: " + zip);

	switch(intProduct) {
		case 0:
			document.forms[1].submit();
			break;
		case 1:
			shipval=CalcCost(1);
			itemval = 49.95;
			if (shipval == 0) {itemval = 0; quantity = 0; }
			document.getElementById('item_name').value = 'Cleveredge 15ft Pack';
			document.getElementById('item_number').value = 'SED-WEBCOR001';
			document.getElementById('amount').value = itemval;
			document.getElementById('quantity').value = quantity;
			document.getElementById('shipping').value = shipval;
			document.getElementById('shipping2').value = shipval;
			document.forms[0].submit();
			break;
		case 2:
			shipval=CalcCost(2);
			itemval = 89.95;
			if (shipval == 0) {itemval = 0; quantity = 0; }
			document.getElementById('item_name').value = 'Cleveredge 30ft Pack';
			document.getElementById('item_number').value = 'SED-WEBCOR002';
			document.getElementById('amount').value = itemval;
			document.getElementById('quantity').value = quantity;
			document.getElementById('shipping').value = shipval;
			document.getElementById('shipping2').value = shipval;
			document.forms[0].submit();
			break;
		case 3:
			shipval=CalcCost(3);
			itemval = 259.95;
			if (shipval == 0) {itemval = 0; quantity = 0; }
			document.getElementById('item_name').value = 'Cleveredge 150ft Pack';
			document.getElementById('item_number').value = 'SED-WEBCOR003';
			document.getElementById('amount').value = itemval;
			document.getElementById('quantity').value = quantity;
			document.getElementById('shipping').value = shipval;
			document.getElementById('shipping2').value = shipval;
			document.forms[0].submit();
			break;
		case 4:
			shipval=CalcCost(4);
			itemval = 19.95;
			if (shipval == 0) {itemval = 0; quantity = 0; }
			document.getElementById('item_name').value = 'Cleveredge 150 Extra Fixing Pins';
			document.getElementById('item_number').value = 'SED-WEBCOR004';
			document.getElementById('amount').value = itemval;
    			document.getElementById('quantity').value = quantity;
			document.getElementById('shipping').value = shipval;
			document.getElementById('shipping2').value = shipval;
			document.forms[0].submit();
			break;
	}
}

function CalcCost (thisitem) {
//	var str = "(error)";
  zne=50
	if (zip == "ZZZ") { // do we already have the zip code
		zip = prompt ("Please enter the destination Post Code - ", "");
		
	}
  zip = zip.substring(0,3); 	// we only need first three chars of the zip code
  zip = zip.toUpperCase()
  SetUpsZ ();     // set up UPS ground shipping zones
  str = "UPS ground rate shipping";
  for (var i=zn-1; i>=0; i--) { // run table backwards
    if ((zip >= zipl[i]) & (zip <= ziph[i])) {  // zip value
      zne = zone[i];            // corresponding zone
      break;                    // get out, now
    } //(myzip.substring(1,1) <
  }
  amt = Dollar(itembyzone[thisitem][zne-50]);          // UPS charges

//if (obj1.os0) obj1.os0.value = "zone " + zne + ", $" + Dollar(amt) + "/item";
//  obj1.shipping.value = Dollar (amt);
//  obj1.shipping2.value = Dollar (amt);	
	str = str + "\n\n" +
		   "Item   = " + thisitem + "\n\n" +
		   "ZIP    = " + zip + "\n\n" +
		   "Zone   = " + zne + "\n\n" +
		   "Charge = $" + amt + "\n\n" ;
	if (amt == 0 ) {
		str = str + "                            SORRY!\n\n" +
		            "We cannot currently ship to your ZIP/area from the website.    \n" +
		            "           Please contact us for a quotation.\n\n";
	}
	alert (str);
return amt;	
}
	
function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < 1) rnd = 1;
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}	


function SetZone () {  // record zip/zone info into table
  zn = 0;                      // count of breakpoints
  for (var i=0; i<arguments.length; i=i+3) {
    zipl[zn] = arguments[i];   // zip code index
    ziph[zn] = arguments[i+1]
    zone[zn] = arguments[i+2]; // zone
    zn = zn + 1;               // number of bkpts
  }
}

function SetUpsZ () {  // set UPS ground Zones
  SetZone 
(
'A0A','A0Z',54, 'A1A','A1N',54, 'A1P','A9Z',54,
'B0A','B2T',54, 'B2V','B3B',54, 'B3C','B3G',54, 'B3H','B4B',54, 'B4C','B9Z',54,
'C0A','C0Z',54, 'C1A','C1E',54, 'C1G','C9Z',54, 
'E0A','E0Z',54, 'E1A','E1G',54, 'E1H','E2G',54, 'E2H','E2S',54, 'E2T','E2Z',54,
'E3A','E3E',54, 'E3G','E9Z',54,
'G0A','G0A',51, 'G0B','G0J',54, 'G0K','G0L',54, 'G0M','G0S',51, 'G0T','G0W',54,
'G0X','G0Z',51, 'G1A','G3A',51, 'G3B','G3K',51, 'G3L','G4P',54, 'G4R','G4S',54,
'G4T','G4V',54, 'G4W','G4Z',54, 'G5A','G5A',51, 'G5B','G5T',54, 'G5V','G6A',51,
'G6B','G6B',51, 'G6C','G6J',51, 'G6K','G6K',51, 'G6L','G6T',51, 'G6V','G6Z',51,
'G7A','G7A',54, 'G7B','G8P',54, 'G8R','G8S',54, 'G8T','G9Z',51,
'H0A','H0L',51, 'H0M','H0M',54, 'H0N','H9Z',51, 
'J0A','J0L',51, 'J0M','J0M',54, 'J0N','J0V',51, 'J0W','J0Z',54, 'J1A','J3C',51,
'J3E','J3E',51, 'J3G','J3S',51, 'J3T','J3V',51, 'J3W','J3X',51, 'J3Y','J5R',51,
'J5S','J5X',51, 'J5Y','J7H',51, 'J7J','J7N',51, 'J7P','J7W',51, 'J7X','J8N',51,
'J8P','J9C',51, 'J9E','J9E',54, 'J9G','J9J',51, 'J9K','J9Z',54,
'K0A','K0H',51, 'K0J','K0J',54, 'K0K','K0Z',51, 'K1A','K4A',51, 'K4B','K7T',51,
'K7V','K8H',54, 'K8J','L1E',51, 'L1F','L1Z',51, 'L2A','L2C',51, 'L2E','L2W',51,
'L2X','L3N',51, 'L3P','L3T',51, 'L3V','L3W',51, 'L3X','L4L',51, 'L4M','L4R',51,
'L4S','L9T',51, 'L9V','L9Z',51, 
'M0A','M9Z',51, 'N0A','N1A',51, 'N1B','N2Y',51, 'N2Z','N2Z',51, 'N3A','N3V',51,
'N3W','N5T',51, 'N5V','N6N',51, 'N6P','N8M',51, 'N8N','N9K',51, 'N9L','N9Z',51,
'P0A','P0Z',54, 'P1A','P2M',54, 'P2N','P2N',54, 'P2P','P3K',54, 'P3L','P3L',54,
'P3M','P3M',54, 'P3N','P3P',54, 'P3R','P4M',54, 'P4N','P4R',54, 'P4S','P4Z',54,
'P5A','P5A',54, 'P5B','P5M',54, 'P5N','P5N',54, 'P5P','P8M',54, 'P8N','P8N',54,
'P8P','P8S',54, 'P8T','P8T',54, 'P8V','P8Z',54, 'P9A','P9A',54, 'P9B','P9M',54,
'P9N','P9N',54, 'P9P','P9Z',54, 
'R0A','R2B',55, 'R2C','R4A',52, 'R4B','R9Z',55,
'S0A','S0Z',55, 'S1A','S3M',55, 'S3N','S3N',55, 'S3P','S3Z',55, 'S4A','S4A',55,
'S4B','S4G',55, 'S4H','S4H',55, 'S4J','S4M',55, 'S4N','S4Z',52, 'S5A','S7G',55,
'S7H','S7V',52, 'S7W','S9Z',55, 
'T0A','T0B',56, 'T0C','T0C',53, 'T0E','T1W',56, 'T1X','T1X',56, 'T1Y','T3Z',53,
'T4A','T4Z',53, 'T5A','T6V',53, 'T6W','T6X',56, 'T6Y','T7W',56, 'T7X','T8N',53,
'T8P','T8Z',56, 'T9A','T9E',53, 'T9G','T9Z',56,
'V0A','V0L',56, 'V0M','V0M',53, 'V0N','V1L',56, 'V1M','V1M',53,'V1N','V2R',56,
'V2S','V2V',56, 'V2W','V3E',53, 'V3G','V3G',56, 'V3H','V7Z',53,'V8A','V8K',56,
'V8L','V9E',53, 'V9G','V9Z',56
);


}

function SaveZIP(myzip)  {
	
	var CookieString;
	var argv = SaveZIP.arguments;
	var argc = SaveZIP.arguments.length;
	var expires = null;
	var path = "/"; //(argc > 3) ? argv[3] : null;
	var domain = "";
	var secure = (argc > 5) ? argv[5] : false;

//Canadian zip is ANA
	myzip = myzip.toUpperCase();
	if ((myzip.substr(0,1) < "A") || (myzip.substr(0,1) > "Z")) { badzip = "ZZZ"; };
	if ((myzip.substr(1,1) < "0") || (myzip.substr(1,1) > "9")) { badzip = "ZZZ"; };
	if ((myzip.substr(2,1) < "A") || (myzip.substr(2,1) > "Z")) { badzip = "ZZZ"; };

	
//	if ( badzip == "ZZZ" ) { myzip = badzip } 

//alert (myzip);
    if (navigator.cookieEnabled == 0) {
  		alert("You need to enable cookies in your browser for this site to load properly!");
	}
	CookieString = "zip =" + escape (myzip) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
	CookieString = "zip =" + escape (myzip);
//alert (CookieString);
	document.cookie = CookieString;
//alert (document.cookie);
	document.forms[1].submit();
}

function GetCookieVal(offset) {
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetZIP () {
	var arg = "zip=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;	
	}
	return null;
}
	

