function selectItem(i) {
	if (document.addtocart.selection.selectedIndex == 0) {
		document.addtocart.selection.selectedIndex = i;
		}
	else {
		document.addtocart.submit();
	}
}

function confirm() {
	if (document.addtocart.quantity.value == "" || document.addtocart.quantity.value == "0") {
		var a = "problem";
	}
	if (document.addtocart.color.type == "select-one") {
		if (document.addtocart.color.selectedIndex == 0) {
			var a = "problem";
		}
	}
	if (document.addtocart.size.type == "select-one") {
		if (document.addtocart.size.selectedIndex == 0) {
			var a = "problem";
		}
	}
	if (a == "problem") {
		alert ("Please make a selection.");
		}
	else {
		document.addtocart.add.value = "yes";
		document.addtocart.submit();
	}
}

var d;
var folder;
var newImages = new Array();
var newFolders = new Array();
var image = new Array();

function initialize(designerID,mediumString,width,length,j) {
	folder = j;
	var tempArray = mediumString.split(",");
	var	arrayLength = tempArray.length;
	d = arrayLength - 1;
	for (var i = 0; i <= arrayLength - 1; i++) {
		var temp = tempArray[i].split(":");
		newImages[i] = temp[1];
		if (designerID < 10) {
			newFolders[i] = "wa00" + designerID + temp[0];
			}
		else if (designerID < 100) {
			newFolders[i] = "wa0" + designerID + temp[0];
			}
		else {
			newFolders[i] = "wa" + designerID + temp[0];
		}
		image[i] = new Image(width,length);
		image[i].src = "/pictures/" + newFolders[i] + "/" + folder + "/" + newImages[i] + folder + ".jpg";
	}
}

function zoomOut(photoshoot,folder,pictureID){
	if (folder == "line") {
		document.picture.src = "/pictures/" + photoshoot + "/line/" + pictureID + ".jpg";
		}
	else {
		document.picture.src = "/pictures/" + photoshoot + "/" + folder + "/" + pictureID + folder + ".jpg";
	}
}

var g = 0;
function zoomIn(i){
	g = (i == newImages.length-1) ? 1 : g = i + 1;
	document.picture.src = "/pictures/" + newFolders[g] + "/" + folder + "/" +  newImages[g] + folder + ".jpg";
}

var g = 0;
function zoomInAlt(i){
	g = (i == newImages.length-1) ? 0 : g = i + 1;
	document.picture.src = "/pictures/" + newFolders[g] + "/" + folder + "/" +  newImages[g] + folder + ".jpg";
}

//General color select, picture swapping function, the index of the current selection is passed back to the page via the variable d
var c = 0;
function swapImages(i){
	if (i.selectedIndex != 0) {
		c = i.selectedIndex;
		b = c - 1;
		if (newImages[b] == "noPhoto") {
			document.picture.src = "/images/noPhoto.jpg";
			}
		else {
			document.picture.src = "/pictures/" + newFolders[b] + "/" + folder + "/" + newImages[b] + folder + ".jpg";
		}
	}
	d = b;
}

//Same as swap images, but resets the picture to the original when selected index=0
var c = 0;
function swapImagesAlt(i,j,k){
	if (i.selectedIndex != 0) {
		c = i.selectedIndex;
		b = c - 1;
		if (newImages[b] == "noPhoto") {
			document.picture.src = "/images/noPhoto.jpg";
			}
		else {
			document.picture.src = "/pictures/" + newFolders[b] + "/" + folder + "/" + newImages[b] + folder + ".jpg";
		}
	}
	else {
		document.picture.src = "/pictures/" + j + "/" + folder + "/" + k + folder + ".jpg";
	}
	d = b;
}

var mainindex = 0;
function createLists(i,j){
	mainindex = document.addtocart.selection.selectedIndex;
	var re =/^:+$/;
	if (mainindex == 0) {
		if (!re.test(i)) {
			document.addtocart.color.options.length = 0;
			document.addtocart.color.options[0] = new Option("Select a color","0");
		}
		if (!re.test(j)) {
			document.addtocart.size.options.length = 0;
			document.addtocart.size.options[0] = new Option("Select a size","0");
		}
	}
	else {
		if (!re.test(i)) {
			var a = i.split(":");
			document.addtocart.color.options.length = 0;
			document.addtocart.color.options[0] = new Option("Select a color","0");
			if (a[mainindex-1] != "") {
				var c = a[mainindex-1].split(",");
				for (var k = 1; k <= c.length; k++) {
					document.addtocart.color.options[k] = new Option("Color: "+c[k-1],k);
				}
			}
			else {
				document.addtocart.color.options[1] = new Option("Color: as shown","1");
			}
		}
		if (!re.test(j)) {
			var a = j.split(":");
			document.addtocart.size.options.length = 0;
			document.addtocart.size.options[0] = new Option("Select a size","0");
			if (a[mainindex-1] != "") {
				var c = a[mainindex-1].split(",");
				for (var k = 1; k <= c.length; k++) {
					document.addtocart.size.options[k] = new Option("Size: "+c[k-1],k);
				}
			}
			else {
				document.addtocart.size.options[1] = new Option("Size: one size","1");
			}
		}
	}
}

//Swaps images when the color lists are created by createLists; var position is used in popup script
var colorindex = 0;
var position = 0;
function swapImagesMultiAlt(i,j){
	mainindex = document.addtocart.selection.selectedIndex;
	if (mainindex == 0) {
		position = mainindex;
	}
	else {
		var c = j.split(",");
		var d = 0;
		for (var e = 1; e < mainindex; e++) {
			d += parseInt(c[e-1]);
		}
		if (i.name == "selection") {
			position = mainindex+d;
		}
		else {
			if (i.selectedIndex == 0 || i.options[i.selectedIndex].text == "Color: as shown") {
				position = mainindex+d;
			}
			else {
				colorindex = i.selectedIndex;
				position = mainindex+colorindex+d;
			}
		}
	}
	document.picture.src = "/pictures/" + newFolders[position] + "/" + folder + "/" + newImages[position] + folder + ".jpg";
}

//Companion to swapImagesMultiAlt; swaps main image; resets the picture to the original when selected index=0
function swapImagesMainAlt(i,j){
	if (i.selectedIndex != 0) {
		mainindex = i.selectedIndex;
		var c = j.split(",");
		var d = 0;
		for (var e = 1; e < mainindex; e++) {
			d += parseInt(c[e-1]);
		}
		position = mainindex+d;
		document.picture.src = "/pictures/" + newFolders[position] + "/" + folder + "/" + newImages[position] + folder + ".jpg";
	}
	else {
		document.picture.src = "/pictures/" + newFolders[0] + "/" + folder + "/" + newImages[0] + folder + ".jpg";
	}
}
