// 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 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];");
	}
}

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 x;
	}
	return false;
}

function valid_email(email)
{
	var chk = /^[^@]{1,64}@[^@]{1,255}$/g;
	if ( !email.match ( chk ) )
	{
		return false;
	}
	
	email_array = email.split("@");
	local_array = email_array[0].split(".");
	for (var i = 0; i < local_array.length; i++)
	{
		var chk = /^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/g;
		if ( !local_array[i].match ( chk ) )
		{ 
			return false;
		}
	}
	
	var chk = /^\[?[0-9\.]+\]?$/g;
	if ( !email_array[1].match ( chk ) )
	{
		domain_array = email_array[1].split(".");
		if(domain_array.length < 2) return false;
		for (var i = 0; i < domain_array.length; i++)
		{
			if(domain_array[i].length < 2) return false;
			
			var chk = /^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/g;
			if ( !domain_array[i].match ( chk ) ) return false;
		}
	}
	return true;
}

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) == '')
		{
			curr_field.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.' + formname + '.' + fields_array[i] + '.focus();');
			return false;
		}
	}
	reg.submit();
}

function setHomepage(what)
{
	if (document.all)
	{
		what.style.behavior='url(#default#homepage)';
		what.setHomePage('http://www.vantony.com/');
	}
	else if (window.sidebar)
	{
		if(window.netscape)
		{
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}  
			catch(e)
			{
				var msg = "Това действие е забранено от Вашия браузер. Ако искате да го активирате, моля въведете 'about:config' в адресната линия, и сменете стойноста на 'signed.applets.codebase_principal_support' на 'true'";
				showLightboxAlert(msg, 'Грешка', '');
			}
		}
	}
}

function addToFavorites(url, title)
{
	// Mozilla Firefox Bookmark
	if ( window.sidebar )
	{
		window.sidebar.addPanel(title, url, "");
	}
	// IE Favorite
	else if( window.external )
	{
		window.external.AddFavorite( url, title);
	}
	// Opera Hotlist
	else if( window.opera && window.print )
	{
		//Opera Hotlist
    obj.setAttribute('href', url);
    obj.setAttribute('title', title);
    obj.setAttribute('rel', 'sidebar');
    obj.click();
    return false;
	}
}

function submitOnEnter(myfield, submit_function, e)
{
	var keycode;
	
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	{
		eval(submit_function);
		return false;
	}
	else return true;
}

function IsImageOk(img)
{
	if(!img.complete)
	{
		return false;
	}
	
	if(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
	{
		return false;
	}
	
	return true;
}

function closeBox(eval_script)
{
	if(document.getElementById('showMessage')) 
	{
		document.body.removeChild(document.getElementById('showMessage'));
		document.body.removeChild(document.getElementById('showMessageBox'));
		
		eval(eval_script);
		
		//enable scroll
		var elementBody = document.getElementsByTagName('html');
		elementBody[0].style.overflow = '';
		elementBody[0].onselectstart = function() {return true;};
	}
}

function showLightboxAlert(msg, msg_title, eval_script)
{
	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 = 'showMessage';
	dl.style.position = 'absolute';
	dl.style.width = '100%';
	dl.style.height = bodyHeight[1] + 'px';
	dl.style.backgroundColor = '#000000';
	dl.style.top = 0+'px';
	dl.style.left = scrollWidth+'px';
	dl.style.textAlign = 'center';
	dl.style.filter='alpha(opacity=70)';
	dl.style.opacity='.70';
	dl.style.zIndex = 1000;
	
	//msg div
	pl = document.createElement('div');
	pl.id = 'showMessageBox';
	pl.style.width = '300px';
	pl.style.position = 'absolute';
	pl.style.backgroundColor = '#FFFFFF';
	pl.style.top = ((windowHeight/2)+scrollHeight)-100+'px';
	pl.style.left = '50%';
	pl.style.marginLeft = '-150px';
	pl.style.padding = '0px';
	pl.style.textAlign = 'left';
	pl.style.border = '#000000 1px solid';
	pl.style.zIndex = 1001;
	
	//close div
	cl = document.createElement('div');
	cl.id = 'showMessageBoxClose';
	cl.className = 'alert_bottom';
	cl.innerHTML = '<input type="button" onclick="javascript:closeBox(\'' + eval_script + '\');" class="default_button" value="Затвори"/>';
	
	pl.innerHTML = '<div class="alert_top"><img src="http://www.vantony.com/images/default/vt_icon_diamond.png" class="diamond dmr10" alt=""/><b>' + msg_title + '</b></div>';
	pl.innerHTML += '<div class="alert_holder">' + msg + '</div>';
	pl.appendChild(cl);
	
	document.body.insertBefore(dl, document.body.firstChild);
	document.body.insertBefore(pl, document.body.firstChild);
	
	//disable scroll
	var elementBody = document.getElementsByTagName('html');
	elementBody[0].style.overflow = 'hidden';
	elementBody[0].onselectstart = function() {return false;};
}

function showBigBox(targetURL, twidth, theight, txt)
{
	//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 = 'showMessage';
	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 = 'showMessageBox';
	pl.style.cssText = 'float:left;';
	pl.style.position = 'absolute';
	pl.style.backgroundColor = '#FFFFFF';
	pl.style.top = ((windowHeight/2)+scrollHeight)-250+'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.vantony.com/images/default/spinner.gif\')';
	pl.style.backgroundPosition = 'center';
	pl.style.backgroundRepeat = 'no-repeat';
	pl.style.overflowY = 'auto';
	pl.style.overflowX = 'hidden';
	
	//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;padding-left:5px;width:' + (twidth - 105) + 'px;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.vantony.com/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});
	
	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 false;};
}

function ExtractNumber(value)
{
	var n = parseInt(value);
	return n == null || isNaN(n) ? 0 : n;
}

function digitCheck(what)
{
	var chk = /[^0-9]/g;
	if ( chk.exec ( what.value ) )
	{ 
		alert ( 'Позволени са само цифри.' );
		what.value = what.value.replace( chk, "" );
		what.focus();
		return false;
	}
	return true;
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name )
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	
	return unescape( document.cookie.substring( len, end ) );
}

function Delete_Cookie( name, path, domain, secure )
{
	var expires_date = new Date();
	expires_date.setTime(expires_date.getTime() - 100);
	
	document.cookie = name + "=" +
	( ( expires_date ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

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 fixLoginUser(what, default_phrase)
{
	if(trim(what.value) == default_phrase) what.value = '';
	else if(trim(what.value) == '') what.value = default_phrase;
}

function fixLoginPass(what, default_phrase)
{
	if(trim(what.value) == default_phrase) what.value = '';
	else if(trim(what.value) == '') what.value = default_phrase;
}

function fixSearch(what, default_phrase)
{
	if(trim(what.value) == default_phrase) what.value = '';
	else if(trim(what.value) == '') what.value = default_phrase;
}

function checkSearchForm(default_phrase)
{
	var reg = document.searchform;
	
	if(trim(reg.phrase.value) == '' || trim(reg.phrase.value) == default_phrase)
	{
		showLightboxAlert('Моля, въведете фраза за търсене.', 'Грешка', 'document.searchform.phrase.focus();');
		return false;
	}
	
	if(trim(reg.phrase.value).length < 3)
	{
		showLightboxAlert('Моля, въведете фраза по-дълга от 2 символа.', 'Грешка', 'document.searchform.phrase.focus();');
		return false;
	}
	
	reg.submit();
}

function checkLoginForm(default_user, default_pass)
{
	var reg = document.loginform;
	
	if(trim(reg.username.value) == '' || trim(reg.username.value) == default_user)
	{
		showLightboxAlert('Моля, въведете Вашето потребителско име.', 'Грешка', 'document.loginform.username.focus();');
		return false;
	}
	
	if(trim(reg.password.value) == '' || trim(reg.password.value) == default_pass)
	{
		showLightboxAlert('Моля, въведете Вашата парола.', 'Грешка', 'document.loginform.password.focus();');
		return false;
	}
	
	reg.submit();
}

function checkRegisterForm()
{
	var reg = document.registerform;
	
	var fields_array = Array('username', 'password', 'password_retype', 'name', 'family_name', 'country'
	, 'city', 'email', 'verify_code');
	var errors_array = Array('Моля, въведете потребителско име.', 'Моля, въведете парола.', 'Моля, повторете паролата.'
	, 'Моля, въведете Вашето име.', 'Моля, въведете Вашата фамилия.', 'Моля, въведете държавата, в която се намирате.'
	, 'Моля, въведете градът, в който се намирате.', 'Моля, въведете Вашият e-mail адрес.', 'Моля, попълнете полето с идентификационния код.'
	, 'Не сте повторили правилно паролата.', 'Моля, въведете валиден e-mail адрес.');
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.registerform.' + fields_array[i] + '.focus();');
			return false;
		}
		//passwords check
		else if(i == 2)
		{
			if(trim(reg.password.value) != trim(reg.password_retype.value))
			{
				currfield.value = '';
				currfield.focus();
				showLightboxAlert(errors_array[9], 'Грешка', 'document.registerform.' + fields_array[i] + '.focus();');
				return false;
			}
		}
		//email check
		else if(i == 7)
		{
			if(!valid_email(trim(currfield.value)))
			{
				currfield.focus();
				showLightboxAlert(errors_array[10], 'Грешка', 'document.registerform.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	if(!reg.agree_terms.checked)
	{
		showLightboxAlert('Трябва да потвърдите, че сте съгласни с <a href="?act=terms" class="underline" title="Общи условия">общите условия</a> на сайта.', 'Грешка', '');
		return false;
	}
	
	reg.submit();
}

function checkEditInfoForm()
{
	var reg = document.editform;
	
	var fields_array = Array('name', 'family_name', 'country', 'city');
	var errors_array = Array('Моля, въведете Вашето име.', 'Моля, въведете Вашата фамилия.', 'Моля, въведете държавата, в която се намирате.'
	, 'Моля, въведете градът, в който се намирате.');
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.editform.' + fields_array[i] + '.focus();');
			return false;
		}
	}
	
	reg.submit();
}

function checkChangePasswordForm()
{
	var reg = document.changepassform;
	
	var fields_array = Array('old_password', 'new_password', 'new_password_retype');
	var errors_array = Array('Моля, въведете старата парола.', 'Моля, въведете новата парола.', 'Моля, повторете паролата.'
	, 'Не сте повторили правилно паролата.');
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.changepassform.' + fields_array[i] + '.focus();');
			return false;
		}
		//passwords check
		else if(i == 2)
		{
			if(trim(reg.new_password.value) != trim(reg.new_password_retype.value))
			{
				currfield.value = '';
				currfield.focus();
				showLightboxAlert(errors_array[3], 'Грешка', 'document.changepassform.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	reg.submit();
}

function checkContactsForm()
{
	var reg = document.contactform;
	
	var fields_array = Array('email', 'title', 'text', 'verify_code');
	var errors_array = Array('Моля, въведете Вашият e-mail адрес.', 'Моля, въведете заглавие на запитването.', 'Моля, въведете текст на запитването.'
	, 'Моля, попълнете полето с идентификационния код.', 'Моля, въведете валиден e-mail адрес.');
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.contactform.' + fields_array[i] + '.focus();');
			return false;
		}
		//email check
		else if(i == 0)
		{
			if(!valid_email(trim(currfield.value)))
			{
				currfield.focus();
				showLightboxAlert(errors_array[4], 'Грешка', 'document.contactform.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	reg.submit();
}

function checkLPForm()
{
	var reg = document.lostpass;
	
	var fields_array = Array('email');
	var errors_array = Array('Моля, въведете Вашият e-mail адрес.', 'Моля, въведете валиден e-mail адрес.');
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.lostpass.' + fields_array[i] + '.focus();');
			return false;
		}
		//email check
		else if(i == 0)
		{
			if(!valid_email(trim(currfield.value)))
			{
				currfield.focus();
				showLightboxAlert(errors_array[1], 'Грешка', 'document.lostpass.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	reg.submit();
}

function checkCPForm()
{
	var reg = document.changepass;
	
	var fields_array = Array('email', 'password', 'password_retype');
	var errors_array = Array('Моля, въведете Вашият e-mail адрес.', 'Моля, въведете парола.', 'Моля, повторете паролата.'
	, 'Моля, въведете валиден e-mail адрес.', 'Не сте повторили правилно паролата.');
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.changepass.' + fields_array[i] + '.focus();');
			return false;
		}
		//email check
		else if(i == 0)
		{
			if(!valid_email(trim(currfield.value)))
			{
				currfield.focus();
				showLightboxAlert(errors_array[3], 'Грешка', 'document.changepass.' + fields_array[i] + '.focus();');
				return false;
			}
		}
		//passwords check
		else if(i == 2)
		{
			if(trim(reg.password.value) != trim(reg.password_retype.value))
			{
				currfield.value = '';
				currfield.focus();
				showLightboxAlert(errors_array[4], 'Грешка', 'document.changepass.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	reg.submit();
}

function expandCollapse(did)
{
	var tdiv = document.getElementById(did);
	
	if(tdiv)
	{
		if(tdiv.style.display == '') Effect.BlindUp(did);
		else Effect.BlindDown(did);
	}
}

function showNextPic()
{
	var current_pic_pos = pic_id_array.in_array(parent_pic_id);
	
	if(current_pic_pos >= 0)
	{
		if(pic_id_array[(current_pic_pos + 1)] != undefined && pic_id_array[(current_pic_pos + 1)] != '') pic_id = pic_id_array[(current_pic_pos + 1)];
		else pic_id = pic_id_array[0];
		
		$('pic_box_' + parent_pic_id).fade();
		$('pic_box_' + pic_id).appear();
		
		parent_pic_id = pic_id;
	}
}

function showPrevPic()
{
	var current_pic_pos = pic_id_array.in_array(parent_pic_id);
	
	if(current_pic_pos >= 0)
	{
		if(pic_id_array[(current_pic_pos - 1)] != undefined && pic_id_array[(current_pic_pos - 1)] != '') pic_id = pic_id_array[(current_pic_pos - 1)];
		else pic_id = pic_id_array[(pic_id_array.length - 1)];
		
		$('pic_box_' + parent_pic_id).fade();
		$('pic_box_' + pic_id).appear();
		
		parent_pic_id = pic_id;
	}
}

function showExactPic(pic_id)
{
	var current_pic_pos = pic_id_array.in_array(parent_pic_id);
	var exact_pic_pos = pic_id_array.in_array(pic_id);
	
	if(parent_pic_id != pic_id)
	{
		$('pic_box_' + parent_pic_id).fade();
		$('pic_box_' + pic_id).appear();
		
		parent_pic_id = pic_id;
	}
}

function addToCart(pid, pprice_leva, pprice_euro, unique_article, animate_div_id)
{
	var scvalue = '';
	
	if(cscvalue = Get_Cookie('sclist'))
	{
		//check exists
		var scarray = cscvalue.split('|');
		for(var i = 0; i < scarray.length; i++)
		{
			var cscarr = scarray[i].split(',');
			if(pid == cscarr[0] && cscarr[4] == 1)
			{
				showLightboxAlert('Този артикул вече е добавен в пазарната количка.', 'Грешка', '');
				return false;
			}
		}
		
		scvalue = cscvalue + '|';
	}
	
	scvalue += pid + ',1,' + pprice_leva + ',' + pprice_euro + ',' + unique_article;
	
	Set_Cookie('sclist', scvalue, 365, '/', '', 0);
	
	var cscvalue = Get_Cookie('sclist');
	var scarray = cscvalue.split('|');
	var levaprice = 0;
	var europrice = 0;
	var artcount = 0;
	for(var i = 0; i < scarray.length; i++)
	{
		var cscarr = scarray[i].split(',');
		levaprice += parseInt(cscarr[2]) * parseInt(cscarr[1]);
		europrice += parseInt(cscarr[3]) * parseInt(cscarr[1]);
		artcount += parseInt(cscarr[1]);
	}
	
	if(animate_div_id != '' && unique_article == 1)
	{
		document.getElementById(animate_div_id).style.position = 'absolute';
		document.getElementById(animate_div_id).style.zIndex = 1000;
		
		JQ('#' + animate_div_id)
			.animate( {
				left : "450px",
				top : "0px",
				opacity : 0
			}, 1300, "swing", function(){$(this).hide();} );
		
		if(document.getElementById('p_' + pid) && 'p_' + pid != animate_div_id) removeElement('p_' + pid);
		if(document.getElementById('lp_' + pid) && 'lp_' + pid != animate_div_id) removeElement('lp_' + pid);
		if(document.getElementById('mvp_' + pid) && 'mvp_' + pid != animate_div_id) removeElement('mvp_' + pid);
		if(document.getElementById('pom_' + pid) && 'pom_' + pid != animate_div_id) removeElement('pom_' + pid);
		if(document.getElementById('p_' + pid + '_button') && 'p_' + pid + '_button' != animate_div_id) removeElement('p_' + pid + '_button');
	}
	
	document.getElementById('sc_articles_count').innerHTML = artcount;
	document.getElementById('sc_price_count').innerHTML = levaprice;
}

function removeFromCart(pid)
{
	
	var scvalue = Array();
	
	if(cscvalue = Get_Cookie('sclist'))
	{
		//check exists
		var scarray = cscvalue.split('|');
		for(var i = 0; i < scarray.length; i++)
		{
			var cscarr = scarray[i].split(',');
			if(pid != cscarr[0])
			{
				scvalue.push(cscarr[0] + ',' + cscarr[1] + ',' + cscarr[2] + ',' + cscarr[3] + ',' + cscarr[4]);
			}
		}
	}
	
	if(scvalue.length > 0)
	{
		scvalue = scvalue.join('|');
		Set_Cookie('sclist', scvalue, 365, '/', '', 0);
	}
	else Delete_Cookie('sclist', '/', '', 0);
	
	var cscvalue = Get_Cookie('sclist');
	var levaprice = 0;
	var europrice = 0;
	var artcount = 0;
	
	if(cscvalue != null && cscvalue != '')
	{
		var scarray = cscvalue.split('|');
		for(var i = 0; i < scarray.length; i++)
		{
			var cscarr = scarray[i].split(',');
			levaprice += parseInt(cscarr[2]) * parseInt(cscarr[1]);
			europrice += parseInt(cscarr[3]) * parseInt(cscarr[1]);
			artcount += parseInt(cscarr[1]);
		}
	}
	
	animate_div_id = 'pcontainer_' + pid;
	if(document.getElementById(animate_div_id))
	{
		document.getElementById(animate_div_id).style.position = 'absolute';
		document.getElementById(animate_div_id).style.zIndex = 1000;
		
		JQ('#' + animate_div_id)
			.animate( {
				left : "450px",
				top : "0px",
				opacity : 0
			}, 1300, "swing", function(){$(this).hide();} );
	}
	
	document.getElementById('sc_articles_count').innerHTML = artcount;
	document.getElementById('sc_price_count').innerHTML = levaprice;
	
	if(document.getElementById('empty_shopping_cart'))
	{
		if(artcount == 0)
		{
			document.getElementById('empty_shopping_cart').style.display = 'block';
			document.getElementById('order_button').style.display = 'none';
		}
		else if(artcount > 0)
		{
			document.getElementById('empty_shopping_cart').style.display = 'none';
			document.getElementById('order_button').style.display = 'block';
		}
	}
}

function checkCart(pid, pquantity, pprice_leva, pprice_euro, unique_article)
{
	var scvalue = Array();
	if(cscvalue = Get_Cookie('sclist'))
	{
		//check exists
		var scarray = cscvalue.split('|');
		for(var i = 0; i < scarray.length; i++)
		{
			var cscarr = scarray[i].split(',');
			if(pid != cscarr[0])
			{
				scvalue.push(cscarr[0] + ',' + cscarr[1] + ',' + cscarr[2] + ',' + cscarr[3] + ',' + cscarr[4]);
			}
		}
	}
	
	scvalue.push(pid + ',' + pquantity + ',' + pprice_leva + ',' + pprice_euro + ',' + unique_article);
	
	if(scvalue.length > 0)
	{
		scvalue = scvalue.join('|');
		Set_Cookie('sclist', scvalue, 365, '/', '', 0);
	}
	else Delete_Cookie('sclist', '/', '', 0);
	
	var cscvalue = Get_Cookie('sclist');
	var levaprice = 0;
	var europrice = 0;
	var artcount = 0;
	
	if(cscvalue != null && cscvalue != '')
	{
		var scarray = cscvalue.split('|');
		for(var i = 0; i < scarray.length; i++)
		{
			var cscarr = scarray[i].split(',');
			levaprice += parseInt(cscarr[2]) * parseInt(cscarr[1]);
			europrice += parseInt(cscarr[3]) * parseInt(cscarr[1]);
			artcount += parseInt(cscarr[1]);
		}
	}

	document.getElementById('sc_articles_count').innerHTML = artcount;
	document.getElementById('sc_price_count').innerHTML = levaprice;

	if(document.getElementById('empty_shopping_cart'))
	{
		if(artcount == 0)
		{
			document.getElementById('empty_shopping_cart').style.display = 'block';
			document.getElementById('order_button').style.display = 'none';
		}
		else if(artcount > 0)
		{
			document.getElementById('empty_shopping_cart').style.display = 'none';
			document.getElementById('order_button').style.display = 'block';
		}
	}
}

function setSCEmptyQuantity(what, pid, price_leva, price_euro, unique_article)
{
	var LANGU = '_bg';
	if(LANGU == '_bg') price = price_leva;
	else price = price_euro;
		
	if(trim(what.value) == '') what.value = 1;
	var nprice = '' + price * what.value + '';
	if(nprice.indexOf(".") == -1) nprice += '.00';
	var nparr = nprice.split('.');

	document.getElementById('p_price_' + pid).innerHTML = nparr[0] + '<sup>' + nparr[1] + '</sup> лв.';
	
	checkCart(pid, what.value, price_leva, price_euro, unique_article);
}

function setSCQuantity(what, pid, price_leva, price_euro, unique_article)
{
	var chk = /[^0-9]/g;
	if(what==null)
	{
		var what="1";
	}
	else
	{
		var what=what.value;
	}
	
	if ( chk.exec ( what ) )
	{
		showLightboxAlert('Позволени са само цифри.', 'Грешка', 'document.getElementById(\\\'' + what.id + '\\\').focus()');
		what.value = what.value.replace(chk, "");
		what.focus();
		return false;
	}
	else
	{
		var LANGU = '_bg';
		if(LANGU == '_bg') price = price_leva;
		else price = price_euro;

		var nprice = '' + price * what + '';
		if(nprice.indexOf(".") == -1) nprice += '.00';
		var nparr = nprice.split('.');
		document.getElementById('p_price_' + pid).innerHTML = nparr[0] + '<sup>' + nparr[1] + '</sup> лв.';
		checkCart(pid, what, price_leva, price_euro, unique_article);
	}
}

function removeElement(elid)
{
	if(document.getElementById(elid))
	{
		document.getElementById(elid).style.display = 'none';
		//document.body.removeChild(document.getElementById(elid));
	}
}

function changeHomeListing(tabid)
{
	document.getElementById('tab_' + tabid).className = 'tab_selected';
	document.getElementById('tab_' + (3 - tabid)).className = 'tab_default';
	
	document.getElementById('tab_' + tabid + '_content').style.display = '';
	document.getElementById('tab_' + (3 - tabid) + '_content').style.display = 'none';
	
	initCarousel('home_slider_' + tabid);
}

function setFontSize(what)
{
	var default_size = 14;
	var min_size = 9;
	var max_size = 18;
	
	var _fontElement = 'description_text';
	
	if(Get_Cookie('fontSize')) document.getElementById(_fontElement).style.fontSize = ExtractNumber(Get_Cookie('fontSize')) + 'px';
	
	if(what == 'default')
	{
		document.getElementById(_fontElement).style.fontSize = default_size + 'px';
	}
	else if(what == 'big')
	{
		if(parseInt(ExtractNumber(document.getElementById(_fontElement).style.fontSize) + 1) <= parseInt(max_size))
		{
			document.getElementById(_fontElement).style.fontSize = parseInt(ExtractNumber(document.getElementById(_fontElement).style.fontSize) + 1) + 'px';
		}
	}
	else if(what == 'small')
	{
		if(parseInt(ExtractNumber(document.getElementById(_fontElement).style.fontSize) - 1) >= parseInt(min_size))
		{
			document.getElementById(_fontElement).style.fontSize = parseInt(ExtractNumber(document.getElementById(_fontElement).style.fontSize) - 1) + 'px';
		}
	}
	else
	{
		if(Get_Cookie('fontSize')) document.getElementById(_fontElement).style.fontSize = ExtractNumber(Get_Cookie('fontSize')) + 'px';
		else document.getElementById(_fontElement).style.fontSize = default_size + 'px';
	}
	
	Set_Cookie( 'fontSize', ExtractNumber(document.getElementById(_fontElement).style.fontSize), 30, '/', '', 0 );
}

function checkNPOForm()
{
	var reg = document.npof;
	
	var LANGU = '_bg';
	if(LANGU == '_bg')
	{
		var fields_array = Array('name', 'family_name', 'city', 'email');
		var errors_array = Array('Моля, въведете Вашето име.', 'Моля, въведете Вашата фамилия.', 'Моля, въведете градът, в който желаете да получите пратката.'
		, 'Моля, въведете Вашият e-mail адрес.', 'Моля, въведете валиден e-mail адрес.');
	}
	else
	{
		var fields_array = Array('name', 'family_name', 'country', 'city', 'address', 'email');
		var errors_array = Array('Моля, въведете Вашето име.', 'Моля, въведете Вашата фамилия.', 'Моля, въведете държавата, в която желаете да получите пратката.'
		, 'Моля, въведете градът, в който желаете да получите пратката.', 'Моля, въведете адрес, на който желаете да получите пратката.', 'Моля, въведете Вашият e-mail адрес.', 'Моля, въведете валиден e-mail адрес.');
	}
	
	for(var i = 0; i < fields_array.length; i++)
	{
		eval("var currfield = reg." + fields_array[i] + ";");
		
		if(trim(currfield.value) == '')
		{
			currfield.focus();
			showLightboxAlert(errors_array[i], 'Грешка', 'document.npof.' + fields_array[i] + '.focus();');
			return false;
		}
		//email check
		else if(fields_array[i] == 'email')
		{
			if(!valid_email(trim(currfield.value)))
			{
				currfield.focus();
				showLightboxAlert(errors_array[(errors_array.length - 1)], 'Грешка', 'document.npof.' + fields_array[i] + '.focus();');
				return false;
			}
		}
	}
	
	reg.submit();
}

function checkLUOForm()
{
	var reg = document.ulof;
	
	if(reg.uladdress[1].checked)
	{
		if(trim(reg.address.value) == '')
		{
			reg.address.focus();
			showLightboxAlert('Моля, въведете адрес, на който желаете да получите пратката.', 'Грешка', 'document.ulof.address.focus();');
			return false;
		}
	}
	
	reg.submit();
}
