function open_printable_version(link) // opens new window
{
	var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link, 'printableWin', win);
	if(newWin){newWin.focus();}
}

function confirmUnsubscribe() // unsubscription confirmation
{

	temp = window.confirm(translate.cnfrm_unsubscribe);
	if (temp) // delete
	{
		window.location = "index.php?killuser=yes";
	}

}

function validate() // newsletter subscription form validation
{
	if (document.subscription_form.email.value.length < 1) {
		alert(translate.err_input_email);
		return false;
	}
	if (document.subscription_form.email.value == 'Email') {
		alert(translate.err_input_email);
		return false;
	}
	return true;
}
function validate_disc() // review form verification
{
	if (document.formD.nick.value.length < 1) {
		alert(translate.err_input_nickname);
		return false;
	}

	if (document.formD.topic.value.length < 1) {
		alert(translate.err_input_message_subject);
		return false;
	}

	return true;
}
function validate_search() {

	if (document.Sform.price1.value != ""
			&& ((document.Sform.price1.value < 0) || isNaN(document.Sform.price1.value))) {
		alert(translate.err_input_price);
		return false;
	}
	if (document.Sform.price2.value != ""
			&& ((document.Sform.price2.value < 0) || isNaN(document.Sform.price2.value))) {
		alert(translate.err_input_price);
		return false;
	}

	return true;
}

function validate_input_digit(e) {
	var keynum;
	var keychar;
	var numcheck;
	try {
		if (window.event) // IE
		{
			keynum = window.event.keyCode;
		} else if (e.which) // Netscape/Firefox/Opera
		{
			keynum = e.which;
		}
	} catch (exception) {
		alert(exception.message);
	}

	keynum = parseInt(keynum);
	if (keynum == 13)
		return false;// ignore Enter
	if (keynum >= 33 && keynum <= 40)
		return true;// skip navigation buttons
	if (keynum == 8)
		return true;// skip backspace
	if (keynum == 17)
		return true;// skip ctrl
	if (keynum == 45)
		return true;// skip insert
	if (keynum == 46)
		return true;// skip delete
	if (keynum >= 96 && keynum <= 105) {
		keynum -= 48;
	}
	keychar = String.fromCharCode(keynum);
	// alert('char '+keychar+'\n code '+keynum);
	numcheck = /\d/;
	return numcheck.test(keychar);
	var res = numcheck.test(keychar);
	// if(!res)alert('char '+keychar+'\n code '+keynum);
	return res;
}

//Anton code
function isParent(child, parent) {
    if (!child || !parent) {
        return false;
    }
    while (true) {
        if (child == parent) {
            return true;
        }
        if (child.parentElement) {
            child = child.parentElement;
        } else if (child.parentNode) {
            child = child.parentNode;
        } else {
            return false;
        }
    }
}

function getOffsetRect(elem) {
    var box = elem.getBoundingClientRect()
     
    var body = document.body
    var docElem = document.documentElement
     
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
     
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0
     
    var top  = box.top +  scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft
     
    return { top: Math.round(top), left: Math.round(left) }
}

function getOffsetSum(elem) {
    var top=0, left=0
    while(elem) {
        top = top + parseFloat(elem.offsetTop)
        left = left + parseFloat(elem.offsetLeft)
        elem = elem.offsetParent       
    }
     
    return {top: Math.round(top), left: Math.round(left)}
}

function getOffset(elem) {
	if (elem.getBoundingClientRect) {
		return getOffsetRect(elem)
	} else {
	return getOffsetSum(elem)
	}
}

function getClientWidth() {
	return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
//Anton code

Behaviour.register({

	'input.input_message' : function(e) {

		e.onfocus = function() {

			this.className = this.className.replace(/input_message/, '')
					+ ' input_message_focus';

			var value = this.getAttribute('rel');
			if(!value){
				value = this.getAttribute('title');
			}
			if (this.value != value)
				return;
			this.value = '';
		}

		e.onblur = function() {
			if (this.value != '')
				return;
			this.className = this.className.replace(/input_message_focus/, '')
					+ ' input_message';
			var value = this.getAttribute('rel');
			if(!value){
				value = this.getAttribute('title');
			}
			this.value = value;
		}
	},
	'.add2cart_handler' : function(element) {

		element.onclick = function() {

			var objForm = getFormByElem(this);
			if (!objForm)
				return true;

			var r_productParam = getElementsByClass('product_option', objForm);

			var query = '';
			for (var i = r_productParam.length - 1; i >= 0; i--) {

				if (!parseInt(r_productParam[i].value))
					continue;

				if (r_productParam[i].name)
					query += '&' + r_productParam[i].name + '='
							+ parseInt(r_productParam[i].value);
			}

			var r_productQty = getElementByClass('product_qty', objForm);
			if (r_productQty) {
				r_productQty = parseInt(r_productQty.value);
				if (r_productQty > 1) {
					query += '&product_qty=' + r_productQty;
				}
			}

			var url = ORIG_LANG_URL
					+ set_query('?ukey=cart&view=noframe&action=add_product&'
									+ query + '&productID='
									+ objForm.getAttribute('rel'), '');
			openFadeIFrame(url);
			return false;
		}
	},
	'.product_option' : function(e) {
		e.onchange = function() {
			var objForm = getFormByElem(this);
			if (!objForm)
				return true;

			var r_productParam = getElementsByClass('product_option', objForm);
			var price = parseFloat(getElementByClass('product_price', objForm).value);
			var list_price = 0;
			var obj = getElementByClass('product_list_price', objForm);
			if (obj)
				list_price = parseFloat(obj.value);

			for (var i = r_productParam.length - 1; i >= 0; i--) {

				var option = select_getCurrOption(r_productParam[i]);
				if (!option)
					continue;

				price += parseFloat(option.getAttribute('rel'));
				list_price += parseFloat(option.getAttribute('rel'));
			}

			getElementByClass('totalPrice', objForm).innerHTML = formatPrice(price);
			var obj = getElementByClass('regularPrice', objForm);
			if (obj)
				obj.innerHTML = formatPrice(list_price);
			var obj = getElementByClass('youSavePrice', objForm);
			if (obj)
				obj.innerHTML = formatPrice(list_price - price)
						+ ' ('
						+ Math
								.round((list_price - price) / list_price * 100,
										2) + '%)';
		}
	},
	'.hndl_proceed_checkout' : function(e) {
		e.onclick = function() {
			openFadeIFrame(ORIG_LANG_URL + set_query('?ukey=cart&view=noframe'));
			return false;
		}
	},
	'input.goto' : function(e) {
		e.onclick = function() {
			if (this.className.search(/confirm/) !== -1
					&& !window.confirm(this.getAttribute('title')))
				return
			document.location.href = this.getAttribute('rel');
		}
	},
	'.gofromfade' : function(e) {
		e.onclick = function() {

			parent.document.location.href = this.href;
			parent.closeFadeIFrame();
			return false;
		}
	},
	//Anton code
	'.brand_tree_handler' : function(e) {
		e.onmouseover = function() {
			var brand_tree = document.getElementById("brand_tree_"+this.getAttribute('rel'));
			if( brand_tree ) {
				pos = getOffset(e);
				brand_tree.style.top = pos.top+'px';
				brand_tree.style.left =  pos.left+'px';
				brand_tree.style.display = 'block';
			};
			return false;
		}
	},
	'.cpt_brand_list li' : function(e) {
		e.onmouseover = function() {
			
			var uls = e.parentNode.getElementsByTagName("ul");
			for (var i=0; i < uls.length; i++) uls[i].style.display = 'none';
			
			if( e.getElementsByTagName("ul")[0] ) {
				var ul = e.getElementsByTagName("ul")[0];
				if(ul.getElementsByTagName("ul")[0] ) {
					var ul2 = ul.getElementsByTagName("ul")[0];
					ul2.style.display = 'block';
				};
				
				pos = getOffset(this);
				if( (pos.left + (this.offsetWidth + 10)*2) > getClientWidth() )
					ul.style.left = ( this.offsetLeft - this.offsetWidth - 30 ) + 'px';
				else
					ul.style.left = ( this.offsetLeft + this.offsetWidth + 10 ) + 'px';
					
				ul.style.top = ( this.offsetTop - (this.offsetHeight/2) ) +'px';
				ul.style.display = 'block';
			};
			return false;
		}
	},
	'.cpt_brand_list ul' : function(e) {
		e.onmouseout = function(event) {
			var event = event || window.event;
			var target = event.relatedTarget || event.toElement;
			if(!isParent(target, e)) e.style.display = 'none';
			return false;
		}
	},
	//Anton code
	'input.digit' : function(e) {
		e.onkeydown = function(event) {
			return validate_input_digit(event);
		}
	}
});

Behaviour.addLoadEvent(function() {

	var totalPrices = getElementsByClass('totalPrice');
	for (var k = totalPrices.length - 1; k >= 0; k--) {

		var objForm = getFormByElem(totalPrices[k]);
		if (!objForm)
			continue;;

		var r_productParam = getElementsByClass('product_option', objForm);
		var price = parseFloat(getElementByClass('product_price', objForm).value);
		var list_price = 0;
		var obj = getElementByClass('product_list_price', objForm);
		if (obj)
			list_price = parseFloat(obj.value);

		for (var i = r_productParam.length - 1; i >= 0; i--) {

			var option = select_getCurrOption(r_productParam[i]);
			if (!option)
				continue;

			price += parseFloat(option.getAttribute('rel'));
			list_price += parseFloat(option.getAttribute('rel'));
		}
		totalPrices[k].innerHTML = formatPrice(price);
		var obj = getElementByClass('regularPrice', objForm);
		if (obj)
			obj.innerHTML = formatPrice(list_price);
		var obj = getElementByClass('youSavePrice', objForm);
		if (obj)
			obj.innerHTML = formatPrice(list_price - price) + ' ('
					+ Math.round((list_price - price) / list_price * 100, 2)
					+ '%)';
	}
});
