var resultSet = new Array();
var staticSearchFields = belenusHome.staticSearchResultFields;
var actTab = null;

function numCompare(a,b) {
    return a.substring(0, a.lastIndexOf(':')) - b.substring(0, b.lastIndexOf(':'));
}

function addColon(arr) {
    for (var i = 0; i < arr.length; i++) arr[i] = arr[i] + ':' + i;
}


function duplicateArray(arr) {
    var newArr = new Array();
    for (var i = 0; i < arr.length; i++) newArr[i] = arr[i];
    return newArr;
}

function removeColon(arr) {
    for (var i = 0; i < arr.length; i++) arr[i] = arr[i].substring(arr[i].lastIndexOf(':') + 1);
}

function reorganizeAllArrays(newArrangement) {
	var cache = new Array();
	for(i in resultSet) {
		for(j in resultSet[0]) {
			cache[j] = resultSet[i][newArrangement[j]]
		}
		resultSet[i] = cache;
		cache = new Array();
	}
}

function sortStaticSearchResult(fieldName, strOrInt) {
	var fieldIndex = null;
	for(w in staticSearchFields) if(staticSearchFields[w] == fieldName) fieldIndex = w;
	if(fieldIndex == null) fieldIndex = 0;
	var tempArray = resultSet[fieldIndex];
	var copy = duplicateArray(tempArray);
	addColon(copy);
	if(strOrInt == "string") copy.sort();
	else	copy.sort(numCompare);
	removeColon(copy);
	reorganizeAllArrays(copy);
}

function paintStaticSearchResult(sortField,strOrInt) {
	sortStaticSearchResult(sortField,strOrInt);
    var canvas = parent.content.document;
    canvas.open();	
    canvas.write("<html>");
    canvas.write('<head>')
	canvas.write('<sc'+'ript type="text/javascript">var belenusHome = parent.belenusHome;</scr'+'ipt>');
	belenusHome.includeCSS(parent.content,"custom/content.css");
    canvas.write('<sc'+'ript type="text/javascript">document.oncontextmenu=disableRightClick;function disableRightClick(){return false;}</sc'+'ript></head>');
    canvas.write('<body>');
    canvas.write("<table>");
    // paint table header
    if(resultSet[0].length > 0) {
	    if(searchField != "tree:") {
		    canvas.write('<tr class="staticSearchResultHeader"><th></th>');
		    for(l in staticSearchFields) {
		    	canvas.write('<th align="left"><a href="javascript:parent.navigation.paintStaticSearchResult(\''+staticSearchFields[l]+'\',\'string\');" onmouseover="top.status=\'\';return true;" id="'+belenusHome.addLKey("s"+Math.random(),parent.content,"",'staticSearchResultField_'+staticSearchFields[l])+'">');
		    	canvas.write(belenusHome.ld['staticSearchResultField_'+staticSearchFields[l]]);
		    	if(sortField == staticSearchFields[l]) 
			    	canvas.write('&nbsp;<img align="absmiddle" src="'+belenusHome.getPath('custom/images/down_current.gif')+'" border="0" height="12" width="11"></a>&nbsp;&nbsp;&nbsp;</th>');
		    	else 
		    		canvas.write('&nbsp;<img align="absmiddle" src="'+belenusHome.getPath('custom/images/down.gif')+'" border="0" height="12" width="11"></a>&nbsp;&nbsp;&nbsp;</th>');
		    }
		    canvas.write('</tr>');
		    
		    for(k in resultSet[0]) {
		    	canvas.write('<tr onclick="if(belenusHome.autoTabMinimize) belenusHome.minimizeTab();belenusHome.eventHandler(\'OpenLink\',\'URL=parts:'+resultSet[resultSet.length-1][k]+'\');">');
		    	canvas.write('<td>&#8226;</td>');
		    	//canvas.write('<tr>');
		    		for(var x = 0; x < resultSet.length-1; x++ ) {
		    			canvas.write('<td>&nbsp;<a href="javascript://" onmouseover="top.status=\'\';return true;">'+resultSet[x][k]+'</a></td>');
		    		}
		    	canvas.write('</tr>');
		    	canvas.write('<tr height="1"><td></td><td style="height:1px;" colspan="'+resultSet.length+'"><img src="x.gif" height="1" width="100%" style="border:0px solid black;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:black"></td></tr>');
		    }
		    
	    } else {
	    	//paint tree search result
	    	for(k in resultSet[0]) {
		    	canvas.write('<tr onclick="belenusHome.eventHandler(\'OpenLink\',\'URL=parts:'+resultSet[1][k]+'\');">');
	   			canvas.write('<td>&nbsp;<a href="javascript://" onmouseover="top.status=\'\'">'+resultSet[0][k]+'</a></td>');
		    	canvas.write('</tr>');
		    	canvas.write('<tr style="height:1px;"><td></td></tr>');
		    }
	    }
    } else {
    	canvas.write('<tr>');
    	canvas.write('<td width="100%" align="center"><strong>'+belenusHome.ld['s_noResultsFound']+'</strong></td>');
    	canvas.write('</tr>');
    }
    document.getElementById('search_ani').style.visibility = "hidden";
    canvas.write("</table></body></html>");
    canvas.close();
}
var searchField = null;
function performSearch() {
	actTab = parent.belenusHome.activeTab;
	var searchTerm = belenusHome.getQueryField(parent.location.search,"searchTerm");
	searchField = belenusHome.getQueryField(parent.location.search,"searchField");
	var searchPath = belenusHome.getQueryField(parent.location.search,"treepath");
	
	var results = "";
	var entries = 0;
	
	if(searchField == "tree:") {
		results = belenusHome.getProperty("Tree","Search","KeyWord="+searchTerm+"&BelowFolder=" + (searchPath!=null ? searchPath : ""));
		results += "";
        results = belenusHome.queryStringToArray(results);
        
        for(var tt = 0; tt <= staticSearchFields.length; tt++) {
        	resultSet[tt] = new Array();
        }
        
        while(results['ItemPath'+entries]) {
        	resultSet[1][entries] = ""+results['ItemPath'+entries]; 
       		//resultSet[0][entries] = ""+results[staticSearchFields[i]+entries];
       		resultSet[0][entries] = ""+belenusHome.getQueryField(belenusHome.getProperty("Global","ItemData","Field=Label&ItemPath="+results['ItemPath'+entries++]),"Label")
        }        
        
	} else {
		results = document.getElementById("SearchApplet").search(searchField, searchTerm, (searchPath!=null ? searchPath : ""));
        results += "";
        results = belenusHome.queryStringToArray(results);
        
        for(var tt = 0; tt <= staticSearchFields.length; tt++) {
        	resultSet[tt] = new Array();
        }
        
        while(results['ItemPath'+entries]) {
        	resultSet[staticSearchFields.length][entries] = ""+ results['ItemPath'+entries]; 
        	for(i in staticSearchFields) {
        		resultSet[i][entries] = ""+results[staticSearchFields[i]+entries];
        	}
        	entries++;
        }
	}
	paintStaticSearchResult(belenusHome.staticInitSort,"string");
}

