/*

    Script: catalog.js

    Copyright: Fishnet NewMedia (c) 2003

    Create: 08/05/03

    Description:

    Changes:

	    07/27/04 SCD - updated InitForm() to select default option for select lists.
	    10/26/04 MBK - add CreateSpec function for adding/editing user defined product fields
	    07/01/05 MBK - add to RemoveParent() -> turn off virtual store label if top-level category is removed

*/


function PopUp(URL, Name, w, h, a, t) {

	// launches a new popup window with the
	// option to set the window's attributes

	// collect attributes
	var winN = Name;		// set the value of the window's name (no spaces allowed)
	var winW = w;			// set window's width to passed width (if any)
	var winH = h;			// set window's height to passed height (if any)
	var aVal = a;			// set the value of the window's attributes (if any)
	var tVal = t;			// set the value of the window's toolbar

	// set attributes if undefined
	if (!winN) winN = Math.round(Math.random() * 1000000000); // set window name to a random number if undefined
	if (!winW) winW = 400;						// set window width if undefined
	if (!winH) winH = 400;						// set window height if undefined
	if (!aVal) aVal = "yes";					// set window default attribute value
	if (!tVal) tVal = "yes";					// set window default toolbar value

	// open popup window
	var newWin = window.open(URL,winN,'width=' + winW + ',height=' + winH + ',resizable=' + aVal + ',scrollbars=' + aVal + ',toolbar=' + tVal);
	if (newWin) {
		newWin.focus();		     // set focus on the new window
	}
	else {
		alert("Please turn off your Pop-up blocking software.");
	}
}

function OpenCalendar(which, calendar) {

	// opens the calendar application based on
	// the caller forms current month/day/year

	// create references to form date objects
	var DaysObject = document.getElementById(which + "_day");
	var MonthObject = document.getElementById(which + "_month");
	var YearObject = document.getElementById(which + "_year");

	// get values from the month/year objects
	var day = DaysObject.options[DaysObject.selectedIndex].text;
	var month = MonthObject.options[MonthObject.selectedIndex].text;
	var year = YearObject.options[YearObject.selectedIndex].text;

	// open calendar window
	PopUp("calendar.cgi?calendar=" + calendar + "&amp;which=" + which + "&amp;month=" + month + "&amp;day=" + day + "&amp;year=" + year,"Calendar","225","235","no","no");
}

function DaysInMonth(month, year) {

	// set the total number of days in a month
	// for the selected month/year combination

	var DaysInMonth =
		month == "04" || month == "06" || month == "09" || month == "11" ? 30 :
		month == "02" && ((year/4) != Math.floor(year/4)) ? 28 :
		month == "02" && ((year/4) == Math.floor(year/4)) ? 29 :
		31;	// defaults to 31
	return DaysInMonth;
}

function ChangeOptionDays(which) {

	// update the total number of days in the
	// day select list based on the month/year

	// create references to form date objects
	var DaysObject = document.getElementById(which + "_day");
	var MonthObject = document.getElementById(which + "_month");
	var YearObject = document.getElementById(which + "_year");

	// get text label from the month/year objects
	var month = MonthObject.options[MonthObject.selectedIndex].text;
	var year = YearObject.options[YearObject.selectedIndex].text;

	// get number of days based on month/year
	var DaysForThisSelection = DaysInMonth(month, year);

	// get current number of day in day object
	var CurrentDaysInSelection = DaysObject.length - 1;

	// if the current number of days is greater
	// than the total possible days based upon
	// month/year, then remove the extra day
	// options.
	if (CurrentDaysInSelection > DaysForThisSelection) {
		for (i = 0; i < (CurrentDaysInSelection - DaysForThisSelection); i++) {
			DaysObject.options[DaysObject.options.length - 1] = null;
		}
	}

	// if the current number of days is less
	// than the total possible days based upon
	// month/year, then add extra days options
	// to the end of the month object.
	if (DaysForThisSelection > CurrentDaysInSelection) {
		for (i = 0; i < (DaysForThisSelection - CurrentDaysInSelection); i++) {
			NewOption = new Option(DaysObject.options.length);
			DaysObject.options[DaysObject.options.length] = NewOption;
		}
	}

	// if the day object has no selection, set
	// the selection to the first option value
	if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

function PassDate(which, month, day, year) {

	// pass the date values to the opener
	// form and close the calendar window

	// check for opener window
	if (opener) {
		// the opener window object is still open

		// create references to form date objects
		var MonthSelect = opener.document.getElementById(which + "_month");
		var DaySelect = opener.document.getElementById(which + "_day");
		var YearSelect = opener.document.getElementById(which + "_year");

		// because the month and day objects have
		// values that start at one we can select
		// the option based on the objects index
		var MonthOption = eval("MonthSelect.options[" + (month) + "]");
		var DayOption = eval("DaySelect.options[" + (day) + "]");

		// because the years option values are
		// unpredictable, we have to loop through
		// each options value to match the year to
		// the index of the matching option value
		var YearOption = "";
		for (var i = 0; i < YearSelect.length; i++) {
			if (YearSelect.options[i].text == year) YearOption = YearSelect.options[i];
		}

		// set the date objects' selected option
		MonthOption.selected = true;
		DayOption.selected = true;
		YearOption.selected = true;

		// close the calendar window
		self.close();
	}
	else alert("An error occured while attempting to pass the date information to the form (error: opener window not found)");
}

function ManageInput(event,type,id,price,style) {

	// product option table:
	// onfocus:
	//    clears price input field of default
	//    price and sets the fields class name
	// onblur:
	//    checks to see if input value is empty
	//    or equal to the default price and sets
	//    the field value and field class name

	// create reference to form element
	var elm = document.getElementById(type + id);

	// value of the input field - price
	var po_price = elm.value;

	// remove leading spaces
	while(po_price.charCodeAt(0) == 32 || po_price.charCodeAt(0) == 160) po_price = po_price.substr(1);

	if (event == "onfocus") {
		// if the input field's value is equal to
		// the default price, clear the input field
		// and update the field class name
		if (po_price == price) {
			elm.value = "";
			elm.className = "product-options-" + style + "input";
		}
	}
	else if (event == "onblur") {
		// if the input field's value is empty, set
		// the input value to the default price and
		// update the field class name.
		// if the input field's value is equal to
		// the default price, clear the input field
		// and update the field class name.
		// base price only: if the input field's
		// value is emtpy, and there is no default
		// price available, set the orderable select
		// list to "N".
		if (price != "") {
			if (po_price == "" || isNaN(parseFloat(po_price)) || parseFloat(po_price) == parseFloat(price)) {
				elm.value = price;
				elm.className = "product-options-" + style + "default";
			}
		}
		else {
			if (type == "price") {
				elm = document.getElementById("order_ind" + id);
				if (po_price == "" || isNaN(parseFloat(po_price))) {
					if (elm.length > 1) {
						// remove "Yes" option from
						// the orderable select list
						OPTION = elm.options[1];
						elm.removeChild(OPTION);
						// enable the message field
						elm = document.getElementById("out_stock_msg" + id);
						elm.disabled = 0;
						var style = elm.className.indexOf("even") != -1 ? "even" : "odd";
						elm.className = "product-options-" + style + "input";
					}
				}
				else {
					if (elm.length == 1) {
						// add the "Yes" option to
						// the orderable select list
						OPTION = document.createElement("OPTION");
						OPTION.value = "A";
						OPTION.className = "yes";
						TEXT = document.createTextNode("Yes");
						OPTION.appendChild(TEXT);
						elm.appendChild(OPTION);
					}
				}
			}
		}
	}
}

function ToggleInput(id) {

	// product option table:
	// enables or disables an option's message text
	// field base on its orderable property status

	// create reference to form element
	var elm = document.getElementById("out_stock_msg" + id);

	if (document.getElementById("order_ind" + id).selectedIndex == "1") {
		// orderable = Yes
		elm.disabled = 1;
		elm.value = "";
		// change the form elements classname
		var style = elm.className.indexOf("even") != -1 ? "even" : "odd";
		elm.className = "product-options-" + style + "disabled";
	}
	else {
		// orderable = No
		elm.disabled = 0;
		// change the form elements classname
		var style = elm.className.indexOf("even") != -1 ? "even" : "odd";
		elm.className = "product-options-" + style + "input";
	}
}

function ClearDefaults() {

	// product option table:
	// clears out all default price values from
	// input fields prior to submitting the form

	// create reference to form element
	var elm = document.maint.elements;

	// clear all default price inputs
	for (var i = 0; i < elm.length; i++) {
		if (elm[i].className.indexOf("default") != -1) elm[i].value = "";
	}

	// submit form
	document.maint.submit();
}

function ToggleButton(name) {

	// product option table:
	// enables or disables a button
	// based on custom instructions

	// create reference to form element
	var elm = document.maint.elements;

	// CLEAR: product option table
	if (name == "reset") {
		// skip through form elements and check for
		// at least one actively checked checkboxes
		var checked = 0;
		for (var i = 0; i < elm.length; i++) {
			if (elm[i].type == "checkbox" && elm[i].checked) {
				// at least one checkbox has been checked
				// set checked flag to true and exit loop
				checked = 1;
				break;
			}
		}

		// update status based on checked flag
		if (checked) {
			// enabled button
			var button = document.getElementsByName(name + "-button");		// set reference to image element
			// enable the button if it is currently enabled
			if (button.item(0).src.indexOf('disabled') != -1) {
				button.item(0).src = "images/" + name + ".gif";				// update image's source
				var link = document.getElementsByName(name + "-link");		// set reference to link element
				link.item(0).href = "javascript:ResetFields()";				// update alink's href
			}
		}
		else {
			// disabled button
			var button = document.getElementsByName(name + "-button");		// set reference to image
			// disable the button if it is currently enabled
			if (button.item(0).src.indexOf('disabled') == -1) {
				button.item(0).src = "images/" + name + "-disabled.gif";	// update image's source
				var link = document.getElementsByName(name + "-link");		// set reference to link element
				var href = link.item(0).getAttributeNode("href");			// set reference to link's href node
				link.item(0).removeAttributeNode(href);						// remove link's href node
			}
		}
	}

	// UPDATE: product option table
	if (name == "update") {
		var button = document.getElementsByName(name + "-button");		// set reference to image element
		// enable the button if it is currently disabled
		if (button.item(0).src.indexOf('disabled') != -1) {
			button.item(0).src = "images/" + name + ".gif";				// update image's source
			var link = document.getElementsByName(name + "-link");		// set reference to link element
			link.item(0).href = "javascript:ClearDefaults();";			// update alink's href
		}
	}
}

function ResetFields() {

	// product option table:
	// empties all text input field values and
	// sets select lists to the first menu item

	var message = '------------------- ALERT! -------------------\n\n';
	message += 'Click "OK" to reset all values for checked\n';
	message += 'properties on the product options table.\n\n';
	message += 'Click "Cancel" to return to the product\n';
	message += 'options table without changes.\n';

	if (confirm(message)) {

		// array of field names
		var ids = new Array();
		ids[0] = "option_number";
		ids[1] = "price";
		ids[2] = "sale_price";
		ids[3] = "quantity_price";
		ids[4] = "out_stock_msg";
		ids[5] = "order_ind";

		// default price values
		var default_price = document.getElementById("default_price").value;
		var default_sale_price = document.getElementById("default_sale_price").value;
		var default_quantity_price = document.getElementById("default_quantity_price").value;

        // loop through each property
		for (var elm_count = 0; elm_count < ids.length; elm_count++) {
			// check to see if the property is to be reset
			if (document.getElementById(ids[elm_count]) && document.getElementById(ids[elm_count]).checked) {
				// reset property
				for (var po_count = 1; document.getElementById(ids[elm_count] + po_count); po_count++) {
					// create reference to property's form element
					var elm = document.getElementById(ids[elm_count] + po_count);
					if (elm.type == "text") {
						// form element is a text field
						var style = elm.className.indexOf("even") != -1 ? "even" : "odd";
						if (ids[elm_count] == "out_stock_msg") {
							if (document.getElementById("order_ind" + po_count).selectedIndex == 0) {
								// delete message
								elm.value = "";
								// change the form elements classname
								elm.className = "product-options-" + style + "input";
							}
							else {
								// delete message
								elm.value = "";
								// change the form elements classname
								elm.className = "product-options-" + style + "disabled";
							}
						}
						else {
							// reset the prices to the default price
							elm.value =
								ids[elm_count] == "price" ? default_price :
								ids[elm_count] == "sale_price" ? default_sale_price :
								ids[elm_count] == "quantity_price" ? default_quantity_price :
								"";

							// change the form elements classname
							elm.className =
								ids[elm_count] == "price" ? "product-options-" + style + "default" :
								ids[elm_count] == "sale_price" ? "product-options-" + style + "default" :
								ids[elm_count] == "quantity_price" ? "product-options-" + style + "default" :
								ids[elm_count] == "quantity_price" ? "product-options-" + style + "default" :
								"product-options-" + style + "input";
						}
					}
					else if (elm.type == "select-one") {
						// form element is a select list
						var style = elm.className.indexOf("even") != -1 ? "even" : "odd";
						if (ids[elm_count] == "order_ind") {
						    // set the option's orderable property
							if (document.getElementById("price" + po_count)) {
								// set the option's orderable property
								// to "Yes" if the option's price is set,
								// otherwise set the option's orderable
								// property to "No"
								var price = document.getElementById("price" + po_count).value;
								if (price != "") {
									// the price has been set
									// orderable = Yes
									elm.selectedIndex = 1;
									 // delete message
									document.getElementById("out_stock_msg" + po_count).value = "";
									// change the form elements classname
									document.getElementById("out_stock_msg" + po_count).className = "product-options-" + style + "disabled";
								}
								else {
									// the price has not been set
									// orderable = No
									elm.selectedIndex = 0;
									// change the form elements classname
									document.getElementById("out_stock_msg" + po_count).className = "product-options-" + style + "input";
								}
							}
							else{
								// the price fields have been disabled
								// orderable = Yes
								elm.selectedIndex = 1;
								// delete message
								document.getElementById("out_stock_msg" + po_count).value = "";
								// change the form elements classname
								document.getElementById("out_stock_msg" + po_count).className = "product-options-" + style + "disabled";
							}
						}
					}
				}
				// uncheck the checkbox
				document.getElementById(ids[elm_count]).checked = 0;
			}
		}

		// disable reset button
		ToggleButton("reset");

		// enable update button if changes
		// to the form data has been made
		ToggleButton("update");
	}
}

function CheckForm() {

	// product option table:
	// skips through each form element and compares
	// the default value, the value assigned to it
	// on load, with the current value. If changes
	// are found, the script returns true.

	// create reference to form element
	var elm = document.maint.elements;

	// initialize change boolean
	var changes = 0;

	// skip through form elements and compare
	// current input value with default value
	for (var i = 0; i < elm.length; i++) {
		if (elm[i].type == "text") {
			if (elm[i].value != elm[i].defaultValue) {
				changes = 1;	// changes have been made
			}
		}
		else if (elm[i].type == "select-one") {
			for (var j = 0; j < elm[i].length; i++) {
				if (!elm[i].options[elm[i].options.selectedIndex].defaultSelected) {
					changes = 1;	// changes have been made
				}
			}
		}
	}

	// return boolean
	return changes;
}

function CountOptions(max_product_options,enable_price) {

	// Product form table:
	// Checks to see if the total number of product option combinations
	// exceeds the max allowed per product. Only options whose "Include
	// this option on the Manage Product Options form" checkbox is check
	// will be counted if the EnablePrice configuration is set to "Y",
	// otherwise all product options will be included.

	max_product_options = max_product_options ? max_product_options : 300;
	enable_price = enable_price ? enable_price : "Y";

	// total option combinations
	var total_options = 0;
	for (var i = 1; i <= 4; i++) {
		var option_element = document.getElementById("option_elementA" + i);
		if (option_element) {
			var total_values = 0;
			var checkbox_image = document.getElementById("checkbox_image" + i);
			if (checkbox_image.className == "checked" || enable_price == "N") {
				for (var j = 1; j; j++) {
					var option_value = document.getElementById("option" + i + "_value" + j);
					if (option_value) {
						option_value = option_value.value;
						// remove leading spaces
						while(option_value.charCodeAt(0) == 32 || option_value.charCodeAt(0) == 160) option_value = option_value.substr(1);
						// increment option count
						// if value is not empty
						if (option_value) total_values++;
					}
					else break;
				}
				// calculate total option combinations
				if (total_values) {
					total_options = total_options ? total_options * total_values : total_values;
				}
			}
		}
		else break;
	}

	var message = '------------------------- ALERT! -------------------------\n\n';
	message += 'The total number of product option combinations\n';
	message += 'cannot exceed ' + max_product_options + '. ';
	if (enable_price == "N") {
		message += 'It is recommended that you\n';
		message += 'remove one or more of the product\'s options or\n';
		message += 'decrease the total number of values within one\n';
		message += 'or more of the product options.\n\n';
	}
	else {
		message += 'It is recommended that you\n';
		message += 'exclude one or more of the product options from\n';
		message += 'the Manage Product Options form. You can do this\n';
		message += 'by unchecking the box next to the "Include this\n';
		message += 'option on the Manage Product Option form" for\n';
		message += 'options that do not require customization of price,\n';
		message += 'option code, stock message or orderable status.\n\n';
	}
	message += 'Click "OK" to return to the form to make changes.\n\n';
	message += 'Total option combinations for this product: ' + total_options;

    // verify that the total number of product options does
    // not exceed max number of product option combinations
	if (total_options > max_product_options) alert (message);
	else return true;

	// return false by default
	return false;
}

function Skip() {

	// product option table:
	// Activated by the "Skip" button
	// Checks for changes to the product option
	// table form. If changes are found, prompt
	// the user for confirmation.

	var message = '-------------------- ALERT! --------------------\n\n';
	message += 'Changes have been made to one or more\n';
	message += 'product option properties.\n\n';
	message += 'Click "OK" to skip product option updates.\n';
	message += 'Please note that all changes made to the\n';
	message += 'product options table will be lost.\n\n';
	message += 'Click "Cancel" to return to the product\n';
	message += 'options tables and click the "Update"\n';
	message += 'button to save the changes.\n';

	if (CheckForm()) {
		// form changes have been made
		// prompt user for confirmation
		if (confirm(message)) location.href = location.href;	// return to product maint
	}
	else location.href = location.href;	// return to product maint
}

function CheckBox() {
	// checks or unchecks all form checkboxes

	// create reference to checkbox image
	var elm = document.getElementById("checkbox");

	// get checkboxes status based
	// upon the image's class name
	var check_status = elm.className;

	// create reference to form element
	var elm_array = document.results.elements;

	// loop through each form element and
	// check or uncheck checkbox elements
	for (var i = 0; i < elm_array.length; i++) {
		if (elm_array[i].type == "checkbox") {
			if (check_status == "unchecked") elm_array[i].checked = 1;
			else elm_array[i].checked = 0;
		}
	}

	// update the image's class name
	if (check_status == "unchecked") {
		elm.className = "checked";
		elm.title = "Uncheck all boxes";
	}
	else {
		elm.className = "unchecked";
		elm.title = "Check all boxes";
	}
}

function ToggleCheckbox(option_number) {
	// checks or unchecks an product option image checkbox
	// this set the product option's manageable flag (Y/N)

	// get flag to see if product options exist for this product
	var options_flag = document.getElementById("options_flag").value;

	// create reference to the checkbox image element
	var img = document.getElementById("checkbox_image" + option_number);
	var elm = document.getElementById("manage_option" + option_number);

	// require confirmation only if an option currently exist for
	// the product, and the options checkbox is currently checked
	var checkbox_image = document.getElementById("checkbox_image" + option_number).className;
	var options_flag = document.getElementById("options_flag").value;
	var require_confirm = options_flag == "Y" ? "Y" : "N";

	if (img.className == "checked") {
		var message = '----------------------- ALERT! -----------------------\n\n';
		message += 'Setting an option to be excluded on the Manage\n';
		message += 'Product Options form will delete the option code,\n';
		message += 'price variations, stock message and orderable\n';
		message += 'status of existing options for this product.\n\n';
		message += 'Click "OK" to exclude this option on the Manange\n';
		message += 'Product Options form.\n';
		message += 'Click "Cancel" to cancel your request.\n';
		if (require_confirm == "N" || confirm(message)) {
			// set flag to delete current product
			// option records when saving product
			if (require_confirm == "Y") document.getElementById("delete_options").value = 'Y';
			// display unchecked image
			img.src = "images/checkbox_unchecked.gif";
			img.className = "unchecked";
			// set the hidden input to no
			elm.value = "N";
		}
	}
	else{
		var message = '----------------------- ALERT! -----------------------\n\n';
		message += 'Setting an option to be included on the Manage\n';
		message += 'Product Options form will delete the option code,\n';
		message += 'price variations, stock message and orderable\n';
		message += 'status of existing options for this product.\n\n';
		message += 'Click "OK" to include this option on the Manange\n';
		message += 'Product Options form.\n';
		message += 'Click "Cancel" to cancel your request.\n';
		if (require_confirm == "N" || confirm(message)) {
			// set flag to delete current product
			// option records when saving product
			if (require_confirm == "Y") document.getElementById("delete_options").value = 'Y';
			// display checked image
			img.src = "images/checkbox_checked.gif";
			img.className = "checked";
			// set the hidden input to yes
			elm.value = "Y";
		}
	}
}

function CheckProduct(product_id,product_number,product_name) {
	// preset vars
	var match = 0;

	// created an array related product ids
	var ids = opener.document.maint.related_product_ids.value.split(",");

	// loop through each of the related product ids
	for (var index in ids) {
		// compare product id with with select
		// product's id to prevent duplicates
		if (product_id == ids[index]) {
			// a match was found
			match = 1;
			// exit the for loop
			break;
		}
	}

	// check for a matched product id or pass the related products HTML to the form
	if (match) {
		alert("\"" + product_name + "\" has already been selected as a related product");
		return false;
	}
	else {
		if (opener) {
			opener.AddProduct(product_id,product_number,product_name);
			return true;
		}
		else alert("An error occured while attempting to pass the related product information to the form (error: opener window not found)");
	}
}

function AddProduct(product_id,product_number,product_name) {

	// add the product id to the related_product_ids field
	// additional product ids will be preceeded by a comma
	document.maint.related_product_ids.value += document.maint.related_product_ids.value ? "," + product_id : product_id;

	// get related products parent container
	var parent_element = document.getElementById("products_parent");

	// get total number of related products
	var products = parent_element.childNodes.length;

	// clear message
	document.getElementById("no_products").style.display = 'none';

	// create new related product HTML
	TABLE = document.createElement("TABLE");
	TABLE.cellPadding = "0";
	TABLE.cellSpacing = "0";
	TABLE.className = "related-product-table";
	TABLE.id = "product" + product_id;
	TBODY = document.createElement("TBODY");
	TR = document.createElement("TR");
	TR.className = "related-product-row";
	TD = document.createElement("TD");
	TD.className = "related-product-cell";
	A = document.createElement("A");
	A.href = "javascript: RemoveProduct('" + product_id + "')";
	IMG = document.createElement("IMG");
	IMG.src = "images/remove_icon.gif";
	IMG.className = "action-image";
	IMG.title = "Remove this product";
	A.appendChild(IMG);
	TD.appendChild(A);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = "related-product-cell";
	TD.noWrap = "true";
	B = document.createElement("B");
	TEXT = document.createTextNode(product_name);
	B.appendChild(TEXT);
	TD.appendChild(B);
	TEXT = document.createTextNode(" (" + product_number + ") ");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);
	TABLE.appendChild(TBODY);
	parent_element.appendChild(TABLE);
}

function RemoveProduct(product_id) {
	// created an array related product ids
	var ids = document.maint.related_product_ids.value.split(",");

	// loop through each of the related product ids
	for (var i in ids) {
		// locate a product id match
		if (product_id == ids[i]) {
			// remove the id from the related product ids array
			ids.splice(i,1);
			// update the related product ids form field
			document.maint.related_product_ids.value = ids.join(",");
			// exit the for loop
			break;
		}
	}

	// get parent element
	var parent_element = document.getElementById("products_parent");

	// get child element
	var child_element = document.getElementById("product" + product_id);

	// get total number of related products
	var products = parent_element.childNodes.length;

	// remove related product
	parent_element.removeChild(child_element);
	products--;

	// display message if no related products exist
	if (!products) document.getElementById("no_products").style.display = 'block';
}

function RemoveParent(category_id) {
	// created an array of parent category ids
	var ids = document.maint.parent_category_ids.value.split(",");

	// loop through each of the parent category ids
	for (var i in ids) {
		// locate a product id match
		if (category_id == ids[i]) {
			// remove the id from the related product ids array
			ids.splice(i,1);
			// update the related product ids form field
			document.maint.parent_category_ids.value = ids.join(",");
			// exit the for loop
			break;
		}
	}

	// get parent element
	var parent_element = document.getElementById("parent_category");

	// get child element
	var child_element = document.getElementById("category" + category_id);

	// remove related product
	parent_element.removeChild(child_element);

	// turn off virtual store label if top-level category is removed
	if (category_id == "NULL") {
		if (document.getElementById &&
			document.getElementById('virtual_store') &&
			document.getElementById('virtual_store').style) {
				document.getElementById('virtual_store').style.display='none';
		}
	}
}

function CategorySort(category_id) {
	// open up popup window for sorting subcategories or products
	which = document.sort.Action.type.toLowerCase() == "hidden" ? document.sort.Action.value : document.sort.Action.options[document.sort.Action.selectedIndex].value;
	PopUp("category_sort.cgi?category_id=" + category_id + "&Action=Sort" + "&which=" + which, "sort","500","500","yes","no");
}

function InitForm() {

	// check and store prechecked radio button
	// initialize form input fields and labels
	// disables all billing method form fields
	// disables all billing method form labels

	var method = "";	// preset the default billing method

	// checked to see if a radio button is prechecked
	for (var i = 0; i < document.checkout.bill_method.length; i++) {
		if (document.checkout.bill_method[i].checked)
			method = document.checkout.bill_method[i].value;
	}

	// disable text and select form elements
	// unless corresponding radio is checked
	for (var i = 0; i < document.checkout.elements.length; i++) {
		if (document.checkout.elements[i].className.substr(8) != method) {
			if (document.checkout.elements[i].type == "text") {
				document.checkout.elements[i].disabled = 1;
				document.checkout.elements[i].style.background = "#EEEEEE";
				document.checkout.elements[i].value = "";
			}
			else if (document.checkout.elements[i].type == "select-one") {
				document.checkout.elements[i].disabled = 1;
				document.checkout.elements[i].style.background = "#EEEEEE";
				// reset select list to the default option if an
				// option has been preset to been selected by default
				var default_selected = 0;
				for (var j = 0; j < document.checkout.elements[i].options.length; j++) {
					if (document.checkout.elements[i].options[j].defaultSelected) {
						// set default option
						document.checkout.elements[i].selectedIndex = j;
						// set default flag
						default_selected = 1;
						// exit the for loop
						break;
					}
				}
				// default the first option if no default selected is found
				if (!default_selected) document.checkout.elements[i].selectedIndex = 0;
			}
		}
	}

	// gray-out all form field labels
	var  DIVs = document.getElementsByTagName("DIV");
	for (var i = 0; i < DIVs.length; i++) {
		if (DIVs[i].className.substr(8) != method) {
			if (DIVs[i].className.substr(0,8) == "payment_") {
				DIVs[i].style.color = "#999999";
			}
		}
	}
}

function ManageForm(radio) {

	// resets payment information form fields
	// reinitializes forms input field labels
	// activates active billing method fields
	// activates active billing method labels

	radio.checked = 1;									// check radion button
	InitForm();											// reinitialize form
	var DIVs = document.getElementsByTagName("DIV");	// get all DIV elements
	for (var j = 0; j < DIVs.length; j++) {				// enable field labels
		if (DIVs[j].className.charAt(8) == radio.value)
			DIVs[j].style.color = "";
	}
	for (var i = 0; i < document.checkout.elements.length; i++) {	// enable form fields
		if (document.checkout.elements[i].className.substr(8) == radio.value) {
			document.checkout.elements[i].disabled = 0;
			document.checkout.elements[i].style.background = "#FFFFFF";
		}
	}
}

function ConfirmAction(elm) {

	// confirms a user submitted action before
	// sending the form and data to the server

	var Action = "";

	if (elm.tagName.toLowerCase() == "form") {
		if (elm.Action.type == "hidden") Action = elm.Action.value.toLowerCase();
		else Action = elm.Action.options[elm.Action.selectedIndex].value.toLowerCase();
	}
	else Action = elm.name.toLowerCase();

	if (Action == "moveproducts" || Action == "copyproducts") {
		if (elm.to_category_id.options[elm.to_category_id.selectedIndex].value) {
			var to_category_id = elm.to_category_id.options[elm.to_category_id.selectedIndex].value;
			var to_category_name = elm.to_category_id.options[elm.to_category_id.selectedIndex].text;
			var from_category_id = elm.category_id.value;
			var from_category_name = elm.category_name.value;

			if (to_category_id != from_category_id) {
				// remove leading spaces
				while(to_category_name.charCodeAt(0) == 32 || to_category_name.charCodeAt(0) == 160) to_category_name = to_category_name.substr(1);
				msg = "Click \"OK\" to " + Action.substr(0,4) + " all products\n________________________\n\nFrom: " +
					from_category_name + "\nTo: " + to_category_name + "\n________________________";
				if (confirm(msg)) return true;
				else return false;
			}
			else {	// same category
				alert("Please selected a different category to " + Action.substr(0,4) + " products to.");
				return false;
			}
		}
		else {	// no category selected
			alert("Please selected a category to " + Action.substr(0,4) + " products to.");
			return false;
		}
	}

	else if (Action == "activate") {
		var where = "";
		var which = "";
		if (elm.where.type == "hidden") where = elm.where.value.toLowerCase();
		else where = elm.where.options[elm.where.selectedIndex].text;
		if (elm.which.type == "hidden") which = elm.which.value.toLowerCase();
		else which = elm.which.options[elm.which.selectedIndex].text;
		var msg = "Click \"OK\" to " + Action + " all staging " + which + " in " + where + ".";
		if (confirm(msg)) return true;
		else return false;
	}

	else if (Action == "printorders") {
		// check to see if orders were selected
		var match = 0;
		if (elm.order_id.length) {
			// the form contains more than one order id check box
			for (var i = 0; i < elm.order_id.length; i++) {
				if (elm.order_id[i].checked) {
					// at least one order was selected
					// set match flag and exit the loop
					match = 1;
					break;
				}
			}
		}
		else{	// the form contains only one order id check box
			if (elm.order_id.checked) match = 1;
		}

		// print the selected orders
		if (match) {
			// submit the form to a new window
			elm.target = "_blank";
			return true;
		}
		else {	// no orders select, alert the user
			alert("Please check the orders you would like printed.");
			return false;
		}
	}

	else if (Action == "deleteshipping") {
		if (confirm("WARNING!!! Click \"OK\" to permanently delete this shipping method.")) return true;
		else return false;
	}

	else if (Action == "deletesupport") {
		if (confirm("WARNING!!! Click \"OK\" to permanently delete this support page.")) return true;
		else return false;
	}

	else if (Action == "deletecategory") {
			var message = '------------------------ ALERT! ------------------------\n\n';
			message += 'Deleting this category will delete all products and\n';
			message += 'categories within this category unless the product\n';
			message += 'or category is assigned to another category.\n\n';
			message += 'Click "OK" to delete this category.\n';
			message += 'Click "Cancel" to cancel your request.\n';
		if (confirm(message)) return true;
		else return false;
	}

	else if (Action == "deleteproduct") {
		if (confirm("WARNING!!! Click \"OK\" to permanently delete this product.")) return true;
		else return false;
	}

	else if (Action == "deleteproducts" || Action == "activateproducts" || Action == "deactivateproducts") {
		var match = 0;
		if (elm.product_id.length) {
			// the form contains more than one product id check box
			for (var i = 0; i < elm.product_id.length; i++) {
				if (elm.product_id[i].checked) {
					// at least one product was selected
					// set match flag and exit the loop
					match = 1;
					break;
				}
			}
		}
		else{	// the form contains only one product id check box
			if (elm.product_id.checked) match = 1;
		}
		if (match) {
			if (Action == "deleteproducts") var msg = "WARNING!!! Click \"OK\" to permanently delete all checked products.";
			else if (Action == "activateproducts") var msg = "WARNING!!! Click \"OK\" to activate all checked products.";
			else if (Action == "deactivateproducts") var msg = "WARNING!!! Click \"OK\" to deactivate all checked products.";
			if (confirm(msg)) return true;
			else return false;
		}
		else {	// no products select, alert the user
			if (Action == "deleteproducts") var msg = "Please check the products you would like deleted.";
			else if (Action == "activateproducts") var msg = "Please check the products you would like activated.";
			else if (Action == "deactivateproducts") var msg = "Please check the products you would like deactivated.";
			alert(msg);
			return false;
		}
	}

	else if (Action == "deletepromo") {
		if (confirm("WARNING!!! Click \"OK\" to permanently delete this promotion.")) return true;
		else return false;
	}

	else if (Action == "deletepromos" || Action == "activatepromos" || Action == "deactivatepromos") {
		var match = 0;
		if (elm.promotion_id.length) {
			// the form contains more than one promo id check box
			for (var i = 0; i < elm.promotion_id.length; i++) {
				if (elm.promotion_id[i].checked) {
					// at least one promotion was selected
					// set match flag and exit the loop
					match = 1;
					break;
				}
			}
		}
		else{	// the form contains only one promotion id check box
			if (elm.promotion_id.checked) match = 1;
		}
		if (match) {
			if (Action == "deletepromos") var msg = "WARNING!!! Click \"OK\" to permanently delete all checked promotions.";
			else if (Action == "activatepromos") var msg = "WARNING!!! Click \"OK\" to activate all checked promotions.";
			else if (Action == "deactivatepromos") var msg = "WARNING!!! Click \"OK\" to deactivate all checked promotions.";
			if (confirm(msg)) return true;
			else return false;
		}
		else {	// no promotion select, alert the user
			if (Action == "deletepromos") var msg = "Please check the promotions you would like deleted.";
			else if (Action == "activatepromos") var msg = "Please check the promotions you would like activated.";
			else if (Action == "deactivatepromos") var msg = "Please check the promotions you would like deactivated.";
			alert(msg);
			return false;
		}
	}

	else if (Action == "updateorder") {
		var status_code = "";
		var order_status= "";
		if (elm.status) {
			status_code = elm.status.options[elm.status.selectedIndex].value;
			order_status = elm.status.options[elm.status.selectedIndex].text;
		}
		if (status_code == "S") {
			var message = '------------------------ ALERT! ------------------------\n\n';
			message += 'You are about to change the status of this order\n';
			message += 'to "' + order_status + '." All but the last four numbers of\n';
			message += 'the credit cards will be permanently removed \n';
			message += 'and the status cannot be changed.\n\n';
			message += 'Click "OK" to continue.\n';
			message += 'Click "Cancel" to cancel your request.\n';
			if (confirm(message)) return true;
			else return false;
		}
		else return true;
	}

	else if (Action == "printorders" || Action == "changestatus-p" || Action == "changestatus-s") {
		// check to see if orders were selected
		var match = 0;
		if (elm.order_id.length) {
			// the form contains more than one order id check box
			for (var i = 0; i < elm.order_id.length; i++) {
				if (elm.order_id[i].checked) {
					// at least one order was selected
					// set match flag and exit the loop
					match = 1;
					break;
				}
			}
		}
		else{
			// the form contains only one order id check box
			if (elm.order_id.checked) match = 1;
		}

		if (Action == "printorders") {
			// print the selected orders
			if (match) {
				// submit the form to a new window
				elm.target = "_blank";
				return true;
			}
			else {	// no orders select, alert the user
				alert("Please check the orders you would like printed.");
				return false;
			}
		}
		else {
			// change the status of selected orders
			if (match) {
				var order_status = elm.Action.options[elm.Action.selectedIndex].text;
				var message = '--------------------- ALERT! ---------------------\n\n';
				if (Action == "changestatus-s") {
					message += 'You are about the change the status of all\n';
					message += 'checked orders to "' + order_status + '." All but the\n';
					message += 'last four numbers of the credit cards will\n';
					message += 'be permanently removed and the status\n';
					message += 'cannot be changed.\n\n';
				}
				else {
					message += 'You are about the change the status of all\n';
					message += 'checked orders to "' + order_status + '."\n\n';
				}
				message += 'Click "OK" to continue.\n';
				message += 'Click "Cancel" to cancel your request.\n';
				if (confirm(message)) {
					// submit the form to the current window
					elm.target = "_self";
					return true;
				}
				else return false;
			}
			else {	// no orders select, alert the user
				alert("Please check the orders you would like updated.");
				return false;
			}
		}
	}

	return false;
}

function EditCategory(elm) {
	// get a list of all TD elements
	var TDs = document.getElementsByTagName("TD");
	// loop through each TD element and set the background color
	for (var i = 0; i < TDs.length; i++) {
		if (TDs[i].className && TDs[i].className.substr(0,3) == "cat") {
			TDs[i].style.backgroundColor = TDs[i].className == elm.className ? "#D1E1F0" : "";
		}
		else if (TDs[i].className && TDs[i].className == "NULL") {
			TDs[i].style.backgroundColor = TDs[i].className == elm.className ? "#D1E1F0" : "";
		}
	}
	// go to edit form
	if (parent.maint) {
		if (elm.className == "NULL") parent.maint.location = "category_maint.cgi?Action=EditCategory&category_id=NULL";
		else if(elm.className) parent.maint.location = "category_maint.cgi?Action=EditCategory&category_id=" + elm.className.substr(3);
	}
}

function SelectCategory(elm) {
	// preset vars
	var match = 0;
	var index = 0;

	// get a list of all TD elements
	var TDs = document.getElementsByTagName("TD");
	// loop through each TD element and set the background color
	for (var i = 0; i < TDs.length; i++) {
		if (elm) {
			if (TDs[i].className == elm.className) {
				TDs[i].style.backgroundColor = TDs[i].style.backgroundColor == "" ? "#D1E1F0" : "";
			}
		}
		else TDs[i].style.backgroundColor = "";
	}
	// add category id to ids array
	if (elm) {
		while (index < ids.length) {
			if (elm.className == ids[index]) {	// NULL
				match = 1;
				break;
			}
			else if (elm.className.substr(3) == ids[index]) {
				match = 1;
				break;
			}
			else index++
		}
		if (match) ids.splice(index,1);
		else {
			if (elm.className == "NULL") ids.unshift("NULL");
			else ids.push(elm.className.substr(3));
		}
	}
}

function ToggleVisibility(div_id,img_id) {
	// display div
	DIV = document.getElementById(div_id);
	DIV.style.display = DIV.style.display == 'none' ? 'block' : 'none';
	// change image
	if (img_id) {
		IMG = document.getElementById(img_id);
		IMG.className = IMG.className == 'hide-large' ? 'show-large' :
				IMG.className == 'show-large' ? 'hide-large' :
				IMG.className == 'hide-small' ? 'show-small' :
				IMG.className == 'show-small' ? 'hide-small' :
				"";
		IMG.src = "images/" + IMG.className + ".gif";
	}
}


function CreateImage(image_label, image_src, sort_order, thumb_images_src, error) {

	// get image sets parent container
	var parent_element = document.getElementById("images_parent");

	// get total number of image sets
	var images = parent_element.childNodes.length;
	var id = images + 1;

	// clear message
	document.getElementById("no_images").style.display = 'none';

	// preset values if undefined
	image_label = image_label ? image_label : "";
	image_src = image_src ? image_src : "";
	sort_order = sort_order ? sort_order : 0;
	thumb_images_src = thumb_images_src ? thumb_images_src : "";

	// product image table
	TABLE = document.createElement("TABLE");
	TABLE.cellPadding = "0";
	TABLE.cellSpacing = "0";
	TABLE.className = "product-image-table";
	TABLE.id = "image" + id;
	TBODY = document.createElement("TBODY");

	// product image label
	TR = document.createElement("TR");
	TR.className = "product-image-headingrow";
	TD = document.createElement("TD");
	TD.className = "product-image-headingcell1";
	TD.noWrap = "true";
	TD.id = "image_number" + id;
	TEXT = document.createTextNode("Image (" + id + "):");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = "product-image-headingcell2";
	INPUT = document.createElement("INPUT");
	INPUT.className = "product-image-input-large";
	INPUT.name = "image_label" + id;
	INPUT.id = "image_label" + id;
	INPUT.value = image_label;
	INPUT.maxLength = "255";
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = "product-image-headingcell3";
	A = document.createElement("A");
	A.id = "image_remove" + id;
	A.href = "javascript: RemoveImage('" + id + "')";
	IMG = document.createElement("IMG");
	IMG.src = "images/remove_icon.gif";
	IMG.title = "Remove this image";
	IMG.className = "action-image";
	A.appendChild(IMG);
	TD.appendChild(A);
	TR.appendChild(TD);
	TBODY.appendChild(TR);

	// product image sequence
	TR = document.createElement("TR");
	TR.className = "product-image-row";
	TD = document.createElement("TD");
	TD.className = "product-image-cell1";
	TD.noWrap = "true";
	TEXT = document.createTextNode("Sequence:");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = "product-image-cell2";
	TD.colSpan = "2";
	INPUT = document.createElement("INPUT");
	INPUT.className = "product-image-input-small";
	INPUT.name = "sort_order" + id;
	INPUT.id = "sort_order" + id;
	INPUT.value = sort_order;
	INPUT.maxLength = "255";
	TD.appendChild(INPUT);
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);

	// thumbnail image
	TR = document.createElement("TR");
	TR.className = "product-image-row";
	TD = document.createElement("TD");
	TD.id = "thumbnail" + id;
	TD.className = "product-image-cell3";
	TD.colSpan = "3";
	if (error == 1) {
		// the thumbnail image was not found
		// print the image src and blank image
		DIV = document.createElement("DIV");
		DIV.id = "error" + id;
		TEXT = document.createTextNode("Photo not found! (" + image_src + ")");
		DIV.appendChild(TEXT);
		TD.appendChild(DIV);
		IMG = document.createElement("IMG");
		IMG.src = "images/spacer.gif";
		IMG.id = "image_thumb" + id;
		TD.appendChild(IMG);
	}
	else {
		// the thumbnail image was found
		// print the thumbnail image only
		IMG = document.createElement("IMG");
		IMG.src = image_src ? thumb_images_src + image_src : "images/spacer.gif";
		IMG.id = "image_thumb" + id;
		TD.appendChild(IMG);
	}
	INPUT = document.createElement("INPUT");
	INPUT.type = "hidden";
	INPUT.name = "image_src" + id;
	INPUT.id = "image_src" + id;
	INPUT.value = image_src;
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);

	// upload image link
	TR = document.createElement("TR");
	TR.className = "product-image-footerrow";
	TD = document.createElement("TD");
	TD.className = "product-image-footercell";
	TD.colSpan = "3";
	A = document.createElement("A");
	A.id = "image_upload" + id;
	A.href = "javascript:PopUp('upload.cgi?image_set=" + id + "','upload','500','350')";
	A.className = "product-image-link";
	IMG = document.createElement("IMG");
	IMG.src = "images/upload_icon.gif";
	IMG.className = "action-image";
	IMG.align = "left";
	A.appendChild(IMG);
	TEXT = document.createTextNode("Upload");
	A.appendChild(TEXT);
	TD.appendChild(A);
	TEXT = document.createTextNode(" an image");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);
	TABLE.appendChild(TBODY);
	parent_element.appendChild(TABLE);
}



function SaveValues() {

	// loops through each product option table
	// loops through each product option value
	// validates & prepares each option values
	// creates string by joining option values
	// writes string to designated input field

	// clear out existing values
	document.getElementById("option_value1").value = "";
	document.getElementById("option_value2").value = "";
	document.getElementById("option_value3").value = "";
	document.getElementById("option_value4").value = "";

	var parent_element = document.getElementById("options_parent");			// option parent container
	var options = parent_element.childNodes.length/2;						// total number of options
	var error = "";															// preset validation error
	for (var i = 1; i <= options; i++) {
		var parent_element = document.getElementById("option_elementA" + i);	// option parent container
		var values = parent_element.childNodes.length;							// total number of values
		var option_values = new Array();										// create value list array
		for (var j = 1; j < values; j++) {
			var input = document.getElementById("option" + i + "_value" + j);		// create reference to input
			var value = input.value;												// get option input value
			var regexp = / $/;														// regular expression obj
			while (regexp.test(value)) value = value.substr(0,value.length - 1);	// remove ending spaces
			var regexp = /^ /;														// regular expression obj
			while (regexp.test(value)) value = value.substr(1);						// remove leading spaces
			if (value.indexOf("|") == -1) {											// check for vertical bar
				if (value) option_values.push(value);								// add value to value list
			}
			else error += "Please remove \"|\" from option (" + i + ") value " + j + "\n";		// vertical bar in value
		}
		var option_value = option_values.join("|");					// join all values with |
		var input = document.getElementById("option_value" + i);	// get reference to input
		input.value = option_value;									// write values into input
	}
	if (error) {			// check for error messages
		alert(error);	// display all error messages
		return false;	// cancel form submit action
	}
	else return true;	// values saved and validated
}



function CreateOption(new_option, option_name, manage_option) {

	// append a new product option table into
	// the product option parent container and
	// append a table for adding option values
	// to the product option table

	// get product option parent container
	var parent_element = document.getElementById("options_parent");

	// get total number of product options
	var options = parent_element.childNodes.length/2;
	var id = options + 1;

	// preset values if undefined
	option_name = option_name ? option_name : "";
	manage_option = manage_option ? manage_option : "N";

	// limit 4 product options
	if (options < 4) {
		// clear message
		document.getElementById("no_options").style.display = 'none';

		// product option label
		TABLE = document.createElement("TABLE");
		TABLE.cellPadding = "0";
		TABLE.cellSpacing = "0";
		TABLE.className = "product-option-headingtable";
		TABLE.id = "option_elementA" + id;
		TBODY = document.createElement("TBODY");
		TR = document.createElement("TR");
		TR.className = "product-option-headingrow2";
		TD = document.createElement("TD");
		TD.colSpan = "3";
		TD.noWrap = "true";
		TD.className = "product-option-headingcell4";
		A = document.createElement("A");
		A.id = "checkbox_link" + id;
		A.href = "javascript: ToggleCheckbox('" + id + "')";
		IMG = document.createElement("IMG");
		IMG.src = manage_option == "Y" ? "images/checkbox_checked.gif" : "images/checkbox_unchecked.gif";
		IMG.className = manage_option == "Y" ? "checked" : "unchecked";
		IMG.name = "checkbox_image" + id;
		IMG.align = "left";
		IMG.id = "checkbox_image" + id;
		A.appendChild(IMG);
		TD.appendChild(A);
		TEXT = document.createTextNode("Include this option on the Manage Product Options form");
		TD.appendChild(TEXT);
		INPUT = document.createElement("INPUT");
		INPUT.type = "hidden";
		INPUT.name = "manage_option" + id;
		INPUT.id = "manage_option" + id;
		INPUT.value = manage_option;
		TD.appendChild(INPUT);
		TR.appendChild(TD);
		TBODY.appendChild(TR);
		TR = document.createElement("TR");
		TR.className = "product-option-headingrow1";
		TD = document.createElement("TD");
		TD.noWrap = "true";
		TD.className = "product-option-headingcell1";
		TD.id = "option_number" + id;
		TEXT = document.createTextNode("Option (" + id + "):");
		TD.appendChild(TEXT);
		TR.appendChild(TD);
		TD = document.createElement("TD");
		TD.className = "product-option-headingcell2";
		INPUT = document.createElement("INPUT");
		INPUT.className = "product-option-input";
		INPUT.name = "option_name" + id;
		INPUT.id = "option_name" + id;
		INPUT.value = option_name;
		INPUT.maxLength = "255";
		TD.appendChild(INPUT);
		TR.appendChild(TD);
		TD = document.createElement("TD");
		TD.className = "product-option-headingcell3";
		A = document.createElement("A");
		A.id = "option_remove" + id;
		A.href = "javascript: RemoveOption('" + id + "')";
		IMG = document.createElement("IMG");
		IMG.src = "images/remove_icon.gif";
		IMG.title = "Remove this option";
		IMG.className = "action-image";
		A.appendChild(IMG);
		TD.appendChild(A);
		TR.appendChild(TD);
		TBODY.appendChild(TR);
		TABLE.appendChild(TBODY);
		parent_element.appendChild(TABLE);

		// NOTE: THIS IS WHERE THE
		// VALUES WILL BE INSERTED

		// new option value link
		TABLE = document.createElement("TABLE");
		TABLE.cellPadding = "0";
		TABLE.cellSpacing = "0";
		TABLE.className = "product-option-footertable";
		TABLE.id = "option_elementB" + id;
		TBODY = document.createElement("TBODY");
		TR = document.createElement("TR");
		TR.className = "product-option-footerrow";
		TD = document.createElement("TD");
		TD.className = "product-option-footercell";
		TD.colSpan = "3";
		A = document.createElement("A");
		A.id = "create_value" + id;
		A.href = "javascript: CreateValue('" + id + "')";
		A.className = "product-option-link";
		IMG = document.createElement("IMG");
		IMG.src = "images/add_icon.gif";
		IMG.className = "action-image";
		IMG.align = "left";
		A.appendChild(IMG);
		TEXT = document.createTextNode("Add");
		A.appendChild(TEXT);
		TD.appendChild(A);
		TEXT = document.createTextNode(" a new value");
		TD.appendChild(TEXT);
		TR.appendChild(TD);
		TBODY.appendChild(TR);
		TABLE.appendChild(TBODY);
		parent_element.appendChild(TABLE);

		// a new option is one created by clicking the "Add"
		// a new option link. existing options are created on
		// initialization of the "Edit Product" form
		if (new_option == "Y") {
			// add three empty option values
			CreateValue(id);
			CreateValue(id);
			CreateValue(id);
		}
	}
	else {
		var message = '------------------------- ALERT! -------------------------\n\n';
		message += 'A product can have a maximum of four (4) options.\n';
		alert(message);
	}
}



function CreateValue(option_number,value_name) {

	// append a new product option value row
	// into an existing product option table
	// the option table's first child is the
	// option name input row

    // a limit of 60 characters is set for the
    // value field to prevent the combination
    // of product option values from exceeding
    // the varchar(255) limit:
    // for example
    // color = red (3 characters)
    // size = small (5 characters)
    // fit = ribbed (6 characters)
    // sleeve = tank (4 characters)
    // product_option = red|small|ribbed|tank
    // (21 total characters w/vertical bars)

	// get product option container
	var parent_element = document.getElementById("option_elementA" + option_number);

	// get total number of option values
	var values = parent_element.childNodes.length;

	// preset values if undefined
	value_name = value_name ? value_name : "";

	// option value input
	TBODY = document.createElement("TBODY");
	TR = document.createElement("TR");
	TD = document.createElement("TD");
	TD.noWrap = "true";
	TD.className = values % 2 ? "product-option-oddcell1" : "product-option-evencell1" ;
	TEXT = document.createTextNode("Value " + values + ":");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = values % 2 ? "product-option-oddcell2" : "product-option-evencell2" ;
	TD.colSpan = "2";
	INPUT = document.createElement("INPUT");
	INPUT.name = "option" + option_number + "_value" + values;
	INPUT.id = "option" + option_number + "_value" + values;
	INPUT.className = values % 2 ? "product-option-oddinput" : "product-option-eveninput" ;
	INPUT.maxLength = "60";
	INPUT.value = value_name;
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);
	parent_element.appendChild(TBODY);
}

function CreateMethod() {

	// create the shipping method rate table
	// used as a container for creating rates

	var parent_element = document.getElementById("method_parent");		// method parent container

	TABLE = document.createElement("TABLE");
	TABLE.className = "shipping-rates-table";
	TABLE.cellPadding = "0";
	TABLE.cellSpacing = "0";
	TABLE.id = "rates_parent";
	TBODY = document.createElement("TBODY");
	TR = document.createElement("TR");
	TR.className = "shipping-rates-headingrow";
	TD = document.createElement("TD");
	TD.className = "shipping-rates-headingcell";
	TD.noWrap = "true";
	TD.colSpan = "5";
	TEXT = document.createTextNode("Merchandise Total");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = "shipping-rates-headingcell";
	TD.noWrap = "true";
	TD.colSpan = "2";
	TEXT = document.createTextNode("S/H Charge");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = "shipping-rates-headingcell";
	TD.noWrap = "true";
	TD.colSpan = "2";
	TEXT = document.createTextNode("Active");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);
	TABLE.appendChild(TBODY);
	parent_element.appendChild(TABLE);
}

function CreateRate(active_ind,amount_start,amount_end,charge) {

	// append a new shipping rate row to
	// a new or existing shipping method

	var parent_element = document.getElementById("rates_parent");			// rates parent container
	var rates = parent_element.childNodes.length - 1;						// total number of rates
	active_ind = active_ind ? active_ind : "";								// preset active status
	amount_start = amount_start ? amount_start : "";						// preset start merch. total
	amount_end = amount_end ? amount_end : "";								// preset end merch. total
	charge = charge ? charge : "";											// preset shipping charge
	var rate_count = parseInt(document.maint.rate_count.value) + 1;			// increment rate count
	document.maint.rate_count.value = rate_count;							// store rate count

	TBODY = document.createElement("TBODY");
	TR = document.createElement("TR");
	TR.className = rates % 2 ? "shipping-rates-evenrow" : "shipping-rates-oddrow";
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	TEXT = document.createTextNode("$");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	INPUT = document.createElement("INPUT");
	INPUT.className = rates % 2 ? "shipping-rates-eveninput" : "shipping-rates-oddinput";
	INPUT.maxLength = "255";

	if (rates == "0") {
		INPUT.disabled = 1;
		INPUT.value = "0.00";
		TD.appendChild(INPUT);
		INPUT = document.createElement("INPUT");
		INPUT.type = "HIDDEN";
		INPUT.value = "0";
	}
	else INPUT.value = amount_start;

	INPUT.name = "amount_start" + (rates + 1);
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	TEXT = document.createTextNode("-");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	TEXT = document.createTextNode("$");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	INPUT = document.createElement("INPUT");
	INPUT.className = rates % 2 ? "shipping-rates-eveninput" : "shipping-rates-oddinput";
	INPUT.name = "amount_end" + (rates + 1);
	INPUT.maxLength = "255";
	INPUT.value = amount_end;
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	TEXT = document.createTextNode("$");
	TD.appendChild(TEXT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	INPUT = document.createElement("INPUT");
	INPUT.className = rates % 2 ? "shipping-rates-eveninput" : "shipping-rates-oddinput";
	INPUT.name = "charge" + (rates + 1);
	INPUT.maxLength = "255";
	INPUT.value = charge;
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = rates % 2 ? "shipping-rates-evencell" : "shipping-rates-oddcell";
	SELECT = document.createElement("SELECT");
	SELECT.name = "active_ind" + (rates + 1);
	OPTION = document.createElement("OPTION");
	OPTION.value = "Y";
	OPTION.className = "yes";
	if (active_ind == "Y") OPTION.selected = 1;
	TEXT = document.createTextNode("Yes");
	OPTION.appendChild(TEXT);
	SELECT.appendChild(OPTION);
	OPTION = document.createElement("OPTION");
	OPTION.value = "N";
	OPTION.className = "no";
	if (active_ind == "N") OPTION.selected = 1;
	TEXT = document.createTextNode("No");
	OPTION.appendChild(TEXT);
	SELECT.appendChild(OPTION);
	TD.appendChild(SELECT);
	TR.appendChild(TD);
	TBODY.appendChild(TR);
	parent_element.appendChild(TBODY);
}

function RemoveOption(option_number) {

	// removes an existing product option table
	// from the product option parent container
	// updates preceeding product option tables
	// by decrementing element names and ids

	option_number = option_number ? option_number : "";

	// require confirmation only if an option currently exist for
	// the product, and the options checkbox is currently checked
	var checkbox_image = document.getElementById("checkbox_image" + option_number).className;
	var options_flag = document.getElementById("options_flag").value;
	var require_confirm = checkbox_image == "unchecked" ? "N" :
						options_flag == "Y" ? "Y" : "N";

	// set the message
	var message = '----------------------- ALERT! -----------------------\n\n';
	message += 'Deleting this option will delete the option code,\n';
	message += 'price variations, stock message and orderable\n';
	message += 'status of existing options for this product.\n\n';
	message += 'Click "OK" to remove this option.\n';
	message += 'Click "Cancel" to keep this option.\n';

	// if the confirmation flag is set to "Y" ("Yes")
	// get confirmation from user before removing option
	if (require_confirm == "N" || confirm(message)) {
		// set flag to delete current product
		// option records when saving product
		if (require_confirm == "Y") document.getElementById("delete_options").value = 'Y';
		// get parent element
		var parent_element = document.getElementById("options_parent");
		// get child element
		var child_elementA = document.getElementById("option_elementA" + option_number);
		var child_elementB = document.getElementById("option_elementB" + option_number);
		// get total number of option
		var options = parent_element.childNodes.length/2;
		// decrement option number references in option
		// sets greater than the deleted option set
		for (var new_id = parseInt(option_number); new_id < options; new_id++) {
			var old_id = new_id + 1;
			if (document.getElementById("option_elementA" + old_id)) {
				// decrement option table id
				var option_elementA = document.getElementById("option_elementA" + old_id);
				option_elementA.id = "option_elementA" + new_id;
				// decrement add value table id
				var option_elementB = document.getElementById("option_elementB" + old_id);
				option_elementB.id = "option_elementB" + new_id;
				// decrement remove option link and id
				var option_remove = document.getElementById("option_remove" + old_id);
				option_remove.href = "javascript: RemoveOption('" + new_id + "')";
				option_remove.id = "option_remove" + new_id;
				// decrement checkbox link and id
				var checkbox_link = document.getElementById("checkbox_link" + old_id);
				checkbox_link.href = "javascript: ToggleCheckbox('" + new_id + "')";
				checkbox_link.id = "checkbox_link" + new_id;
				// decrement checkbox image name and id
				var checkbox_image = document.getElementById("checkbox_image" + old_id);
				checkbox_image.name = "checkbox_image" + new_id;
				checkbox_image.id = "checkbox_image" + new_id;
				// decrement manage option name and id
				var manage_option = document.getElementById("manage_option" + old_id);
				manage_option.name = "manage_option" + new_id;
				manage_option.id = "manage_option" + new_id;
				// decrement add value link and id
				var create_value = document.getElementById("create_value" + old_id);
				create_value.href = "javascript: CreateValue('" + new_id + "')";
				create_value.id = "create_value" + new_id;
				// decrement option label input name and id
				var option_name = document.getElementById("option_name" + old_id);
				option_name.name = "option_name" + new_id;
				option_name.id = "option_name" + new_id;
				// decrement option set identifier value and id
				var option_number = document.getElementById("option_number" + old_id);
				option_number.innerHTML = "Option (" + new_id + "):";
				option_number.id = "option_number" + new_id;
				// decrement all option value names and ids
				var values = option_elementA.childNodes.length - 1;
				for (var value_id = 1; value_id <= values; value_id++) {
					if (document.getElementById("option" + old_id + "_value" + value_id)) {
						// decrement value name input name and id
						var value_name = document.getElementById("option" + old_id + "_value" + value_id);
						value_name.name = "option" + new_id + "_value" + value_id;
						value_name.id = "option" + new_id + "_value" + value_id;
					}
				}
			}
		}
		// remove option
		parent_element.removeChild(child_elementA);
		parent_element.removeChild(child_elementB);
		options--;

		// display message if no product options exist
		if (!options) document.getElementById("no_options").style.display = 'block';
	}
}

function RemoveImage(image_number) {

	// removes an existing product image table
	// from the product image parent container
	// updates preceeding product image tables
	// by decrementing element names and ids

	image_number = image_number ? image_number : "";

	var message = '--------------- ALERT! ---------------\n\n';
	message += 'Click "OK" to remove this image.\n';
	message += 'Click "Cancel" to keep this image.\n';

	if (confirm(message)) {
		// get parent element
		var parent_element = document.getElementById("images_parent");

		// get child element
		var child_element = document.getElementById("image" + image_number);

		// get total number of image sets
		var images = parent_element.childNodes.length;

		// decrement image number references in image
		// sets greater than the deleted image set
		for (var new_id = parseInt(image_number); new_id < images; new_id++) {
			var old_id = new_id + 1;
			if (document.getElementById("image" + old_id)) {
				// decrement image table id
				var image = document.getElementById("image" + old_id);
				image.id = "image" + new_id;
				// decrement remove image set link and id
				var image_remove = document.getElementById("image_remove" + old_id);
				image_remove.href = "javascript:RemoveImage('" + new_id + "')";
				image_remove.id = "image_remove" + new_id;
				// decrement image label input name and id
				var image_label = document.getElementById("image_label" + old_id);
				image_label.name = "image_label" + new_id;
				image_label.id = "image_label" + new_id;
				// decrement image sort order input name and id
				var sort_order = document.getElementById("sort_order" + old_id);
				sort_order.name = "sort_order" + new_id;
				sort_order.id = "sort_order" + new_id;
				// decrement upload link value
				var image_upload = document.getElementById("image_upload" + old_id);
				image_upload.href = "javascript:PopUp('upload.cgi?image_set=" + new_id + "','upload','500','350')"
				image_upload.id = "image_upload" + new_id;
				// decrement image src input name and id
				var image_src = document.getElementById("image_src" + old_id);
				image_src.name = "image_src" + new_id;
				image_src.id = "image_src" + new_id;
				// decrement image thumb name and id
				var image_thumb = document.getElementById("image_thumb" + old_id);
				image_thumb.id = "image_thumb" + new_id;
				// decrement image set identifier value and id
				var image_number = document.getElementById("image_number" + old_id);
				image_number.innerHTML = "Image (" + new_id + "):";
				image_number.id = "image_number" + new_id;
			}
		}

		// remove image set
		parent_element.removeChild(child_element);
		images--;

		// display message if no images set exist
		if (!images) document.getElementById("no_images").style.display = 'block';
	}
}

// turn on roll-over image
function imgOn(imgName, imgSource) {
	image_on = new Image();
	image_on.src = imgSource;
	document[imgName].src = image_on.src;
}

// turn off roll-over image
function imgOff(imgName, imgSource) {
	image_off = new Image();
	image_off.src = imgSource;
	document[imgName].src = image_off.src;
}

// divOn
// show hidden div
function divOn(element_id) {
	if (document.getElementById &&
		document.getElementById(element_id) &&
		document.getElementById(element_id).style)
			document.getElementById(element_id).style.display="block";
}

// divOff
// hide div
function divOff(element_id) {
	if (document.getElementById &&
		document.getElementById(element_id) &&
		document.getElementById(element_id).style)
			document.getElementById(element_id).style.display="none";
}

// backgroundOn
// turn on background color
function backgroundOn(element_id) {
	if (document.getElementById &&
		document.getElementById(element_id) &&
		document.getElementById(element_id).style)
			document.getElementById(element_id).style.backgroundColor="#CCCCCC";
}

// backgroundOff
// turn off background color
function backgroundOff(element_id) {
	if (document.getElementById &&
		document.getElementById(element_id) &&
		document.getElementById(element_id).style)
			document.getElementById(element_id).style.backgroundColor="#FFFFFF";
}

// ValidateAdd
// check quantity when adding an item to shopping cart
function ValidateAdd() {

	// set error message
	msg = "";

	// set quantity field
	var qty;
	qty = document.product.quantity.value;

	// check quantity is a number
	// check quantity greater zero
	if (qty.search(/\D/) > -1) {
		msg += "Quantity must be a number greater than zero\n";
	}
	else {
		if (qty <= 0) {
			msg += "Please enter a quantity greater than zero\n";
		}
	}

	// alert message if quantity did not pass check
	if (msg != "") {
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

// javascript functions to show/hide product option message
var new_id = "";	// new product option value
var old_id = "";	// old product option value

// ShowMessage
// Function to show/hide the option message
function ShowMessage(element_id) {
	// set element to show
	new_id = element_id;
	if (old_id) {
		// old message exists
		// turn off old message
		MessageOff('old_id');
		// set old message id to new id
		old_id = new_id;
	}
	else {
		// old message does not exists
		// set old message id to new id
		old_id = new_id;
	}
	// turn on new message
	MessageOn('new_id');
}

// MessageOff
// Function to hide the option message
function MessageOff() {
	// hide option message
	if (document.getElementById && document.getElementById(old_id) && document.getElementById(old_id).style) {
		document.getElementById(old_id).style.display="none";

		// turn on active 'add to cart' image (DISPLAY)
		if (document.getElementById && document.getElementById('addcart_active') && document.getElementById('addcart_active').style) {
			document.getElementById('addcart_active').style.display="block";
		}

		// turn off inactive 'add to cart' image (DO NOT DISPLAY)
		if (document.getElementById && document.getElementById('addcart_inactive') && document.getElementById('addcart_inactive').style) {
			document.getElementById('addcart_inactive').style.display="none";
		}
	}
}

// Function to turn on the option message
function MessageOn() {
	// show option message
	if (document.getElementById && document.getElementById(new_id) && document.getElementById(new_id).style) {
		document.getElementById(new_id).style.display="block";

		// turn off active 'add to cart' image (DO NOT DISPLAY)
		if (document.getElementById && document.getElementById('addcart_active') && document.getElementById('addcart_active').style) {
			document.getElementById('addcart_active').style.display="none";
		}

		// turn on inactive 'add to cart' image (DISPLAY)
		if (document.getElementById && document.getElementById('addcart_inactive') && document.getElementById('addcart_inactive').style) {
			document.getElementById('addcart_inactive').style.display="block";
		}
	}
}

function CreateSpec(spec_id, spec_value, spec_name, hide_ind) {

	// append a new product spec table into
	// the product spec parent container and
	// append a table for adding option values
	// to the product spec table

	// get product spec parent container
	var parent_element = document.getElementById("product_spec_parent");

	// get total number of product specs
	var specs = parent_element.childNodes.length;
	var id = specs + 1;

	// preset values if undefined
	spec_value = spec_value ? spec_value : "";
	spec_name = spec_name ? spec_name : "";

	// no limit of product specs
	// clear message
	document.getElementById("no_product_specs").style.display = 'none';

	// product spec label and value
	TR = document.createElement("TR");
	TD = document.createElement("TD");
	TD.noWrap = "true";
	TD.className = specs % 2 ? "product-spec-evencell1" : "product-spec-oddcell1";
	TD.id = "product_spec" + id;
	TEXT = document.createTextNode(spec_name);
	TD.appendChild(TEXT);
	if (hide_ind == "Y") {
		SPAN = document.createElement("SPAN");
		SPAN.className = "product-spec-hidden";
		TEXT = document.createTextNode(" (hidden)");
		SPAN.appendChild(TEXT);
		TD.appendChild(SPAN);
	}
	TR.appendChild(TD);
	TD = document.createElement("TD");
	TD.className = specs % 2 ? "product-spec-evencell2" : "product-spec-oddcell2";
	INPUT = document.createElement("INPUT");
	INPUT.className = specs % 2 ? "product-spec-eveninput" : "product-spec-oddinput";
	INPUT.name = "spec_value" + spec_id;
	INPUT.id = "spec_value" + spec_id;
	INPUT.value = spec_value;
	INPUT.maxLength = "255";
	TD.appendChild(INPUT);
	TR.appendChild(TD);
	parent_element.appendChild(TR);
}

function PromoValPref(elm) {
	var promo_val_pref_D = document.getElementById("promo_val_pref_D");
	var promo_val_pref_P = document.getElementById("promo_val_pref_P");
	var promotion_value = elm.options[elm.options.selectedIndex].value;
	promo_val_pref_D.style.display = promotion_value == "D" ? "inline" : "none";
	promo_val_pref_P.style.display = promotion_value == "P" ? "inline" : "none";
}

function SetInput(elm, action, text) {
	if (action == "blur") {
		if (elm.value == "") {
			elm.value = text;
		}
	}
	else if (action == "focus") {
		if (elm.value == text){
			elm.value = "";
		}
	}
}





