<!--
// splash ->
function subscribe(){
	if(checkMail(document.frmSubscription.mail.value)){
		getContent('subscription.php','mail=' + document.frmSubscription.mail.value, fill_subscribe);
	}else{
		document.frmSubscription.mail.focus();
		alert('Моля въведете валиден e-mail адрес!');
	}
}

function fill_subscribe(){
	if (req.readyState == 4){
		if (req.status == 200) {
			alert(req.responseText);
		}else {
			alert('Възникна грешка. Моля опитайте отново!');
		}
	}
}
// splash

function setCookie(c_name,value,expiredays){
	var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
	document.cookie= c_name+ '=' +escape(value)+ ((expiredays==null) ? '' : ';expires='+exdate.toGMTString());
}

function getCookie(c_name){
if (document.cookie.length>0){
	c_start=document.cookie.indexOf(c_name + '=');
	if (c_start!=-1){
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(';',c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return '';
}

function show_category(id){
	var value = getCookie('div_' + id);
	if ((value == '')||(value == 'none')){
		document.getElementById('div_' + id).style.display = "block";
		setCookie('div_' + id, 'block', 1000);
	}else if(value == 'block'){
		document.getElementById('div_' + id).style.display = 'none';
		setCookie('div_' + id, 'none', 1000);
	}
}

function load_category(id){
	value = getCookie('div_' + id);
	if (value == ''){
		value = 'none';
	}
	document.getElementById('div_' + id).style.display = value;
}

function checkMail(str){
	var filter=/^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(str);
}

function checkDigit(str){
	var filter=/^[0-9]{2,3}$/;
	return (filter.test(str));
}

function checkUser(str){
	var filter=/^([a-zA-Z0-9_\.\-])+$/;
	return filter.test(str);
}

function can_subscribe(){
	if(checkMail(document.frmSubscription.mail.value)){
		return true;
	}else{
		alert('Моля въведете валиден e-mail адрес!');
		return false;
	}
}

var req=false;
function getContent(url,paramstr,fhandle){
	try{
		req = new XMLHttpRequest(); // Opera 8.0+, Firefox, Safari
	}catch(e){
		try{ // IЕ
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				req= new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){ // Друга грешка
				alert("Моля използвайте някои от следните браузъри: Konqueror, IExplorer 5.0+, Mozilla 1.0+, Netscape 7.1+, Safari 1.2+, Opera 7.6+");
				return false;
			}
		}
	}
	rand = Math.floor(Math.random()*100000);
	req.onreadystatechange=fhandle;
	req.open('GET', url+'?rand=' + rand + '&' + encodeURI(paramstr), true);
	req.send(null);
}

function find_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 find_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 index = 0;
var max_distance = 0;
var step = 15;
var min_img_width = 16;
var min_img_height = 16;
var float_div = null;
var fload_img = null;
var cart = null;
var img_top;
var img_left;
var cart_top;
var cart_left;
var img_width;
var img_height;
var ajax_loaded = false;
var animation_loaded = false;
var right_cart_html = '';
var width_step;
var height_step;
var left_step;
var top_step;
function moveImage(){
	if (index < max_distance ){
		if (img_top > cart_top){
			img_top = img_top - top_step;
			float_div.style.top =  Math.ceil(img_top) + 'px';
		}
		if(img_left < cart_left){
			img_left = img_left + left_step;
			float_div.style.left = Math.ceil(img_left) + 'px';
		}
		if(img_width > min_img_width){
			img_width = img_width - width_step;
			float_img.width = Math.ceil(img_width);
		}
		if(img_height > min_img_height){
			img_height = img_height - height_step;
			float_img.height = Math.ceil(img_height);
		}
		index = index + step;
		setTimeout("moveImage()", 1);
	}else{
		if(ajax_loaded){cart.innerHTML = right_cart_html;}
		animation_loaded = true;
		float_img.style.display = 'none';
	}
}

function add_to_cart(id, quantity){
	ajax_loaded = false;
	animation_loaded = false;
	right_cart_html = '';
	getContent('/get/right_cart_add.php','id=' + id + '&quantity=' + quantity, fill_right_cart);

	float_div = document.getElementById('div_img_move');
	art_img = document.getElementById('article_img_' + id);
	float_img = document.getElementById('img_move');
	cart = document.getElementById('right_cart');
	img_top = find_top(art_img);
	img_left = find_left(art_img);
	img_width = art_img.width;
	img_height = art_img.height;
	cart_top = find_top(cart) + 10;
	cart_left = find_left(cart) + 30;
	float_img.src = art_img.src;
	float_img.style.top = art_img.style.top;
	float_img.style.left = art_img.style.left;
	float_img.width = img_width;
	float_img.height = img_height;
	float_img.style.display = 'block';
	index = 0;
	max_distance = Math.max(img_top - cart_top , cart_left - img_left);
	width_step = ((img_width - min_img_width)/max_distance )*step;
	height_step = ((img_height - min_img_height)/max_distance )*step;
	left_step = ((cart_left - img_left)/max_distance )*step;
	top_step =((img_top - cart_top )/max_distance )*step;
	moveImage();
}

function del_article(id){
	if(confirm('Сигурни ли сте, че желаете да извадите този продукт от кошницата?')){
		animation_loaded = true;
		getContent('/get/right_cart_del.php','id=' + id, fill_right_cart);
	}
}

function fill_right_cart(){
	if (req.readyState == 4){
		if (req.status == 200) {
			ajax_loaded = true;
			if(animation_loaded){
				document.getElementById('right_cart').innerHTML = req.responseText;
			}else{
				right_cart_html = req.responseText;
			}
		}else {
			alert('Възникна грешка. Моля опитайте отново!');
		}
	}
}

function fill_last_items(){
	sns_ids = getCookie('sns_ids');
	if(sns_ids != ''){
		sns_ids_c = getCookie('sns_ids_c');
		li = document.getElementById('last_items');
		li.options.length = 1;
		var sns_array = sns_ids.split(",");
		var sns_array_c = sns_ids_c.split("|");
		for(var i=0; i < sns_array.length; i++){
			var item = document.createElement("OPTION");
			item.innerHTML = sns_array_c[i]; 
			item.value = sns_array[i];
			li.appendChild(item);
		}
	}
}
function load_article_item(){
	document.location.href = 'article-' + document.getElementById('last_items').value + '.html';
}

function sns_soon(s){
	alert('Очаквайте скоро в магазините на SNS оригиналните висококачествени хранителни добавки на марката ' + s + '!');
}
//-->