/**
 *File:$Id: functions.js,v 1.1.1.1 2011-07-02 12:01:30 cvssys Exp $
 *通用功能函数
 *
 */
//Firefox关闭窗口
function FfClose(){
    top.window.opener = top;
    top.window.open('','_parent','');
    top.window.close();
	}
//IE7关闭窗口
function IE7Close(){
    top.window.opener = top;
    top.window.open('','_self','');
    top.window.close();
}
//其它浏览器的关闭
function closeWindow(){
	var ua=navigator.userAgent;
	var ie=navigator.appName=="Microsoft Internet Explorer"?true:false;
	if(ie){
		var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE "))));
		if(IEversion< 5.5){
			var str = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">';
			str += '<param name="Command" value="Close"></object>';
			document.body.insertAdjacentHTML("beforeEnd", str);
			document.all.noTipClose.Click();
		}
		else{
			//window.opener =null;
			//window.close();
			IE7Close();
		}
	}
	else{
		var browser=getBrowseType();
		//alert(browser);
		if(browser=="Firefox")FfClose();
	}
}
/*
 * 判断浏览器函数
 */
function getBrowseType() {
	ua = navigator.userAgent.toLowerCase(),
			check = function(r) {
				return r.test(ua);
			},
			isOpera = check(/opera/),
			isChrome = check(/chrome/),
			isWebKit = check(/webkit/),
			isSafari = !isChrome && check(/safari/),
			isSafari2 = isSafari && check(/applewebkit\/4/), // unique to
																// Safari 2
			isSafari3 = isSafari && check(/version\/3/), isSafari4 = isSafari
					&& check(/version\/4/), isIE = !isOpera && check(/msie/),
			isIE7 = isIE && check(/msie 7/), isIE8 = isIE && check(/msie 8/),
			isIE6 = isIE && !isIE7 && !isIE8, isGecko = !isWebKit
					&& check(/gecko/), isGecko2 = isGecko && check(/rv:1\.8/),
			isGecko3 = isGecko && check(/rv:1\.9/),
			isFirefox2 = check(/firefox\/2/), isFirefox3 = check(/firefox\/3/);
	if (isIE8)
		return "IE8";
	else if (isIE7)
		return "IE7";
	else if (isIE6)
		return "IE6";
	else if (isIE)
		return "IE";
	else if (isFirefox2)
		return "Firefox";
	else if (isFirefox3)
		return "Firefox";
	else if (isOpera)
		return "Opera";
	else if (isChrome)
		return "Chrome";
	else if (isSafari)
		return "Safari";
	else
		return "unknown";
}
// 过滤首尾空格trim
function trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
// 过滤左边空格ltrim
function ltrim(str) {
	return str.replace(/(^\s*)/g, "");
}// 过滤右边空格rtrim
function rtrim(str) {
	return str.replace(/(\s*$)/g, "");
}
// 打开文件夹夹
function OpenFolder(Open_ID, Close_ID) {
	Open_ID = document.getElementById(Open_ID);
	Close_ID = document.getElementById(Close_ID);
	Open_ID.style.display = '';
	Close_ID.style.display = 'none';
	return;
}
// 打开文件夹夹
function CloseFolder(Open_ID, Close_ID) {
	Open_ID = document.getElementById(Open_ID);
	Close_ID = document.getElementById(Close_ID);
	Open_ID.style.display = 'none';
	Close_ID.style.display = '';
	return;
}
// IE6中处理PNG图
function correctPNG() {
	if ((getBrowseType() == "IE6")) {// IE6才处理
		for ( var i = 0; i < document.images.length; i++) {
			var img = document.images[i];
			var imgName = img.src.toUpperCase();
			if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : "";
				var imgClass = (img.className) ? "class='" + img.className
						+ "' " : "";
				var imgTitle = (img.title) ? "title='" + img.title + "' "
						: "title='" + img.alt + "' ";
				var imgStyle = "display:inline-block;" + img.style.cssText;
				if (img.align == "left")
					imgStyle = "float:left;" + imgStyle;
				if (img.align == "right")
					imgStyle = "float:right;" + imgStyle;
				if (img.parentElement.href)
					imgStyle = "cursor:hand;" + imgStyle;
				imgWidth = (img.width == 0) ? 20 : img.width;
				imgHeight = (img.height == 0) ? 18 : img.height;
				var strNewHTML = "<span "
						+ imgID
						+ imgClass
						+ imgTitle
						+ " style=\""
						+ "width:"
						+ imgWidth
						+ "px; height:"
						+ imgHeight
						+ "px;"
						+ imgStyle
						+ ";"
						+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
						+ "(src=\'" + img.src
						+ "\', sizingMethod='scale');\"></span>"; // scale,crop
				// alert(strNewHTML+"i="+i);
				img.outerHTML = strNewHTML;
				i = i - 1;
			}
		}
	}
}
// IE6中处理PNG图
function singleCorrectPNG(id) {
	if ((getBrowseType() == "IE6")) {// IE6才处理
		var img = document.getElementById(id);
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
			 var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' "
					: "";
			var imgTitle = (img.title) ? "title='" + img.title + "' "
					: "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left")
				imgStyle = "float:left;" + imgStyle;
			if (img.align == "right")
				imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href)
				imgStyle = "cursor:hand;" + imgStyle;
			imgWidth = (img.width == 0) ? 20 : img.width;
			imgHeight = (img.height == 0) ? 18 : img.height;
			var strNewHTML = "<span "
					+ imgID
					+ imgClass
					+ imgTitle
					+ " style=\""
					+ "width:"
					+ imgWidth
					+ "px; height:"
					+ imgHeight
					+ "px;"
					+ imgStyle
					+ ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src=\'" + img.src
					+ "\', sizingMethod='scale');\"></span>"; // scale,crop
			// alert(strNewHTML+"i="+i);
			img.outerHTML = strNewHTML;
		}
	}
}
/*
 * CheckAll：选择所有的checkbox
 */
function CheckAll(form) {
	//alert("run here!");
	for ( var i = 0; i < form.elements.length; i++)
		form.elements[i].checked = true;
}
/*
 * FanAll:反选所有的checkbox
 */
function FanAll(form) {
	for ( var i = 0; i < form.elements.length; i++)
		form.elements[i].checked = !(form.elements[i].checked);
}
// 检查字段的值是否存在
function checkFieldsValue(FieldsMsg, FieldsID, formID, ajaxUrl, table) {
	var msg = "";
	var fields = document.getElementById(FieldsID).value;
	var form = document.getElementById(formID);// 获得form
	var op = document.getElementById("operate").value;
	if (op == "add") {//只有增加才判断是否存在
		if (fields == "")
			msg = "1." + FieldsMsg + "不能为空\n";
		// 创建ajax对象
		var AjaxObj = createAjaxObj();
		/* var eflag=false; */
		AjaxObj.onreadystatechange = function() {
			if ((AjaxObj.readyState == 4)&& (AjaxObj.status == 200)){
					// if(AjaxObj.responseText=="true")
					// eflag=true;//alert("这个"+FieldsMsg+"已经存在");
					if (AjaxObj.responseText == "true") alert("这个" + FieldsMsg + "已经存在");
				}
			return;
		};
		if (msg != "")	alert(msg);
		else {
			if (AjaxObj.readyState == 4 || AjaxObj.readyState == 0) {
				var sendData = "table=" + table + "&fields=" + FieldsID
						+ "&value=" + fields;
				// alert(sendData);
				// 查询用户是否存在
				AjaxObj.open("post", ajaxUrl, true);// post方法所有数据都是通过send提交的,也可以以get形式提交数据，放在url中
				AjaxObj.setRequestHeader('Content-type',
						'application/x-www-form-urlencoded');
				AjaxObj.send(sendData);
			}
		}
	}
	/*
	 * if(eflag) return true; else return false;
	 */
}
/*
 * 设置焦点
 */
function setFocus(id) {
	id = document.getElementById(id);
	if (id) {
		id.focus();
	}
	return false;
}
/*
 * getQuery:获得已有的查询
 */
function getQuery(qid, url) {
	var height = 400, width = 600;
	if (getBrowseType() == "Firefox") {
		height += 0;
		width += 0;
	}
	var param = 'height=' + height + ', width=' + width;
	param += ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no';
	url += "?qid=" + qid;
	// alert(url);
	var winName = "_blankQuery";
	var bWindow = window.open(url, winName, param);
	bWindow.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
	bWindow.focus();
}
