// JavaScript Document

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function showBigMap() {
 var divstyle = new String();
 divstyle = document.getElementById("mapSmall").style.visibility;
 if (divstyle.toLowerCase()=="visible" || divstyle == "") {
  document.getElementById('mapSmall').style.visibility='hidden';
  document.getElementById('mapBig').style.visibility='visible';
 }
}

function showSmallMap() {
 var divstyle = new String();
 divstyle = document.getElementById("mapBig").style.visibility;
 if (divstyle.toLowerCase()=="visible" || divstyle == "") {
  document.getElementById('mapBig').style.visibility='hidden';
  document.getElementById('mapSmall').style.visibility='visible';
 }
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


/*
Change posiotion of passed element.
passedPointersID - pointer's ID.
passedPanelsID - container's ID.
*/
var pos_LEFT=0;
var pos_TOP=0;

function panelPositioner(passedPointersID, passedPanelsID)
{
	var intLeftPosition=0;
	var intTopPosition=0;
	//var currObjPointer=document.getElementById(passedPointersID);
	var currObjPointer=passedPointersID;
	var currPanelForPositioning=document.getElementById(passedPanelsID);
	
	intTopPosition = currObjPointer.offsetHeight;
	if(currObjPointer.offsetParent)
	{
		intLeftPosition += currObjPointer.offsetLeft;
		intTopPosition += currObjPointer.offsetTop;
		
		var parent = currObjPointer.offsetParent;
		
		while(parent)
		{
			intLeftPosition += parent.offsetLeft;
			intTopPosition += parent.offsetTop;
			parent = parent.offsetParent;
		}
		
		intLeftPosition += document.documentElement.scrollLeft + document.body.scrollLeft;
		intTopPosition += document.documentElement.scrollTop + document.body.scrollTop;
		intLeftPosition -= document.documentElement.scrollLeft;
		intTopPosition -= document.documentElement.scrollTop;
		
		
		if(document.all) 	// IE
		{
			if(passedPointersID=='myPointerAdvMenu')
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=pos_LEFT;
			}
			else if (passedPointersID=='myPointerCatMenu')
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=pos_LEFT;
			}
			else
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=pos_LEFT;
			}
		}
		if(!document.all) 	// !IE
		{
			if(passedPointersID=='myPointerAdvMenu')
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=eval(pos_LEFT-1);
			}
			else if (passedPointersID=='myPointerCatMenu')
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=eval(pos_LEFT-1);
			}
			else
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=pos_LEFT;
			}
		}
		if(window.opera)	//	Opera
		{
			if(passedPointersID=='myPointerAdvMenu')
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=eval(pos_LEFT-1);
			}
			else if (passedPointersID=='myPointerCatMenu')
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=eval(pos_LEFT-1);
			}
			else
			{
				intTopPosition+=pos_TOP;
				intLeftPosition+=pos_LEFT;
			}
		}
		
		currPanelForPositioning.style.left=intLeftPosition+'px';
		currPanelForPositioning.style.top=intTopPosition+'px';
		//currPanelForPositioning.style.visibility='visible';
		
	}
	pos_LEFT=0;
	pos_TOP=0;

}
/*
Restore posiotion of passed element.
passedPanelsID - container's ID.
*/
function restorePositioner(passedPanelsID)
{
	var currPanelForPositioning=document.getElementById(passedPanelsID);
	currPanelForPositioning.style.left=0+'px';
	currPanelForPositioning.style.top=0+'px';
	currPanelForPositioning.style.visibility='hidden';
	
}

function Left(obj)
{
var curleft = 0;
if (obj.offsetParent)
while (1) {
curleft += obj.offsetLeft;
if (!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if (obj.x)
curleft += obj.x;
return curleft;
}

function Top(obj)
{
var curtop = 0;
if (obj.offsetParent)
while (1) {
curtop += obj.offsetTop;
if (!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if (obj.y)
curtop += obj.y;
return curtop;
}

var mouseX = 0;
var mouseY = 0;

//if (BrowserDetect.browser != 'Explorer') document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;

function getMouseXY(e) {
  //e = event || window.event;
  if (BrowserDetect.browser == 'Explorer') { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX;
    mouseY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0}
  if (mouseY < 0){mouseY = 0}  

  return true
}

function moveElement(sObj) {
  var t = document.getElementById('mainTable');
  if (t) {var w = t.scrollWidth} else { var w = 1000;}
  
  if (BrowserDetect.browser == 'Explorer') {
    sObj.style.left = mouseX - (mouseX + sObj.scrollWidth > w ? sObj.scrollWidth : 0) + "px";
    sObj.style.top = mouseY + f_scrollTop() + 5 + "px";
   } else {
	getMouseXY;
	sObj.style.marginLeft = mouseX - (mouseX + sObj.scrollWidth > w ? sObj.scrollWidth : 0) + "px";
	sObj.style.marginTop = mouseY + "px";   
   }	
}

function showHideBaloon(divID,show) {
 var d = document.getElementById(divID);
 if (d) {
  if (show == true) {
   moveElement(d);
   //panelPositioner(tagID, divID)
   d.style.visibility = 'visible';	  
  }	else {
   d.style.visibility = 'hidden';
   d.innerHtml = '';
  } 
 }
}

function autoShowHideDiv(divID,position) {
 var d = document.getElementById(divID);
 if (d) {
  if (d.style.visibility == 'hidden') {
   if (position != null) d.style.position = position;
   d.style.height = 'auto';
   d.style.visibility = 'visible';	  
   d.style.display = 'block';
   if (BrowserDetect.browser == 'Explorer') d.style.display = 'inline-block'; 
  }	else {
   d.style.visibility = 'hidden';
   d.style.display = 'none';
   if (BrowserDetect.browser == 'Explorer') d.style.display = 'none';
   if (position != null) d.style.position = 'absolute';
   d.style.height = '0px';
  } 
 }
}

function scrollDiv(direction,step,divID, divWrapper)
{
 /* direction: 0 = left scroll; 1 = right scroll*/
     var md = document.getElementById(divID); 
     var wrapper = document.getElementById(divWrapper);
     var width = parseInt(md.style.width);
     var wrpWidth = parseInt(wrapper.style.width);
     var scrStep = -step;
     var mrg = 0;
        
     mrg = parseInt(md.style.marginLeft); 
   
     if ((Math.abs(mrg) + wrpWidth <= width) || (direction == 1)) {
         if (direction == 0) {
           mrg = mrg + scrStep;
         } else if (mrg < 0) {
           mrg = mrg - scrStep;
         }
    }
    md.style.marginLeft = mrg + 'px';
}

function ajaxCall(file,f,parameters,tagID,ajaxLoader)
{
	var url = 'ajax/' + file +'.php?' + (f != '' ? '&function=' + f : '') +  parameters;
	fillup(url,tagID,ajaxLoader,false);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' трябва да съдържа e-mail адрес.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' трябва да съдържа число.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' трябва да съдържа цифри между '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' е задължително.\n'; }
  } if (errors) alert('Грешка при въвеждане:\n'+errors);
  document.MM_returnValue = (errors == '');
}