﻿function PreviewImg(imgFileUrl,width)
{  
    var newPreview = document.getElementById("newPreview");   
    newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFileUrl;  
    
    var image=new Image(); 
    image.src=imgFileUrl; 
    if (width > 0)
    {
        if(image.width > 0 && image.height > 0){ 
             if(image.width >= width){ 
                newPreview.style.width = width; 
                newPreview.style.height = (image.height * width)/image.width; 
             }else{ 
                newPreview.style.width = image.width; 
                newPreview.style.height = image.height; 
            }  
        }
    }
    else
    {
        newPreview.style.width=image.width; 
        newPreview.style.height =image.height; 
    }   
    
     //img.fileSize //取得文件长度   
}   




function ChangeValidateCodeImg()
{
	document.getElementById("imgValidateCode").src = "ValidateCode.aspx?"+Math.random();
}

function ShowDateTimeNow()
{
    tmpDate = new Date();
    date = tmpDate.getDate();
    month= tmpDate.getMonth() + 1 ;
    year= tmpDate.getYear();
    document.write(year);
    document.write("年");
    document.write(month);
    document.write("月");
    document.write(date);
    document.write("日 ");

    myArray=new Array(6);
    myArray[0]="星期日"
    myArray[1]="星期一"
    myArray[2]="星期二"
    myArray[3]="星期三"
    myArray[4]="星期四"
    myArray[5]="星期五"
    myArray[6]="星期六"
    weekday=tmpDate.getDay();
    if (weekday==0 | weekday==6)
    {
    document.write(myArray[weekday])
    }
    else
    {document.write(myArray[weekday])
    };
}

function OpenWindow(url, width, height)
{
	var chasm = screen.availWidth;
	var mount = screen.availHeight;
	var w = width;
	var h = height;
	window.open(url,'SaveToAlbum','width=' + w + ',height=' + h + ',left=' + ((chasm - w - 10) * .5) + ',top=' + ((mount - h - 30) * .5) + ',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=no');
}

function  WindowToCenter()  
{  
   var  top=((window.screen.availHeight-document.body.clientHeight)/2);  
   var  left=((window.screen.availWidth-document.body.clientWidth)/2);  
 
   window.moveTo(left,  top);  
}  

function FTB_LengthCheck(source, arguments)
{
	if (arguments.Value.length > 1073741823 )
		arguments.IsValid = false;
	else
		arguments.IsValid = true;
} 

function FTB_PreviewImage(theDiv,filepath,filename,width,height,size)
 {
    if ( document.getElementById('command_DeleteImageButton') != null)
        document.getElementById('command_DeleteImageButton').disabled = false;
 
	image = document.getElementById('img_preview');
	image.src = filepath + "/" + filename;
	image.width = width;
	image.height = height;	
	
	document.getElementById('img_width').value = width;
	document.getElementById('img_height').value = height;	
	
	document.getElementById('img_feedback_message').value = filename + " (" + size + " byte)";	
}

function FTB_DimensionChange(sender)
{
	switch (sender.id) {
		default:
		case "img_dim_original": 
			document.getElementById('img_width_custom').value = '';
			document.getElementById('img_height_custom').value = '';
			document.getElementById('img_percentage').value = '';
			
			document.getElementById('img_width_custom').disabled = true;
			document.getElementById('img_height_custom').disabled = true;
			document.getElementById('img_percentage').disabled = true;
			
			FTB_ResetImage();
			break;
		case "img_dim_custom":
			document.getElementById('img_width_custom').value = document.getElementById('img_width').value;
			document.getElementById('img_height_custom').value = document.getElementById('img_height').value;
			document.getElementById('img_percentage').value = '';
			
			document.getElementById('img_width_custom').disabled = false;
			document.getElementById('img_height_custom').disabled = false;
			document.getElementById('img_percentage').disabled = true;			
			break;	
		case "img_dim_percentage":
			document.getElementById('img_width_custom').value = '';
			document.getElementById('img_height_custom').value = '';
			document.getElementById('img_percentage').value = '100';
			
			document.getElementById('img_width_custom').disabled = true;
			document.getElementById('img_height_custom').disabled = true;
			document.getElementById('img_percentage').disabled = false;	
			FTB_SetImageByPercentage();		
			break;	
	}
}

function FTB_ResetImage()
 {
	previewImage = document.getElementById('img_preview');
	width = document.getElementById('img_width').value;
	height = document.getElementById('img_height').value;

	previewImage.width = width;
	previewImage.height = height;
}

function FTB_SetImageByPercentage()
 {
	previewImage = document.getElementById('img_preview');
	width = document.getElementById('img_width').value;
	height = document.getElementById('img_height').value;
	percentage = document.getElementById('img_percentage').value;

	previewImage.width = width * percentage / 100;
	previewImage.height = height * percentage / 100;	
}

function FTB_UpdatePreview(sender) {
	src = document.getElementById('img_feedback_message').value;
	if (src == null || src == '') return;
	
	previewImage = document.getElementById('img_preview');
	width = document.getElementById('img_width').value;
	height = document.getElementById('img_height').value;
	customWidth = document.getElementById('img_width_custom').value;
	customHeight = document.getElementById('img_height_custom').value;
	lockRatio = document.getElementById('img_lockRatio').checked;
	
	if (sender.id == 'img_percentage') {
		FTB_SetImageByPercentage();
	} else {
	
		if (lockRatio) {
			if (sender.id == 'img_width_custom') {			
				previewImage.width = customWidth;
				previewImage.height = height * ( customWidth / width);
				document.getElementById('img_height_custom').value = height * ( customWidth / width);
			} else if (sender.id == 'img_height_custom') {
				previewImage.width = width * ( customHeight / height);		
				previewImage.height = customHeight;
				
				document.getElementById('img_width_custom').value = width * ( customHeight / height);
			}
		} else {
			previewImage.width = customWidth;
			previewImage.height = customHeight;
		}
		
	}
}

function FTB_InsertImage() {
	
	image = document.getElementById('img_preview');
	src = document.getElementById('img_preview').src;
	if (src == '' || src == null) return;
	
	alt = document.getElementById('img_alt').value;
	title = document.getElementById('img_title').value;
	width = image.width; 
	height = image.height; 
	align = document.getElementById('img_align').options[document.getElementById('img_align').selectedIndex].value;
	
	hspace = document.getElementById('img_hspace').value;
	vspace = document.getElementById('img_vspace').value;
	border = document.getElementById('img_border').value;
	
	ftb = document.getElementById('TargetFreeTextBox').value;

	img = '<img src="' + src + '"' + 
		( (alt != '') ? ' alt="' + alt + '"' : '' ) + 
		( (title != '') ? ' title="' + title + '"' : '' ) + 
		( (width != '') ? ' width="' + width + '"' : '' ) + 
		( (height != '') ? ' height="' + height + '"' : '' ) + 
		( (height != '') ? ' height="' + height + '"' : '' ) + 
		( (align != '') ? ' align="' + align + '"' : '' ) + 
		( (hspace != '') ? ' hspace="' + hspace + '"' : '' ) + 
		( (vspace != '') ? ' vspace="' + vspace + '"' : '' ) + 
		( (border != '') ? ' border="' + border + '"' : '' ) + 
		' />';
		
	window.opener.FTB_API[ftb].InsertHtml(img);
}


//编辑下载时验证
function CheckUploadEidt(tbxFlag,tbxFilePath,upload,allowFileType,tbxTitle,tbxPublishDateTime,tbxEndDateTime,dplParentName,tbxSortIndex)
{
    
    var result="";
    var reg1=/^\d+$/;
    for(var i=0;i<dplParentName.options.length;i++)
    {
        if(dplParentName.options[i].selected == true)
        {
            if(!reg1.test(dplParentName.options[i].value))
            {
                result=" - 必须选择所属结构。\r\n";
               break;
            }
        }
    }

    if(tbxTitle.value=="")
    {
            result+=" - 标题不能够为空。\r\n";
    } 
    
    
    var reg=/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
    if(!reg.test(tbxPublishDateTime.value))
    {
        result+=" - 下载发布开始时间输入错误。\r\n";
    }
     if(!reg.test(tbxEndDateTime.value))
    {
         result+=" - 下载发布结束时间输入错误。\r\n";
    }
    
    if(tbxSortIndex.value !="")
    {
        var regSortIndex=/^\d+$/;
        if(regSortIndex.test(tbxSortIndex.value))
          result+=" - 下载显示序号输入错误。\r\n";
    }
    
    if(tbxFlag.value!='1')
    {
       if(upload.value!="")     
       {            
            result+=UploadFileValidate(upload,allowFileType);            
       }
    }
    if(result!="")
    {
    
        alert(result);
        event.returnValue=false;
        return;
    }
}


function CheckUpload(tbxFlag,tbxFilePath,upload,allowFileType,tbxTitle,tbxPublishDateTime,tbxEndDateTime,dplParentName)
{   
    var result="";
    var reg1=/^\d+$/;
    for(var i=0;i<dplParentName.options.length;i++)
    {
        if(dplParentName.options[i].selected == true)
        {
            if(!reg1.test(dplParentName.options[i].value))
            {
                result=" - 必须选择所属结构。\r\n";
               break;
            }
        }
    }

    if(tbxTitle.value=="")
    {
            result+=" - 标题不能够为空。\r\n";
    } 
    
    
    var reg=/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
    if(!reg.test(tbxPublishDateTime.value))
    {
        result+=" - 下载发布开始时间输入错误。\r\n";
    }
     if(!reg.test(tbxEndDateTime.value))
    {
         result+=" - 下载发布结束时间输入错误。\r\n";
    }

    if(tbxFlag.value=='1')
    {
       if(tbxFilePath.value=='')
       {
                result+=" - 请选择一个FTP文件。\r\n";
       }        
    }
    else
   {
       if(upload.value=='')
       {
              result+=" - 请上传一个文件。\r\n";

       }
      else
      
       {            
            result+=UploadFileValidate(upload,allowFileType);            
       }
    }
    if(result!="")
    {
    
        alert(result);
        event.returnValue=false;
        return;
    }
}


//验证友情链接表单
function CheckFriendLink(tbxTitle,tbxUrlLink,upload,allowFileType,imgSize,maxlength,flag)
{
    var result="";
    if(tbxTitle.value=="")
    {
            result+=" - 标题不能够为空。\r\n";
    } 
    if(tbxUrlLink.value=="")
    {
            result+=" - 链接地址不能够为空。\r\n";
    } 
   
    if(flag=="Edit")   //编辑状态
    {
        if(upload.value!="")
        {
             result+=UploadImageValidate(upload.value,imgSize,maxlength);
             result+=UploadFileValidate(upload,allowFileType);  
        }
    }
    else
    {
        if(upload.value!="")
        {
             result+=UploadImageValidate(upload.value,imgSize,maxlength);
             result+=UploadFileValidate(upload,allowFileType);  
        }
//        else  
//        {
//            result+=" - 必须上传一个图片。\r\n";
//        }     
     }               
       
    if(result!="")
    {
    
        alert(result);
        event.returnValue=false;
        return;
    }
}
//演示课件
function CheckCoursewareDemoImg(tbxTitle,tbxUrlLink,upload,allowFileType,imgSize,maxlength,flag)
{
    var result="";
    if(tbxTitle.value=="")
    {
            result+=" - 演示课件名称不能够为空。\r\n";
    } 
    if(tbxUrlLink.value=="")
    {
            result+=" - 演示课件地址不能够为空。\r\n";
    } 
   
    if(flag=="Edit")   //编辑状态
    {
        if(upload.value!="")
        {
             result+=UploadImageValidate(upload.value,imgSize,maxlength);
             result+=UploadFileValidate(upload,allowFileType);  
        }
    }
    else
    {
        if(upload.value!="")
        {
             result+=UploadImageValidate(upload.value,imgSize,maxlength);
             result+=UploadFileValidate(upload,allowFileType);  
        }
     }               
       
    if(result!="")
    {
    
        alert(result);
        event.returnValue=false;
        return;
    }
}

//验证Flash新闻表单
function CheckFlashArticle(tbxTitle,tbxArticleUrl,upload,tbxPublishDateTime,tbxEndDateTime,allowFileType,imgSize,maxlength,flag)
{
    var result="";
    if(tbxTitle.value=="")
    {
            result+=" - 新闻标题不能够为空。\r\n";
    } 
    if(tbxArticleUrl.value=="")
    {
            result+=" - 新闻 Url 地址不能够为空。\r\n";
    } 
     var reg=/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
    if(!reg.test(tbxPublishDateTime.value))
    {
        result+=" - Flash新闻发布开始时间输入错误。\r\n";
    }
     if(!reg.test(tbxEndDateTime.value))
    {
         result+=" - Flash新闻发布结束时间输入错误。\r\n";
    }
    if(flag=="Edit")   //编辑状态
    {
        if(upload.value!="")
        {
             result+=UploadImageValidate(upload.value,imgSize,maxlength);
             result+=UploadFileValidate(upload,allowFileType);  
        }
    }
    else
    {
        if(upload.value!="")
        {
             result+=UploadImageValidate(upload.value,imgSize,maxlength);
             result+=UploadFileValidate(upload,allowFileType);  
        }
        else  
        {
            result+=" - 必须上传一个图片。\r\n";
        }     
     }               
       
    if(result!="")
    {
    
        alert(result);
        event.returnValue=false;
        return;
    }
}



//验证图片长度和大小 
//imgFileUrl:文件路径，
//imgSize文件大小，“*”分隔。
//maxlength：允许大小。
//返回错误信息。
function UploadImageValidate(imgFileUrl,imgSize,maxlength)
{      
    var image=new Image();
    image.src = imgFileUrl;   
    var allows=imgSize.split("*");
    var allowwidth=allows[0];
    var allowheight=allows[1];
    var result="";
    if(image.width > 0 && image.height > 0)
    { 
         if(image.width > allowwidth || image.height>allowheight)
         { 
            result+=" - 系统只允许上传小于等于 "+allowwidth+" X "+allowheight+" 的文件。\r\n";
         } 
          if(parseInt(image.fileSize)>parseInt(maxlength))
        {
            result+=" - 上传图片的长度"+image.fileSize+"超出了系统允许的最大范围，系统只允许上传 "+maxlength+" Byte 的文件。\r\n";
        } 
    }
    else {
        result = " - 上传文件不是图片格式或路径有误。\r\n" + image.src;
    }
    return result;
    
     //img.fileSize //取得文件长度   
} 


//验证文件类型，upload:文件路径，allowFileType：允许类型，返回错误信息。
function UploadFileValidate(upload,allowFileType)
{
    var rv = "";
   
            var index=upload.value.lastIndexOf(".");
            var extension=upload.value.substr(index).toLowerCase();
            var fileTypes = allowFileType.toLowerCase().split(",");
            var flag = false;
            for(var i = 0; i < fileTypes.length; i++)
            {
                if (fileTypes[i]==extension)
                {
                    flag = true;
                    break;
                }
             }
            if(false == flag ) 
            {
                rv=" - 系统只允许上传 " + allowFileType + " 类型的文件。\r\n";
            }
            return rv;
}　　 

function CheckSelect(idVal)
{
     var chkList = document.getElementsByTagName("input");
      var flag=false;
    if( chkList != null)
    {
        for (i=0; i<chkList.length; i++) 
        {
            if( chkList[i].type.toLowerCase() == "checkbox" && chkList[i].id.indexOf ( idVal ) != -1 && chkList[i].disabled == false )
            {
                if( chkList[i].checked )
                {
                    flag=true;
                    break;
                }
            }
        }
    }
    if(flag==false)
    {
        alert("必须至少选择一条数据！");
        event.returnValue=false;
        return;
    }
}
////键盘只能输入数字
//function KeyPress(objTR) {
//    var txtval = objTR.value;

//    var key = event.keyCode;

//    if ((key < 48 || key > 57) || (key == 45 || key == 46)) {

//        event.keyCode = 0;

//    }


//}

function CheckMaterialImg(tbxTitle,tbxUnits,tbxStockAmount, upload, allowFileType, imgSize, maxlength, flag) {
    var result = "";
    if (tbxTitle.value == "") {
        result += " - 资料名称不能够为空。\r\n";
    }
    if (tbxStockAmount.value == "") {
        result += " - 资料库存不能够为空。\r\n";
    }
    if (tbxUnits.value == "") {
        result += " - 资料计量单位不能够为空。\r\n";
    }
   

    if (result != "") {

        alert(result);
        event.returnValue = false;
        return;
    }
}

//键盘只能输入数字,不能输入0
function KeyPress(objTR) {
    var txtval = objTR.value;

    var key = event.keyCode;
    if (parseInt(txtval)==0) {
       
        if ((key < 49 || key > 57) || (key == 45 || key == 46)) {

            event.keyCode = 0;

        }
    }
    else {
        if ((key < 48 || key > 57) || (key == 45 || key == 46)) {           
            event.keyCode = 0;
            
        }
       
    }

}

//结合KeyPress使用，让用户只能输入正整数。
function KeyUp(objTR) {
    
    if (objTR.value == '') {
        objTR.value = '';
    }
}
