
var selectedmdfidseries;
var selectedmdfidmodel;

var totalMatched = 0;
var scrollHeight = 0;

function openMDFTree()
{
mdfTree.openAll();
}

function closeMDFTree(){
mdfTree.closeAll();
}

function openMDFTreeToId(thisId)
{
closeMDFTree();
mdfTree.openToSingle(thisId,true,false);

}
function searchFromButton(thisVal)
{
searchTree(thisVal);

return false;
}

function searchFromText(thisVal)
{




searchTree(thisVal);
return false;
}


function searchTree(thisVal) {


	thisVal=thisVal.replace(/^\s+|\s+$/g,'');
	
	thisVal=thisVal.replace(/\(/g,'\\(');
	thisVal=thisVal.replace(/\)/g,'\\)');
	thisVal=thisVal.replace(/\[/g,'\\[');
	thisVal=thisVal.replace(/\]/g,'\\]');

	thisVal=thisVal.replace(/\{/g,'\\{');
	thisVal=thisVal.replace(/\}/g,'\\}');
	thisVal=thisVal.replace(/\./g,'\\.');

	if((thisVal==null)||(thisVal==""))
	return false;

	//closeMDFTree();


	//thisVal.replace(/\s*/g,'');

	var re=new RegExp(thisVal.toUpperCase());

	var compareValue=null;
	scroll(0,0);
	
	var firstMatch = true;
	var setArray = {};
	var closeClearFlag = false;
	var l = 0;

	//Changes done for tactical 7769. Clearhighlighting everytime the search is done.
	clearHighLighting();
	
	for(var j=0;j<mdfTree.aNodes.length;j++)	{

		compareValue=mdfTree.aNodes[j].name.toUpperCase();
	
		if(thisVal.indexOf('(')==-1){
			compareValue=compareValue.replace(/\(/g,'\\(');
		}
		if(thisVal.indexOf(')')==-1){
			compareValue=compareValue.replace(/\)/g,'\\)');
		}
		if(thisVal.indexOf('[')==-1){
			compareValue=compareValue.replace(/\[/g,'\\[');
		}
		if(thisVal.indexOf(']')==-1){
			compareValue=compareValue.replace(/\]/g,'\\]');
		}
		if(thisVal.indexOf('{')==-1){
			compareValue=compareValue.replace(/\{/g,'\\{');
		}
		if(thisVal.indexOf('}')==-1){
			compareValue=compareValue.replace(/\}/g,'\\}');
		}
		if(thisVal.indexOf('.')==-1){
			compareValue=compareValue.replace(/\./g,'\\.');
		}

		if(null!=re.exec(compareValue.toUpperCase())) {
			if(!closeClearFlag) {
				closeMDFTree();
				clearHighLighting();
				closeClearFlag = true;
			}
			mdfTree.openToSingle_Updated(mdfTree.aNodes[j].id,true,false,mdfTree.aNodes[j].pid);
			
			var ic=mdfTree.aNodes[j].icon;
			var isMatched=ic.match(/page/g);
			if(firstMatch) {
			if(isMatched){
				var selectedDivId ='smdfTree'+j;
				//scrollHeight=document.getElementById(selectedDivId).offsetTop
				scrollHeight = (getHeight(document.getElementById(selectedDivId))-40);
				firstMatch=false;
			}else{
				var matchDivId ='dmdfTree'+j;
				if(document.getElementById(matchDivId)) {
				//scrollHeight=document.getElementById(matchDivId).offsetTop
				scrollHeight = getHeight(document.getElementById(matchDivId))-40;
				firstMatch=false;
				}
				
		   	}
			} 

			 for(var m=0;m<mdfTree.aNodes.length;m++)	{
				var cn = null;
				var id1 = null;
				if(isMatched){
					cn = mdfTree.aNodes[m];
					id1 = m;
				}else{
					cn=mdfTree.aNodes[j];
					id1 = j;
				}
				if((mdfTree.aNodes[m].pid==mdfTree.aNodes[j].id ) || (mdfTree.aNodes[m].id==mdfTree.aNodes[j].pid )){	
					
					while(cn){							
						mdfTree.openToSingle_Updated(cn.id,true,false,cn.pid);
						
						cn=mdfTree.getParentNode(cn.pid);											 
						if(l>=50)
							break;
					}
					var ic1=mdfTree.aNodes[m].icon;
					var isMatched1=ic1.match(/page/g);
					if(mdfTree.aNodes[m].pid!=-1) {					
						 if((isMatched1!=null && mdfTree.aNodes[m].url!=null)) {
							setArray[""+m] = 1;
							l=l+1;						
						}
					}

					var ic2=mdfTree.aNodes[j].icon;
					var isMatched2=ic2.match(/page/g);
					if(mdfTree.aNodes[j].pid!=-1) {					
						 if((isMatched2!=null && mdfTree.aNodes[j].url!=null)) {
							setArray[""+j] = 1;
							l=l+1;						
						}
					}

				}
					if(l>=50)							
						break;
			}

				if(l>=50)							
					break;
		}
	}
	
	mdfTree.searchResults=l;	
	if(l<50 && l>0){
	  totalMatched = setClassName(setArray);
	  //scrolltoSelection1(scrollHeight); 
	}	

	return false;
}


function setClassName(arr2) {
  var count = 0;

  	var  arr1 =     document.getElementsByTagName("A");	
 	for(var p=0;p<arr1.length;p++){
  		for(arr2Key in arr2) {
	       var k = arr2Key;
	       var g = 'smdfTree'+k;
           if(arr1[p].id == g) {
	           var eNew = arr1[p];										
	           if(eNew!=null) {
	               eNew.className = "nodeSel";
	           }
	       
	           if(eNew.className=="nodeSel") {
	           	 count++;
	           }									
           } 	
  		}
  	}

  return count;
}

function hightLightTreeSeries(thisVal, opt)
{

if(document.getElementById("totResults") &&  opt!=1){
        var div1=document.getElementById("totResults");
        div1.style.display='none';
}

closeMDFTree();

var isRoot='0';
var isMatch='0';
var firstoccurance=false;

for(var k=0;k<mdfTree.aNodes.length;k++)
{
if(thisVal==mdfTree.aNodes[k].id)
{
if(mdfTree.aNodes[k].name.toUpperCase()=='1AAAROOT')
{
isRoot=1;
break;
}
}
}
if(isRoot==0)
{
for(var j=0;j<mdfTree.aNodes.length;j++)
{
if(thisVal==mdfTree.aNodes[j].pid)
{
isMatch=1;
mdfTree.openToSingle(mdfTree.aNodes[j].id,true,false);
if(!firstoccurance){
selectedmdfidseries="s"+"mdfTree"+j
firstoccurance=true;
}
window.setTimeout('scrolltoSelection(selectedmdfidseries)',1);
}
}
}
if(isRoot==0&&isMatch==0)
{
for(var j=0;j<mdfTree.aNodes.length;j++)
{
if(thisVal==mdfTree.aNodes[j].id)
{
mdfTree.openToSingle_Updated(mdfTree.aNodes[j].id,true,false,mdfTree.aNodes[j].pid);
break;
}
}
}


}
function hightLightTreeModel(thisVal)
{
if(document.getElementById("totResults")){
	var div1=document.getElementById("totResults");
	div1.style.display='none';
}
closeMDFTree();
var arr=document.getElementsByTagName("A");
for(var j=0;j<mdfTree.aNodes.length;j++)
{


if(thisVal==mdfTree.aNodes[j].id)
{
mdfTree.openToSingle(mdfTree.aNodes[j].id,true,false);
selectedmdfidmodel="s"+"mdfTree"+j;
window.setTimeout('scrolltoSelection(selectedmdfidmodel)',1);
break;
}

}
}
function hightLightTreeORG(thisVal)
{
var cn;
var nodeid;
var parentId=thisVal;
for(var j=0;j<mdfTree.aNodes.length;j++)
{
if(parentId=mdfTree.aNodes[j].pid)
{
mdfTree.openToSingle(mdfTree.aNodes[j].id,true,false);
}
}
}

function highLightTreeModel(thisVal)
{


if(document.getElementById("totResults")){
        var div1=document.getElementById("totResults");
        div1.style.display='none';
}

closeMDFTree();

for(var j=0;j<mdfTree.aNodes.length;j++)
{
if(thisVal==mdfTree.aNodes[j].id){
searchFromButton(mdfTree.aNodes[j].name);
selectedmdfidmodel="s"+"mdfTree"+j;
}
}

totalMatched = 0;
scrolltoSelection1(scrollHeight); 
}
function searchTree1(thisVal)
{
var allsids="";
var sid="";
var count=1;

closeMDFTree();

if((thisVal==null)||(thisVal==""))
return false;

var re=new RegExp(thisVal.toUpperCase());

for(var j=0;j<mdfTree.aNodes.length;j++)
{
var match=re.exec(mdfTree.aNodes[j].name.toUpperCase());

if(match!=null)
{
if(count>1)
{
allsids+=":";
}
allsids+=mdfTree.aNodes[j].id;
sid=mdfTree.aNodes[j].id;
count++;
}
}

if(count<=2)
{
var tmp=count+": single "+sid;
mdfTree.openToSingle(sid,true,false);
}else
{
var tmp=count+": multiple "+allsids;
mdfTree.openToMultiple(allsids,true,false);
}
return false;
}

function scrolltoSelection(selectedDivId){
if(document.getElementById(selectedDivId)!=null){
var height;
if(navigator.appName.indexOf("Microsoft")!=-1){
height=document.getElementById(selectedDivId).offsetTop+document.getElementById(selectedDivId).parentNode.offsetTop+600;
}
else{
height=document.getElementById(selectedDivId).offsetTop
}
window.scrollBy(0,height);
}
}

function scrolltoSel(){
if(selectedmdfidseries!=undefined)
scrolltoSelection(selectedmdfidseries);
}

function scrolltoSelModel(){
if(selectedmdfidmodel!=undefined)
scrolltoSelection(selectedmdfidmodel);
}

function scrolltoSelection1(height) {
	window.scrollBy(0, height);
}
/* */
function getTotalMatched() {
	var cnt = 0;
	for(var j=0;j<mdfTree.aNodes.length;j++)	{
		var arr =document.getElementsByTagName("A");
			
		if (arr.length > 1) {			
			for (i=0;i<arr.length;i++)  {
				var tmp="s" + "mdfTree" + j;
				var eNew;
				 if(arr[i].id==tmp) {
					eNew = arr[i];
					if(eNew != null && eNew.className=='nodeSel')
						cnt++;
					 }
				}
		}
	}
	return cnt;
}

function showResults(thisVal){

var div1=document.getElementById("totResults");
var div2;
if(document.getElementById('show')){
div1.removeChild(document.getElementById('show'));
}
div2=document.createElement('div');
div2.id='show';
//var text=document.createTextNode(mdfTree.searchResults);
var text=document.createTextNode(thisVal);
div2.appendChild(text);
div1.appendChild(div2);
div1.style.display='';
var divline;
if(document.getElementById("lowerLine")){
divline = document.getElementById("lowerLine");
divline.style.display='';
}
}


function searchShowTrack(thisVal,page,track,show,otherParam){
	
	if((thisVal!=null) && (thisVal!="")) {
		thisVal = thisVal.replace(/(^\s*)|(\s*$)/g, '');
	}
	searchFromButton(thisVal); 
	if(show) {
          _showResults(thisVal);
	}
	if(totalMatched!=0){
		scrolltoSelection1(scrollHeight); 
	}
	 var referrerUrl = getReferrerFileName(document.referrer);	
         var p = 'action=search&searchphrase='+thisVal+'&page='+page+'&pos='+totalMatched+'&referrer='+referrerUrl+otherParam;
         if((thisVal!=null) && (thisVal!="")) {
     	 if(track) {
	  var url =  '/support/downloads/pub/TrackActivity.x?'+p;
	  trackActivity(url);
	 }
	 }
	totalMatched = 0;
}

function trackActivity(url) {
	var img = new Image();		
	img.src=url;
}

function getReferrerFileName(referrerUrl)
 {
	 var fileName = '';
	 //var referrerUrl = document.referrer;
	 if(referrerUrl!="" && referrerUrl!=null) {
	 var x = referrerUrl.length;
	 if(x>=1){
	  while((referrerUrl.substring(x,x-1))!= "/"){ x--; } var clipstart = x;
	  fileName = 	 referrerUrl.substring(referrerUrl.length,clipstart);
	  fileName = fileName.replace(/\?.*/,'');
	 }
	}
	return fileName;
} 

function _showResults(thisVal){

if(mdfTree.searchResults>=50) {
	mdfTree.searchResults = 0;
	var searchVal ='Your search returned more than 50 results. Please refine your search.' ;
	showResults(searchVal);
 	var aTagArray = document.getElementsByTagName("A");

	if(aTagArray.length>1){
	   for(var p=0;p<aTagArray.length;p++){
		eNew = aTagArray[p];
		if(eNew!=null && eNew.className=='nodeSel'){
			eNew.className="node";
		}
	   }
	 }
		mdfTree.closeAll();
} else {
		var searchVal = '';		
		if(totalMatched==0){
			 mdfTree.searchResults = '-1';
		} else{
			mdfTree.searchResults = totalMatched;
		}
	
		// mdfTree.searchResults = totalMatched;
		var origVal=thisVal.replace(/^\s+|\s+$/g,'');
		if(origVal==null || origVal==''){
			mdfTree.searchResults = '-1';
			searchVal = 'There is no search text entered. Enter search text then select Go.';
		} else {
			searchVal = totalMatched+' results for '+thisVal;
		}
		showResults(searchVal);
}

}

function clearHighLighting() {

	  var aTagArray = document.getElementsByTagName("A");

	  if(aTagArray.length>1){
		   for(var p=0;p<aTagArray.length;p++){
				eNew = aTagArray[p];
				if(eNew!=null && eNew.className=='nodeSel'){
					eNew.className="";
				}
		   }
	  }


}

function getHeight(obj) {
	var yPos = eval(obj).offsetTop;
	var tempEl = eval(obj).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
return yPos;
}

