// 获取元素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:350px; dialogHeight:300px; 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(/[^A-Za-z0-9]/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!=FiltAbcNum_d(this.value)){this.value=FiltAbcNum_d(this.value)}"
// 应用例子 onkeyup="this.value=FiltAbcNum_d(this.value)"
function FiltAbcNum_d(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');
	}
	*/
}

// 过滤掉HTML标签和空格、换行、制表符
function FiltHtmlTag(str) {
	str = str.replace(/<\/?[^>]*>/g,'');			// 去除HTML tag
	str = str.replace(/(\t|\r|\n| |\&nbsp;)/g,'');	// 去除空格、换行、制表符
	return str;
}

// 把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){
	$.ajaxSetup({cache:false});
	$.get(urlStr, function(result){
		eval(result);
	});
}


// 复制内容
function StrToCopy(copy){
	if (window.clipboardData){
		window.clipboardData.setData("Text", copy);
	}else if(navigator.userAgent.indexOf("Opera") != -1){
		window.location = copy;
	}else if(window.netscape){
		try {
			netscape.security.PrivilegeManager
					.enablePrivilege("UniversalXPConnect");
		}catch (e){
			alert("你使用的FireFox浏览器,复制功能被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车。\n然后将“signed.applets.codebase_principal_support”双击，设置为“true”");
			return;
		}
		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);
		str.data = copy;
		trans.setTransferData("text/unicode", str, copy.length * 2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans, null, clipid.kGlobalClipboard);
	}else{
		alert("你的浏览器不支持一键复制功能");
		return;
	}
	alert("复制成功")
	return false;
}


// 复制内容(获取ID所在的value)
function ValueToCopy(id){
	copy = $id(id).value
	if (window.clipboardData){
		window.clipboardData.setData("Text", copy);
	}else if(navigator.userAgent.indexOf("Opera") != -1){
		window.location = copy;
	}else if(window.netscape){
		try {
			netscape.security.PrivilegeManager
					.enablePrivilege("UniversalXPConnect");
		}catch (e){
			alert("你使用的FireFox浏览器,复制功能被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车。\n然后将“signed.applets.codebase_principal_support”双击，设置为“true”");
			return;
		}
		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);
		str.data = copy;
		trans.setTransferData("text/unicode", str, copy.length * 2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans, null, clipid.kGlobalClipboard);
	}else{
		alert("你的浏览器不支持一键复制功能");
		return;
	}
	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();
}



	ajaxDealStr = "数据处理中...";
	ajaxLoadStr = "数据读取中...";

// POST表单AJAX处理
function AjaxPostDeal(formName){
	try {
		document.getElementById("loadingStr").innerHTML = "<span style='font-size:14px;'><img src='images/onload.gif' style='margin-right:5px;' />"+ ajaxDealStr +"</span>";
	}catch (e) {}

	formNameObj = document.getElementById(formName);
	var formNameUrl = formNameObj.getAttribute("action"), formNameContent = formValueToStr(formNameObj);
	$.post(formNameUrl,formNameContent,function(result){
		try {
			document.getElementById("loadingStr").innerHTML = "";
		}catch (e) {}
		eval(result.replace(/<\s*(script[^>]*)>([\s\S][^<]*)<\/\s*script>/gi,"$2"));
	});
	return false;
}

// 通过表单name获取该表单所有元素并组成GET字符串
function formValueToStr(formObj) {
	var qstr = "", and = "", elem, value;
	for(var i = 0; i< formObj.length; ++i) {
		elem = formObj[i];
		if (elem.name!='') {
			value=undefined;
			switch(elem.type) {
				case "select-one":
					if(elem.selectedIndex > -1) {
						value = elem.options[elem.selectedIndex].value;
					}
					else {
						value = "";
					}
					break;
				case"select-multiple":
					var selMul=elem.options;
					for(var w=0;w<selMul.length;++w){
						if(selMul[w].selected){
							qstr += and+elem.name+"="+escape(selMul[w].value);
							and = "&";
						}
					}
					break;
				case "checkbox":
				case "radio":
					if (elem.checked == true) {
						value = elem.value;
					}
					break;
				default:
					value = elem.value;
			}
			if(value!=undefined){
				value = escape(value);
				qstr += and + elem.name + "=" + value;
				and = "&";
			}
		}
	}
	return qstr;
}


// GET提交AJAX处理
function AjaxGetDeal(urlStr){
	try {
		document.getElementById("loadingStr").innerHTML = "<span style='font-size:14px;'><img src='images/onload.gif' style='margin-right:5px;' />"+ ajaxDealStr +"</span>";
	}catch (e) {}

	$.ajaxSetup({cache:false});
	$.get(urlStr, function(result){
		try {
			document.getElementById("loadingStr").innerHTML = "";
		}catch (e) {}
		eval(result.replace(/<\s*(script[^>]*)>([\s\S][^<]*)<\/\s*script>/gi,"$2"));
	});
	return false;
}


// GET提交AJAX处理
function AjaxGetDealToAlert(urlStr){
	$.ajaxSetup({cache:false});
	$.get(urlStr, function(result){
		alert(result.replace(/<\s*(script[^>]*)>([\s\S][^<]*)<\/\s*script>/gi,"$2"));
	});
	return false;
}


// GET提交AJAX处理
function AjaxGetDealToId(urlStr,outputID){
	$.ajaxSetup({cache:false});
	$.get(urlStr, function(result){
		document.getElementById(outputID).innerHTML = result;
		try{
			WindowHeight(0);
		}catch (e){}
	});
	return false;
}
