/** configurator object **/
configHelper = new function() {
    var oSystem = {};
    var oSections = {};
    var oRunStatus = { bExpand: true, bInited: false };
    var oInitStatus = { curcat: {}, cursec: {}, curpart: {} };
    var oUIs = { menubar: null,
        caseheader: null, casebig: null, casesmall: null,
        formbox: null, form: null,
        cats: null, formtabs: null, viewswitch: null,
        price1: null, price2: null, shipdate1: null, shipdate2: null,
        bigimg: null, chat: null,
        btnext: null, btpre: null
    };

    this.runstatus = function() {
        return oRunStatus;
    };

    this.get_part = function(secidobj, partid) {
        var oRtn = null;
        var oSec = typeof (secidobj) == "string" ? oSections[secidobj] : secidobj;
        if (oSec) {
            for (var iPart = 0; iPart < oSec.parts.length; iPart++) {
                var oPart = oSec.parts[iPart];
                if (oPart.id == partid) { oRtn = oPart; break; }
            }
        }
        return oRtn;
    };

    this.conf_beg_ = function(args) {
        oSystem = { id: args[0], key: args[1], fc: args[2], fs: args[3], p: args[4], n: args[5], cats: [] };
    };

    this.cat_beg_ = function(args) {
        var oCat = { id: args[0], n: args[1], secs: [] };
        oSystem.cats.push(oCat);

        oInitStatus.curcat = oCat;
    };

    this.sec_beg_ = function(args) {
        var oSec = { id: args[0], spec: args[1], mlt: args[2], def: args[3], odef: null, n: args[4], h: args[5], f: args[6], parts: [], rmdparts: [] };
        oSections[oSec.id] = oSec;
        oInitStatus.curcat.secs.push(oSec);

        oInitStatus.cursec = oSec;
    };

    this.part_beg_ = function(args) {
        var oPart = { id: args[0], chk: args[1], op: args[2], /*original price*/p: args[2], def: args[3], odef: null, to: args[4], n: args[5], sn: args[6], sp: args[7], pic: args[8], spic: args[9], l360: args[10], lcolors: [], fcolors: [], glys: [], attrs: [] };
        if (args[11]) oPart.attrs.tp = args[11];
        oPart.sn = oPart.sn ? oPart.sn.replace("%name%", oPart.n) : oPart.n;
        oInitStatus.cursec.parts.push(oPart);
        if (oInitStatus.cursec.def == oPart.id) {
            oInitStatus.cursec.odef = oPart;
            oPart.chk = 1;
            oPart.op = oPart.p = 0;
        }

        oInitStatus.curpart = oPart;
    };

    this.attr_beg_ = function(args) {
        var oAttr = { id: args[0], op: args[1], /*original price*/p: args[1], n: args[2], sn: args[3], sp: args[4], pic: args[5], chk: 0 };
        oAttr.sn = oAttr.sn ? oAttr.sn.replace("%name%", oAttr.n) : oAttr.n;
        oInitStatus.curpart.attrs.push(oAttr);
        if (oInitStatus.curpart.def == oAttr.id) {
            oAttr.op = oAttr.p = 0;
            oAttr.chk = 1;
            oInitStatus.curpart.odef = oAttr;
        }
    };

    this.lcolor_beg_ = function(args) {
        var oColor = { tp: args[0], ico: args[1], pic: args[2] };
        oInitStatus.curpart.lcolors.push(oColor);
    };

    this.fcolor_beg_ = function(args) {
        var oColor = { tp: args[0], ico: args[1], pic: args[2] };
        oInitStatus.curpart.fcolors.push(oColor);
    };

    this.gallery_beg_ = function(args) {
        var oGallry = { ico: args[0], pic: args[1] };
        oInitStatus.curpart.glys.push(oGallry);
    };

    this.rush_beg_ = function(args) {
        for (var i = 0; i < args.length; i += 2) {
            var oRush = this.get_part("RUSH", args[i]);
            if (oRush) oRush.rn = args[i + 1];
        }
    };

    var sort_tmb_parts = function(a, b) {
        if (a.to > b.to) return 1;
        else if (a.to == b.to) return 0;
        else return -1;
    };

    this.init_tmb_parts = function() {
        for (var id in oSections) {
            var oSec = oSections[id];
            for (var i = 0; i < oSec.parts.length; i++) {
                var oPart = oSec.parts[i];
                if (oPart.to > 0) oSec.rmdparts.push(oPart);
            }
            if (oSec.rmdparts.length > 1) oSec.rmdparts.sort(sort_tmb_parts);
        }
    };

    this.init = function(args) {
        this.init_tmb_parts();

        //load | edit system process
        var bUpdate = false;
        if (typeof (config_editForm) == "function") {
            if (!config_editForm()) {
                alert("Price or Options have changed. Re-select");
                window.location = window.location;
                return;
            } else {
                bUpdate = true;
            }
        }

        conf_init_JQobjects(oUIs);
        if (bUpdate) {
            //update price
            conf_getUIs().price1.html("$" + oSystem.p);
            conf_getUIs().price2.html("$" + oSystem.p);

            //update rush date
            var sNewDate = oSections["RUSH"].odef.rn;
            conf_getUIs().shipdate1.html(sNewDate);
            conf_getUIs().shipdate2.html(sNewDate);
        }

        conf_create_catstab(oSystem.cats, oUIs.cats);
        conf_create_secs(oSystem.cats, oUIs.form);

        oRunStatus.bInited = true;
    };

    this.getUIs = function(args) {
        return oUIs;
    };

    this.getAttrDefStr = function(secidobj, partidobj, attridobj, is_sn, is_opt, is_chked) {
        var sRtn = "";
        var oSec = typeof (secidobj) == "string" ? oSections[secidobj] : secidobj;
        var oPart = typeof (partidobj) == "string" ? this.get_part(oSec, partidobj) : partidobj;
        if (!oPart) return sRtn;
        var oAttr = null;
        if (typeof (attridobj) == "string") {
            for (var i = 0; i < oPart.attrs.length; i++) {
                if (oPart.attrs[i].id == attridobj) { oAttr = oPart.attrs[i]; break; }
            }
        } else {
            oAttr = attridobj;
        }
        if (!oAttr) return sRtn;

        sRtn += is_sn ? oAttr.sn : oAttr.n;
        if (typeof (is_chked) == "undefined") is_chked = (oAttr.chk == 1);
        if (is_opt) {
            sRtn += is_chked ? '' : '[' + ((oAttr.p * 1) >= 0 ? '+' : '') + oAttr.p + ']';
        } else {
            sRtn += ' <strong mdata="' + oAttr.id + '" ' + (is_chked ? 'class="hide"' : '') + '>[' + ((oAttr.p * 1) >= 0 ? '+' : '') + oAttr.p + ']</strong>';
        }
        return sRtn;
    }

    this.getPartDefStr = function(secidobj, partidobj, is_sn, is_chked, is_sectitle) {
        var sRtn = "";
        var oSec = typeof (secidobj) == "string" ? oSections[secidobj] : secidobj;
        var oPart = typeof (partidobj) == "string" ? this.get_part(oSec, partidobj) : partidobj;
        if (!oPart) return sRtn;

        sRtn += is_sn ? oPart.sn : oPart.n;
        if (is_sectitle) {
            if (oPart.odef) sRtn += ' (' + oPart.odef.n + ')';
        } else {
            if (typeof (is_chked) == "undefined") is_chked = oPart.chk;
            if (oPart.odef) sRtn += '<span mdata="a" ' + ((is_chked && (!oSec.mlt)) ? ' class="hide"' : '') + '> (' + oPart.odef.n + ')</span>';
            sRtn += '<strong mdata="p" ' + ((is_chked && (!oSec.mlt)) ? ' class="hide"' : '') + '> [' + ((oPart.p * 1) >= 0 ? '+' : '') + oPart.p + ']</strong>';
            if (oPart.odef) sRtn += '<em mdata="a" ' + ((is_chked && (!oSec.mlt)) ? ' class="hide"' : '') + '> More ' + oPart.attrs.tp + ' ...</em>';
        }

        return sRtn;
    };

    this.getSecDefStr = function(secid) {
        var sRtn = "";
        var oSec = oSections[secid];
        if (!oSec) return sRtn;

        if (oSec.mlt == 0) {
            sRtn = this.getPartDefStr(oSec, oSec.def, false, true, true);
        } else {
            var iCnt = 0;
            for (var i = 0; i < oSec.parts.length; i++) {
                if (oSec.parts[i].chk) {
                    if (sRtn.length == 0) sRtn = this.getPartDefStr(oSec, oSec.parts[i].id, false, true, true);
                    iCnt++;
                }
            }
            if (iCnt > 1) { iCnt--; sRtn += ', and more(<b>' + iCnt + '</b>)'; }
        }

        return sRtn ? sRtn : "None";
    };

    this.calculate_price = function(secid, partid, attrid, ischked) {
        var oSec = oSections[secid];
        var oPart = this.get_part(oSec, partid);
        if (!oPart) return;

        var oAttr = null;
        if (attrid) {
            for (var i = 0; i < oPart.attrs.length; i++) {
                if (oPart.attrs[i].id == attrid) { oAttr = oPart.attrs[i]; break; }
            }
        }

        //delta price
        var idOPrice = oPart.p;
        var idAPrice = oAttr ? oAttr.p : 0;
        var idPrice = idOPrice + idAPrice;
        if (oSec.mlt) {
            oSystem.p += ischked ? idPrice : idPrice * -1;
            oPart.chk = ischked;
            if (ischked) oSec.odef = oPart;
        } else {
            oSystem.p += idPrice;
            oSec.odef = oPart;
            oSec.def = oPart.id;
            for (var iPart = 0; iPart < oSec.parts.length; iPart++) {
                var oPartLoop = oSec.parts[iPart];
                if (oPartLoop.p >= 9999) continue;
                oPartLoop.p = oPart.id == oPartLoop.id ? 0 : oPartLoop.p - idPrice;
                oPartLoop.chk = (oPart.id == oPartLoop.id) ? 1 : 0;
                if (oAttr && oPart.id == oPartLoop.id) {
                    oPart.odef = oAttr;
                    oPart.def = oAttr.id;
                    for (var iAttr = 0; iAttr < oPartLoop.attrs.length; iAttr++) {
                        oAttrLoop = oPartLoop.attrs[iAttr];
                        oAttrLoop.p -= idAPrice;
                        oAttrLoop.chk = (oAttr.id == oAttrLoop.id) ? 1 : 0;
                    }
                }
            }
        }


        return { osys: oSystem, osec: oSec, opart: oPart, oattr: oAttr };
    };

    this.sel = function(objid, secid, partid, attrid, tmborder, toselect) {
        var oJQSelectlist = $("div#seclist_" + secid, conf_getUIs().form);
        var oJQPart = $("#" + objid, oJQSelectlist);

        //hide pr-attrs and show curr-attrs, re-calculate price, update dispaly
        if (oJQPart) {
            var bPartChecked = typeof (toselect) == "undefined" ? oJQPart.attr("checked") : toselect;
            var oRtn = this.calculate_price(secid, partid, attrid, bPartChecked);

            //update system price
            conf_getUIs().price1.html("$" + oRtn.osys.p);
            conf_getUIs().price2.html("$" + oRtn.osys.p);

            //update shipping date
            if (secid == "RUSH") {
                var sNewDate = oRtn.opart.rn;
                conf_getUIs().shipdate1.html(sNewDate);
                conf_getUIs().shipdate2.html(sNewDate);
            }

            //update picture
            var sPic = conf_getpic(oRtn.oattr ? oRtn.oattr : oRtn.opart);
            oJQSelectlist.find("div.previewimg > a > img").attr("src", sPic);

            //update section title
            $("div#secheader_" + secid + ">h3>span").html(this.getSecDefStr(secid));

            var oSec = oRtn.osec;
            //update part options' title & price & attr type(show/hide)
            for (var iPart = 0; iPart < oSec.parts.length; iPart++) {
                var oPart = oSec.parts[iPart];
                var sPartHtmlID = conf_xname(oSec.mlt ? "chk" : "rad", oSec.id, oPart.id, '');
                var sPartHtmlRmdID = oPart.to == 0 ? '' : conf_xname(oSec.mlt ? "rmdchk" : "rmdrad", oSec.id, oPart.id, '');
                var sAttrDivHtmlRmdID = (oPart.to > 0 && oPart.attrs.length > 0) ? conf_xname("rmddiv", oSec.id, oPart.id, '') : '';
                var sHtmlPartLabelRmdId = oPart.to == 0 ? '' : conf_xname((oSec.mlt ? 'labrmdcbx' : 'labrmdrad'), oSec.id, oPart.id, '');
                var sHtmlPartImgRmdId = oPart.to == 0 ? '' : conf_xname((oSec.mlt ? 'imgrmdcbx' : 'imgrmdrad'), oSec.id, oPart.id, '');
                var oJQPart = $("#" + sPartHtmlID, oJQSelectlist);
                var oJQPartLabel = oJQPart.next();
                if (oPart.odef) oJQPartLabel.children('span[mdata="a"]').html(' (' + oPart.odef.n + ')');
                oJQPartLabel.children('strong[mdata="p"]').html(' [' + (oPart.p >= 0 ? '+' : '') + oPart.p + ']');
                if (oPart.chk) {
                    //update list
                    $('#' + sPartHtmlID, oJQSelectlist).attr("checked", true);
                    oJQPartLabel.children("[mdata]").addClass("hide");
                    oJQPartLabel.next().removeClass("hide");
                    //update recommend list
                    if (sPartHtmlRmdID) $('#' + sPartHtmlRmdID, oJQSelectlist).attr("checked", true);
                    if (sAttrDivHtmlRmdID) $('#' + sAttrDivHtmlRmdID, oJQSelectlist).removeClass("hide");
                    if (sHtmlPartLabelRmdId) {
                        var oJQRmdLable = $("#" + sHtmlPartLabelRmdId, oJQSelectlist);
                        $("span[mdata],strong[mdata],em[mdata]", oJQRmdLable).addClass("hide");
                        if (oPart.odef) $("span[mdata]", oJQRmdLable).html(' (' + oPart.odef.n + ')');
                        $("strong[mdata]", oJQRmdLable).html(' [' + (oPart.p >= 0 ? '+' : '') + oPart.p + ']');
                    }
                    if (sHtmlPartImgRmdId) $('#' + sHtmlPartImgRmdId, oJQSelectlist).attr("src", sPic);
                } else {
                    //update list
                    $('#' + sPartHtmlID).attr("checked", false);
                    oJQPartLabel.children().removeClass("hide");
                    oJQPartLabel.next().addClass("hide");
                    //update recommend list
                    if (sPartHtmlRmdID) $('#' + sPartHtmlRmdID, oJQSelectlist).attr("checked", false);
                    if (sAttrDivHtmlRmdID) $('#' + sAttrDivHtmlRmdID, oJQSelectlist).addClass("hide");
                    if (sHtmlPartLabelRmdId) {
                        var oJQRmdLable = $("#" + sHtmlPartLabelRmdId, oJQSelectlist);
                        $("span[mdata],strong[mdata],em[mdata]", oJQRmdLable).removeClass("hide");
                        if (oPart.odef) $("span[mdata]", oJQRmdLable).html(' (' + oPart.odef.n + ')');
                        $("strong[mdata]", oJQRmdLable).html(' [' + (oPart.p >= 0 ? '+' : '') + oPart.p + ']');
                    }
                }

                //just update current part' attrs
                if (oPart.attrs.length == 0 || oPart.id != oRtn.opart.id) continue;
                var sHtmlSelectRmdID = (oPart.to > 0 && oPart.attrs.length > 0) ? conf_xname("rmdsel", oSec.id, oPart.id, '') : '';
                var oJQRmdSelect = sHtmlSelectRmdID ? $('#' + sHtmlSelectRmdID, oJQSelectlist) : null;
                if (oJQRmdSelect) oJQRmdSelect.val(attrid);
                var oJQAttr = oJQPartLabel.next();
                for (var iAttr = 0; iAttr < oPart.attrs.length; iAttr++) {
                    var oAttr = oPart.attrs[iAttr];
                    var sAttrHtmlID = conf_xname("rad", oSec.id, oPart.id, oAttr.id);
                    var oJQAttrList = oJQPartLabel.next();
                    var oJQAttrLabel = oJQAttrList.find("input#" + sAttrHtmlID).next();
                    oJQAttrLabel.children("strong[mdata]").html(' [' + (oAttr.p >= 0 ? '+' : '') + oAttr.p + ']');
                    if (oAttr.chk) {
                        $("#" + sAttrHtmlID, oJQAttrList).attr("checked", true);
                        oJQAttrLabel.children("strong[mdata]").addClass("hide");
                    } else {
                        $("#" + sAttrHtmlID, oJQAttrList).attr("checked", false);
                        oJQAttrLabel.children("strong[mdata]").removeClass("hide");
                    }
                    //update recommend attr
                    if (oJQRmdSelect) $("option:eq(" + iAttr + ")", oJQRmdSelect).html(this.getAttrDefStr(oSec, oPart, oAttr, false, true));
                }
            }
        }
        this.validate(secid);
    };

    this.getSpec = function(p, o) {
        if (!p) return false;
        var oSec = oSections[p];
        var oPart = typeof (o) == "undefined" ? oSec.odef : oParts[o];
        if (!oPart) return false;
        var sName = oPart.n + (oPart.odef ? ' (' + oPart.odef.n + ')' : '');

        window.open("/spec/getspec.aspx?n=" + p + "&v=" + escape(sName).replace(/\+/g, "%2b"), "spec", "width=640,height=480,location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes").focus();
        return false;
    };

    this.edit = function(p, o, a) {
        this.calculate_price(p, o, a, true);
    };

    this.comparePrice = function(price) {
        if (oSystem.p != price) {
            return false;
        }

        return true;
    };

    /** validate functions **/
    this.getoptions = function(secid) { var oSec = oSections[secid]; return oSec ? oSec.parts : []; };
    this.getsels = function(secid, partid) {
        var aryRtn = [];
        var oSec = oSections[secid];
        if (partid.length > 0) {
            var oPart = this.get_part(secid, partid);
            for (var i = 0; i < oPart.attrs.length; i++) {
                var oAttr = oPart.attrs[i];
                if (oAttr.chk) aryRtn.push(oAttr);
            }
        } else {
            for (var i = 0; i < oSec.parts.length; i++) {
                var oPart = oSec.parts[i];
                if (oPart.chk) aryRtn.push(oPart);
            }
        }
        return aryRtn;
    };

    this.getsel = function(secid, partid, defid) {
        var aryRlt = this.getsels(secid, partid);
        return aryRlt.length > 0 ? aryRlt[0] : this.get_part(secid, defid);
    };

    this.getJQid = function(secidobj, partidobj) {
        var oSec = typeof (secidobj) == "string" ? oSections[secidobj] : secidobj;
        var oPart = typeof (partidobj) == "string" ? this.get_part(oSec, partidobj) : partidobj;
        return (oSec.mlt ? "cbx_" : "rad_") + oSec.id + "_" + oPart.id;
    };

    this.setsel = function(secid, partid, attrid, tosel) {
        var oPart = this.get_part(secid, partid);
        oPart.chk = tosel;
        $("#" + this.getJQid(secid, partid)).attr("checked", tosel);
        this.sel(null, secid, partid, attrid, 0, tosel);
    };

    this.validate = function(p) {
        if (p == "RAID" || p == "HDD" || p == "HDD2" || p == "HDD3" || p == "HDD4") this.checkraid();
        if (p == "OS" || p.substr(0, 4) == "SOFT" || p.substr(0, 6) == "MSGAME") this.checkms();
        if (p == "MOTHERBOARD" || p == "VIDEO" || p == "VIDEO2") this.checkcfAttr();
        if (p == "MOTHERBOARD" || p == "VIDEO" || p == "VIDEO2" || p == "VIDEO3") this.checkvideo3();
        if (p == "CPU" || p == "FREEBIE_CU") { this.checkfreebie_cu(); this.checkfreebie4GPD_cu(); }
        if (p == "MEMORY" || p == "FREEBIE_RM") this.checkfreebie_rm();
        if (p == "OS" || p == "SOFT") this.checkmsof07();
        if (p == "OS" || p == "FREEBIE_OS") this.checkms2008();
        if (p == "OS" || p == "OS_UPGRADE") this.checkos7_upgrade();
        if (p == "VIDEO" || p == "VC_GAMES") this.checkfreebie_bspsf4();
// TJANDRA - 10/30/2009 12:07:01 PM  --  no more preinstall        if (p == "WIN7_PREINSTALL" || p == "OS_UPGRADE") this.checkos7_preinstall();
        if (p == "VIDEO" || p == "FREEGAME_VC02") this.checkfreebie_amdvc1();
        if (p == "VIDEO" || p == "FREEGAME_VC") this.checkfreebie_ativc();
        if (p == "VIDEO" || p == "VIDEO2" || p == "FREEBIE_VC") this.checkfreebie_aoc();
    };

    //Age of Conan
    this.checkfreebie_aoc = function() {
        var ol = this.getoptions("FREEBIE_VC");
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk) {
                var parseval_o = ol[i].id;
                if (parseval_o == "FREE_AOC") {
                    var n = ol[i].n;
                    var optid = this.getsel("VIDEO", "", "VC_NONE").id;
                    if (optid.substring(0, 4) != "FX88" && optid.substring(0, 4) != "FX96" && optid.substring(0, 4) != "FX98" && optid.substring(0, 5) != "FXGTX" && optid.substring(0, 6) != "VC_OCZ" && optid.substring(0, 8) != "VC_JHL90" && optid.substring(0, 9) != "VC_M9008D" && optid.substring(0, 9) != "VC_MS1722" && optid.substring(0, 13) != "VC_9800MGT512") {
                        alert(n + " is only available on systems configured with 8800, 9600, 9800 and GTX 200 series video card.");
                        this.setsel("SOFT", ol[i].id, null, true);
                    }
                }
            }
        }
    };

    this.checkvideoSliOS = function() {
        var ok = false;
        // check OS Selection and Video Card Selection
        var vc2 = this.getsel("VIDEO2", "", "VC_NONE").id;
        var vc3 = this.getsel("VIDEO3", "", "VC_NONE").id;
        var os = this.getsel("OS", "", "OS_NONE").id;
        var osa = this.getsel("OS", os, "OS_NONE").id;
        ok |= (osa.indexOf("OS64") == 0 || osa.indexOf("OS32") == 0);

        if (!ok) {
            //No video 3
            if (vc2 == "FX9800GX21GB" && vc3 == "VC_NONE") {
                alert("3 way SLI mode must use Window Vista Operating System");
                this.setsel("VIDEO2", "VC_NONE", null, true);
            }
            else if (vc2 != "VC_NONE" && vc3 != "VC_NONE") {
                alert("3 way SLI mode must use Window Vista Operating System");
                setsel("VIDEO3", "VC_NONE", null, true);
            }
        }
    };

    this.checkraid = function() {
        var raid = this.getsel("RAID", "", "RAID_NONE").id;
        if (raid != "RAID_NONE") {
            var hdd1 = this.getsel("HDD", "", "").id;
            var hdd2 = this.getsel("HDD2", "", "").id;
            var hdd3 = this.getsel("HDD3", "", "").id;
            var hdd4 = this.getsel("HDD4", "", "").id;

            if ((raid == "RAID_0" || raid == "RAID_1") && hdd1 != hdd2) {
                alert("You must have 2 identical Serial ATA hard drives to have RAID function.");
                this.setsel("RAID", "RAID_NONE", null, true);
            } else if ((raid == "RAID_0X4" || raid == "RAID_1X4" || raid == "RAID_10X4") && (hdd1 != hdd2 || hdd1 != hdd3 || hdd1 != hdd4)) {
                alert("You must have 4 identical Serial ATA hard drives to have the selected RAID function.");
                this.setsel("RAID", "RAID_NONE", null, true);
            }
        }
    };

    this.check939 = function() {
        var cpu = this.getsel("CPU", "", "").id;
        var mb = this.getsel("MOTHERBOARD", "", "").id;
        if ((cpu.substr(0, 2) == "A9" && mb.substr(0, 2) != "A9") || (cpu.substr(0, 2) != "A9" && mb.substr(0, 2) == "A9")) {
            alert("939-pin processors can only be used with 939-pin socket motherboards.");
        }
    };

    this.checkquadcore = function() {
        var cpu = this.getsel("CPU", "", "").id;
        var mb = this.getsel("MOTHERBOARD", "", "").id;
        if (cpu.indexOf("_QX9650") == -1 && cpu.indexOf("_Q") > -1 && mb.indexOf("_Q") == -1) {
            alert("You must select a Quad-Core supporting motherboard for the CPU you choose.");
        }
    };

    this.checkqx9650 = function() {
        var cpu = this.getsel("CPU", "", "").id;
        var mb = this.getsel("MOTHERBOARD", "", "").id;
        if (cpu.indexOf("_QX9650") > -1 && mb.indexOf("_QX9650") == -1 && mb.indexOf("IMB_ASUS") == -1) {
            alert("You must select a QX9650 supporting motherboard for the CPU you choose.");
        }
    }

    this.checkqx9770 = function() {
        var cpu = this.getsel("CPU", "", "").id;
        if (cpu.indexOf("_QX9770") > -1) {
            var mb = this.getsel("MOTHERBOARD", "", "").id;
            if (mb.indexOf("_QX9650_3WAY") == -1 && mb.indexOf("_QX9650_4WAY") == -1 && mb.indexOf("_QX9650_CF") == -1) {
                alert("You must select a QX9650 or QX9770 supporting motherboard");
                return;
            }
        }
    };

    this.checkfree_msgame = function() {
        var os = this.getsel("OS", "", "").id;
        var freeos = this.getsel("FREEBIE_OS", "", "").id;
        if (freeos.substring(0, 7) == "FREE_MS") {
            //check OS 
            if (os == "OS_PREMIUM" || os == "OS_BUZ" || os == "OS_ULTIMATE" || os == "OS_BASIC") { }
            else {
                alert("Free Microsoft game requires a purchase of any Microsoft Windows Vista Operating System.");
                this.setsel("FREEBIE_OS", "FREE_NONE", null, true);
            }
        }
    };

    this.checkms_ = function(p) {
        var ol = this.getoptions(p);
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk) {
                var id = ol[i].id;
                var n = ol[i].n;
                if (id.substr(0, 5) == "SW_MS" || id.substr(0, 7) == "GAME_MS") {
                    alert("Purchase of " + n + " requires a purchase of any Microsoft Windows Operating System.");
                    this.setsel(p, id, null, false);
                }
            }
        }
    };

    this.checkms2008 = function() {
        var freeos = this.getsel("FREEBIE_OS", "", "FREE_NONE").id;
        if (freeos != "FREE_NONE") {
            var os = this.getsel("OS", "", "OS_NONE").id;
            if (os.substr(0, 8) != "OS_BASIC" && os.substr(0, 10) != "OS_PREMIUM" && os.substr(0, 11) != "OS_ULTIMATE" && os.substring(0, 6) != "OS_BUZ" && os.substring(0, 12) != "WIN7_PREMIUM" && os.substring(0, 8) != "WIN7_PRO" && os.substring(0, 13) != "WIN7_ULTIMATE") {
                alert("To get a FREE game of Microsoft®Flight Simulator Standard or HALO 2, a purchase of Microsoft®Windows 7™ or Window Vista™ operating system must be required.");
                this.setsel("FREEBIE_OS", "FREE_NONE", null, true);
            }
        }
    };

    this.checkms = function() {
        var os = this.getsel("OS", "", "OS_NONE").id;
        if (os == "OS_NONE") {
            this.checkms_("SOFT");
            this.checkms_("MSGAME");
        }
    };

    this.checkfreebie_rm = function() {
        var freerm = this.getsel("FREEBIE_RM", "", "FREE_NONE").id;
        if (freerm != "FREE_NONE") {
            var m1 = this.getsel("MEMORY", "", "FREE_NONE").id;
            var memopt = this.getsel("MEMORY", m1, "").id;
            //check attribute
            if (memopt != "KingstonX") {
                alert("Only Kingston HyperX memory is eligible to claim the Free 4GB Flash Drive.");
                this.setsel("FREEBIE_RM", "FREE_NONE", null, true);
            }
            return;
        }
    };

    this.checkcf = function() {
        var vc2 = this.getsel("VIDEO2", "", "VC_NONE").id; ;
        if (vc2 == "RX300XTPCIE_128" || vc2 == "RX550PCIE_256" || vc2 == "RX1650P256" || vc2 == "RX1950PRO256" || vc2 == "RX1950XTX512" ||
			vc2 == "RX2400XT256" || vc2 == "RX2600PRO512" || vc2 == "RX2600XT256" || vc2 == "RX2900XT512" || vc2 == "RX2900XT1GB" || vc2 == "RX3450") {
            var mb = this.getsel("MOTHERBOARD", "", "").id;
            if (mb.indexOf("_CF") == -1) {
                alert("You must use a CrossFire supporting motherboard to have CrossFire videocards.\n" +
						"Please select the correct motherboard first and then reselect the second video card");
                this.setsel("VIDEO2", "VC_NONE", null, true);
                return;
            }
            var vc1 = this.getsel("VIDEO", "", "VC_NONE").id;
            if ((vc2 == "RX300XTPCIE_128" && vc1 != "RX300XTPCIE_128") ||
				(vc2 == "RX550PCIE_256" && vc1 != "RX550PCIE_256") ||
				(vc2 == "RX1650P256" && vc1 != "RX1650P256") ||
				(vc2 == "RX1950PRO256" && vc1 != "RX1950PRO256") ||
				(vc2 == "RX1950XTX512" && vc1 != "RX1950XTX512") ||
				(vc2 == "RX2400XT256" && vc1 != "RX2400XT256") ||
				(vc2 == "RX2600PRO512" && vc1 != "RX2600PRO512") ||
				(vc2 == "RX2600XT256" && vc1 != "RX2600XT256") ||
				(vc2 == "RX2900XT512" && vc1 != "RX2900XT512") ||
				(vc2 == "RX2900XT1GB" && vc1 != "RX2900XT1GB") ||
				(vc2 == "RX3450" && vc1 != "RX3450")) {

                alert("The first video card is not compatible with " + getname(x_name("", "VIDEO2", vc2, ""), "") + "\n" +
						"Please select the correct video card first and then reselect the second video card.");
                this.setsel("VIDEO2", "VC_NONE", null, true);
            }
        }
    };

    this.checkcfAttr = function() {
        var ovc2 = this.getsel("VIDEO2", "", "VC_NONE");
        var vc2 = ovc2.id;
        if (vc2 == "RX3450" || vc2 == "RX3650" || vc2 == "RX3870_X2" ||
	     vc2 == "RX4670512" || vc2 == "RX4850" || vc2 == "RX4870512" || vc2 == "RX4870X2" ||
	     vc2 == "RX4350512" || vc2 == "RX4550512" || vc2 == "RX4650512" || vc2 == "RX4670512" || vc2 == "RX4830512" || vc2 == "RX48501GB") {
            var mb = this.getsel("MOTHERBOARD", "", "").id;
            if (mb.indexOf("_CF") == -1) {
                alert("You must use a CrossFire supporting motherboard to have CrossFire videocards.\n" +
						"Please select the correct motherboard first and then reselect the second video card");
                this.setsel("VIDEO2", "VC_NONE", null, true);
                return;
            }

            var vc1 = this.getsel("VIDEO", "", "VC_NONE");
            if (
				(vc2 == "RX3450" && vc1 != "RX3450") ||
				(vc2 == "RX3650" && vc1 != "RX3650") ||
				(vc2 == "RX3870_X2" && vc1 != "RX3870_X2") ||
				(vc2 == "RX4670512" && vc1 != "RX4670512") ||
				(vc2 == "RX4830512" && vc1 != "RX4830512") ||
				(vc2 == "RX4850" && vc1 != "RX4850") ||
				(vc2 == "RX48501GB" && vc1 != "RX48501GB") ||
				(vc2 == "RX4870512" && vc1 != "RX4870512") ||
				(vc2 == "RX4870X2" && vc1 != "RX4870X2") ||
				(vc2 == "RX4350512" && vc1 != "RX4350512") ||
				(vc2 == "RX4550512" && vc1 != "RX4550512") ||
				(vc2 == "RX4650512" && vc1 != "RX4650512") ||
				(vc2 == "RX4670512" && vc1 != "RX4670512") ||
				(vc2 == "RX4830512" && vc1 != "RX4830512") ||
				(vc2 == "RX48501GB" && vc1 != "RX48501GB")
				) {
                alert("The first video card is not compatible with " + ovc2.n + "\n" +
						"Please select the correct video card first and then reselect the second video card.");
                this.setsel("VIDEO2", "VC_NONE", null, true);
            }
            else {
                // else check both attributes
                var ok1 = this.getsel("VIDEO", vc1, "").id;
                var ok2 = this.getsel("VIDEO", vc2, "").id;

                if ((vc1 == "RX3450" && vc2 == "RX3450") || (vc1 == "RX3650" && vc2 == "RX3650") || (vc1 == "RX3870_X2" && vc2 == "RX3870_X2")) {
                    if ((ok1 == "MAJOR" && ok2 != "MAJOR") || (ok1 == "PowerColor" && ok2 != "PowerColor") || (ok1 == "Sapphire" && ok2 != "Sapphire")) {
                        alert("The selections of the second video card you selected is not compatible with " + ovc2.n + "\n" +
                            "Please reselect the second video card.");
                        this.setsel("VIDEO2", vc2, ok1, true);
                        this.setsel("VIDEO2", "VC_NONE", null, true);
                        return;
                    }
                }
            }
        }
    };

    this.checkvideo3 = function() {
        var vc3 = this.getsel("VIDEO3", "", "VC_NONE").id;
        if (vc3 != "VC_NONE") {
            var mb = this.getsel("MOTHERBOARD", "", "").id;
            if (mb.indexOf("_3WAY") == -1) {
                alert("Your selection of motherboard does not support three video cards.");
                this.setsel("VIDEO3", "VC_NONE", null, true);
                return;
            }
            var vc1 = this.getsel("VIDEO", "", "VC_NONE").id;
            var vc2 = this.getsel("VIDEO2", "", "VC_NONE").id;
            if (vc1 != vc3 && vc2 != vc3) {
                alert("You must select three identical video cards for 3-way SLI.");
                this.setsel("VIDEO3", "VC_NONE", null, true);
            }
        }
    };

    /* For Video Card 3 new ID from VC_NONE to VC3_NONE 10/9/2009 */
    this.checkvideo3_2 = function() {
        var vc3 = getsel("VIDEO3", "", "VC3_NONE").id;
        if (vc3 != "VC3_NONE") {
            var mb = this.getsel("MOTHERBOARD", "", "").id;
            if (mb.indexOf("_3WAY") == -1) {
                alert("Your selection of motherboard does not support three video cards.");
                this.setsel("VIDEO3", "VC3_NONE", null, true);
                return;
            }
            var vc1 = this.getsel("VIDEO", "", "VC_NONE").id;
            var vc2 = this.getsel("VIDEO2", "", "VC2_NONE").id;
            if (vc1 != vc3 && vc2 != vc3) {
                alert("You must select three identical video cards for 3-way SLI.");
                this.setsel("VIDEO3", "VC3_NONE", null, true);
            }
        }
    };

    this.checkvideoSliVista = function() {
        var vc3 = getsel("VIDEO3", "", "VC_NONE");
        if (vc3 != "VC_NONE") {
            var mb = this.getsel("MOTHERBOARD", "", "");
            if (mb.indexOf("_3WAY") == -1) {
                alert("Your selection of motherboard does not support three video cards.");
                this.setsel("VIDEO3", "VC_NONE", null, true);
                return;
            }
            var vc1 = this.getsel("VIDEO", "", "VC_NONE").id;
            var vc2 = this.getsel("VIDEO2", "", "VC_NONE").id;
            if (vc1 != vc3 && vc2 != vc3) {
                alert("You must select three identical video cards for 3-way SLI.");
                this.setsel("VIDEO3", "VC_NONE", null, true);
            }
        }
    };

    this.checktvrc = function() {
        var tvc = this.getsel("TVRC", "", "TVRC_NONE").id;
        if (tvc != "TVRC_NONE") {
            var os = this.getsel("OS", "", "OS_NONE").id;
            if (os.substr(0, 6) != "OS_MCE" && os.substr(0, 10) != "OS_PREMIUM" && os.substr(0, 11) != "OS_ULTIMATE") {
                alert("Your selection of Operating System is not eligible for purchasing any Media Center item.");
                this.setsel("TVRC", "TVRC_NONE", null, true);
            }
        }
    };

    this.checkpurchase_bsp = function() {
        var ol = this.getoptions("SOFT");
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk) {
                if (ol[i].id == "SW_BATTLEPACIFIC") {
                    var n = ol[i].n;
                    var vc = this.getsel("VIDEO", "", "VC_NONE").id;
                    if (vc.substr(0, 2) != "FX") {
                        alert(n + " is only available on systems configured with NVIDIA video card.");
                        this.setsel("SOFT", ol[i].id, null, false);
                    }
                }
            }
        }
    };

    this.checkfreebie_bsp = function() {
        var ol = this.getoptions("VC_GAMES");
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk) {
                if (ol[i].id == "SW_BATTLEPACIFIC") {
                    var n = ol[i].n;
                    var vc = this.getsel("VIDEO", "", "VC_NONE").id;
                    if (vc.substr(0, 4) != "FX98" && vc.substr(0, 4) != "FXGT" && vc != "FXNV") {
                        alert(n + " is only available on systems configured with NVIDIA 9800GT or above video card.");
                        this.setsel("VC_GAMES", ol[i].id, null, false);
                    }
                }
            }
        }
    };

    this.checkfreebie_bspsf4 = function() {
        var vcgame = this.getsel("VC_GAMES", "", "FREE_NONE").id;
        if (vcgame != "FREE_NONE") {
            var v1 = this.getsel("VIDEO", "", "VC_NONE").id;
            if (v1.substr(0, 4) != "FX98" && v1.substr(0, 4) != "FXGT" && v1.substr(0, 4) != "FXNV" && v1 != 'FX210512' && v1 != 'VC_I7NTBK' && v1 != 'VC_M57TU' && v1 != 'VC_M860TU') {
                alert("You need to select an NVIDIA GeForce 9800 series or higher video card to be eligible for the free item.");
                this.setsel("VC_GAMES", "FREE_NONE", null, true);
            }
        }
    };
    
    this.checkfreebie_bspsf4_fx = function() {
        var vcgame = this.getsel("VC_GAMES", "", "FREE_NONE").id;
        if (vcgame != "FREE_NONE") {
            var v1 = this.getsel("VIDEO", "", "VC_NONE").id;
            if (v1.substr(0, 2) != "FX") {
                alert("You need to select an NVIDIA Video Card to be eligible for the free item.");
                this.setsel("VC_GAMES", "FREE_NONE", null, true);
            }
        }
    };


    this.checkfreebie_amdvc1 = function() {
        var freebie = this.getsel("FREEGAME_VC02", "", "FREE_NONE").id;
        if (freebie != "FREE_NONE") {
            var v1 = this.getsel("VIDEO", "", "VC_NONE").id;
            if (freebie != "FREE_BF" && freebie != "FREE_SR") {
                if (v1.substr(0, 4) != "FX98" && v1.substr(0, 4) != "FXGT" && v1.substr(0, 4) != "FXNV" && v1 != "FX210512") {
                    alert("You need to select an NVIDIA GeForce 9800 series or higher video card to be eligible for the free item.");
                    this.setsel("FREEGAME_VC02", "FREE_NONE", null, true);
                }
            } else {
                if (v1.substr(0, 2) != "RX") {
                    alert("You need to select an ATI Video Card to be eligible for the free item.");
                    this.setsel("FREEGAME_VC02", "FREE_NONE", null, true);
                }
            }
        }
    };

    this.checkfreebie_vc = function() {
        var freevc2 = this.getsel("FREEBIE_VC2", "", "FREE_NONE").id;
        if (freevc2 != "FREE_NONE") {
            var v1 = this.getsel("VIDEO", "", "VC_NONE").id;
            var v2 = this.getsel("VIDEO2", "", "VC_NONE").id;
            var v3 = this.getsel("VIDEO3", "", "VC_NONE").id;
            if (v1.substr(0, 4) != "FX88" && v2.substr(0, 4) != "FX88" && v3.substr(0, 4) != "FX88" && v1.substr(0, 4) != "FX96" && v2.substr(0, 4) != "FX96" && v3.substr(0, 4) != "FX96" && v1.substr(0, 4) != "FX98" && v2.substr(0, 4) != "FX98" && v3.substr(0, 4) != "FX98") {
                alert("You need to select an NVIDIA GeForce 8800 series or higher video card to be eligible for the free item.");
                this.setsel("FREEBIE_VC2", "FREE_NONE", null, true);
            }
        }
    };

    this.checkfreebie_amdvc1_fx = function() {
        var freebie = this.getsel("FREEGAME_VC02", "", "FREE_NONE").id;
        if (freebie != "FREE_NONE") {
            var v1 = this.getsel("VIDEO", "", "VC_NONE").id;
            if (freebie != "FREE_BF" && freebie != "FREE_SR") {
                if (v1.substr(0, 2) != "FX" ) {
                    alert("You need to select an NVIDIA video card to be eligible for the free item.");
                    this.setsel("FREEGAME_VC02", "FREE_NONE", null, true);
                }
            } else {
                if (v1.substr(0, 2) != "RX") {
                    alert("You need to select an ATI Video Card to be eligible for the free item.");
                    this.setsel("FREEGAME_VC02", "FREE_NONE", null, true);
                }
            }
        }
    };

    this.checkfreebie_ativc = function() {
        var freevc = this.getsel("FREEGAME_VC", "", "FREE_NONE");
        if (freevc != "FREE_NONE") {
            var v1 = this.getsel("VIDEO", "", "VC_NONE").id;
            if (v1.substr(0, 2) != "RX") {
                alert("You need to select an ATI Video Card to be eligible for the free item.");
                this.setsel("FREEGAME_VC", "FREE_NONE", null, true);
            }
        }
    };

    this.checkos7_upgrade = function() {
        var osid = this.getsel("OS", "", "OS_NONE").id;
        var ok = this.getsel("OS", osid, ""); if (ok) ok = ok.id;
        var upgradeid = this.getsel("OS_UPGRADE", "", "OS7_NONE").id;
        var upgradeopt = this.getsel("OS_UPGRADE", upgradeid, ""); if (upgradeopt) upgradeopt = upgradeopt.id;

        if (upgradeid != "OS7_NONE") {
            if ((osid == "WIN7_PREMIUM" && upgradeid != "OS7_NONE") || (osid == "WIN7_PRO" && upgradeid != "OS7_NONE") || (osid == "WIN7_ULTIMATE" && upgradeid != "OS7_NONE") || (osid == "OS_PREMIUM" && upgradeid != "OS7_PREMIUM") || (osid == "OS_BUZ" && upgradeid != "OS7_PRO") || (osid == "OS_ULTIMATE" && upgradeid != "OS7_ULTIMATE") || (osid == "OS_NONE" && upgradeid != "OS7_NONE") || (osid == "OS_XPPRO" && upgradeid != "OS7_NONE") || (osid == "OS_BASIC" && upgradeid != "OS7_NONE") || (osid == "OS_XPHOME" && upgradeid != "OS7_NONE") || (osid == "OS_XPPRO" && upgradeid != "OS7_NONE")) {
                alert("Only Vista can upgrade to Windows 7 " + "\n" + "* Vista Home Premium can only upgrade to Windows 7 Home Premium " + "\n" + "* Vista Business can only upgrade to Windows 7 Professional " + "\n" + "* Vista Ultimate can only upgrade to Windows 7 Ultimate ");
                this.setsel("OS_UPGRADE", "OS7_NONE", null, false);
            }
            else if ((osid == "OS_PREMIUM" && upgradeid == "OS7_PREMIUM") || (osid == "OS_BUZ" && upgradeid == "OS7_PRO") || (osid == "OS_ULTIMATE" && upgradeid == "OS7_ULTIMATE")) {
                //check OS bit
                if (upgradeopt != ok) {
                    alert("Vista 32-bit OS can only upgrade to Windows 7 32-bit OS" + "\n" + "Vista 64-bit OS can only upgrade to Windows 7 64-bit OS");
                    this.setsel("OS_UPGRADE", upgradeid, ok, true);
                    this.setsel("OS_UPGRADE", "OS7_NONE", null, true);
                }
            }
            //setsel("OS_UPGRADE", "OS7_NONE");
        }
    };

// TJANDRA - 10/30/2009 12:07:01 PM  --  no more preinstall
/*    this.checkos7_preinstall = function() {
        var osid = this.getsel("WIN7_PREINSTALL", "", "WIN7_NONE").id;
        var upgradeid = this.getsel("OS_UPGRADE", "", "OS7_NONE").id;

        if (upgradeid == "OS7_NONE" && (osid == "WIN7_YES" || osid == "WIN7_NO")) {
            alert(" Windows 7 Upgrade Option Service is only available to those who choose Windows 7 Upgrade coupon. ");
            this.setsel("WIN7_PREINSTALL", "WIN7_NONE", null, true);
        }
    };*/

    this.checkfreebie_cu = function() {
        var ol = this.getoptions("FREEBIE_CU");
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk) {
                if (ol[i].id == "FREEAOE3" || ol[i].id == "FREEROL") {
                    var n = ol[i].n;
                    var cpu = this.getsel("CPU", "", "CPU_NONE").id;
                    if (cpu.indexOf("_ZM") == -1 && cpu.indexOf("_X4") == -1 && cpu.indexOf("_X2") == -1 && cpu.indexOf("_FX") == -1 && cpu.indexOf("_QFX") == -1 && cpu.indexOf("_MT") == -1 && cpu.indexOf("_ML") == -1 && cpu.indexOf("_TL") == -1) {
                        alert(n + " is only available on systems configured with X2, FX, or Turion CPU.");
                        this.setsel("CPU", cpu, null, false);
                    }
                }
            }
        }
    };

    this.checkfreebie4GPD_cu = function() {
        var ol = this.getoptions("FREEBIE_CU");
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk) {
                if (ol[i].id == "FREE4GPD") {
                    var n = ol[i].n;
                    var cpu = this.getsel("CPU", "", "CPU_NONE").id;
                    if (cpu.indexOf("_QX9") == -1 && cpu.indexOf("_Q9") == -1) {
                        alert(n + " is only available on Intel systems configured with Q9000 OR QX9000 Series CPUs.");
                        this.setsel("CPU", cpu, null, false);
                    }
                }
            }
        }
    };

    this.checkmsof07 = function() {
        var ol = this.getoptions("SOFT");
        for (var i = 0; i < ol.length; i++) {
            if (ol[i].chk && ol[i].id == "SW_MSOFREE") {
                var n = ol[i].n;
                var optid = this.getsel("OS", "", "OS_NONE").id;
                if (optid != "OS_BASIC" && optid != "OS_PREMIUM" && optid != "OS_BUZ" && optid != "OS_BUZ64" && optid != "OS_ULTIMATE" && optid != "WIN7_PREMIUM" && optid != "WIN7_PRO" && optid != "WIN7_ULTIMATE") {
                    alert(n + " is only available on systems configured with Microsoft Vista operating system.");
                    this.setunsel("SOFT", ol[i].id, true);
                }
                break;
            }
        }
    };

    this.test = function() { return oSystem; };
    //this.test1 = function() { return oSections; };
};

/** wrappers **/
function conf_beg_() { configHelper.conf_beg_(arguments); }
function cat_beg_() { configHelper.cat_beg_(arguments); }
function sec_beg_() { configHelper.sec_beg_(arguments); }
function part_beg_() { configHelper.part_beg_(arguments); }
function lcolor_beg_() { configHelper.lcolor_beg_(arguments); }
function fcolor_beg_() { configHelper.fcolor_beg_(arguments); }
function gallery_beg_() { configHelper.gallery_beg_(arguments); }
function attr_beg_() { configHelper.attr_beg_(arguments); }
function rush_beg_() { configHelper.rush_beg_(arguments); }

function conf_init() { configHelper.init(arguments); }
function conf_getUIs() { return configHelper.getUIs(arguments); }
function conf_getSecDefStr(secid) { return configHelper.getSecDefStr(secid); }
function conf_getPartStr(secidobj, partidobj) { return configHelper.getPartDefStr(secidobj, partidobj, true); }
function conf_getAttrStr(secidobj, partidobj, attridobj, is_opt) { return configHelper.getAttrDefStr(secidobj, partidobj, attridobj, true, is_opt); }
function conf_idname(x) { return "id=\"" + x + "\" name=\"" + x + "\""; }
function conf_xname(n, secid, partid, attrid) { return (n == "" ? "" : n + "_") + secid + (partid == "" ? "" : "_" + partid + (attrid == "" ? "" : "_" + attrid)); }
function conf_sel(obj, secid, partid, attrid, tmborder, toselect) { return configHelper.sel(obj, secid, partid, attrid, tmborder, toselect); }
function conf_gs(p, o) { return configHelper.getSpec(p, o); }
function config_comparePrice(price) { return configHelper.comparePrice(price); }
function config_edit(p, o, a) { configHelper.edit(p, o, a); }

/** html renders and operators  **/
function conf_getpic(opart) {
    var sPic = "";
    if (opart) { sPic = opart.odef ? opart.odef.pic : opart.pic; }
    if (!sPic) sPic = "spacer.gif";
    if (sPic.substr(0, 1) != "/") sPic = "/images/" + sPic;
    return sPic;
}

function conf_case_showhide(bshow) {
    var oUIs = conf_getUIs();
    var oJQHeader = oUIs.caseheader, oJQCasebig = oUIs.casebig, oJQCasesmall = oUIs.casesmall;
    var toShow = typeof (bshow) != "undefined" ? bshow : oJQHeader.attr("class") == "c_title_hide";
    if (toShow) {
        oJQHeader.attr("class", "c_title_show");
        oJQCasebig.removeClass("hide");
        oJQCasesmall.addClass("hide");
    } else {
        oJQHeader.attr("class", "c_title_hide");
        oJQCasebig.addClass("hide");
        oJQCasesmall.removeClass("hide");
    }
}

function conf_init_JQobjects(oUIs) {
    if (!oUIs) return;
    oUIs.menubar = $("div#navigations");
    oUIs.caseheader = $("h2#conf_case_header");
    oUIs.casebig = $("div#conf_infoarea_big");
    oUIs.casesmall = $("div#conf_infoarea_small");
    oUIs.formbox = $("div#conf_form_box");
    oUIs.cats = $("ul#conf_cats");
    oUIs.formtabs = $("div#conf_form_tabs");
    oUIs.viewswitch = $("a#conf_view_switch");
    oUIs.price1 = $("strong#config_price1");
    oUIs.price2 = $("strong#config_price2");
    oUIs.shipdate1 = $("span#config_date1");
    oUIs.shipdate2 = $("span#config_date2");
    oUIs.bigimg = $("img#config_bigimg");
    oUIs.chat = $("div#config_chat");
    oUIs.form = $("form#config_form");
    oUIs.btnext = $("#btn_continue");
    oUIs.btpre = $("#btn_prevpage");
};

function conf_create_catstab(aryCats, JQCats) {
    if (!aryCats) return;

    var sHtml = '';
    for (var i = 0; i < aryCats.length; i++) {
        sHtml += '<li><a mdata="' + aryCats[i].id + '" id="config_cats_' + aryCats[i].id + '" class="' + (i == 0 ? "currentab" : "waitab") + '" onclick="return conf_switch_cats(' + i + ');" href="#">' + aryCats[i].n + '</a></li>';
    }
    JQCats.html(sHtml);
}

function conf_switch_cats(idx, bshowfirst) {
    //change cats tab
    var iCur = iNew = iCnt = 0, sCatId = '';
    $("li>a", conf_getUIs().cats).each(function(i) {
        var oJQItm = $(this);
        var sClass = oJQItm.attr("class");
        var sId = oJQItm.attr("mdata");
        iCnt++;
        if (sClass == "currentab") iCur = i;
        if (i == idx) {
            iNew = i;
            sCatId = sId;
        }
        oJQItm.attr("class", i == idx ? "currentab" : "waitab");
    });

    var oJQBtnPre = configHelper.getUIs().btpre;
    var oJQBtnNex = configHelper.getUIs().btnext;

    if (iNew != iCur) {
        //hide sections not in current category
        $('div.itemheader[mcat!="' + sCatId + '"], div.iteminfo[mcat!="' + sCatId + '"]', conf_getUIs().form).addClass("hide");
        $('div.itemheader[mcat!="' + sCatId + '"] >div>img', conf_getUIs().form).each(function(i) { var oJQImg = $(this); oJQImg.attr("src", oJQImg.attr("src").replace("Show", "Hide")); });
        //show sections in current category
        if (configHelper.runstatus().bExpand) {
            $('div.itemheader[mcat="' + sCatId + '"], div.iteminfo[mcat="' + sCatId + '"]', conf_getUIs().form).removeClass("hide");
            $('div.itemheader[mcat="' + sCatId + '"] >div>img', conf_getUIs().form).each(function(i) { var oJQImg = $(this); oJQImg.attr("src", oJQImg.attr("src").replace("Hide", "Show")); });
        } else {
            $('div.itemheader[mcat="' + sCatId + '"]', conf_getUIs().form).removeClass("hide");
        }

        oJQBtnNex.attr("class", (iNew == iCnt - 1) ? "btn_cart" : "btn_continue");
        if (iNew > 0) oJQBtnPre.show(); else oJQBtnPre.hide();
    }

    //show first sec
    if (bshowfirst) conf_expose_sec(conf_getUIs().form.find(".itemheader>h3:visible:eq(0)")[0], true);

    //scroll to first sec
    $('html, body').animate({ scrollTop: conf_getUIs().formbox.offset().top - conf_getUIs().menubar.height() }, 500);

    return false;
}

function conf_continue(obj) {
    //submit
    if ($(obj).attr("class") == "btn_cart") {
        $("form#config_form").submit();
        return false;
    }

    var bNext = $(obj).attr("id") == "btn_continue";

    //change category
    var iCur = 0, iCnt = 0;
    $("li>a", conf_getUIs().cats).each(function(i) {
        if ($(this).attr("class") == "currentab") { iCur = i; }
        iCnt++;
    });
    if (iCur < iCnt - 1 && bNext) conf_switch_cats(iCur + 1, true);
    if (iCur > 0 && (!bNext)) conf_switch_cats(iCur - 1, true);

    return false;
}

function conf_create_secs(aryCats, JQForm) {
    var sCssEx = configHelper.runstatus().bExpand ? '' : ' hide';
    var sImgEye = configHelper.runstatus().bExpand ? 'EyeShow' : 'EyeHide';
    var sBDHtml = new StringBuilder();
    var sSpacer = "-----------------------------------------";
    for (var iCat = 0; iCat < aryCats.length; iCat++) {
        var oCat = aryCats[iCat];
        for (var iSec = 0; iSec < oCat.secs.length; iSec++) {
            var oSec = oCat.secs[iSec];
            sBDHtml.append('<div class="itemheader' + (iCat == 0 ? '' : ' hide') + '" id="secheader_' + oSec.id + '" mcat="' + oCat.id + '" msec="' + oSec.id + '">' +
                    '<input type="hidden" ' + conf_idname("pi") + ' value="' + oSec.id + '" />' +
                    '<input type="hidden" name="' + oSec.id + '" value="' + oSec.n + '" />' +
                    '<h3 title="Show/hide options list for ' + oSec.n + '" class="onlyshow" onclick="conf_expose_sec(this)">' + oSec.n + ': <span>' + conf_getSecDefStr(oSec.id) + '</span></h3>' +
                    '<div class="right-btns"><img onclick="conf_expose_sec(\'' + oSec.id + '\')" src="/template/default/images/' + sImgEye + '.png" title="Show/hide options list for ' + oSec.n + '" />' +
                    (oSec.spec == 1 ? '<a title="Help me choose" href="#" class="helplink" onclick="return conf_gs(\'' + oSec.id + '\')"><img src="/template/default/images/HelpIcon.png" /><span>Help me choose</span></a>' : '') +
                    '</div>' +
                    '</div>');  //itemheader
            sBDHtml.append('<div class="iteminfo' + (iCat == 0 ? sCssEx : ' hide') + '" id="seclist_' + oSec.id + '" mcat="' + oCat.id + '" msec="' + oSec.id + '">');

            //recommend parts list
            var sBDHtmlRmd = new StringBuilder();
            if (oSec.rmdparts.length > 0) {
                sBDHtmlRmd.append('<div class="recommend"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>');
                var oPartsRmd = oSec.rmdparts;
                for (var iPartRmd = 0; iPartRmd < oPartsRmd.length; iPartRmd++) {
                    var oPartRmd = oPartsRmd[iPartRmd];
                    var sHtmlPartRmdId = conf_xname((oSec.mlt ? 'rmdcbx' : 'rmdrad'), oSec.id, oPartRmd.id, '');
                    var sHtmlPartLabelRmdId = conf_xname((oSec.mlt ? 'labrmdcbx' : 'labrmdrad'), oSec.id, oPartRmd.id, '');
                    var sHtmlPartImgRmdId = conf_xname((oSec.mlt ? 'imgrmdcbx' : 'imgrmdrad'), oSec.id, oPartRmd.id, '');
                    var sHtmlPartRmdName = conf_xname('tmbo', oSec.id, '', '');
                    sBDHtmlRmd.append('<td class="' + (iPartRmd == oPartsRmd.length - 1 ? 'nobor' : 'havebor') + '"><div class="nice">' +
                        '<span class="choicebox">' +
                        '<input id="' + sHtmlPartRmdId + '"name="' + sHtmlPartRmdName + '" type="' + (oSec.mlt ? 'checkbox' : 'radio') + '"' + (oPartRmd.chk ? ' checked' : '') + '  onclick="conf_sel(\'' + sHtmlPartRmdId + '\',\'' + oSec.id + '\',\'' + oPartRmd.id + '\',\'\',' + oPartRmd.to + ')" /></span>' +
                        '<div class="nicepro" onclick="conf_sel(\'' + sHtmlPartRmdId + '\',\'' + oSec.id + '\',\'' + oPartRmd.id + '\',\'\',' + oPartRmd.to + ')"><img id="' + sHtmlPartImgRmdId + '" src="' + conf_getpic(oPartRmd) + '" /></div>' +
                        '<label class="niceinfo" id="' + sHtmlPartLabelRmdId + '" for="' + sHtmlPartRmdId + '">' + configHelper.getPartDefStr(oSec, oPartRmd, true, oPartRmd.chk) + '</label>');
                    if (oPartRmd.attrs.length > 0) {
                        sBDHtmlRmd.append('<div id="' + conf_xname("rmddiv", oSec.id, oPartRmd.id, '') + '" class="morecolor' + (oPartRmd.chk == "0" ? ' hide' : '') + '"><b>' + oPartRmd.attrs.tp + '</b>' +
                                    ' <select id="' + conf_xname("rmdsel", oSec.id, oPartRmd.id, '') + '" onchange="conf_sel(\'' + sHtmlPartRmdId + '\',\'' + oSec.id + '\',\'' + oPartRmd.id + '\',this.value,' + oPartRmd.to + ',true)"> ');
                        for (var iAttrRmd = 0; iAttrRmd < oPartRmd.attrs.length; iAttrRmd++) {
                            var oAttrRmd = oPartRmd.attrs[iAttrRmd];
                            sBDHtmlRmd.append('<option value="' + oAttrRmd.id + '"' + (oAttrRmd.chk ? ' selected' : '') + '>' + conf_getAttrStr(oSec, oPartRmd, oAttrRmd, true) + '</option>');
                        }
                        sBDHtmlRmd.append('</select></div>');
                    }
                    sBDHtmlRmd.append('<div></td>');
                }
                sBDHtmlRmd.append('</tr></table></div>'); //recommend
            }

            //part list
            var sBDHtmlList = new StringBuilder('<div class="selectlist">');
            sBDHtmlList.append('<div class="previewimg"><a title="Click to view more info" href="#" onclick="return false;"><img width="80" src="' + conf_getpic(oSec.odef) + '"/><br/> </a></div><dl>');
            if (oSec.h) sBDHtmlList.append('<dd>' + oSec.h + '</dd>');
            var sHtmlSecName = conf_xname('o', oSec.id, '', '');
            if (oSec.mlt) sBDHtmlList.append('<input type="hidden" value="1"' + conf_idname("m_" + oSec.id) + '/>');
            for (var iPart = 0; iPart < oSec.parts.length; iPart++) {
                var oPart = oSec.parts[iPart];
                var sHtmlPartID = conf_xname((oSec.mlt ? 'cbx' : 'rad'), oSec.id, oPart.id, '');

                if (oPart.p >= 9000) { sBDHtmlList.append('<dd><p class="slspace">' + sSpacer + oPart.n + sSpacer + '</p></dd>'); continue; }

                sBDHtmlList.append('<dd>');
                sBDHtmlList.append('<input type="' + (oSec.mlt ? 'checkbox' : 'radio') + '" ' + (oPart.chk ? 'checked' : '') +
                             ' value="' + oPart.op + '|' + oPart.id + '|' + oPart.n.replace(/"/g, "'") + '" name="' + sHtmlSecName + '"' +
                             ' id="' + sHtmlPartID + '" onclick="conf_sel(\'' + sHtmlPartID + '\',\'' + oSec.id + '\',\'' + oPart.id + '\',\'\',' + oPart.to + ')"/>');
                sBDHtmlList.append('<label for="' + sHtmlPartID + '">' + configHelper.getPartDefStr(oSec, oPart, true, oPart.chk) + '</label>');
                if (oPart.attrs.length) {
                    var sHtmlAttName = conf_xname('a', oSec.id, oPart.id, '');
                    sBDHtmlList.append('<dl id="' + conf_xname('attr', oSec.id, oPart.id, '') + '"' + (oPart.id != oSec.def ? ' class="hide"' : '') + '>');
                    for (var iAttr = 0; iAttr < oPart.attrs.length; iAttr++) {
                        var oAttr = oPart.attrs[iAttr];
                        var sHtmlAttrID = conf_xname('rad', oSec.id, oPart.id, oAttr.id);
                        sBDHtmlList.append('<dd><input type="radio" id="' + sHtmlAttrID + '" name="' + sHtmlAttName + '" value="' + oAttr.op + '|' + oAttr.id + '|' + oAttr.n + '"' + (oAttr.id == oPart.def ? ' checked' : '') +
                                     ' onclick="conf_sel(\'' + sHtmlPartID + '\',\'' + oSec.id + '\',\'' + oPart.id + '\',\'' + oAttr.id + '\',' + oPart.to + ')" />' +
                                    '<label for="' + sHtmlAttrID + '">' + conf_getAttrStr(oSec, oPart, oAttr, false) + '</label></dd>');
                    }
                    sBDHtmlList.append("</dl>");
                }

                sBDHtmlList.append('</dd>');
            }
            if (oSec.f) sBDHtmlList.append('<dd>' + oSec.f + '</dd>');
            sBDHtmlList.append('</dl></div>'); //selectlist

            sBDHtml.append(sBDHtmlRmd.toString());
            sBDHtml.append(sBDHtmlList.toString());
            sBDHtml.append('</div>'); //iteminfo
        }
    }
    JQForm.append(sBDHtml.toString());
}

function conf_expose_sec(secidobj, toexpose) {
    if (!secidobj) return false;
    var oJQSecHeader = typeof (secidobj) == "string" ? $("div#secheader_" + secidobj, conf_getUIs().formbox) : $(secidobj).parent();
    var oJQSecHeaderIcon = $("div.right-btns>img", oJQSecHeader);
    var sHeaderIconSrc = oJQSecHeaderIcon.attr("src");
    var oJQSecList = oJQSecHeader.next();
    var bToExpose = false;
    var bExposed = sHeaderIconSrc.indexOf("Show") >= 0;

    //if toexpose not assigned,auto hide/open the list
    if (typeof (toexpose) == "undefined") {
        bToExpose = !bExposed;
    } else {
        bToExpose = toexpose;
    }

    if (bToExpose && !bExposed) { //exposed it
        sHeaderIconSrc = sHeaderIconSrc.replace("Hide", "Show");
        oJQSecHeaderIcon.attr("src", sHeaderIconSrc);
        oJQSecList.removeClass("hide");
    } else if (!bToExpose && bExposed) { //close it
        sHeaderIconSrc = sHeaderIconSrc.replace("Show", "Hide");
        oJQSecHeaderIcon.attr("src", sHeaderIconSrc);
        oJQSecList.addClass("hide");
    }
    return false;
}

function conf_expand_secsall(toexpand) {
    var oJQBtn = conf_getUIs().viewswitch;
    var oJQImg = $("img", oJQBtn);
    var sImgSrc = oJQImg.attr("src");
    var bToExpand = typeof (toexpand) == "undefined" ? sImgSrc.indexOf("Expand") >= 0 : toexpand;
    configHelper.runstatus().bExpand = bToExpand;
    var sTitle = "";

    if (bToExpand) {
        $(".itemheader:visible", conf_getUIs().form).each(function(i) {
            conf_expose_sec($("h3", this), true);
        });
        sImgSrc = sImgSrc.replace("Expand", "Collapase");
        sTitle = "Expand all";
    } else {
        $(".itemheader:visible", conf_getUIs().form).each(function(i) {
            conf_expose_sec($("h3", this), false);
        });
        sImgSrc = sImgSrc.replace("Collapase", "Expand");
        sTitle = "Collapase all";
    }

    oJQImg.attr("src", sImgSrc);
    oJQBtn.attr("title", sTitle);

    return false;
}

function conf_nav_posit() {
    var oJQFormbox = conf_getUIs().formbox;
    var oJQFormtabs = conf_getUIs().formtabs;

    var lwindow_y = $(window).scrollTop();
    var lmenu_h = conf_getUIs().menubar.height();
    lwindow_y += lmenu_h;
    var lformbox_y = oJQFormbox.offset().top;
    var lnav_y = oJQFormtabs.offset().top;

    var iPos = 0;
    if (lwindow_y - lformbox_y >= 0 && lformbox_y + oJQFormbox.height() - lwindow_y - oJQFormtabs.height() >= 0) {
        if ($.browser.msie && $.browser.version < "7") {
            iPos = lwindow_y - lformbox_y;
            oJQFormtabs.css("position", "relative").css("top", iPos + "px");
        } else {
            oJQFormtabs.css("position", "fixed").css("top", lmenu_h + "px");
        }
    } else {
        iPos = lformbox_y + oJQFormbox.height() - lwindow_y - oJQFormtabs.height() <= 0 ? oJQFormbox.height() - oJQFormtabs.height() : 0;
        oJQFormtabs.css("position", "relative").css("top", iPos + "px");
    }
}

function conf_begin_cust() {
    conf_case_showhide(false);
    conf_expose_sec(conf_getUIs().form.find(".itemheader>h3:visible:eq(0)")[0], true);
    $('html, body').animate({ scrollTop: conf_getUIs().caseheader.offset().top - conf_getUIs().menubar.height() }, 500);
    return false;
}

function form_submit() {
    if (configHelper.runstatus().bInited) {
        var oJQForm = $("form#config_form");
        oJQForm.find("input:not(:checked)[name^='o_']").remove();
        oJQForm.find("input:not(:checked)[name^='a_']").remove(); 
        document.config_form.submit();
    }
    return false;
}

function showBigImg(sSrc) {
    var bIs360 = sSrc.indexOf("xml") >= 0;
    var sDivID = "config_360view";
    var sImgID = "config_bigimg";
    if (bIs360) {
        var so = new SWFObject("/flash/360view/360.swf?cfg=" + sSrc, sDivID, "400", "440", "9", "#000000");
        so.addParam("wmode", "Transparent");
        so.write(sDivID);
        $("#" + sImgID).hide();
        $("#" + sDivID).show();
    } else {
        $("#" + sDivID).hide();
        $("#" + sImgID).show();
        ImageLoader.loadImg(sImgID, sSrc);
    }
    return false;
}


/*select fix for IE*/
function fixSelectWidth() {
    if (!$.browser.msie) return;
    $("form#config_form").find("div.morecolor > select").focus(function() {
        var oJQThis = $(this);
        oJQThis.data("oldWidth", oJQThis.css("width"))
            .css("position", "absolute")
			.css("z-index", "1000")
			.css("width", "auto");
    }).blur(function() {
        var oJQThis = $(this);
        oJQThis.css("width", oJQThis.data("oldWidth"));
    });
}

/** start the configurator **/
$('#wrapper').css("cursor", "wait");
$(function() {
    conf_init();
    
    fixSelectWidth();

    conf_nav_posit();
    $(window).scroll(conf_nav_posit);
    $(window).resize(conf_nav_posit);
    $('#wrapper').css("cursor", "auto");
});
