﻿// JScript File
    
    var _messageWait = 'Please Wait.....';
    var _saveButton = null;
    var _callTimeout = null;
    var _windowWait = null;
    var _parentAbsolute = null;
    var _btnPrintId  = null;    
    
    function showWindowWait()
    {
        if (_callTimeout != null)
        {
            if (_parentAbsolute == null)
            {
                _parentAbsolute = document.getElementById('shadowContent');
                if (_parentAbsolute == null)
                    _parentAbsolute = document.body;
            }
            document.getElementById('tdMessage').innerText = _messageWait;
            _windowWait.style.top = _parentAbsolute.scrollTop + Math.round((_parentAbsolute.scrollHeight - _windowWait.style.pixelHeight) / 2);
            _windowWait.style.left = _parentAbsolute.scrollLeft + Math.round((_parentAbsolute.scrollWidth - _windowWait.style.pixelWidth) / 2);
            _windowWait.style.display = '';
            _callTimeout = null;
        }
    }
    
    function initWindowWait()
    {
        _windowWait = document.getElementById('divWindowWait');
        if (_windowWait != null)
        {
            _saveButton = event.srcElement;
            _callTimeout = setTimeout("showWindowWait()", 2000);
            _saveButton.disabled = true;
            DisabledStyle(_saveButton, true);
        }
    }

    function stopWindowWait()
    {
        if (_windowWait != null)
        {
            _windowWait.style.display = 'none';
            if (_callTimeout != null)
                clearTimeout(_callTimeout);
            
            if (_saveButton != null)
            {
                _saveButton.disabled = false;
                DisabledStyle(_saveButton, false);
            }
        }
    }
    
    function CallServerRules(datacolumnkey, answervalue, responseguid, controlClientId, answervaluetext, execRulesOnDisabledControlDuringOnBlurEvent, bClosePopupWindow) {
    
        CallServerRules(datacolumnkey, answervalue, responseguid, controlClientId, answervaluetext, execRulesOnDisabledControlDuringOnBlurEvent, bClosePopupWindow, null)
    }
    
    function CallServerRules(datacolumnkey, answervalue, responseguid, controlClientId, answervaluetext, execRulesOnDisabledControlDuringOnBlurEvent, bClosePopupWindow, bChangeText)
    {
        if (!execRulesOnDisabledControlDuringOnBlurEvent)
        {
            if (document.getElementById(controlClientId).type == 'textarea' || 
            document.getElementById(controlClientId).type == 'text')
            {
                if (document.getElementById(controlClientId).disabled == true || 
                    document.getElementById(controlClientId).readOnly == true)
                    {
                        return false;                               
                    }
                    
            }
            else
            {
                if (document.getElementById(controlClientId).disabled == true)
                {
                     return false;                               
                }
            }
        }
        
        var today = new Date();
        var postData = new Array();
         
        postData.push('action=1');
        postData.push('datacolumnkey=' + datacolumnkey);
        postData.push('answervalue=' + answervalue);
        
        if (bChangeText!= null) {
            postData.push('changeText=1');
            postData.push('answervaluetext=' + answervaluetext);
        }
        
        postData.push('responseguid=' + responseguid);
        postData.push('controlClientId=' + controlClientId);
        postData.push('date=' + today);

        if (bClosePopupWindow == null)
            bClosePopupWindow = false;

        CallServerWebMethod('FormViewWebMethods.aspx', true, bClosePopupWindow ? onWebRequestCompleteAndClosePopup : onWebRequestComplete, postData);
        return false;
    }
    
    function OnClearPage(responseGuid)
    {
        var today = new Date();
        var postData = new Array();
        
        postData.push('action=6');
        postData.push('responseGuid=' + responseGuid);
        postData.push('formViewKey=' + formViewKey);
        postData.push('date=' + today);

        CallServerWebMethod('FormViewWebMethods.aspx', true, resetPage, postData);
        initWindowWait();
        return false;
    }
    
    function resetPage(result)
    {
        stopWindowWait();

        var answerResultArray = result.split("<-->");
        var strGridChildQuestions = "";
        for (var Ind=0; Ind < answerResultArray.length; Ind++)
        {
            var answer = answerResultArray[Ind].split("<&&>");
            if ((answer[1] != '') && (answer[1] != null))
            {
                if ((answer[4] != '') && (answer[4] != null))
                {
                    if (document.getElementById(answer[4]).type == 'select-one')
                    {
                        PopulateChildrenSelectControl(answer[4], answer[5]);
                    }
                }
                
                //To Obtain ghe the control ids of Child Question Grids
                if (answer[1].indexOf("GridChildQuestion1") > -1)
                {
                  if (strGridChildQuestions.indexOf(answer[1]) < 0)
                  {
                    if (strGridChildQuestions != "") strGridChildQuestions += ","
                    strGridChildQuestions += answer[1];
                  }
                }
                
                SetControlEnabled(answer[1], answer[3], answer[6], answer[2]);
                SetControlValue(answer[1], answer[2], answer[7]);
            }
        }                    
        if (typeof(arrayGridChildOnClick) != 'undefined')
        {
            if (strGridChildQuestions != "")
            {
                var arrGridChildQuestions = strGridChildQuestions.split(",");
                for (var p = 0; p < arrGridChildQuestions.length; p++)
                {
                    eval("DelRows" + arrGridChildQuestions[p] + "()");
                }
                //DelRows();
            }
        }
        clearErrors();
    }

    function OnNextPage(responseGuid)
    {
        var today = new Date();
        var postData = new Array();
        
        resetInputTextHiddenValues();
        postData.push('action=2');
        postData.push('responseGuid=' + responseGuid);
        postData.push('formViewKey=' + formViewKey);

        CallServerWebMethod('FormViewWebMethods.aspx', true, nextPageNumber, postData);
        initWindowWait();
        return false;   
    }
    
    function nextPageNumber(result)
    {
        if (result != 'True')
        {
            stopWindowWait();
            ShowError(result);
        }
        else
        {
            var today = new Date();
            var postData = new Array();
            
            postData.push('action=3');
            postData.push('nextpage=NextPage');

            CallServerWebMethod('FormViewWebMethods.aspx', true, submitPage, postData);
        }
    }
    
    function OnBackPage(controlClientId)
    {
        var today = new Date();
        var postData = new Array();
        
        resetInputTextHiddenValues();
        
        postData.push('action=7');
        postData.push('controlClientId=' + controlClientId);
        postData.push('date=' + today);

        CallServerWebMethod('FormViewWebMethods.aspx', true, submitPage, postData);
        initWindowWait();
    }

    function submitPage(result)
    {
        resetInputTextValues();
        document.getElementById("aspnetForm").submit();
    }
            
    function saveSubmitPage(result) 
    {
        if (result == "" )
        {
            resetInputTextHiddenValues();
            resetInputTextValues();
            stopWindowWait();            
            document.getElementById("aspnetForm").submit();    
        }
        else
        {
            stopWindowWait();
            ShowError(result);
        }
    }

    function Save(responseGuid)
    {
        if (existPasswordControl)
            ConfirmPassword(responseGuid);
        else
        {
            var today = new Date();
            var postData = new Array();

            postData.push('action=4');
            postData.push('responseGuid=' + responseGuid);
            postData.push('formViewKey=' + formViewKey);
            postData.push('date=' + today);

            CallServerWebMethod('FormViewWebMethods.aspx', true, saveTemplate, postData);
            initWindowWait();
        }
        return false;
    }

    function saveTemplate(result) 
    {
        var input_box=confirm(saveMessage);
        if (input_box==true)
        {
            var today = new Date();
            var postData = new Array();

            postData.push('action=5');
            postData.push('formViewKey=' + formViewKey);
            postData.push('date=' + today);

            CallServerWebMethod('FormViewWebMethods.aspx', true, saveSubmitPage, postData);
        }
        else
            stopWindowWait();
    }
    
    function saveChildVR(result) 
    {
        if (result != 'True')
        {
            ShowError(result);
            stopWindowWait();
        }
        else
        {
            var input_box=confirm(saveMessage);
            if (input_box==true)
            {
                var today = new Date();
                var postData = new Array();

                postData.push('action=8');
                postData.push('formViewKey=' + formViewKey);
                postData.push('date=' + today);

                CallServerWebMethod('FormViewWebMethods.aspx', true, saveSubmitPage, postData);
            }
            else
                stopWindowWait();
        }
    }
    
    function SetReportsNumber(controlId) 
    {
        var today = new Date();
        var postData = new Array();

        postData.push('action=12');
        postData.push('controlId=' + controlId);

        CallServerWebMethod('FormViewWebMethods.aspx', true, SetReportsNumberResult, postData);
        return false;
    }
    
    function SetReportsNumberResult(result)
    {
        document.getElementById("aspnetForm").submit();
    }

    function onWebRequestComplete(result) 
    {        
        var answerResultArray = result.split("<-->");
        for (var Ind=0; Ind < answerResultArray.length; Ind++)
        {
            var answer = answerResultArray[Ind].split("<&&>");
            if ((answer[1] != '') && (answer[1] != null))
            {
                SetControlEnabled(answer[1], answer[3], answer[6], answer[2]);
                SetControlValue(answer[1], answer[2], answer[7]);
                                                                                        
                if ((answer[4] != '') && (answer[4] != null))
                    if (document.getElementById(answer[4]).type == 'select-one')
                    {
                        PopulateChildrenSelectControl(answer[4], answer[5]);
                    }
            }
        }
    }

    function onWebRequestCompleteAndClosePopup(result) 
    {
        popupWindow_closeWindow('popupWnd');
        onWebRequestComplete(result);
    }

    function PopulateChildrenSelectControl(SelectControlId, ControlElements)
    {
        var ItemArray = new Array();
        
        if (ControlElements != "" && ControlElements != null)
        {    
            ItemArray = ControlElements.split("<VRS>");
            
            for (var Ind=0; Ind < ItemArray.length; Ind++){
                var ItemElementArray = ItemArray[Ind].split("<VRE>");
                ItemArray[Ind] = new Array(2);
                ItemArray[Ind][0] = ItemElementArray[1];
                ItemArray[Ind][1] = ItemElementArray[0];
            }
        }
        
        var controlToPopulate = document.getElementById(SelectControlId);
        
//        for (var q=controlToPopulate.options.length;q>=0;q--)
//        {
            //controlToPopulate.options[q]=null;
//        }
        //This is for unresize options in IE6
        controlToPopulate.innerHTML = "";
        
        myEle = document.createElement("option") ;
        myEle.setAttribute('value', 'null');
        
        var txt = document.createTextNode(strAddSelect);
        myEle.appendChild(txt);
        myEle.selected = true;
        controlToPopulate.appendChild(myEle);
        
        // Now loop through the array of individual items
        // Any containing the same child id are added to
        // the second dropdown box
        for ( x = 0 ; x < ItemArray.length  ; x++ ) 
        {
            myEle = document.createElement("option") ;
            myEle.setAttribute('value',ItemArray[x][0]);
            var txt = document.createTextNode(ItemArray[x][1]);
            myEle.appendChild(txt);
            controlToPopulate.appendChild(myEle);
        }
    }

    function SetControlValue(ControlId, ControlValue, ControlValueText)
    {
        var ctrlElement = document.getElementById(ControlId);
        if (ctrlElement == null)
            return;

        var controlNumber = 0;
        if (ctrlElement.type == 'checkbox') {controlNumber = 1}
        if (ControlId.indexOf("RadioButtonBooleanList1") != -1) {controlNumber = 2}
        if (ControlId.indexOf("RadioButtonList1") != -1) {controlNumber = 3}
        if (ControlId.indexOf("PasswordText1") != -1) {controlNumber = 4}
        if (ControlId.indexOf("chkMultiSelection") != -1) {controlNumber = 5}
        if (ControlId.indexOf("lstMslcUnSelectedElements") != -1) {controlNumber = 6}
        if (ControlId.indexOf("cmdGetFiles") != -1) {controlNumber = 7}

        switch (controlNumber) {
            case 1:
               SetControlValueCheckBox(ControlId, ControlValue);
               break
            case 2:
               SetControlValueRadioBoolean(ControlId, ControlValue);
               break
            case 3:
               SetControlValueRadioVR(ControlId, ControlValue);
               break
            case 5:
               SetControlValueMultiSelectionCheckBox(ControlId, ControlValue);
               break
            case 6:
               SetControlValueMultiSelectionList(ControlId, ControlValue);
               break
            case 7:
               SetControlMediaFile(ControlId, ControlValue);
               break
            default:
                var value = ControlValue;
                var bw = clientBrowser();
                
                if (controlNumber == 4)
                SetControlValuePassword(ControlId, ControlValue);
                
                var showAnswerText = '';
                if (bw == 0)
                {
                    showAnswerText = ctrlElement.showAnswerText;
                }
                else
                { 
                    if (ctrlElement.attributes['showAnswerText'] != null)
                    {
                        showAnswerText = ctrlElement.attributes['showAnswerText'].text;
                    }
                }
                
                if (showAnswerText == "true")
                {
                    value = ControlValueText;
                    var controlAnswerValueId;
                    if (bw == 0)
                    {
                        controlAnswerValueId = ctrlElement.controlAnswerValueID;
                        document.getElementById(controlAnswerValueId).defaultValue = ControlValue;
                    }
                    else 
                    {
                        controlAnswerValueId = ctrlElement.attributes['controlAnswerValueID'].text;
                        var attrDefaultValue = document.createAttribute('defaultValue');
                        attrDefaultValue.nodeValue = ControlValue;
                        document.getElementById(controlAnswerValueId).setAttributeNode(attrDefaultValue);
                    }
                }
                if (ctrlElement.type == 'select-one' && value == '')
                    value =  'null';
                if (ControlId.indexOf("txtUserPlaces") != -1)
                    value =  '';
                if (ctrlElement.type == 'select-one' && (ControlId.indexOf("lstUserPlaces") != -1 || ControlId.indexOf("lstUserRoles") != -1))
                {
                    value =  'null';
                    ctrlElement.options.length = 0;
                    
                    if (ControlId.indexOf("lstUserPlaces") != -1)
                    {
                        var defaultPlaceObj = document.getElementById(ControlId.replace('lstUserPlaces', 'lblDefaultPlaceText'));
                        defaultPlaceObj.innerHTML = '';
                    }                    
                }
                if (ControlId.indexOf('TranslationTextBox') != -1)
                {
                    if (ControlValueText.toLowerCase() == 'false')
                        value =  ctrlElement.value;
                    else
                    {
                        var numOfLang = parseInt(ctrlElement.attributes['NumberOfLanguages'].value);
                        var setValue = '';
                        for (i = 1; i < numOfLang; i++)
                        {
                            setValue += '||';
                        }

                        document.getElementById(ControlId.replace('TranslationTextBox', 'etcTranslationName_hidTranslations')).value = setValue;
                        document.getElementById(ControlId.replace('TranslationTextBox', 'etcTranslationName_hidAudioFiles')).value = setValue;

                        if (numOfLang == 1)
                        {
                            //We must empty the control element value before fire the check control onclick event
                            ctrlElement.value = '';
                            
                            var checkControl = document.getElementById(ctrlElement.attributes['id_CheckAllowHTML'].value);
                            
                            checkControl.checked = false;
                            
                            if (checkControl.parentElement.style.display == '')
                            {
                                checkControl.onclick();
                            }
                        }
                        
                        value =  '';
                    }
                }

                ctrlElement.value = unescape(value);
                
                if (ctrlElement.type == 'select-one')
                    if (ctrlElement.selectedIndex == -1)
                        ctrlElement.value = 'null';
                break;
        }
    }    
    
    function SetControlValuePassword(ControlId, ControlValue)
    {
        if (ControlValue == '')
        {
            var secondPasswordId = ControlId.substring(0, ControlId.length - 1) + '2';
            var oldPassword = ControlId.substring(0, ControlId.length - 1) + '0';
            document.getElementById(secondPasswordId).value = '';
            if (document.getElementById(oldPassword))
                document.getElementById(oldPassword).value = '';
        }
        
    }
    function SetControlValueCheckBox(ControlId, ControlValue)
    {
        if  (ControlValue.toLowerCase() == "false")
            document.getElementById(ControlId).checked = false;

        if  (ControlValue.toLowerCase() == "true")
                document.getElementById(ControlId).checked = true;
                
        if  (ControlValue.toLowerCase() == "")
            document.getElementById(ControlId).checked = false;    
    }
    
    function SetControlValueRadioBoolean(ControlId, ControlValue)
    {
        if  (ControlValue.toLowerCase() == "false")
            document.getElementById(ControlId + '_1').checked = true;

        if  (ControlValue.toLowerCase() == "true")
            document.getElementById(ControlId + '_0').checked = true;
            
        if  (ControlValue.toLowerCase() == "")
        {
            document.getElementById(ControlId + '_0').checked = false;
            document.getElementById(ControlId + '_1').checked = false;                    
        }    
    }

    function SetControlValueMultiSelectionList(ControlId, ControlValue)
    {
        var instanceId = ControlId.replace('lstMslcUnSelectedElements', 'mslc');

        if (ControlValue.toLowerCase() == '')
        {
            //Execute the property 'DelAll()' of the current instance
            eval(instanceId + '.SendDataToServer = false;' + instanceId + '.DelAll();' + instanceId + '.SendDataToServer = true;');
        }
    }
    
    function SetControlValueMultiSelectionCheckBox(ControlId, ControlValue)
    {
        var response = true;
        var checkBox = null;
        var itemCount = 0;

        if  (ControlValue.toLowerCase() == "")
        {
            while (response)
            {
                checkBox = document.getElementById(ControlId + '_' + itemCount);
                itemCount += 1;
                
                if (checkBox != null)
                {
                    checkBox.checked = false;
                }
                else
                {
                    response = false;
                }
            }                    
        }        
    }
    
    function SetControlValueRadioVR(ControlId, ControlValue)
    {
        var controlName = ControlId.replace(/_/g, '$');
        var radioButton = document.getElementsByName(controlName);
        if  (ControlValue.toLowerCase() == "")
        {
            for (i=0;i<radioButton.length;i++)        
            {
                document.getElementById(ControlId + '_' + i).checked = false;
            }
            return;
        }        
        var i;
        for (i=0;i<radioButton.length;i++){
            if (radioButton[i].value==ControlValue)
	        {
	          document.getElementById(ControlId + '_' + i).checked = 'checked';
              break;
	        }
	    }
    } 
    
    function SetControlMediaFile(ControlId, ControlValue)
    {
        var id_xmlFiles = ControlId.replace('cmdGetFiles', 'hidMediaFiles');
        var id_divFiles = ControlId.replace('cmdGetFiles', 'divMediaFiles');

        if (ControlValue == '')
        {
            document.getElementById(id_xmlFiles).value = '';
            document.getElementById(id_divFiles).innerHTML = '';
        }
    }
    
    function SetControlEnabled(ControlId, ControlIsEnabled, ControlIsReadOnly, ControlValue)
    {
        var ctrlElement = document.getElementById(ControlId);
        if (ctrlElement == null)
            return;

        var controlNumber = 0;
        if (ControlId.indexOf("GridChildQuestion") != -1)
        {
            controlNumber = 1;
        }
        if (ControlId.indexOf("RadioButtonBooleanList1") != -1 || ControlId.indexOf("RadioButtonList1") != -1)
        {
            controlNumber = 2;
        }        
        if (ControlId.indexOf("txtSelectSimpleArea") != -1)
        {
            controlNumber = 3;
        }
        if (ControlId.indexOf("txtSelectSimpleUser") != -1)
        {
            controlNumber = 4;
        }
        if (ControlId.indexOf("lstUserPlaces") != -1 || ControlId.indexOf("txtUserPlaces") != -1)
        {
            controlNumber = 5;
        }        
        if (ControlId.indexOf("validResponseChildTemplate") != -1)
        {
            controlNumber = 6;
        }
        if (ControlId.indexOf("chkMultiSelection") != -1)
        {
            controlNumber = 7;
        }
        if (ControlId.indexOf('lstMslcUnSelectedElements') != -1)
        {
            controlNumber = 8;
        }
        if (ControlId.indexOf('lstUserRoles') != -1)
        {
            controlNumber = 9;
        }
        if (ControlId.indexOf('cmdGetFiles') != -1)
        {
            controlNumber = 10;
        }
        if (ControlId.indexOf('HierarchyText') != -1)
        {
            controlNumber = 12;
        }
        
        switch (controlNumber)
        {
            case 1:
               SetControlTableEnabled(ControlId, ControlIsEnabled, ControlValue);
               break
            case 2:
                DisabledStyle(ctrlElement, !(ControlIsEnabled.toLowerCase() == 'true'));
                SetControlInputBooleanEnabled(ControlId, ControlIsEnabled, 'radio');
               break;
            case 3:
               SetControlSelectAreaEnabled(ControlId, ControlIsEnabled, ControlValue);
               break
            case 4:
               SetControlSelectUserEnabled(ControlId, ControlIsEnabled, ControlValue);
               break
            case 5:
               SetControlUserPlaceEnabled(ControlId, ControlIsEnabled, ControlValue);
               break
            case 6:
               SetControlValidResponseChildEnabled(ControlId, ControlIsEnabled, ControlValue);
               break               
            case 7:
                DisabledStyle(ctrlElement, !(ControlIsEnabled.toLowerCase() == 'true'));
                SetControlInputBooleanEnabled(ControlId, ControlIsEnabled, 'checkbox');
                break;
            case 8:
                DisabledStyle(ctrlElement, !(ControlIsEnabled.toLowerCase() == 'true'));
                SetControlMultiSelectionListEnabled(ControlId, ControlIsEnabled);
                break;
            case 9:
               SetControlUserRoleEnabled(ControlId, ControlIsEnabled, ControlValue);
               break
            case 10:
               SetControlInputBooleanEnabled(ControlId, ControlIsEnabled, 'button');
               break
            case 12:
               SetControlHierarchyEnabled(ControlId, ControlIsEnabled, ControlValue);
               break
            default:
                //Only for translation controls
                var numOfLang = 99;
                 
                if (ctrlElement.attributes['editTranslationButtonID'] != null)
                {
                    var editTranslationButtonClientId = ctrlElement.attributes['editTranslationButtonID'].value;                    
                    var isControlEnabled = (ControlIsEnabled.toLowerCase() == "true" ? true : false);
                    numOfLang = parseInt(ctrlElement.attributes['NumberOfLanguages'].value);
                    
                    if (numOfLang == 1)
                    {
                        if (isControlEnabled)
                        {
                            ctrlElement.onblur = function(){saveTranslation(ControlId)};
                        }
                        else
                        {
                            ctrlElement.onblur = null;
                        }
                    }
                    else
                    {
                        var ctrlButton = document.getElementById(editTranslationButtonClientId);
                        ctrlButton.disabled = isControlEnabled ? false : true;
                        DisabledStyle(ctrlButton, !isControlEnabled);
                    }
                    
                    var allowHtml = false;
                    
                    if (ctrlElement.attributes['allowHtml'] != null)
                    {
                        allowHtml = (ctrlElement.attributes['allowHtml'].value.toLowerCase() == "true" ? true : false);
                    }                    
                    
                    if (allowHtml && ctrlElement.attributes['id_CheckAllowHTML'] != null)
                    {
                        var chkId = ctrlElement.attributes['id_CheckAllowHTML'].value;
                        
                        document.getElementById(chkId).parentElement.disabled = (isControlEnabled ? '' : 'disabled');
                    }
                    
                    if (ctrlElement.attributes['id_LnkAudio'] != null)
                    {
                        var lnkId = ctrlElement.attributes['id_LnkAudio'].value;
                        
                        document.getElementById(lnkId).style.display = (isControlEnabled ? '' : 'none')
                    }
                    
                    if (ctrlElement.attributes['id_ImgAudio'] != null)
                    {
                        var imgId = ctrlElement.attributes['id_ImgAudio'].value;
                        var hidId = ctrlElement.attributes['id_AudioFiles'].value;
                        var isImageEnabled = (isControlEnabled && document.getElementById(hidId).value != '')
                        
                        document.getElementById(imgId).style.display = (isImageEnabled ? '' : 'none')                    
                    }
                }

                if (ControlIsEnabled.toLowerCase() == "true") 
                {   
                    
                    ctrlElement.disabled = false;
                    if (ctrlElement.type == 'textarea' || ctrlElement.type == 'text')
                        ctrlElement.readOnly = false;

                    DisabledStyle(ctrlElement, false);
                }
                if (ControlIsEnabled.toLowerCase() == "false" || (ControlId.indexOf("TranslationTextBox") != -1 && numOfLang > 1))
                {
                    if (ControlIsReadOnly.toLowerCase() == "true") 
                    {
                        if (ctrlElement.type == 'text') 
                        {
                            ctrlElement.disabled = true;
                            ctrlElement.readOnly = true;   
                            ctrlElement.disabled = false;
                        } 
                        else
                        {
                            ctrlElement.disabled = true;
                        }
                    }
                    else 
                    {                        
                        if (ControlValue == '')
                            ctrlElement.disabled = true;
                        else
                        {
                            if (ctrlElement.type == 'textarea' || ctrlElement.type == 'text')
                            {
                                ctrlElement.disabled = false;
                                ctrlElement.readOnly = true;                               
                            }
                            else
                            {
                                ctrlElement.disabled = true;
                            }
                        }
                    }
                    DisabledStyle(ctrlElement, true);
                }
                break;
        } 
    }
    
    function SetControlMultiSelectionListEnabled(ControlId, ControlIsEnabled)
    {
        var isEnabled = true;
        var instanceId = ControlId.replace('lstMslcUnSelectedElements', 'mslc');

        if (ControlIsEnabled.toLowerCase() != "true")
        {
            isEnabled = false;
        }
        
        //Execute the property 'Enable()' of the current instance
        eval(instanceId + '.Enable(' + isEnabled + ');');
    }
    
    function SetControlInputBooleanEnabled(ControlId, ControlIsEnabled, inputType)
    {
        function getObj(name)
        {
            /* DHTML Micro API
            * Source: http://www.quirksmode.org/js/dhtmloptions.html
            */
            if (document.getElementById) // test if browser supports document.getElementById
            {
                this.obj = document.getElementById(name);
                this.style = document.getElementById(name).style;
                this.parent = document.getElementById(name).parentNode;
            }
            else if (document.all) // test if browser supports document.all
            {
                this.obj = document.all[name];
                this.style = document.all[name].style;
                this.parent = document.all[name].parentElement;
            }
            else if (document.layers) // test if browser supports document.layers
            {
                this.obj = document.layers[name];
                this.style = document.layers[name].style;
                this.parent = document.layers[name].parentNode;
            }
        }

        var input = document.getElementsByTagName('INPUT');

        for (var t = 0; t < input.length; t++) 
        {   
            if (input[t].type == inputType)
            {   
                if (input[t].id.indexOf(ControlId) != -1)
                {   
                    var oCheck = new getObj(input[t].id);

                    if (ControlIsEnabled.toLowerCase() == "true") 
                    {   
                        oCheck.obj.disabled = false;
                        oCheck.parent.disabled = false;
                    }
                    else
                    {
                        oCheck.obj.disabled = true;
                        oCheck.parent.disabled = true;
                    }                    
                }
            }
        }
    }
    
    function ShowChildFormView(childFormViewKey, responseGuid, action, dataColumnKey, title, clientTableId, parentColumnTag, themeName)
    {
        gridChildTableId = clientTableId;
        popupWindow_showWindow('popupWnd', 'ChildFormViewPopup.aspx?ChildFormViewKey=' + childFormViewKey + '&ResponseGuid=' +  responseGuid + '&Action=' + action + '&DataColumnKey=' + dataColumnKey + '&ParentColumnTag=' + parentColumnTag + '&clientTableId=' + clientTableId + '&ThemeName=' + themeName, 750, 400, title);
    }

    function ShowAudio(audioName)
    {
        popupWindow_showWindow('popupWnd', showAudioPage + '?audioName=' + audioName, 450, 213, '');
    }    
    
    function closepopupWindow()
    {
        popupWindow_closeWindow('popupWnd');
    }

    function clearErrors()
    {
        var errorSpan=document.getElementsByTagName('span');
        for(i=0;i<errorSpan.length;i++)
        {   
            if (errorSpan[i].id.indexOf('ErrorLabel') != -1 || errorSpan[i].id.indexOf('SubFormViewError') != -1)
            {
                errorSpan[i].innerHTML = '';
            }
        }
    }
    
    function ExecuteValidateRulesChildVR(responseGuid)
    {
        var today = new Date();
        var postData = new Array();

        postData.push('action=4');
        postData.push('responseGuid=' + responseGuid);
        postData.push('formViewKey=' + formViewKey);
        postData.push('date=' + today);

        CallServerWebMethod('FormViewWebMethods.aspx', true, saveChildVR, postData);
        initWindowWait();
        return false;   
    }

    function DisabledStyle(object, disabled)
    {
        var disabledStyleCss = 'background-color: threedlightshadow; border-right: inactivecaption 1px solid; border-top: inactivecaption 1px solid; border-left: inactivecaption 1px solid; border-bottom: inactivecaption 1px solid;';

        for(ii = 0; ii < document.styleSheets.length; ii++)
        {
            var rules = document.styleSheets[ii].cssRules? document.styleSheets[ii].cssRules : document.styleSheets[ii].rules;
            for(jj = 0; jj < rules.length; jj++)
            {
                if(rules[jj].selectorText == '.inputTextDisabled')
                {
                    disabledStyleCss = rules[jj].style.cssText;
                    break;
                }
            }
        }
        
        var styles = disabledStyleCss.toUpperCase().split(';');
        
        if (disabled)
        {
            if (styles.length > 0)
            {
                for (var i = 0; i < styles.length; i++)
                {
                    var values = styles[i].split(':');
                    
                    if (values.length == 2)
                    {
                        var s1 = Trim(values[0]);
                        var s2 = Trim(values[1]);
                        var stringToAppend = s1 + ': ' + s2;
                        
                        if (s1 != '' && s2 != '')
                        {
                            object.style.cssText = object.style.cssText + '; ' + stringToAppend;
                        }
                    }
                }
            }            
        }
        else
        {
            for (var i = 0; i < styles.length; i++)
            {
                var values = styles[i].split(':');

                if (values.length == 2)
                {
                    var stringToRemove = Trim(values[0]) + ': ' + Trim(values[1]);
                    
                    object.style.cssText = RemoveString(object.style.cssText.toUpperCase(), stringToRemove);
                }
            }            
        }
    }    
    
    function ShowPrintForm(url, FormViewKey, title)
    {
        popupWindow_showWindow('printWnd', url + '?viewKey=' + FormViewKey + "&viewType=1&menuKey=0" , 689, 273, title);
    }
    
    function closepopupPrintWindow()
    {
        popupWindow_closeWindow('printWnd');
    }
    

function setVRChildCtrlsEnabled(contentControlId, nameControlId, controlIsEnabled)
{
    var contentTable = document.getElementById(contentControlId);
    var prefix = contentControlId.replace('validResponseChildTemplate', '');
    var suffix = '_' + nameControlId;
    var pattern = '(' + prefix + ')[0-9A-Za-z_]+(' + suffix + ')';
    var matches = contentTable.innerHTML.match(new RegExp(pattern, 'g'));
    var lastValue = '';
    
    if (matches != null && matches.length != 0)
    {
        for (var count = 0; count < matches.length; count++)
        {
           if (lastValue != matches[count])
           {
                SetControlEnabled(matches[count], controlIsEnabled, 'true')
                lastValue = matches[count];
           }
        }            
    }
}

function SetControlValidResponseChildEnabled(ControlId, ControlIsEnabled, ControlValue)
{
    var ctrlIsEnabled = ControlIsEnabled.toLowerCase() == 'true' ? 'true' : 'false';
    
    setVRChildCtrlsEnabled(ControlId, 'Text1', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'RadioButtonBooleanList1', ctrlIsEnabled, false);    
    setVRChildCtrlsEnabled(ControlId, 'Checkbox1', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'DateTimeTextBox', ctrlIsEnabled, true);
    setVRChildCtrlsEnabled(ControlId, 'txtDecimal', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'cmdGetFiles', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'TranslationTextBox', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'Select1', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'txtSelectSimpleArea', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'txtSelectSimpleUser', ctrlIsEnabled, false);
    setVRChildCtrlsEnabled(ControlId, 'txtUserId', ctrlIsEnabled, false);
    
    document.getElementById(ControlId).focus();
}
    
function RunCallServerRules(datacolumnkey, answervalue, responseguid, controlClientId, answervaluetext, execRulesOnDisabledControlDuringOnBlurEvent)
{
    var _callServerRulesTimeout = setTimeout("CallServerRules('" + datacolumnkey + "'," + 
    answervalue + ",'" + responseguid + "','" + controlClientId + "','" + answervaluetext + "', true)", 500);
}

function resetInputTextHiddenValues()
{
    //This function is to avoid submit dangerous code like <xxx> 
    
    var inputTexts =document.getElementsByTagName('INPUT');
    for (var t = 0; t < inputTexts.length; t++) {
        if (inputTexts[t].type == 'hidden') 
            if (inputTexts[t].name.indexOf('hidTranslations') > 0) {
                inputTexts[t].value = '';
            }
    }

}
function resetInputTextValues()
{
    //This function is to avoid submit dangerous code like <xxx> 
    var inputTexts =document.getElementsByTagName('INPUT');
    for (var t = 0; t < inputTexts.length; t++) if (inputTexts[t].type == 'text') inputTexts[t].value = '';
    var inputTextArea =document.getElementsByTagName('textarea');
    for (var t = 0; t < inputTextArea.length; t++) inputTextArea[t].value = '';
}

function MultiSelListControl(source, target, dataColumnKey, responseGuid)
{
    var source = document.getElementById(source); 
    var target = document.getElementById(target);
    var isEnabled = true;
    
    this.SendDataToServer = true;
    
    this.Enable = function(enabled)
    {
        DisabledStyle(source, !enabled);
        DisabledStyle(target, !enabled);
        
        var oAction1 = document.getElementById(source.id.replace('lstMslcUnSelectedElements', 'btnDelAllMslcElements'))
        var oAction2 = document.getElementById(source.id.replace('lstMslcUnSelectedElements', 'btnDelMslcElements'))
        var oAction3 = document.getElementById(source.id.replace('lstMslcUnSelectedElements', 'btnAddMslcElements'))
        var oAction4 = document.getElementById(source.id.replace('lstMslcUnSelectedElements', 'btnAddAllMslcElements'))
        
        oAction1.disabled = !enabled;
        oAction2.disabled = !enabled;
        oAction3.disabled = !enabled;
        oAction4.disabled = !enabled;
        
        isEnabled = enabled;
    };
    
    this.DelAll = function()
    {
        if (isEnabled)
        {
            AddDelOptions(target, source, '0', this.SendDataToServer);
        }
    };
    
    this.AddAll = function()
    {
        if (isEnabled)
        {
            AddDelOptions(source, target, '1', this.SendDataToServer);
        }
    };

    this.Del = function()
    {
        if (isEnabled)
        {
            AddDelOption(target, source, '0', this.SendDataToServer);
        }
    };
    
    this.Add = function()
    {
        if (isEnabled)
        {
            AddDelOption(source, target, '1', this.SendDataToServer);
        }
    };
    
//    function AddDelOption(source, target, isSelected, sendDataToServer)
//    {
//        if (source.length > 0)
//        {
//            if (source.selectedIndex > -1)
//            {
//                var option = source.options[source.selectedIndex];
//                var token = '';

//                token += option.value + '|' + isSelected;

//                target.appendChild(option);
//                
//                if (sendDataToServer)
//                {
//                    CallServerRules(dataColumnKey, token, responseGuid, (isSelected ? source.id : target.id), '');
//                }
//            }
//        }            
//    }
    
    function AddDelOption(source, target, isSelected, sendDataToServer)
    {
        var len = source.length -1;
        var token = '';

        if (source.selectedIndex > -1)
        {
            var iOption= new Array(len);
            var iItems=0;
            for(i = len; i >= 0; i--)
            {
                if (source.item(i).selected)
                {
                    iOption[iItems] = source.options[i];
                    iItems++;
                    token += source.item(i).value + '|' + isSelected;
                    if (i > 0)
                    {
                        token += '||';
                    }
                }
            }
            for(i = iItems-1; i >= 0; i--)
            {
                target.appendChild(iOption[i]);
            }        

            if (token != '')
            {
                if (sendDataToServer)
                {
                    CallServerRules(dataColumnKey, token, responseGuid, (isSelected ? source.id : target.id), '');
                }
            }
        }
    }    
    
    function AddDelOptions(source, target, isSelected, sendDataToServer)
    {
        var len = source.length -1;
        var token = '';

        for(i = 0; i <= len; i++)
        {
            token += source.item(i).value + '|' + isSelected;
            
            if (i < len)
            {
                token += '||';
            }
        }
       
        if (token != '')
        {
            while(source.length > 0)
            {
                var option = source.options[0];
                target.appendChild(option);
            }

            if (sendDataToServer)
            {
                CallServerRules(dataColumnKey, token, responseGuid, (isSelected ? source.id : target.id), '');
            }
        }
    }
}   
