//Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
//<script>

var g_eGalTable;
var g_eErrorDiv;
var g_eCurrentRow;
var g_eGalTableDiv;
var g_eGalTitleDiv;
var g_eFindButton;
var g_eResetButton;

var m_rgPostFields;
var m_szUrlParams = "";
var m_objXML;
var m_xmlHTTP;

var g_rgElemRecipients = new Array();
var g_elemTo;
var g_elemCc;
var g_elemBcc;
var g_objMessage;

var g_selectedEmail="";
var g_objXmlSelectedRecipient=null;
var g_eWinDocAll;
var m_iANcolumn;

function window.onload() 
{
    g_eWinDocAll = window.document.all;

    if (window.dialogArguments != null)
    {
        var objWinDlgArgs = window.dialogArguments;
        btnTo.innerText = objWinDlgArgs["ToName"];
        btnCc.innerText = objWinDlgArgs["CcName"];
        btnBcc.innerText = objWinDlgArgs["BccName"];
        divAddRecipButtons.style.display = "";
        g_objMessage           = objWinDlgArgs["objMessage"]; 
    }
    
    g_eGalTable    = g_eWinDocAll['tblGalInfo'];
    g_eGalTable.rows(0).style.display="none";
    g_eGalTableDiv = g_eWinDocAll['divtable'];
    g_eGalTitleDiv = g_eWinDocAll['divtitles'];
    g_eErrorDiv    = g_eWinDocAll['divErrorMessage'];
    g_eFindButton  = g_eWinDocAll['btnfind'];
    g_eResetButton = g_eWinDocAll['btnreset'];

    m_rgPostFields = g_eWinDocAll['dataField']

    g_eGalTable.attachEvent("onmousedown",event_GalTable_onmousedown);
	g_eGalTable.attachEvent("ondragstart", event_GalTable_OnDragStart);
    window.document.body.attachEvent("onkeydown", event_window_onkeydown);
    g_eWinDocAll['DN'].focus();
}

function addRecipient(iWhich)
{
    if (g_objXmlSelectedRecipient.selectSingleNode("a:AN").text != "")
    {
        g_objMessage.addRecipients(iWhich+1,g_objXmlSelectedRecipient.selectSingleNode("a:DN").text + "[smtp:" + g_objXmlSelectedRecipient.selectSingleNode("a:EM").text + "]")
        g_objMessage.checkNames();
    }    
}

function event_window_onkeydown()
{
    if (event.keyCode==27)
    {
        this.close();
    }
	if (!event.ctrlKey) return(true);

    if (event.ctrlKey && event.altKey && event.keyCode==75 ) 
    {
        event.cancelBubble=true;
        event.returnValue=false;
        prompt("DEBUG WINDOW: Current URL parameters",m_szUrlParams);
    }
}

function event_GalTable_OnDragStart()
{
    if (event.srcElement.tagName == "A")
    {
        var eTR = event.srcElement.parentElement;
        while(eTR.tagName != "TR")
        {
            eTR = eTR.parentElement;
        }
    	window.event.dataTransfer.effectAllowed="all"; 
   		window.event.dataTransfer.setData("Text", "mailto:"+eTR.smtp);	
    	window.event.dataTransfer.setData("URL", "mailto:"+eTR.smtp) ;
    
    }    
}

function event_GalTable_onmousedown()
{
    if (event.srcElement.tagName != "TH")
    {
        if (g_eCurrentRow != null)
        {
            g_eCurrentRow.setAttribute("className","rowunselected");
        }
        eTR = event.srcElement.parentElement;
        while(eTR.tagName != "TR")
        {
            eTR = eTR.parentElement;
        }
       	eTR.setAttribute("className","rowselected");
       	g_eCurrentRow = eTR;
        processSelectedRow();        
    }
}

function event_button_find() {
    m_szUrlParams   = ""
    
    clearContents();
    
    for (var x=0; x<m_rgPostFields.length; x++)
    {
        if (m_rgPostFields[x].value != "")
        {
            fHasData = true;
            m_szUrlParams += "&" + m_rgPostFields[x].name + "=" + toUTF8(m_rgPostFields[x].value);
        }    
    }
    
    if (m_szUrlParams.length > 0)
    {
        queryGetData();
    }
    else
    {
        alert(g_idsNoSearchInfo_Text);
        window.document.all['DN'].focus();
    }    
    enableRecipientButtons(false);
}


function queryGetData() 
{
	m_xmlHTTP = new ActiveXObject("Microsoft.xmlhttp");
	try 
	{
		var szURL = g_szBaseURL + "?Cmd=galfind" + m_szUrlParams;
		m_xmlHTTP.open("GET",szURL,true); 
		if(g_szAcceptLang != "" )
		{
			m_xmlHTTP.setRequestHeader("Accept-Language:",g_szAcceptLang);
		}
		m_xmlHTTP.setRequestHeader( "Content-type:", "application/x-www-UTF8-encoded");	 
        m_xmlHTTP.onreadystatechange =event_listen_getGalData;
		m_xmlHTTP.send();
        doProgressIndicator(true);		
	}
	catch(e)
	{
        alert("DLG_GAL.JS error: "+e.description + "\r\nError number: " + e.number);
	}
}

function event_noop()
{
    return(false);
}    



function event_listen_getGalData()
{

	if(m_xmlHTTP.readyState == 4)
	{
        doProgressIndicator(false);		
    	m_xmlHTTP.onreadystatechange = event_noop;//re-entrant gate
	    if(m_xmlHTTP.responseText.charAt(0)=='\r')
	    {
	    	alert("error in event_listen_getGalData(): no responseText returned");
	    }
        else
        {
	        
	        m_objXML = new ActiveXObject("Microsoft.XMLDOM");
	        if (m_objXML.loadXML(m_xmlHTTP.responseText)==true)
	        {
                processXmlData();
            }
            else
            {
                alert("error in event_listen_getGalData(): "+m_objXML.parseError.reason);
            }    

	        
	    }    

    }

}

function processSelectedRow()
{
    var szEM = g_eCurrentRow.em;
    nodeItem = m_objXML.selectSingleNode("a:response/a:addresslist/a:item[a:EM = '"+ szEM +"']")
    g_objXmlSelectedRecipient = m_objXML.selectSingleNode("a:response/a:addresslist/a:item[a:EM = '"+ szEM +"']")
    g_selectedEmail = nodeItem.selectSingleNode("a:EM").text;
    if (btnTo.disabled == true)
    {
        enableRecipientButtons(true);
    }
}

function enableRecipientButtons(fYesNo)
{
    btnTo.disabled = !fYesNo;    
    btnCc.disabled = !fYesNo;    
    btnBcc.disabled = !fYesNo;    
    if (fYesNo == false)
    {
        g_selectedEmail="";
    }
}

function processXmlData()
{
    var rgData = new Array();
    var nodesAllItems = m_objXML.selectNodes("a:response/a:addresslist/a:item");
    
    if (nodesAllItems.length > 0)
    {
       	g_eGalTable.rows(0).style.display="";
        for(var x=0; x<nodesAllItems.length; x++)
        {            	        
	        rgData[0] = nodesAllItems.item(x).selectSingleNode("a:DN").text;
	        rgData[1] = nodesAllItems.item(x).selectSingleNode("a:PH").text;
	        rgData[2] = nodesAllItems.item(x).selectSingleNode("a:OF").text;
	        rgData[3] = nodesAllItems.item(x).selectSingleNode("a:TL").text;
	        rgData[4] = nodesAllItems.item(x).selectSingleNode("a:CP").text;
	        rgData[5] = nodesAllItems.item(x).selectSingleNode("a:AN").text;
	        m_iANcolumn = 5;//map where AN is for processSelectedRow();
	        addAddress(rgData,rgData[0] + "[SMTP:" + nodesAllItems.item(x).selectSingleNode("a:EM").text + "]",nodesAllItems.item(x).selectSingleNode("a:EM").text);
	    }
	}
	else
	{
        var nodesAllErrors = m_objXML.selectSingleNode("a:response/a:addresslist/a:error");
        
        if (nodesAllErrors != null)
        {            	        
    	    addMessage(nodesAllErrors.text);
        }
        else
        {
            addMessage(L_idsUnknownError_Text + "\r\n"+ L_idsTrySimplifySearch_Text);
        }
	}
}

function doProgressIndicator(fDisplay)
{
    if (fDisplay)
    {
        addMessage(L_idsSearcing_Text + "...");
        g_eFindButton.disabled = true;
        g_eResetButton.disabled = true;
    }
    else
    {
        clearContents();
        g_eFindButton.disabled = false;
        g_eResetButton.disabled = false;
    }    

}

function addAddress(rgData,szSMTP,szEmail)
{

    var eTD;
	var eTR = g_eGalTable.insertRow();
	eTR.setAttribute("className","rowunselected");
	eTR.setAttribute("smtp",szSMTP);
	eTR.setAttribute("em",szEmail);

	for (var x=0;x<rgData.length;x++)
	{
	    eTD = eTR.insertCell();
	    eTD.halign = "top";
	    eTD.style.paddingRight = "10px";
	    if (x==0) 
	    {
	        eTD.innerHTML = "<a href='#' onclick='return(false)' class='anchors'></a><nobr>" + rgData[x]+"</nobr>";
	    }    
	    else 
	    {
	        eTD.innerHTML = "<nobr>"+rgData[x]+"</nobr>";
	    }    
	}
}

function addMessage(szMsg)
{
    g_eErrorDiv.innerText = szMsg;
}

function clearContents()
{
    for (var x=g_eGalTable.rows.length-1; x >= 1; x--)
    {
        g_eGalTable.deleteRow(x);
    }
    g_eGalTable.rows(0).style.display="none";
    g_eErrorDiv.innerText = "";

}

function event_button_reset()
{
    clearContents();
    for (var x=0; x < m_rgPostFields.length; x++)
    {
        m_rgPostFields[x].value = ""
    }       
    enableRecipientButtons(false);    
}

//returns all characters utf8 encoded
function toUTF8(szInput)
{
    var wch,x,uch="",szRet="";

    for (x=0; x<szInput.length; x++)
    {
        wch=szInput.charCodeAt(x);
        if (!(wch & 0xFF80))
        {
            szRet += "%" + wch.toString(16);
        }
        else if (!(wch & 0xF000))
        {
            uch = "%" + (wch>>6 | 0xC0).toString(16) + 
                  "%" + (wch & 0x3F | 0x80).toString(16);
            szRet += uch; 
        }
        else
        {
            uch = "%" + (wch >> 12 | 0xE0).toString(16) + 
                  "%" + (((wch >> 6) & 0x3F) | 0x80).toString(16) +
                  "%" + (wch & 0x3F | 0x80).toString(16);
            szRet += uch; 
        }
    }
    return(szRet);
}
