function CheckAll(form)  {
  for (var i=0;i<form.elements.length;i++)    {
    var e = form.elements[i];
    if (e.name != 'chkall')       e.checked = form.chkall.checked; 
   }
}
function initimg(parpic,maxwidth,maxheight)
{
	var scale=maxwidth/maxheight;
	var realscale=parpic.width/parpic.height;
	if((parpic.width>maxwidth)||(parpic.height>maxheight))
	{
		if(realscale>scale)
		{
			parpic.width=maxwidth;
		}
		else
		{
			parpic.height=maxheight;
		}
	}
}
function CheckItems(itemName,checked)
{
	var c = document.getElementsByName(itemName);
    for (var i=0; i<c.length; i++)
	{
		if (document.getElementsByName(itemName)[0].checked == true)
		{
			c[i].checked = true;
		}else{
			c[i].checked = false;
		}
		
	}
}
function request() {
	var A=null;
	try {
		A=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {
		try {
			A=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc) {
			A=null;
		}
	}
	if ( !A && typeof XMLHttpRequest != "undefined" ){
		A=new XMLHttpRequest();
	}
	return A;
}

function Trim(str) 
{ 
	var str;
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
} 

function LTrim(str) 
{ 
	var str;
	return str.replace(/(^\s*)/g, ""); 
} 

function RTrim(str) 
{ 
	var str;
	return str.replace(/(\s*$)/g, ""); 
}
var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
function generateMixed(n) {
    var res = "";
    for(var i = 0; i < n ; i ++) {
        var id = Math.ceil(Math.random()*35);
        res += chars[id];
    }
    return res;
}


var maxWidth=232;
var maxHeight=147;
var fileTypes=["jpg","gif","png"];
var outImage="preview";
var defaultPic="/html/img/nopic.gif";
var globalPic;
function preview(what, num){
	var source=what.value;
	var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
	for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
	globalPic=new Image();
	if (i<fileTypes.length) globalPic.src=source;
	else {
		globalPic.src=defaultPic;
		what.outerHTML = what.outerHTML.replace(/value=\w/g,"");
		alert("文件类型错误，允许的图片文件后缀是: "+fileTypes.join(", "));
	}
	setTimeout("applyChanges("+num+")",200);
}

function applyChanges(num){
	var field=document.getElementById(outImage + num);
	var x=parseInt(globalPic.width);
	var y=parseInt(globalPic.height);
	if (x>maxWidth) {
		y*=maxWidth/x;
		x=maxWidth;
	}
	if (y>maxHeight) {
		x*=maxHeight/y;
		y=maxHeight;
	}
	field.style.display=(x<1 || y<1)?"none":"";
	field.src=globalPic.src;
	field.width=x;
	field.height=y;
}
