
// Global for brower version branching.
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4));
var IE = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4));
var dialogWin = new Object();

function openDialog(url, width, height,title, returnFunc)
{
    var returnValue;
    if (IE)
    {
        var dialogParms = 'dialogHeight:' + height + 'px;dialogWidth:' + width + 'px; px;resizable:no;center:yes;status:0;help:0;';
        returnValue = window.showModalDialog(url,title,dialogParms);
        return returnValue;
    }
    else {
       openDGDialog(url, width, height, returnFunc);
       return 'NOTIE';
    }
}

// Generate a modal dialog.
// Parameters:
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    returnFunc - the function that will process the returnvalue
function openDGDialog(url, width, height, returnFunc)
{
	if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) 
	{
		// Initialize properties of the modal dialog object.
		dialogWin.returnFunc = returnFunc
		dialogWin.returnedValue = null;
		dialogWin.url = url
		dialogWin.width = width
		dialogWin.height = height
		
		// Keep name unique so Navigator doesn't overwrite an existing dialog.
		dialogWin.name = (new Date()).getSeconds().toString()
		// Assemble window attributes and try to center the dialog.
		if (!IE) 
		{
			// Center on the main window.
			dialogWin.left = window.screenX + 
			   ((window.outerWidth - dialogWin.width) / 2)
			dialogWin.top = window.screenY + 
			   ((window.outerHeight - dialogWin.height) / 2)
			var attr = "screenX=" + dialogWin.left + ",screenY=" + dialogWin.top + ",resizable=no,width=" + dialogWin.width + ",height=" + dialogWin.height
		} 
		
		// Generate the dialog and make sure it has focus.
		dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr);
	    dialogWin.win.focus();
	} 
	else 
	{
		dialogWin.win.focus();
	}
}

// Event handler to inhibit Navigator form element 
function deadend() 
{
	if (dialogWin.win && !dialogWin.win.closed) {
		dialogWin.win.focus()
		return false
	}
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.
function blockEvents() 
{   
	window.captureEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
	window.onclick = deadend
	window.onfocus = checkModal
}

// As dialog closes, restore the main window's original event mechanisms.
function unblockEvents() 
{
	window.releaseEvents(Event.CLICK | Event.MOUSEDOWN | Event.MOUSEUP | Event.FOCUS)
	window.onclick = null
	window.onfocus = null
		
	// invoke the return function
	dialogWin.returnFunc(dialogWin.returnedValue)
}

// Invoked by onFocus event handler of EVERY frame, return focus to dialog window if it's open.
function checkModal() 
{
	setTimeout("finishChecking()", 50)
	return true
}

function finishChecking() 
{
	if (dialogWin.win && !dialogWin.win.closed) 
	{
		dialogWin.win.focus() 
	}
}

function processCommit(returnValue)
{
    // routine to process value returned by dialog
    if (returnValue == null) {returnValue = 'undefined';}
    if (returnValue == 'done')
    {
        // refresh the screen
        document.forms[0].submit();
    }
    else
    {
        var returnmsg = returnValue.split('|');
        if (returnmsg[0] == 'error')
        {
            alert('There was an error processing the Commit - \n' + returnmsg[1]);
        }
    }
}

function importPaymentRequests()
{
    // routine to allow the user to select an excel file, and upload that to server for processing
    var url = 'PaymentRequestImport.aspx';
    var returnValue = openDialog(url,545,170,'Payment Request Import',processPaymentImports);
    if (returnValue != 'NOTIE')            
        processPaymentImports(returnValue);
}
        
function processPaymentImports(returnValue)
{
    // routine to process value to returned by dialog
    if (returnValue == null) {returnValue = 'undefined';}
    if (returnValue != 'undefined')
    {
	    // check if a commit was made
   	    var returnmsg = returnValue.split('|');
   	    if (returnmsg[0] == 'commit')
            commitPaymentImports(returnmsg[1]);
    }
}
        
function commitPaymentImports(url)
{
    // url to commit a Payment Request Import, done here because of timing issues with show modal dialog
    dialogWin = new Object();
    var url = 'Progress.aspx?type=' + url;
    var returnValue = openDialog(url,410,110,'Payment Imports Commit', processCommitPastel);
    if (returnValue != 'NOTIE')            
        processCommitPaymentImports(returnValue);
}
        
function processCommitPaymentImports(returnValue)
{
    // routine to process value returned by dialog
    if (returnValue == null) {returnValue = 'undefined';}
    if (returnValue != 'done')
    {
        var returnmsg = returnValue.split('|');
        if (returnmsg[0] == 'error')
        {
            alert('There was an error processing the Import - \n' + returnmsg[1]);
        }
    }
    else
    {
        // refresh
        document.forms[0].submit();
    }
}

function newPayRequest(pkid)
{
    // routine to bring up the New Payment Request Dialog
    var url = 'NewPaymentRequest.aspx';
    if (pkid != '')
        url += '?guidId=' + pkid;
    var returnValue = openDialog(url,900,460,'New Payment Request', processNewPayRequest);
    if (returnValue != 'NOTIE')            
        processNewPayRequest(returnValue);
}
        
function processNewPayRequest(returnValue)
{
    // routine to process value returned by dialog
    if (returnValue == null) {returnValue = 'undefined';}
    if (returnValue != 'undefined')
    {
	    // check if a commit was made
   	    var returnmsg = returnValue.split('|');
   	    if (returnmsg[0] == 'commit')
    	    commitNewPayRequest(returnmsg[1]);
    	if (returnmsg[0] == 'refresh')
    	    document.forms[0].submit();
     }
}

function commitNewPayRequest(guid)
{
    // url to commit a new request, done here because of timing issues with show modal dialog
    dialogWin = new Object();
    var url = 'Progress.aspx?type=1&guid=' + guid;
    var returnValue = openDialog(url,410,110,'New Payment Request Commit', processCommit);
    if (returnValue != 'NOTIE')            
        processCommit(returnValue);
}

function viewPastelExports()
{
    // routine to bring up Pastel Export Dialog
    var url = 'PastelExport.aspx';
    var returnValue = openDialog(url,740,370,'Pastel Export',processPastelExports);
    if (returnValue != 'NOTIE')            
        processPastelExports(returnValue);
}
        
function processPastelExports(returnValue)
{
    // routine to process value to returned by dialog
    if (returnValue == null) {returnValue = 'undefined';}
    if (returnValue != 'undefined')
    {
	    // check if a commit was made
   	    var returnmsg = returnValue.split('|');
   	    if (returnmsg[0] == 'commit')
    	    commitPastel(returnmsg[1]);
    }
}

function commitPastel(url)
{
    // url to commit a Pastel Export Request, done here because of timing issues with show modal dialog
    dialogWin = new Object();
    var url = 'Progress.aspx?type=' + url;
    var returnValue = openDialog(url,410,110,'Pastel Export Commit', processCommitPastel);
    if (returnValue != 'NOTIE')            
        processCommitPastel(returnValue);

}

function processCommitPastel(returnValue)
{
    // routine to process value returned by dialog
    if (returnValue == null) {returnValue = 'undefined';}
    if (returnValue != 'done')
    {
        var returnmsg = returnValue.split('|');
        if (returnmsg[0] == 'error')
        {
            alert('There was an error processing the Commit - \n' + returnmsg[1]);
        }
    }
}

function selectFilter()
{
    // routine to bring up task filter dialog
    document.forms[0].TaskType.value = 'Filter';
    var url = 'TaskFilter.aspx?' + document.forms[0].SendCriteria.value;
    var returnValue = openDialog(url,450,310,'Task Filter', processFilter);
    if (returnValue != 'NOTIE')            
        processFilter(returnValue);
}
        
function processFilter(returnValue)
{
    // routine to process value returned by dialog
    if (returnValue == null) {returnValue = 'undefined';} 
    if (returnValue != 'undefined') 
    {
        document.forms[0].Criteria.value = returnValue;
        document.forms[0].submit();
    }
}
function viewPaymentReport(url)
{
    // routine to view the audit report
    window.open('./UploadTemp/' + url,'_blank');
}

function dummyClose(returnValue)
{
}         
               



