var CHOOSE_RECIPIENT_URL = '?path=mail/Addressbook&content-type=plain'
var FIND_CONTACT_URL     = '?path=mail/FindContact&content-type=plain'

function scrollFix(){var obol=document.getElementById('ol');obol.style.top=posTop()+'px';obol.style.left=posLeft()+'px';}

function sizeFix(){var obol=document.getElementById('ol');obol.style.height=pageHeight()+'px';obol.style.width=pageWidth()+'px';}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;}

function pageHeight() {return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;}    

function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}

function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
                  

function closeDialog ( )
{
    var obj = document.getElementById('ol');
    obj.style.display = 'none';
    var dlg = document.getElementById('modaldlg');
    dlg.innerHTML = '';
    dlg.style.visibility = 'hidden';
//    window.location.reload();
}



/**
 * Show a modal dialog
 */
function doModalDialog ( title, src, height, width)
{
    var b               = 'block';
    var p               = 'px';
    var obol            = document.getElementById('ol');
    obol.style.height   = pageHeight()+p;
    obol.style.width    = pageWidth()+p;
    obol.style.top      = posTop()+p;
    obol.style.left     = posLeft()+p;
    obol.style.display  = b;

    height = height || 40; 
    width  = width || 600; 
    var dlg = document.getElementById('modaldlg');
    dlg.innerHTML = '<div id="dlghandle" style="width: '+width+'px; border: 1px solid #555;"><table style="cursor: move;" cellspacing="0" cellpadding="0" border="0" width="100%"><tr style="background-image: url(/images/dlgbg_gray.png);"><td style="font-weight: bold; height: 19px;color: #000;">&nbsp;&nbsp;'+title+'</td><td style="color: #fff;" align="right"><a style="color: #fff;" href="javascript:closeDialog()"><img src="/images/dlgclose_gray.png" border="0" alt="Close"/></a></td></tr></table><div id="dlgcontent"><iframe id="dlgiframe" style="border: 0px solid #009;" src="'+src+'"></iframe></div><table cellspacing="0" cellpadding="0" border="0"><tr><td width="99%" style="background-image: url(/images/dlgstatusbar.png);">&nbsp;</td><td><img src="/images/dlgresize.png"/></td></tr></table></div>';
    dlg.style.left = (pageWidth()/2)-(width/2);
    dlg.style.top  = (pageHeight()/2)-(height/2)
    dlg.style.height=height;
    dlg.style.width=width;
    dlg.style.position='absolute';
    dlg.style.visibility = 'visible';

    window.onscroll = scrollFix;
    window.onresize = sizeFix;
}


/**
 * Set up the handling of choose recipient
 */
function handle_chooseRecipient ( )
{
    alert(args);
}


/**
 * Choose a recipient
 */
function chooseRecipient ( )
{
//    doModal(CHOOSE_RECIPIENT_URL, handle_chooseRecipient);
    doModalDialog ( 'Choose Recipient', CHOOSE_RECIPIENT_URL, 300, 500);
}


function resizeDialogToContent ( sourceObj, innerHeight, innerWidth )
{
    // Get the dialog handles
    var containerObj = document.getElementById('dlghandle');
    var innerObj     = document.getElementById('dlgcontent');
    var iframeObj    = document.getElementById('dlgiframe');

    // Get the size we want to use
    var height = innerHeight;
    var width  = innerWidth;

//alert('Height: ' + height +
//      ' Width: ' + width  );

    // Resize the container
    containerObj.style.width  = width;
    innerObj.style.width      = width;
    innerObj.style.height     = height;

    // Resize the iframe
    iframeObj.style.height = height;
    iframeObj.style.width  = width;
}


function selectRecipient ( id, name, address )
{
    var toObj   = window.parent.document.getElementById('to');
    var addrObj = window.parent.document.getElementById('addressList');

    if (toObj.value != '' )
        toObj.value += ',';
    
    if (addrObj.value != '' )
        addrObj.value += ',';
    

    toObj.value += name + '<'+address+'>';
    addrObj.value += id;
}

function sendMessage ( )
{
    document.getElementById('composeForm').submit();
}

function replyMessage ( )
{
    document.getElementById('replyForm').submit();
}

function forwardMessage ( )
{
    document.getElementById('message_command').value='forward';
    document.getElementById('replyForm').submit();
}

function deleteMessage ( id )
{
    document.getElementById('message_command').value='delete';
    document.getElementById('replyForm').action='?path=mail/Delete&id='+id;
    document.getElementById('replyForm').submit();
}

function findContact ( )
{
    doModalDialog ( 'Find Contact', FIND_CONTACT_URL, 300, 500);
}

function emptyTrash ( )
{
    alert('Empty Trash');
}

function setCaretToStart (control) 
{
    if (control.createTextRange) 
    {
        var range = control.createTextRange();
        range.collapse(true);
        range.select();
    }
    else if (control.setSelectionRange) 
    {
        control.focus();
        control.setSelectionRange(0, 0);
    }
}

