var msgWin=null;
window.onbeforeunload = function()
{
    if (msgWin != null && msgWin != "undefined")
        msgWin.close();
}
function ShowTicketText(ticketId)
{
    var url=GetBaseUrl()+"/Ticket/ViewTicket.aspx?ticketID="+ticketId;
	msgWin=window.open(url,"TicketTextWin","width=950px, height=700px, top=10, left=10, scrollbars=1, status=0, menubar=0, toolbar=0, resizable=1");
	
	PassValues();
}
function LoadTicketConfirmation(ticketID, ticketFunctionID, edittype, occTicketId, noExtensionMessage)
{
    if ( noExtensionMessage != undefined && noExtensionMessage != null && noExtensionMessage != '')
    {
        var md=new ModalDialogue();
        md.Title='IRTH One Call';
        md.Text=noExtensionMessage;
        md.Show();
    }
    else
    {
        var url=GetBaseUrl()+"/Ticket/ViewTicket.aspx?ticketID="+ticketID+"&functionID="+ticketFunctionID+"&editType="+edittype+"&occTicketID="+occTicketId;
	    msgWin=window.open(url,"TicketWin","width=800px, height=500px, top=10, left=10, scrollbars=1, status=0, menubar=0, toolbar=0, resizable=1");
        PassValues();	
    }
}
function LoadTicketEdit(ticketID, ticketFunctionID, edittype, occTicketId, usePopup)
{

    if (usePopup) 
    {
        var url = GetBaseUrl() + "/Ticket/TicketEditing.aspx?ticketID=" + ticketID + "&functionID=" + ticketFunctionID + "&editType=" + edittype + "&occTicketID=" + occTicketId;
        msgWin = window.open(url, "TicketWin", "width=800px, height=500px, top=10, left=10, scrollbars=1, status=0, menubar=0, toolbar=0, resizable=1");
        PassValues();
    }
    else 
    {
        var url = GetBaseUrl() + "/Ticket/TicketCreation.aspx?ViewTicketID=" + ticketID + "&functionID=" + ticketFunctionID + "&editType=" + edittype + "&occTicketID=" + occTicketId;
        window.location = url;
    }
}
function ShowPositiveResponse(ticketId)
{
    var url=GetBaseUrl()+"/Ticket/PositiveResponse/EnterResponse.aspx?ticketID="+ticketId;
	msgWin=window.open(url,"ShowPositiveResponseWin","width=950px, height=700px, top=10, left=10, scrollbars=1, status=0, menubar=0, toolbar=0, resizable=1");
}

function EnterPositiveResponse(ticketId)
{
 var url=GetBaseUrl()+"/Ticket/ViewTicket.aspx?ticketID="+ticketId;
	msgWin=window.open(url,"TicketTextWin","width=950px, height=700px, top=10, left=10, scrollbars=1, status=0, menubar=0, toolbar=0, resizable=1");
}
function PassValues()
{
    try
    {//Need to try catch here because if the server gets recompiled by changing the web.config, or resetting iis
     // msgWin.document does not allow access here.
        if(msgWin.document.readyState != "complete")
	    {
		    tm=setTimeout("PassValues();",10);
		    return;
	    }
    	
	    msgWin.SetCameFromApp("true");
    }
    catch(e)
    {

    }
}
function CopyTicket(ticketID, ticketFunctionID)
{
    window.location = GetBaseUrl()+"/Ticket/TicketCreation.aspx?CopyFromTicketID="+ticketID;
}
function Refresh()
{
    var postbackControls = GetPostbackControls();
    if (postbackControls == null)
        return;

    postbackControls.hiddenResultControl.value = "1";
    postbackControls.hiddenTriggerControl.fireEvent("onchange");
}

function ExecuteCommand(command, argList)
{
    if ((command == null) || (command == undefined) || (command == ""))
        return;

    var postbackControls = GetPostbackControls();
    if (postbackControls == null)
        return;

    var cmd = "2|" + command;

    for (var i = 0; i < argList.length; i++)
    {
        cmd += "|";
        if (argList[i] != null)
            cmd += argList[i];
    }

    postbackControls.hiddenResultControl.value = cmd;
    postbackControls.hiddenTriggerControl.fireEvent("onchange");
}

function GetPostbackControls()
{
    var hiddenResultControl;
    var hiddenTriggerControl;
    var frm = document.forms[0]; 

    for (var i = 1; i < frm.elements.length; i++)
    {
        if ((hiddenResultControl != null) && (hiddenTriggerControl != null))
            break;

        var e = frm.elements[i];
        if (e.type != "hidden" && e.className != "hiddenItem")
            continue;

        if (e.title == "hiddenRefreshSearch")
            hiddenResultControl = e;
        else if (e.id.indexOf("hdnDropDown") > 0)
            hiddenTriggerControl = e;
    }

    if ((hiddenResultControl == null) || (hiddenTriggerControl == null))
        return null;

    return { hiddenResultControl: hiddenResultControl, hiddenTriggerControl: hiddenTriggerControl };
}

