/*-------------------------------------------------------------------------*/
// Get cookie
/*-------------------------------------------------------------------------*/
var cookie_id = "";
var cookie_domain = "";
var cookie_path = "/";

function my_getcookie(name){
	cname = cookie_id + name + '=';
	cpos  = document.cookie.indexOf(cname);	
	if (cpos != -1){
		cstart = cpos + cname.length;
		cend   = document.cookie.indexOf(";", cstart);		
		if (cend == -1){
			cend = document.cookie.length;
		}		
		return unescape(document.cookie.substring(cstart, cend));
	}	
	return null;
}
/*-------------------------------------------------------------------------*/
// Set cookie
/*-------------------------------------------------------------------------*/
function my_setcookie(name,value,sticky){
	expire = "";
	domain = "";
	path   = "/";	
	if ( sticky ){
		expire = "; expires=Wed, 1 Jan 2030 00:00:00 GMT";
	}
	if (cookie_domain != ""){
		domain = '; domain=' + cookie_domain;
	}	
	if (cookie_path != ""){
		path = cookie_path;
	}	
	document.cookie = cookie_id + name + "=" + value + "; path=" + path + expire + domain + ';';
}

function togglecategory(fid,iNum){
	saved = new Array();
	clean = new Array();
	//-----------------------------------
	// Get any saved info
	//-----------------------------------	
	if (tmp = my_getcookie('collapseprefs')){
		saved = tmp.split(",");
	}	
	//-----------------------------------
	// Remove bit if exists
	//-----------------------------------	
	for( i = 0 ; i < saved.length; i++ ){
		if (saved[i] != fid && saved[i] != ""){
			clean[clean.length] = saved[i];
		}
	}	
	//-----------------------------------
	// show?
	//-----------------------------------	
	var obj = el(fid);
	var img = el('img_'+ fid);
	if (obj == null || img == null) return;
	var tag = obj.tagName.toLowerCase();
	var sCurrentFile = img.src.toLowerCase().substr(img.src.lastIndexOf('/'));

	if (iNum == null)
		iNum = 10;

	if (sCurrentFile == '/panel_up.gif'){
		clean[ clean.length ] = fid;
		if (tag == 'div')
			collapseElement(obj, iNum);
		else
			obj.style.display = 'none';
		img.src='../Images/Panel_Down.gif';
		img.title = '显示';
	}else{
		if (tag == 'div')
			expandElement(obj, iNum);
		else
			obj.style.display = 'block';
		img.src='../Images/Panel_Up.gif';
		img.title = '隐藏';
	}
	my_setcookie('collapseprefs', clean.join(','), 1);
}

/*-------------------------------------------------------------------------*/
// delay <div> Max or Min  ----  Start
/*-------------------------------------------------------------------------*/
var delay = new Array();

function doDelay(sType, iTime, pFunc, oContext){
	if (this.delay[sType] == null){
		this.delay[sType] = new delayObject(pFunc, oContext, sType);
		this.delay[sType].num = window.setTimeout(getObjMethRef(this.delay[sType], 'complete'), iTime);
	}
}

function cancelDelay(sType) {
	if (this.delay[sType] != null){
		window.clearTimeout(this.delay[sType].num);
		this.delay[sType] = null;
	}
}

function delayObject(pFunc, oContext, sType){
	this.num = null;
	this.pfunc = pFunc;
	this.context = oContext;
	this.type = sType;
}

delayObject.prototype.complete = function (){
	delay[this.type] = null;
	this.pfunc(this.context);
}

function createElement(sTagName){
	if (document.createElement) 
		return document.createElement(sTagName);
	else 
		return null;
}

function cancelCollapseElement(oCtl){
	cancelDelay(oCtl.id + 'col');
	oCtl.style.display = 'none';
}

function collapseElement(oCtl, iNum, pCallBack) {
	if (iNum == null)
		iNum = 10;
	oCtl.style.overflow = 'hidden';
	var oContext = new Object();
	oContext.num = iNum;
	oContext.ctl = oCtl;
	oContext.pfunc = pCallBack;
	oCtl.origHeight = oCtl.offsetHeight;
	__collapseElement(oContext);
}

function __collapseElement(oContext){
	var iNum = oContext.num;
	var oCtl = oContext.ctl;
	
	var iStep = oCtl.origHeight / iNum;
	if (oCtl.offsetHeight - (iStep*2) > 0){
		oCtl.style.height = oCtl.offsetHeight - iStep +'px';
		doDelay(oCtl.id + 'col', 10, __collapseElement, oContext);
	}else{
		oCtl.style.display = 'none';
		if (oContext.pfunc != null)
			oContext.pfunc();
	}
}

function cancelExpandElement(oCtl){
	cancelDelay(oCtl.id + 'exp');
	oCtl.style.overflow = '';
	oCtl.style.height = '';			
}

function expandElement(oCtl, iNum, pCallBack){
	if (iNum == null)
		iNum = 10;
	
	if (oCtl.style.display == 'none' && oCtl.origHeight == null){
		oCtl.style.display = '';
		oCtl.style.overflow = '';
		oCtl.origHeight = oCtl.offsetHeight;
		oCtl.style.overflow = 'hidden';
		oCtl.style.height = '1px';
	}
	oCtl.style.display = '';

	var oContext = new Object();
	oContext.num = iNum;
	oContext.ctl = oCtl;
	oContext.pfunc = pCallBack;
	__expandElement(oContext);
}

function __expandElement(oContext){
	var iNum = oContext.num;
	var oCtl = oContext.ctl;
	var iStep = oCtl.origHeight / iNum;
	if (oCtl.offsetHeight + iStep < oCtl.origHeight){
		oCtl.style.height = oCtl.offsetHeight + iStep +'px';
		doDelay(oCtl.id + 'exp', 10, __expandElement, oContext);
	}else{
		oCtl.style.overflow = '';
		oCtl.style.height = '';
		if (oContext.pfunc != null)
			oContext.pfunc();
	}				
}

function getObjMethRef(obj, methodName){
	return (function(e)	{e = e||window.event; return obj[methodName](e, this); } );
}
/*-------------------------------------------------------------------------*/
// delay <div> Max or Min  ----  End
/*-------------------------------------------------------------------------*/

//获取元素绝对位置，返回数组
function GetAbsoluteLocationEx(element){
	if (arguments.length != 1 || element == null){ 
		return null; 
	} 
	var element = (typeof element == "object") ? element : el(element); 
	var offsetTop = element.offsetTop; 
	var offsetLeft = element.offsetLeft; 
	var offsetWidth = element.offsetWidth; 
	var offsetHeight = element.offsetHeight; 
	do{
		element = element.offsetParent;
		offsetTop += element.offsetTop; 
		offsetLeft += element.offsetLeft; 
	} while(element.tagName.toLowerCase() != "body");

	return{ Top:offsetTop, Left:offsetLeft, Width:offsetWidth, Height:offsetHeight };
}

/*-----------------------------------*/
//隐藏或显示搜索工具栏 如果要在初始化显示工具栏,应设置 isDisplay = true
/*-----------------------------------*/
var isDisplay = false;
function ShowSearchPanel(iNum){
	if (iNum == null)
		iNum = 5;
	var obj = el('tbSearchPanel');
	if (obj == null) return;
	var tag = obj.tagName.toLowerCase();
	if (!isDisplay){
		if (tag == 'div')
			expandElement(obj,iNum);
		else
			obj.style.display = 'block';
		isDisplay = true;
	}else{
		if (tag == 'div')
			collapseElement(obj,iNum);
		else
			obj.style.display = 'none';
		isDisplay = false;
	}
}

/*-----------------------------------*/
//分页验证
function validationPageControl(){
	try{
		var pageSize = el("PageControl1_txtPageSize");
		var pageCurr = el("PageControl1_txtGoPage");
		if ((!pageSize) || (!pageCurr)){ return; }
		if (jscomTrimString(pageSize.value) == ""){
			alert("信息提示：每页行数不能为空!   ");
			getFocus("PageControl1_txtPageSize");
			return false;
		}
		if (!jscomIsNumber(jscomTrimString(pageSize.value))){
			alert("信息提示：每页行数只能输入整数!   ");
			getFocus("PageControl1_txtPageSize");
			getFocusTextLast("PageControl1_txtPageSize")
			return false;
		}
		if (jscomTrimString(pageCurr.value) == ""){
			alert("信息提示：页码不能为空!   ");
			getFocus("PageControl1_txtGoPage");
			return false;
		}
		if (!jscomIsNumber(jscomTrimString(pageCurr.value))){
			alert("信息提示：页码只能输入整数!   ");
			getFocus("PageControl1_txtGoPage");
			getFocusTextLast("PageControl1_txtGoPage")
			return false;
		}
		return true;
	}catch(e){}
}
function submitPageControl(e){
	try{
		e = window.event ? window.event : e ;
		if (e.keyCode == 13){
			submitStatus(true);
			if (validationPageControl()){
				__doPostBack('PageControl1$btnGoPage','');
			}else{
				submitStatus(false);
				return false;
			}
		}
	}catch(e){}
}
function submitStatus(vFlag){
	var objs = document.getElementsByTagName('input');
	for(var i=0; i<objs.length; i++){
		if(objs[i].type.toLowerCase() == 'submit')
			objs[i].disabled = vFlag;
	}
}
/*-----------------------------------*/

var jscomPopupDiv = "<div id='PopupDiv' style='position:absolute;overflow:visible;display:none;width:120px;height:50px;right:5px;bottom:5px;z-index:+999;'></div>";
document.write(jscomPopupDiv);

function jscomSelectOneUser(){
	jscomNewOpenBySize("../User/SelectUser.aspx","SelectUser",650,500);
}

function jscomSelectOneAdmin(){
	jscomNewOpenBySize("../User/SelectAdmin.aspx","SelectAdmin",540,450);
}

function jscomSelectPaper(){
	jscomNewOpenBySize("../Test/CompoundingSelectPaper.aspx","Selectpaper",640,450);
}

function jscomSelectOneBooking(){
	jscomNewOpenBySize("../Consultation/StuBookingSelect.aspx","SelectBooking",750,540);
}

function gotoURL(url){
	if(url=="") return;
	var w = window.open(url,"Main","");
	if (!w){
		try{
			window.parent.frames["Main"].location.href = url;
		}catch(e){}
	}
}

function GoQuickLink(obj,target,parameter){
	try{
		if (obj == null || target == null || parameter == null)
			return;
		var val = obj.options[obj.selectedIndex].value;
		if (val != ""){
			document.location.href = target +"?"+ parameter +"="+ val;
		}
	}catch(e){}
}

function ShowHelp(dir,target){
	try{
		if (dir == null)
			dir = "Back";
		if (target == null)
			target = "";
		jscomNewOpenBySize("../Help/"+ dir +"/Help.html?"+ target,"Help",750,540);
	}catch(e){}
}

//双击表格时操作的函数
function DoActionWhenDBLClickRow(GridName,ItemIndex){
	eval(el(GridName+"_hlDefaultOpen"+ItemIndex).href);
}

//********************************************************************
//全选函数
function jcomSelectAllRecords(SelectAllName,SelectName){
	if(!SelectAllName){
		SelectAllName = "chkSelectAll";
	}
	if(!SelectName){
		SelectName = "chkSelect";
	}
	if(els(SelectName) == null) return "";
	var checked=el(SelectAllName).checked;
	var sSelectedIDs = "";
	if(typeof(els(SelectName).length) == 'undefined'){
		els(SelectName).checked = checked;
		sSelectedIDs = els(SelectName).value;
		return sSelectedIDs;
	}else{
		for (var i=0; i < els(SelectName).length; i ++){
			if(i == 0){
				if (!els(SelectName)[i].disabled){
					els(SelectName)[i].checked = checked;
					sSelectedIDs += els(SelectName).value;
				}
			}else{		
				if (!els(SelectName)[i].disabled){
					els(SelectName)[i].checked = checked;
					sSelectedIDs += ","+els(SelectName).value;
				}
			}
		}
	}
	return sSelectedIDs;
}

function jcomGetFirstSelectedRecord(SelectName){
	if(!SelectName)
		SelectName = "chkSelect";
		
		if(els(SelectName) == null)
			return "";
		
		var sSelectIDs = "";
		if(typeof(els(SelectName).length) == 'undefined'){
			if (els(SelectName).checked == true){
				sSelectIDs=els(SelectName).value;
				return sSelectIDs;
			}else{
				return "";
			}
		}

		var nCount =els(SelectName).length;
		for (var i=0; i < nCount; i ++){
			if (els(SelectName)[i].checked == true){
				return els(SelectName)[i].value;
			} 				
		} 
		return "";
}

function jcomGetAllSelectedRecords(SelectName){
	if(!SelectName)
		SelectName = "chkSelect";
		
	if(els(SelectName) == null)
		return "";
		
	var sSelectIDs = "";
	if(typeof(els(SelectName).length) == 'undefined'){
		if (els(SelectName).checked == true){
			sSelectIDs = els(SelectName).value;
			return sSelectIDs;
		}else{
			return "";
		}
	}
	
	var nCount = els(SelectName).length;
	for (var i=0; i < nCount; i ++){
		if (els(SelectName)[i].checked == true){
			sSelectIDs += els(SelectName)[i].value+",";
		} 				
	}
	if (sSelectIDs != ""){
		sSelectIDs = sSelectIDs.substr(0,sSelectIDs.length-1);
	}
	return sSelectIDs;
}

function jcomSelectAddCancelAll(obj){//全选或全部取消函数
	try{
		if (obj == null) obj = "chkSelectAll";
		var checkbox = el(obj);
		if (checkbox == null) return;
		checkbox.click();
	}catch(e){}
}
//******************************************************

function jscomExportTableToWord(tableName){
	if(el(tableName).rows.length==0){
		alert("没有内容可导！");
		return;
	}

	var oWord;
	try{
		oWord = new ActiveXObject("Word.Application"); // Get a new workbook.
	}catch(e){
		alert("无法调用Office对象，请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中！");
		return;
	}
	var oDocument = oWord.Documents.Add();
	var oDocument = oWord.ActiveDocument; 
	
	var table = el(tableName);
	var nRows = table.rows.length; 
	var nCols = table.rows(0).cells.length;
	for (i=0;i<nRows;i++){
		nCol=0;
		nCols=table.rows(i).cells.length;
		for (j=0;j<nCols;j++){ 
			if(table.rows(i).cells(j)){
				oDocument.Paragraphs.Add();
				oDocument.Paragraphs.Last.Alignment = 0;
				oDocument.Paragraphs.Last.Range.Bold =false;
				if(j==0)
					oDocument.Paragraphs.Last.Range.Font.Size = 14;
				else
					oDocument.Paragraphs.Last.Range.Font.Size = 10;
				oDocument.Paragraphs.Last.Range.Font.name = "宋体";
				oDocument.Paragraphs.Last.Range.InsertAfter(table.rows(i).cells(j).innerText);
			}
			nCol=nCol+1;
		} 
	}
	oWord.Visible = true;
}

function jscomExportTableToExcel(tableName,ignoreCols,ignoreLastRows){ 
	if (!confirm("注意：\n导出到Excel导出到Excel将比较耗时，1000条记录大约要一分钟，建议一次导出记录数不超过1000条。你确定要导出吗？")) return;
	var oXL;
	try{
		oXL = new ActiveXObject("Excel.Application"); // Get a new workbook.
	}catch(e){
		alert("无法调用Office对象，请确保您的机器已安装了Office并已将本系统的站点名加入到IE的信任站点列表中！");
		return;
	}
	try{
		var oWB = oXL.Workbooks.Add();
		var oSheet = oWB.ActiveSheet; 
		var table = el(tableName);
		var nRows = table.rows.length; 
		if(ignoreLastRows && ignoreLastRows.valueOf()!=0) nRows=nRows-ignoreLastRows.valueOf();
		var nCols = table.rows(0).cells.length; // Add table headers going cell by cell.
		var nCol=0;
		if (!ignoreCols) ignoreCols='';
		//提高效率
		if(ignoreCols==''){
			for (i=0;i<nRows;i++){
				nCol=0;
				for (j=0;j<nCols;j++) 
				{ 
					if(table.rows(i).cells(nCol)) oSheet.Cells(i+1,nCol+1).value = table.rows(i).cells(nCol).innerText;
					nCol=nCol+1;
				} 
			}
		}else{
			ignoreCols=','+ignoreCols+',';
			for (i=0;i<nRows;i++){
				nCol=0;
				for (j=0;j<nCols;j++){ 
					if (ignoreCols.indexOf(','+j+',')==-1){
						if(table.rows(i).cells(j)) oSheet.Cells(i+1,nCol+1).value = table.rows(i).cells(j).innerText;
						nCol=nCol+1;
					}
				} 
			}
		}
		oXL.Visible = true;
		oXL.UserControl = true;
	}catch(e){
		alert("导出失败！"+e);
	}
}

function jscomShowConfirm(message){
	var returnValue=showModalDialog('../Modules/MessageBox.aspx',message,"dialogWidth:350px;dialogHeight:240px;status:no;scrollbars=no");
	if(typeof(returnValue)=='undefined')
		returnValue=0;
	return returnValue;
}

var sShowMessageWindowArgument="";
function jscomShowMessageWindow(title,message,width,height){

	sShowMessageWindowArgument=title+"|-|"+message;
	if(width==null)
		width="500";
		
	if(height==null)
		height="400";
	
	var url="../Modules/ShowMessage.aspx";
	
    var tt,w,left,top;
	left=(screen.width-width)/2;
	if(left<0){ left=0;}

	top=(screen.height-60-height)/2;
	if(top<0){ top=0;}

    tt="toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=yes,";
    tt=tt+"width="+width+",height="+height+",left="+left+",top="+top;
	w=window.open(url,"MessageWindow",tt);
	try{
		w.focus();
	}catch(e){}	
}

//给上一个含数jscomShowMessageWindow作参数回去的接口
function GetsShowMessageWindowArgument(){
	return sShowMessageWindowArgument;
}
		
function jscomShowMessageDialog(title,message,width,height){
	if(width==null)
		width="500";
		
	if(height==null)
		height="400";
	showModelessDialog('../Modules/ShowMessage.aspx',title+"|-|"+message,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;status:no;scrollbars=auto");
}

//选择数据行时，改变数据行的背景色
function SelectDataTableRows(table,checkBox){
	try{
		if (!table)
			table = "dgList";
		var oTable = el(table);
		if (oTable){
			if (!checkBox)
				checkBox = "chkSelect";
			var aCheckBoxes = els(checkBox);
			var aRows = oTable.tBodies[0].rows;
			var nRows = aRows.length-1;
			for (var i=0;i<=nRows;i++){
				if (aCheckBoxes[i].checked==true){
					aRows[i+1].className = 'SelRow';
				}else{
					if (i%2 == 0){
						aRows[i+1].className = 'Row0';
					}else{
						aRows[i+1].className = 'Row1';
					}
				}
			}
		}
		else return false;
	}catch(e){}
}
