﻿var fromMail = 0;

function getOnlineArchive(ThreadId,isMail)
{
    fromMail = isMail;
    SearchEngineService.getOnlineArchive(ThreadId,OngetOnlineArchiveComplete,OngetOnlineArchiveTimeOut,OngetOnlineArchiveError);
}

           
function OngetOnlineArchiveComplete(args)
{        
    document.getElementById("OnlineTable").innerHTML = args;
    if (fromMail == 1)
        changeDepatmentTabs();
}

function OngetOnlineArchiveTimeOut(args)
{
    if (args == "[object Object]")
        ReloadSession();
    else
        alert("!ארעה תקלה בעת טעינת המסמכים, אנא נסה שנית" + "\n" + args);
}

function OngetOnlineArchiveError(args)
{
    if (args == "[object Object]")
        ReloadSession();
    else
        alert("!ארעה תקלה בעת טעינת המסמכים, אנא נסה שנית" + "\n" + args);
}


//////////////////////////////////////////////////////////////////////////////

function loadQuestionDetailsByThreadId(ThreadId)
{
    document.getElementById("HidThreadId").value = ThreadId;
    SearchEngineService.loadQuestionDetailsByThreadId(ThreadId,OnloadQuestionDetailsByThreadIdComplete,OnloadQuestionDetailsByThreadIdTimeOut,OnloadQuestionDetailsByThreadIdError);
}
           
function OnloadQuestionDetailsByThreadIdComplete(args)
{    
    var ThreadId = document.getElementById("HidThreadId").value;
    document.getElementById("divThread_"+ThreadId).innerHTML = args;
}

function OnloadQuestionDetailsByThreadIdTimeOut(args)
{    
    alert("!ארעה תקלה בעת טעינת המסמכים, אנא נסה שנית" + "\n" + args);
}

function OnloadQuestionDetailsByThreadIdError(args)
{
    if (args == "[object Object]")
        ReloadSession();
    else
        alert("!ארעה תקלה בעת טעינת המסמכים, אנא נסה שנית" + "\n" + args);
}

/////////////////////////////////////////////////////////////////////////////

function AddNewQuestion()
{    
//    if (!CheckIsActive())
//    {
        //DisableButtons();
        
        var subject=document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_txtSubject").value;
        if (subject.trim()=="")
        {
           alert("חובה למלא נושא לפנייה")  
           EnableButtons();
           return ;
        }    
        var body=document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_txtMessage").value;        
        if (body.trim()=="")
        {
           alert("חובה למלא תוכן לפנייה")  
           EnableButtons();
           return ;
        }
        
        var radQuestionType = document.getElementById("radioQuestionType1").status;        
        var QuestionType;
        if (radQuestionType)
            QuestionType = 0;
        else
            QuestionType = 1;
                        
        var parentThread = document.getElementById("HidThreadId").value;

        SearchEngineService.AddNewQuestion(subject,body,parentThread,QuestionType,OnAddNewQuestionComplete,OnAddNewQuestionTimeOut,OnAddNewQuestionError);
//   }
}


function OnAddNewQuestionComplete(args)
{
//document.images["BtnSubmitD"].src = 'Images/Kolmas/SendR.gif';
if (args==true)
{
    clearNewQuestionFields();
    alert("שאלתך נקלטה במערכת," + "\n" + "בדקות הקרובות ישלח אליך מייל המאשר" + "\n" + "את קבלת הפניה. תודה, מערכת כל עתיד");
}
else
    alert("!ארעה תקלה בעת הוספת השאלה" + "\n" + "בדוק שהינך מחובר למערכת");

EnableButtons();
}

function OnAddNewQuestionTimeOut(args)
{
    //document.images["BtnSubmitD"].src = 'Images/Kolatid/SendR.gif';
    alert("!ארעה תקלה בעת הוספת השאלה, אנא נסה שנית" + "\n" + args);
}

function OnAddNewQuestionError(args)
{
    //document.images["BtnSubmitD"].src = 'Images/Kolatid/SendR.gif';   
    alert("!ארעה תקלה בעת הוספת השאלה, אנא נסה שנית" + "\n" + args);
}

function clearNewQuestionFields()
{        
    document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_txtSubject").value = "";
    document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_txtMessage").value = "";
    if (document.getElementById("HidThreadId").value == "0")
        document.getElementById("radioQuestionType1").status = true;
 
}


function setQestionsDisplay(imgId,trId,ThreadId)
{
    //debugger;
    //if (document.images[imgId].nameProp == 'Plus.gif')
    if (document.getElementById(trId).className == "HidTr")
    {
        document.images[imgId].src = 'Images/Minus.gif';
        document.getElementById(trId).className = 'ShowTr';
        var divId = "divThread_"+ThreadId
        if (document.getElementById(divId).innerHTML == "")
            loadQuestionDetailsByThreadId(ThreadId); 
    }
    else
    {
        document.images[imgId].src = 'Images/Plus.gif';
        document.getElementById(trId).className = 'hide';
    }
    
    //document.getElementById(trId.id).className = "ShowTr";    
}

function changeDepartmentTab(threadId , questionType)
{
    document.getElementById("HidThreadId").value = threadId;
    if (questionType == 0)
        document.getElementById("radioQuestionType1").status = true;
    else
        document.getElementById("radioQuestionType2").status = true;
        
    //document.getElementById("radioQuestionType1").disabled = true; 
    //document.getElementById("radioQuestionType2").disabled = true;
    clearNewQuestionFields();    
    //tabManager.ChangeTab('tdNew');
    document.getElementById("divNewQuestion").className = 'ShowHomePageDiv';
    document.getElementById("divArchive").className = 'HideHomePageDiv';
}

//disables the NewQuestion.ascx buttons
function DisableButtons()
{

    var sendButton =document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_BtnSubmitD");
    
    sendButton.src = "Images/send_gray.gif";
    sendButton.className ="newQuestionButtonDisabled";
    
    var clearButton =document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_BtnClearD");
    
    clearButton.src = "Images/clean_gray.gif";
    clearButton.className="newQuestionButtonDisabled";
}

//enables the NewQuestion.ascx buttons
function EnableButtons()
{
    var sendButton =document.getElementById("BtnSubmitD");
    
    sendButton.src = "Images/Kolatid/SendR.gif";
    sendButton.className="newQuestionButtonEnabled";
    
    var clearButton =document.getElementById("BtnClearD");
    clearButton.src = "Images/Kolatid/cleanR.gif";
    clearButton.className="newQuestionButtonEnabled";
    
}

//Checks if a question is currently being processed (by checking if the buttons are enabled)
function CheckIsActive()
{
    var sendButton =document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_BtnSubmitD");
    if (sendButton.className=="newQuestionButtonDisabled")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function changeDepatmentTabs()
{
    if (document.images["imgNew"].nameProp == 'newqu.gif')
    {
         document.images["imgNew"].src = 'Images/Tab/newqu-p.gif';
         document.images["imgArchive"].src = 'Images/Tab/archive.gif';
         document.getElementById("HidThreadId").value = "0";
         document.getElementById("radioQuestionType1").status = true;
         document.getElementById("divNewQuestion").className = 'ShowHomePageDiv';
         document.getElementById("divArchive").className = 'HideHomePageDiv';
         document.getElementById("radioQuestionType1").disabled = false; 
         document.getElementById("radioQuestionType2").disabled = false;
         document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_txtSubject").value = "";
         document.getElementById("ctl00_mainContentPlaceHolder_NewQuestion1_txtMessage").value = "";           
    }
    else
    {
        document.images["imgNew"].src = 'Images/Tab/newqu.gif';
        document.images["imgArchive"].src = 'Images/Tab/archive-p.gif';
        document.getElementById("divArchive").className = 'ShowHomePageDiv';
        document.getElementById("divNewQuestion").className = 'HideHomePageDiv';
    }
    
}
