﻿function ChangePageView(id, viewName) {
    var multiPage = $find(id);
    var pageView = multiPage.findPageViewByID(viewName);
    if (pageView)
        pageView.set_selected(true);
}

function SetSelectedValue(dropDownList, selectValue) {
    if (dropDownList) {
        for (var i = 0; i < dropDownList.options.length; i++) {
            if (dropDownList.options[i].value == selectValue) {
                dropDownList.selectedIndex = i;
                return;
            }
        }
    }
}

function SetSelectedValueA(dropDownList, selectValue) {
    SetSelectedValue($get(dropDownList), selectValue);    
}

function ClearDropDownList(dropDownList) {
    if (dropDownList) {
        var count;
        for (count = 0; count < dropDownList.options.length; count++) {
            dropDownList.remove(count);
        }
    }
}

function ClearRadSelectionA(dropDownList) {
    ClearRadSelection($find(dropDownList));
}

function ClearRadSelection(dropDownList) {
    SetRadSelectedText(dropDownList, null);
    dropDownList.clearSelection();
}

function SetRadSelectedTextValue(dropDownList, selectedValue, selectedText) {
    SetRadSelectedText(dropDownList, selectedText);
    SetRadSelectedValue(dropDownList, selectedValue);
}

function SetRadSelectedValue(dropDownList, selectedValue) {
    if (selectedValue) {
        if (dropDownList.get_items().get_count() > 0) {
            var node = dropDownList.findItemByValue(selectedValue);
            if (node) {
                node.select();
            }
        }
        else { dropDownList.set_value(selectedValue); }
    }
    else { dropDownList.set_value(''); }
}

function SetRadSelectedTextA(dropDownList, selectedText) {
    SetRadSelectedText($find(dropDownList), selectedText);
}

function SetRadSelectedText(dropDownList, selectedText) {
    if (selectedText) {
        if (dropDownList.get_items().get_count() > 0) {
            var node = dropDownList.findItemByText(selectedText);
            if (node) {
                node.select();
            }
        }
        else { dropDownList.set_text(selectedText); }
    }
    else { dropDownList.set_text(''); }
}

function SetRadSelectedA(dropDownList, selectedValue, selectedText) {
    SetRadSelected($find(dropDownList), selectedValue, selectedText);
}

function SetRadSelected(dropDownList, selectedValue, selectedText) {
    if (selectedText) {
        dropDownList.set_text(selectedText);
    }
    if (selectedValue) {
        dropDownList.set_value(selectedValue);
    }
    SetRadSelectedTextValue(dropDownList, selectedValue, selectedText);
}

function GetRadSelectedValueA(dropDownList) {
    return GetRadSelectedValue($find(dropDownList));
}

function GetRadSelectedValue(dropDownList) {
    var item = dropDownList.get_selectedItem();
    var itemValue = dropDownList.get_value();
    if (item) {
        return item.get_value();
    }
    else if (itemValue) {
        return itemValue;
    }
    else {
        return '';
    }
}

function GetRadSelectedTextOnlyA(dropDownList) {
    return GetRadSelectedTextOnly($find(dropDownList));
}

function GetRadSelectedTextOnly(dropDownList) {
    var itemText = dropDownList.get_text();
    if (itemText === undefined) {
        return '';
    }
    if ((itemText.Length===0) || (itemText === 'null') || (itemText === '')) {
        return '';
    }
    return itemText;
}

function GetRadSelectedTextA(dropDownList) {
    return GetRadSelectedText($find(dropDownList));
}

function GetRadSelectedText(dropDownList) {
    var item = dropDownList.get_selectedItem();
    var itemText = dropDownList.get_text();
    if (item) {
        return item.get_text();
    }
    else if (itemText) {
        return itemText;
    }
    else {
        return '';
    }
}

function GetRadSelectedAttributeValueA(dropDownList, attributeName, attributeDefault) {
    return GetRadSelectedAttributeValue($find(dropDownList), attributeName, attributeDefault);
}

function GetRadSelectedAttributeValue(dropDownList, attributeName, attributeDefault) {
    var item = dropDownList.get_selectedItem();
    if (item) {
        try {
            return item.get_attributes().getAttribute(attributeName);
        }
        catch (err) {
            return attributeDefault;
        }
    }
    else {
        return attributeDefault;
    }
}

function SetSelectedTextA(dropDownList, selectValue) {
    SetSelectedText($get(dropDownList), selectValue);
}

function SetSelectedText(dropDownList, selectText) {
    for (var i = 0; i < dropDownList.options.length; i++) {
        if (dropDownList.options[i].text == selectText) {
            dropDownList.selectedIndex = i;
            return;
        }
    }
}

function GetSelectedValue(dropDownList) {
    if (dropDownList) {
        if (dropDownList.options.length > 0) {
            return dropDownList.options[dropDownList.selectedIndex].value;
        }
        else { return ''; }
    }
    else { return null; }
}

function GetSelectedValueA(dropDownList) {
    return GetSelectedValue($get(dropDownList));
}

function GetSelectedText(dropDownList) {
    if (dropDownList) {
        if (dropDownList.options.length > 0) {
            return dropDownList.options[dropDownList.selectedIndex].text;
        }
        else { return ''; }
    }
    else { return null; }
}

function GetSelectedTextA(dropDownList) {
    return GetSelectedText($get(dropDownList));
}

function SetRadTextBoxValueA(textBox, value) {
    SetRadTextBoxValue($find(textBox), value);
}

function HasValue(value) {
    var test = typeof (value);
    if (test == 'string' || test == 'number') {
        return true;
    }
    else {
        return false;
    }
}

function SetRadTextBoxValue(textBox, value) {
    try {
        if (HasValue(value)) {
            textBox.set_value(value);
        }
        else {
            textBox.set_value('');
        }
    }
    catch (ex) { }
}

function expandCollapse1(CurrentImageId, AreaId) {
    var img = document.getElementById(CurrentImageId);
    var obj1 = document.getElementById(AreaId);

    if (obj1.style.display == "none") {
        obj1.style.display = "block";
        img.src = "/images/components/minus.gif";
    }
    else {
        obj1.style.display = "none";
        img.src = "/images/components/plus.gif";
    }
}

function expandCollapse2(CurrentImageId, Area1Id, Area2Id) {
    var img = document.getElementById(CurrentImageId);
    var obj1 = document.getElementById(Area1Id);
    var obj2 = document.getElementById(Area2Id);

    if (obj1.style.display == "none") {
        obj1.style.display = "block";
        obj2.style.display = "none";
        img.src = "/images/components/minus.gif";
    }
    else {
        obj1.style.display = "none";
        obj2.style.display = "";
        img.src = "/images/components/plus.gif";
    }
}

function copyToClipboard(obj) {
    var s;
    var clipObj = document.getElementById(obj)
    if (null != clipObj) {
        s = clipObj.innerHTML;
    }
    else {
        s = "";
    }

    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData("Text", s);
    }
    else {
        // You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        // create a transferable
        var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        // specify the data we wish to handle. Plaintext in this case.
        trans.addDataFlavor('text/unicode');

        // To get the data from the transferable we need two new objects
        var str = new Object();
        var len = new Object();

        str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

        var copytext = meintext;

        str.data = copytext;

        trans.setTransferData("text/unicode", str, copytext.length * [[[[2]]]]);

        var clipid = Components.interfaces.nsIClipboard;

        if (!clip) return false;

        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
}

function copyTextToClipboard(obj) {
    var s;
    if (null != obj) {
        s = obj;
    }
    else {
        s = "";
    }

    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData("Text", s);
    }
    else {
        // You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

        var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip) return;

        // create a transferable
        var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
        if (!trans) return;

        // specify the data we wish to handle. Plaintext in this case.
        trans.addDataFlavor('text/unicode');

        // To get the data from the transferable we need two new objects
        var str = new Object();
        var len = new Object();

        str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);

        var copytext = meintext;

        str.data = copytext;

        trans.setTransferData("text/unicode", str, copytext.length * [[[[2]]]]);

        var clipid = Components.interfaces.nsIClipboard;

        if (!clip) return false;

        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
}

function GetQueryStringVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return '';
    //alert('Query Variable ' + variable + ' not found');
}

function GetDataFromControl(ControlID) {
    var Ctrl = document.getElementById(ControlID);
    return (Ctrl) ? escape(Ctrl.value) : "";
}

function ValidatePage(ValidationGroup) {
    if (typeof (Page_ClientValidate) == 'function') Page_ClientValidate(ValidationGroup);
    if (Page_IsValid) {
        return true;
    }
    else {
        return false;
    }
}

function ClearRadCalendarA(ClientId) {
    ClearRadCalendar($find(ClientId));
}

function ClearRadCalendar(calendar) {
    var dates = calendar.get_selectedDates();
    calendar.unselectDates(dates);
}

function GetRadCalendarDate(calendar) {
    var selectedDate = calendar.get_selectedDate();
    var dateInput = calendar.get_dateInput();
    var datePattern = calendar.get_calendar().DateTimeFormatInfo.ShortDatePattern;
    var rt = dateInput.get_dateFormatInfo().FormatDate(selectedDate, datePattern);
    return rt;
}

function GetRadCalendarDateA(calendarId) {
    GetRadCalendarDate($find(calendarId));
}

function getRadioValueA(radioObj) {
    return getRadioValue(document.getElementsByName(radioObj));
}

function getRadioValue(radioObj) {
    if (!radioObj)
        return "";
    var radioLength = radioObj.length;
    if (radioLength == undefined)
        if (radioObj.checked)
        return radioObj.value;
    else
        return "";
    for (var i = 0; i < radioLength; i++) {
        if (radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function setRadioValueA(radioObj, newValue) {
    return setRadioValue(document.getElementsByName(radioObj), newValue);
}

function setRadioValue(radioObj, newValue) {
    if (!radioObj)
        return;
    var radioLength = radioObj.length;
    if (radioLength == undefined) {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for (var i = 0; i < radioLength; i++) {
        radioObj[i].checked = false;
        if (radioObj[i].value == newValue.toString()) {
            radioObj[i].checked = true;
        }
    }
}

function onSilverlightError(sender, args) {

    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}

function GetRadSelectedDate(dt) {
    return dt.get_dateInput().get_value();
}

function GetRadSelectedDateA(dt) {
    return GetRadSelectedDate($find(dt));
}