/* form focus functions */
function focusfield(pField) {
	lNode = pField;
	if (lNode.parentNode)
	do {
		lNode = lNode.parentNode;
	} while(lNode && lNode.tagName != 'SPAN');
	lNode.className += ' over';
}

function blurfield(pField) {
	lNode = pField;
	if (lNode.parentNode)
	do {
		lNode = lNode.parentNode;
	} while(lNode && lNode.tagName != 'SPAN');
	lNode.className = lNode.className.replace('over', '');
}

/* avatar selector functions */

var gAvatarCurPage = 0;
var gAvatarPageSize = 14;
var gAvatarCat = 0;

function getTargetEventElement(e) {
	var targ;
	if (!e) {
		var e=window.event;
	}
	if (e.target) {
		targ=e.target;
	} else if (e.srcElement) {
		targ=e.srcElement;
	}
	if (targ.nodeType == 3) { /* protiv nqkakyv safari bug */
		targ = targ.parentNode;
	}
	return targ;
}

function chooseavatar(e) {
	var targ = getTargetEventElement(e);
	document.getElementById('useravatar').src = targ.src;
	document.getElementById('avatar').value = targ.src;
	document.getElementById('avatarcat').value = 0;
	showavatars(0, 0);
}

function showavatarsoption(psel) {
	var selval = psel.options[psel.selectedIndex].value;
	showavatars(selval, 0);
}
function showAvatarsPageNext() {
	showavatars(gAvatarCat, gAvatarCurPage+1);
}
function showAvatarsPagePrev() {
	showavatars(gAvatarCat, gAvatarCurPage-1);
}
function showAvatarsPageNum(e) {
	var targ = getTargetEventElement(e);
	showavatars(gAvatarCat, targ.innerHTML-1);
}
function initAvatars() {
	showavatars(document.getElementById('avatarcat').value, 0);
}

function showAvatarsPageNumIn(e) {
	//~ var targ = getTargetEventElement(e);
	showavatars(gAvatarCat, e.innerHTML-1);
}
function showavatars(pCat, pPage) {
	var asel = document.getElementById('avatarselector');
	var aupl = document.getElementById('avataruploader');
	gAvatarCat = pCat;
	gAvatarCurPage = 0;
	if (pCat > 0 || pCat == -2) {
		gAvatarCurPage = pPage;
		asel.innerHTML = '';

		var adiv = document.createElement('DIV');
		asel.appendChild(adiv);

		//~ addAvatar(asel, 'http://login.economedia.bg/lgavatars/0.gif');

		for (i = (gAvatarCurPage * gAvatarPageSize); i < (gAvatarCurPage * gAvatarPageSize) + gAvatarPageSize; i++) {
			if (gAvatars['grp' + pCat] && gAvatars['grp' + pCat][i])
				addAvatar(asel, 'http://login.economedia.bg/lgavatars/' + (gAvatars['grp' + pCat][i]));
		}

		var avatarscnt = (gAvatars['grp' + pCat] ? gAvatars['grp' + pCat].length : 0);

		if (avatarscnt > gAvatarPageSize) {
			maxpages = Math.ceil(avatarscnt / gAvatarPageSize);
			lHtml = '<table class="avatarpageing" width="100%"><tr><td width="25%">';

			if (gAvatarCurPage > 0) {
				lHtml += '<a href="javascript: void(0);" onclick="showAvatarsPagePrev(this)">назад</a>';
			} else {
				lHtml += 'назад';
			}

			lHtml += '</td><td width="50%" align="center">';

			for(i = 0; i < maxpages; i++) {
				if (i == gAvatarCurPage) {
					lHtml += '<b>' + (i + 1) + '</b>';
				} else {
					lHtml += '<a href="javascript: void(0);" onclick="showAvatarsPageNumIn(this)">' + (i + 1) + '</a>';
				}
			}

			lHtml += '</td><td width="25%" align="right">';

			if ((gAvatarCurPage + 1) < maxpages) {
				lHtml += '<a href="javascript: void(0);" onclick="showAvatarsPageNext(this)">напред</a>';
			} else {
				lHtml += 'напред';
			}

			lHtml += '</td></tr></table>';
			adiv.innerHTML = lHtml;
		}

		asel.className = '';
		aupl.className = 'hideavatars';
	} else if (pCat == -1) {
		aupl.className = '';
		asel.className = 'hideavatars';
	} else {
		aupl.className = 'hideavatars';
		asel.className = 'hideavatars';
	}
}

function addAvatar(pCont, pSrc) {
	var newa = document.createElement('A');
	var newimg = document.createElement('IMG');
	newimg.src = pSrc;
	notclosuresrc = newimg.src;
	newa.href = '#';
	newa.onclick = chooseavatar;
	newa.appendChild(newimg);
	pCont.appendChild(newa);
}




/* other */

function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}

if (getURLParam('sendhash_old') != ''){

	hash = getURLParam('sendhash');

	switch (getURLParam('action')) {
		case 'reg' :
			jQuery(document).ready(
				function(){
					tb_show(
						"",
						"register.php?sendhash=" + hash + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=450&width=400&modal=true",
						false
					);
				}
			);
		break;
		case 'forgotten' :
			jQuery(document).ready(
				function(){
					tb_show(
						"",
						"forgotten.php?sendhash=" + hash + "&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=250&width=400&modal=true",
						false
					);
				}
			);
		break;
	}
}

/* search button dropdown */

var gSearchSitesIcons = {
	'dnevnik': {ico: 'http://login.economedia.bg/i/sico/dnevnik.gif', url: 'http://www.dnevnik.bg/search.php?tAction=search', name: 'Дневник'},
	'evropa': {ico: 'http://login.economedia.bg/i/sico/dnevnik.gif', url: 'http://evropa.dnevnik.bg/search.php?tAction=search', name: 'Европа Дневник'},
	'pazari': {ico: 'http://login.economedia.bg/i/sico/dnevnik.gif', url: 'http://pazari.dnevnik.bg/search.php?tAction=search', name: 'Пазари Дневник'},
	'live': {ico: 'http://www.dnevnik.bg/live/favicon.gif', url: 'http://www.dnevnik.bg/live/search.php?tAction=search', name: 'Дневник L!VE'},
	'capital': {ico: 'http://login.economedia.bg/i/sico/capital.gif', url: 'http://www.capital.bg/search.php?tAction=search', name: 'Капитал'},
	'zagrada': {ico: 'http://login.economedia.bg/i/sico/zagrada.gif', url: 'http://www.gradski.bg/search.php?tAction=search', name: 'Градски Вестник'},
	'karieri': {ico: 'http://login.economedia.bg/i/sico/karieri.gif', url: 'http://www.karieri.bg/search/ft.php?tAction=search', name: 'Кариери'},
	'horemag': {ico: 'http://login.economedia.bg/i/sico/horemag.gif', url: 'http://www.horemag.bg/search.php?tAction=search', name: 'Хоремаг'},
	'regal': {ico: 'http://login.economedia.bg/i/sico/regal.gif', url: 'http://www.regal.bg/search.php?tAction=search', name: 'Регал'},
	'foreignpolicy': {ico: 'http://login.economedia.bg/i/sico/fp.gif', url: 'http://www.foreignpolicy.bg/search.php?tAction=search', name: 'Foreign Policy - България'},
	'stroitelstvo': {ico: 'http://login.economedia.bg/i/sico/common.gif', url: 'http://www.stroitelstvo.info/search.php?tAction=search', name: 'Строителство градът'},
	'indeximoti': {ico: 'http://login.economedia.bg/i/sico/common.gif', url: 'http://www.indeximoti.bg/search.php?tAction=search', name: 'Индекс Имоти'},
	'bacchus': {ico: 'http://login.economedia.bg/i/sico/bacchus.gif', url: 'http://www.bacchus.bg/search.php?tAction=search', name: 'Бакхус'}
	//~ 'economedia': {ico: 'http://login.economedia.bg/i/sico/common.gif', url: 'http://www.economedia.bg/search.php?tAction=search', name: 'Икономедиа'}
};

jQuery(document).ready(function() {
	var workBoxVal = jQuery('#workbox').val();
	if(workBoxVal && ((workBoxVal == 1) || (workBoxVal == 2) || (workBoxVal == 3) || (workBoxVal == 5) || (workBoxVal == 10))) {
		jQuery('#workrelated select').removeAttr('disabled');
		jQuery('label.pale').removeClass('pale');
		var workPos = jQuery('#workpos').val();
		if(workPos && workPos == 7) {
			jQuery('#otherjobpos').show();
		}
		
		var department = jQuery('#department').val();
		if(department && department == 10) {
			jQuery('#otherdepartment').show();
		}

		if(jQuery('#editions').val()) {
			jQuery('#subseditions').show();
		}
		
	}
	
	if (jQuery('.whatHolder a').length == 0) {
		return true;
	}
	var lCurSite = document.location.host.match(/([\d\w]+)\.[\d\w:]+$/)[1];
	if (lCurSite && gSearchSitesIcons[lCurSite]) {
		jQuery('.whatHolder a img')[0].src = gSearchSitesIcons[lCurSite].ico;
		jQuery('#lgSearchForm')[0].action = gSearchSitesIcons[lCurSite].url;
		jQuery('#lgSearchForm input')[0].value = 'Търсене в ' + gSearchSitesIcons[lCurSite].name;
	}

	jQuery('.whatHolder a').click(function() {
		icopos = jQuery(this).find('img').offset();
		icopos.top += jQuery(this).find('img').height();
		icopos.left -= 2;
		if (!document.getElementById('umsSearchSel')) {
			jQuery("body").append("<div id='umsSearchSel'></div><iframe id='umsSearchSelIframe' frameborder='0' scrolling='no' src='about:blank'></iframe>");
			jQuery("#umsSearchSelIframe").css({position: 'absolute', top: (icopos.top) + 'px', left: (icopos.left) + 'px', display: 'none', width: '200px', background: '#fff', border: '1px solid white', zIndex: '99', margin: '0', padding: '0'});
			jQuery("#umsSearchSel").css({position: 'absolute', top: (icopos.top) + 'px', left: (icopos.left) + 'px', display: 'none', width: '200px', background: '#fff', border: '1px solid silver', zIndex: '100', margin: '0', padding: '0'});

			var lHeight = 0;

			for (i in gSearchSitesIcons) {
				jQuery("#umsSearchSel").append('<label for="umsSearchSel' + i + '" style="height: 20px;"><input type="radio" id="umsSearchSel' + i + '" name="umsSearchSelSite" value="' + gSearchSitesIcons[i].url + '" icosrc="' + gSearchSitesIcons[i].ico + '" iconame="' + gSearchSitesIcons[i].name + '" /> ' + gSearchSitesIcons[i].name + '</label>');
				jQuery('#umsSearchSel' + i).click(function() {
					
					jQuery('.whatHolder a img')[0].src = this.getAttribute('icosrc');
					jQuery('#lgSearchForm')[0].action = this.value;
					if (jQuery('#lgSearchForm input')[0].value.substr(0, 10) != 'Търсене в ') return 0;
					jQuery('#lgSearchForm input')[0].value = 'Търсене в ' + this.getAttribute('iconame');
				});
				lHeight += 26;
			}

			jQuery("#umsSearchSelIframe").css({height: lHeight + 'px'});
			jQuery("#umsSearchSel").css({height: lHeight + 'px'});
		}

		jQuery("#umsSearchSelIframe").show('fast');
		jQuery("#umsSearchSel").show('fast');

		return false;
	});

	jQuery(document).click(function() {
		jQuery("#umsSearchSelIframe").hide('fast');
		jQuery("#umsSearchSel").hide('fast');
	});
	
});

// Function to get url query field value
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//Function for links inside iframe
function openregthickbox() {
	tb_show('',localloginurl+'?showform=register&TB_iframe=true&height=520&width=770&modal=true','null');
}

function openloginthickbox() {
	tb_show('',localloginurl+'?showform=login&TB_iframe=true&height=240&width=351&modal=true','null');
}

function openlostpassthickbox() {
	tb_show('',localloginurl+'?showform=lostpass&TB_iframe=true&height=240&width=351&modal=true','null');
}

function openfaqthickbox() {
	tb_show('',localloginurl+'?show=faq&TB_iframe=true&height=500&width=351&modal=true','null');
}

function openreasonsthickbox() {
	tb_show('',localloginurl+'?show=reasons&TB_iframe=true&height=500&width=351&modal=true','null');
}

function openusloviathickbox() {
	tb_show('',localloginurl+'?show=uslovia&TB_iframe=true&height=500&width=351&modal=true','null');
}

function openeditprofilethickbox() {
	tb_show('',localloginurl+'?showform=editprofile&TB_iframe=true&height=550&width=770&modal=true','null');
}

function openviewprofilethickbox() {
	tb_show('',localloginurl+'?show=profile&TB_iframe=true&height=391&width=700&modal=true','null');
}

function openservicesthickbox() {
	tb_show('',localloginurl+'?showform=services&TB_iframe=true&height=400&width=770&modal=true','null');
}

function openupdatethickbox(valhash) {
	tb_show('',localloginurl+'?showform=updatepersonaldata&hash=' + valhash + '&TB_iframe=true&height=520&width=770&modal=true','null');
}

// im data field

function imFormInit() {
	
	imChangeType();
	
	jQuery("#imtype").change(function() {
		imChangeType();
	});
	
	jQuery("#imval").change(function() {
		var lImAll = jQuery("#im")[0].value;
		var lImType = jQuery("#imtype")[0].options[jQuery("#imtype")[0].selectedIndex].value;
		jQuery("#im")[0].value = imSetToStr(lImAll, lImType, this.value);
	});
}

function imChangeType() {
	var lImAll = jQuery("#im")[0].value;
	var lImType = jQuery("#imtype")[0].options[jQuery("#imtype")[0].selectedIndex].value;
	jQuery("#imval")[0].value = imGetFromStr(lImAll, lImType);
}

function imGetFromStr(pImStr, pType) {
	var lArr = pImStr.split(/\t\t\t/);
	if (lArr.length) {
		for (var i = 0; i < lArr.length; i++) {
			var lIm = lArr[i].split(':::');
			if (pType == lIm[0]) return (lIm[1] ? lIm[1] : '');
		}
	}
	return '';
}

function imSetToStr(pImStr, pType, pVal) {
	var lArr = pImStr.split(/\t\t\t/);
	var lRetArr = new Array();
	var lNewCnt = 0;
	var lSet;
	if (lArr.length) {
		for (var i = 0; i < lArr.length; i++) {
			var lIm = lArr[i].split(':::');
			if (pType == lIm[0]) {
				if (pVal != '') {
					lRetArr[lNewCnt++] = pType + ':::' + pVal;
				}
				lSet = true;
			} else {
				lRetArr[lNewCnt++] = lArr[i];
			}
		}
		if (!lSet) {
			if (pVal != '') {
				lRetArr[lNewCnt++] = pType + ':::' + pVal;
			}
		}
	}
	return lRetArr.join("\t\t\t");
}