/* localUpload object  by 木鸟  www.iwms.net */

function LocalUpload(actionUrl, maxSize, types, userID, maxNum)
{
	this.installed = false;
	this.activex = null;
	this.types = types;
	this.files = new Array();
	
	try {
		this.activex = new ActiveXObject("IWMSComponent.LocalUp");
		this.activex.ActionUrl = actionUrl;
		this.activex.MaxSize = maxSize;
		this.activex.Types = types;
		this.activex.UserID = userID;
		this.activex.MaxNum = maxNum;
		this.installed = true;
	}catch(e){
//		alert(e.message+"没安装localup插件");
	}
	
	if (!this.installed){
		var ignore = this.getCookie("ignore_lu")=="y";
		if (!ignore){
			window.setTimeout("LocalUploadDown();",3000);
		}
	}
	
	if (this.installed){
		var tmp = this.getCookie("newsMember");
		if (tmp.length>0){
			var tar = tmp.split("&");
			for (var j=0; j<tar.length; j++){
				var ttar = tar[j].split("=");
				if (ttar[0] == "password"){
					this.activex.UserPass = ttar[1];
				}
			}
		}
	}
}

LocalUpload.prototype.upload = function(filepath)
{
	var re = /^((file\:\/\/\/)?\w:.+?\.\w+)/ig;
	
	if (!this.installed || filepath.indexOf(".")<0 || !re.test(filepath)){
		return false;
	}
	var ext = filepath.substr(filepath.lastIndexOf(".")+1);
	re = new RegExp("(^|,)"+ ext +"(,|$)","ig");
//	alert(re.source+"   "+ this.types );
	
	if (!re.test(this.types)){
//		alert("扩展名错误");
		return false;
	}
	
	this.activex.FilePath = filepath;
	var file = this.activex.Upload();
	if (file.length>0 && file.search(/\.\w+$/)>0){
		this.files.push(file);
		return true;
	}else{
		return false;
	}
}

LocalUpload.prototype.htmlUpload = function(html)
{
	if (!this.installed || html.length==0){
		return html;
	}
	var arr;
	var str = html;
	var re = /<img[^>]*?\ssrc=(['"]?)((file\:\/\/\/)?\w:.+?)\1([ >]|\/>)/ig;
	while ( (arr=re.exec(html)) != null){
		var url = arr[2];
//		alert(url);
		if (this.upload(url)){
			var re2 = new RegExp(url.replace(/\\/g,"\\\\").replace(/\./g,"\\."),"ig");
			str = str.replace(re2, this.files[this.files.length-1]);
		}
	}
	return str;
}

LocalUpload.prototype.getCookie = function (Name)			//cookies读取
{
	var search = Name + "="
	if(document.cookie.length > 0) 
	{
		offset = document.cookie.indexOf(search)
		if(offset != -1) 
		{
			offset += search.length
			end = document.cookie.indexOf(";", offset)
			if(end == -1) end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
		 }else
		 	return "";
	 }
}

function setCookie(name, value, expires)		//cookies设置
{
	var LargeExpDate = new Date ();
	LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
	document.cookie = name + "=" + escape (value) + "; path=/;" +((expires == null) ? "" : (" expires=" +LargeExpDate.toGMTString()));
}


function LocalUploadDown()
{
//	if (!confirm("你没有安装iwms自动上传插件，请下载安装。不想再看到该提示点击取消")){
		alert("你没有安装iwms自动上传插件，请下载安装。");
		setCookie("ignore_lu","y",31);
//	}
	var ifm = document.createElement("iframe");
	ifm.src = "inc/iwmsAx.exe";
	ifm.height = 0;
	ifm.width = 0;
	ifm.style.visibility = "hidden";
	document.body.appendChild(ifm);
}
 


