﻿/*************************************************************
// Comments: This javascript code is used for handling the client
// side events of the request more info control.
*************************************************************/
       
    var REQUEST_INFO_TYPE_ANSWERED = 1;
    var REQUEST_INFO_TYPE_DO_NOT_KNOW = 2;
    var REQUEST_INFO_TYPE_UNANSWERED = 0; 
    var REQUEST_INFO_ANSWER_DO_NOT_KNOW = "The consumer does not know the answer to this question.";
    
    function RequestInfo()
    {
        // Nothing here as all instances prototyped.
    }    
    
      
    RequestInfo.prototype.IsPublicView = true;
    
    // Control ids.
    RequestInfo.prototype.LinkSaveReplyControlId = "";
    RequestInfo.prototype.LinkReportAbuseControlId = "";
    RequestInfo.prototype.LinkSaveReplyDoNotKnowControlId = "";
    RequestInfo.prototype.HiddenPostIdControlId = "";
    RequestInfo.prototype.TextConsumerAnswerControlId = "";
    
    // Event handlers.
    RequestInfo.prototype.Load = RequestInfo_OnLoad;
    
    RequestInfo.prototype.WriteReply = RequestInfo_WriteReply;
    RequestInfo.prototype.DoNotKnow = RequestInfo_DoNotKnow;        
    RequestInfo.prototype.Save = RequestInfo_Save;
    RequestInfo.prototype.Cancel = RequestInfo_Cancel;
    RequestInfo.prototype.Report = RequestInfo_Report;
    
    RequestInfo.prototype.GetTextBoxPrefix = RequestInfo_GetTextBoxPrefix;
    RequestInfo.prototype.GetLinkPrefix = RequestInfo_GetLinkPrefix;
    RequestInfo.prototype.ShowHideUpdateLinks = RequestInfo_ShowHideUpdateLinks;
    RequestInfo.prototype.ConfigureTextBoxUI = RequestInfo_ConfigureTextBoxUI
    RequestInfo.prototype.GetTextBoxId = RequestInfo_GetTextBoxId;
    RequestInfo.prototype.GetRequestTypeName = RequestInfo_GetRequestTypeName;
    
    
    ///<summary>Initializes the UI.</summary>
    function RequestInfo_OnLoad()
    {           
        ShowHideItem(this.LinkSaveReplyControlId, false);
        ShowHideItem(this.LinkReportAbuseControlId, false);
        ShowHideItem(this.LinkSaveReplyDoNotKnowControlId, false);
        ShowHideItem(this.TextConsumerAnswerControlId, false);
    }
    
    
    ///<summary>Gets the prefix used for naming the textbox's.</summary>
    function RequestInfo_GetTextBoxPrefix(requestType)
    {
        var prefix = "txtReqInfoAnswered_";
        
        if(requestType == REQUEST_INFO_TYPE_UNANSWERED)
            prefix = "txtReqInfoUnAnswered_";
            
        else if(requestType == REQUEST_INFO_TYPE_DO_NOT_KNOW)
            prefix = "txtReqInfoDoNotKnow_";
            
        return prefix;
    }
    
    
    function RequestInfo_GetRequestTypeName(requestType)
    {
        var name = "Answered";
        
        if(requestType == REQUEST_INFO_TYPE_UNANSWERED)
            name = "UnAnswered";
                        
        else if(requestType == REQUEST_INFO_TYPE_DO_NOT_KNOW)
            name = "DoNotKnow";
            
        return name;
    }
    
    
    function RequestInfo_GetLinkPrefix(requestType)
    {
        var prefix = "lnkReqInfoAnswered_";
        
        if(requestType == REQUEST_INFO_TYPE_UNANSWERED)
            prefix = "lnkReqInfoUnAnswered_";
            
        else if(requestType == REQUEST_INFO_TYPE_DO_NOT_KNOW)
            prefix = "lnkReqInfoDoNotKnow_";
            
        return prefix;
    }
    
    
    function RequestInfo_Report(requestType, postId)
    {
        // Store the post id.       
        document.getElementById(this.HiddenPostIdControlId).value = postId;  
        var controlId = this.LinkReportAbuseControlId;
        document.getElementById(controlId).click();
    }
    
    
    function RequestInfo_DoNotKnow(requestType, postId)
    {                    
        // Make the textbox editable.
        this.ConfigureTextBoxUI(requestType, postId, true, true);
        var txtId = this.GetTextBoxId(requestType, postId);
        
        // Put in the text "consumer...does not know.";
        document.getElementById(txtId).value = REQUEST_INFO_ANSWER_DO_NOT_KNOW;        
        
        // Show the links
        this.ShowHideUpdateLinks(requestType, postId, true);        
    }
    
    
    function RequestInfo_WriteReply(requestType, postId)
    {
        // Make the textbox editable.        
        this.ConfigureTextBoxUI(requestType, postId, true, true);
        
        // Show the links
        this.ShowHideUpdateLinks(requestType, postId, true);
    }
    
    
    function RequestInfo_Save(requestType, postId)
    {
        var txtId = this.GetTextBoxId(requestType, postId);
        var consumerAnswer = document.getElementById(txtId).value;
        var linkControlId = this.LinkSaveReplyControlId;;
        
        if(consumerAnswer == REQUEST_INFO_ANSWER_DO_NOT_KNOW)
        {
            linkControlId = this.LinkSaveReplyDoNotKnowControlId;
        }
        
        // Set the consumer answer text into the server side text box control.
        document.getElementById(this.TextConsumerAnswerControlId).value = consumerAnswer; 
        
        // Store the question post id.       
        document.getElementById(this.HiddenPostIdControlId).value = postId;  
        
        document.getElementById(linkControlId).click();     
    }
    
    
    function RequestInfo_Cancel(requestType, postId)
    {
        this.ConfigureTextBoxUI(requestType, postId, false, false);
        
        // Show the links
        this.ShowHideUpdateLinks(requestType, postId, false);            
    }
    
    
    function RequestInfo_ShowHideUpdateLinks(requestType, postId, blnShow)
    {
        var reqTypeName = this.GetRequestTypeName(requestType);
        var linksAreaControlId = "reqInfo" + reqTypeName + "UpdateLinksArea_" + postId;
        ShowHideItem(linksAreaControlId, blnShow);
    }
    
    
    function RequestInfo_GetTextBoxId(requestType, postId)
    {
        var txtPrefix = this.GetTextBoxPrefix(requestType);        
        var txtId = txtPrefix + postId;
        
        return txtId;
    }
    
    
    function RequestInfo_ConfigureTextBoxUI(requestType, postId, show, enable)
    {              
        var txtId = this.GetTextBoxId(requestType, postId);
        var isReadOnly = !enable;
        
        if(requestType == REQUEST_INFO_TYPE_UNANSWERED)
        {
            var replyAreaId = "reqInfoReplyArea_" + postId;
            ShowHideItem(replyAreaId, show);
        }
        ChangeControlEditState(txtId, isReadOnly); 
    }    
