//Anton code
function CloseSearch() {
	var ajax_search = document.getElementById('ajax_search');
	if( ajax_search ) {
		ajax_search.innerHTML = '';
		ajax_search.style.display = 'none';
	};
}

function setPosition()
{
    var Y = getClientCenterY() - 98;
	if ( window.opera ) Y = ( Y * 0.6 ).toFixed(0);

	document.getElementById('add_to_cart').style.left = ( getClientCenterX() - 92 ) + 'px';
	document.getElementById('add_to_cart').style.top = Y + 'px';
	document.getElementById('add_to_cart').style.display = 'block';
	document.getElementById('add_to_cart').style.position = 'absolute';

	window.setTimeout(function() {closePosition()}, 2500);
}

function closePosition() {

	document.getElementById('add_to_cart').style.left = -1000 + 'px';
	document.getElementById('add_to_cart').style.top = -1000 + 'px';
	document.getElementById('add_to_cart').style.display = 'none';
}

function getBodyScrollTop()
{
    return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
    return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

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

function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getClientCenterX()
{
    return parseInt(getClientWidth()/2)+getBodyScrollLeft();
}

function getClientCenterY()
{
    return parseInt(getClientHeight()/2)+getBodyScrollTop();
}
//Anton code

		function open_printable_version(link) //opens new window
		{
			var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
			newWin = window.open(link,'printableWin',win);
			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;
		}
		
//Anton code
function upd_cart() {
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {
		
		if (req.readyState == 4) {
			
			if(req.responseText)alert(req.responseText);
			if(is_null(req.responseJS))return;
			
			if(req.responseJS._AJAXMESSAGE){
				
				var msgEntry = new Message();
				msgEntry.init(req.responseJS._AJAXMESSAGE);
				
				if(!msgEntry.isSuccess()){
				alert(msgEntry.getMessage());
					return;
				}
				
			};
			
			if(!req.responseJS.cart_info) return;
			var shopping_cart = getElementByClass('cpt_shopping_cart_info', parent.document, 'div');
			if(shopping_cart) shopping_cart.innerHTML = req.responseJS.cart_info;
			Behaviour.apply();
			
		}
	};
	
	try {
		req.open(null, "?ukey=cart&caller=1&initscript=ajaxservice", true);
		req.send( { 'action': 'upd_cart' } );
	} catch ( e ) {
		catchResult(e);
	} finally {	;}
	
	return false;
}
//Anton code

Behaviour.register({

	'input.input_message': function(e){
	
		e.onfocus = function(){

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

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

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

			var r_productParam = getElementsByClass('product_option', objForm);
			var query = '';
			if( r_productParam ) {
				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);
			}

			var req = new JsHttpRequest();
			req.onreadystatechange = function() {
				
				if (req.readyState == 4) {
					
					if(req.responseText)alert(req.responseText);
						if(is_null(req.responseJS))return;
					
					if(req.responseJS._AJAXMESSAGE){
						
						var msgEntry = new Message();
						msgEntry.init(req.responseJS._AJAXMESSAGE);
						
						if(!msgEntry.isSuccess()){
						alert(msgEntry.getMessage());
							return;
						}
						
					}
						
					if(!req.responseJS.cart_info) return;
					setPosition();
					var shopping_cart = getElementByClass('cpt_shopping_cart_info', document, 'div');
					if(shopping_cart) shopping_cart.innerHTML = req.responseJS.cart_info;
					
					Behaviour.apply();
					
				}
			};
			
			try {
				req.open(null, "?ukey=cart&caller=1&initscript=ajaxservice"+query, true);
				req.send( { 'action': 'add_product', 'productID': objForm.getAttribute('rel'), 'product_qty': r_productQty  } );
			} catch ( e ) {
				catchResult(e);
			} finally {	;}
			
			return false;
		},
		element.onmouseover = function(){ 
			if( this.src.search('add2cart_small_ru') !== -1 )
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/act_add2cart_small_ru.gif';
			else
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/act_add2cart_ru.gif';
		},
		element.onmouseout = function(){ 
			if( this.src.search('add2cart_small_ru') !== -1 )
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/add2cart_small_ru.gif';
			else
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/add2cart_ru.gif';
		}
	},
	'.hndl_reserve' : function(e) {
		element.onclick = function() {
			var objForm = getFormByElem(this);
			if(!objForm) return true;
			openFadeIFrame(ORIG_LANG_URL + set_query('?ukey=reserve&view=noframe&productID='+objForm.getAttribute('rel') ));
			return false;
		},
		element.onmouseover = function(){ 
			if( this.src.search('reserve_small_ru') !== -1 )
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/act_reserve_small_ru.gif';
			else
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/act_reserve_ru.gif';
		},
		element.onmouseout = function(){ 
			if( this.src.search('reserve_small_ru') !== -1 )
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/reserve_small_ru.gif';
			else
				this.src = ORIG_URL+'/published/publicdata/U2578/attachments/SC/images/reserve_ru.gif';
		}
	},
	//Anton code
	'.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;
		}
	},
	//Anton code
	'.hndl_phonecall' : function(e) {
		e.onclick = function() {
			openFadeIFrame(ORIG_LANG_URL + set_query('?ukey=phonecall&view=noframe'));
			return false;
		}
	},
	'input#searchstring' : function(e) {
		
		e.onkeyup = function() {
			
			var searchstring = document.getElementById('searchstring');
			
			var req = new JsHttpRequest();
			req.onreadystatechange = function() {
				
				if (req.readyState == 4) {
					
					if(req.responseText)alert(req.responseText);
						if(is_null(req.responseJS))return;
					
					if(req.responseJS._AJAXMESSAGE){
						
						var msgEntry = new Message();
						msgEntry.init(req.responseJS._AJAXMESSAGE);
						
						if(!msgEntry.isSuccess()){
						alert(msgEntry.getMessage());
							return;
						}
						
					}
					
					if(!req.responseJS.search_list) return;
					var ajax_search = document.getElementById('ajax_search');
					if( ajax_search ) {
						ajax_search.innerHTML = req.responseJS.search_list;
						ajax_search.style.display = 'block';
					};
					
					Behaviour.apply();
					
				}
			};
			
			try {
				req.open(null, "?ukey=cart&caller=1&initscript=ajaxservice", true);
				req.send( { 'action': 'ajax_search', 'searchstring': searchstring.value  } );
			} catch ( e ) {
				catchResult(e);
			} finally {	;}
			
			return false;
		},
		
		e.onblur = function() {
			window.setTimeout( function() {CloseSearch()}, 3000 );
			Behaviour.apply();
			return false;
		}
		
	},
	//Anton code
	'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;
		}
	}
});

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)+'%)';
	}
});
