
function buildOptionSelector(arr, name, dependentListName, promptTxt, cssClass) {	

	if (!promptTxt)
		promptTxt = "-Select-";

	document.write("<select");
	if (cssClass)
		document.write(" class=\"" + cssClass + "\"");
	document.write(" name=\"" + name + "\" onchange=\"refreshAvailability(this, this.form." + dependentListName + ")\">");			

	document.write("<option value=\"\">" + promptTxt + "</option>");
	for (var i in arr) {
		var option = "<option value=\"" + i + "\">" + arr[i]["label"] + "</option>";
		document.write(option);
	}
	document.write("</select>");			
	
}

function selectOptionByValue(list, value) {
	for (var i = 0; i < list.options.length; i++) {
		if (list.options[i].value == value) {
			list.selectedIndex = i;
			return;
		}
	}
}

function showAll(targetList) {
	refArray = (targetList.name == "size_selector") ? sizes : colors;

	//remove options 
	clearList(targetList);				
	
	//load new options as per refArray
	for (i in refArray) 
		addOption(targetList, refArray[i]["label"], i);					
}

function showAvailable(srcList, targetList) {
	//get reference array
	refArray = (srcList.name == "size_selector") ? sizes : colors;
	
	availableOptions = refArray[srcList[srcList.selectedIndex].value]["available_in"];			
	//remove options 
	clearList(targetList);				

	//load new options as per selected id of srcList
	for (i in availableOptions) 
		addOption(targetList, availableOptions[i]["label"], [i]);								
		
	
}

/*function checkPrice(list1, list2) {
	//alert(list1.selectedIndex);
	//alert(list2.selectedIndex);
	if (typeof list1.form.price == "undefined" || !price)
		return;
		
	if (list1.selectedIndex > 0 && list2.selectedIndex > 0) {
		with (list1.form) 	
			price.value = priceLookupMatrix[getSelectedValue(size_selector)][getSelectedValue(color_selector)];
	} else {
		list1.form.price.value = "";
	}	
}*/

function clearList(targetList) {
	with (targetList)
		for (var i = 1; i < options.length; ) 
			options[i] = null;				
}

function refreshAvailability(srcList, targetList) {
	var origVal = targetList[targetList.selectedIndex].value;
	if (srcList.selectedIndex <= 0) {
		showAll(targetList);
	} else {
		showAvailable(srcList, targetList);
	}		
	selectOptionByValue(targetList, origVal);
	
	//checkPrice(srcList, targetList);
}

function buildResetButton() {
	document.write("<a class=\"button\" href=\"javascript:resetOptions()\">clear selections</a>");
}

function resetOptions() {
	with (document.itemDetailForm) {
		color_selector.selectedIndex = 0;
		size_selector.selectedIndex = 0;
		showAll(color_selector);
		showAll(size_selector);
		//price.value = "";
	}
}

function addToCart() {
	if (!validateForm())
		return;
		
	with (document.itemDetailForm) {		
		//siz.value = getScaleEntryId(getSelectedValue(size_selector), getSelectedValue(color_selector));
		//attr1.value = getAttr1EntryId(getSelectedValue(color_selector), getSelectedValue(size_selector));
		action = "/cart/add_to_cart.cfm";
		submit();	
	}
}

function validateForm() {
	with (document.itemDetailForm) {
		var msg;
		var field;
		if (!hasValue(size_selector)) {				
			msg = "Please select a size."
			fld = size_selector;
		} else if (!hasValue(color_selector)) {
			msg = "Please select a color/print.";
			fld = color_selector;
		} else if (!hasValue(qty)) {
			msg = "Please specify quantity.";
			fld = qty;
		} else if (!checkInteger(qty.value)) {
			msg = "Quantity is invalid.";
			fld = qty;
		}
		
		if (msg) {
			$.prompt(msg) 
			fld.focus();
			if (fld.select)
				fld.select();
			return false;
		} else
			return true;	
	}
}

function addToWishList() {
	if (!validateForm())
		return;
	
	with (document.itemDetailForm) {
		//siz.value = getScaleEntryId(getSelectedValue(size_selector), getSelectedValue(color_selector));
		//attr1.value = getAttr1EntryId(getSelectedValue(color_selector), getSelectedValue(size_selector));		
		//var urlToken = (typeof CFID != "undefined" && typeof CFTOKEN != "undefined") ? ("CFID=" + CFID.value + "&CFTOKEN=" + CFTOKEN.value) : ""; 
		//url = "/addtowishlist.cfm?qty=" + qty.value + "&id=" + id.value + "&of2=" + of2.value + "&siz=" + siz.value + "&attr1=" + attr1.value + "&clearanceitem=" + clearanceitem.value; // + "&" + urlToken;
		//document.location.href = url;
		action = "/addtowishlist";
		submit();
		
	}
	
}

/*function getScaleEntryId(size, color) {
	return scaleLookupMatrix[size][color];
}

function getAttr1EntryId(color, size) {
	return colorLookupMatrix[color][size];
}*/


function preventBrowserCache() {	
	with (document.itemDetailForm) {
		color_selector.selectedIndex = 0;
		size_selector.selectedIndex = 0;
		if (typeof price != "undefined" && price)
			price.value = "";
	}
}

addEvent(window, "load", preventBrowserCache);

