﻿/*
	shipping rate calculator
	(c) Copyright Kris Koiner of VanDigital.com (kris at vandigital dot com)
	written for Peter McGrath of Eyeball Productions
*/

//for the populate function, the maximum number of items available for each
var maxitemcount = 5;

/*
	shipping: shipping costs
	rows: usa10, usa34, intsur, intair, can (same order as dropdown box)
	cols: base, 1st item, 2nd item, ...
*/
var datashipping = Array(
	Array(0,22,3,5),
	Array(0,31,8,7),
	Array(0,22,22,22,22,4.50),
	Array(0,51.50,51.50,25,20),
	Array(0,2,2,2,1)
);


/*** NOTE: from here down, items must be ordered the same as indexes will be cross referenced
			also, items must be ordered from highest price to lowest price
*/
/*
	modifiers: percentage of normal shipping
	rows: items 0 - max
	cols: shipping types
*/
var datamodifiers = Array(
	Array(1,1,1,1,1,1),
	Array(1,1,1,1,1,1),
	Array(1,1,1,1,1,1),
	Array(1,1,1,1,1,1),
	Array(1,1,1,1,1,1),
	Array(1,1,1,1,1,1)
	/* ,Array(.5,.5,.5,.5,.5,.5) */
);

// dataprices: the price for each item
var dataprices = Array(105,105,105,95,95,95,95);

//the available items, in the order to check
var items = Array("i_soul1","i_gospel","i_blues","i_rb4","i_rb3","i_rb2","i_rb1");
var descriptions = Array("The Soul Discography Volume 1","The Gospel Discography","The Blues Discography","The R&B Indies Volume 4","The R&B Indies Volume 3","The R&B Indies Volume 2","The R&B Indies Volume 1");

//there is no need to edit the code below
//=======================================================================================
//private variables
var lastshipping = 0;
var lastsubtotal = -1; //used as error checking

//public functions ===============
function populateboxes(form) {
	for (i = 0; i < items.length; i++) {
		if (form.elements[items[i]].options.length < maxitemcount+1) { //bugfix
		for (j = 0; j <= maxitemcount; j++) {
			newitm = document.createElement('option');
			newitm.text = j;
			try { //for w3
				form.elements[items[i]].add(newitm,null);
			} catch(e) { //for ie
				form.elements[items[i]].add(newitm);
			}
		}
		}
	}
}

function recalculate(form) {
	//set variables
	var subtotal = 0.0;
	var shipping = 0.0;
	var nitems = 0;          
	var d = document.getElementById("messages");                  
	if (form.shipping.selectedIndex < 1) {
		d.innerHTML = "Please select a shipping option.";
		lastshipping = -1;
		return false;
	} else {
		d.innerHTML = "";
	}
	var shiptype = form.shipping.selectedIndex -1;

	//get base cost
	shipping = datashipping[shiptype][0];

	//loop through items in reverse order and add price and shipping costs
	for (i = 0; i < items.length; i++) {
		q = form.elements[items[i]].selectedIndex;
		subtotal += q * dataprices[i];
		for (j = 0; j < q; j++) {
			nitems++;
			shipping += getItemShippingCost(nitems,shiptype) * getItemShippingModifier(i,nitems);
		}
	}

	//error checking
	if (nitems == 0) { //there were no items
		shipping = 0;
	}
	lastshipping = shipping;
	lastsubtotal = subtotal;

	//load the values into the document
	document.getElementById("tsubtotal").innerHTML = subtotal;
	document.getElementById("tshipping").innerHTML = shipping;
	document.getElementById("ttotal").innerHTML = subtotal + shipping;
}

function submittopaypal(form) {
	var subtotal = 0;
	var nitems = 0;
	var d = document.getElementById("messages");                  
	d.innerHTML = "Please wait...";
	var url = "https://www.paypal.com/cgi-bin/webscr";
	var query = '?cmd=_cart&upload=1&submit=1'; //items
	query += '&business=mcgrath@therandbindies.com&currency_code=USD'; //personal

	//loop through items in reverse order and add price and shipping costs
	j = 0;
	for (i = 0; i < items.length; i++) {
		q = form.elements[items[i]].selectedIndex;
		if (q > 0) {
			j++;
			subtotal += q * dataprices[i];
			nitems += q;
			query += "&item_name_" + j + "=" + escape(descriptions[i]);
			query += "&amount_" + j + "=" + dataprices[i];
			query += "&quantity_" + j + "=" + q;
		}
	}
	j++;
	var shipping = form.shipping.options[form.shipping.selectedIndex].text;
	query += "&item_name_"+j+"=" + escape("Shipping: " + shipping);
	query += "&amount_"+j+"=0&shipping_"+j+"="+lastshipping;

	if (nitems == 0) { //no items exist
		alert("No items were selected.");
		return false;
	} else if (lastsubtotal != subtotal) { //subtotals don't match
		alert("An error has occured. Please start again.");
		return false;
 	}
 	
	//send data
	//alert(query.length + 37);
	location.href = url + query;
	return false;

}

//private functions ===========
function getItemShippingCost(item,shiptype) {
	if (item >= datashipping[shiptype].length) {
		return datashipping[shiptype][datashipping[shiptype].length-1];
	} else {
		return datashipping[shiptype][item];
	}
}
function getItemShippingModifier(objnum,shiptype) {
	if (shiptype >= datamodifiers[objnum].length) {
		return datamodifiers[objnum][datamodifiers[objnum].length-1];
	} else {
		return datamodifiers[objnum][shiptype];
	}
}

