﻿
_Download = function() {       
}

_Download.prototype = {
    initialize: function() {                
                
    },              
    DoDownload : function(download_item, success_function) {
        download_item["Action"] = "DoDownload"; 
        $j.getJSON("DownloadHandler.ashx",
                   download_item,
                   function(result) {                                                                      
                        if (result.ResultType == 2) {
                            $('download_status').innerHTML = result.Message;
                        }
                        else {
                            if (result.Result == 'SaveDownloadSuccess') {                                    
                                modalDownload.hide(); 
                                if (success_function) {
                                    success_function.call();
                                } else {
                                    if (DownloadManager.IsEditMode())
                                    {
                                        var doc =  window.frames['editIFrame'].document;
                                        var edit_url = doc.getElementById('editURL');
                                        var edit_txt = doc.getElementById('editedText');
                                        edit_url.value = result.ClientRefId.trim();;
                                        edit_txt.value = escape(GetEditedContent());            
                                        doc.forms[0].submit();                                            
                                    } else 
                                        frames['dwnloadIFrame'].location.href = result.ClientRefId.trim();
                                }
                            } else {
                                modalDownload.show(); 
                                $('download_status').innerHTML = result.Result;
                            }
                        }                             
                   });
    },
    DoOneUpDownload : function() {    
        var lb = $('dwnloadUri');
        if (lb == null) {
            alert('Unable to read download Url.');
            return;
        }
        var download_item = eval('('+lb.options[lb.selectedIndex].value+');');               
        this.DoDownload(download_item,null);
    },
    CreateDownloadRequest : function(download_item) {
         download_item["Action"] = "CreateDownloadRequest";
         $j.getJSON("DownloadHandler.ashx",
                        download_item,
                        function(result) {                                                        
                            if (result.ResultType == 2) {
                                $('download_status').innerHTML = result.Message;
                            }
                            else {
                                $('download_status').innerHTML = result.Result;
                            }                            
                        });
    },
    ApproveDownloadRequest : function(download_item) {
        download_item["Action"] = "ApproveDownloadRequest";
         $j.getJSON("DownloadHandler.ashx",
                        download_item,
                        function(result) {                                                           
                                if (result.ResultType == 2) {
                                    $('download_status').innerHTML = result.Message;
                                }
                                else {
                                    $('download_status').innerHTML = result.Result;
                                }                            
                        });
    },
    IsEditMode : function() {
       var elem = document.getElementById('content_edit');   
       if (elem && (elem.style.display == '' || elem.style.display == 'block'))
            return true;
       else
            return false; 
    }
}
var DownloadManager = new _Download();
