function popupWindow(tgetPage,queryString,winWidth,winHeight,winType,winName)
{
    var targetPage;
    var windowHeight;
    var windowWidth;
    var windowType;
    var windowName;

    if(queryString)
        targetPage=tgetPage+'?'+queryString;    
    else    
        targetPage=tgetPage;
    
    
    if(winHeight == 0)
        windowHeight=320;
    else
        windowHeight = winHeight;
        
    if(winWidth == 0)
        windowWidth=350;
    else
        windowWidth = winWidth;
        
    if(!winType)
        windowType = POP_MODAL;
    else
        windowType = winType;
        
    if(!winName)
        windowName="_blank";
    else
        windowName=winName;

    
    utilPopup.openWindow(targetPage, windowName, windowWidth, windowHeight,null, null, windowType); 
}

function forwardPage(tgetPage,queryString)
{

    var targetPage;

    if (queryString)
        targetPage=tgetPage+'?'+queryString;    
    else        
        targetPage=tgetPage;
        
    document.location.href = targetPage;    
}

function newCustomer()
{
    // Forwarding to blank page because directly popping up newcustomerblank.jsp was creating
    // problems when user entered bad data and page was forwarded to showalert.jsp.  When
    // user clicked back, it cleared out all the fields.
    //utilPopup.openWindow("newcustomerblank.jsp", "Customer", 980, 600, null, null, POP_MODAL);
    utilPopup.openWindow("newcustomerblank.jsp", "Customer", 980, 620, null, null, POP_MODAL);
}

function forwardNewCustomer()
{
    document.frmCustomer.action = "editcustomer.jsp?sourcePage=workPlan";
    document.frmCustomer.submit();
}
function newEmail(cid, cl_email_addr) {   
    if ((cl_email_addr == null) || (cl_email_addr == ""))
        displayErrorMessage( 'No primary email address', 'Please enter a primary email address and then compose email', null, false );
    else {
        var composeEmailWindow = new Advent.ClientEmailWindow({ 
            cid: cid,
            height: 600,
            width: 750
        });
        composeEmailWindow.initComposeEmail({
            Action: 'CreateEmail', ParamList: 'ClientID:' + cid + ',StandardEmailID:'
        },{
            to: cl_email_addr
        });
        composeEmailWindow.show();
        composeEmailWindow.getEl().mask( 'Loading...' );
    }
}

function refresh(frameObj)
{
    if(frameObj.history)
        frameObj.history.go(0);
    else
        frameObj.src = frameObj.src;
}

function delEmailAddr(cid,emailaddr,optemailaddr)
{
    utilPopup.openWindow("deleteemailaddrconfirm.jsp?ClientID=" + cid + "&EmailAddress=" + emailaddr + "&OptionalEmailAddress=" + optemailaddr, "EmailList", 350, 320, null, null, POP_MODAL);
}
function showPhone(clientID,phoneType,phoneNo)
{
     utilPopup.openWindow("parentdnclist.jsp?ClientID=" + clientID + "&PhoneType=" + phoneType + "&PhoneNo=" + phoneNo,"ViewCustomer", 550, 320, null, null, POP_MODAL);
}
function addDncList(clientID, phoneType, listType)
{   
    document.all.frame1.src = "savednc.jsp?Action=Yes&ClientID=" + clientID + "&PhoneType=" + phoneType + "&ListType=" + listType;  
}
function delDncList(clientID, phoneType, listType)
{   
    document.all.frame1.src = "savednc.jsp?Action=No&ClientID=" + clientID + "&PhoneType=" + phoneType + "&ListType=" + listType;   
}

function showComposeStandard(cl_id, cl_email_addr)
{
    if ((cl_email_addr == null) || (cl_email_addr == ""))
        displayErrorMessage( 'No primary email address', 'Please enter a primary email address and then compose email', null, false );
    else {
        if( !Ext.getCmp( 'emailListWindow' )) {
            var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});
            new Ext.Window({
                buttons: [{
                    handler: function( btn, e ) {
                        var composeStdEmail = function( abbrev, mime ) {
                            btn.findParentByType( 'window' ).hide();
                            var composeEmailWindow = new Advent.ClientEmailWindow({
                                cid: cl_id,
                                height: 600,
                                width: 750
                            });
                            composeEmailWindow.initComposeStdEmail({
                                Action: 'CreateEmail', ParamList: 'ClientID:' + cl_id + ',StandardEmailID:' + abbrev
                            },{
                                to: cl_email_addr
                            }, mime );
                            composeEmailWindow.show();
                            composeEmailWindow.getEl().mask( 'Loading...' );
                        };
                        var selected = Ext.getCmp( 'emailListGrid' ).getSelectionModel().getSelected();
                        if( selected ) {
                            var sentDate = selected.data.SentDate;
                            if( sentDate != '' )
                                Ext.Msg.confirm( 'Confirm', 'Are you sure you want to compose this email? It was last sent on ' + sentDate, function( response ) {
                                    if( response == 'yes' )
                                        composeStdEmail( selected.data.AbbreviationName, selected.data.MimeType  );
                                });
                            else
                                composeStdEmail( selected.data.AbbreviationName, selected.data.MimeType  );

                        }
                    },
                    text: 'Compose'
                },{
                    handler: function( btn, e ) {
                        btn.findParentByType( 'window' ).hide();
                    },
                    text: 'Close'
                }],
                closeAction: 'hide',
                height: 600,
                id: 'emailListWindow',
                items: [{
                    autoExpandColumn: 'description',
                    cm: new Ext.grid.ColumnModel([
                        sm,
                        {sortable: true, dataIndex: 'AbbreviationName', header: 'Abbreviation', width: 76},
                        {sortable: true, dataIndex: 'DescriptionInfo', header: 'Description', id: 'description', width: 281 },
                        {sortable: true, dataIndex: 'SubjectOfAbbreviation', header: 'Subject', width: 281 },
                        {sortable: true, dataIndex: 'SentDate', header: 'Sent', width: 112}
                    ]),
                    id: 'emailListGrid',
                    sm: sm,
                    store: crm.common.datastores.emaillist({
                        params: {
                            ClientID: advClient.id
                        }
                    }),
                    xtype: 'grid'
                }],
                layout: 'fit',
                modal: true,
                title: 'Standard Email List',
                width: 750
            });
        }
        Ext.getCmp( 'emailListWindow' ).show();
    }
}
function showBIStateSearch(form, tag, id)
{
    showStateSearch(form, tag, document.getElementById(id), DEFAULT);
}
function showStatusCode(frmName, id)
{
    showStausCodeSearch(frmName, "", "StatusCode", document.getElementById(id), 3, document.forms[frmName].elements["StatusCode"].value);
}
function showStatusCodeSelect(frmName, id)
{
    showStausCodeSearchSelect(frmName, "StatusCodeDescription", "StatusCode", document.getElementById(id), 3, document.forms[frmName].elements["StatusCode"].value);
}
function editRentalInventory(id)
{
    document.location.href  = "viewrentalinventorydetail.jsp?ID=" + id+"&View=text";
}
function showRentalMake(frmName, id)
{
    showRentalMakeSearch(frmName, "", "Make", "MakeID", document.getElementById(id), 4);
}
function editRentalDriver(cid, strLastName, strFirstName, primaryyn, id)
{
    utilPopup.openWindow("editrentaldrivers.jsp?ID=" + cid + "&LastName=" + strLastName + "&FirstName=" + strFirstName + "&PrimaryYN=" + primaryyn +"&AgreementID="+id, "EditRentalDriver", 350, 320, null, null, POP_MODAL);
}
function setZipLength(obj, countryType, nextObj, nextToNextObj)
{
	
	var limit;
	var allowedChars;
	var autoTabTo;
	if("ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf((obj.value.charAt(0)).toUpperCase()) < 0 )
	{
		limit = 5;
		allowedChars = CH_NUMERIC|CH_SPECIAL;
		autoTabTo = nextObj;
	}
	else
	{
		limit = 6;
		allowedChars = CH_ALL;
		autoTabTo = nextToNextObj;
	}
	
	utilKeyboard.AllowedChar = allowedChars;
	
	if(autoTabTo != '')
		utilKeyboard.autoTab(obj,autoTabTo,limit);
	
	if(limit < obj.value.length)
		obj.value = obj.value.substring(0,limit);
}        

