// JScript File
//ViewTickets.aspx

var x = 4;
var y = 1;
function pageLoadInit(Id)
{
    
    var DDL_TimeOut = document.getElementById(Id); 
    
    if (DDL_TimeOut != null) {

        var ticketRefreshTime = getCookie('ticketRefreshTime');
        ticketRefreshTime = ticketRefreshTime == null ? DDL_TimeOut.options[DDL_TimeOut.selectedIndex].value : ticketRefreshTime;

        x = ticketRefreshTime;                    

        $('#' + Id).val(x);

        //$('#' + Id).selectpicker('refresh');
        initVirtualSelectPlugin('#' + Id);
        startClock(Id)
    }
        
}
function startClock(Id){
    var DDL_TimeOut = document.getElementById(Id); 

    if (DDL_TimeOut != null){

        x = x-y;            
       
        setTimeout("startClock('"+Id+"')",20000);
        if(x==0)
        {                
            var array = Id.split('DDL_TimeOut');
            var HF_ViewId =  document.getElementById(array[0]+'HF_ViewId');
            
            x = document.getElementById(array[0]+'DDL_TimeOut').options[document.getElementById(array[0]+'DDL_TimeOut').selectedIndex].value;
            //console.log('button click:', "#" + array[0] + 'Btn_Refresh');
            $("#"+array[0]+'Btn_Refresh').click();
            //document.location = "ViewTickets.aspx?V="+HF_ViewId.value+"&T="+DDL_TimeOut.selectedIndex;                    
        }
    }
}
function UpdateTimer(Id)
{
    var DDL_TimeOut = document.getElementById(Id); 
    x = DDL_TimeOut.options[DDL_TimeOut.selectedIndex].value;  
    var ticketRefreshTime = getCookie('ticketRefreshTime');
    if (x != ticketRefreshTime)
        setCookie('ticketRefreshTime', x, 365);
}
function IncreaseTimer(CB,CBC,C)
{
    x=40;
    if (C!='1')
    {
        SetChecked(CB, CBC)
    }
}

function resetVars()
{
    x = 4;
    y = 1;
}

function DepartmentChangeTickets(Id, LBX)
{
    var DDL_AssignTo = document.getElementById(Id);     
    DDL_AssignTo.options.length=1;//Resized to one element.
  
    var HF_AgentId =  document.getElementById(Id.replace(/DDL_AssignTo/,'HF_AgentId'));
    var LBX_Agents =  document.getElementById(LBX);
    var DDL_CDepartment =  document.getElementById(Id.replace(/DDL_AssignTo/,'DDL_CDepartment'));
    HF_AgentId.value=-1;
    var I=1;
    for(l=0;l<LBX_Agents.length;l+=1)
    {
        if (DDL_CDepartment.value==LBX_Agents.options[l].value)
        {
            var optText=LBX_Agents.options[l].innerHTML;
            array = optText.split(';');
            DDL_AssignTo.options[I] = new Option(array[1],array[0]);    
            I+=1;    
        }
    }
    
    setTimeout(function () {
        DDL_AssignTo.selectedIndex = 0;
        //$('#' + Id).selectpicker('refresh');
        initVirtualSelectPlugin('#' + Id);
    }, 500
    );

}

function DepartmentChange(Id)
{
    var DDL_AssignTo = document.getElementById(Id);     
    DDL_AssignTo.options.length=1;//Resized to one element.
   
    var HF_AgentId =  document.getElementById(Id.replace(/DDL_AssignTo/,'HF_AgentId'));
    var LBX_Agents = document.getElementById(Id.replace(/DDL_AssignTo/,'LBX_Agents'));  
    var DDL_CDepartment = document.getElementById(Id.replace(/DDL_AssignTo/,'DDL_CDepartment'));  
    HF_AgentId.value=-1;
    var I=1;
    for(l=0;l<LBX_Agents.length;l+=1)
    {
        if (DDL_CDepartment.value==LBX_Agents.options[l].value)
        {
            var optText=LBX_Agents.options[l].innerHTML;
            array = optText.split(';');
            DDL_AssignTo.options[I] = new Option(array[1],array[0]);    
            I+=1;    
        }
    }
    
    setTimeout(function () {
        DDL_AssignTo.selectedIndex = 0;
        //$('#' + Id).selectpicker('refresh');
        initVirtualSelectPlugin('#' + Id);
    }, 500);

}

function ChangePostBack(Id) {
   
    var DDL_AssignTo = document.getElementById(Id);    
    DDL_AssignTo.selectedIndex = 0;


    __doPostBack( document.getElementById(Id.replace(/DDL_AssignTo/,'Btn_Change')));
    return false;
}

function PopAgentChange(Id)
{
    var DDL_AssignTo = document.getElementById(Id);
    var array = Id.split('DDL_AssignTo');
    var HF_AgentId =  document.getElementById(array[0]+'HF_AgentId' + array[1]);
    HF_AgentId.value=DDL_AssignTo.value;
}


//End ViewTickets.aspx

// ------------- Add Announcement -------------- //
function disableExpirationdate(Id)
{ 
    var ChkBx_NeverExpire=document.getElementById(Id);
    var array = Id.split('ChkBx_NeverExpire');
    var TxBx_Expirationdate =  document.getElementById(array[0]+'TxBx_Expirationdate');
  
                              
    if (ChkBx_NeverExpire.checked == true)
    {
        TxBx_Expirationdate.style.color="#ffffff";
        TxBx_Expirationdate.disabled=true;
                         
    }
    else
    {TxBx_Expirationdate.disabled=false;
        TxBx_Expirationdate.style.color="#000000";
    }

}


function AddText(text,TxBx_Id) 
{ 
    //var tarea = document.aspnetForm.ctl00_RightContentPlaceHolder_TxBx_Body;
    var tarea = TxBx_Id;
    if (typeof tarea.selectionStart != 'undefined')
    { // if it supports DOM2
        start = tarea.selectionStart;
        end = tarea.selectionEnd;
        tarea.value = tarea.value.substr(0,tarea.selectionStart) + text + tarea.value.substr(tarea.selectionEnd);
        tarea.focus();
        tarea.selectionStart = ((start - end) == 0) ? start + text.length : start;
        tarea.selectionEnd = start + text.length;
    } 
    else 
    {
        if (tarea.createTextRange && tarea.caretPos) 
        {
            var caretPos = tarea.caretPos;
            caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?   text + ' ' : text;
        }
        else 
        {
            tarea.value += text;
        }
        tarea.focus(caretPos);
    }
}


function InsertVariable(variable,DDL_Id,TxBx_Id) 
{
    var text = getText(TxBx_Id);

    //alert(variable);
    if (variable) 
    {
        //AddTxt="["+variable+"]" + text + "[/"+variable+"]";
        AddTxt= text  + variable;
        AddText(AddTxt,TxBx_Id);
    }
    //document.Form1.DDL_Variables.selectedIndex=0;
    //document.aspnetForm.ctl00_RightContentPlaceHolder_DDL_Variables.selectedIndex=0;

    setTimeout(function () { DDL_Id.selectedIndex = 0; $(DDL_Id).trigger('change'); }, 500);
}

function InsertVariableEmailT(variable,DDL_Id,TxBx_Id,TxBx_Sub,HF) 
{
    if(DDL_Id && DDL_Id.selectedIndex > 0){
        var text = getText(TxBx_Id);
        var subj =  getText(TxBx_Sub);
        //var TxBx = document.getElementById(TxBx_Sub);
        //var HField = document.getElementById(HF);

        //alert(variable);
        //alert(HF.value);
        //alert("HI");
        if(HF.value == '0'){
            //alert("FOCUSED");
            if (variable) 
            {
                //AddTxt="["+variable+"]" + text + "[/"+variable+"]";
                AddTxt= subj  + variable;
                AddText(AddTxt,TxBx_Sub);
            }
        }
        else {
            if (variable) 
            {
                //AddTxt="["+variable+"]" + text + "[/"+variable+"]";
                AddTxt= text  + variable;
                AddText(AddTxt,TxBx_Id);
            }
        }
        //document.Form1.DDL_Variables.selectedIndex=0;
        //document.aspnetForm.ctl00_RightContentPlaceHolder_DDL_Variables.selectedIndex=0;
        setTimeout(function () { DDL_Id.selectedIndex = 0; $(DDL_Id).trigger('change'); }, 500);
    }
}

function storeCaret(ftext) 
{
    if (ftext.createTextRange) 
    {
        ftext.caretPos = document.selection.createRange().duplicate();
    }
}

function CopyOption(TxBx_Sub,HF){
    //var TxBx = document.getElementById(TxBx_Sub);
    //alert(TxBx_Sub);
    var HField = document.getElementById(HF);

    HField.value = TxBx_Sub;
    //alert(HField.value);
}

function getText(TxBx_Id) 
{

    //var tarea = document.aspnetForm.ctl00_RightContentPlaceHolder_TxBx_Body;

    var tarea = TxBx_Id;
    if (tarea.createTextRange && tarea.caretPos) 
    {
        return tarea.caretPos.text;
    } 
    else 
        if (typeof tarea.selectionStart != 'undefined')
        {
            return tarea.value.substr(tarea.selectionStart,tarea.selectionEnd-tarea.selectionStart)
        }

    return '';
}
