﻿/********************************************************/
/* Author: Matt Morrison                                */
/* Date:   14-Feb-2010                                  */
/* Copyright®:  www.etailsystems.com                     */
/********************************************************/

var selectTypeArray = new Array();
var typeArrayData = new Array();
var superattributespresent = false;

var canCustomHeightSubmit = true;
var canCustomWidthSubmit = true;

function DisplayFormValues() {
    var docForm = document.getElementById('buy_form');
    //Check if the form exists.
    if (docForm != null) {
        var str = '';
        //Get Form elements.
        var vElements = docForm.elements;
        var itemIndex = 0;
        //loop through and get selects.
        for (var i = 0; i < vElements.length; i++) {
            if (vElements[i].type == "select-one") {
                var thisElem = document.getElementById(vElements[i].id);
                if (thisElem.id == "skuid_datastore") {
                    for (var k = 0; k < thisElem.length; k++) {
                        if (thisElem[k].text != '') { typeArrayData.push(thisElem[k].value); }
                    }
                }
                //Used for super attribute exclusions.
                var superAttributeId = thisElem.id.substr(0, 3);
                if (thisElem.id != "skuid_datastore" && thisElem.id != "skuid" && (superAttributeId != "sa_" && superAttributeId != "ra_")) { selectTypeArray.push(thisElem.id); itemIndex++; }
                if (superAttributeId == "sa_" || superAttributeId == "ra_") { superattributespresent = true; }
            }
        }

        var ddl = document.getElementById(selectTypeArray[0]);
        if (ddl != null) {
            ddl.options.length = 0;
            newoption = new Option("select " + selectTypeArray[0].replace('skuid_', '').toLowerCase() + "..", "");
            var boxLength = ddl.length;
            ddl.options[boxLength] = newoption;
            boxLength = ddl.length;
            for (var staIndex = 0; staIndex < typeArrayData.length; staIndex++) {
                var tmpArray = typeArrayData[staIndex].split(',');
                if (tmpArray[1] == selectTypeArray[0].replace('skuid_', '')) {
                    var doesExisit = false;
                    for (var checkNextIndex = 0; checkNextIndex < boxLength; checkNextIndex++) { if (ddl.options[checkNextIndex].text == tmpArray[2]) { doesExisit = true; } }
                    if (!doesExisit) { newoption = new Option(tmpArray[2], typeArrayData[staIndex]); ddl.options[boxLength] = newoption; boxLength = ddl.length; }
                }
            }
        }
        for (var ii = 1; ii < selectTypeArray.length; ii++) {
            var ddl = document.getElementById(selectTypeArray[ii]);
            if (ddl != null) { newoption = new Option("select " + selectTypeArray[ii - 1].replace('skuid_', '').toLowerCase() + " first..", ""); ddl.options[0] = newoption; }
        }
    }
}

function CheckMultiSkuAttributeValue(senderObject) {

    var senderElement = document.getElementById(senderObject);
    var stoppedIndex = 0;
    var vSelectedValue;
    var vSelectedType;
    var searchComaparerType;
    var searchComparerValue;
    var vSelectedArray = new Array();

    if (senderElement != null) {

        var selObjectData = senderElement.options[senderElement.selectedIndex].value.split(',');

        var selectSubmitClear = document.getElementById("skuid");
        selectSubmitClear.options.length = 0;

        if (selObjectData.length > 2) {

            vSelectedType = selObjectData[1];
            vSelectedValue = selObjectData[2];

            for (var objIndex = 0; objIndex < selectTypeArray.length; objIndex++) {
                var ddl = document.getElementById(selectTypeArray[objIndex]);
                if (ddl != null) {
                    var objArray;
                    if (senderObject == ddl.id) {
                        objArray = ddl.options[ddl.selectedIndex].value.split(',');
                        vSelectedArray.push(objArray[1] + "," + objArray[2]);
                        stoppedIndex = objIndex;
                    } else {
                        if (ddl.selectedIndex > 0 && objIndex <= stoppedIndex) {
                            objArray = ddl.options[ddl.selectedIndex].value.split(',');
                            vSelectedArray.push(objArray[1] + "," + objArray[2]);
                        }
                    }
                }
            }
            for (var clearIndex = stoppedIndex + 1; clearIndex < selectTypeArray.length; clearIndex++) {
                var ddlClear = document.getElementById(selectTypeArray[clearIndex]);
                ddlClear.options.length = 0;
                //newoption = new Option("Please select your " + selectTypeArray[clearIndex + 1].replace('skuid_', '').toLowerCase() + "..", "");
                //ddlClear.options[0] = newoption;
            }

            if (selectTypeArray.length > 0) {

                for (var i = 0; i < selectTypeArray.length; i++) {
                    if (selectTypeArray[i] == senderObject) {

                        var nextElement = document.getElementById(selectTypeArray[i + 1]);
                        var nextElementTemp = document.getElementById(selectTypeArray[i + 2]);
                        if (nextElement != null) {
                            nextElement.options.length = 0;
                            newoption = new Option("select your " + selectTypeArray[i + 1].replace('skuid_', '').toLowerCase() + "..", "");
                            var boxLength = nextElement.length;
                            nextElement.options[boxLength] = newoption;
                            boxLength = nextElement.length;
                            if (nextElementTemp != null) {
                                newoption = new Option("select your " + selectTypeArray[i + 1].replace('skuid_', '').toLowerCase() + " first..", "");
                                nextElementTemp.options[0] = newoption;
                            }
                            var nextObjectType = nextElement.name.replace('skuid_', '');
                            for (var j = 0; j < typeArrayData.length; j++) {

                                var arrayObjectData = typeArrayData[j].split(',');

                                //Search for the [1] index to find the Type we are looking for
                                for (var k = 0; k < arrayObjectData.length; k++) {

                                    //Found the type, everything after here is compareable to vSelectedArray[n].
                                    if (arrayObjectData[k] == nextObjectType) {
                                        var subarrayObjectData = arrayObjectData;

                                        //set the actual value to be displayed.
                                        var thisObjectTypeText = arrayObjectData[2];

                                        //loop through the rest of our values and see if they match previously selected objects.
                                        for (var saodIndex = k; saodIndex < subarrayObjectData.length; saodIndex++) {

                                            searchComaparerType = subarrayObjectData[saodIndex];
                                            searchComparerValue = subarrayObjectData[saodIndex + 1];

                                            if (vSelectedType == searchComaparerType && vSelectedValue == searchComparerValue) {
                                                var strComp = "";
                                                for (var nn = 0; nn < vSelectedArray.length; nn++) {
                                                    strComp += vSelectedArray[nn];
                                                    if (nn == 0) { strComp += ","; }
                                                }
                                                if (strComp.lastIndexOf(',') == (strComp.length - 1)) {
                                                    strComp = strComp.substring(0, strComp.lastIndexOf(','));
                                                }
                                                var doesExisit = false;
                                                if (typeArrayData[j].indexOf(strComp) > 0) {

                                                    for (var checkNextIndex = 0; checkNextIndex < nextElement.length; checkNextIndex++) {
                                                        if (nextElement.options[checkNextIndex].text == thisObjectTypeText) { doesExisit = true; }
                                                    }
                                                    if (!doesExisit) {
                                                        newoption = new Option(thisObjectTypeText, typeArrayData[j]);
                                                        nextElement.options[boxLength] = newoption;
                                                        boxLength = nextElement.length;
                                                    }

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else {
                            var selectSubmit = document.getElementById("skuid");
                            if (selectSubmit != null) {
                                newoption = new Option(selObjectData[0] + "-" + selObjectData[1], selObjectData[0]);
                                selectSubmit.options[0] = newoption;
                            }
                        }
                    }
                }
            }
        }
    }
    vSelectedArray = null;
}

function IsNavigatorIE7() {
    var appVer = navigator.appVersion;
    if (appVer.indexOf('MSIE 7.') > 0) { return true; } else { return false; }
}

/*=============================================================================================*/
/*  Default Page Submit for all sites all requests                                             */
/*=============================================================================================*/
function CheckPageForSubmit() {

    var boolCanSubmit = true;
    var selectSubmit = document.getElementById("skuid");

    if (selectSubmit != null) {
        if (selectSubmit.type != 'hidden') {
            if (selectSubmit.options.length == 0) { boolCanSubmit = false; }
            if (selectSubmit.selectedIndex < 0) { boolCanSubmit = false; }
            if (selectSubmit.value == '' || selectSubmit.value == '0') { boolCanSubmit = false; }
        }
        if (boolCanSubmit) {
            if (superattributespresent == true) {
                EtailEncodePersText();
                ValidateSuperAttributes('buy_form');
            }
            else {
                EtailEncodePersText();
                var docForm = document.getElementById('buy_form');
                docForm.submit();
            }
        } else {
            alert("Please select a product to add to your basket!");
        }
    } else {
        if (superattributespresent == true) {
            EtailEncodePersText();
            ValidateSuperAttributes('buy_form');
        }
        else {
            var docForm = document.getElementById('buy_form');
            EtailEncodePersText();
            docForm.submit();
        }
    }
}

function EtailEncodePersText() {
    $('#pers1text').val($('#pers1text').val().replace(/,/g, '~'));
    $('#pers2text').val($('#pers2text').val().replace(/,/g, '~'));
    $('#pers3text').val($('#pers3text').val().replace(/,/g, '~'));
    $('#gifttext').val($('#gifttext').val().replace(/,/g, '~'));
}
/*=============================================================================================*/