﻿if (!Sitecore || Sitecore == 'undefined') {
    var Sitecore = new Object();
}

if (!Sitecore.Wfm || Sitecore.Wfm == 'undefined') {
    Sitecore.Wfm = new Object();
}


Sitecore.Wfm.getCssValue = function(element, attributeName) {
    var pos = element.className.indexOf(" " + attributeName);
    if (element.className.indexOf(attributeName) == 0) {
        pos = 0;
    }
    else {
        ++pos;
    }

    if (pos > -1) {
        pos = pos + attributeName.length + 1;
        try {
            var start = pos;
            var end = element.className.indexOf(" ", pos);
            if (end == -1) {
                end = element.className.length;
            }
            return unescape(element.className.substring(start, end).replace(/\+/g, " "));
        } catch (ex) {

        }
    }
    return element.readAttribute(attributeName);
}

Sitecore.Wfm.getAttributeAncestor = function(element, attributeName, skipme) {

    if (!skipme && element.className != null && element.className.indexOf(" " + attributeName) >= 0) {
        return element;
    }

    var foundelement = Element.up(element, '[class*="' + attributeName + '"]');
    if (foundelement != null) {
        return foundelement;
    }

    var ancestors = element.ancestors();
    ancestors.each(function(item) {
        if (item.className != null && item.className != '' &&
        item.className.indexOf(attributeName) > -1) {
            foundelement = item;
            return;
        }
    })

    if (foundelement != null) {
        return foundelement;
    }

    return Element.up(element, "[" + attributeName + "]")
}



Sitecore.Wfm.updateDateComplexValue = function(obj) {

    var fieldItem = Sitecore.Wfm.getAttributeAncestor(obj, Sitecore.Wfm.AttributeKey.FieldIDKey, true);

    if (fieldItem != null) {
        var id = fieldItem.id;

        var year = $(id + '_year');
        var month = $(id + '_month');
        var day = $(id + '_day');
        if ((year || month || day) != null) {
            var monthValue = month.getValue();
            if (monthValue.length == 1) {
                monthValue = "0" + monthValue;
            }

            var dayValue = day.getValue();
            if (dayValue.length == 1) {
                dayValue = "0" + dayValue;
            }

            $$("#" + id + "_complexvalue")[0].value = year.getValue() + monthValue + dayValue + "T000000";
        }
    }
}

Sitecore.Wfm.updateListComplexValue = function(obj) {
    var fieldItem = Sitecore.Wfm.getAttributeAncestor(obj, Sitecore.Wfm.AttributeKey.FieldIDKey, true);
    if (fieldItem != null) {
        var id = fieldItem.id;

        var complexvalue = "";
        $$("#" + id + " input[type='radio'], #" + id + " input[type='checkbox']").each(function(element) {
            var value = element.getValue()

            if (value == null && element.type != "checkbox") {
                value = element.value;
            }

            if (element.type == "checkbox") {
                if (value == "on") {
                    var label = element.next("label[for]", 0);
                    complexvalue += (", " + (label != null ? label.innerHTML : element.id))
                }
            } else {
                if (element.type == "radio") {
                    if (element.checked) {
                        complexvalue += (", " + value);
                    }
                } else {
                    complexvalue += (", " + value);
                }
            }
        });

        $$("#" + id + "_complexvalue")[0].value = complexvalue.substr(1, complexvalue.length);
    }
}



Sitecore.Wfm.palayAudio = function(target, w, h, path) {

    Plugin.embed("Windows Media", { width: w, height: h, src: '/temp/audion.wav', autoplay: "true" }, target);
    
    return false;
}

//Sitecore.Wfm.updateCreditCardComplexValue = function(obj) {

//    var fieldItem = Element.up(obj, "[fieldid]");
//    if (fieldItem != null) {
//        var id = fieldItem.id;

//        var CardType = Element.down(fieldItem, "[cardType]");
//        var CardNumber = Element.down(fieldItem, "[cardNumber]");

//        if (CardType != null && CardNumber != null) {
//            $$("#" + id + "_complexvalue")[0].value = CardType.getValue() + "/<schidden>" + CardNumber.getValue() + "</schidden>";
//        }
//    }
//}

