// CheckBoxSelected.js Version 1.01
// for VS.NET 2003
// Update by rackliu. 2003.09.04
// Update by rackliu. 2004.03.24
// Update by rackliu. 2005.01.13
// 新增有關 UltraWebGrid 的部份函數

var ie = document.all;

function SelAllResult(CB){
	//CB = CB.children[0];
	SetChkStatus(CB.checked);
}

function SetChkStatus(status)
{
	var frm = document.forms[0];
	
	for(i=0;i<frm.length;i++)
	{
		e = frm.elements(i);
		if (e.name.indexOf("chkResultSelect") != -1){
			
			if(!status){
				dL(e);
				e.checked = false;
			}
			else{
				hL(e);
				e.checked = true;	
			}
		}
	}
}

function SelResult(CB){
	var frm = document.forms[0];
	
	//CB = CB.children[0];
	if (CB.checked){
		hL(CB);
	}
	else{
		dL(CB);
		for(i=0;i<frm.length;i++){
			e = frm.elements(i);
			if (e.name.indexOf("chkResultSelectAll") != -1){
				e.checked = false;
			}
		}
	}
}

function hL(E){
	if (ie)
	{
		while (E.tagName!="TR")
		{E=E.parentElement;}
	}
	else
	{
		while (E.tagName!="TR")
		{E=E.parentNode;}
	}
	// 2004/03/24 保留原有 className 到 tag
	if(!E.tag)
		E.tag = E.className;
	//
	E.className = "H";
}

function dL(E){
	if (ie)
	{
		while (E.tagName!="TR")
		{E=E.parentElement;}
	}
	else
	{
		while (E.tagName!="TR")
		{E=E.parentNode;}
	}
	
	// 2004/03/24 復原原有 className
	if(E.tag)
		E.className = E.tag;
	else
		E.className = "";	// no change.
}
	
	
//***** for Infragistics.WebUI.UltraWebGrid

	function ExpandAllRows(gridID, expandAll) {
		// Loop thru the rows of Band 0 and expand each one
		var oGrid = igtbl_getGridById(gridID);
		var oRows = oGrid.Rows;
		oGrid.suspendUpdates(true);
		for(i=0; i<oRows.length; i++) 
		{
			oRow = oRows.getRow(i);
			if(expandAll == true) {
				oRow.setExpanded(true);
			}
			else {
				oRow.setExpanded(false);
			}
			// child-1
			if( oRow.ChildRowsCount > 0) 
			{
				var oChildRows = oRow.Rows;
				for(j=0; j < oChildRows.length; j++) 
				{
					oChildRow = oChildRows.getRow(j);
					if(expandAll == true) {
						oChildRow.setExpanded(true);
					}
					else {
						oChildRow.setExpanded(false);
					}
					
					// child-2
					if( oChildRow.ChildRowsCount > 0) 
					{
						var oChildRows2 = oChildRow.Rows;
						for(k=0; k < oChildRows2.length; k++)
						{
							oChildRow2 = oChildRows2.getRow(k);
							if(expandAll == true) {
								oChildRow2.setExpanded(true);
							}
							else {
								oChildRow2.setExpanded(false);
							}
							// child-3
							if( oChildRow2.ChildRowsCount > 0) 
							{
								var oChildRows3 = oChildRow2.Rows;
								for(l=0; l < oChildRows3.length; l++) {
									oChildRow3 = oChildRows3.getRow(l);
									if(expandAll == true) {
										oChildRow3.setExpanded(true);
									}
									else {
										oChildRow3.setExpanded(false);
									}									
								} // FOR end of child - 3
							} // IF end of child - 3						
						} // FOR end of child - 2
					} // IF end of child - 2
				} // FOR end of child - 1
			} // IF end of child - 1
		}	// FOR end  of ROW
		oGrid.suspendUpdates(false);		
	}
	
	function SelectAllRows(gridID, selectAll) {
		// Select all rows in the grid
		var oGrid = igtbl_getGridById(gridID);
		var oRows = oGrid.Rows;
		oGrid.suspendUpdates(true);
		for(i=0; i<oRows.length; i++) 
		{
			oRow = oRows.getRow(i);
			if(selectAll == true) {
				oRow.setSelected(true);
			}
			else {
				oRow.setSelected(false);
			}
			// child-1
			if( oRow.ChildRowsCount > 0) 
			{
				var oChildRows = oRow.Rows;
				for(j=0; j < oChildRows.length; j++) 
				{
					oChildRow = oChildRows.getRow(j);
					if(selectAll == true) {
						oChildRow.setSelected(true);
					}
					else {
						oChildRow.setSelected(false);
					}					
					// child-2
					if( oChildRow.ChildRowsCount > 0) 
					{
						var oChildRows2 = oChildRow.Rows;
						for(k=0; k < oChildRows2.length; k++)
						{
							oChildRow2 = oChildRows2.getRow(k);
							if(selectAll == true) {
								oChildRow2.setSelected(true);
							}
							else {
								oChildRow2.setSelected(false);
							}
							// child-3
							if( oChildRow2.ChildRowsCount > 0) 
							{
								var oChildRows3 = oChildRow2.Rows;
								for(l=0; l < oChildRows3.length; l++) {
									oChildRow3 = oChildRows3.getRow(l);
									if(selectAll == true) {
										oChildRow3.setSelected(true);
									}
									else {
										oChildRow3.setSelected(false);
									}									
								} // FOR end of child - 3
							} // IF end of child - 3						
						} // FOR end of child - 2
					} // IF end of child - 2
				} // FOR end of child - 1
			} // IF end of child - 1
		}	// FOR end  of ROW
		oGrid.suspendUpdates(false);
	}
	
	function CountGridRows(gridID) {
		// Count all rows and place the value into the label field
		var oGrid = igtbl_getGridById(gridID);
		var oRows = oGrid.Rows;
		var total = oRows.length;
		for(i=0; i<oRows.length; i++) {
			oRow = oRows.getRow(i);
			total += oRow.ChildRowsCount;
		}
		return total;
	}	
	
	function CountGridSelectedRows(gridID) {
		// Count all rows and place the value into the label field
		var oGrid = igtbl_getGridById(gridID);
		var oRows = oGrid.Rows;
		var total = 0;
		for(i=0; i<oRows.length; i++) {
			oRow = oRows.getRow(i);
			if(oRow.getSelected())
				total ++;
			//
			total += oRow.ChildRowsCount;
		}
		return total;
	}		
//***** End for Infragistics.WebUI.UltraWebGrid


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

