<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<style type="text/css">
body {
BACKGROUND:#f0f0f0;
MARGIN:0;
FONT:normal 12px 宋体;
}

input {
border:1px solid #ccc;
height:22px;
font-size:12px;
color:#252728;
padding:3px 0px 0px 6px;
BACKGROUND:#fff;
}

.input2{
border:#614948 0px solid;
width:250px;
height:23px;
color:#A28181;
font-size:12px;
overflow:hidden;
background-image:url({$vd['sc']}images/upload.gif);
background-position:0px 0px;
background-repeat:no-repeat;
}

.file{
width:0px; 
height:20px;
margin-left:-144px;
*margin-left:-35px; 
filter:alpha(opacity=0);
-moz-opacity:.0;
opacity:0.0;
cursor:pointer;
}

#preview_size_fake{
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);
visibility:hidden;
}
#frmUpload div{
margin-left:55px
}
</style>
</head>
<body>
<script type="text/javascript">
  function BuildBaseUrl( command )
  {
  	IsFlash();
    var sUrl =
      document.getElementById('cmbConnector').value +
      '?Command=' + command +
      '&Type=' + document.getElementById('cmbType').value +
      '&CurrentFolder=' + encodeURIComponent(document.getElementById('txtFolder').value) ;
    return sUrl ;
  }
  
  function IsFlash()
  {
  	obj = document.getElementById("txtFileUpload");
  	if( obj.value.match( /.swf/i ) ){
        document.getElementById("cmbType").value = "Flash";
        return true;
    }
    else
  	{
  		return false;
  	}
  } 
  
  function SetFrameUrl( url )
  {
    document.getElementById('eRunningFrame').src = url ;
  
    document.getElementById('eUrl').innerHTML = url ;
  }
  
  function GetFolders()
  {
    SetFrameUrl( BuildBaseUrl( 'GetFolders' ) ) ;
    return false ;
  }
  
  function GetFoldersAndFiles()
  {
    SetFrameUrl( BuildBaseUrl( 'GetFoldersAndFiles' ) ) ;
    return false ;
  }
  
  function CreateFolder()
  {
    var sFolder = prompt( 'Type the folder name:', 'Test Folder' ) ;
  
    if ( ! sFolder )
      return false ;
  
    var sUrl = BuildBaseUrl( 'CreateFolder' ) ;
    sUrl += '&NewFolderName=' + encodeURIComponent( sFolder ) ;
  
    SetFrameUrl( sUrl ) ;
    return false ;
  }
  
  function OnUploadCompleted( errorNumber, fileName )
  {
    switch ( errorNumber )
    {
      case 0 :
        SetFile(fileName);
        //alert( 'File uploaded with no errors' ) ;
        break ;
      case 201 :
        GetFoldersAndFiles() ;
        SetFile(fileName);
        //alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
        break ;
      case 202 :
        alert( 'Invalid file' ) ;
        break ;
      default :
        alert( 'Error on file upload. Error number: ' + errorNumber ) ;
        break ;
    }
  }
  
  this.frames.frmUpload = this ;
  
  function SetAction()
  {
    var sUrl = BuildBaseUrl( 'FileUpload' ) ;
    document.getElementById('eUrl').innerHTML = sUrl ;
    document.getElementById('frmUpload').action = sUrl ;
  }
  
  function ShowImg(obj, imgid)
  {
    if( !obj.value.match( /.jpg|.gif|.png|.swf|.bmp/i ) ){
        alert('图片格式无效！');
        return false;
    }
    
    var newimgid = window.parent.creatDiv();
    if( obj.files &&  obj.files[0] )
    {
      imgpath = obj.files[0].getAsDataURL();
      window.parent.document.getElementById(newimgid).innerHTML = "<img src='" + imgpath + "' onload='{$vd['fun']}(this)'/>";
    } 
    else if(!window.XMLHttpRequest) 
    {
      imgpath = obj.value;
      window.parent.document.getElementById(newimgid).innerHTML = "<img src='" + imgpath + "' onload='{$vd['fun']}(this)'/>";
    } 
    else 
    {
      imgpath = obj.value;
      window.parent.document.getElementById(newimgid).innerHTML = "<img src='" + imgpath + "' onload='{$vd['fun']}(this)' style='display:none'/>";
      
      imgpath = getValueIE8(obj);
      
      objPreviewSizeFake = document.getElementById('preview_size_fake' );
      window.parent.document.getElementById(newimgid).filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src  = imgpath;
      objPreviewSizeFake.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = imgpath;

      autoSizePreview( window.parent.document.getElementById(newimgid),40, 40 );
      picobj = window.parent.document.getElementById("pic");
      try{
          picobj.innerHTML = "";
      }catch(e){}
      picobj.className = "ctablePic preview_fake";
      picobj.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src  = imgpath;
      //objPreviewSizeFake.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = imgpath;

      //autoSizePreview( window.parent.document.getElementById("pic"),objPreviewSizeFake.offsetWidth, objPreviewSizeFake.offsetHeight );
    }
    //window.parent.document.getElementById(newimgid).innerHTML = "<img src='" + imgpath + "' style='width:40px' onload='{$vd['fun']}(this)'/>";
    SetAction();
    document.getElementById("frmUpload").submit();
  }
  
  function getValueIE8(obj)
  {
    obj.select();
    return document.selection.createRange().text;
  }
  
  function autoSizePreview( objPre, originalWidth, originalHeight )
  {
    var zoom = clacImgZoomParam(250,250,originalWidth,originalHeight);
    objPre.style.width = zoom.width + 'px';
    objPre.style.height = zoom.height + 'px';
    objPre.style.marginTop = 0 + 'px';
    objPre.style.marginLeft = 5 + 'px';
  }

  function clacImgZoomParam( maxWidth, maxHeight, imgWidth, imgHeight )
  {
    var param = { width:imgWidth, height:imgHeight,top:0,left:0};

    if(imgHeight < imgWidth)
    {
      if(imgWidth > maxWidth)
      {
        param.width = maxWidth + "px";
      }
    }
    else
    {
      if(imgHeight > maxHeight)
      {
        param.height = maxHeight + "px";
      }
    }

    param.left = (maxWidth - param.width) / 2;
    param.top = (maxHeight - param.height) / 2;

    return param;
  }
  
  function SetFile(fname)
  {
    //get the name
    //document.getElementById("uploadresult").innerHTML = "上传成功!路径：" + fname;
    var temp = fname.split('/');
    var oldval = window.parent.document.getElementById("{$vd['inputid']}").value;
    if(oldval != "")
    {
      oldval = oldval + ",";
    }
    window.parent.document.getElementById("{$vd['inputid']}").value = oldval + temp[temp.length-1];
  }
</script>
<form id="frmUpload" action="" target="eRunningFrame" method="post" enctype="multipart/form-data">
  <input id="txtFolder" type="hidden" value="{$vd['dirname']}" name="txtFolder" />
  <input id="cmbType"   type="hidden" value="Image" name="cmbType" />
  <div class="input2">
  <input id="txtFileUpload" type="file" name="NewFile" onchange="SetAction();ShowImg(this, {$vd['imgid']});" class="file"/>
  </div>
</form>
<img id="preview_size_fake"/>
<span id="eUrl" style="display:none"></span>
<iframe id="eRunningFrame" src="javascript:void(0)" name="eRunningFrame" width="100%" height="100%" style="display:none"></iframe>
<span id="eUrl" style="display:none"></span>
<input type="hidden" id="cmbConnector" name="cmbConnector" value="{$vd['connecter']}" />
<script type="text/javascript">
  if (window.XMLHttpRequest) 
  {
    if(!window.ActiveXObject)
    {
      document.getElementById("txtFileUpload").style.marginLeft = "-95px";
    } 
  } 
</script>
</body>
</html>
