// JavaScript Document
// <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
var lang = (lang==undefined) ? 'bg' : lang;

isNS = (navigator.appName == 'Netscape');
isIE = (navigator.appName.indexOf('Microsoft') != -1);
isOP = (navigator.appName == 'Opera');
isCR = (navigator.userAgent.indexOf('Chrome') != -1)
ns4 = (document.layers);
ie4 = (document.all && !document.getElementById);
ie5 = (document.all && document.getElementById);
ns6 = (!document.all && document.getElementById);
useSWF = true;

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 xGetElementById(e) {
	if(typeof(e)!='string') return e;
	if(document.getElementById) e=document.getElementById(e);
	else if(document.all) e=document.all[e];
	else e=null;
	return e;
}
function xInnerHtml(e,h){
	if(!(e=xGetElementById(e)) || !xStr(e.innerHTML)) return null;
	var s = e.innerHTML;
	if (xStr(h)) {e.innerHTML = h;}
	return s;
}
function xStr(s){
	for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
	return true;
}

function PopupPic_(sPicURL, id) { 
     window.open( "/popup.php?pic="+sPicURL+"&id="+id, "",  
     "resizable=1,HEIGHT=400,WIDTH=400, left=3, top=5"); 
   }
function PopupPic(sPicURL) { 
     window.open( "/popup.html?"+sPicURL, "",  
     "resizable=1,HEIGHT=200,WIDTH=200, left=3, top=5"); 
   }
// main menu stuff ===================================================================
var registerOver = false;
var vMenu = new Object();
vMenu.IDs = [];
vMenu.URIs = [];
var nWait = new Number();
var timerId;
var timerHide;
var active = '';
// ********************************
// XML functions
// ********************************

function importXML(_URI) {
	if (document.implementation && document.implementation.createDocument) {
		if (!isCR) {
			xmlDoc = document.implementation.createDocument("", "", null);
			xmlDoc.onload = parseXML_NS; //createTable;
			xmlDoc.load(_URI);
		} else {
			ChromeLoadXML(_URI);
		}
	} else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) parseXML(); //createTable()
		};
		xmlDoc.load(_URI); // "/Scripts/menu_"+lang+".xml"
 	} else {
		alert('Your browser can\'t handle this script');
		return;
	}
}

function ChromeLoadXML(_URI) {
	var url = _URI; //"/swf/menu.xml";
	var httpRequest = new XMLHttpRequest();
	httpRequest.open("GET", url, false);
	httpRequest.onreadystatechange = function (e) {
		if (httpRequest.readyState == 4) {
			if(httpRequest.status == 200) {
				// alert(httpRequest.responseText);
				xmlDoc = httpRequest.responseXML; //httpRequest.responseXML
				parseXML_NS(); //createTable;
			} else {
				alert("Error loading page\n");
			}
		}
	};

	httpRequest.send(null);
}

function parseXML() {
	var x = xmlDoc.documentElement;
	//alert(x.childNodes.length);
	vMenu.IDs = [];
	vMenu.URIs = [];
	for (i=0; i<x.childNodes.length; i++) {
		// txt="Начало" ID="10" active="1" url="/index.php"
		vMenu.IDs[i] = x.childNodes(i).getAttribute("ID"); 
		// 'ID : txt : url : _self'
		vMenu.URIs[i] = x.childNodes(i).getAttribute("ID") + " : " + x.childNodes(i).getAttribute("txt") + " : " + x.childNodes(i).getAttribute("url") + " : " + "_self";
	}
	writeMenu();
}

function parseXML_NS() {
	// var x = xmlDoc.getElementsByTagName('MENU');
	var x = xmlDoc.documentElement;
	var y = x.childNodes;
	var theData = '';
	var _ID = '';
	var _txt = '';
	var _url = '';
	vMenu.IDs = [];
	vMenu.URIs = [];
	
	for (i=0;i<y.length;i++) {
		if (y[i].nodeType != 1) continue;
		// alert(y[i].attributes['ID'].nodeValue);
		_ID = y[i].attributes['ID'].nodeValue;
		_txt = y[i].attributes['txt'].nodeValue;
		_url = y[i].attributes['url'].nodeValue;
		
		vMenu.IDs[i] = _ID;
		vMenu.URIs[i] = _ID + " : " + _txt + " : " + _url + " : " + "_self";
	}
	
	writeMenu();
}
// ********************************

function findCmd(ID) {
	var i = 0;
	for (i in vMenu.IDs) { if (vMenu.IDs[i] == ID) return i; }
	return -1;
}

function replaceCharacters(conversionString,inChar,outChar) {
  var convertedString = conversionString.split(inChar);
  convertedString = convertedString.join(outChar);
  return convertedString;
}

function menuCmd(ID, p) {
	var id = Number(ID);
	var _cmd = findCmd(ID);
	var _tStr = vMenu.URIs[_cmd];
	var _parseURI = _tStr.split(' : ');
	var _txt = replaceCharacters(_parseURI[1], " ", "&nbsp;");
	var _url = (_parseURI[2] == '#') ? '#' : _parseURI[2];
	var _targ = (_parseURI[3] == undefined) ? '' : _parseURI[3];
//	var _path = (p == undefined) ? '' : './';
//	var a = (id%100 == 0) ? '' : '?a=' + ID + '&';
	var a = '?a=' + ID + '&';
	var href = ' href="' + _url + a + '" target="' + _targ +'"';
	var menu = [_parseURI[0], _txt, href]; // id, txt, url 
	return menu;
}

function addItem(ID) {
	var actChk = '';
	var act ='';
	var tmp = '';
	var id = Number(ID);
	var mnu = menuCmd(ID);
	if (id%100 == 0) {
		act = (ID==active) ? '-active' : '';
		tmp = '<div class="vmenu-nav'+act+'"><a '+mnu[2]+' onMouseDown="doIt('+ID+'); this.blur(); return false"><div class="vmenu-nav-title">'+mnu[1]+'</div><div class="vmenu-nav-bullet"><img src="/pictures/prazen.gif" width="172" height="22" border="0"></div></a></div>';
	} else {
		tmp = '<div class="vmenu-sub"><a '+mnu[2]+' onMouseDown="doIt('+ID+'); this.blur(); return false"><div class="vmenu-nav-title-sub">'+mnu[1]+'</div><div class=""><img src="/pictures/prazen.gif" width="172" height="22" border="0"></div></a></div>';
	}
	return tmp;
}


// function addSubItems(ID) {
function addSubMenus() {
	var ID = active;
	var tmp = '';
	var id = Number(ID);
	var i = 0;
	var iTmp = 0;

	//clearTimeout(nWait);
	window.clearTimeout(timerId);	
	for (i in vMenu.IDs) {
		if (Math.round(id/100) == Math.floor(Number(vMenu.IDs[i])/100)) {
			tmp += addItem(vMenu.IDs[i]);
		} else {
			if (Number(vMenu.IDs[i])%100 == 0) tmp += addItem(vMenu.IDs[i]);
		}
	}
	xInnerHtml('vmenu', tmp);
	
	o++;
}

var o = 0;
function doIt(ID) {
	if (active == ID) {
		doHide();
	} else {
		active = ID;
		if (timerId != undefined) {
			window.clearTimeout(timerId);
		}
		timerId = window.setTimeout("addSubMenus()", 300);
	}
}

function hideSubMenu() {
	var i=0;
	var id=0;
	var tmp='';
	// active = 'NONE';
	if (timerHide != undefined) { window.clearTimeout(timerHide) }
	for (i in vMenu.IDs) {
		id = Number(vMenu.IDs[i]);
		// alert(id);
		if (id%100 == 0) tmp += addItem(id);
	}
	xInnerHtml('vmenu', tmp);
}

function doHide() {
	if (timerHide != undefined) { window.clearTimeout(timerHide) }
	timerHide = window.setTimeout("hideSubMenu()", 300);
}

function getVars(varName) {
	var _i;
	var _srch = window.location.search.substr(1);
	var _vars = _srch.split("&");
	for (_i in _vars) {
		var _v = _vars[_i].split("=");
		if (_v[0] == varName) {
			return _v[1];
			break;
		}
	}
	return "NONE";
}

var imageList=new Array(); 

function initMenu() {
	// importXML();
	importXML("/Scripts/menu_"+lang+".xml");
}

function writeMenu() {
	var tmp = '';
	var id = 0;
	var i = 0;
	var actChk = '';
	
	var images=new Array('/pictures/vm_bgr_1.png','/pictures/vm_bullet.png','/pictures/vm_sub.png'); 
	for (i in images) {  // preloading imgs
  		imageList[i] = new Image(); 
  		imageList[i].src = images[i]; 
 	} 
	
	active = getVars("a");
	if (active == "NONE") {
		for (i in vMenu.IDs) {
			id = Number(vMenu.IDs[i]);
			if (id%100 == 0) tmp += addItem(id);
		}
		xInnerHtml('vmenu', tmp);
	} else {
		actChk = active.substr(0,active.length-2) + '00';
		doIt(actChk);
	}
}
// end of main menu stuff ==============================================================

function swap_img(img_src, img_txt) {
  var obj = document.getElementById('snimka');
  var obj2 = document.getElementById('img_tеxt');
	obj.src = img_src;
	obj2.innerHTML = img_txt; 
}

function crawler() {
	if (window.name!='sredna' && !((self.innerHeight == 0) && (self.innerWidth == 0))) top.location.replace('/');
}

function calcHeight_admin(koi) {
	var the_height=document.body.scrollHeight;
	var obj = parent.document.getElementById(koi);
	obj.style.height = (!isNS && !isOP) ? the_height : the_height+'px';
}

function calcHeight(koi) {
	crawler();
	var the_height=document.body.scrollHeight;
	var obj = parent.document.getElementById(koi);
	obj.style.height = (!isNS && !isOP) ? the_height : the_height+'px';
}

function sh_div(what) {
	 var i=0;
	 var coll = [];
	 while (document.getElementById('d'+ i)) {
	  coll[i] = document.getElementById('d'+ i);
		i++;
	 }
 obj = document.getElementById(what);
 var i=0;
 if (obj.className == 'off') {
	 while (coll[i]) {
		coll[i].className = 'off';
		i++;
		} 
	obj.className = 'on';
 } else {
	obj.className = 'off';
 }
}

// --- Window functions -------------------------

function openWindow(url, name) {
  popupWin = window.open(url, name, 'scrollbars=yes,resizable=yes,status=no,personal=no,menu=no,location=no,toolbar=no,left=20,top=20,width=770,height=500')
}

//example: openSizedWindow('http://www.mysite.com','myName','400','500');
function openSizedWindow(url, name, myHeight, myWidth) {
 popupWin = window.open(url, name, 'scrollbars=yes,resizable=yes,status=yes,personal=no,menu=no,location=no,toolbar=no,left=20,top=20,width=' +myWidth+',height='+myHeight+'');
}

function openSizedWindowPlain(url, name, myHeight, myWidth) {
 popupWin = window.open(url, name, 'scrollbars=no,resizable=no,status=no,personal=no,menu=no,location=no,toolbar=no,left=20,top=20,width=' +myWidth+',height='+myHeight+'');
}

function openSizedWindowUI(url, name, myHeight, myWidth) {
 popupWin = window.open(url, name, 'scrollbars=yes,resizable=yes,status=yes,personal=no,menubar=yes,location=yes,toolbar=yes,left=20,top=20,width=' +myWidth+',height='+myHeight+'');
}
// ----------------------------------------------
 function send_(f, pole1, pole2, mail_) {
	var obj7 = document.getElementById(mail_);
	var email = obj7.value;
	var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
     var what1 = "Въведете вашето име!";
     var what2 = "Напишете въпрос!";
     var obj = document.getElementById(f);
  if (obj[pole1].value == "") {
    alert (what1);
		return false;
  } else if ((emailRegxp.test(email) != true) || (email == "")) {
		alert("Въведете валиден e-mail адрес!");
		return false;
  } else if (obj[pole2].value == "") {
    alert (what2);
		return false;
  } else {
    obj.submit();
  }
}
