/* This Function will sort string arrays*/
var cur_val,compare_val,temp;
var lastline = 0;
function sortarrays(arrayname)
{
	for (i=0; i<arrayname.length;i++)
	{
		
		cur_val = arrayname[i];
		cur_val_temp = arrayname[i].toLowerCase();
		cur_order_val = order_array_temp[i];
		for (j=i; j<arrayname.length;j++)
		{
			compare_val = arrayname[j];
			compare_val_temp = arrayname[j].toLowerCase();
			compare_order_val = order_array_temp[j];
			if(cur_val_temp < compare_val_temp)
			{
				arrayname[i]=compare_val;
				arrayname[j]=cur_val;
				order_array_temp[i] = compare_order_val;
				order_array_temp[j] = cur_order_val;
				cur_val = arrayname[i];
				cur_val_temp = arrayname[i].toLowerCase();
				cur_order_val =order_array_temp[i];
			}
		}
	}
		//The above function always return ascending order. So change the order based on requirement

		if(direction == "asc")
		{
			arrayname.reverse();
			order_array_temp.reverse();
			direction = "desc";
		}
		else
		{
			direction = "asc";
		}
		
}
// This Function will sort Date arrays having "DD Mon YYYY" format 


function sortDatearrays(arrayname)
{
	for (i=0; i<arrayname.length;i++)
	{
		cur_val = arrayname[i];
		cur_val_temp = convertAsDate(cur_val);
		cur_order_val = order_array_temp[i];
		for (j=i; j<arrayname.length;j++)
		{
			compare_val = arrayname[j];
			compare_val_temp = convertAsDate(compare_val);
			compare_order_val = order_array_temp[j];
			if(cur_val_temp < compare_val_temp)
			{
				arrayname[i]=compare_val;
				arrayname[j]=cur_val;
				order_array_temp[i] = compare_order_val;
				order_array_temp[j] = cur_order_val;
				cur_val = arrayname[i];
				cur_val_temp = convertAsDate(arrayname[i]);
				cur_order_val =order_array_temp[i];
			}
		}
	}
	//The above function always return ascending order. So change the order based on requirement

		if(direction == "asc")
		{
			arrayname.reverse();
			order_array_temp.reverse();
			direction = "desc";
		}
		else
		{
			direction = "asc";
		}
}

	// This is a Sub Function used to convert the "DD Mon YYYY" format into 
	//standard GMT format in order make date comparison easier

function convertAsDate(dat)
{
	if(dat) {
		new_dt = new Date(dat);
		return new_dt;
	}
}

//Base function which handles sorting 

function baseSort(sorttype)
{
	
	
	if(basetype == 'First')
	{
		basetype = sorttype;
		direction = "desc";
		//Re-initialise the arrays when the sorting type is once changed
		intialize();
	}
	else if(basetype != sorttype)
	{
		basetype = sorttype;  
		direction = "asc";
		//Re-initialise the arrays when the sorting type is once changed
		intialize();
	}
	if(direction == "asc")
	{
		image = "/images/common/btn_grnarrows_up.gif";
	}
	else if(direction == "desc")
	{
		image = "/images/common/btn_grnarrows_down.gif";
	}
	if(sorttype=='DN')
	{
		document.getElementById("decinum").style.textDecoration = "underline";
		document.getElementById("announdate").style.textDecoration = "none";
		sortarrays(decisionnumber_array_temp);
		selectedcasetype = tempselectedcasetype;
		selectedpositiontype = tempselectedpositiontype; 
		buildTable(decisionnumber_array,announcedate_array,name_array,casetype_array,positiontype_array,order_array_temp,DBMarket_array);
		document.numberimage.src = image;
	}
	else if(sorttype=='AD')
	{
		
		document.getElementById("announdate").style.textDecoration = "underline";
		document.getElementById("decinum").style.textDecoration = "none";
		sortDatearrays(announcedate_array_temp);
		selectedcasetype = tempselectedcasetype;
		selectedpositiontype = tempselectedpositiontype; 
		buildTable(decisionnumber_array,announcedate_array,name_array,casetype_array,positiontype_array,order_array_temp,DBMarket_array);
		document.dateimage.src = image;
		
	}
/*	else if(sorttype=='N')
	{
		sortarrays(name_array_temp);
		buildTable(decisionnumber_array,announcedate_array,name_array,casetype_array,order_array_temp);
		document.nameimage.src = image;
	} */ 
} 

// This Function gets the selected casetype and calls the base casetype filter function

function getCaseType(selectedval)
{
	document.getElementById("positiontype").selectedIndex=0;
	document.getElementById("decinum").style.textDecoration = "underline";
	document.getElementById("announdate").style.textDecoration = "none";
	selectedcasetype = selectedval;
	buildTable(decisionnumber_array,announcedate_array,name_array,casetype_array,positiontype_array,order_array_temp,DBMarket_array);
}

function getPositionType(selectedvalpos)
{
	document.getElementById("casetype").selectedIndex=0;
	document.getElementById("decinum").style.textDecoration = "underline";
	document.getElementById("announdate").style.textDecoration = "none";
	selectedpositiontype = selectedvalpos;
	buildTable(decisionnumber_array,announcedate_array,name_array,casetype_array,positiontype_array,order_array_temp,DBMarket_array);
}
 

//	This Function will clear the table element inside Div tag before ynamically generating the Table

	function clearTbody() 
	{
		reqDivElem = document.getElementById("maintablediv");
			while (reqDivElem.childNodes.length > 0) 
			{
				reqDivElem.removeChild(reqDivElem.firstChild);
			}
	}


	//This Function will dynamically generate the Table

	
	function buildTable(decnumarr,anndatearr,namearr,casetypearr,positiontypearr,orderarr,marketarr) 
	{
					
		tempselectedcasetype = selectedcasetype;
		tempselectedpositiontype = selectedpositiontype; 
		clearTbody();
		mydiv = document.getElementById("maintablediv");
		mytable = document.createElement("TABLE");
		mytable.setAttribute("border","0");
		mytable.setAttribute("cellPadding","0");
		mytable.setAttribute("cellSpacing","0");
		mytable.setAttribute("width","619");
		mytablebody = document.createElement("TBODY");
		var flag_row_exists = false;
	//Code for Market Check -- NYSE
			if(market=="nyse" || market=="altus")
			{	
	
				if(selectedcasetype != 'All')
				{
					selectedpositiontype = 'All';
					
					for(jj=0;jj<orderarr.length;jj++) 
					{
						
						mycurrent_row=document.createElement("TR");
						mycurrent_row.setAttribute("vAlign","top")
						var appendRow = false;
						for(icount=0;icount<5;icount++)
						{
								if(selectedcasetype == casetypearr[orderarr[jj]])
								{
									flag_row_exists = true;
									mycurrent_cell=document.createElement("TD");
									if( jj == orderarr.length-1 )
									{
										mycurrent_cell.className="gratop2bot";
									}
									else
									{
										mycurrent_cell.className="gradtop";
									}
									if(icount==0)
									{
										text = decnumarr[orderarr[jj]];

										var strname = text;
										var number,url;
										til_index = strname.indexOf("#~~#");
										if(til_index != -1)
										{
											number = strname.substr(0, til_index);
											url = strname.substr(til_index+4);
										}
										else
										{
											number = strname;
											url = "#";
										}
										mycurrent_cell.setAttribute("width","70");
										
										my_anchor=document.createElement("a");
										my_anchor.setAttribute("href", url);
										currenttext=document.createTextNode(number);
										my_anchor.appendChild(currenttext);
										mycurrent_cell.appendChild(my_anchor);
									}
									if(icount==1) 
									{
										text = anndatearr[orderarr[jj]];

										if(text==null || text.length <1)
										{
											text = "\u00a0";
										}
										mycurrent_cell.setAttribute("width","90"); 
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									} 
									if(icount==2) 
									{
										text = namearr[orderarr[jj]];

										mycurrent_cell.setAttribute("width","170");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==3) {
										text = positiontypearr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","135");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==4) 
									{
										text = casetypearr[orderarr[jj]];
										
										mycurrent_cell.setAttribute("width","150");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);  
									}
									mycurrent_row.appendChild(mycurrent_cell);
									appendRow = true;
								}
							 
						}
						if(appendRow)
							mytablebody.appendChild(mycurrent_row);
					} 
				}				
				if(selectedpositiontype != 'All')
				{
					selectedcasetype = 'All'; 
				
					for(jj=0;jj<orderarr.length;jj++) 
					{
						
						mycurrent_row=document.createElement("TR");
						mycurrent_row.setAttribute("vAlign","top")
						var appendRow = false;
						for(icount=0;icount<5;icount++)
						{
								if(selectedpositiontype == positiontypearr[orderarr[jj]])
								{
									flag_row_exists = true;
									mycurrent_cell=document.createElement("TD");
									if( jj == orderarr.length-1 )
									{
										mycurrent_cell.className="gratop2bot";
									}
									else
									{
										mycurrent_cell.className="gradtop";
									}
									if(icount==0)
									{
										text = decnumarr[orderarr[jj]];

										var strname = text;
										var number,url;
										til_index = strname.indexOf("#~~#");
										if(til_index != -1)
										{
											number = strname.substr(0, til_index);
											url = strname.substr(til_index+4);
										}
										else
										{
											number = strname;
											url = "#";
										}
										mycurrent_cell.setAttribute("width","70");
										
										my_anchor=document.createElement("a");
										my_anchor.setAttribute("href", url);
										currenttext=document.createTextNode(number);
										my_anchor.appendChild(currenttext);
										mycurrent_cell.appendChild(my_anchor);
									}
									if(icount==1) 
									{
										text = anndatearr[orderarr[jj]];

										if(text==null || text.length <1)
										{
											text = "\u00a0";
										}
										mycurrent_cell.setAttribute("width","90"); 
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									} 
									if(icount==2) 
									{
										text = namearr[orderarr[jj]];

										mycurrent_cell.setAttribute("width","170");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==3) {
										text = positiontypearr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","135");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==4) 
									{
										text = casetypearr[orderarr[jj]];
										
										mycurrent_cell.setAttribute("width","150");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);  
									}
									mycurrent_row.appendChild(mycurrent_cell);
									appendRow = true;
								}
							 
						}
						if(appendRow)
							mytablebody.appendChild(mycurrent_row);
					} 
				}			
				if(selectedcasetype == 'All' && selectedpositiontype == 'All')
				{
					
					for(jj=0;jj<orderarr.length;jj++) 
					{
						
						mycurrent_row=document.createElement("TR");
						mycurrent_row.setAttribute("vAlign","top")
						var appendRow = false;
						for(icount=0;icount<6;icount++)
						{
		//						if(selectedpositiontype == positiontypearr[orderarr[jj]].substring(0,positiontypearr[orderarr[jj]].indexOf(' ')))
								{
									flag_row_exists = true;
									mycurrent_cell=document.createElement("TD");
									if( jj == orderarr.length-1 )
									{
										mycurrent_cell.className="gratop2bot";
									} 
									else
									{
										mycurrent_cell.className="gradtop";
									}
									if(icount==0)
									{
										text = decnumarr[orderarr[jj]];

										var strname = text;
										var number,url;
										til_index = strname.indexOf("#~~#");
										if(til_index != -1)
										{
											number = strname.substr(0, til_index);
											url = strname.substr(til_index+4);
										}
										else
										{
											number = strname;
											url = "#";
										}
										mycurrent_cell.setAttribute("width","70");
										
										my_anchor=document.createElement("a");
										my_anchor.setAttribute("href", url);
										currenttext=document.createTextNode(number);
										my_anchor.appendChild(currenttext);
										mycurrent_cell.appendChild(my_anchor);
									}
									if(icount==1) 
									{
										text = anndatearr[orderarr[jj]];

										if(text==null || text.length <1)
										{
											text = "\u00a0";
										}
										mycurrent_cell.setAttribute("width","90"); 
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									} 
									if(icount==2) 
									{
										text = namearr[orderarr[jj]];

										mycurrent_cell.setAttribute("width","170");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==3) {
										text = positiontypearr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","135");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==4) 
									{
										text = casetypearr[orderarr[jj]];
										
										mycurrent_cell.setAttribute("width","150");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);  
									}
									mycurrent_row.appendChild(mycurrent_cell);
									appendRow = true;
								}
							 
						}
						if(appendRow)
							mytablebody.appendChild(mycurrent_row);
					} 
				}
				if(selectedcasetype != 'All')
				   selectedcasetype = 'All';

				if(selectedpositiontype != 'All')
				   selectedpositiontype = 'All';


				if(!flag_row_exists) //adding no-records-found row if there are no records found
				{
					no_rec_row = document.createElement("TR");
					no_rec_cell=document.createElement("TD");
					no_rec_cell.setAttribute("width","619");
					no_rec_cell.setAttribute("align","center");
					no_rec_cell.className = "gradtop";
					no_rec_text=document.createTextNode("There are no Disciplinary Actions at this time");
					no_rec_cell.appendChild(no_rec_text);
					no_rec_row.appendChild(no_rec_cell);
					mytablebody.appendChild(no_rec_row);
				}
				else
				{
					if(jj < orderarr.length-1)
					{
						lastline = 2;		
					}
				}
		}
		else //Code added for Market -- arca
		{
				if(selectedcasetype != 'All')
				{
					selectedpositiontype = 'All';
					
					for(jj=0;jj<orderarr.length;jj++) 
					{
						
						mycurrent_row=document.createElement("TR");
						mycurrent_row.setAttribute("vAlign","top")
						var appendRow = false;
						for(icount=0;icount<6;icount++)
						{
								if(selectedcasetype == casetypearr[orderarr[jj]])
								{
									flag_row_exists = true;
									mycurrent_cell=document.createElement("TD");
									if( jj == orderarr.length-1 )
									{
										mycurrent_cell.className="gratop2bot";
									}
									else
									{
										mycurrent_cell.className="gradtop";
									}
									if(icount==0)
									{
										text = decnumarr[orderarr[jj]];

										var strname = text;
										var number,url;
										til_index = strname.indexOf("#~~#");
										if(til_index != -1)
										{
											number = strname.substr(0, til_index);
											url = strname.substr(til_index+4);
										}
										else
										{
											number = strname;
											url = "#";
										}
										mycurrent_cell.setAttribute("width","70");
										
										my_anchor=document.createElement("a");
										my_anchor.setAttribute("href", url);
										currenttext=document.createTextNode(number);
										my_anchor.appendChild(currenttext);
										mycurrent_cell.appendChild(my_anchor);
									}
									if(icount==1) 
									{
										text = anndatearr[orderarr[jj]];

										if(text==null || text.length <1)
										{
											text = "\u00a0";
										}
										mycurrent_cell.setAttribute("width","90"); 
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									} 
									if(icount==2) 
									{
										text = namearr[orderarr[jj]];

										mycurrent_cell.setAttribute("width","120");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==3) {
										text = marketarr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","45");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==4) {
										text = positiontypearr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","135");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==5) 
									{
										text = casetypearr[orderarr[jj]];
										
										mycurrent_cell.setAttribute("width","150");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);  
									}
									mycurrent_row.appendChild(mycurrent_cell);
									appendRow = true;
								}
							 
						}
						if(appendRow)
							mytablebody.appendChild(mycurrent_row);
					} 
				}				
				if(selectedpositiontype != 'All')
				{
					selectedcasetype = 'All'; 
				
					for(jj=0;jj<orderarr.length;jj++) 
					{
						
						mycurrent_row=document.createElement("TR");
						mycurrent_row.setAttribute("vAlign","top")
						var appendRow = false;
						for(icount=0;icount<6;icount++)
						{
								if(selectedpositiontype == positiontypearr[orderarr[jj]])
								{
									flag_row_exists = true;
									mycurrent_cell=document.createElement("TD");
									if( jj == orderarr.length-1 )
									{
										mycurrent_cell.className="gratop2bot";
									}
									else
									{
										mycurrent_cell.className="gradtop";
									}
									if(icount==0)
									{
										text = decnumarr[orderarr[jj]];

										var strname = text;
										var number,url;
										til_index = strname.indexOf("#~~#");
										if(til_index != -1)
										{
											number = strname.substr(0, til_index);
											url = strname.substr(til_index+4);
										}
										else
										{
											number = strname;
											url = "#";
										}
										mycurrent_cell.setAttribute("width","70");
										
										my_anchor=document.createElement("a");
										my_anchor.setAttribute("href", url);
										currenttext=document.createTextNode(number);
										my_anchor.appendChild(currenttext);
										mycurrent_cell.appendChild(my_anchor);
									}
									if(icount==1) 
									{
										text = anndatearr[orderarr[jj]];

										if(text==null || text.length <1)
										{
											text = "\u00a0";
										}
										mycurrent_cell.setAttribute("width","90"); 
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									} 
									if(icount==2) 
									{
										text = namearr[orderarr[jj]];

										mycurrent_cell.setAttribute("width","120");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==3) {
										text = marketarr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","45");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==4) {
										text = positiontypearr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","135");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==5) 
									{
										text = casetypearr[orderarr[jj]];
										
										mycurrent_cell.setAttribute("width","150");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);  
									}
									mycurrent_row.appendChild(mycurrent_cell);
									appendRow = true;
								}
							 
						}
						if(appendRow)
							mytablebody.appendChild(mycurrent_row);
					} 
				}			
				if(selectedcasetype == 'All' && selectedpositiontype == 'All')
				{
					
					for(jj=0;jj<orderarr.length;jj++) 
					{
						
						mycurrent_row=document.createElement("TR");
						mycurrent_row.setAttribute("vAlign","top")
						var appendRow = false;
						for(icount=0;icount<6;icount++)
						{
		//						if(selectedpositiontype == positiontypearr[orderarr[jj]].substring(0,positiontypearr[orderarr[jj]].indexOf(' ')))
								{
									flag_row_exists = true;
									mycurrent_cell=document.createElement("TD");
									if( jj == orderarr.length-1 )
									{
										mycurrent_cell.className="gratop2bot";
									} 
									else
									{
										mycurrent_cell.className="gradtop";
									}
									if(icount==0)
									{
										text = decnumarr[orderarr[jj]];

										var strname = text;
										var number,url;
										til_index = strname.indexOf("#~~#");
										if(til_index != -1)
										{
											number = strname.substr(0, til_index);
											url = strname.substr(til_index+4);
										}
										else
										{
											number = strname;
											url = "#";
										}
										mycurrent_cell.setAttribute("width","70");
										
										my_anchor=document.createElement("a");
										my_anchor.setAttribute("href", url);
										currenttext=document.createTextNode(number);
										my_anchor.appendChild(currenttext);
										mycurrent_cell.appendChild(my_anchor);
									}
									if(icount==1) 
									{
										text = anndatearr[orderarr[jj]];

										if(text==null || text.length <1)
										{
											text = "\u00a0";
										}
										mycurrent_cell.setAttribute("width","90"); 
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									} 
									if(icount==2) 
									{
										text = namearr[orderarr[jj]];

										mycurrent_cell.setAttribute("width","120");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==3) {
										text = marketarr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","45");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==4) {
										text = positiontypearr[orderarr[jj]];
										mycurrent_cell.setAttribute("width","135");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);
									}
									if(icount==5) 
									{
										text = casetypearr[orderarr[jj]];
										
										mycurrent_cell.setAttribute("width","150");
										currenttext=document.createTextNode(text);
										mycurrent_cell.appendChild(currenttext);  
									}
									mycurrent_row.appendChild(mycurrent_cell);
									appendRow = true;
								}
			
						}
						if(appendRow)
							mytablebody.appendChild(mycurrent_row);
					} 
				}
				if(selectedcasetype != 'All')
				   selectedcasetype = 'All';

				if(selectedpositiontype != 'All')
				   selectedpositiontype = 'All';


				if(!flag_row_exists) //adding no-records-found row if there are no records found
				{
					no_rec_row = document.createElement("TR");
					no_rec_cell=document.createElement("TD");
					no_rec_cell.setAttribute("width","619");
					no_rec_cell.setAttribute("align","center");
					no_rec_cell.className = "gradtop";
					no_rec_text=document.createTextNode("There are no Disciplinary Actions at this time");
					no_rec_cell.appendChild(no_rec_text);
					no_rec_row.appendChild(no_rec_cell);
					mytablebody.appendChild(no_rec_row);
				}
				else
				{
					if(jj < orderarr.length-1)
					{
						lastline = 1;		
					}
				}
		}//Code for Market -- ARCA end here
		mytable.appendChild(mytablebody);
		mydiv.appendChild(mytable);  
    }


baseSort('DN');