configurator.doPreEvent = function(object, type, args) {}
// add our onChangeConfigurator stuff here
configurator.doPostEvent = function(object, type, args) {
    if (type == "mouseover") {
        if (helpImages[object.id] !== undefined) {
            document.getElementById("helpImage").src = helpImages[object.id];
        }
        if (helpText[object.id] !== undefined) {
            document.getElementById("helpText").innerHTML = helpText[object.id];
        }
    } else if (type == "mouseout") {
        if (helpImages["default"] !== undefined) {
            document.getElementById("helpImage").src = helpImages["default"];
        }
        if (helpText["default"] !== undefined) {
            document.getElementById("helpText").innerHTML = helpText["default"];
        }
	} else {
        // we must first clear filter on all configurator changes since we're
        // not constraining the configurator from the locator
        locator.clearAllFilters();

        // now we can add filters for selected configurator attributes
        if (configurator.isComplete()) {
            for (var i = 0; i < configurator.attributes.length; i++) {
                var att = configurator.attributes[i];
                if (att.isDiscrete && att.selectedValue != null) {
                    locator.setColumnFilter(att.id, att.selectedValue.id, null, null);
                } else if (!att.isDiscrete) {
                    for (var j = 0; j < att.values.length; j++) {
                        var atv = att.values[j];
                        if (atv.isSelected) {
                            locator.setColumnFilter(atv.id, atv.id, null, null);
                        }
                    }
                }
            }
            if (args != "interim_event") {
                locator.isVisible = true;
                document.getElementById("locatorText").innerHTML = dynamicLocaleLabels["locatorInstructions"];
                locator.onChangeFilter(true);
            }
        } else {
            document.getElementById("locatorText").innerHTML = "";
            locator.isVisible = false;
            locator.display();
        }

		displayAttributeList();
        displayRequiredAttributeCount();
	}
}

var COOKIE_NAME = SERIES.toUpperCase() + "data";

function saveData() {
	// document.cookie = COOKIE_NAME + "=" + escape(conf.getData());
}

function restoreData() {
}

function onClickReset() {
    // remove any locator constraints
    for (var i = 0; i < configurator.attributes.length; i++) {
    	var a = configurator.attributes[i];
    	for (var j = 0; j < a.values.length; j++) {
    		a.values[j].constraints.remove("locator");
    	}
    }

	configurator.setValue(null);
	if (locator != null) {
		locator.page = 0;
		locator.sort = null;
		if (locator.columns != null) {
			for (var i = 0; i < locator.columns.length; i++) {
				locator.columns.filter = null;
			}
		}
		locator.onClickResetAll(document.getElementById("locatorCell"));
	}
}

function setConfiguratorEvents(allow) {
    if (document.getElementById("configuratorTable") != null) {
        if (allow) {
            document.getElementById("configuratorTable").className = "activeConfiguratorTable";
        } else {
            document.getElementById("configuratorTable").className = "inactiveConfiguratorTable";
        }
    }
    configurator.allowEvents = allow;
}

function getSubmitAssemblyResultsCallback(text, headers, callingContext) {
    locator.waitingForServer = false;
    setConfiguratorEvents(true);
    document.body.style.cursor = "";
    eval("var o = " + text + ";");
    if (typeof o == "string" && o.length > 0) {
        configurator.setValue(o);
        if (decodeAssemblyAutoclickMap != null) {
            for (var i = 0; i < decodeAssemblyAutoclickMap.length; i++) {
                var reg = new RegExp(decodeAssemblyAutoclickMap[i][0]);
                if (decodedAssembly.match(reg)) {
                    var val = configurator.getAttributeValue(decodeAssemblyAutoclickMap[i][1]);
                    configurator.handleEvent(val, "click", "interim_event")
                }
            }
        }
        configurator.doPostEvent(null, "click", null);
    } else {
        alert("No products matching the given catalog number and voltage were found.");
    }
}

var decodedAssembly = null;
function onClickSubmitAssembly() {
	var asse = document.getElementById("enterAssemblyInput");
	var assemblyPn = asse.value;
	if (assemblyPn == null || assemblyPn.trim().length == 0) {
		alert("Please enter an assembly number.");
		return;
	// } else if (assemblyPn.length < 8 || assemblyPn.indexOf("855") === -1) {
    } else if (assemblyPn.length < 7) {
        alert("Assembly number must include a complete base part number");
        return;
    }

	var volte = document.getElementById("enterAssemblyVoltageSelect");
	var voltageId = volte.options[volte.selectedIndex].value;
	if (voltageId == null || voltageId == "no selection") {
		alert("Please select a voltage from the dropdown list.");
		return;
	}

    decodedAssembly = assemblyPn.toUpperCase();

    // assume that any prefix means that's the only prefix and only prepend %
    // if nothing before the base part number
    // for now though we're always prepending on 55x
    var searchPn = '%' + assemblyPn.toUpperCase();
    /*
    if (searchPn.indexOf("8314") == 0) {
        searchPn = '%' + searchPn;
    }
    */

    var s = "decode?domain=" + SERIES + "&id=" + encodeURIComponent(searchPn);
    if (voltageId !== "none") {
        s += "&voltage=" + voltageId;
        decodedAssembly += " " + voltageId;
    }

    if (isDebug) {
        var s = "Match for: " + decodedAssembly + "\n";
        if (decodeAssemblyAutoclickMap != null) {
            for (var i = 0; i < decodeAssemblyAutoclickMap.length; i++) {
                var reg = new RegExp(decodeAssemblyAutoclickMap[i][0]);
                if (decodedAssembly.match(reg)) {
                    s += i + " - \"" + decodeAssemblyAutoclickMap[i][0] + "\" - " + decodeAssemblyAutoclickMap[i][1] + "\n";
                } else {
                    s += i + " - \"" + decodeAssemblyAutoclickMap[i][0] + "\" - no match\n";
                }
            }
        }
        alert(s);
        return;
    }

    // s += "&debug=true";
    locator.waitingForServer = true;
    setConfiguratorEvents(false);
    document.body.style.cursor = "wait";
    ajaxCaller.getPlainText(s, getSubmitAssemblyResultsCallback);
}

function displayHelp(object, type) {
    if (typeof getLocaleResource === "function") {
        if (type == "mouseover") {
            if (getLocaleResource("infoImage_" + object.id) != "") document.getElementById("helpImage").src = getLocaleResource("infoImage_" + object.id);
            if (getLocaleResource("infoText_" + object.id) != "") document.getElementById("helpText").innerHTML = getLocaleResource("infoText_" + object.id);
        } else {
            document.getElementById("helpImage").src = getLocaleResource("infoImage_default");
            document.getElementById("helpText").innerHTML = getLocaleResource("infoText_default");
        }
    }
}

function onClickChangeLocale(locale) {
    if (SERIES != null) requestParameters["series"] = SERIES;
    var data = configurator.getValueAsString();
    if (data != null && data.length > 0) requestParameters["data"] = data;
    requestParameters["lang"] = locale;
    requestParameters["units"] = locator.measurementSystem;
    sendPost(SERIES + "_selector.jsp", requestParameters);
}

function onClickToggleUnit() {
	var newUnit = "metric";
	if (document.getElementById("unitEnglish").checked) newUnit = "english";
    if (newUnit != locator.measurementSystem) {
        locator.measurementSystem = newUnit;
		displayAttributeList();
		locator.display();
	}
}

function displayRequiredAttributeCount() {
    var rcount = 0;
    var count = 0;
    for (var i = 0; i < configurator.attributes.length; i++) {
        if (configurator.attributes[i].isRequired) {
            rcount++;
            if (configurator.attributes[i].hasSelection()) {
                count++;
            }
        }
    }
    document.getElementById("requiredAttributeCount").innerHTML = count + " of " + rcount;
}

// NOTE: this function isn't used anymore.  There were too many problems with
// constraining the configurator from the locator, so now there is no such
// constraining
function onChangeLocator() {
    var isFiltered = locator.getCurrentFilter(true) != null;
    for (var i = 0; i < configurator.attributes.length; i++) {
        var attr = configurator.attributes[i];
        cvals = attr.values;
        for (var j = 0; j < cvals.length; j++) {
            var found = true;
            if (isFiltered) {
                var found = false;
                var key = (attr.isDiscrete) ? attr.id : cvals[j].id;
                var lvals = locator.columnMap[key].values;
                if (lvals != null) {
                    for (var k = 0; k < lvals.length; k++) {
                        if (cvals[j].label == lvals[k]) {
                            found = true;
                            break;
                        }
                    }
                } else {
                    found = true;
                }
            }
            if (found) {
                cvals[j].constraints.remove("locator");
            } else {
                cvals[j].constraints.add(new cds.configurator.Constraint("locator", null, false));
            }
            configurator.renderer.display(cvals[j], "constraint");
        }
    }
	configurator.display();
}

function onLocatorSelectProduct(product, label, isToday) {
    if (showTodayLink || !isToday) {
        if (SERIES != null) requestParameters["series"] = SERIES;
        var data = configurator.getValueAsString();
        if (data != null && data.length > 0) requestParameters["data"] = data;
        requestParameters["product"] = product;
        requestParameters["units"] = locator.measurementSystem;
        sendPost("details_55x.jsp", requestParameters);
    } else {
        var voltage = configurator.getAttribute("aVoltage").selectedValue;
        if (voltage != null) voltage = (voltage.id.indexOf("AC") >= 0) ? "AC" : "DC";
        var leadLength = (configurator.getAttributeValue("avElectricalConnectionNoBoxType4Conduit").isSelected ||
                configurator.getAttributeValue("avElectricalConnectionNoBoxType7Conduit").isSelected ||
                configurator.getAttributeValue("avElectricalConnectionBox18Leads").isSelected) ? "D" : "";
        var postParameters = {"productid": label.replace(" DC", "/DC").replace(" ", voltage) + leadLength};
        sendPost("http://www.ascovalve.com/Applications/ProductSearch/productlink.aspx", postParameters, "_blank", "get");
    }
}

function onLocatorSelectRow(idx) {
	displayAttributeList(idx);
}

function displayAttributeList(idx) {
	var e = document.getElementById("cfgAttributeList");
	while (e.hasChildNodes()) {
		e.removeChild(e.lastChild);
	}

	var a = configurator.getConfiguredAttributes();
	var count = 0;
	if (a != null) {
		for (var i = 0; i < a.length; i++) {
			var tr = document.createElement("tr");
			e.appendChild(tr);
			tr.className = (count++ % 2 == 0) ? "cdsEvenRow" : "cdsOddRow";
			var td = document.createElement("td");
			tr.appendChild(td);
			td.className = "cdsAttributeTableCellLeft";
			td.innerHTML = a[i].getLocalizedLabel();
			td = document.createElement("td");
			tr.appendChild(td);
			td.className = "cdsAttributeTableCellRight";
			if (a[i].id == "voltageGroup") {
				td.innerHTML = voltageAliasList[a[i].getValue()[0].id];
			} else {
				td.innerHTML = a[i].getLocalizedValueLabel();
			}
		}
	}

    if (idx != null) {
        var row = locator.rows[idx];
        if (row != null) {
            for (var i = 2; i < this.locator.columns.length; i++) {
                if (row[i] != null && row[i] != "null") {
                    var column = this.locator.columns[i];
                    if (configurator.getAttribute(column.id) == null && column.isVisibleInDetails) {
                        var tr = document.createElement("tr");
                        e.appendChild(tr);
                        tr.className = (count++ % 2 == 0) ? "cdsEvenRow" : "cdsOddRow";
                        var td = document.createElement("td");
                        tr.appendChild(td);
                        td.className = "cdsAttributeTableCellLeft";
                        var label = column.label;
                        if (locator.measurementSystem != null) {
                            if (locator.measurementSystem == "metric") {
                                if (column.unitMetric != null) {
                                    label += " (" + column.unitMetric + ")";
                                }
                            } else {
                                if (column.unitEnglish != null) {
                                    label += " (" + column.unitEnglish + ")";
                                }
                            }
                        }
                        td.innerHTML = label;
                        td = document.createElement("td");
                        tr.appendChild(td);
                        td.className = "cdsAttributeTableCellRight";
                        td.innerHTML = locator.getDisplayValue(row[i], column);
                    }
                }
            }
        }
    }
}

function initVoltageSelect(attributeId) {
    var a = configurator.getAttribute(attributeId), i, e, opt;

    e = document.getElementById("enterAssemblyVoltageSelect");
    for (i = 0; i < a.values.length; i++) {
        opt = document.createElement("option");
        e.appendChild(opt);
        opt.value = ascoNetVoltageMap[a.values[i].id];
        opt.innerHTML = a.values[i].label;
    }
}

window.onload = function() {
	configurator.render();
    nestingManager.enforce();
    locator.initFromJSON(leJson);

    if (requestParameters["data"] != null) {
        configurator.setValue(requestParameters["data"]);
    }
    configurator.doPostEvent(null, "click", null);
    for (var k in helpImages) {
        var img = new Image();
        img.src = helpImages[k];
    }

    initVoltageSelect("aVoltage");
}

window.onUnload = function() {
	configurator.destroy();
	ruleManager.destroy();
    nestingManager.destroy();
}


