// 获取元素id
function $id(str){
	return document.getElementById(str);
}

// 获取元素name
function $name(str){
	return document.getElementsByName(str);
}


// 文字水印参数，默认关闭 isWatermark=font&watermarkPos=centerMiddle&watermarkPadding=6&watermarkFontContent=网钛科技&watermarkFontSize=14&watermarkFontColor=black
// 图片水印参数，默认关闭 isWatermark=img&watermarkPath=upFile/water.gif&watermarkPos=centerMiddle&watermarkPadding=6
// 其他参数 otherPara
// 区域模式 areaMode=
// 上传模式 upMode：more批量上传，其他单文件上传
// 打开上传载入图片窗口
function OT_OpenUpImg(fileMode,fileFormName,fileDir,otherPara){
	var arr = window.open("info_upImg.asp?fileMode="+ fileMode +"&fileFormName="+ fileFormName +"&upPath="+ fileDir +"&upFileType=images"+ otherPara,"","top=250,left=340,width=600,height=300,menubar=no,scrollbars=yes,status=no,resizable=yes");
//	if (arr != null){
//		OT_InsertImg(arr);
//	}
}

// 打开上传载入图片窗口
function OT_OpenUpFile(fileMode,fileFormName,fileDir,otherPara){
	var arr = window.open("info_upFile.asp?fileMode="+ fileMode +"&fileFormName="+ fileFormName +"&upPath="+ fileDir + otherPara,"","top=250,left=340,width=600,height=300,menubar=no,scrollbars=yes,status=no,resizable=yes");
//	if (arr != null){
//		OT_InsertImg(arr);
//	}
}

// 打开上传大文件窗口
function OT_OpenUpBigFile(fileMode,fileFormName,fileDir,upMode,otherPara){
	var arr = window.open("info_upBigFile.asp?fileMode="+ fileMode +"&fileFormName="+ fileFormName +"&upPath="+ fileDir +"&upMode="+ upMode + otherPara,"","top=250,left=340,width=660,height=300,menubar=no,scrollbars=yes,status=no,resizable=yes");
//	if (arr != null){
//		OT_InsertImg(arr);
//	}
}

// 打开颜色框
function OT_OpenColor(retMode,inputName){
	var arr = showModalDialog('info_setColor.htm','','dialogWidth:300px; dialogHeight:260px; status:0; help:0');
	if (arr != null){
		if (retMode=="input"){
			document.getElementById(inputName).value = arr;
			document.getElementById(inputName).style.color = arr;
		}else if (retMode=="function"){
			DealColorBox(arr);
		}
	}
	
}
// if(isNaN(value))execCommand('undo');

// 过滤特殊符号
// 应用例子 onkeyup="if (this.value!=FiltVarStr(this.value)){this.value=FiltVarStr(this.value)}"
// 应用例子 onkeyup="this.value=FiltVarStr(this.value)"
function FiltVarStr(str){
	return str.replace(/[^\w\u4E00-\u9FA5]/g,'')
}

// 过滤小数
// 应用例子 onkeyup="if (this.value!=FiltDecimal(this.value)){this.value=FiltDecimal(this.value)}"
// 应用例子 onkeyup="this.value=FiltDecimal(this.value)"
function FiltDecimal(str){
	return str.replace(/[^\d*\.?\d{0,2}$]/g,'')
}

// 过滤整数
// 应用例子 onkeyup="if (this.value!=FiltInt(this.value)){this.value=FiltInt(this.value)}"
// 应用例子 onkeyup="this.value=FiltInt(this.value)"
function FiltInt(str){
	return str.replace(/\D/g,'')
}

// 过滤整数
// 应用例子 onkeyup="if (this.value!=FiltABCNum(this.value)){this.value=FiltABCNum(this.value)}"
// 应用例子 onkeyup="this.value=FiltABCNum(this.value)"
function FiltABCNum(str){
	return str.replace(/[^\w]/ig,'')
}

// 过滤非数字、字母、下划线
// 应用例子 onkeyup="if (this.value!=FiltAbcNum_(this.value)){this.value=FiltAbcNum_(this.value)}"
// 应用例子 onkeyup="this.value=FiltAbcNum_(this.value)"
function FiltAbcNum_(str){
	return str.replace(/[^A-Za-z0-9_]/ig,'')
}

// 过滤数字
// 应用例子 onkeyup="if (this.value!=FiltNumber(this.value)){this.value=FiltNumber(this.value)}"
// 应用例子 onkeyup="this.value=FiltNumber(this.value)"
function FiltNumber(str) {
	var newStr="";
	if(str.substring(0,1)=="-"){ newStr="-"; }
	var newStr2=(str.replace(/[^0-9.]/g,'')).replace(/[.][0-9]*[.]/, '.');

	if (newStr2.substring(0,1)=="."){ newStr2="0"+ newStr2; }
	newStr = newStr + newStr2
	return newStr;
	/*
	if (/^(\+|-)?\d+($|\.\d+$)/.test( str )) {
	}else {
		execCommand('undo');
	}
	*/
} 

// 把Option的text值覆盖toID文本框
// 应用例子 onchange="OptionTextTo('labItemID','labItemName');"
function OptionTextTo(sourceID,toID){
	document.getElementById(toID).value=document.getElementById(sourceID).options[document.getElementById(sourceID).selectedIndex].text;
}


// 生成随机数
// num：生成个数
function RndNumber(num) {
   var a = new Array("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", "A", "B", "C", "D", "E", "F", "G", "H", "I", "Z", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
   var b = "", c;
   for(i=1; i<=num; i++){
      c = Math.floor(Math.random() * a.length);
      b = b + a[c];
//      a = a.del(c);
   }
   return b;
}

function RndNumberSort(num) {
   var a = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
   var b = "", c;
   for(i=1; i<=num; i++){
      c = Math.floor(Math.random() * a.length);
      b = b + a[c];
//      a = a.del(c);
   }
   return b;
}



// 使用AJAX异步无刷新
function UseAjax(urlStr){
	var ajax=new AJAXRequest();
	ajax.get(
		// 请求的地址
		urlStr,
		// 回调函数，注意，是回调函数名，不要带括号
		function(obj) { eval(obj.responseText); }
	);
}


// 复制内容
function StrToCopy(copy){
	if (window.clipboardData){
		window.clipboardData.setData("Text", copy);
	}else if (window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=copy;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	alert("复制成功")
	return false;
}


// 复制内容(获取ID所在的value)
function ValueToCopy(id){
	copy = $id(id).value
	if (window.clipboardData){
		window.clipboardData.setData("Text", copy);
	}else if (window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=copy;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	alert("复制成功")
	return false;
}



// 数组变量获取下拉框全部选项
function SelectOptionArr(selectName){
	var SelectOptionArray = new Array();

	for (soi=0; soi<document.getElementById(selectName).options.length; soi++){
		SelectOptionArray[document.getElementById(selectName).options[soi].value] = document.getElementById(selectName).options[soi].text;
	}
	return SelectOptionArray;
}

// 下拉框内容检索
function SetOptionData(selectName,arrObj){
	document.getElementById(selectName).options.length=0;
	for (var key in arrObj){
		document.getElementById(selectName).options.add(new Option(arrObj[key],key));
	}
}

// 下拉框内容检索
function SelectOptionSearch(sourceID,selectName,arrObj){
	document.getElementById(selectName).options.length=0;
	for (var key in arrObj){
		if (arrObj[key].lastIndexOf(document.getElementById(sourceID).value)>=0){
			document.getElementById(selectName).options.add(new Option(arrObj[key],key));
		}
	}
}

// 清理下拉框内容
function SelectOptionClear(selectName,defText){
	document.getElementById(selectName).options.length=0; 
	document.getElementById(selectName).options.add(new Option(defText,""));
	document.getElementById(selectName).value = "";
}



// 获取编辑器中HTML内容
function GetEditorHTML(EditorName) {
	var editor = CKEDITOR.instances[EditorName];
	return(editor.document.getBody().getHtml());
}

// 获取编辑器中文字内容
function GetEditorText(EditorName) {
	var editor = CKEDITOR.instances[EditorName];
	return(editor.document.getBody().getText());
}

// 设置编辑器中内容
function SetEditorHtml(EditorName, ContentStr) {
	var editor = CKEDITOR.instances[EditorName];
	editor.setData(ContentStr);
}

// 插入字符串到编辑器中
function InsertStrToEditor(EditorName, ContentStr) {
	var editor = CKEDITOR.instances[EditorName];
	editor.insertHtml(ContentStr);
}


function FocusAddText(inputId,str){
	var ubb=document.getElementById(inputId);
	var ubbLength=ubb.value.length;
	ubb.focus();
	if(typeof document.selection !="undefined"){
		document.selection.createRange().text=str;  
	}else{
		ubb.value=ubb.value.substr(0,ubb.selectionStart)+str+ubb.value.substring(ubb.selectionStart,ubbLength);
	}
}

function InputAddText(inputId,str){
	document.getElementById(inputId).value=document.getElementById(inputId).value+str;
}




// date 可以是时间对象也可以是字符串，如果是后者，形式必须为: yyyy-mm-dd hh:mm:ss 其中分隔符不定。"2006年12月29日 16点01分23秒" 也是合法的
function DateAdd(interval,number,date){
	number = parseInt(number);
	if (typeof(date)=="string"){
	date = date.split(/\D/);
	--date[1];
	eval("var date = new Date("+date.join(",")+")");
	}
	if (typeof(date)=="object"){
	var date = date
	}
	switch(interval){
	case "y": date.setFullYear(date.getFullYear()+number); break;
	case "m": date.setMonth(date.getMonth()+number); break;
	case "d": date.setDate(date.getDate()+number); break;
	case "w": date.setDate(date.getDate()+7*number); break;
	case "h": date.setHours(date.getHour()+number); break;
	case "n": date.setMinutes(date.getMinutes()+number); break;
	case "s": date.setSeconds(date.getSeconds()+number); break;
	case "l": date.setMilliseconds(date.getMilliseconds()+number); break;
	} 
	return date.getFullYear() +"-"+ (date.getMonth()+1) +"-"+ date.getDate();
}
