﻿// JScript File
var dialog_callback = false;
var logged_in = false;
var logged_out = false;
var selected_tab = null;
var iframe = true;
var aValidateFrames = new Array();
var sLastPanelId = '';
var nDlg1PosX = 0;
var nDlg1PosY = 0;
var nDlg2PosX = 0;
var nDlg2PosY = 0;

var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera",
		    versionSearch: "Version"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function NullFunction() {
}

function ShareValue(nValue) {
    if (nValue == 0) {
        return '';
    }
    else {
        return '(' + nValue + ')';
    }
}

function LogicConvert(nValue) {
    if (nValue == 0) {
        return sResourceGridNo;
    }
    else {
        return sResourceGridYes;
    }
}

function WhichBrowser() {
    var agt = navigator.userAgent.toLowerCase();
    if (agt.indexOf('msie') != -1) {
        return 'IE';
    }
    if (agt.indexOf('firefox') != -1) {
        return 'Firefox';
    }
    if (agt.indexOf('opera') != -1) {
        return 'Opera';
    }
    if (agt.indexOf('safari') != -1) {
        return 'Safari';
    }
    if (agt.indexOf('staroffice') != -1) {
        return 'Star Office';
    }
    if (agt.indexOf('webtv') != -1) {
        return 'WebTV';
    }
    if (agt.indexOf('beonex') != -1) {
        return 'Beonex';
    }
    if (agt.indexOf('chimera') != -1) {
        return 'Chimera';
    }
    if (agt.indexOf('netpositive') != -1) {
        return 'NetPositive';
    }
    if (agt.indexOf('phoenix') != -1) {
        return 'Phoenix';
    }
    if (agt.indexOf('skipstone') != -1) {
        return 'SkipStone';
    }
    if (agt.indexOf('netscape') != -1) {
        return 'Netscape';
    }
    if (agt.indexOf('mozilla/5.0') != -1) {
        return 'Mozilla';
    }
    if (agt.indexOf('\/') != -1) {
        if (agt.substr(0, agt.indexOf('\/')) != 'mozilla') {
            return navigator.userAgent.substr(0, agt.indexOf('\/'));
        }
        else {
            return 'Netscape';
        }
    }
    else if (agt.indexOf(' ') != -1) {
        return navigator.userAgent.substr(0, agt.indexOf(' '));
    }
    else {
        return navigator.userAgent;
    }
}

function GetDateControlSelectedDate(sControl) {
    var dDate;
    var oDay;
    var oMonth;
    var oYear;
    var nDay;
    var nMonth;
    var nYear;
    oDay = $w(sControl + '_day');
    nDay = parseInt(oDay.value, 10);
    oMonth = $w(sControl + '_month');
    nMonth = parseInt(oMonth.value, 10);
    oYear = $w(sControl + '_year');
    nYear = parseInt(oYear.value);
    dDate = new Date();
    dDate.setFullYear(nYear, nMonth - 1, nDay);
    return dDate;
}

function DateToAnsiString(dDate) {
    var nMonth = dDate.getMonth() + 1;
    var nDay = dDate.getDate();
    var nYear = dDate.getFullYear();
    var sDate = nYear.toString() + '-' + nMonth.toString() + '-' + nDay.toString();
    return sDate;
}

function DateToAnsiString2(dDate) {
    var nMonth = dDate.getMonth() + 1;
    var nDay = dDate.getDate();
    var nYear = dDate.getFullYear();
    var nHours = dDate.getHours();
    var sHours = nHours.toString();
    if (sHours.length == 1) {
        sHours = '0' + sHours;
    }
    var nMinutes = dDate.getMinutes();
    var sMinutes = nMinutes.toString();
    if (sMinutes.length == 1) {
        sMinutes = '0' + sMinutes;
    }
    var sDate = nYear.toString() + '-' + nMonth.toString() + '-' + nDay.toString() + ' ' + sHours + ':' + sMinutes;
    return sDate;
}

function AnsiStringToDate(sDate) {
    var dDate = new Date();
    var nYear = parseInt(sDate.substr(0, 4));
    var sMonth = sDate.substr(5, 2);
    if (sDate.substr(5, 1) == '0') {
        sMonth = sDate.substr(6, 1);
    }
    var nMonth = parseInt(sMonth, 10);
    var sDay = sDate.substr(8, 2);
    if (sDate.substr(8, 1) == '0') {
        sMonth = sDate.substr(9, 1);
    }
    var nDay = parseInt(sDay, 10);
    dDate.setFullYear(nYear, nMonth - 1, nDay);
    return dDate;
}

function KeyDownHandler(oEvent, oButton)
{
	if (oEvent.keyCode == 13)
	{
		oEvent.returnValue = false;
		oEvent.cancel = true;
		$w(oButton).click();
	}
}

function KeyDownEvaluator(oEvent, sEval)
{
	if (oEvent.keyCode == 13)
	{
		oEvent.returnValue = false;
		oEvent.cancel = true;

		if (sEval != null || sEval != '')
			eval(sEval);

		return false;
	}
}

function ConsumeKey(oEvent) {
    if (oEvent.keyCode == 13) {
        oEvent.returnValue = false;
        oEvent.cancel = true;
        return false;
    }
}

// load help viewer
function helpPopupWindow(HelpCode, LanguageId) {
    var x = window.screen.width / 2;
    var y = window.screen.height / 2;
    var w;
    w = window.open('help.aspx?Code=' + HelpCode + '&Lang=' + LanguageId, 'HelpWindow', 'resizable=1,status=0,menubar=0,scrollbars=0,toolbar=0,height=300,width=500,screenX=' + x + ',screenY=' + y);
    if (w != null) {
        w.focus();
    }
}

function TutorialPopup(sCode) {
    window.open('http://vimeopro.com/websurveycreator/wsc-training-videos/video/' + sCode);
}

function setCellBGimage(id, sImgSrc) {
    var c = document.getElementById ? $w(id) :
        document.all ? document.all[id] :
        document.layers ? document[id + 'Outer'].document[id + 'Inner'] : null;
    if (document.layers) {
        c.background.src = sImgSrc;
    }
    else {
        c.style.backgroundImage = 'url(' + sImgSrc + ')';
    }
}

function ToggleSpecify(sTag, sChoice) {
    var aElements = getElementsByAttribute('input', 'wspecify', sTag);
    for (nItem = 0; nItem < aElements.length; nItem++) {
        oElement = aElements[nItem];
        if (oElement.value != sChoice) {
            var sId = oElement.id + '_Other';
            var oOther = $w(sId);
            if (oOther != null) {
                oOther.value = '';
                oOther.readOnly = true;
            }
        }
    }
}

function ToggleComment(sCommentEnabledChoicesId, sCommentId, bCommentsEnabledByDefault) {
    var sCommentEnabledChoices = $w(sCommentEnabledChoicesId);
    var oComment = $w(sCommentId);
    var aChoices = sCommentEnabledChoices.value.split(',');
    var iChoice = 0;
    var bAnyChecked = false;
    var bEnabledChecked = false;

    for (iChoice = 0; iChoice < aChoices.length; iChoice++) {
        // check each comment enabled choice that might be checked
        var oChoice = $w(aChoices[iChoice]);
        bAnyChecked |= oChoice.checked;
        if ((oChoice.getAttribute('wcomment')) == 'yes')
            bEnabledChecked |= oChoice.checked;
    }

    // override default to enable (if true)
    if (!bAnyChecked)
        bEnabledChecked |= bCommentsEnabledByDefault;

    // check for comment container (if exists, then we using the long box method (hide/show)
    var oCommentContainer = $w(sCommentId + 'Container');
    if (oCommentContainer != null) {
        if (bEnabledChecked) {
            ShowDiv(oCommentContainer);
            oComment.disabled = false;
        }
        else {
            HideDiv(oCommentContainer);
            oComment.disabled = true;
        }
    }
    else if (oComment != null) {
        if (bEnabledChecked) {
            oComment.disabled = false;
            oComment.style.background = null;
        }
        else {
            oComment.value = '';
            oComment.disabled = true;
            oComment.style.background = '#EAEAEA';
        }
    }
}

function ToggleExclusive(sTag, sChoice) {
    var aElements = getElementsByAttribute('input', 'wexclusive', sTag);
    var bChecked = false;
    var oElement;
    for (nItem = 0; nItem < aElements.length; nItem++) {
        oElement = aElements[nItem];
        if (oElement.value == sChoice) {
            bChecked = oElement.checked;
            break;
        }
    }
    for (nItem = 0; nItem < aElements.length; nItem++) {
        oElement = aElements[nItem];
        if (oElement.value != sChoice) {
            if (bChecked) {
                oElement.checked = false;
                oElement.disabled = true;
                var sId = oElement.id + '_Other';
                var oOther = $w(sId);
                if (oOther != null) {
                    oOther.value = '';
                    oOther.readOnly = true;
                }
            }
            else {
                oElement.disabled = false;
            }
            CheckChangeCss(oElement.id, 1);
        }
    }
}

function ToggleExclusiveGrid(sTag, sChoice) {
    var aElements = getElementsByAttribute('input', 'wexclusive', sTag);
    var bChecked = false;
    var oElement;
    for (nItem = 0; nItem < aElements.length; nItem++) {
        oElement = aElements[nItem];
        if (oElement.value == sChoice) {
            bChecked = oElement.checked;
            if (bChecked) {
                RadioChangeCss(oElement.id, 2);
            }
            else {
                RadioChangeCss(oElement.id, 7);
            }
            break;
        }
    }
    for (nItem = 0; nItem < aElements.length; nItem++) {
        oElement = aElements[nItem];
        var oContainer = $w(oElement.id + '_table');
        if (oElement.value != sChoice) {
            if (bChecked) {
                oElement.checked = false;
                oElement.disabled = true;
                oContainer.setAttribute('disableonclick', oContainer.getAttribute('onclick'));
                oContainer.removeAttribute('onclick', 0);
                GridRadioChangeCss(oElement.id, 7);
            }
            else {
                oElement.disabled = false;
                oContainer.setAttribute('onclick', oContainer.getAttribute('disableonclick'));
                if (oElement.checked) {
                    GridRadioChangeCss(oElement.id, 2);
                }
                else {
                    GridRadioChangeCss(oElement.id, 7);
                }
            }
        }
    }
}

function ToggleDatePicker(bCheckBox, sPickerId) {
    var oDiv1 = $w(sPickerId + '-Div1');
    var oDiv2 = $w(sPickerId + '-Div2');

    if (bCheckBox) {
        oDiv1.style.display = 'none';
        oDiv2.style.display = '';
        this.picker.SetSelectedDate();
    }
    else {
        oDiv1.style.display = '';
        oDiv2.style.display = 'none';
        this.picker.ClearSelectedDate();
    }
}

function DisableInput(form_element) {
    var oElement = $w(form_element);
    if (oElement != null) {
        oElement.disabled = true;
    }
}

function EnableInput(form_element) {
    var oElement = $w(form_element);
    if (oElement != null) {
        oElement.disabled = false;
    }
}

function MakeReadonlyInput(form_element) {
    var oElement = $w(form_element);
    if (oElement != null) {
        oElement.readOnly = true;
    }
}

function MakeWriteInput(form_element) {
    var oElement = $w(form_element);
    if (oElement != null) {
        oElement.readOnly = false;
    }
}

function IsDivHidden(form_element) {
    var bHidden = false;
    if ($w(form_element) != null) {
        bHidden = ($w(form_element).style.display == 'none');
    }
    return bHidden;
}

function HideDiv(form_element) {
    if ($w(form_element) != null) {
        $w(form_element).style.display = 'none';
    }
}

function ShowDiv(form_element) {
    if ($w(form_element) != null) {
        $w(form_element).style.display = '';
    }
}

function UnDisableControls(aControls) {
    var nLength = aControls.length;
    for (var nItem = 0; nItem < nLength; nItem++) {
        var sControl = aControls[nItem];
        var oControl = $w(sControl);
        if (oControl != null) {
            oControl.disabled = false;
        }
    }
}

function DisableControls() {
    var nLength = DisableControls.arguments.length;
    var aDisabled = new Array();
    for (var nItem = 0; nItem < nLength; nItem++) {
        var sControl = DisableControls.arguments[nItem];
        var oControl = $w(sControl);
        if (oControl != null) {
            if (!oControl.disabled) {
                aDisabled.push(sControl);
                oControl.disabled = true;
            }
        }
    }
    return aDisabled;
}

function FloatDiv(form_element) {
    if ($w(form_element) == null) {
        alert(form_element);
    }
    else {
        $w(form_element).style.display = 'block';
    }
}

function DelayedReload() {
    setTimeout('__doPostBack(\'__Page\', \'\');', 1000);
}

function panel_over(oDiv) {
    if (oDiv != null) {
        var sDivName = oDiv.id
        var sClass = $w(sDivName + '_L').className;
        if (sClass != 'panel_yellow_left') {
            $w(sDivName + '_L').className = 'panel_black_left';
            $w(sDivName + '_R').className = 'panel_black_right';
            $w(sDivName + '_T').className = 'panel_black_top';
            if ($w(sDivName + '_S') != null) {
                $w(sDivName + '_S').className = 'panel_black_top';
            }
            $w(sDivName + '_N').className = 'panel_black_next';
        }
    }
}

function panel_out2(oDiv) {
    if (oDiv != null) {
        var sDivName = oDiv.id
        var sClass = $w(sDivName + '_L').className;
        $w(sDivName + '_L').className = 'panel_grey_left';
        $w(sDivName + '_R').className = 'panel_grey_right';
        $w(sDivName + '_T').className = 'panel_grey_top';
        if ($w(sDivName + '_S') != null) {
            $w(sDivName + '_S').className = 'panel_grey_top';
        }
        $w(sDivName + '_N').className = 'panel_grey_next';
    }
}

function panel_out(oDiv) {
    if (oDiv != null) {
        var sDivName = oDiv.id
        var sClass = $w(sDivName + '_L').className;
        if (sClass != 'panel_yellow_left') {
            $w(sDivName + '_L').className = 'panel_grey_left';
            $w(sDivName + '_R').className = 'panel_grey_right';
            $w(sDivName + '_T').className = 'panel_grey_top';
            if ($w(sDivName + '_S') != null) {
                $w(sDivName + '_S').className = 'panel_grey_top';
            }
            $w(sDivName + '_N').className = 'panel_grey_next';
        }
    }
}

function panel_summaryerror(sDivName) {
    var oDiv = $w(sDivName + '_Footer');
    if (oDiv != null) {
        $w(sDivName + '_Footer').className = 'panel_footer_error';
    }
}

function panel_summarynormal(sDivName) {
    var oDiv = $w(sDivName + '_Footer');
    if (oDiv != null) {
        $w(sDivName + '_Footer').className = 'panel_footer_normal';
    }
}

function panel_error(oDiv) {
    if (oDiv != null) {
        var sDivName = oDiv.id
        $w(sDivName + '_L').className = 'panel_yellow_left';
        $w(sDivName + '_R').className = 'panel_yellow_right';
        $w(sDivName + '_T').className = 'panel_yellow_top';
        if ($w(sDivName + '_S') != null) {
            $w(sDivName + '_S').className = 'panel_yellow_top';
        }
        $w(sDivName + '_N').className = 'panel_yellow_next';
    }
}

function ValidateInit() {
    aValidateFrames.length = 0;
}

function ValidateFrameYellowBox() {
    var sCurrFrame
    for (var nLoop = 0; nLoop < aValidateFrames.length; nLoop++) {
        sCurrFrame = aValidateFrames[nLoop];
        if (sCurrFrame.substr(0, 1) == '0') {
            panel_error($w(sCurrFrame.substr(1)));
        }
        else {
            panel_out($w(sCurrFrame.substr(1)));
        }
    }
}

function ValidateFrameAdd(sFrame, bValid) {
    var sCurrFrame
    var bFound;
    if (sFrame != null && sFrame.length > 0) {
        bFound = false;
        for (var nLoop = 0; nLoop < aValidateFrames.length; nLoop++) {
            sCurrFrame = aValidateFrames[nLoop];
            if (sCurrFrame.substr(1) == sFrame) {
                if (!bValid && sCurrFrame.substr(0, 1) == '1') {
                    aValidateFrames[nLoop] = '0' + sFrame;
                    panel_error($w(sFrame));
                }
                bFound = true;
                break;
            }
        }
        if (!bFound) {
            if (bValid) {
                aValidateFrames.push('1' + sFrame);
                panel_out2($w(sFrame));
            }
            else {
                aValidateFrames.push('0' + sFrame);
                panel_error($w(sFrame));
            }
        }
    }
}

function ForceValidate(sGroup) {
    ValidateInit();
    panel_summarynormal(sGroup);
    return Page_ClientValidate(sGroup);
}

function redirect(url) {
    location.href = url;
}

function image_swap(element, filename) {
    var img = ((typeof element).toLowerCase() == 'object') ? element : $w(element);
    img.src = get_image_path(img.src) + filename;
    //alert(img.src);
}

function get_image_path(src) {
    return src.substring(0, (src.lastIndexOf('/') + 1));
}

function get_image_file(src) {
    return src.substring(src.lastIndexOf('/') + 1);
}

function NullEvent() {
}

function setTabAble(id, index, setfocus) {
    var obj = $w(id);
    if (obj != null) {
        obj.tabIndex = index;
        //obj.tabEnabled = true;
        if (setfocus) {
            obj.focus();
        }
    }
}

//	Content rollover functions
function contents_hover(element) {
    var contents = element.getElementsByTagName('div')[0];
    contents.className = contents.className + '-hover';
}

function contents_out(element) {
    var contents = element.getElementsByTagName('div')[0];
    var classname = contents.className.substring(0, contents.className.lastIndexOf('-'));
    contents.className = classname;
}

/* Dialogs */

function DebugAlert(sMessage) {
    if (isdebug) {
        sMessage = "*** Debug ***\n\n " + sMessage;
        alert(sMessage);
    }
}
function ShowAlertDialog(sMessage) {
    sMessage = 'WebSurveyCreator.com\n\n' + sMessage;
    alert(sMessage);
}

function ShowErrorDialog(sMessage, sError) {
    sMessage = 'WebSurveyCreator.com\n\n' + sMessage + '\n\n' + sError;
    alert(sMessage);
}

function ShowConfirmDialog(sMessage) {
    sMessage = 'WebSurveyCreator.com\n\n' + sMessage;
    return confirm(sMessage);
}

function Dialog_OnCloseDialog1(sender, args) {
    window.scrollTo(nDlg1PosX, nDlg1PosY);
    nDlg1PosX = 0;
    nDlg1PosY = 0;
}

function Dialog_OnCloseDialog2(sender, args) {
    window.scrollTo(nDlg2PosX, nDlg2PosY);
    nDlg2PosX = 0;
    nDlg2PosY = 0;
}

function ToolBar1_OnLoad(sender, eventArgs) {
    sender.dispose = function() { } // to stop the hiding of the toolbar;
}

function CallGetKnowledgebase(sCode) {
    var oDiv = $w('knowledgebasediv');
    try {
        var sResult = GetKnowledgebase(sCode);
        oDiv.innerHTML = sResult;
        ShowDiv('knowledgebasediv');
        CreateCookie('wsc-kb', 'true', 365);
    }
    catch (e) {
        CreateCookie('wsc-kb', 'false', 365);
    }
    return false;
}

function CloseKnowledgebase() {
    CreateCookie('wsc-kb', 'false', 365);
    HideDiv('knowledgebasediv');
}

function Logout() {
    var sResult;
    try {
        sResult = ProcessLogout();
        EraseCookie('wsc-session');
        setTimeout('parent.location.href = \'login.aspx\'', 1000);
    }
    catch (e) {
        redirect('login.aspx');
    }
    return false;
}

/* Style Sheet Changes */
function SwitchSurveyTheme(oDropDown) {
    var sTheme = oDropDown.options[oDropDown.selectedIndex].value;
    var oStyleRef = $w('SurveyTheme');
    if (oStyleRef != null) {
        oStyleRef.href = 'themes/' + sTheme + '.css';
    }
}

function RadioChangeValCss(sControl, nStyle) {
    var oDiv = $w(sControl + '_table');

    var sClassName = oDiv.className;
    sClassName = sClassName.replace('-error', '');

    if (nStyle == 5) {
        oDiv.className = sClassName + '-error';
    }
    else if (nStyle == 6) {
        oDiv.className = sClassName;
    }
}

function RadioChangeCss(oControl, nStyle) {
    var oObject = $w(oControl + '_table');
    var oRadio = $w(oControl);
    var sClassName = oObject.className;
    var bChecked = oRadio.checked;
    var bSelected = sClassName.search('-sel') > -1;
    var bEnabled = sClassName.search('-enabled') > -1;
    var bDisabled = sClassName.search('-disabled') > -1;
    var bHover = sClassName.search('-hover') > -1;
    var bNormal = sClassName.search('-normal') > -1;
    var bError = sClassName.search('-error') > -1;
    sClassName = sClassName.replace('-hover', '');
    sClassName = sClassName.replace('-normal', '');
    sClassName = sClassName.replace('-enabled', '');
    sClassName = sClassName.replace('-disabled', '');
    sClassName = sClassName.replace('-error', '');
    sClassName = sClassName.replace('-sel', '');

    if (nStyle == 2) {
        // Focus
        if (bSelected || bChecked) {
            sClassName = sClassName + '-enabled-normal-sel';
        }
        else {
            sClassName = sClassName + '-enabled-normal';
        }
    }
    else if (nStyle == 3) {
        // Over
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover-error';
            }

        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover';
            }
            else {
                sClassName = sClassName + '-enabled-hover';
            }
        }
    }
    else if (nStyle == 4) {
        // Out
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal-error';
            }
        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-normal-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal';
            }
            else {
                sClassName = sClassName + '-enabled-normal';
            }
        }
    }
    else if (nStyle == 7) {
        // Unclick
        if (bHover) {
            sClassName = sClassName + '-enabled-hover';
        }
        else {
            sClassName = sClassName + '-enabled-normal';
        }
    }
    oObject.className = sClassName;
}

function RadioSelect(oControl)
{
	var oInput;
	var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
	var sName = oObject.name;
	var aElements = document.getElementsByName(sName);
	RadioChangeValCss(sName, 6);
	for (nItem = 0; nItem < aElements.length; nItem++)
	{
		if (!aElements[nItem].checked)
		{
			RadioChangeCss(aElements[nItem].id, 7);
			//RadioChangeCss(sName + '_' + nItem, 7);
			MakeReadonlyInput(aElements[nItem].id + '_Other');
			//MakeReadonlyInput(sName + '_' + nItem + '_Other');
		}
	}
	oObject.click();
	MakeWriteInput(oObject.id + '_Other');
	RadioChangeCss(oObject.id, 2);
	if ($w(oObject.id + '_Other') != null)
	{
		$w(oObject.id + '_Other').focus();
	}
}

function CheckBoxSelect(sId)
{
	var oObject = $w(sId);

	if (oObject != null)
	{
		if (oObject.checked)
			oObject.value = '1';
		else
			oObject.value = '0';
	}
}

function CheckChangeCss(oControl, nStyle) {
    var oObject = $w(oControl + '_table');
    var oCheck = $w(oControl);
    var sClassName = oObject.className;
    var bChecked = oCheck.checked;
    var bSelected = sClassName.search('-sel') > -1;
    var bEnabled = sClassName.search('-enabled') > -1;
    var bDisabled = sClassName.search('-disabled') > -1;
    var bHover = sClassName.search('-hover') > -1;
    var bNormal = sClassName.search('-normal') > -1;
    var bError = sClassName.search('-error') > -1;
    sClassName = sClassName.replace('-hover', '');
    sClassName = sClassName.replace('-normal', '');
    sClassName = sClassName.replace('-enabled', '');
    sClassName = sClassName.replace('-disabled', '');
    sClassName = sClassName.replace('-error', '');
    sClassName = sClassName.replace('-sel', '');

    if (nStyle == 2) {
        // Focus
        if (bSelected || bChecked) {
            sClassName = sClassName + '-enabled-normal-sel';
        }
        else {
            sClassName = sClassName + '-enabled-hover';
        }
    }
    else if (nStyle == 3) {
        // Over
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover-error';
            }

        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover';
            }
            else {
                sClassName = sClassName + '-enabled-hover';
            }
        }
    }
    else if (nStyle == 4) {
        // Out
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal-error';
            }
        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-normal-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal';
            }
            else {
                sClassName = sClassName + '-enabled-normal';
            }
        }
    }
    else if (nStyle == 1) {
        // blur
        if (bChecked) {
            sClassName = sClassName + '-enabled-normal-sel';
        }
        else {
            sClassName = sClassName + '-enabled-normal';
        }
    }
    else if (nStyle == 5) {
        // focus
        if (bChecked) {
            sClassName = sClassName + '-enabled-hover-sel';
        }
        else {
            sClassName = sClassName + '-enabled-hover';
        }
    }
    else if (nStyle == 6) {
        // click
        if (bChecked) {
            sClassName = sClassName + '-enabled-normal-sel';
        }
        else {
            sClassName = sClassName + '-enabled-hover';
        }
    }
    else if (nStyle == 7) {
        // Unclick
        sClassName = sClassName + '-enabled-hover';
    }
    oObject.className = sClassName;
}

function CheckSelect(oControl) {
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    //oObject.focus();
    oObject.click();
    if (oObject.checked) {
        CheckChangeCss(oControl, 2);
        MakeWriteInput(oObject.id + '_Other');
        if ($w(oObject.id + '_Other') != null) {
            $w(oObject.id + '_Other').focus();
        }
    }
    else {
        CheckChangeCss(oControl, 7);
        MakeReadonlyInput(oObject.id + '_Other');
    }
}

function CheckSelectOnly(oControl) {
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    if (!oObject.checked) {
        if (!oObject.disabled) {
            oObject.click();
            CheckChangeCss(oControl, 2);
            MakeWriteInput(oObject.id + '_Other');
        }
    }
}

function CheckOtherInput(oControl) {
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    if (oObject.checked) {
        MakeWriteInput(oObject.id + '_Other');
    }
    else {
        MakeReadonlyInput(oObject.id + '_Other');
        if ($w(oObject.id + '_Other') != null) {
            $w(oObject.id + '_Other').value = '';
        }
    }
}

function ChangeCss(oControl, nStyle) {
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    if (oObject != null && oObject.tagName.toLowerCase() == 'select' && BrowserDetect.searchVersion == 'MSIE') {
        return;
    }
    else if (oObject != null) {
        var sClassName = oObject.className;
        var bFocus = sClassName.search('-focus') > -1;
        var bEnabled = sClassName.search('-enabled') > -1;
        var bDisabled = sClassName.search('-disabled') > -1;
        var bHover = sClassName.search('-hover') > -1;
        var bNormal = sClassName.search('-normal') > -1;
        var bError = sClassName.search('-error') > -1;
        sClassName = sClassName.replace('-focus', '');
        sClassName = sClassName.replace('-hover', '');
        sClassName = sClassName.replace('-normal', '');
        sClassName = sClassName.replace('-enabled', '');
        sClassName = sClassName.replace('-disabled', '');
        sClassName = sClassName.replace('-error', '');

        if (oObject.tagName.toLowerCase() == 'select') {
            var aOptGroups = oObject.getElementsByTagName('optgroup');
            if (aOptGroups != null) {
                for (var iGroup = 0; iGroup < aOptGroups.length; iGroup++) {
                    var oGroup = aOptGroups[iGroup];
                    if (oGroup != null) {
                        if (nStyle == 2 || nStyle == 3) {
                            oGroup.className = 'listbox-optgroup-focus';
                        }
                        else {
                            oGroup.className = 'listbox-optgroup';
                        }
                    }
                }

                var aOptNonGroups = oObject.getElementsByTagName('option');
                if (aOptNonGroups != null) {
                    for (var iItem = 0; iItem < aOptNonGroups.length; iItem++) {
                        var oItem = aOptNonGroups[iItem];
                        if (oItem != null) {
                            if (nStyle == 2 || nStyle == 3) {
                                oItem.className = 'listbox-optgroup-item-focus';
                            }
                            else {
                                oItem.className = 'listbox-optgroup-item';
                            }
                        }
                    }
                }
            }
        }

        if (oObject.tagName.toLowerCase() == 'input' || oObject.tagName.toLowerCase() == 'select' || oObject.tagName.toLowerCase() == 'textarea') {
            if (nStyle == 1) {
                // Blur
                if (bFocus) {
                    sClassName = sClassName + '-enabled-normal';
                }
                else if (bDisabled) {
                    sClassName = sClassName + '-disabled-normal';
                }
                else {
                    sClassName = sClassName + '-enabled-normal';
                }
            }
            else if (nStyle == 2) {
                // Focus
                if (bEnabled) {
                    sClassName = sClassName + '-focus-normal';
                }
                else if (bDisabled) {
                    sClassName = sClassName + '-disabled-normal';
                }
                else {
                    sClassName = sClassName + '-enabled-normal';
                }
            }
            else if (nStyle == 3) {
                // Over
                if (bError) {
                    if (bFocus) {
                        sClassName = sClassName + '-focus-hover-error';
                    }
                    else if (bEnabled) {
                        sClassName = sClassName + '-enabled-hover-error';
                    }
                    else if (bDisabled) {
                        sClassName = sClassName + '-disabled-hover-error';
                    }

                }
                else {
                    if (bFocus) {
                        sClassName = sClassName + '-focus-hover';
                    }
                    else if (bEnabled) {
                        sClassName = sClassName + '-enabled-hover';
                    }
                    else if (bDisabled) {
                        sClassName = sClassName + '-disabled-hover';
                    }
                }
            }
            else if (nStyle == 4) {
                // Out
                if (bError) {
                    if (bFocus) {
                        sClassName = sClassName + '-focus-normal-error';
                    }
                    else if (bEnabled) {
                        sClassName = sClassName + '-enabled-normal-error';
                    }
                    else if (bDisabled) {
                        sClassName = sClassName + '-disabled-normal-error';
                    }
                }
                else {
                    if (bFocus) {
                        sClassName = sClassName + '-focus-normal';
                    }
                    else if (bEnabled) {
                        sClassName = sClassName + '-enabled-normal';
                    }
                    else if (bDisabled) {
                        sClassName = sClassName + '-disabled-normal';
                    }
                }
            }
            else if (nStyle == 5) {
                // Not Valid
                if (bFocus) {
                    sClassName = sClassName + '-focus-normal-error';
                }
                else if (bEnabled) {
                    sClassName = sClassName + '-enabled-normal-error';
                }
                else if (bDisabled) {
                    sClassName = sClassName + '-disabled-normal-error';
                }
            }
            else if (nStyle == 6) {
                if (bFocus) {
                    sClassName = sClassName + '-focus-normal';
                }
                else if (bEnabled) {
                    sClassName = sClassName + '-enabled-normal';
                }
                else if (bDisabled) {
                    sClassName = sClassName + '-disabled-normal';
                }
            }
            oObject.className = sClassName;
        }
    }
}



// GRID RADIO STUFF TEST
function GridRadioChangeCss(oControl, nStyle) {
    var oObject = $w(oControl + '_table');
    var oRadio = $w(oControl);
    var sClassName = oObject.className;
    var bChecked = oRadio.checked;
    var bSelected = sClassName.search('-sel') > -1;
    var bEnabled = sClassName.search('-enabled') > -1;
    var bDisabled = sClassName.search('-disabled') > -1;
    var bHover = sClassName.search('-hover') > -1;
    var bNormal = sClassName.search('-normal') > -1;
    var bError = sClassName.search('-error') > -1;
    sClassName = sClassName.replace('-hover', '');
    sClassName = sClassName.replace('-normal', '');
    sClassName = sClassName.replace('-enabled', '');
    sClassName = sClassName.replace('-disabled', '');
    sClassName = sClassName.replace('-error', '');
    sClassName = sClassName.replace('-sel', '');

    if (nStyle == 2) {
        // Focus
        if (bSelected || bChecked) {
            sClassName = sClassName + '-enabled-normal-sel';
        }
        else {
            sClassName = sClassName + '-enabled-normal';
        }
    }
    else if (nStyle == 3) {
        // Over
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover-error';
            }

        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover';
            }
            else {
                sClassName = sClassName + '-enabled-hover';
            }
        }
    }
    else if (nStyle == 4) {
        // Out
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal-error';
            }
        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-normal-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal';
            }
            else {
                sClassName = sClassName + '-enabled-normal';
            }
        }
    }
    else if (nStyle == 7) {
        // Unclick
        if (bHover) {
            sClassName = sClassName + '-enabled-hover';
        }
        else {
            sClassName = sClassName + '-enabled-normal';
        }
    }
    oObject.className = sClassName;
}

function GridRadioSelect(oControl) {
    var oInput;
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    var sName = oObject.name;
    var aElements = document.getElementsByName(sName);
    //RadioChangeValCss(sName, 6);
    for (nItem = 0; nItem < aElements.length; nItem++) {
        if (!aElements[nItem].checked) {
            RadioChangeCss(aElements[nItem].id, 7);
            //RadioChangeCss(sName + '_' + nItem, 7);
            MakeReadonlyInput(aElements[nItem].id + '_Other');
            //MakeReadonlyInput(sName + '_' + nItem + '_Other');
        }
    }
    oObject.click();
    MakeWriteInput(oObject.id + '_Other');
    RadioChangeCss(oObject.id, 2);
    if ($w(oObject.id + '_Other') != null) {
        $w(oObject.id + '_Other').focus();
    }
}

function GridCheckSelect(oControl) {
    var oInput;
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    var sName = oObject.name;
    var aElements = document.getElementsByName(sName);

    //oObject.click();
    if (oObject.checked) {
        oObject.checked = false;
        RadioChangeCss(oObject.id, 7);
    }
    else {
        oObject.checked = true;
        RadioChangeCss(oObject.id, 2);
    }
}

function GridCheckCss(oControl) {
    var oInput;
    var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);
    if (oObject.checked) {
        RadioChangeCss(oObject.id, 2);
    }
    else {
        RadioChangeCss(oObject.id, 7);
    }
}

function GridNumericColumnTotal(sQuestionId, sChoiceId) {
    var oHidden = $w(sQuestionId + '_' + sChoiceId + '_columntotal');
    var oLabel = $w(sQuestionId + '_' + sChoiceId + '_columntotallabel');
    var nTotal = 0;
    var bResult = true;
    var oTotalMax = $w(sQuestionId + '_totalmax');
    var oTotalOperand = $w(sQuestionId + '_totaloperand');
    var nTotalValidation = null;
    var sOperand = '';

    if (oTotalMax != null) nTotalValidation = oTotalMax.value;
    if (oTotalOperand != null) sOperand = oTotalOperand.value;

    var aElements = getElementsByAttribute('input', 'choice', sChoiceId);

    for (var i = 0; i < aElements.length; i++) {
        var oInputBox = $w(aElements[i]);
        var sTemp = oInputBox.value;

        if (sTemp.length > 0) {
            var nValue = parseInt(sTemp);

            if (!isNaN(nValue)) {
                nTotal = nTotal + nValue;
            }
        }
    }

    oHidden.value = nTotal.toString();
    //javascript and hidden questions will not have this...
    if (oLabel != null) oLabel.innerHTML = oHidden.value;

    if (nTotal > 0 && nTotalValidation != null) {
        switch (sOperand) {
            case '1':
                bResult = nTotal == nTotalValidation;
                break;
            case '0':
            default:
                bResult = nTotal <= nTotalValidation;
                break;
        }
    }

    if (!bResult) {
        if (oLabel != null) oLabel.className = 'wsc_grid-rowtotal-errorlabel';
    }
    else {
        if (oLabel != null) oLabel.className = '';
    }

    return bResult;
}

function DropDownListSelect(oControl) {
    var dropdown = $w(oControl);
    var myindex = dropdown.selectedIndex
    if (myindex != null) {
        if (myindex == dropdown.options.length - 1) {
            ShowDiv(oControl + '_Other_Div');
        }
        else {
            HideDiv(oControl + '_Other_Div');
        }
    }
    else {
        HideDiv(oControl + '_Other_Div');
    }
}

function CheckClearSearchToolbar(oToolbar, sText) {
    var oItems = oToolbar.get_items();
    for (var nItem = 0; nItem < oItems.get_length(); nItem++) {
        var oItem = oItems.getItem(nItem);
        if (oItem.get_id() == 'ClearSearchButton') {
            oItem.set_enabled(sText.length > 0);
            break;
        }
    }
}

function IsUrl(s) {
    var sUrlReg = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return sUrlReg.test(s);
}

function ValidateUrl(sender, args) {
    var sUrl = args.Value;
    args.IsValid = IsUrl(sUrl);
    return;
}

function ValidatePassword(sPassword1, sPassword2) {
    var nCount;
    var bSpace = false;
    var sResult = 'false';

    if (sPassword1 != null && sPassword1.length > 0 && sPassword2 != null && sPassword2.length > 0 && sPassword1 == sPassword2) {
        nCount = 0;

        for (var nLoop = 0; nLoop < sPassword1.length; nLoop++) {
            if (sPassword1.substr(nLoop, 1) != ' ') {
                nCount++;
            }
            else if (sPassword1.substr(nLoop, 1) == ' ') {
                bSpace = true;
            }
        }

        if (!bSpace && nCount >= 6)  // must not contain spaces and >= 6 characters
        {
            sResult = 'true';
        }
    }

    return sResult;
}

function ValidateTimeZone(oCountry, oRegion) {
    var nRegions = -1;  // means NOT valid

    if (oCountry != null) {
        var sCountry = oCountry.options[oCountry.selectedIndex].value;

        if (sCountry.length == 0) {
            return nRegions;
        }

        nRegions = parseInt(GetTimeZoneRegionCount(sCountry));

        if (nRegions > 0) {
            if (oRegion != null) {
                var sRegion = oRegion.options[oRegion.selectedIndex].value

                if (sRegion.length == 0) {
                    nRegions = -1;
                }
            }
        }
    }

    return nRegions;
}

function ValidateEmailAddress(sender, args) {
//    var sEmailReg = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$';
    //    var oRegex = new RegExp(sEmailReg);

    var sEmail = args.Value;
    var sResult = 'false'

    sResult = CanUseEmailAddress(sEmail);

    if (sResult == 'true') {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }

    return sResult;
}

function ValidateEmailAddressSimple(sender, args) {
    //    var sEmailReg = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$';
    //    var oRegex = new RegExp(sEmailReg);
    var sEmail = args.Value;

    var sResult = VerifyEmailAddress(sEmail);
    if (sResult == 'true') {
        args.IsValid = true;
    }
    else {
        args.IsValid = false;
    }
    return;
}

function ValidateShareEmailAddress(sender, args) {
    //    var sEmailReg = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$';
    //    var oRegex = new RegExp(sEmailReg);
    var sEmail = args.Value;

    var sResult = CanUseEmailAddress(sEmail);
    if (sResult == 'true') {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
    return;
}

//function ValidateTextListData3(sender, args) {
//    var sTextList = args.Value;
//    if (sTextList.length == 0) {
//        args.IsValid = false;
//        return;
//    }
//    var sResult = ValidateTextList(sTextList, true);
//    if (sResult == 'true') {
//        args.IsValid = true;
//    }
//    else {
//        args.IsValid = false;
//    }
//    return;
//}

/* Support Functions */
function AllTrim(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}
function DisplayGridPageInfo(sError, Grid) {
    var nCurrentPageIndex = Grid.get_currentPageIndex();
    var nPageCount = Grid.get_pageCount();
    var nRecordCount = Grid.get_recordCount();
    var sText = '&nbsp;';
    if (nRecordCount > 0) {
        var nPageIndex = nCurrentPageIndex + 1;
        if (nRecordCount == 1) {
            sText = sResourceGridPage + '&nbsp;<b>' + nPageIndex.toString() + '</b>&nbsp;' + sResourceGridOf + '&nbsp;<b>' + nPageCount.toString() + '</b>&nbsp;(' + nRecordCount.toString() + '&nbsp;' + sResourceGridTotalItem + ')';
        }
        else {
            sText = sResourceGridPage + '&nbsp;<b>' + nPageIndex.toString() + '</b>&nbsp;' + sResourceGridOf + '&nbsp;<b>' + nPageCount.toString() + '</b>&nbsp;(' + nRecordCount.toString() + '&nbsp;' + sResourceGridTotalItems + ')';
        }
    }
    else {
        sText = sError;
    }
    return sText;
}

/* Cookies */
function CreateCookie(sName, sValue, nDays) {
    var dDate = new Date();
    var sExpires = '';
    if (nDays) {
        dDate.setTime(dDate.getTime() + (nDays * 24 * 60 * 60 * 1000));
        sExpires = '; expires=' + dDate.toGMTString();
    }
    document.cookie = sName + '=' + sValue + sExpires + '; path=/';
}

function ReadCookie(sCookie) {

    var nBegin, nEnd;
    var sResult = '';

    nBegin = document.cookie.indexOf(sCookie);

    if (nBegin > -1) {
        nBegin = nBegin + sCookie.length + 1;
        nEnd = nBegin;

        //while we haven't hit ";" and it's not end of cookie
        while (document.cookie.charAt(nEnd) != ";" && nEnd <= document.cookie.length) {
            nEnd++;
        }
        sResult = document.cookie.substring(nBegin, nEnd);
        return sResult;
    }
    return null;
}

function EraseCookie(sName) {
    CreateCookie(sName, '', -1);
}

function $w() {
    var aElements = new Array();
    var nLength = arguments.length;
    for (var i = 0; i < nLength; i++) {
        var oElement = arguments[i];
        if (typeof oElement == 'string') {
            oElement = document.getElementById(oElement);
        }
        if (nLength == 1) {
            return oElement;
        }
        aElements.push(oElement);
    }
    return aElements;
}

function getElementsByAttribute(sTagName, sAttribute, sAttributeValue) {
    var aElements = (document.all) ? document.all : document.getElementsByTagName(sTagName);
    var nLength = aElements.length;
    var aReturn = new Array();
    for (var i = 0; i < nLength; i++) {
        var oElement = aElements[i];
        var oAttribute = oElement.getAttribute(sAttribute);
        if (typeof oAttribute == 'string' && oAttribute.length > 0) {
            if (oAttribute == sAttributeValue) {
                aReturn.push(oElement);
            }
        }
    }
    return aReturn;
}

function UpdateMenuItem(oItems, sValue) {
    // Update Menu Items...
    // Return the selected piece of text
    var sReturn = '';
    var nItemCount = oItems.get_length()
    for (var nItem = 0; nItem < nItemCount; nItem++) {
        oItem = oItems.getItem(nItem);
        if (oItem.get_value() == sValue) {
            oItem.set_lookId('BoldItemLook');
            sReturn = oItem.get_text();
        }
        else {
            oItem.set_lookId('DefaultItemLook');
        }
    }
    return sReturn;
}

function SelectAllGrid(oGridTable, bChecked) {

    var oColumns = oGridTable.get_columns();   //columns
    var nRows = oGridTable.getRowCount(); //rows
    var iColumnIdx; //column index to update in the grid.

    for (nCol = 0; nCol < oColumns.length; nCol++) {
        if (oColumns[nCol].get_dataField() == 'ItemChecked') {
            iColumnIdx = nCol;
        }
    }

    for (nRow = 0; nRow < nRows; nRow++) {
        var oItem = oGridTable.getRow(nRow);
        oItem.setValue(iColumnIdx, bChecked);
    }
}

function trim(str) {
    if (!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}

function UpdateMenuNoToggle(oItems, sMenuId) {

    if (oItems != null) {
        var nItems = oItems.get_length();
        var oItem;
        for (nItem = 0; nItem < nItems; nItem++) {
            oItem = oItems.getItem(nItem);
            if (oItem != null) {
                if (oItem.get_id() == sMenuId) {
                    oItem.set_lookId('BoldItemLook');
                }
                else {
                    oItem.set_lookId('DefaultItemLook');
                }
            }
        }
    }
    if (sMenuId == '__choose__') {
        sMenuId = '';
    }

    return sMenuId;
}

function UpdateMenuToggle(oItems, sMenuId) {
    var sItems = '';

    if (oItems != null) {
        var nItems = oItems.get_length();
        var oItem;

        for (nItem = 0; nItem < nItems; nItem++) {
            oItem = oItems.getItem(nItem);

            if (oItem != null) {
                var sId = oItem.get_id();

                if (sMenuId == '__choose__') {
                    if (sId == sMenuId) {
                        oItem.set_lookId('BoldItemLook');
                    }
                    else {
                        oItem.set_lookId('DefaultItemLook');
                    }
                }
                else if (sId == '__choose__') {
                    oItem.set_lookId('DefaultItemLook');
                }
                else if (sId == sMenuId) {
                    if (oItem.get_lookId() == 'BoldItemLook') {
                        oItem.set_lookId('DefaultItemLook');
                    }
                    else {
                        if (sItems != '')
                            sItems = sItems + ',';

                        sItems = sItems + sId;
                        oItem.set_lookId('BoldItemLook');
                    }
                }
                else if (oItem.get_lookId() == 'BoldItemLook') {
                    if (sItems != '')
                        sItems = sItems + ',';

                    sItems = sItems + sId;
                }
            }
        }
    }

    if (sMenuId == '__choose__') {
        sItems = '';
    }

    return sItems;
}

function RefreshMenu(oItems, sSelected) {
    if (oItems != null) {
        sSelected = sSelected.trim();

        // remove any , at end of string
        while (sSelected.substring(sSelected.length - 1, sSelected.length) == ',')
            sSelected = sSelected.substring(0, sSelected.length - 1);

        var SelectedArray = sSelected.split(',');
        var nItems = oItems.get_length();
        var sId;

        // Loop on items
        for (nItem = 0; nItem < nItems; nItem++) {
            oItem = oItems.getItem(nItem);
            sId = oItem.get_id();

            // Loop on selected values
            if (SelectedArray.length == 1 && SelectedArray[0] == '') {
                if (sId == '__choose__')
                    oItem.set_lookId('BoldItemLook');
            }
            else {
                if (sId == '__choose__')
                    oItem.set_lookId('DefaultItemLook');

                for (var i = 0; i < SelectedArray.length; i++) {
                    if (sId == SelectedArray[i])
                        oItem.set_lookId('BoldItemLook');
                }
            }
        }
    }
}

// Javascript for Rating Control

function onStarClick(hiddenval, iStarCount) {
    $w(hiddenval).value = iStarCount;
    onStarOut(hiddenval);
}

function onStarHover(hiddenval, iStarCount) {
    for (var i1 = 1; i1 <= 5; i1++) {
        replaceImage(hiddenval + '_imgStar' + '' + i1 + '', '/images/xEmptyStar16.gif');
    }
    for (var i2 = 1; i2 <= 5; i2++) {
        if (i2 <= iStarCount) {
            replaceImage(hiddenval + '_imgStar' + '' + i2 + '', '/images/xFullStar16.gif');
        }
        else {
            replaceImage(hiddenval + '_imgStar' + '' + i2 + '', '/images/xEmptyStar16.gif');
        }
    }
}

function onStarOut(hiddenval) {
    var iStarCount = $w(hiddenval).value;
    for (var i1 = 1; i1 <= 5; i1++) {
        replaceImage(hiddenval + '_imgStar' + '' + i1 + '', '/images/xEmptyStar16.gif');
    }
    for (var i2 = 1; i2 <= 5; i2++) {
        if (i2 <= iStarCount) {
            replaceImage(hiddenval + '_imgStar' + '' + i2 + '', '/images/xFullStar16.gif');
        }
        else {
            replaceImage(hiddenval + '_imgStar' + '' + i2 + '', '/images/xEmptyStar16.gif');
        }
    }
}

function replaceImage(sOldImageId, sNewImageUrl) {
    var oImage = document.getElementById(sOldImageId);
    oImage.src = sNewImageUrl;
}

function isNumeric(value) {
    return typeof value != "boolean" && value != null && !isNaN(+value);
}

function isValidSignedPositiveInteger(value) {
    //remove all leading and trailing spaces
    value = value.replace(/^\s+|\s+$/g, '');

    //create Regular Expression object
    var regEx = /^[+]?[0-9]+$/;

    //validate the data
    return regEx.test(value);
}

function isValidSignedInteger(value) {
    //remove all leading and trailing spaces
    value = value.replace(/^\s+|\s+$/g, '');

    //create Regular Expression object
    var regEx = /^[-+]?[0-9]+$/;

    //validate the data
    return regEx.test(value);
}

function stringFormat(str) {
    for (i = 1; i < arguments.length; i++)
        str = str.replace('{' + (i - 1) + '}', arguments[i]);

    return str;
}

String.prototype.format = function() {
    var txt = this, i = arguments.length;

    while (i--) {
        txt = txt.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }

    return txt;
}


/* Ranked Question scripts */
function ChoiceChangeCss(oControl, oSelectedId, nStyle) {
    var oObject = $w(oControl + '_table');
    //var oRadio = $w(oControl);
    var sClassName = oObject.className;
    //var bChecked = oRadio.checked;
    var bChecked = $w(oSelectedId).value == oControl;
    var bSelected = sClassName.search('-sel') > -1;
    var bEnabled = sClassName.search('-enabled') > -1;
    var bDisabled = sClassName.search('-disabled') > -1;
    var bHover = sClassName.search('-hover') > -1;
    var bNormal = sClassName.search('-normal') > -1;
    var bError = sClassName.search('-error') > -1;
    sClassName = sClassName.replace('-hover', '');
    sClassName = sClassName.replace('-normal', '');
    sClassName = sClassName.replace('-enabled', '');
    sClassName = sClassName.replace('-disabled', '');
    sClassName = sClassName.replace('-error', '');
    sClassName = sClassName.replace('-sel', '');

    if (nStyle == 2) {
        // Focus
        //if (bSelected || bChecked) {
        sClassName = sClassName + '-enabled-normal-sel';
        //}
        //else {
        //    sClassName = sClassName + '-enabled-normal';
        //}
    }
    else if (nStyle == 3) {
        // Over
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover-error';
            }

        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-hover-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-hover';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-hover';
            }
            else {
                sClassName = sClassName + '-enabled-hover';
            }
        }
    }
    else if (nStyle == 4) {
        // Out
        if (bError) {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-sel-error';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal-error';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal-error';
            }
        }
        else {
            if (bSelected || bChecked) {
                sClassName = sClassName + '-enabled-normal-sel';
            }
            else if (bEnabled) {
                sClassName = sClassName + '-enabled-normal';
            }
            else if (bDisabled) {
                sClassName = sClassName + '-disabled-normal';
            }
            else {
                sClassName = sClassName + '-enabled-normal';
            }
        }
    }
    else if (nStyle == 7) {
        // Unclick
        if (bHover) {
            sClassName = sClassName + '-enabled-hover';
        }
        else {
            sClassName = sClassName + '-enabled-normal';
        }
    }
    oObject.className = sClassName;
}

function UnRankedClick(oQuestionId, oChoiceId, oRankedListId, oSelectedId, oChoiceListId) {
    // add to bottom of ranked list
    var sRankedList = $w(oRankedListId).value;

    if (sRankedList.length > 0) {
        sRankedList += ',';
    }
    sRankedList += oChoiceId;

    $w(oRankedListId).value = sRankedList;

    RenderList(oQuestionId, oRankedListId, oSelectedId, oChoiceListId);
}

function RankPop2(oQuestionId, oChoiceId, oRankedListId, oSelectedId, oChoiceListId) {
    RankedClick(oQuestionId, oChoiceId, oSelectedId);
    RankPop(oQuestionId, oRankedListId, oSelectedId, oChoiceListId);
}

function RankedClick(oQuestionId, oChoiceId, oSelectedId) {
    //var oObject = ((typeof oControl).toLowerCase() == 'object') ? oControl : $w(oControl);

    var oPreviousSelected = $w(oSelectedId).value;
    if (oPreviousSelected != null && oPreviousSelected.length > 0) {
        ChoiceChangeCss(oQuestionId + '_rc_' + oPreviousSelected, oSelectedId, 7);
    }
    $w(oSelectedId).value = oChoiceId;
    ChoiceChangeCss(oQuestionId + '_rc_' + oChoiceId, oSelectedId, 2);
}

function RenderList(oQuestionId, oRankedListId, oSelectedId, oChoiceListId) {
    var sChoiceList = $w(oChoiceListId).value;
    var aUnrankedElements = sChoiceList.split(',');
    var sRankedList = $w(oRankedListId).value;
    var aElements = sRankedList.split(',');
    var oRankedChoiceContainer = $w(oQuestionId + '_rankedcontainer');
    var oChoiceContainer;

    // clear ranked list
    oRankedChoiceContainer.innerHTML = '';

    // show all divs in unranked list (reset unranked list)
    for (nItem = 0; nItem < aUnrankedElements.length; nItem++) {
        oChoiceContainer = $w(oQuestionId + '_uc_' + aUnrankedElements[nItem] + '_container');
        ShowDiv(oChoiceContainer);
    }

    oChoiceContainer = $w(oQuestionId + '_unrank');
    HideDiv(oChoiceContainer);

    var sWidth = $w(oQuestionId + '_rcwidth').value;
    var sRender = $w(oQuestionId + '_render').value;

    // show ranked divs, hide in unranked list
    for (nItem = 0; nItem < aElements.length; nItem++) {
        oChoiceContainer = $w(oQuestionId + '_uc_' + aElements[nItem] + '_container');
        var oChoice = $w(oQuestionId + '_uc_' + aElements[nItem]); // unranked choice
        var oRankedId = oQuestionId + '_rc_' + aElements[nItem]; // ranked choice ID
        var oChoiceId = aElements[nItem]; // unranked choice ID

        if (oChoiceContainer != null && oChoice != null) {
            // copy contents of unranked choice
            var sChoice = oChoice.innerHTML;

            if (sRender == 'mobile') {
                sWidth = '';
            }
            else {
                sWidth = 'width:' + sWidth + ';';
            }
            var sHeight = $w(oQuestionId + '_rcheight').value;

            if (sRender == 'mobile') {
                oRankedChoiceContainer.innerHTML += '<div class="wsc_rankedchoice"><div id="' + oRankedId + '_table" class="wsc_rankedchoice-enabled-normal" style="' + sWidth + 'min-height:' + sHeight + ';padding:3px 2px 3px 2px;"><table id="' + oRankedId + '" width="100%" height="30px" onmouseover="ChoiceChangeCss(\'' + oRankedId + '\', \'' + oSelectedId + '\', 3);" onmouseout="ChoiceChangeCss(\'' + oRankedId + '\', \'' + oSelectedId + '\', 4);" onclick="RankPop2(\'' + oQuestionId + '\', \'' + oChoiceId + '\', \'' + oRankedListId + '\', \'' + oSelectedId + '\', \'' + oChoiceListId + '\');">' + sChoice + '</table></div></div>';
            }
            else {
                oRankedChoiceContainer.innerHTML += '<div class="wsc_rankedchoice"><div id="' + oRankedId + '_table" class="wsc_rankedchoice-enabled-normal" style="' + sWidth + 'min-height:' + sHeight + ';padding:3px 2px 3px 2px;"><table id="' + oRankedId + '" width="100%" height="30px" onmouseover="ChoiceChangeCss(\'' + oRankedId + '\', \'' + oSelectedId + '\', 3);" onmouseout="ChoiceChangeCss(\'' + oRankedId + '\', \'' + oSelectedId + '\', 4);" onclick="RankedClick(\'' + oQuestionId + '\', \'' + oChoiceId + '\', \'' + oSelectedId + '\');">' + sChoice + '</table></div></div>';
            }

            // Hide Div in unranked list
            HideDiv(oChoiceContainer);
        }
    }

    oChoiceContainer = $w(oQuestionId + '_unrank');
    if (sChoiceList.length == sRankedList.length) {
        ShowDiv(oChoiceContainer);
    }
    else {
        HideDiv(oChoiceContainer);
    }

    if (sRankedList.length == 0) {
        oRankedChoiceContainer.innerHTML += '<div class="wsc_ranked_norank">' + sResourceSurveyQuestionRankingNoneRanked + '</div>';
    }

    // redraw any selected item
    var oSelected = $w(oSelectedId).value;
    if (oSelected != null && oSelected.length > 0) {
        ChoiceChangeCss(oQuestionId + '_rc_' + oSelected, oSelectedId, 2);
    }
}

function RankPop(oQuestionId, oRankedListId, oSelectedId, oChoiceListId) {
    var oSelected = $w(oSelectedId).value;
    if (oSelected != null && oSelected.length > 0) {
        var sRankedList = $w(oRankedListId).value;
        var aElements = sRankedList.split(',');
        var sRedoList = '';

        for (nItem = 0; nItem < aElements.length; nItem++) {
            if (oSelected != aElements[nItem]) {
                // rebuild list without the selected one...
                if (sRedoList.length > 0) {
                    sRedoList += ',';
                }
                sRedoList += aElements[nItem];
            }
        }

        // set hidden list
        $w(oRankedListId).value = sRedoList;
        // 	reset hidden selected	
        $w(oSelectedId).value = '';

        RenderList(oQuestionId, oRankedListId, oSelectedId, oChoiceListId);
    }
    else {
        //WArn!
        //alert(sResourceSurveyQuestionRankingRemoveError);//'Please select an item to remove'
    }
}

function RankUp(oQuestionId, oRankedListId, oSelectedId, oChoiceListId) {
    var oSelected = $w(oSelectedId).value;
    if (oSelected != null && oSelected.length > 0) {
        var sRankedList = $w(oRankedListId).value;
        var aElements = sRankedList.split(',');
        var sRedoList = '';
        var nPreviousIndex = 0;

        if (sRankedList.length > 0 && aElements.length > 1) {
            for (nItem = 0; nItem < aElements.length; nItem++) {
                if (aElements[nItem] == oSelected) {
                    // do the switch
                    aElements[nItem] = aElements[nPreviousIndex];
                    aElements[nPreviousIndex] = oSelected;
                }
                else {
                    nPreviousIndex = nItem;
                }
            }

            // set the new list
            $w(oRankedListId).value = ArrayToString(aElements);
            // render list
            RenderList(oQuestionId, oRankedListId, oSelectedId, oChoiceListId);
        }
    }
    else {
        // warn
        //alert(sResourceSurveyQuestionRankingUpError);//'Please select an item to move up'
    }
}

function RankDown(oQuestionId, oRankedListId, oSelectedId, oChoiceListId) {
    var oSelected = $w(oSelectedId).value;
    if (oSelected != null && oSelected.length > 0) {
        var sRankedList = $w(oRankedListId).value;
        var aElements = sRankedList.split(',');
        var sRedoList = '';
        var nPreviousIndex = 0;

        if (sRankedList.length > 0 && aElements.length > 1) {
            for (nItem = 0; nItem < aElements.length; nItem++) {
                if (aElements[nItem] == oSelected) {
                    if (nItem < (aElements.length - 1)) {
                        // do the switch
                        aElements[nItem] = aElements[nItem + 1];
                        aElements[nItem + 1] = oSelected;
                        break;
                    }
                }
            }

            // set the new list
            $w(oRankedListId).value = ArrayToString(aElements);

            // render list
            RenderList(oQuestionId, oRankedListId, oSelectedId, oChoiceListId);
        }
    }
    else {
        // warn
        //alert(sResourceSurveyQuestionRankingDownError);//'Please select an item to move down'
    }
}

function ArrayToString(aElements) {
    var sElements = '';
    for (nItem = 0; nItem < aElements.length; nItem++) {
        if (sElements.length > 0) {
            sElements += ',';
        }
        sElements += aElements[nItem];
    }
    return sElements;
}

function RankClear(oQuestionId, oRankedListId, oSelectedId, oChoiceListId) {
    $w(oRankedListId).value = ''; // empty list
    $w(oSelectedId).value = ''; // empty selected
    RenderList(oQuestionId, oRankedListId, oSelectedId, oChoiceListId);
}

function ClearTextBox(textbox, text) {
    var oTextBox = $w(textbox);
    if (oTextBox.value == text) {
        oTextBox.value = '';
    }
}

function ResetTextBox(textbox, text) {
    var oTextBox = $w(textbox);
    if (oTextBox.value == '') {
        oTextBox.value = text;
    }
}

function TrackRedirect(sUrl, sPage) {
    try {
        var pageTracker = _gat._getTracker("UA-10422860-1");
        pageTracker._trackPageview(sPage);
    }
    catch (err)
    { }
    window.open(sUrl);
}

// Help Dialog
//#region
function HelpCallback_onCallbackError(sender, eventArgs) {
    var sStatus = eventArgs.get_message();
    if (sStatus != null && sStatus == 'loggedout') {
        ShowAlertDialog(sResourceMessageErrorTimeOut);
        document.forms[0].submit();
    }
    else {
        ShowAlertDialog(sResourceMessageErrorGeneralCallback);
    }
}

function HelpCallback_onCallbackComplete(sender, eventArgs) {
    HelpDialog.Show();
}

function Dialog_OnShowHelp() {
    // i dont do anyfink
}

function HelpDialog_Show(sCategory, sTag, sPostId) {
    try {
        var bOK = CheckSessionState() == 'true';
        if (!bOK) {
            ShowAlertDialog(sResourceMessageErrorTimeOut);
            document.forms[0].submit();
        }
        else {
            HelpCallback.callback(sCategory, sTag, sPostId);
        }
    }
    catch (e) {
        ShowAlertDialog(sResourceMessageErrorGeneralCallback);
    }
}

function HelpDialog_Hide() {
    if (HelpDialog.get_isShowing()) {
        var oForm = document.forms[0];
        var nElements = oForm.elements.length;
        for (nLoop = 0; nLoop < nElements; nLoop++) {
            var oElement = oForm.elements[nLoop];
            oElement.disabled = false;
        }
        HelpDialog.Close();
    }
}
//#endregion

function getFlashVersion()
{
	// test for IE
	try
	{
		try
		{
			// avoid fp6 minor version lookup issues
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try
			{
				axo.AllowScriptAccess = 'always';
			}
			catch (e)
			{
				return '6,0,0';
			}
		}
		catch (e) { }
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	}
	catch (e)
	{
		// other browsers
		try
		{
			if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
			{
				return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
			}
		}
		catch (e) { }
	}
	return '0,0,0';
}

