﻿if( window.parent != null )
	window.parent.scroll( 0, 0 );

function tdClick( td )
{
	if( td.style.backgroundImage == "url(../images/checkMark.gif)" )
		td.style.backgroundImage = "";
	else
		td.style.backgroundImage = "url(../images/checkMark.gif)";
		
	var	tableMap		= document.getElementById( "tableMap" );
	var	tableMapZoom	= document.getElementById( "tableMapZoom" );

	tableMapZoom.rows[td.parentElement.rowIndex].cells[td.cellIndex].style.backgroundImage = td.style.backgroundImage;
	tableMap.rows[ td.parentElement.rowIndex ].cells[ td.cellIndex ].style.backgroundImage = td.style.backgroundImage;
}

function buttonAdvacedSearch_Click( button )
{
	var trAdvancedSearch = document.getElementById( "trAdvancedSearch" );
	var trLineSplitter = document.getElementById( "trLineSplitter" );
	
	if( trAdvancedSearch.style.display == "" )
	{
		trAdvancedSearch.style.display = "none";
		trLineSplitter.style.display = "none";
	}
	else
	{
		trAdvancedSearch.style.display = "";
		trLineSplitter.style.display = "";
	}
}

function buttonSearch_Click( )
{
	var hiddenStreetParts = document.getElementById( "hiddenStreetParts" );
	var tableMap = document.getElementById( "tableMap" );

	hiddenStreetParts.value = "";
	for( var rowIndex = 0; rowIndex < tableMap.rows.length; rowIndex++ )
	{
		var row = tableMap.rows[ rowIndex ];
		
		for( var index = 0; index < row.cells.length; index++ )
		{
			var cell = row.cells[ index ];
			
			if( cell.style.backgroundImage != "url(../images/checkMark.gif)" )
				hiddenStreetParts.value += "0";
			else
				hiddenStreetParts.value += "1";
		}
	}
}

function CheckNumericalCharacters( inputObject )
{
	var value = Trim( inputObject.value );
	value = value.replace( /,*/g, "" );
	
	var regExpr = new RegExp( "^\\d*$" );
	
	if( !regExpr.test( value ))
	{
		inputObject.style.backgroundColor = "red";
		return false;
	}

	inputObject.style.backgroundColor = "";
	return true;
}

function Trim( text )
{
	if( text == null )
		return null;

	return text.replace( /^\s+|\s+$/g, "" );
}

function EnsureDataCorrected( )
{
	var trError = document.getElementById( "trError" );
	var objectCollection = document.body.getElementsByTagName( "input" );
	
	for( var index = 0; index < objectCollection.length; index++ )
	{
		var input = objectCollection[ index ];
		if( input != null && input.type == "text" && input.style.backgroundColor == "red" )
		{
			trError.style.display = "";
			return false;
		}
	}

	trError.style.display = "none";
	return true;
}

function ZoomChange( )
{
	var divZoomBackground	= document.getElementById( "divZoomBackground" );
	var	tableMapZoom		= document.getElementById( "tableMapZoom" );
	var tdZoomImage			= document.getElementById( "tdZoomImage" );

	if( divZoomBackground.style.display == "none" )
	{
		divZoomBackground.style.display = "";
		tableMapZoom.style.backgroundImage = "url(../images/tehran.gif)";

		tdZoomImage.style.backgroundImage = "url(../images/zoomout.jpg)";
		tdZoomImage.style.cursor = "url(../cursors/zoomout.cur), pointer";
	}
	else
	{
		divZoomBackground.style.display = "none";
	}
}

function bodyLoad( )
{
	displayMarks( );
	
	var pageList = document.getElementById( "pageList" );
	if( pageList == null )
		return;

	var anchorList = pageList.getElementsByTagName( "a" );
	if( anchorList.length > 0 )
	{
		var firstAnchor = anchorList[ 0 ];
		var baseUrl = firstAnchor.href;

		for( index = 0; index < anchorList.length; index++ )
			anchorList[ index ].href = baseUrl + "&page=" + anchorList[ index ].getAttribute( "page" );
	}
}

function displayMarks( )
{
	var hiddenStreetParts = document.getElementById( "hiddenStreetParts" );
	var tableMap = document.getElementById( "tableMap" );

	if( tableMap == null || hiddenStreetParts.value == "" )
		return;

	for( var rowIndex = 0; rowIndex < tableMap.rows.length; rowIndex++ )
	{
		var row = tableMap.rows[ rowIndex ];

		for( var index = 0; index < row.cells.length; index++ )
		{
			var cell = row.cells[ index ];

			if( hiddenStreetParts.value[ rowIndex * 5 + index ] == "1" )
				cell.style.backgroundImage = "url(../images/checkMark.gif)";
		}
	}
}

function formatNumber( textbox )
{
	if( CheckNumericalCharacters( textbox ))
	{
		var value = textbox.value.replace( /,*/g, "" )
		var formattedValue = "";
		
		for( var index = value.length - 1; index >= 0; index-- )
		{
			if(( value.length - 1 - index ) > 0 && (( value.length - 1 - index ) % 3 ) == 0 )
				formattedValue = "," + formattedValue;
				
			formattedValue = value.charAt( index ) + formattedValue;
		}

		if( textbox.value != formattedValue )
			textbox.value = formattedValue;
	}
}