//Function Used to Invoke Excel and Download the Data.

function downloadToExcel(thecontent){	
	var bodyHTML;
	var contentHTML;	
	var ocontentItem = document.all(thecontent);	
	if (!ocontentItem){
		alert('Download content for this report not completely defined. Please Contact the Admin Group');
		return;
	}
	//alert("Please wait while the document being downloaded.\nClick YES if you get the next message \nfor allowing Activex interaction on this page.");

	//Save the complete content to be restored once clipboard is copied with the select download content	
		bodyHTML = document.body.innerHTML;
	
	//Compose the initial download content with all href tags built in	

		contentHTML =  ocontentItem.innerHTML;		
				     
	//clean the content using custom Javascript function to remove 
	//all the anchor tags but retain their text elements as Labels

		contentHTML = cleanhtml(contentHTML,'<a');					
		contentHTML = cleanhtml(contentHTML,'</a>');
		contentHTML = cleanhtml(contentHTML,'<img');					
		contentHTML = cleanhtml(contentHTML,'</img>');
		
	document.body.innerHTML = contentHTML;
	
//	document.body.style.cursor = 'wait'
	
	this.document.execCommand("SelectAll", false);
	this.document.execCommand("Copy", true);     
	this.document.execCommand("UnSelect", false);   
	
	document.body.innerHTML = bodyHTML;
					
	// Start Excel and get Application object.   
	  
	try {
		var oXL = new ActiveXObject("Excel.Application");     
		document.body.style.cursor = 'default';
		oXL.Visible = true;   
		
		// Get a new workbook.     
		var oWB = oXL.Workbooks.Add();     
		
		//set the object to the active sheet
		var oSheet = oWB.ActiveSheet; 


   			var d, s="";
		   d = new Date();
		   s += (d.getMonth() + 1) + "-";
		   s += d.getDate() + "-";
		   s += d.getYear();
   
			oSheet.Name = s;
		//copy the contents of the clipboard to the active sheet
	

			oSheet.Paste(); 		
			oXL.Visible = true;
			oXL.UserControl = true;	
	}

		catch(err)	{
			alert("Cannot Invoke Excel Application. Please refresh the browser and download again.");
		}
		document.body.style.cursor = 'default';
}


//note: The following function is a recursive function and exits after all the instances of the tag are removed
//	from then on it unwinds the stack of calls to this function


function cleanhtml(content,ConstantPortionofTag)
{
	var temp =  content ;
	var tag = ConstantPortionofTag;	
	var k; 

	tag=tag.toUpperCase();
	var stringfirst;
	var stringend;			
	
	var i = temp.toUpperCase().indexOf(tag);					
	if ( i == '-1')	{
		return content;
	}		
	stringfirst= content.substring(0,i);		
	j = content.indexOf('>',i+1) + 1;		
	if (j == content.length){
		j = j - 1;
	}
	stringend = content.substring(j,content.length);
	content = stringfirst + stringend;	
	content = cleanhtml(content,tag);		
	return content;

}

