var imglist = new Array("1-006-1A","1-008-2A","1-010-3A","1-012-4A","1-014-5A","1-016-6A","1-018-7A","1-020-8A","1-022-9A","1-024-10A","1-026-11A","1-028-12A","1-030-13A","1-032-14A","1-034-15A","1-036-16A","1-038-17A","1-040-18A","1-042-19A","1-044-20A","1-046-21A","2-007-2","2-009-3","2-011-4","2-013-5","2-015-6","2-017-7","2-019-8","2-021-9","2-023-10","2-025-11","2-027-12","2-029-13","2-031-14","2-033-15","2-035-16","2-037-17","2-039-18","2-041-19","2-043-20","2-045-21","2-047-22","2-049-23","2-051-24","2-053-25","2-055-26","2-057-27","2-059-28","2-063-30","2-065-31","2-067-32","2-069-33","2-071-34","2-073-35","2-075-36","2-077-37");

var acnt,bcnt,ccnt;

function buildsel(t) {
	var i;
	if (t.value != "") {
		if (isNaN(i=parseInt(t.value)) || i<1 || i>99) {
			alert('Invalid quantity!');
			t.value = "";
			}
		}
	buildselx();
	}
function buildselx() {
	var i,seltext = "";
	acnt = bcnt = ccnt = 0;
	for (i=0;i<imglist.length;i++)
		seltext += buildtext(imglist[i]);
	with (document) {
		getElementById("acnt").innerHTML = acnt;
		getElementById("attl").innerHTML = "$" + (acnt*3);
		getElementById("bcnt").innerHTML = bcnt;
		getElementById("bttl").innerHTML = bcnt*6;
		getElementById("ccnt").innerHTML = ccnt;
		getElementById("cttl").innerHTML = ccnt*12;
		getElementById("tcnt").innerHTML = acnt+bcnt+ccnt;
		getElementById("tttl").innerHTML = "$" + (acnt*3+bcnt*6+ccnt*12);

		getElementById("whole").value = seltext;
		getElementById("maila").href = (seltext == "") ? "" : "mailto:mmckinl607@earthlink.net,rhsphotos@kevinmooresoftware.com?subject=RHS%2079%20Photo%20Request&body=" + escape(seltext);
		setCookie("seltext",seltext);
		}
	}
function buildtext(nm) {
	var s,v,v2,v3;
	s = nm.charAt(0) + nm.substring(2,5);
	with (document.main) {
		v = eval("A" + s + ".value");
		v2 = eval("B" + s + ".value");
		v3 = eval("C" + s + ".value");

		if (v != "" || v2 != "" || v3 !="") {
			s = nm + ":";
			if (v != "") {
				s += " " + v + "-3x5s";
				acnt+=parseInt(v);
				}
			if (v2 != "") {
				s += " " + v2 + "-5x7s";
				bcnt+=parseInt(v2);
				}
			if (v3 != "") {
				s += " " + v3 + "-8x10s";
				ccnt+=parseInt(v3);
				}
			s += "\n";
			}
		else
			s = "";
		}
	return s;
	}
function setquans() {
	var sLines,sTokens,i,j,k,s,sQuan,iC=0;
	for (i=0;i<imglist.length;i++)
		zapone(imglist[i]);
	sLines = document.getElementById("whole").value.split("\n");
	for (i = 0; i < sLines.length; i++) {
		if (sLines[i] != "" ) {
			j = sLines[i].length;
			if (sLines[i].charAt(j-1)!="s") j--;
			sTokens = sLines[i].substring(0,j).split(" ");
			if (sTokens.length > 1 && sTokens[0].charAt(sTokens[0].length-1) == ":") {
				s = sTokens[0].substring(0,sTokens[0].length-1);
				for (j = 0; j < imglist.length && s != imglist[j]; j++);
				if (j < imglist.length) {
					for (j = 1; j < sTokens.length; j++) {
						sQuan = sTokens[j].split("-");
						valid_token:
						if (sQuan.length == 2 && !isNaN(k=parseInt(sQuan[0])) && k>0 && k<100) {
							switch (sQuan[1]) {
								case "3x5s":
									s = "A";
									break;
								case "5x7s":
									s = "B";
									break;
								case "8x10s":
									s = "C";
									break;
								default:
									alert("I don't understand '" + sQuan[1] + "' (item " + j + " in line " + (i+1) + ").");
									break valid_token;
								}
							document.getElementById(s + sTokens[0].charAt(0) + sTokens[0].substring(2,5)).value = sQuan[0];
							iC++;
							}
						else
							alert("I don't understand '" + sTokens[j] + "' (item " + j + " in line " + (i+1) + ").");
						}
					}
				else
					alert("Photo ID '" + s + "' not found (line " + (i+1) + ").");
				}
			else
				alert("No colon after photo ID, bad spacing or no quantities specified in line " + (i+1) + ".");
			}
		}
	buildselx();
	if (iC) {
		alert(iC + " quantities set!"); 
		}
	else {
		if (sLines.length > 1 || sLines[0] !="")
			alert("No quantities set!");
		else
			alert("All quantities cleared.");
		}
	}
function zapone(nm) {
	var s;
	s = nm.charAt(0) + nm.substring(2,5);
	with (document.main) {
		eval("A" + s + ".value=\"\"");
		eval("B" + s + ".value=\"\"");
		eval("C" + s + ".value=\"\"");
		}
	}
function zapall() {
	document.getElementById("whole").value = "";
	setquans();
	}
function setCookie(sName, sValue) {
    var d = new Date ();
	d.setTime(d.getTime() + (365 * 24 * 3600 * 1000));
	document.cookie = sName + "=" + escape(sValue) +  "; expires=" + d.toGMTString();
	}
function getCookie(sName) {
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0] && aCrumb.length > 1)
			return unescape(aCrumb[1]);
		}
	return "";
	}

	var oBody,oRow,oCell,sHTML,i,j,x,sa;
	with (document) {
		oBody = getElementById("stuff");

		for (i=0; i<imglist.length; i+=4) {
			oRow = createElement("TR");
			oBody.appendChild(oRow);
			for (j=0; j < 4 && j + i < imglist.length; j++) {
				s = imglist[i+j];
				oCell = createElement("TD");
				oCell.innerHTML = "<a href=\"images/1710020-R" + s + ".jpg\" target=_blank><img border=0 src=\"images/1710020-R" + s + ".jpg\" height=150></a>";
				oCell.align = "center";
				oRow.appendChild(oCell);
				}
			oRow = createElement("TR");
			oBody.appendChild(oRow);
			for (j=0; j < 4 && j + i < imglist.length; j++) {
				s = imglist[i+j];
				oCell = createElement("TD");
				oCell.innerText = "1710020-R" + s;
				oCell.align = "center";
				oCell.vAlign = "top";
				oRow.appendChild(oCell);
				}
			oRow = createElement("TR");
			oBody.appendChild(oRow);
			for (j=0; j < 4 && j + i < imglist.length; j++) {
				s = imglist[i+j];
				s = s.charAt(0) + s.substring(2,5);
				oCell = createElement("TD");
				sHTML = "<td valign=top align=center><input type=text id=A" + s + " size=2 onchange=\"buildsel(this);\"> 3x5s&nbsp;&nbsp;";
				sHTML += "<input type=text id=B" + s + " size=2 onchange=\"buildsel(this);\"> 5x7s&nbsp;&nbsp;";
				sHTML += "<input type=text id=C" + s + " size=2 onchange=\"buildsel(this);\"> 8x10s";
				oCell.innerHTML = sHTML;
				oCell.align = "center";
				oCell.vAlign = "top";
				oRow.appendChild(oCell);
				}
			oRow = createElement("TR");
			oBody.appendChild(oRow);
			oCell = createElement("TD");
			oCell.innerHTML = "&nbsp;"
			oRow.appendChild(oCell);
			oRow = createElement("TR");
			oBody.appendChild(oRow);
			oCell = createElement("TD");
			oCell.innerHTML = "&nbsp;"
			oRow.appendChild(oCell);
			}
		if ((getElementById("whole").value = getCookie("seltext")) != "")
			setquans();
		}

