﻿var currentAmount = new Array();
var titles = new Array();
var shippingCost = new Array(3,4,6,8);

function boxChecked(itemNumber) {
	if (currentAmount[itemNumber]==0) {
		currentAmount[itemNumber] = 1;
		document.quantities.elements["quantity"+itemNumber].value=1;
	}
	else {
		currentAmount[itemNumber] = 0;
		document.quantities.elements["quantity"+itemNumber].value=0;
	}
}
function checkIfValid(itemNumber) {
	var n = document.quantities.elements["quantity"+itemNumber];
	if (isNaN(n.value)) {
		window.alert(n.value+" is not a valid value");
		n.value=currentAmount[itemNumber];
	}
	else {
		currentAmount[itemNumber] = parseInt(n.value);
		if (n.value>0) {
			document.quantities.elements["visposter"][itemNumber].checked = true;
		}
	}
}
function checkPosters() {
	totalAmount = 0;
	itemString = "";
	for (i=0; i<currentAmount.length; i++) {
		totalAmount += currentAmount[i];
	}
	if (totalAmount % 6 != 0 || totalAmount<=0) {
		window.alert("Total = multiple of 6");
		return false;
	}
	else {
		var countrySelected = -1;
		for (i=0; i<document.countries.country.length; i++) {
			if (document.countries.country[i].checked) {
				countrySelected = i;
			}
		}
		if (countrySelected<0) {
			window.alert("Shipping destination must be selected.");
			return false;
		}
		else {
			
			for (i=0; i<currentAmount.length; i++) {
				if (currentAmount[i]>0) {
					itemString += titles[i] + ": " + currentAmount[i] + " -- ";
				}
			}
			totalSum = 20*totalAmount/6 + shippingCost[countrySelected]*totalAmount/6;
			document.postersPaypal.amount.value = totalSum;
			document.postersPaypal.item_number.value = itemString;
			return true;
		}
	}
}
function uncheckAll() {
	var f = document.quantities.elements["visposter"];
	for (i=0; i<f.length; i++) {
		f[i].checked = false;
		currentAmount[i] = 0;
		titles[i] = f[i].value;
	}
	for (i=0; i<document.countries.country.length; i++) {
		document.countries.country[i].checked = false;
	}

	for (i=0; i<currentAmount.length; i++) {
		document.quantities.elements["quantity"+i].value="0";
	}
}
window.onload=uncheckAll;