function requestSearch( aType, aVal )
{
	requestMap( "search", 0, 0, 0, 0, 0, aType + '&aVal=' + aVal );
}

function findInMap( )
{
	var findElem = document.getElementById( 'searchStr' );
	if ( findElem )
	{
		if ( findElem.value.length > 1 ) requestSearch( 'street', findElem.value );
	}
}

var nosearchIFrameHeight = 0;

function clearSearhPane()
{
	try
	{
		var searchResPane = document.getElementById( 'searchRes' )
		if ( searchResPane ) searchResPane.innerHTML =  '&nbsp;';
		var findElem = document.getElementById( 'searchStr' );
		if ( findElem ) 
		{
			findElem.value = '';
			document.getElementById( 'clearFind' ).style.display = "none";

			if ( window.parent && nosearchIFrameHeight > 0 ) 
			{
				var searchPane = window.parent.document.getElementById( 'mapFrame' );
				if ( parseInt( searchPane.style.height ).toString() != 'NaN' )
					searchPane.style.height = nosearchIFrameHeight;
				else
					searchPane.height = nosearchIFrameHeight;

				nosearchIFrameHeight = 0;
			}
		}
	}
	catch ( er )
	{
		;
	}
}

function clearFind()
{
	requestMap( "clearsearch", 0, 0, 0, 0, 0, "" );
	clearSearhPane();
}

function catchEnter( e )
{
	if ( !e ) e = window.event;
	var keyCode = ( e.keyCode ) ? e.keyCode : e.which;

	if ( keyCode == 13 ) // 'Enter'
	{
		findInMap();
		clearEvent( e );
		return false;
	}
	return true;
}

function hglght( idStr, aType, cl, clType )
{
	if ( clType == 0 ) return;
	requestMap( "hglght", 0, 0, 0, 0, 0, aType + '&aVal=' + idStr + '&cl=' + cl + '&clType=' + clType );
	return false;
}

function mergeByName( ar )
{
	if ( !ar ) return null;

//	ar = ar.sort();

	var saveName = '', saveType = -1, saveAr, arLen = ar.length, tmpName, tmpID, tmpType;
	var resAr = new Array(5), count = 0;

	resAr[0] = new Array();
	resAr[1] = new Array();
	resAr[2] = new Array();
	resAr[3] = new Array();
	resAr[4] = new Array();

	for ( var i = 0; i < arLen; i++ )
	{
		if ( !ar[i].length ) continue;

		tmpAr = ar[i].split( '|' );
		tmpName = tmpAr[0];
		tmpType = tmpAr[3];

		if ( tmpName == saveName && tmpType == saveType )
		{
			tmpID = tmpID + ',' + tmpAr[2];
		}
		else
		{
			if ( saveName )
			{
				resAr[0][count] = saveName;
				resAr[1][count] = saveAr[1];
				resAr[2][count] = tmpID;
				resAr[3][count] = saveType;
				resAr[4][count] = saveAr[4];
				count++;
			}

			tmpID = tmpAr[2];
			saveName = tmpName;
			saveType = tmpType;
			saveAr = tmpAr;
		}
	}
	if ( saveName ) 
	{
		resAr[0][count] = saveName;
		resAr[1][count] = saveAr[1];
		resAr[2][count] = tmpID;
		resAr[3][count] = saveType;
		resAr[4][count] = saveAr[4];
	}


	try {
		if ( window.parent )
		{
			var searchPane = window.parent.document.getElementById( 'mapFrame' );
			if ( parseInt( searchPane.style.height ).toString() != 'NaN' )
			{
				if ( nosearchIFrameHeight == 0 ) nosearchIFrameHeight = parseInt( searchPane.style.height );
				searchPane.style.height = nosearchIFrameHeight + 14 * arLen;
			}
			else
			{
				if ( nosearchIFrameHeight == 0 ) nosearchIFrameHeight = parseInt( searchPane.height );
				searchPane.height = nosearchIFrameHeight + 14 * arLen;
			}
		}
	}
	catch (er)
	{
		return resAr;
	}

	return resAr;
}

function procesSearchRes( aRes )
{
	if ( !aRes )
	{
		if ( window.parent && nosearchIFrameHeight > 0 ) 
		{
			var searchPane = window.parent.document.getElementById( 'mapFrame' );
			if ( parseInt( searchPane.style.height ).toString() != 'NaN' )
				searchPane.style.height = nosearchIFrameHeight;
			else
				searchPane.height = nosearchIFrameHeight;
			nosearchIFrameHeight = 0;
		}
		return "ничего не найдено";
	}

	try
	{
		var resArray = mergeByName( aRes.split( ';' ) ), resHTML = '';
		for ( var i = 0; i < resArray[0].length; i++ )
		{
			resArray[3][i] = resArray[3][i].replace( '#', 'улица ');
			resHTML = resHTML + "<tr><td><a href='#' onClick='return hglght(\"" + resArray[2][i] + "\"," + resArray[1][i] + ",\"" + resArray[3][i] + "\"," + resArray[4][i] + ")'>" + resArray[0][i] + "</a></td><td><font size='-3' color='#666666'>" + resArray[3][i] + "</font></td></tr>";
		}
		if ( resHTML ) resHTML = '<table cellpadding=0 cellspacing=0 border=0 width=100%>' + resHTML + '</table>';
		document.getElementById( 'clearFind' ).style.display = "block";
	}
	catch( er )
	{
		;
	}
	return resHTML;	
}

function updateSearchInfo()
{
	try
	{
		if ( !requestFrame ) requestFrame = window.frames[ 'requestImage' ];
		var requestDoc = requestFrame.document;
		if ( requestDoc )
		{
			if ( requestDoc.getElementById( 'streetsList' ) ) 
			{
				var searchRes = requestDoc.getElementById( 'streetsList' ).title;
				var searchResPane = document.getElementById( 'searchRes' )
				if ( searchResPane ) searchResPane.innerHTML =  procesSearchRes( searchRes );
			}
		}
	}
	catch ( er )
	{
		;
	}
}
