﻿
/*
fileSavePath:檔案儲存路徑
Id:檔案Id
IsShowMessage:是否呈現上傳完訊息
ShowImageTargetID:已上傳圖片的IMG Tag Id
successFuncName:上傳成功後,接著執行的js function
*/

function ajaxFileUpload(fileSavePath, Id, IsShowMessage, ShowImageTargetID, successFuncName) {
    /*
    prepareing ajax file upload
    url: the url of script file handling the uploaded files
    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
    dataType: it support json, xml
    secureuri:use secure protocol
    success: call back function when the ajax complete
    error: callback function when the ajax failed
    */
    $.ajaxFileUpload
		(
			{
			    url: '/FileUpload/FileUpload/' + fileSavePath,
			    secureuri: false,
			    fileElementId: 'fileToUpload_' + Id,
			    dataType: 'xml',
			    success: function(data, status) {

			        $returndata = $(data).find('message:eq(0)');
			        //alert($returndata.attr('status'));
			        //alert('fileSavePath: ' + fileSavePath + '  Id:' + Id + '  IsShowMessage: ' + IsShowMessage + '  ShowImageTargetID: ' + ShowImageTargetID + '  successFuncName: ' + successFuncName);
			        if ($returndata.attr('status') == 'OK') {
			            $('#hid_' + Id).val($returndata.attr('newfilename'));

			            if (ShowImageTargetID != 'undefined' && ShowImageTargetID != "") {
			                $('#' + ShowImageTargetID).attr('src', $returndata.attr('newfilename'));
			                $('#' + ShowImageTargetID).attr('style', 'display');
			            }

			        }
			        $('#labShowMsg_' + Id).html($returndata.attr('status'));

			        if (IsShowMessage.toUpperCase() == 'TRUE')
			            alert("上傳檔案狀態:" + $returndata.attr('status') + " \r\n說明:" + $returndata.attr('description') + " \r\n檔案儲存名稱:" + $returndata.attr('newfilename'));
			        //alert(successFuncName + "('" + Id + "','" + $returndata.attr('status') + "','" + $returndata.attr('newfilename') + "')");
			        if (successFuncName != 'undefined' && successFuncName != "") {
			            if (eval("typeof " + successFuncName + " == 'function'")) {
			                //alert("555");
			                //eval(successFuncName + "('" + Id + "','" + $returndata.attr('status') + "','" + $returndata.attr('newfilename') + "')");
			                eval(successFuncName + "('" + $returndata.attr('status') + "','" + $returndata.attr('newfilename') + "')");
			            }
			        }
			    },
			    error: function(data, status, e) {
			        alert(e);
			    }
			}
		)
    return false;
}



/*
fileSavePath:檔案儲存在
Id:檔案Id
IsShowMessage:是否呈現上傳完訊息
ShowImageTargetID:已上傳圖片的IMG Tag Id
successFuncName:上傳成功後,接著執行的js function
*/

function uninetFileUpload(fileSavePath, Id
, IsShowMessage, ShowImageTargetID, successFuncName
, size, width, height, TargetId, ThumbWidth, ThumbHeight
, DefaultValue, showThumb, extension, uploadType) {

    var uploadControl = "FileUpload";
    if (uploadType != null
        && uploadType != '') {
        uploadControl = uploadType;
    }
    $.ajaxFileUpload
		(
			{
			    url: '/' + uploadControl + '/Upload/' + fileSavePath + '_' + width + '_' + height,
			    secureuri: false,
			    fileElementId: 'fileToUpload_' + Id,
			    dataType: 'xml',
			    success: function(data, status) {
			        $returndata = $(data).find('message:eq(0)');

			        if ($returndata.attr('status') == 'OK') {
			            $('#hid_' + TargetId).val($returndata.attr('newfilename'));

			            // setting display
			            if (ShowImageTargetID != 'undefined' && ShowImageTargetID != "") {
			                var UploadFileType = $returndata.attr('type');
			                var UploadFileName = $returndata.attr('newfilename');

			                showUploadFile(
			                    Id, UploadFileName, ThumbWidth, ThumbHeight
			                    , ShowImageTargetID, DefaultValue, width, height
			                    , showThumb);
			            }
			        }
			        $('#labShowMsg_' + Id).html($returndata.attr('status'));

			        // reslut message

			        if (IsShowMessage.toUpperCase() == 'TRUE') {
			            var blnSuccess = true;
			            var strResult = "成功";
			            if ($returndata.attr('status') != 'OK') {
			                blnSuccess = false;
			                strResult = "未成功";
			            }

			            var resultInfo = "上傳檔案狀態 : " + strResult
			                    + " \r\n說明 : " + $returndata.attr('description');
			            if (blnSuccess) {
			                resultInfo =
			                    resultInfo + " \r\n檔案名稱 : "
			                    + $returndata.attr('newfilename').split('/')[$returndata.attr('newfilename').split('/').length - 1];
			            }
			            
			            // show message
			            alert(resultInfo);
			        }

			        if (successFuncName != 'undefined' && successFuncName != "") {
			            if (eval("typeof " + successFuncName + " == 'function'")) {
			                eval(successFuncName + "('" + Id + "','" + $returndata.attr('status') + "','" + $returndata.attr('newfilename') + "')");
			            }
			        }
			    },
			    error: function(data, status, e) {
			        alert(e);
			    }
			}
		);
    return false;
}

// show thumb
function showUploadFile(Id, UploadFileName, ThumbWidth, ThumbHeight, ShowImageTargetID, DefaultValue, width, height, showThumb) {
    
    var ImgDataType = new Array();
    ImgDataType[0] = "jpg";
    ImgDataType[1] = "gif";
    ImgDataType[2] = "png";
    //var FlashDataType = "swf";

    var FileDataType = new Array();
    FileDataType[0] = "pdf";
    FileDataType[1] = "doc";
    FileDataType[2] = "xls";
    FileDataType[3] = "ocx";
    

    var i = 0;
    var UploadFileType = "";
    
    while (ImgDataType[i]) {
        if (UploadFileName.slice(UploadFileName.length - 3) == ImgDataType[i])
            UploadFileType = "image";
        i++;
    }

    if (UploadFileType == "" && UploadFileName.slice(UploadFileName.length - 3) == "flv") {
        UploadFileType = "flash";
    }
    else if (UploadFileType == "" && UploadFileName.slice(UploadFileName.length - 3) == "swf") {
        UploadFileType = "flash";
    }

    // show Thumb or not
    if (showThumb) {
        if (UploadFileType == 'image') {
            var size = '';
            var dt = new Date();
            if (ThumbWidth != "0") size += 'width= "' + ThumbWidth + '"';

            $('#' + ShowImageTargetID).html('<img src="' + UploadFileName + '?ss=' + dt.getMilliseconds() + '" ' + size + '>');

        }
        else if (UploadFileType == 'flash') {
            if (width == "0") width = 200;
            if (height == "0") height = 200;
            var flashHmtl = '<div style="position: absolute">'
			                + '<img src="/Content/flash/transparent.gif" style="opacity:0;filter:alpha(opacity=0)" width="' + width + '" height="' + height + '">'
			                + '</div>'
			                + '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + width + '" height="' + height + '">'
			                + '<param name="movie" value="' + UploadFileName + '"/>'
			                + '<param name="wmode" value="transparent" />'
			                + '<object wmode="transparent" type="application/x-shockwave-flash" data="' + UploadFileName + '" width="' + width + '" height="' + height + '">'
			                + '</object>'
			                + '</object>';
            $('#' + ShowImageTargetID).html(flashHmtl);
        }
    }
    
    // hide browser file component
    $('#fileToUpload_' + Id).hide();
    $('#FilmPreviewPath' + Id).val(UploadFileName);
    $('#' + Id + 'ComponentC').hide();

    // show file link
    var fileIcon = "";
    var fileExt = UploadFileName.slice(UploadFileName.length - 3).toLowerCase();
    var isFind = false;
    
    // setting file Icon
    i = 0;
    while (ImgDataType[i]) {
        if (UploadFileName.slice(UploadFileName.length - 3).toLowerCase() == ImgDataType[i]) {
            isFind = true;
            fileIcon = '<img src="/Content/Uninet/images/fileTypeIcon/picture.png" border="0" />';
        }
        i++;
    }
    if (!isFind) {
        i = 0;
        while (FileDataType[i]) {
            if (UploadFileName.slice(UploadFileName.length - 3).toLowerCase() == FileDataType[i]) {
                isFind = true;
                fileIcon = '<img src="/Content/Uninet/images/fileTypeIcon/' + FileDataType[i] + '.png" border="0" />';
            }
            i++;
        }
    }
    if (!isFind) {
        fileIcon = '<img src="/Content/Uninet/images/fileTypeIcon/file.png" style="" border="0" />';
    }
    $('#ShowFileSavePath' + ShowImageTargetID).html( fileIcon +" <a href=\"" + UploadFileName + "\" rel=\"" + ShowImageTargetID + "\" target=\"_bank\">" + UploadFileName.split('/')[UploadFileName.split('/').length - 1] + "</a>");
    $('#ShowFileSavePath' + ShowImageTargetID).show();

    // This line is used for light box component.
    // Temporarily comment out following lines until bug fix for multi-file uploaded function.

    var isImg = false;
    i = 0;
    while (ImgDataType[i]) {
        if (UploadFileName.slice(UploadFileName.length - 3).toLowerCase() == ImgDataType[i]) {
            isImg = true;
        }
        i++;
    }

    if (isImg) {
        $("a[rel^='" + ShowImageTargetID + "']").fancybox({
            'titleShow': false
        });
    } 
    
    $('#DeleteUploadFile' + ShowImageTargetID).show();

}

function GetUploadImageFieldNameSingle(fieldName) {
    return "FilmPreviewPath" + fieldName + "1";
}

function ResetImagefield(fieldName) {
    
    var filedName = GetUploadImageFieldNameSingle(fieldName);

    // clear
    $('#DeleteUploadFile' + fieldName + "1").parent().children("label").html('');
    $('#DeleteUploadFile' + fieldName + "1").parent().children("input").eq(0).val('');
    $('#DeleteUploadFile' + fieldName + "1").hide();
    $('#DeleteUploadFile' + fieldName + "1").parent().children("input").eq(2).hide();
    $('#DeleteUploadFile' + fieldName + "1").parent().children("input").eq(0).show();
    $('#DeleteUploadFile' + fieldName + "1").parent().children("span").show();

    if ($('#DeleteUploadFile' + fieldName + "1").parent().children("input").eq(1).val('') != '') {
        $('#resetButton' + fieldName).click();
    }
}
