/** Lightbox Configuration **/
var fileLoadingImage = "/skins/default/images/lightbox/loading.gif";        
var fileBottomNavCloseImage = "/skins/default/images/lightbox/closelabel.gif";

var overlayOpacity = 0.8;    // controls transparency of shadow overlay

var animate = true;            // toggles resizing animations
var resizeSpeed = 7;        // controls the speed of the image resizing animations (1=slowest and 10=fastest)

var borderSize = 10;        //if you adjust the padding in the CSS, you will need to update this variable
/** End Lightbox Configuration **/

var CBSMisc = {
//    popupWindows : [],
    
    showPopup : function(url, name, width, height, scrollbars) {
        if (scrollbars == undefined) scrollbars = true;
        scrollbars = !!scrollbars;
        
        var left   = (screen.width  - width)  / 2;
        var top    = (screen.height - height) / 2;
        var arguments = 'width='+width+', height='+height +
                        ', top='+top+', left='+left + 
                        ', scrollbars=' + (scrollbars ? 1 : 0);
        
        if (url.substring(0, 7) != 'http://' && url.substring(0, 8) != 'https://' && url.substring(0, 6) != 'about:' && url != '') {
            url = 'http://'+url;
        }
        
        popupWindow = window.open(url, name, arguments);
        if (popupWindow) {
            if (window.focus) {
                popupWindow.focus();
            }
//            CBSMisc.popupWindows.push(popupWindow);
            return popupWindow;
        } else {
            CBSGUI.showAlert("You appear to be using a pop-up blocker. Please disable it or allow this site to open pop-up window, then try again.");
            return false;
        }
    },
    
    showEmailFromHistory : function(body, subject, id) {
        var newWindow = CBSMisc.showPopup('', 'email_'+id, 800, 600, true);
        if (newWindow) {
            newWindow.document.body.innerHTML = '';
            newWindow.document.write(body);
        }
    },
    
    showInvoice : function(id) {
        function handler(r) {
            if (r && r.data) {
                var newWindow = CBSMisc.showPopup('', 'invoice_'+id, 800, 600, true);
                if (newWindow) {
                    newWindow.document.body.innerHTML = '';
                    newWindow.document.write(r.data);
                    newWindow.document.close();
                }
            }
        }
        //newRequest : function(url, handler, showProgress, method, postdata, showErrMsg, allowRetries)
        CBSAjax.newRequest('ajax.php?f=view-invoice&i='+id, handler, true);
        return false;
    },

    payment : {
        TYPE_OFFSITE            : 'offsite'
    },
    
    payInvoiceSubmit : function() {
        function payInvoiceSubmitHandler(r) {
            if (r) {
                var data = r.data;
                if (!data) {
                    CBSGUI.showAlert('There was a problem processing your invoice payment. Please contact customer services.');
                    return;
                } else {
                    CBS.getEl('paymentformdiv').innerHTML = data.paymentform;
                    CBS.getEl("paymentform").submit();
                }
            }
        }
        
        var invoiceId = CBS.getFieldValue('invoiceid');
        var paymentMethodId = CBS.getSelectedRadioButtonValue('paymentmethod');
        CBSAjax.newRequest('ajax.php?f=pay-invoice-submit&i='+invoiceId+'&p='+paymentMethodId, payInvoiceSubmitHandler, true, 'get', null, true, true);
    },
    
    submitTicketAttachmentsSetup : false,
    submitTicketNextAttachmentNumber : 1,
    
    addSubmitTicketAttachment : function() {
        /*
        <tbody id="submitticket_attachments">
            <tr>
                <th>
                    <label for="attachment_1">Attachment:</label>
                </th>
                <td>
                    <input type="file" name="attachments[]" id="attachment_1" /> <input type="button" value="Remove" class="newsletterbutton" />
                </td>
            </tr>
        </tbody>
        */
        var attachments = CBS.getEl('submitticket_attachments');
        if (!CBSMisc.submitTicketAttachmentsSetup) {
            attachments.innerHTML = '';
            CBSMisc.submitTicketAttachmentsSetup = true;
        }
        
        var tr = CBS.cE('tr');
            var th = CBS.cE('th');
                var lbl = CBS.cE('label');
                    lbl.setAttribute('for', 'attachment_'+CBSMisc.submitTicketNextAttachmentNumber);
                    lbl.appendChild(CBS.cTN('Attachment:'));
                th.appendChild(lbl);
            tr.appendChild(th);
            var td = CBS.cE('td');
                var file = CBS.cE('input');
                    file.type = 'file';
                    file.name = 'opt_file[]';
                    file.id = 'attachment_'+CBSMisc.submitTicketNextAttachmentNumber;
                td.appendChild(file);
                td.appendChild(CBS.cTN(' '));
                var remove = CBS.cE('input');
                    remove.type = 'button';
                    remove.value = 'Remove';
                    remove.className = 'newsletterbutton';
                    CBS.addEvent(remove, 'click', function(){tr.parentNode.removeChild(tr);});
                td.appendChild(remove);
            tr.appendChild(td);
        attachments.appendChild(tr);
        
        CBSMisc.submitTicketNextAttachmentNumber++;
    }
};