var currentTopicId;
var currentMainTopicId;
var currentTopicText;

function popupHelp(url)
{
    window.open(url,'Shortcuts_Help','width=590,height=550,left=100,top=50,menubar=no,toolbar=yes,location=no,status=no,scrollbars=yes',false);
    return;
}

function getSubTopics()
{
    //When the process starts, hide the welcome
    var welcomeArea = document.getElementById("welcome");
    welcomeArea.style.display = "none";
    
    hideElements('main');
    
    var mainTopic = document.getElementById("mainTopic");
    if (mainTopic.selectedIndex == 0)
    {
       return;
    }
    
    var mainTopicId = mainTopic.options[mainTopic.selectedIndex].value;
    
    currentTopicId = mainTopicId;
    currentMainTopicId = mainTopicId;
    currentTopicText = mainTopic.options[mainTopic.selectedIndex].text;
    
    var url = "/HelpSubtopics.action";
    var params = "topicId=" + mainTopicId;
    myAjax = new Ajax.Request(url, {method: 'post', parameters: params, asynchronous: false, onComplete: updateSubtopics});
}

function updateSubtopics(request)
{
    var response = request.responseText.evalJSON();
    var result = response.status;
   
    if (result == "error")
    {
        alert("There was an error retrieving Help data.  Please fill out the following form to submit feedback to our Customer Care team.");
        
        //Set the form topic Id
        document.getElementById("ticketForm").topicId.value = currentTopicId;
        document.getElementById("ticketForm").mainTopicId.value = currentMainTopicId;
        
        var feedback = document.getElementById("sendFeedback");
        feedback.style.display = "block";
        return; 
    }
    
    if (response != null && response != "")
    {
        if (response.helpElementIds == null || response.helpElementText == null)
        {
            alert("There was an error retrieving Help data.  Please fill out the following form to submit feedback to our Customer Care team.");
            //Set the form topic Id
            document.getElementById("ticketForm").topicId.value = currentTopicId;
            document.getElementById("ticketForm").mainTopicId.value = currentMainTopicId;
            
            var feedback = document.getElementById("sendFeedback");
            feedback.style.display = "block";
            return; 
        }
    
        var subTopicObj = document.getElementById("helpSubTopic");
        subTopicObj.style.display = "block";
        
        var subTopicDropdown = document.getElementById("subTopic");
        subTopicDropdown.disabled = false;

        subTopicDropdown.options.length = 0;
        subTopicDropdown.options[0]=new Option("Please select a sub-topic.", "select", true, false);
        
        var topicIdString = new String(response.helpElementIds);
        var topicTextString = new String(response.helpElementText);
        var topicIdArray = topicIdString.split("|");
        var topicTextArray = topicTextString.split("|");
        
        //If there is only one, go ahead and call the update to display solution.
        if (topicIdArray.length == 1)
        {
          subTopicDropdown.options[1]=new Option(topicTextArray[0], topicIdArray[0], false, false);
          subTopicDropdown.options[1].selected = true;
          getTopicFaq();
        }
        else
        {
            for (var i=0; i < topicIdArray.length; i++)
            {       
                subTopicDropdown.options[i+1]=new Option(topicTextArray[i], topicIdArray[i], false, false);
            }
        }
    }
}

function getTopicFaq()
{
    hideElements('sub');
    
    var subTopic = document.getElementById("subTopic");
    if (subTopic.selectedIndex == 0)
    {
       return;
    } 
    
    var subTopicId = subTopic.options[subTopic.selectedIndex].value;

    currentTopicId = subTopicId;
    currentTopicText = subTopic.options[subTopic.selectedIndex].text;

    var url = "/HelpTopicAnswer.action";
    var params = "topicId=" + subTopicId + "&mainTopicId=" + currentMainTopicId;
    myAjax = new Ajax.Request(url, {method: 'post', parameters: params, asynchronous: false, onComplete: displayFaq});
}

function displayFaq(request)
{
    var response = request.responseText.evalJSON();
    
    var result = response.status;
   
    if (result == "error")
    {
        alert("There was an error retrieving Help data.  Please fill out the following form to submit feedback to our Customer Care team.");
        
        //Set the form topic Id
        document.getElementById("ticketForm").topicId.value = currentTopicId;
        document.getElementById("ticketForm").mainTopicId.value = currentMainTopicId;
        
        var feedback = document.getElementById("sendFeedback");
        feedback.style.display = "block";
        return; 
    }
    
    if (response != null && response != "")
    {
        if (response.topicAnswer == null)
        {
            alert("There was an error retrieving Help data.  Please notify help@shortcuts.com.");
            return;
        }
           
        var answerTitle = document.getElementById("answerTopic");
        var answerText  = document.getElementById("answerText");
        var moreInfoLink  = document.getElementById("moreInfoLink");

        answerTitle.innerHTML = currentTopicText;
        answerText.innerHTML  = response.topicAnswer;
        moreInfoLink.href = "javascript:getTopicQuestions(" + currentTopicId + ")";

        document.getElementById("helpContent").style.display = "block";
    }
}

function getTopicQuestions(topicId)
{
    hideElements('question');
    currentTopicId = topicId;
    
    var url = "/HelpTopicQuestions.action";
    var params = "topicId=" + topicId + "&mainTopicId=" + currentMainTopicId;
    myAjax = new Ajax.Request(url, {method: 'post', parameters: params, asynchronous: false, onComplete: showQuestions});
}

function showQuestions(request)
{
    var response = request.responseText.evalJSON();
    var result = response.status;
   
    if (result == "error")
    {
        //Simply proceed without custom questions...
        
        //Set the form topic Id
        document.getElementById("ticketForm").topicId.value = currentTopicId;
        document.getElementById("ticketForm").mainTopicId.value = currentMainTopicId;
        
        var feedback = document.getElementById("sendFeedback");
        feedback.style.display = "block";
        return; 
    }
   
    if (response != null && response != "")
    {
        var questionsDiv = document.getElementById("helpQuestions");
        
        var questionIdString = new String(response.helpElementIds);
        var questionTextString = new String(response.helpElementText);

        if (questionIdString != "undefined" && questionIdString != "" && 
            questionTextString != "undefined" && questionTextString !="")
        {
            var questionIdArray = questionIdString.split("|");
            var questionTextArray = questionTextString.split("|");
            
            var questionList = "";
            for (var i=0; i < questionIdArray.length; i++)
            {       
                questionList += "<br style=\"top-margin:2px;\"/>" + questionTextArray[i] + 
                                " <input type=\"text\" name=\"question-" + questionIdArray[i] + "\"/>";
            }
            questionsDiv.innerHTML = questionList;
        }

        //Set the form topic Id
        document.getElementById("ticketForm").topicId.value = currentTopicId;
        document.getElementById("ticketForm").mainTopicId.value = currentMainTopicId;
        
        var feedback = document.getElementById("sendFeedback");
        feedback.style.display = "block";
    }
}

function helpFormValidate(form)
{
    if(form.loginId.value.length == 0)
    {
       alert("Please be sure to fill out your email address.");
       form.loginId.focus();
       return false;
    }
    
    if(!check_email_addr(form.loginId.value))
    {
        alert("Please enter your email in the format email@domain.com.");
        form.loginId.focus();
        form.loginId.select();
        return false;
    }
    
    if(form.comments.value.length == 0)
    {
       alert("Please be sure to provide details about the issue you are experiencing.");
       form.comments.focus();
       return false;
    }
    return true;
}

function searchTerms()
{
    //Perform AJAX call to search topics...
    
    var searchResults = document.getElementById("searchTopicResults");
    searchResults.style.display = "block";
}

function hideElements(shown)
{
    if (shown == "main")
    {
       document.getElementById("helpSubTopic").style.display = "none";
    }

    if (shown == "main" || shown == "sub")
    {
       document.getElementById("helpContent").style.display = "none";
    }

    if (shown == "main" || shown == "sub" || shown == "question")
    {
       document.getElementById("helpQuestions").innerHTML = "";
       document.getElementById("ticketComments").value = "";
       document.getElementById("sendFeedback").style.display = "none";
    }
}