﻿// JScript File
// Code Region is used by Checkbox Column
//**********************For CheckBox**********************************************
//function ChangeBackGroundColor(checkBox,selectColor,unSelectColor)
//    {
//        
////      //changing row style accordingly
//        if(checkBox.checked)
//           //checkbox.Cell.Row ( this is the target row where checkbox is added
//           checkBox.parentNode.parentNode.style.backgroundColor=selectColor;
//        else
//           checkBox.parentNode.parentNode.style.backgroundColor=unSelectColor;
//           return true;
//    }
//    function HighLightRow(checkboxref,rowId,selectColor,unSelectColor)
//    {
//       
//        
//                 //check        .TableCell .TableRow  .TableHead .Table    
//        var oGrid=checkboxref.parentNode.parentNode.parentNode.parentNode;
////      ChangeBackGroundColor
//        ChangeBackGroundColor(checkboxref,selectColor,unSelectColor);   
//       
//        var rowIndex=0;
//        //oGrid.children[0].childNodes[0].children.length
//        var columnIndex=0;
//        var columnName;
//        var obj=checkboxref.parentNode;
//        
//        columnIndex=getClickedCheckBoxIndex(obj);
//        
//        //after getting the right column 
//        // needs to get Header Checkbox
//                         //Table.THEAD      .TR     .TD                   .CheckBox   
//        var HeaderCheckBox=oGrid.children[0].rows[0].children[columnIndex].firstChild;
//        //Initilizating check box 
//        HeaderCheckBox.checked=true;
//        
//        // start looping from TBODY
//        for(rowIndex=0;rowIndex<oGrid.children[1].rows.length;rowIndex++)
//        {
//          // TABLE .TBODY      .TR(s)  .TD         .TARGET CHECK BOX                         
//          var checkBox= oGrid.children[1].rows[rowIndex].children[columnIndex].firstChild;
//          //will use circuit switching to uncheck Header Checkbox means when we find first 
//          //unchecked checkbox in Tbody rows we will uncheck Header check box and break 
//          //otherwise it is fine               
//          if(checkBox.type="checkbox")
//          {
//                if(!checkBox.checked) 
//                {
//                  HeaderCheckBox.checked=false;
//                  break;
//                }
//          }
//        }   
//       return true;
//    }
//    function ChangeSelection(HeaderCheckBox,selectColor,unSelectColor)
//    {
//        
//        var rowIndex=0;
//                 //check        .TableCell .TableRow  .TableHead .Table    
//        var oGrid=HeaderCheckBox.parentNode.parentNode.parentNode.parentNode;
//        var columnIndex=0;
//        var columnName;
//        var obj=HeaderCheckBox.parentNode;
//        
//        columnIndex=getClickedCheckBoxIndex(obj);
//        
//        // start looping from TBODY
//        for(rowIndex=0;rowIndex<oGrid.children[1].rows.length;rowIndex++)
//        {
//          // TABLE .TBODY      .TR(s)  .TD         .TARGET CHECK BOX                         
//           var checkBox= oGrid.children[1].rows[rowIndex].children[columnIndex].firstChild;      
//          if(checkBox.type="checkbox")
//          {
//                checkBox.checked=HeaderCheckBox.checked;
//                //now need to change the background color of the row accordingly
//                //debugger;
//                ChangeBackGroundColor(checkBox,selectColor,unSelectColor);
//          }
//        }
//        return true;
//    }
//    
//    function getClickedCheckBoxIndex(obj)
//    {
//        var columnIndex=0;
//        while(obj.previousSibling){
//			obj = obj.previousSibling;
//			if(obj.tagName=='TD')columnIndex++;		
//		}
//		return columnIndex;
//    }
    //**********************For CheckBox**********************************************
    /// Code region used for sorting and related functionality
    //
    //
    var tableCounter = 0;
	var arraySort = new Array();
	var okToSort = true;
	var activeColumn = new Array();
	var arrowImagePath = "images/";	// Path to arrow images
			
	function sortNumeric(a,b){
		
		a = a.replace(/,/,'.');
		b = b.replace(/,/,'.');
		a = a.replace(/[^\d\-\.\/]/g,'');
		b = b.replace(/[^\d\-\.\/]/g,'');
		if(a.indexOf('/')>=0)a = eval(a);
		if(b.indexOf('/')>=0)b = eval(b);
		return a/1 - b/1;
	}
	
	function sortString(a, b) {

	  if ( a.toUpperCase() < b.toUpperCase() ) return -1;
	  if ( a.toUpperCase() > b.toUpperCase() ) return 1;
	  return 0;
	}	
	function cancelEvent()
	{
		return false;
	}
	
	function sortTable()
	{
	    
		if(! okToSort)return;
		 okToSort = false;
		/* Getting index of current column */
		var obj = this;
		var indexThis = 0;
				
		while(obj.previousSibling){
			obj = obj.previousSibling;
			if(obj.tagName=='TD')indexThis++;		
		}
		var images = this.getElementsByTagName('IMG');
		
		if(this.getAttribute('direction') || this.direction){
			direction = this.getAttribute('direction');
			if(navigator.userAgent.indexOf('Opera')>=0)direction = this.direction;
			if(direction=='ascending'){
				direction = 'descending';
				this.setAttribute('direction','descending');
				this.direction = 'descending';	
			}else{
				direction = 'ascending';
				this.setAttribute('direction','ascending');		
				this.direction = 'ascending';		
			}
		}else{
			direction = 'ascending';
			this.setAttribute('direction','ascending');
			this.direction = 'ascending';
		}
						
		if(direction=='descending'){
			images[1].style.display='inline';
			images[1].style.visibility='visible';
			images[0].style.display='none';
		}else{
			images[0].style.display='inline';
			images[0].style.visibility='visible';
			images[1].style.display='none';	
		}
		
		var tableObj = this.parentNode.parentNode.parentNode;
		var tBody = tableObj.getElementsByTagName('TBODY')[0];	
		var sortExpression=document.getElementById("__SORTEXPR_"+tableObj.id);		
		var widgetIndex = 0;//SSSStableObj.id.replace(/[^\d]/g,'');
		var sortMethod =  arraySort[widgetIndex][indexThis]; // N = numeric, S = String
		if(activeColumn[widgetIndex] && activeColumn[widgetIndex]!=this){
			var images = activeColumn[widgetIndex].getElementsByTagName('IMG');
			images[0].style.display='none';
			images[1].style.display='inline';
			images[1].style.visibility = 'hidden';
			if(activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction');			
		}

		activeColumn[widgetIndex] = this;		
		var cellArray = new Array();
		var cellObjArray = new Array();
//		for(var no=1;no<tableObj.rows.length;no++){
//			var content= tableObj.rows[no].cells[indexThis].innerHTML+'';
//			cellArray.push(content);
//			cellObjArray.push(tableObj.rows[no].cells[indexThis]);
//		}
        for(var no=0;no<tBody.rows.length;no++){
			var content= tBody.rows[no].cells[indexThis].innerHTML+'';
			cellArray.push(content);
			cellObjArray.push(tBody.rows[no].cells[indexThis]);
		}
		
		if(sortMethod=='N'){
			cellArray = cellArray.sort(sortNumeric);
		}else{
			cellArray = cellArray.sort(sortString);
		}
		
		if(direction=='descending'){
			for(var no=cellArray.length;no>=0;no--){
				for(var no2=0;no2<cellObjArray.length;no2++){
					if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
						cellObjArray[no2].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[no2].parentNode);				
					}				
				}			
			}
		}else{
			for(var no=0;no<cellArray.length;no++){
				for(var no2=0;no2<cellObjArray.length;no2++){
					if(cellObjArray[no2].innerHTML == cellArray[no] && !cellObjArray[no2].getAttribute('allreadySorted')){
						cellObjArray[no2].setAttribute('allreadySorted','1');	
						tBody.appendChild(cellObjArray[no2].parentNode);				
					}				
				}			
			}				
		}
		
		for(var no2=0;no2<cellObjArray.length;no2++){
			cellObjArray[no2].removeAttribute('allreadySorted');		
		}
	    sortExpression.value=indexThis+','+this.getAttribute('direction');
		 okToSort = true;				
	}
	
	function initTableWidget(objId,width,height,sortArray)
	{	    	   
		width = width + '';
		height = height + '';
		var obj = document.getElementById(objId);
    	var initialSortExpression=obj.getAttribute('InitialSortColumn');
    	var sortExpression=document.getElementById("__SORTEXPR_" + objId);
    	//document.getElementById("__SORTEXPR_" + objId);
    				
		if(navigator.userAgent.indexOf('MSIE')>=0){
			obj.parentNode.style.overflowY = 'auto';
		}		
		 arraySort[tableCounter] = sortArray;
		
		if(width.indexOf('%')>=0){
			obj.style.width = width;
			obj.parentNode.style.width = width;
		}else{
			obj.style.width = width + 'px';
			obj.parentNode.style.width = width + 'px';
		}

		if(height.indexOf('%')>=0){
			obj.parentNode.style.height = height;			
			
		}else{
			obj.parentNode.style.height = height + 'px';
		}
		obj.cellSpacing = 0;
		obj.cellPadding = 0;
	
		var tHead = obj.getElementsByTagName('THEAD')[0];
		var cells = tHead.getElementsByTagName('TD');
		for(var no=0;no<cells.length;no++){
			cells[no].className = 'ColumnHeader';
			cells[no].onselectstart = cancelEvent;
			if(no==cells.length-1){
				cells[no].style.borderRight = '0px';	
			}
					
			if(sortArray[no]){
				cells[no].onmouseover = highlightTableHeader;
				cells[no].onmouseout =  deHighlightTableHeader;
				cells[no].onmousedown = mousedownTableHeader;		
				cells[no].onmouseup = highlightTableHeader;		
				cells[no].onclick = sortTable;	
				
				var img = document.createElement('IMG');
				img.src = arrowImagePath + 'up.gif';
				cells[no].appendChild(img);	
				img.style.visibility = 'hidden';
				img.style.width="15px";
				img.style.height="15px";
				var img = document.createElement('IMG');
				img.src = arrowImagePath + 'down.gif';
				cells[no].appendChild(img);	
				img.style.display = 'none';
				img.style.width="15px";
				img.style.height="15px";
												
			    var sortInfo;
			    if(sortExpression.value.length==0)
			         sortInfo=initialSortExpression.split(",");
			    else
			        sortInfo=sortExpression.value.split(",");
			    
			    if(sortInfo.length>0)
			    {
			        if(no==sortInfo[0])
				    { 
				        if(sortInfo[1]=="ascending")
				            sortInfo[1]="descending";
				        else
				            sortInfo[1]="ascending";
				        
				        cells[no].setAttribute('direction',sortInfo[1]);
				        cells[no].click();
                       
				    }
			    } 							
			}else{
				cells[no].style.cursor = 'default';	
			}						
		}		
		var tBody = obj.getElementsByTagName('TBODY')[0];
		if(document.all && navigator.userAgent.indexOf('Opera')<0){
			tBody.style.display='block';			
		}else{
			tBody.style.height = (obj.parentNode.clientHeight-tHead.offsetHeight) + 'px';
			if(navigator.userAgent.indexOf('Opera')>=0){
				obj.parentNode.style.overflow = 'auto';
			}
		}
		tableCounter++;
	}
	
	function highlightTableHeader()
	{
	    
		this.className='HighlightColumnHeader';
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode.parentNode;
			this.parentNode.style.top = divObj.scrollTop + 'px';
		}			
	}
	
	function deHighlightTableHeader()
	{
		this.className='ColumnHeader';		
	}
	
	function mousedownTableHeader()
	{
		this.className='ColumnHeaderDown';
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode.parentNode;
			this.parentNode.style.top = divObj.scrollTop + 'px';
		}		
	}
	
	function highlightDataRow()
	{
		if(navigator.userAgent.indexOf('Opera')>=0)return;
		this.className='GridTable_RowHighLight';
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode;
			var tHead = divObj.getElementsByTagName('TR')[0];
			tHead.style.top = divObj.scrollTop + 'px';
			
		}	
	}
	
	function deHighlightDataRow()
	{
		if(navigator.userAgent.indexOf('Opera')>=0)return;
		this.className=null;
		if(document.all){	// I.E fix for "jumping" headings
			var divObj = this.parentNode.parentNode.parentNode;
			var tHead = divObj.getElementsByTagName('TR')[0];
			tHead.style.top = divObj.scrollTop + 'px';
		}			
	}
