﻿/* 
* Copyright: Defaqto Ltd
* Website: www.defaqto.com
* Date: 25/11/2008
*/

function QuestionDropDown(containerId, controlId, endPointUrl, columnCount, displayConfigurationId, questionIndex, trackingProcessorId) {

    this.ContainerId = containerId;
    this.ControlId = controlId;
    this.EndPoint = document.location.protocol + '//' + endPointUrl + "/ajaxEndpoint/LoadAnswersByQuestionIdAsJson";
    this.QuestionEndPoint = document.location.protocol + '//' + endPointUrl + "/ajaxEndpoint/LoadQuestionByQuestionIdAsJson";
    this.ColumnCount = columnCount;
    this.DisplayConfigurationId = displayConfigurationId;
    this.QuestionIndex = questionIndex;
    this.Guid = document.getElementById(guid).value;
    this.TrackingProcessorId = trackingProcessorId;
    this.objControl = document.getElementById(this.ContainerId + "_" + this.ControlId);
    
    //Add trim method to string  object  
    String.prototype.trim = function() {
        return this.replace(/\,$/, ''); ;
    }

    QuestionDropDown.prototype.onchange = function(scope) {

        var selectedQuestionId = scope.objControl.options[scope.objControl.selectedIndex].value;
        var selectedQuestionDescription = scope.objControl.options[scope.objControl.selectedIndex].innerHTML;
        var url = scope.EndPoint;
        var xmlHttpObj = CreateXmlHttpRequestObject();
        var selectedCell = new Array(scope.ColumnCount);

        xmlHttpObj.open("POST", url, true);
        xmlHttpObj.onreadystatechange = function() {
            if (xmlHttpObj.readyState == READYSTATE_COMPLETE) {
                if (xmlHttpObj.status == HTTPSTATUS_OK) {
                    var result = eval('(' + xmlHttpObj.responseText + ')');
                    result = result.d;

                    if (result.length > 0) {
                        var resultInt = 0;

                        for (var i = 0; i < selectedCell.length; i++) {
                            if (selectedCell[i] == -1) {
                                continue;
                            }

                            if (i == 1) {
                                // call the MI tracking function.
                                window[scope.TrackingProcessorId.ProductDropDownChanged(scope.TrackingProcessorId)];
                            }
                            var cellDataId = scope.ContainerId + "_CellData" + scope.QuestionIndex + "-" + i;
                            var cellImageId = scope.ContainerId + "_CellImg" + scope.QuestionIndex + "-" + i;
                            var answerCellId = scope.ContainerId + "_AnswerCell" + scope.QuestionIndex;

                            var cellData = document.getElementById(cellDataId);
                            var cellImg = document.getElementById(cellImageId);
                            var answerCell = document.getElementById(answerCellId);

                            var ColumnProductDescription = "";
                            if (document.getElementById(scope.ContainerId + "_productTitle" + i) != null) {
                                ColumnProductDescription = document.getElementById(scope.ContainerId + "_productTitle" + i);
                            }
                            var productDescription = "";
                            if (ColumnProductDescription.innerHTML != null) {

                                productDescription = ColumnProductDescription.innerHTML;
                            }

                            if (answerCell != null) {
                                answerCell.innerHTML = result[resultInt].HurdleText;

                                if (answerCell.innerHTML == "null") {
                                    answerCell.innerHTML = "";
                                }
                            }

                            if (cellData != null && cellImg != null) {

                                cellData.style.cursor = "default";
                                cellData.style.color = "#000000";
                                cellData.style.textDecoration = "none";
                                cellData.onclick = "";

                                if (result[resultInt].BrochureDisplayType == 1) {
                                    cellData.innerHTML = result[resultInt].BrochureText;
                                }
                                else if (result[resultInt].BrochureDisplayType == 2) {

                                    cellData.style.cursor = "pointer";
                                    cellData.style.color = "#0000FF";
                                    cellData.style.textDecoration = "underline";
                                    cellData.innerHTML = result[resultInt].DisplayText;
                                    cellData.innerHTML = result[resultInt].DisplayText;

                                    var brochureText = result[resultInt].BrochureText;
                                    var title = selectedQuestionDescription + " - " + productDescription;

                                    cellData.onclick = scope.SetBrochureTextPopup(scope, title, brochureText);
                                }
                                else {
                                    cellData.innerHTML = result[resultInt].DisplayText;
                                }

                                cellImg.src = result[resultInt].DisplayImageUrl;
                            }
                            resultInt++;
                        }
                    }
                }
            }
        }

        var productIds = "";

        for (var i = 0; i < scope.ColumnCount; i++) {
            var cellDataId = scope.ContainerId + "_CellData" + scope.QuestionIndex + "-" + i;
            var cellData = document.getElementById(cellDataId);
            if (cellData != null) {
                var id = cellData.getAttribute("productId");
                if (id > 0) {
                    selectedCell[i] = id;
                    productIds = productIds + id + ",";
                }
                else {
                    selectedCell[i] = -1;
                }
            }
        }

        var body = '{"sessionToken":"' + scope.Guid + '","displayConfigurationId":' + scope.DisplayConfigurationId + ',"productIdList":[' + productIds.trim() + '],"questionId":' + selectedQuestionId + '}';
        xmlHttpObj.setRequestHeader("Content-type", "application/json");
        xmlHttpObj.send(body);

        scope.helpTextUpdate(scope, selectedQuestionId);

    }

    QuestionDropDown.prototype.SetBrochureTextPopup = function(scope, title, brochureText) {
        return function() {
            ShowHelpDialog(scope.ContainerId, "helpWrapper", "helpTitle", "helpText", brochureText, title,false);
        }
    }

    QuestionDropDown.prototype.helpTextUpdate = function(scope, questionId) {

        var xmlHttpObj = CreateXmlHttpRequestObject();
        if (xmlHttpObj) {
            var url = scope.QuestionEndPoint;
            xmlHttpObj.open("POST", url, true);
            xmlHttpObj.onreadystatechange = function() {
                if (xmlHttpObj.readyState == READYSTATE_COMPLETE) {
                    if (xmlHttpObj.status == HTTPSTATUS_OK) {
                        var result = eval('(' + xmlHttpObj.responseText + ')');
                        result = result.d;

                        if (result != null) {
                            if (result.Help != null) {
                                var text = result.Help.replace("\n", "<br/>").replace("\r", "").replace("'", "\\'");
                            }
                            else {
                                var text = "";
                            }
                            var title = result.QuestionText;
                            var helpImg = document.getElementById(scope.ContainerId + "_imgHelp" + scope.QuestionIndex);
                            
                            helpImg.onclick = function() {
                                ShowHelpDialog(scope.ContainerId, "helpWrapper", "helpTitle", "helpText", text, title, true);
                            }
                            
                        }
                        else {
                            alert("You do not have permission to use this service method.");
                        }

                    }
                }
            }

            var body = '{"sessionToken":"' + scope.Guid + '","questionId":' + questionId + '}';
            xmlHttpObj.setRequestHeader("Content-type", "application/json");
            xmlHttpObj.send(body);
        }
    }
    
}

