function qualityHelp() {
	window.open("Quality Help.html", "Quality_Help",
		"width=360, height=220, location=no, menubar=no, status=no titlebar=no, toolbar=no, scrollbars=yes, resizable=no");
}

function debug(text) {
	var debugWindow = window.open();
	debugWindow.document.write( text );
}

function getElement(id) {
	return document.all[id];
}

function dump(obj) {
	var ret = "";
	for (w in obj) {
		ret += (w + " = " + obj[w] + "<br>\n");
	}
	if( ret == "" ) ret = "(null)";
	return ret;
}

function init() {
	for( var i=1; i<ids.length; i++ ) {
		disabled( i, true );
	}
//	disabled( price, true );  //GPZ
//	getElement("date").appendChild( document.createTextNode( revisionDate ) );  //GPZ
}

function choice(e) {
	if( typeof e == "string" ) e = getElement(e);
	if( e.tagName.toLowerCase() == "select" ) {
		return e.options[ e.selectedIndex ].value.toLowerCase();
	}
	else if( (e.tagName.toLowerCase() == "input") && (e.type.toLowerCase() == "text") ) {
		return e.value.toLowerCase();
	}
	return null;
}

function validateSheet() {
	for(var i=0; i<ids.length; i++) {
		if( typeof ids[i] == "object" ) {
			for(var j=0; j<ids[i].length; j++) {
				var e = getElement(ids[i][j]);
				if( ((e.tagName.toLowerCase() == "select") && (e.selectedIndex == 0)) || (e.disabled == true) ) {
//					disabled( price, true );  //GPZ
	getElement("US").value = "";
	getElement("part").value = "";
					return;
				}
				else if( (e.tagName.toLowerCase() == "input") && (e.type.toLowerCase() == "text") && (e.value.length <= 0) ) {
//					disabled( price, true );  //GPZ
	getElement("US").value = "";
	getElement("part").value = "";
					return;
				}
			}
		} else {
			var e = getElement(ids[i]);
			if( ((e.tagName.toLowerCase() == "select") && (e.selectedIndex == 0)) || (e.disabled == true) ) {
//				disabled( price, true );  //GPZ
	getElement("US").value = "";
	getElement("part").value = "";
				return;
			}
			else if( (e.tagName.toLowerCase() == "input") && (e.type.toLowerCase() == "text") && (e.value.length <= 0) ) {
//				disabled( price, true );  //GPZ
	getElement("US").value = "";
	getElement("part").value = "";
				return;
			}
		}
	}
//	disabled( price, false );  //GPZ
	calculatePrice();
}

function disabled(index, bool) {
	var obj;
	if( typeof index == "object" ) {
		obj = index;
	} else {
		obj = ids[index];
	}
	if( typeof obj == "string" ) {
		getElement(obj).disabled = bool;
	} else {
		for( var i=0; i<obj.length; i++ ) {
			getElement(obj[i]).disabled = bool;
		}
	}
}

function isItemDisabled(index) {
	var obj;
	if( typeof index == "object" ) {
		obj = index;
	} else {
		obj = ids[index];
	}

	if( typeof obj == "string" ) {
		return getElement(obj).disabled;
	} else {
		return getElement(obj[0]).disabled;
	}
}

function change() {
	var e = window.event.srcElement;
	if( e.id == "quality" ) {
		getElement("printout").checked = false;
		if( choice(e) == "premium" ) {
			getElement("printoutcell").style.visibility = "visible";
		}
		else {
			getElement("printoutcell").style.visibility = "hidden";
		}
	}
	if( (e.id == "con1") || (e.id == "con2") ) {
		if( (choice(e) == "lcupc") || (choice(e) == "muupc") ) {
			removeItem("jacket", "3mm");
		} else {
			addItem("jacket", "3mm", "3 mm");
		}
	}
	if( ( e.tagName != "SELECT" ) || ( e.selectedIndex > 0 ) ) {
		for( var i=0; i<ids.length; i++ ) {
			if( typeof ids[i] == "string" ) {
				if( e.id == ids[i] ) {
					disabled( i+1, false );
				}
			} else {
				for( var j=0; j<ids[i].length; j++ ) {
					if( e.id == ids[i][j] ) {
						disabled( i+1, false );
					}
				}
			}
		}
	}
	if( e.tagName == "SELECT" ) {
		validateSheet();
	}
}

function fixPrice(price) {
	price*=100;
	Math.round(price);
	price = parseInt(price,10);
	price /= 100;
	return price.toFixed(2);
}

function calculatePrice() {
	var p1 = premium[ choice("con1") ];
	var p2 = premium[ choice("con2") ];
	var length = parseFloat(choice("length")) - 1;

	if( length != parseInt(length) ) {
		length = parseInt(length + 1);
	}
	if( length < 0 ) length = 0;

	var us = p1 + p2 + length*usMeter;
	if( choice("plex") == "duplex" ) {
		us = 3.75 + 2*us;
	}
	us *= specialFactor;
	if( getElement("printout").checked ) {
		us += 2;
	}
	var can = conversionRate * us;

	getElement("US").value = fixPrice(us);
	getElement("part").value = partNumber();
}

function enableCalculate() {
	var fmc = 3.3;
	var l = window.event.srcElement;
	var v;
	var meters, feet;

	if( l.id == "length" ) {
		meters = Math.round(parseFloat( l.value ) * 10 ) / 10;
		feet = Math.round( meters * fmc * 10 ) / 10;
		if( !isNaN(meters) ) {
//			disabled( price, false );  //GPZ
			calculatePrice();
			if( l.value != meters ) {
				l.value = meters;
			}
			getElement("lengthFeet").value = feet;
		} else {
			getElement("lengthFeet").value = "";
//			disabled( price, true );  //GPZ
	getElement("US").value = fixPrice(us);
	getElement("part").value = partNumber();
		}
	} else {
		feet = Math.round( parseFloat(l.value) * 10 ) / 10;
		meters = Math.round( feet / fmc * 10 ) / 10;
		if( !isNaN(feet) ) {
			validateSheet();
			getElement("length").value = meters;
			if( l.value != feet ) {
				l.value = feet;
			}
		} else {
			getElement("length").value = "";
			validateSheet();
		}
	}
}

function removeItem(id, itemName) {
	var e = getElement(id);
	if(!id) return;
	if( e.tagName != "SELECT" ) return;
	var o = e.options;
	for(var i=0; i<o.length; i++) {
		if( o[i].value  == itemName ) {
			o.remove(i);
			return;
		}
	}
}

function addItem(id, value, text) {
	var e = getElement(id);
	if(!id) return;
	if( e.tagName != "SELECT" ) return;
	var o = e.options;
	for(var i=0; i<o.length; i++) {
		if( o[i].value == value ) {
			return;
		}
	}
	var item = document.createElement("option");
	item.name = id;
	item.value = value;
	item.text = text;
	o.add(item);
}

function addToOrder() {
	var bEnabled = true;
	var i, obj, thisDocument, thatDocument, oLength, oLengthFeet;

	thisDocument = window.document;
	thatDocument = top.frameBody.document;

	oLength = getElement("length");
	oLengthFeet = getElement("lengthFeet");

	for( i=1; i<ids.length; i++ ) {
		if( isItemDisabled(i) ) {
			bEnabled = false;

			break;
		}
	}

	if ( !bEnabled ) {
		alert('Please complete the form above before adding this patch cord to your order.');

		return;
	}

	if ( oLengthFeet.value.length == 0
	  || oLength.value.length == 0 ) {
		alert('Please enter a valid length in either meters or feet.');

		return;
	}

	if ( oLengthFeet.value == 0
	  || oLength.value == 0 ) {
		alert('Please enter a length greater than 0 feet/meters.');

		return;
	}

	//	Loop through the frameBody columns to find the first empty one
	for ( i=1; i<=12; i++ ) {
		obj = thatDocument.getElementById("PART_" + i);

		if( obj.value.length == 0 ) {
			break;
		}
	}

	if (i == 13) {
		alert("You've reached the maximum number of patch cords allowed for this order.  Please submit this order and continue on a new one.");

		return;
	}

	//	Add the part number to the column
	thatDocument.getElementById("PART_" + i).value = getElement("part").value;

	//	Set the quantity to 1
	thatDocument.getElementById("QTY_PART" + i).value = 1;

	//	Set the dollar amount
	top.frameBody.window.updatePrice(thatDocument.getElementById("PART_" + i));

	//	Reset the page
	window.location = window.location;
}
