/*
Note: 
1. For date fields presently the logic is built on the assumption that the value attribute and the format attribute are in the format mm/dd/yyyy. This will be changed to accomodate different date formats in future releases

2. The variable below named "plaincell" works as a flag to determine whether the cells in the table contain input tags or are just plain cells. When set to true this script will work for tables where cells have no input fields, when set to false this script will work for tables with input fields. So if you receive javascript error then check this variables value to see whether it is set right. 
*/


//Global variables decaration******************************************************
	var rowArray=new Array();
	var cellArray=new Array();
	var storeArray=new Array();
	var columnnames=new Array();	
	var titleArray=new Array();
	var sortcol;
	var plaincell;//This variable should be set to "true" to sort columns with cell which have input fields and "false" to sort columns with plain text cells.
	var descending=true;
	var globobject;	//This object holds the reference to the table on which all the operations are being performed. It is set in inittable function below.
	var dupmaintable;//This variable holds the object reference to the original table.
	var temptitlerownode;
	var temparraytablefirstrow;
	var arrowimage="/images/sup.gif";
	var togglingbgcolor="#99ffff";//This variable holds the value for background color for the cell when the arrow changes in the cell as the user clicks the title value in first row each time to sort.
	var togglingbgothercolor="#336699";
	var sortswitch=0;
//***********************************************************************************
function initTable(tableobj,sortcolumn)
{
	globobject=tableobj;
	dupmaintable=document.getElementById(tableobj).cloneNode(true);
	titlerowdupNode = document.getElementById(tableobj).rows[0].cloneNode(true);
	init();
	//sortTable(tableobj,sortcolumn);
}

function sortTable(obj,sortcol,sortflag)
{
//Initialisations for find function************************************************
if (rownum!=0)
{
document.getElementById(globobject).rows[rownum].cells[cellnum].innerHTML=tempval;
}
//***********************************************************************************
//set plaincell variable value based on whther the column being sorted upon has cells with input fields or has cells with plain text value******************************************************
/*if (document.getElementById(globobject).rows[1].cells[sortcol].getElementsByTagName("INPUT")[0])
{
plaincell="false";
}
else
{
plaincell="true";
}*/
//*********************************************************************************************
				if (sortswitch==0)
				{
				sortswitch=1;
				}
				else
				{
				sortswitch=0;
				}
				duptablenode=document.getElementById(obj).cloneNode(true);

				for (i=1;i<=document.getElementById(obj).rows.length-1;i++)
					{
					rowArray[i-1]=document.getElementById(obj).rows[i];
     				setplaincellvar(document.getElementById(obj).rows[i].cells[sortcol]);
					if (plaincell=="false")
					{
	if (document.getElementById(obj).rows[i].cells[sortcol].getElementsByTagName("INPUT")[0].getAttribute("isdate")=="false")
	{
	cellArray[i-1]=document.getElementById(obj).rows[i].cells[sortcol].getElementsByTagName("INPUT")[0].getAttribute("value")+"^"+(i-1);
	}
	else
	{
			if (document.getElementById(obj).rows[i].cells[sortcol].getAttribute("moddate"))
			{
			var moddate=document.getElementById(obj).rows[i].cells[sortcol].getAttribute("moddate");
document.getElementById(obj).rows[i].cells[sortcol].setAttribute("moddate",moddate);
			cellArray[i-1]=moddate+"^"+(i-1);
			}
			else
			{
			var moddate=document.getElementById(obj).rows[i].cells[sortcol].getElementsByTagName("INPUT")[0].getAttribute("format")+"isdate";
document.getElementById(obj).rows[i].cells[sortcol].setAttribute("moddate",moddate);
			cellArray[i-1]=moddate+"^"+(i-1);
			}
	}
					}
					else
					{
if (document.getElementById(obj).rows[i].cells[sortcol].getAttribute("isdate")=="false")
{				
				if (document.getElementById(obj).rows[i].cells[sortcol].lastChild.nodeType==1)
				{
cellArray[i-1]=document.getElementById(obj).rows[i].cells[sortcol].lastChild.innerHTML+"^"+(i-1);	
				}
				else
				{
cellArray[i-1]=document.getElementById(obj).rows[i].cells[sortcol].innerHTML+"^"+(i-1);		
				}
}
else
{
			if (document.getElementById(obj).rows[i].cells[sortcol].getAttribute("moddate"))
			{
			var moddate=document.getElementById(obj).rows[i].cells[sortcol].getAttribute("moddate");
document.getElementById(obj).rows[i].cells[sortcol].setAttribute("moddate",moddate);
			cellArray[i-1]=moddate+"^"+(i-1);
			}
			else
			{
			var moddate=document.getElementById(obj).rows[i].cells[sortcol].getAttribute("format")+"isdate";
document.getElementById(obj).rows[i].cells[sortcol].setAttribute("moddate",moddate);
			cellArray[i-1]=moddate+"^"+(i-1);
			}

}				
					}
					}
	//If sort function is called by groupby function then only ascending sort should take place**
	if (sortflag)
	{
	descending=false;
	}
	else
	{
						if (descending)
							descending = false;
							else
							descending = true;
	}
	//*******************************************************************************************
	
//storeArray acts like a lookup array holding reference to entire row object with reference to a column value***********************************************************************************						
				for (j=0;j<=rowArray.length-1;j++)
					{
					storeArray[cellArray[j]]=rowArray[j];
					}
//********************************************************************************************					
					cellArray.sort(compare);
					
//create anchor tag for title row**************************************************************
				for (g=0;g<=document.getElementById(obj).rows[0].cells.length-1;g++)
				{
					newanchor=document.createElement("A");
				//Add a image tags to all cells of first row *******************************
					tempimg=document.createElement("IMG");
					tempdiv=document.createElement("DIV");
					if (g==sortcol)
					{
					tempimg.setAttribute("src",arrowimage);
					tempimg.setAttribute("width","11");	
					tempimg.setAttribute("height","7");	
					duptablenode.rows[0].cells[g].style.backgroundColor=togglingbgcolor;
					}
					else
					{
					tempimg.setAttribute("src","/images/shim.gif");		
					tempimg.setAttribute("width","1");	
					tempimg.setAttribute("height","1");	
					duptablenode.rows[0].cells[g].style.backgroundColor=togglingbgothercolor;			
					}
					if (g==sortcol)
					{
					//duptablenode.rows[0].cells[g].style.backgroundColor=togglingbgcolor;
					}
					else
					{
					//duptablenode.rows[0].cells[g].style.backgroundColor=togglingbgothercolor;					
					}
					if (duptablenode.rows[0].cells[g].lastChild.nodeType==1)
					{
					newText=titleArray[g];
					}
					else
					{
					newText=document.createTextNode(titleArray[g]);
					}
					newanchor.setAttribute("href","javascript:sortTable('"+obj+"','"+g+"')");
					newanchor.appendChild(newText);
					
					if (duptablenode.rows[0].cells[g].lastChild.nodeName!="IMG")
					{
					for (v=0;v<=duptablenode.rows[0].cells[g].childNodes.length-1;v++)
					{
duptablenode.rows[0].cells[g].removeChild(duptablenode.rows[0].cells[g].childNodes[v]);					
					}
					duptablenode.rows[0].cells[g].appendChild(newanchor);	
//Put The arrow image to only that cell of the title row on which sort is being performed****
for (o=0;o<=duptablenode.rows[0].cells.length-1;o++)
{
if (duptablenode.rows[0].cells[o].getElementsByTagName("IMG")[0] && o!=sortcol)
{
duptablenode.rows[0].cells[o].getElementsByTagName("IMG")[0].removeAttribute("src");
}
}
if (g==sortcol)
{
duptablenode.rows[0].cells[g].appendChild(tempimg);
}
//*******************************************************************************************
					
	//				duptablenode.rows[0].cells[g].appendChild(tempimg);					
					}
					else
					{
		if (g==sortcol)
		{			
	//duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].src=arrowimage;
duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].setAttribute("src",arrowimage);
duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].setAttribute("width","11");	
duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].setAttribute("height","7");	
	    }
		else
		{
	//duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].src="/images/shim.gif";		
duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].setAttribute("src","/images/shim.gif");
duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].setAttribute("width","1");	
duptablenode.rows[0].cells[g].getElementsByTagName("IMG")[0].setAttribute("height","1");	
		}
					}
					//**************************************************************************					
				}
//**********************************************************************************************
			
				for(m=1;m<=document.getElementById(obj).rows.length-1;m++)
					{
temptrnode=storeArray[cellArray[m-1]].cloneNode(true);

//mergeAttributes method used below IS NOT SUPPORTED BY NETSCAPE6+*****************************
		if (IE4 || IE5)
		{
		//duptablenode.rows[m].replaceNode(temptrnode)
		//mergeAttributes has had problems with IE4 and IE5,so if you encounter error in IE4 OR IE5 then change the line below to call transferattributes function(uncomment the below lines).**
		duptablenode.rows[m].mergeAttributes(temptrnode);
		//**************************************************************************************
		
		//call the function which transfers attributes****************************************
		//transferattributes(storeArray[cellArray[m-1]],duptablenode.rows[m]);
		//************************************************************************************
		
		}
		else
		{
		//call the function which transfers attributes****************************************
		transferattributes(storeArray[cellArray[m-1]],duptablenode.rows[m]);
		//************************************************************************************
		}
//***********************************************************************************************
							for(n=0;n<=document.getElementById(obj).rows[m].cells.length-1;n++)
							 {
							 setplaincellvar(document.getElementById(obj).rows[m].cells[n]);
					if (plaincell=="false")
					{
if (duptablenode.rows[m].cells[n].getElementsByTagName("INPUT")[0].getAttribute("isdate")=="true")
{
duptablenode.rows[m].cells[n].getElementsByTagName("INPUT")[0].setAttribute("moddate",storeArray[cellArray[m-1]].cells[n].getElementsByTagName("INPUT")[0].getAttribute("moddate"));					
duptablenode.rows[m].cells[n].getElementsByTagName("INPUT")[0].setAttribute("value",storeArray[cellArray[m-1]].cells[n].getElementsByTagName("INPUT")[0].getAttribute("value"));
duptablenode.rows[m].cells[n].getElementsByTagName("INPUT")[0].setAttribute("format",storeArray[cellArray[m-1]].cells[n].getElementsByTagName("INPUT")[0].getAttribute("format"));					
}
else
{
temptdnode=storeArray[cellArray[m-1]].cells[n].cloneNode(true);
duptablenode.rows[m].replaceChild(temptdnode,duptablenode.rows[m].cells[n]);
}
					}
					else
					{
				if (document.getElementById(obj).rows[m].cells[n].lastChild.nodeType==1)
				{
temptdnode1=storeArray[cellArray[m-1]].cells[n].cloneNode(true);
duptablenode.rows[m].replaceChild(temptdnode1,duptablenode.rows[m].cells[n]);
duptablenode.rows[m].cells[n].lastChild.innerHTML=storeArray[cellArray[m-1]].cells[n].lastChild.innerHTML;
				}
				else
				{
temptdnode2=storeArray[cellArray[m-1]].cells[n].cloneNode(true);
duptablenode.rows[m].replaceChild(temptdnode2,duptablenode.rows[m].cells[n]);
duptablenode.rows[m].cells[n].lastChild.nodeValue=storeArray[cellArray[m-1]].cells[n].innerHTML;
				}
					 }
							 }
					}
document.getElementById("tdholdingtables").replaceChild(duptablenode,document.getElementById(obj));

if ( sortflag != "true" )
{
//temptitlerownode=duptablenode.rows[0].cloneNode(true);
}
if ( sortswitch == 1 )
{
arrowimage="/images/sdown.gif";
}
else
{
arrowimage="/images/sup.gif";
}

}

//The following function gets executed whe any value is changed in any oif the input fields but the function is written to handle date field cases.
function checkdatechange(inpobj)
	{
	if (inpobj.getAttribute("isdate")=="true")
	{
	inpobj.setAttribute("format",inpobj.value);
	inpobj.setAttribute("value",inpobj.value)
	}
	else
	{
	inpobj.setAttribute("value",inpobj.value)	
	}
}

function transferattributes(from,to)
{
if (N6)
{
	for(t=0;t<=to.attributes.length-1;t++)
	{
	to.removeAttribute(to.attributes[t].nodeName);
	}
}
	for(h=0;h<=from.attributes.length-1;h++)
	{
		if (from.attributes[h].specified)
		 {
			to.setAttribute(from.attributes[h].nodeName,from.attributes[h].nodeValue);
         }
		 else
		 {
			to.setAttribute(from.attributes[h].nodeName,"");		 
		 }
	}
}
//*****************************************************************************
// Function to be used for Array sorting
//*****************************************************************************
function compare(a, b)
{
	// Getting the element array for inputs (a,b)
	var aRowContent = a;
	var bRowContent = b;
	// Needed in case the data conversion is necessary
	var aToBeCompared, bToBeCompared;
	aToBeCompared=aRowContent;
	aToBeCompared=aToBeCompared;
	bToBeCompared=bToBeCompared;
	bToBeCompared=bRowContent;
	if (aToBeCompared.substring(10,16)=="isdate")
	{
		//For Sorting date fields*****************************************
	datea=aToBeCompared.substring(0,10);
	dateb=bToBeCompared.substring(0,10);
	var yeara = 1 * datea.substring(6,10);
	var yearb = 1 * dateb.substring(6,10);
	if (yeara > yearb) if (!descending){return 1;}else{return -1;};
	if (yeara < yearb) if (!descending){return -1;}else{return 1;};
	//return 0;

	
	var montha = 1 * datea.substring(0,2);
	var monthb = 1 * dateb.substring(0,2);
	if (montha > monthb) if (!descending){return 1;}else{return -1;};
	if (montha < monthb) if (!descending){return -1;}else{return 1;};
	
	var daya = 1 * datea.substring(3,5);
	var dayb = 1 * dateb.substring(3,5);
	if (daya > dayb) if (!descending){return 1;}else{return -1;};
	 if (!descending){return -1;}else{return 1;};
		//*************************************************************************
	}
	
	//For sorting numbers**********
	//if ((parseFloat(aToBeCompared)>=0 && parseFloat(aToBeCompared)<=10000) && (parseFloat(bToBeCompared)>=0 && parseFloat(bToBeCompared)<=10000))
if (typeof(eval(parseFloat(aToBeCompared)))=="number" && typeof(eval(parseFloat(bToBeCompared)))=="number")	
{
		//if (parseFloat(aToBeCompared) < parseFloat(bToBeCompared))
		if (eval(parseFloat(aToBeCompared)) < eval(parseFloat(bToBeCompared)))
		if (!descending)
		{
			return -1;
		}
		else
		{
			return 1;
		}
	//if (parseFloat(aToBeCompared) > parseFloat(bToBeCompared))
	if (eval(parseFloat(aToBeCompared)) > eval(parseFloat(bToBeCompared)))
		if (!descending)
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}	
	//*******************************
	
	if (! isNaN(aRowContent))
		aToBeCompared = parseInt(aRowContent, 10);
	else
		aToBeCompared = aToBeCompared ;

	if (! isNaN(bRowContent))
		bToBeCompared = parseInt(bRowContent, 10);
	else
		bToBeCompared = bToBeCompared;

	if (aToBeCompared < bToBeCompared)
		if (!descending)
		{
			return -1;
		}
		else
		{
			return 1;
		}
	if (aToBeCompared > bToBeCompared)
		if (!descending)
		{
			return 1;
		}
		else
		{
			return -1;
		}
	return 0;
}


function setplaincellvar(cellobj)
{
if (cellobj.getElementsByTagName("INPUT")[0])
{
plaincell="false";
}
else
{
plaincell="true";
}
}	