function hideValue(what, def)
{
	if(trim(what.value) == def)
	{
		what.value = "";
	}
	return;
}

function showValue(what, def)
{
	if(trim(what.value) == '')
	{
		what.value = def;
	}
	return;
}

// Removes leading whitespaces
function LTrim( value )
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value )
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value )
{
	return LTrim(RTrim(value));
	
}

function loadImages(path, pics)
{
	pics_arr = pics.split(',');
	for(i=0; i < pics_arr.length; i++)
	{
		eval('c' + i + ' = new Image();');
		eval('c' + i + '.src = path + "' + pics_arr[i] + '";');
	}
}

function changeBgck(what, img)
{
	document.getElementById(what).style.backgroundImage = "url('"+img+"')";
	//document.getElementById(what).style.backgroundPosition = 'middle';
}

function preloadImages(img_array)
{
	pics_array = img_array.split(',');
	for(i = 0; i < pics_array.length; i++)
	{
		eval("c"+i+" = new Image; c"+i+".src = pics_array[i];");
	}
}

var timer_id;
var direction=0;
var curr = 0;
function scroll_iframe(frm,inc,dir)
{
  if (timer_id) clearTimeout(timer_id);
  if (window.frames[frm])
  {
    if (direction == 0)
    {
    	curr += inc;
    	window.frames[frm].scrollBy(0, inc);
    	if(curr >= window.frames[frm].document.body.offsetHeight)
    	{
    		direction = 1;
    		curr = window.frames[frm].document.body.offsetHeight;
    	}
    }
    else if (direction == 1)
    {
    	curr -= inc;
    	window.frames[frm].scrollBy(0, -inc);
    	if(curr <= 0)
    	{
    		direction = 0;
    		curr = 0;
    	}
    }
    //else window.frames[frm].scrollBy(inc, 0);
    timer_id = setTimeout("scroll_iframe('" + frm + "'," + inc + ",'" + dir + "')", 5);
  }
}

function stopScroll()
{
	if (timer_id) clearTimeout(timer_id);
}

function change_class(tid, cfg)
{
	if(cfg == 1) newclass = 'admin_button';
	else if(cfg == 2) newclass = 'admin_button_over';
	
	//var dtarget = document.getElementById(tid);
	
	tid.className = newclass;
}

Array.prototype.in_array = function ( obj )
{
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ )
	{
		if ( this[x] == obj ) return true;
	}
	return false;
}

function getAreas(formname, area_id_field, city_id_field, country_id , selected_area_id , selected_city_id)
{
	var pars = 'doit=1&country_id=' + country_id;
	
	new Ajax.Request('http://www.job4you.bg/ajax/get_areas.php', {method: 'post', postBody:pars, onSuccess: function(transport)
		{
			var result = transport.responseText.split('|');
			
			var snew_res = 0;
			eval("var sel = document." + formname + "." + area_id_field + ";");
			sel.options.length = 0;
			sel.options[0] = new Option('-- област --', '');
			
			for(var i = 0; i < result.length; i++)
			{
				if(result[i] != '')
				{
					var new_res_array = result[i].split(',');
					
					sel.options[(i+1)] = new Option(new_res_array[1], new_res_array[0]);
					if(selected_area_id == new_res_array[0]) sel.options[(i+1)].selected = true;
				}
			}
			
			getCities(formname, city_id_field, selected_area_id , selected_city_id);
		}
	});
}

function getCities(formname, city_id_field, area_id, selected_city_id)
{
	var pars = 'doit=1&area_id=' + area_id;
	
	new Ajax.Request('http://www.job4you.bg/ajax/get_cities.php', {method: 'post', postBody:pars, onSuccess: function(transport)
		{
			var result = transport.responseText.split('|');
			
			var snew_res = 0;
			eval("var sel = document." + formname + "." + city_id_field + ";");
			sel.options.length = 0;
			sel.options[0] = new Option('-- град --', '');
			
			for(var i = 0; i < result.length; i++)
			{
				if(result[i] != '')
				{
					var new_res_array = result[i].split(',');
					
					sel.options[(i+1)] = new Option(new_res_array[1], new_res_array[0]);
					if(selected_city_id == new_res_array[0]) sel.options[(i+1)].selected = true;
				}
			}
		}
	});
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function edit_publish_ad() {
	document.getElementById('preview_box').style.display = 'none';
	document.getElementById('publish_box').style.display = '';
}

function publish_ad() {
	document.getElementById('preview_text').value = 1;
	document.publishform.submit();
}

function verify_question() {
	var form = document.add_q_form;
	if(form.question_text.value == '') return alert('Моля въведете въпрос!');
	else {
		if(getCheckedValue(form.r1) == 1) form.submit();
		else {
			var points = 0;
			if(form.answer1.value != '') points += 1;
			if(form.answer2.value != '') points += 1;
			if(form.answer3.value != '') points += 1;
			if(form.answer4.value != '') points += 1;
			if(form.answer5.value != '') points += 1;
			
			if(points < 2) return alert('Моля посочете поне два възможни отговора!');
			else form.submit();
		}
	}

}

function verify_question_edit(id) {
	eval("var form = document.edit_form_"+ id + ";");
	if(form.question_text.value == '') return alert('Моля въведете въпрос!');
	else {
		if(getCheckedValue(form.r1) == 1) form.submit();
		else {
			var points = 0;
			if(form.answer1.value != '') points += 1;
			if(form.answer2.value != '') points += 1;
			if(form.answer3.value != '') points += 1;
			if(form.answer4.value != '') points += 1;
			if(form.answer5.value != '') points += 1;
			
			if(points < 2) return alert('Моля посочете поне два възможни отговора!');
			else form.submit();
		}
	}

}

function edit_question(id) {
	document.getElementById('question_'+id).style.display = 'none';
	document.getElementById('edit_question_'+id).style.display = '';
}

function cancel_edit(id) {
	document.getElementById('question_'+id).style.display = '';
	document.getElementById('edit_question_'+id).style.display = 'none';
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		// alert("Invalid E-mail ID")
		 return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		 //alert("Invalid E-mail ID")
		 return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			//alert("Invalid E-mail ID")
			return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
			//alert("Invalid E-mail ID")
			return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			//alert("Invalid E-mail ID")
			return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
			//alert("Invalid E-mail ID")
			return false
	 }
	
	 if (str.indexOf(" ")!=-1){
			//alert("Invalid E-mail ID")
			return false
	 }

	 return true					
}


function submit_feedback() {
	var form = document.feedback_form;
	
	if(echeck(form.email.value) == false) {
		alert('Моля въведете валиде e-mail!');
		return false;
	}
	
	else if(form.message.value == '') {
		alert('Моля въведете съобщение!');
		return false;
	}
	else form.submit();
	
}

function shFaq(what)
{
	if(document.getElementById('faq' + what))
	{
		var tfaq = document.getElementById('faq' + what);
		
		if(tfaq.style.display == '') tfaq.style.display = 'none';
		else if(tfaq.style.display == 'none') tfaq.style.display = '';
	}
}

function verify_private_data_form() {
	var form = document.private_data_form;
	if(form.realname.value == '') {
		alert("Моля посочете вашето име!");
		return false;
	}
	else if(form.familyname.value == '') {
		alert("Моля посочете вашето фамилно име!");
		return false;	
	}
	else if(form.phone.value == '') {
		alert("Моля посочете телефон за контакти!");
		return false;
	}
	else if(echeck(form.email.value) == false) {
		alert("Моля посочете валиден e-mail адрес!");
		return false;
	}
	else form.submit();
}

function verify_change_password() {
	var form = document.change_password_form;
	if(form.old_pass.value == '') {
		alert("Моля въведете вашата стара парола!");
		return false;
	}
	
	if(form.new_pass.value == '') {
		alert("Моля въведете вашата нова парола!");
		return false;
	}
	
	if(form.new_pass.value.length  < 6) {
		alert("Новата ви парола трябва да бъде минимум 6 символа");
		return false;
	}
	
	if(form.new_pass.value != form.new_pass1.value) {
		alert("Новите пароли не съвпадат!");
		return false;
	}
	else form.submit();
}

function verify_firm_form() {
	var form = document.firm_form;
	if(form.firm_name.value == "") {
		alert("Моля въведете име на фирмата!");
		return false;
	}
	
	if(form.address_label.value == '') {
		alert("Моля въведете адрес на фирмата!");
		return false;
	}
	
	if(form.tax_number.value == '') {
		alert("Моля въведете данъчен номер!");
		return false;
	}
	
	if(form.bulstat_label.value == '') {
		alert("Моля въведете булстат!");
		return false;
	}
	
	if(form.registration_address.value == '') {
		alert("Моля въведете адрес по регистрацията!");
		return false;
	}
	
	if(form.mol.value == '') {
		alert("Моля въведете МОЛ!");
		return false;
	}
	
	if(form.phone_label.value == '') {
		alert("Моля въведете телефон!");
		return false;
	}
	
	else form.submit();
	
}

function checkForm(formname, fields_array, errors_array)
{
	eval("var reg = document." + formname + ";");
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var curr_field = reg." + fields_array[i] + ";");
		if(trim(curr_field.value) == '')
		{
			alert(errors_array[i]);
			curr_field.focus();
			return false;
		}
	}
	reg.submit();
}

function addFilter() {
	document.getElementById('add_filter_box').style.display = 'none';
	document.getElementById('add_box').style.display = '';
}

function cancelAddFilter() {
	document.getElementById('add_filter_box').style.display = '';
	document.getElementById('add_box').style.display = 'none';
}

function verify_add_filter() {
	var form = document.add_form;
	if(form.title.value == '') {
		alert('Моля посочете заглавие на филтъра!');
		return false;
	}
	else form.submit();
}

function show_hide_search_form() {
	if(document.getElementById('search_box_').style.display == 'none') {
		document.getElementById('search_box_').style.display = '';
		document.search_form_status_img.src = 'http://www.job4you.bg/images/default/jb_plus_icon.gif';
	}
	else {
		document.getElementById('search_box_').style.display = 'none';
		document.search_form_status_img.src = 'http://www.job4you.bg/images/default/jb_minus_icon.gif';
	}
}

function CreateBookmarkLink() { 
	title = document.title;  
	url = window.location.href
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,"");	
	} 
	else if( window.external ) { 
	window.external.AddFavorite( url, title); 
	}	
	else if(window.opera && window.print) { 
		return true; 
	} 
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowW, windowH;
	if (self.innerHeight) {	// all except Explorer
		windowW = self.innerWidth;
		windowH = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowW = document.documentElement.clientWidth;
		windowH = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowW = document.body.clientWidth;
		windowH = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowH){
		pageHeight = windowH;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowW){	
		pageWidth = windowW;
	} else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowW,windowH) 
	return arrayPageSize;
}

function winWidth()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    myWidth = window.innerWidth;
  }
  else if( document.documentElement && document.documentElement.clientWidth )
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  }
  else if( document.body && document.body.clientWidth )
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function winHeight()
{
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' )
  {
    //Non-IE
    myHeight = window.innerHeight;
  }
  else if( document.documentElement && document.documentElement.clientHeight )
  {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  }
  else if( document.body && document.body.clientHeight )
  {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getScrollY()
{
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' )
 	{
    //Netscape compliant
    scrOfY = window.pageYOffset;
  }
  else if( document.body && document.body.scrollTop )
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  }
  else if( document.documentElement && document.documentElement.scrollTop )
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function getScrollY()
{
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' )
 	{
    //Netscape compliant
    scrOfY = window.pageYOffset;
  }
  else if( document.body && document.body.scrollTop )
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  }
  else if( document.documentElement && document.documentElement.scrollTop )
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function getScrollX()
{
  var scrOfX = 0;
  if( typeof( window.pageXOffset ) == 'number' )
 	{
    //Netscape compliant
    scrOfX = window.pageXOffset;
  }
  else if( document.body && document.body.scrollLeft )
  {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  }
  else if( document.documentElement && document.documentElement.scrollLeft )
  {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function closeBox()
{
	if(document.getElementById('showBox')) 
	{
		document.body.removeChild(document.getElementById('showBox'));
		document.body.removeChild(document.getElementById('showBoxContent'));
		
		var elementBody = document.getElementsByTagName('html');
		elementBody[0].style.overflow = '';
		elementBody[0].onselectstart = function() {return true;};
	}
}


function showBigBox(targetURL, twidth, theight)
{
	//opera fix
	var agt = navigator.userAgent.toLowerCase();
  var is_opera = (agt.indexOf("opera") != -1);
	if(is_opera) txt = Url.decode(txt);
	
	var sizes = getPageSize();
	var scrollWidth = getScrollX();
	var scrollHeight = getScrollY();
	var windowWidth = winWidth();
	var windowHeight = winHeight();
	var bodyHeight = getPageSize();
	
	//main div
	dl = document.createElement('div');
	dl.id = 'showBox';
	dl.style.position = 'absolute';
	dl.style.width = '100%';
	//dl.style.height = parseInt(document.getElementById('allover').offsetHeight) + 40 + 'px';
	dl.style.height = bodyHeight[1] + 'px';
	dl.style.backgroundColor = '#000000';
	dl.style.top = 0+'px';
	//dl.style.top = scrollHeight+'px';
	dl.style.left = scrollWidth+'px';
	dl.style.textAlign = 'center';
	dl.style.filter='alpha(opacity=70)';
	dl.style.opacity='.70';
	dl.style.zIndex = 1000;
	
	//content div
	pl = document.createElement('div');
	pl.id = 'showBoxContent';
	pl.style.cssText = 'float:left;';
	pl.style.position = 'absolute';
	pl.style.backgroundColor = '#FFFFFF';
	pl.style.top = ((windowHeight/2)+scrollHeight)-200+'px';
	pl.style.left = '50%';
	pl.style.marginLeft = '-' + (twidth + 3)/2 +'px';
	pl.style.padding = '10px 5px 10px 14px';
	pl.style.width = twidth + 3 + 'px';
	pl.style.height = theight + 45 + 'px';
	pl.style.textAlign = 'center';
	pl.style.zIndex = 1001;
	pl.style.backgroundImage = 'url(\'http://www.job4you.bg/images/default/spinner.gif\')';
	pl.style.backgroundPosition = 'center';
	pl.style.backgroundRepeat = 'no-repeat';
	
	//close div
	cl = document.createElement('div');
	cl.id = 'showBoxContentClose';
	cl.style.cssText = 'float:left;';
	cl.style.width = twidth + 7 + 'px';
	//cl.style.border = 'red 1px solid';
	cl.style.backgroundColor = '#FFFFFF';
	cl.style.textAlign = 'left';
	cl.style.padding = '10px 0px 10px 0px';
	//cl_in = '<div style="float:left;width:400px;color:#1e9e09;font-size:12px;">' + txt + '</div>';
	cl_in = '<div style="float:right;width:90px;text-align:left;"><a href="javascript:closeBox();" style="text-decoration:none;color:#666666;white-spacing:nowrap;">Затвори <img src="http://www.job4you.bg/images/default/close.gif" alt="Затвори" style="vertical-align:middle;border:0px;"/></a></div>';
	cl.innerHTML = cl_in;
	
	//content
	content_div = document.createElement('div');
	content_div.style.cssText = 'float:left;';
	content_div.style.width = twidth + 'px';
	content_div.style.height = theight + 'px';
	content_div.id = 'contentBox';
	
	pl.appendChild(content_div);
	pl.appendChild(cl);
	
	var pars = '';
	new Ajax.Updater('contentBox', targetURL, {method:'get', postBody:pars, evalScripts:true, onSuccess:function() { removeSpinner(); } });
	
	document.body.insertBefore(dl, document.body.firstChild);
	document.body.insertBefore(pl, document.body.firstChild);
	
	var elementBody = document.getElementsByTagName('html');
	elementBody[0].style.overflow = 'hidden';
	elementBody[0].onselectstart = function() {return true;};
}

function removeSpinner() {
	pl.style.backgroundImage = '';
}

function switch_lng_box(id) {
	var cbox = document.getElementById('language_check_' + id);
	if(cbox.checked == true) document.getElementById('language_select_' + id).style.display = '';
	else document.getElementById('language_select_' + id).style.display = 'none';
}

function changeCVlangu() {
	var value = document.getElementById('langu').value;
	document.location='?act=mycv&lid=' + value;
}

function verify_motivation() {
	var form = document.mo_form;
	if(form.description.value == '') {
		alert('Моля попълнете мотивационното писмо!');
		return false;
	}
	else form.submit();
}