/*-------------------------------------------------------------------------------
* File: 			common.js
* Description:		
* Date created:		January 2005
* Date modified:	April 2005
*------------------------------------------------------------------------------*/
var common_DEBUG = false;

try {
	var m_sClientBrowserType;
	var m_sClientPlatform;
	GetBrowserType();

	// scale list for BG and CITY maps
	var scaleArrBG = new Array(3480000,1500000,500000,200000,100000);
	var scaleArrCITY = new Array(100000,25000,10000,3000,1200);

	var m_selectedTool;
	var m_divLoader;
	
	//var selectionColor = "#CCFFCC";
	//var backString = "";
	//var previousExtent = "";
	//var stackSize = 3;
	//var currStImgSize = 0;
	//var currStExtSize = 0;
	//var neededNewExtent = false;
	//var nowBackDone = false;
	//var screenWidth = screen.width;
	//var screenHeight = screen.Height;
	//var screenInches = 17;
	//var inchesInMeter = parseFloat("39,3701");//inches per meter
	
	var currCenterX;
	var currCenterY;
	var refScale;

	//small zoom box coords
	var startDivX;
	var endDivX;
	var startDivY;
	var endDivY;
	
	var sMapXMin;
	var sMapXMax;
	var sMapYMin;
	var sMapYMax;
	
	var smHeight = 105;
	var smWidth = 150;
	
	var sxDistance = Math.abs(sRight - sLeft);
	var syDistance = Math.abs(sTop - sBottom);
    	
    var sMapX;
    var sMapY;

	var smZoomLeft;
	var smZoomTop;
	var smZoomWidth;
	var smZoomHeight;
	
	var inProcesOfDivMoving = false;
	var startOpDivX;
	var startOpDivY;
	
	var newCenterX;
	var newCenterY;
	
	var smallZoomBoxCenterX;
	var smallZoomBoxCenterY;
	
	var divMoved = false;
	
} catch (e) {
	if (common_DEBUG)
		alert('common.js: variable definition section --> ' + e.description)
}

//-----------------------------------------------------------------------------

function GetBrowserType()
{
	//alert(navigator.appName + "\n" + navigator.appVersion + "\n" + navigator.userAgent + "\n" + navigator.platform)
	
	if (navigator.userAgent.indexOf("Firefox")>=0) {
		m_sClientBrowserType = 'Firefox';
	} else if (navigator.userAgent.indexOf('MSIE') >=0) {
		m_sClientBrowserType = 'IE';
	} else if (navigator.userAgent.indexOf('Opera') >=0) {
		m_sClientBrowserType = 'Opera';
	} else if (navigator.userAgent.indexOf('Netscape6') >=0) {
		m_sClientBrowserType = 'NS';
	}
	
	if (navigator.platform.indexOf("Win") >= 0) {
		m_sClientPlatform = 'Windows';
	} else if (navigator.platform.indexOf("Mac") >= 0) {
		m_sClientPlatform = 'Mac';
	}
}

function getRefScale(){
	try {
		var pWidth = 21;//sm
		var pHeight = 29.7;//sm
	
		var partX = top.frmMap.iWidth/screen.width;
		
		var pImgWidth = parseFloat("14.7");
		if(top.numSizeSelected == 2)
			pImgWidth = parseFloat("19.72");
		if(top.numSizeSelected == 3)
			pImgWidth = parseFloat("27.636");
		
		
		var imgElem = document.getElementById("frmMap");
			
		var refScale = (top.frmMap.eRight - top.frmMap.eLeft);
		
		refScale = refScale*100;

	 	refScale = refScale/pImgWidth;

	 	return refScale;
	} catch (e) { alert('getRefScale(): ' + e.description); }
}

function zoomToScale(scale){
	var intRefScale = getRefScale();
	var refRatio = scale/intRefScale;
	var xDist = top.frmMap.eRight - top.frmMap.eLeft;
	var yDist = Math.round(top.frmMap.eTop - top.frmMap.eBottom); 
	var xCenter = top.frmMap.eLeft + xDist/2;

	var yCenter = top.frmMap.eBottom + yDist/2;
		
	var newXDist = xDist * refRatio;
	var newYDist = yDist * refRatio;

	var newELeft = xCenter - newXDist/2;
	var newERight = xCenter + newXDist/2;
	var newETop = yCenter + newYDist/2;
	var newEBottom = yCenter - newYDist/2;
		
	var additional = "&ZoomBox=" + newELeft + ";" + newEBottom + ";" + newERight + ";" + newETop; 
	ShowLoader();
	top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=ZoomIn" + additional;
}

function onRefScaleTextBoxKeyPress(ev, val){	
	if (ev.keyCode == 13)
	{
		var minRef = (top.frmMap.isSofTypeService == "true")?scaleArrCITY[scaleArrCITY.length-1]:scaleArrBG[scaleArrBG.length-1];
		var maxRef = (top.frmMap.isSofTypeService == "true")?scaleArrCITY[0]:scaleArrBG[0];
		
		var currentScale = parseInt(val);
		
		if ( isNaN(currentScale) )
		{
			alert(MSG_1);
			return;
		}
		
		if (currentScale > maxRef || currentScale < minRef)
		{
			alert(printf(MSG_2, minRef, maxRef)); //alert("Въведете мащаб между "+ minRef +" и "+ maxRef +".");
			return;
		}
	
		zoomToScale(currentScale);
	}
}

function mRefScale(){
   var refScale = getRefScale();
   if(refScale > 100000)
   {
  	var remRef = refScale%10000;
  	refScale = refScale - remRef; 
  	if(remRef > 5000)
  		refScale = refScale + 10000;
  }
	else
	{
		if(refScale > 10000)
		{
			var remRef = refScale%1000;
			refScale = refScale - remRef;
			if(remRef > 500)
				refScale = refScale + 1000;
		}
		else
		{
			if(refScale > 1000)
			{
				var remRef = refScale%100;
				refScale = refScale - remRef;
				if(remRef > 50)
				refScale = refScale + 100;
			}
			else
			{
				var remRef = refScale%10;
				refScale -= remRef;
				if(remRef > 5)
					refScale += 10; 
			}
		}
	}
	document.getElementById("refScaleTextBox").value = Math.round(refScale);
}

function load(){
	mRefScale();
	m_selectedTool = getObject("tool").value;
	m_divLoader = getObject("divLoader");
}

function getObject(id){
	return document.getElementById(id)
}

function HideLoader(){
	try {
		document.getElementById("divLoader").style.visibility = 'hidden'
	} catch (e) {
		if (common_DEBUG)
			alert('common.js: HideLoader --> ' + e.Description)
	}
}
	
function ShowLoader(){
	try {
	
		var element = document.getElementById("frmMap");
		var iWidth = element.clientWidth;
		var iHeight = element.clientHeight;
			
		var currElement = element;
		var parentElement = element.offsetParent;
			
		var iTop = currElement.offsetTop;
		var iLeft = currElement.offsetLeft;
			
		while (parentElement.tagName != "BODY")
		{
			currElement = parentElement;
			parentElement = currElement.offsetParent;
				
			iTop += parseInt(currElement.offsetTop);
			iLeft += parseInt(currElement.offsetLeft);
		}
		
		var imgLoader = document.getElementById("imgLoader");
		
		var iPosTop = iTop + iHeight/2 - imgLoader.height/2;
		var iPosLeft = iLeft + iWidth/2 - imgLoader.width/2;
	
		var loader = document.getElementById("divLoader");
		loader.style.top = iPosTop + 'px';
		loader.style.left = iPosLeft + 'px';
		loader.style.visibility = 'visible';
	} catch (e) {
		if (common_DEBUG)
			alert('common.js: ShowLoader --> ' + e.Description)
	}
}

/*
function mapClick(ev){
	var oPoint = new Object();
	//oPoint.x = ev.offsetX;
    //oPoint.y = ev.offsetY;

	if (m_sClientBrowserType == 'Netscape') {
		oPoint.x = ev.clientX;
		oPoint.y = ev.clientY;
	} else {
		oPoint.x = event.offsetX;
		oPoint.y = event.offsetY;
	}
	
	//oPoint.x = ev.clientX;
    //oPoint.y = ev.clientY;
	
	var selectedTool = getObject("tool").value;
	
	if ( selectedTool == "Identify" ) {
		ShowForm(false, "Identify", "", "x=" + oPoint.x + "&y=" + oPoint.y);
	}
	if ( selectedTool == "SetMarker" ){
		ShowLoader();
		top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=SetMarker&x=" + oPoint.x + "&y=" + oPoint.y
	}
	if ( selectedTool == "Info" ) {
		ShowForm(false, "Info", "", "x=" + oPoint.x + "&y=" + oPoint.y);
	}
	if (selectedTool == "addPointToRoute0"){
		AddObjToRouteByXY(oPoint.x,oPoint.y, 0);
	}
	if (selectedTool == "addPointToRoute1"){
		AddObjToRouteByXY(oPoint.x,oPoint.y, 1);
	}
	if (selectedTool == "addPointToRoute2"){
		AddObjToRouteByXY(oPoint.x,oPoint.y, 2);
	}
	if ( selectedTool == "IdentifyFavourites" ) {
		ShowForm(false, "IdentifyFavourites", "", "x=" + oPoint.x + "&y=" + oPoint.y);
	}	
	if ( selectedTool == "Favourite" ) {
		descr = getDescription("IdentifyFavourites");
		AddFeature("AddFavoriteFeature", oPoint.x , oPoint.y, descr);
	}	
	if (selectedTool == "AddFav" ) {
		descr = getTxtDescription();
		AddFeature("AddFavoriteFeature", oPoint.x , oPoint.y, descr);	
		top.document.getElementById("tool").value = "none";	
	}	
	if (selectedTool == "AddBufferPoint" ) {
		var buffDist = top.document.getElementById("txtBufferDistance").value
		ShowLoader();
		AddBufferPoint(oPoint.x , oPoint.y , buffDist);     
		top.document.getElementById("tool").value = "none";      
	}	
}
*/

function ZoomToFeature(parentWindow, layerName, id, desc){
	if (id == ''){
		return;
	}
	top.frmMap.location.href = 'Map.asp?lng="+ g_LANG +"&action=goto&layer=' + layerName + '&id=' + id + '&desc=' + desc;
}

function ShowForm(isModal, frm, paramValue){
	var formName;
	var paramName;
	var height;
	var width;
	var wName;
	var newWindow;
	
	switch (frm){
		case "Route":
			formName = "routesCalc.asp";
			paramName = "tpanl";
			height = "600px";
			width = "500px";
			wName = "Routes"
			break;
		case "Survey":
			formName = "Survey.asp";
			paramName = "";
			height = "600px";
			width = "600px";
			wName = "Common"
			break;
		case "Advertise":
			formName = "Advertise.asp";
			paramName = "";
			height = "600px";
			width = "600px";
			wName = "Common"
			break;
		case "AboutUs":
			formName = "AboutUs.asp";
			paramName = "";
			height = "600px";
			width = "600px";
			wName = "Common"
			break;
		case "News":
			formName = "News.asp";
			paramName = "";
			height = "600px";
			width = "600px";
			wName = "Common"
			break;
		case "AddObject":
			formName = "ObjOrder.asp";
			paramName = "";
			height = "600px";
			width = "600px";
			wName = "AddObject"
			break;
		case "Feedback":
			formName = "feedbackForm.asp";
			paramName = "";
			height = "600px";
			width = "600px";
			wName = "Feedback"
			break;
		case "Identify":
			formName = "Identify.asp";
			paramName = "act";
			height = "500px";
			width = "600px";
			wName = "Identify"
			break;
		case "GetLink":
			formName = "getLink.asp";
			paramName = "";
			height = "250px";
			width = "750px";
			wName = "GetLink"
			break;			
		case "Info":
			formName = "featureInfo.asp";
			paramName = "layer";
			height = "200px";
			width = "300px";
			wName = "Info"
			break;
		//case "IdentifyFavourites":
		//	formName = "favouriteInfo.asp";
		//	paramName = "layer";
		//	height = "200px";
		//	width = "300px";
		//	wName = "IdentifyFavourites"
		//	break;
		case "FavouritesList":
			formName = "FavouritesList.asp";
			paramName = "act";
			height = "500px";
			width = "600px";
			wName = "Identify"
			break;
		case "Print":
			formName = "Print.asp";
			paramName = "";
			height = "600px";
			width = "700px";
			wName = "Print";
			break;
		case "Help":
			formName = "Help.asp";
			paramName = "";
			height = "600px";
			width = "660px";
			wName = "Common";
			break;
		case "searchResult":
			formName = "searchResultForm.asp";
			paramName = "";
			height = "500px";
			width = "650px";
			wName = "Search";
			break;
		//case "Buffer":
		//	formName = "BufferResults.asp";
		//	paramName = "";
		//	height = "400px";
		//	width = "500px";
		//	wName = "Search"
		//	break;
		//case "BufferInfo":
		//	formName = "BufferInfo.asp";
		//	paramName = "message";
		//	height = "200px";
		//	width = "300px";
		//	wName = "Search"
		//	break;
		case "Legend":
			formName = "Legend.asp";
			paramName = "";
			height = "500px";
			width = "650px";
			wName = "Legend";
			break;
		default:
			wName = "_blank";
			//do nothing
	}
	
	var queryString = "";
	if ( (paramName != '') && (paramValue != '') )
	{
		queryString = paramName + "=" + paramValue;
	}
	
	if ( arguments.length > 3 )
	{
		queryString = (queryString == '') ? "" : queryString + "&"
		queryString = queryString + arguments[3];
	}
	
	//alert(queryString)
	
	try {
		var leftPos = 10;
		var topPos = 10;
	} catch (e) { alert(e.description) }
	
	if (isModal == true){
		newWindow = window.showModalDialog(formName + "?" + queryString, self, "dialogHeight:" + height + ";dialogWidth:" + width + ";center:yes;resizable:no;status:no");
		return newWindow;
	}
	else
	{
		newWindow = window.open(formName + "?" + queryString, wName + top.g_sessionId, "height=" + height + ",width=" + width + ",toolbar=no,titlebar=yes,menubar=no,location=no,resizable=yes,status=yes,scrollbars=yes,top="+ topPos +",left="+ leftPos, false);
		return newWindow;
	}
}

/*
function pushExtent(){
    if(currStExtSize < stackSize)
		currStExtSize++;
	else
    {
		var firstTagIndex = previousExtent.indexOf("<>");
		previousExtent = previousExtent.substr(firstTagIndex + 2);
    }
    if(previousExtent != "")
    {
		previousExtent += "<>";
	}
	
	var backImg = document.getElementById("backImg");
	backImg.src = "img/btnBack.gif"

    previousExtent += top.frmMap.eLeft + ";";
    previousExtent += top.frmMap.eRight + ";";
    previousExtent += top.frmMap.eTop + ";";
    previousExtent += top.frmMap.eBottom + ";"	
}

function popExtent(){
	if(currStExtSize > 0)
		currStExtSize--;
		
	if(previousExtent == "" || previousExtent == null)
	{	
		return "";
	}
	var lastTagIndex = previousExtent.lastIndexOf("<>");
	
	if(lastTagIndex == -1)
	{
	   var resString = previousExtent;
	   previousExtent = "";
	   
	   var backImg = document.getElementById("backImg");
	   backImg.src = "img/zoomLast.gif"
	   
	   return resString;
	}   
	var resString = previousExtent.substr( lastTagIndex + 2 );
	
	previousExtent = previousExtent.substring( 0, lastTagIndex );
	return resString;
}

function doRealWork(resString){
	var arrResString = resString.split(";");

	top.frmMap.eLeft = parseFloat(arrResString[0]);
	top.frmMap.eRight = parseFloat(arrResString[1]);
	top.frmMap.eTop = parseFloat(arrResString[2]);
	top.frmMap.eBottom = parseFloat(arrResString[3]);
}
*/

function canMoveF(){
    if(m_divLoader == null)
		return false;
	if(m_divLoader.style.visibility == "visible")
	    return false;
	else
		return true;
}

function currCenter(){
	currCenterX = top.frmMap.iWidth/2 + top.frmMap.m_mapXMin;
	currCenterY = top.frmMap.iHeight/2 + top.frmMap.m_mapYMin;
}

function minusBtnClick(){
	var currElem = getCurrent();
	moveSrcToPred(currElem);
}

function moveSrcToPred(curr){
	if(curr > 1 && canMoveF() == true)
	{
		var next = curr - 1;
		moveSrc(curr, next);
		
		ShowLoader();
		
		var currScaleArr = new Array();
		if (top.frmMap.isSofTypeService == "true")
    	{
    		currScaleArr = scaleArrCITY;
    	}
		else
		{
			currScaleArr = scaleArrBG;
		}
		zoomToScale(currScaleArr[next-1]);
	}	
}

function plusBtnClick(){ 
	var currElem = getCurrent();
	moveSrcToNext(currElem);
}

function moveSrc(curr, next){
	var item, elemColl, currElem, nextElem;
	
	elemColl = document.getElementById("ZoomToScale" + curr).childNodes;
	for (item in elemColl){
		if (elemColl[item].nodeType == 1){
			if (elemColl[item].tagName == 'IMG'){
				currElem = elemColl[item];
				break;
			}
		}
	}
	elemColl = document.getElementById("ZoomToScale" + next).childNodes;
	for (item in elemColl){
		if (elemColl[item].nodeType == 1){
			if (elemColl[item].tagName == 'IMG'){
				nextElem = elemColl[item];
				break;
			}
		}
	}
	
	var actSrc = currElem.src;
	currElem.src = nextElem.src;
	nextElem.src = actSrc;
}

function getCurrent(){
	var elem, elemColl, item;
	for (var i = 1; i <= 5; i++)
	{
		elemColl = document.getElementById("ZoomToScale" + i).childNodes;
		for (item in elemColl){
			if (elemColl[item].nodeType == 1){
				if (elemColl[item].tagName == 'IMG'){
					elem = elemColl[item];
					break;
				}
			}
		}
	

		if(elem.src.indexOf("zmcur.gif") != -1)
		{
			break;
		}
	}
	return i;	
}

function moveSrcToNext(curr){
	if(curr < 5 && canMoveF() == true)
	{
	    var next = curr + 1;
		
		moveSrc(curr, next);
		
		ShowLoader();
		
		var currScaleArr = new Array();
		if (top.frmMap.isSofTypeService == "true")
    	{
    		currScaleArr = scaleArrCITY;
    	}
		else
		{
			currScaleArr = scaleArrBG;
		}
		zoomToScale(currScaleArr[next-1]);
	}	
}

function clickZoomToScale(elem){
	var curr = getCurrent();
	var elemID = elem.id;
	var number = elemID.substr(elemID.length - 1);
	var currScaleArr = new Array();
	
	if (top.frmMap.isSofTypeService == "true")
    {
    	currScaleArr = scaleArrCITY;
    }
	else
	{
		currScaleArr = scaleArrBG;
	}
	
	zoomToScale(currScaleArr[number-1])
	moveSrc(curr, number);
}

function convertMapToImgCoordinates(x, y){
	newCenterX = (x - top.frmMap.eLeft)*top.frmMap.iWidth/top.frmMap.xDistance;
	var mouseY = (y - top.frmMap.eBottom)*top.frmMap.iHeight/top.frmMap.yDistance;
	newCenterY = top.frmMap.iHeight - mouseY;
}

//------------------------------------------------------------
function OVMap_MouseDown(ev)
{
	inProcesOfDivMoving = true;
	
	if (m_sClientBrowserType == 'Firefox')
	{
		startOpDivX = ev.clientX;
		startOpDivY = ev.clientY;
	}
	else if (m_sClientBrowserType == 'IE')
	{
		startOpDivX = event.clientX;
		startOpDivY = event.clientY;
	}
	getSMapCoords(document.getElementById("smallMap"));
	startOpDivX -= sMapXMin;
	startOpDivY -= sMapYMin;
	
	if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
		//document.getElementById("smallMap").setCapture();
	}
	else
	{
		ev.preventDefault();
	}
}
	
function OVMap_MouseUp(ev)
{
	//if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
		//document.getElementById("smallMap").releaseCapture();
	//}
	
	if (divMoved == true && top.frmMap.hasMap())
	{
		getSmallZoomBoxCenter();
	
		getSMapCoords(document.getElementById("smallMap"));
		smallZoomBoxCenterX -= sMapXMin;
		smallZoomBoxCenterY -= sMapYMin;
	
		inProcesOfDivMoving = false;
	
		getSmallMapXY(smallZoomBoxCenterX, smallZoomBoxCenterY);
		convertMapToImgCoordinates(sMapX, sMapY);
	
		divMoved = false;
		ShowLoader();
		top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=Pan&x=" + Math.round(newCenterX) + "&y=" + Math.round(newCenterY);
		
		//alert(Math.round(newCenterX) + ":" + Math.round(newCenterY));
		
	}
}

function OVMap_MouseMove(ev)
{
	//if ((m_sClientBrowserType == 'IE') && (m_sClientPlatform == 'Windows')) {
	//	document.getElementById("smallMap").setCapture();
	//}
	
	var smallZoomBox = document.getElementById("smallZoomBox");
	getTopLeftZoomBox();
	
	var endOpDivX;
	var endOpDivY;
	
	if (m_sClientBrowserType == 'Firefox')
	{
		endOpDivX = ev.clientX;
		endOpDivY = ev.clientY;
	}
	else if (m_sClientBrowserType == 'IE')
	{
		endOpDivX = event.clientX;
		endOpDivY = event.clientY;
	}
	getSMapCoords(document.getElementById("smallMap"));
	endOpDivX -= sMapXMin;
	endOpDivY -= sMapYMin; 
		
	var translationX = endOpDivX - startOpDivX;
	var translationY = endOpDivY - startOpDivY;
	
	var resX = smZoomLeft + translationX;
	var resY = smZoomTop + translationY;
	
	var zmbRight = resX + smZoomWidth;
	var zmbBottom = resY + smZoomHeight;
	
	if (inProcesOfDivMoving == true && resX >= sMapXMin && resY >= sMapYMin && zmbRight <= sMapXMax && zmbBottom <= sMapYMax)
	{		
		smallZoomBox.style.left = resX;
		smallZoomBox.style.top = resY;
	
		startOpDivX = endOpDivX;
		startOpDivY = endOpDivY;
	
		divMoved = true;
	}
}

function onOVMapClick(ev)
{
	var clickX;
	var clickY;
	
	if (m_sClientBrowserType == 'Firefox')
	{
		clickX = ev.clientX;
		clickY = ev.clientY;
	}
	else if (m_sClientBrowserType == 'IE')
	{
		clickX = event.clientX;
		clickY = event.clientY;
	}
	getSMapCoords(document.getElementById("smallMap"));
	clickX -= sMapXMin;
	clickY -= sMapYMin; 
	
	getSmallMapXY(clickX, clickY);
	convertMapToImgCoordinates(sMapX, sMapY);
	
	if (top.frmMap.hasMap())
	{
		ShowLoader();
		top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=Pan&x=" + Math.round(newCenterX) + "&y=" + Math.round(newCenterY);
	}
}
	
function getTopLeftZoomBox()
{
	var smallZoomBoxStyle = document.getElementById("smallZoomBox").style;
	
	smZoomLeft = parseInt( smallZoomBoxStyle.left.substring(0, smallZoomBoxStyle.left.indexOf("px")));
	smZoomTop = parseInt( smallZoomBoxStyle.top.substring(0, smallZoomBoxStyle.top.indexOf("px")) );
	smZoomWidth = parseInt( smallZoomBoxStyle.width.substring(0, smallZoomBoxStyle.width.indexOf("px")) );
	smZoomHeight = parseInt( smallZoomBoxStyle.height.substring(0, smallZoomBoxStyle.height.indexOf("px")) );
}
	
function getSmallZoomBoxCenter()
{
	var smallZoomBoxStyle = document.getElementById("smallZoomBox").style;
	
	smZoomLeft = parseInt( smallZoomBoxStyle.left.substring(0, smallZoomBoxStyle.left.indexOf("px")));
	smZoomTop = parseInt( smallZoomBoxStyle.top.substring(0, smallZoomBoxStyle.top.indexOf("px")) );
	smZoomWidth = parseInt( smallZoomBoxStyle.width.substring(0, smallZoomBoxStyle.width.indexOf("px")) );
	smZoomHeight = parseInt( smallZoomBoxStyle.height.substring(0, smallZoomBoxStyle.height.indexOf("px")) );
	
	smallZoomBoxCenterX = Math.round(smZoomLeft + smZoomWidth/2);
	smallZoomBoxCenterY = Math.round(smZoomTop + smZoomHeight/2);
}
	
function findPerfectFitExtent()
{
	var i = 5;
	var comm = "top.frmMap."
	var serv;
	if (top.frmMap.isSofTypeService == "true")
		serv = "s";
	else
		serv = "";
	
	var currScaleArr = new Array();
	if (top.frmMap.isSofTypeService == "true")
    {
    	currScaleArr = scaleArrCITY;
    }
	else
	{
		currScaleArr = scaleArrBG;
	}
	var scale = getRefScale();	   
	for(i = 4; i >= 0; i--)
	{
		if (( i == 0 ) && ( scale >= currScaleArr[0] ))
			break;
		
		if (( i == 4 ) && ( scale <= currScaleArr[4] ))
			break;
		
		if ( scale >= currScaleArr[i] && scale < currScaleArr[i-1])
			break;
	}
		
	var curr = getCurrent();
	var next = i+1;

	moveSrc(curr, next);
}

function getSMapCoords(element)
{
	var iTop = element.offsetTop;
	var iLeft = element.offsetLeft;
	
	var currElement = element;
	var parentElement = element.offsetParent;
	
	while (parentElement.tagName != "BODY")
	{
		currElement = parentElement;
		parentElement = currElement.offsetParent;
	
		iTop += parseInt(currElement.offsetTop);
		iLeft += parseInt(currElement.offsetLeft);
	}
	
	sMapXMin = iLeft;
	sMapXMax = iLeft + smWidth;
	sMapYMin = iTop;
	sMapYMax = iTop + smHeight;
}
	
function putDivInPlace()
{
	try {
		getSMapCoords(document.getElementById("smallMap"));
		
		var wind = document.getElementById("smallZoomBox");
		
		wind.style.visibility = "hidden";
		
		wind.style.left = (startDivX > 0)?sMapXMin + startDivX : sMapXMin;//no tova e offset v map-a
		wind.style.top = (startDivY > 0)?sMapYMin + startDivY : sMapYMin;
		
		var width = endDivX - startDivX;
		var height = endDivY - startDivY;
		
		if(startDivX < 0)
		startDivX = 0;
		if(startDivY < 0)
		startDivY = 0;   
		
		var smallWidth = Math.abs( (startDivX + width < smWidth) ? width : smWidth - startDivX );
		var smallHeight = Math.abs( (startDivY + height < smHeight) ? height : smHeight - startDivY );
		
		wind.style.width = (smallWidth <= 8) ? 8 : smallWidth;
		wind.style.height = (smallHeight <= 8) ? 8 * (smallHeight/smallWidth) : smallHeight;
		
		wind.style.visibility = "visible";
	}
	catch (e)
	{
		if (common_DEBUG)
			alert("common.js [putDivInPlace] --> " + e.description);
	}
}

function getSmallMapXY(screenX, screenY)
{
	var pixelX;
	var pixelY;
	
	pixelX = sxDistance / smWidth;
	sMapX = pixelX * screenX + sLeft;

	pixelY = syDistance / smHeight;
	sMapY = pixelY * (smHeight - screenY) + sBottom;

}

//added by MM
function showFavouritePoint(x, y)
{
	opener.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=showFavouritePoint&x=" + x + "&y=" + y
}

function showFavouriteItem(x, y , ekatte)
{
	parent.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=showFavouritePoint&x=" + x + "&y=" + y + "&ekatte=" + ekatte
}

//function FavouritesView_Click()
//{
//	document.forms[0].isView.value = 1
//}

function Favourites_View_Click()
{
	top.ShowLoader();
	top.frmFavourites.document.forms[0].isView.value = 1;
	top.frmFavourites.document.forms[0].submit();
}

function Favourites_Delete_Click()
{
	var agree=confirm(MSG_3);
	if (!agree){
		return;
	}	
	top.frmFavourites.document.forms[0].isView.value = 0;
	top.frmFavourites.document.forms[0].submit();
}

function Close_Click()
{
	window.close()	
}

function getDescription(cmd)
{
	var obj_description = 'none';
	switch(cmd)
	{
		case "IdentifyFavourites":
		 	obj_description = prompt(MSG_4, ' ');
		 	if ( (obj_description==' ') || (obj_description==null) ) 
		 	{ 
		   obj_description = 'none';
		  }
		  break;
		  default:
	}
  return obj_description;
}

function getTxtDescription(cmd)
{
	var obj_description = 'none';
		 	obj_description = top.document.getElementById("favouriteDescription").value; // prompt('Въведете описание на обекта', ' ');
		 	if ( (obj_description==' ') || (obj_description=='') || (obj_description==null) ) 
		 	{ 
		   obj_description = 'none';	
		   alert (MSG_5);
		   return obj_description;
		  }
  return obj_description;
}

//Function to add vaforite object on the map :) 
function AddFeature(parentWindow, x, y, descr){
	parent.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=AddFavoriteFeature&x=" + x + "&y=" + y + "&descr=" + descr
	top.turnOnFavourites(); //MM 2005.10.25
}
//end of added by MM

//MM 18.08.2005
function cmdAddFavouriteFind()
{
	//
		var obj_description = 'none';
		obj_description = top.document.getElementById("favouriteDescription").value; // prompt('Въведете описание на обекта', ' ');
		if ( (obj_description==' ') || (obj_description=='') || (obj_description==null) ) 
		{ 
		   alert ('Не сте задали описание');
		   return;
		}
		setCrossCursor();
		//descr = getTxtDescription("IdentifyFavourites");
		//AddFeature("AddFavoriteFeature", oPoint.x , oPoint.y, descr);
		top.document.getElementById("tool").value = "AddFav";
		getDescriptionTooltip(mapTool);
		//alert ('1');
}

function setCrossCursor()
{
	//var map = document.frames["frmMap"].document.getElementById("imgMap")
	var mapFrame = document.getElementById("frmMap");
	var map = mapFrame.contentWindow.document.getElementById("imgMap");
	var cursorType;
	cursorType = "crosshair";
	map.style.cursor = cursorType;
}

function setNormalCursor()
{
	//var map = document.frames["frmMap"].document.getElementById("imgMap")
	var mapFrame = document.getElementById("frmMap");
	var map = mapFrame.contentWindow.document.getElementById("imgMap");
	var cursorType;
	cursorType = "default";
	map.style.cursor = cursorType;
}

//MM 2005.08.25
function clearFavouriteDescr()
{
	//alert('clearFavouriteDescr');
	top.document.getElementById("favouriteDescription").value = "";
	getDescriptionTooltip(decrTool);
}

function changeClick()
{
	if (top.document.getElementById("favouriteDescription").value != "")
	{
		//alert('onchange');
		getDescriptionTooltip(okTool);
	}
	else
	{
		getDescriptionTooltip(decrTool);	
	}
}

function getDescriptionTooltip(tooltipId)
{
	decrTool.className = "off";
	okTool.className = "off";
	mapTool.className = "off";
	tooltipId.className = "textred";
}

function cmdSelectedBufferObject_Click()
{
	var obj_dist = '';
	obj_dist = top.document.getElementById("txtBufferDistance").value;
	try {
		if ( (obj_dist==' ') || (obj_dist=='') || (obj_dist==null) ) 
		{
			//ShowForm(true, "BufferInfo", MSG_6);
			top.document.getElementById("InfoMsg_4").innerHTML = MSG_6;
			return;
		}
	
		if ( isNaN(obj_dist) ){
			//ShowForm(true, "BufferInfo", MSG_7);
			top.document.getElementById("InfoMsg_4").innerHTML = MSG_7;         
			return;
		}
		ShowLoader();
		frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=GetSelectedPoint" + "&dist=" + obj_dist;
		top.document.getElementById("InfoMsg_4").innerHTML = MSG_12;
		//top.document.getElementById("divLoaderWait").style.visibility = 'visible';
    }
	catch (e) {
		//ShowForm(true, "BufferInfo", MSG_8);
		top.document.getElementById("InfoMsg_4").innerHTML = MSG_8; 
		//top.document.getElementById("divLoaderWait").style.visibility = 'Hidden';            
		return;
	}
}

function cmdMapBufferObject_Click()
{
	var obj_dist = '';
	obj_dist = top.document.getElementById("txtBufferDistance").value;

	try {
		if ( (obj_dist==' ') || (obj_dist=='') || (obj_dist==null) ) 
		{
			//ShowForm(true, "BufferInfo", MSG_6);			
			top.document.getElementById("InfoMsg_4").innerHTML = MSG_6;
			return;
		}
	
		if ( isNaN(obj_dist) ) {
			//ShowForm(true, "BufferInfo", MSG_7);			
			top.document.getElementById("InfoMsg_4").innerHTML = MSG_7;
			return;
		}

		//top.document.getElementById("divLoaderWait").style.visibility = 'visible';
		top.document.getElementById("InfoMsg_4").innerHTML = '&nbsp;';
	}
	catch (e) {
		//top.document.getElementById("divLoaderWait").style.visibility = 'Hidden';
		//ShowForm(true, "BufferInfo", MSG_8);		
		top.document.getElementById("InfoMsg_4").innerHTML = MSG_8;
		return;
	}
}

function cmdSearchBufferObject_Click()
{
	ShowLoader();
	ClearInfoMsg();
	top.document.getElementById("InfoMsg_4").innerHTML = MSG_12;
	document.forms["frmBuffer"].cmd.value = "SearchBuffer";
	document.forms["frmBuffer"].submit();
}

function AddBufferPoint(x, y, dist){
	frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=AddBufferPoint&x=" + x + "&y=" + y + "&dist=" + dist
}

function removeBuffer(){
	opener.top.ShowLoader();
	opener.top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=RemoveBuffer";
}

function removeBuffer2(){
	ShowLoader();
	top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=RemoveBuffer";
}

function updatePoints(x, y, dist, ekatte){
	top.document.getElementById("bufferEKATTE").value = ekatte;
	top.document.getElementById("bufferX").value = x;
	top.document.getElementById("bufferY").value = y;
	top.frmMap.location.href = "Map.asp?lng="+ g_LANG +"&action=AddBufferPoint2&x1=" + x + "&y1=" + y + "&dist1=" + dist
}

function printf(strConst){
	var pos, re;
	var paramNum = arguments.length;
	for (var i = 1; i<paramNum; i++){
		pos = i-1;
		re = new RegExp("\\{"+ pos +"\\}","i");
		strConst = strConst.replace(re, arguments[i])
	}
	return strConst;
}

function gotoAnchorName(){
	var url = window.location.href;
	var pos = url.indexOf("#");
	var sAnchor;
	if (pos != -1){
		sAnchor = url.substring(pos+1,url.length);
	}
	try {
		var oAnchor = document.getElementById(sAnchor);
		oAnchor.focus();
	} catch (e) { }
}

function mozillaTest(ev)
{
	if (m_sClientBrowserType == 'Firefox') {
		alert("clientXY (" + ev.clientX + "," + ev.clientY + ")" + "\nscreenXY (" + ev.screenX + "," + ev.screenY + ")" + "\npageXY (" + ev.pageX + "," + ev.pageY + ")");
	} else if (m_sClientBrowserType == 'IE') {
		alert("clientXY (" + event.clientX + "," + event.clientY + ")" + "\nscreenXY (" + event.screenX + "," + event.screenY + ")" + "\npageXY (" + event.pageX + "," + event.pageY + ")");
	}
}

