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;
}

$(document).ready(function () {
    var screenHeight = screen.height;
    if (screenHeight) {
    }
    var windowHeight = $(window).height();
    var footer = $("#footer").offset();
    if(windowHeight>(footer.top+91)) {
    	$("#subfooter").css("height", (windowHeight-footer.top)+'px')
    }
    //alert(windowHeight);
    //alert(footer.top);
    //alert($(window).height());
});

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;
}
function save_metod_comment(elem) {
			var paramObj = new Object();
			paramObj['action'] = 'save_metod_comment';
			paramObj['type'] = 'order_comment';
			paramObj['order_comment'] = elem.value;

    		var req = new JsHttpRequest();
    		req.onreadystatechange = function() {

        		if (req.readyState != 4) {
        			return false;
                }
                if(req.responseText) {
                	alert(req.responseText);
                	return false;
                }
    		};

    		try {
        		req.open('POST', set_query('&ukey=cart&caller=1&initscript=ajaxservice'), true);
        		req.send(paramObj);
    		}
    		catch ( e ){catchResult(e);}
    		finally { ;}
}
function setInputValuedefault() {	$("textarea.textarea_message").focus(function () {
			$(this).removeClass('textarea_message').addClass('input_message_focus');
			var value = $(this).attr('rel');
			alert(value);
			/*
			var value = this.getAttribute('rel');
			if(!value){
				value = this.getAttribute('title');
			}
			if (this.value != value)
				return;
			this.value = '';
			*/
    });
}
var myflag = false;
Behaviour.register({
	/*'form.foundedproduct' :  function(e) {
		e.onsubmit = function() {			if (this.readysubmit.value == 0) {				alert(msg_service_list_empty);
				return false;
			}
			return true;
		}
	},*/
	'input.save_and_print' : function (e) {		e.onclick = function() {			return (this.form.readysubmit.value == 1) ? true:false;
		}
	},
	'form.foundproduct' : function(e) {
		e.onsubmit = function() {
         	document.getElementById('finded_products').innerHTML = '';
         	var search_string = this.searchstring.value.replace(/^\s+/, '');
         	var search_string = search_string.replace(/\s+$/, '');
         	if (search_string == '') {
         		//alert("ENTER STRING");
         		return false;         	}
			var paramObj = new Object();
			paramObj['action'] = 'ajax_search_products';
			paramObj['search_string'] = search_string;

    		var req = new JsHttpRequest();
    		req.onreadystatechange = function() {

        		if (req.readyState != 4) {
        			return false;
                }
                if(req.responseText) {
                	alert(req.responseText);
                	return false;
                }
        		var products = req.responseJS.products;
        		if (products.length == 0) {
            		document.getElementById('finded_products').innerHTML = msg_no_products_found;
            		return;
        		};

                var _html = '';
                for(i=0; i<products.length; i++) {               		_html += '<a href="" onClick="return addRowInTable(' + products[i].productID + ', \'' + products[i].name + '\');">' + products[i].name + '</a><br />';
                }
                document.getElementById('finded_products').innerHTML = _html;


    		};

    		try {
        		req.open('POST', set_query('&ukey=services_send&caller=1&initscript=ajaxservice'), true);
        		req.send(paramObj);
    		}
    		catch ( e ){catchResult(e);}
    		finally { ;}

         	return false;
		}

	},
	'input.product_qty' : function(e) {
        /*
		e.onfocus =
		e.onblur =
		e.onclick =
		e.ondblclick =
		e.onkeypress = */
		/*e.onchange =
		e.onkeydown =*/
		e.onkeyup =
		function() {
         	var value = +this.value;
         	var re = /^\d+$/g;
			res = re.exec(value);
			if (isNaN(value)) {
         		this.value = 1;
			}
		}

	},
	'textarea.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;
		}
	},

	'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;
		}
	},
	'select.shippingMethodID' : function(element) {
		element.onchange = function() {
			var paramObj = new Object();
			paramObj['action'] = 'save_metod_comment';
			paramObj['type'] = 'shipping_metod';
			paramObj['shippingMethodID'] = select_getCurrValue(this);

    		var req = new JsHttpRequest();
    		req.onreadystatechange = function() {

        		if (req.readyState != 4) {
        			return false;
                }
                if(req.responseText) {
                	alert(req.responseText);
                	return false;
                }
    		};

    		try {
        		req.open('POST', set_query('&ukey=cart&caller=1&initscript=ajaxservice'), true);
        		req.send(paramObj);
    		}
    		catch ( e ){catchResult(e);}
    		finally { ;}

			return false;
		}
	},
	'select.paymentMethodID' : function(element) {
		element.onchange = function() {
			var paramObj = new Object();
			paramObj['action'] = 'save_metod_comment';
			paramObj['type'] = 'payment_metod';
			paramObj['paymentMethodID'] = select_getCurrValue(this);

    		var req = new JsHttpRequest();
    		req.onreadystatechange = function() {

        		if (req.readyState != 4) {
        			return false;
                }
                if(req.responseText) {
                	alert(req.responseText);
                	return false;
                }
    		};

    		try {
        		req.open('POST', set_query('&ukey=cart&caller=1&initscript=ajaxservice'), true);
        		req.send(paramObj);
    		}
    		catch ( e ){catchResult(e);}
    		finally { ;}

			return false;
		}
	},
	'textarea.order_comment' : function(element) {
		element.onblur = function() {
				save_metod_comment(this);
		}
	},
	'.prdnotification' : function(element) {
		element.onclick = function() {
			var paramObj = new Object();
			paramObj['action'] = 'save_prdnotification';
			paramObj['productID'] = this.getAttribute('rel');

    		var req = new JsHttpRequest();
    		req.onreadystatechange = function() {

        		if (req.readyState != 4) {
        			return false;
                }
                if(req.responseText) {
                	alert(req.responseText);
                	return false;
                }

				alert(req.responseJS.notificationText);
    		};

    		try {
        		req.open('POST', set_query('&ukey=cart&caller=1&initscript=ajaxservice'), true);
        		req.send(paramObj);
    		}
    		catch ( e ){catchResult(e);}
    		finally { ;}
			return false;
		}
	},
	'.add2cart_handler' : function(element) {

		element.onclick = function() {
            myflag = true;
            var row = $(this).parents("td").parents("tr");
   			$("td", row).css("background-color", "#D1F4DE");

			sswgt_CartManager.showwait();

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

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

			var paramObj = new Object();
			paramObj['action'] = 'ajaxs_add_product';
			for (var i = r_productParam.length - 1; i >= 0; i--) {

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

				if (r_productParam[i].name)
					paramObj[r_productParam[i].name] = parseInt(r_productParam[i].value);
					//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) {
					paramObj['product_qty'] = r_productQty;
					//query += '&product_qty=' + r_productQty;
				}
			}
			paramObj['productID'] = objForm.getAttribute('rel');;

    		var req = new JsHttpRequest();
    		req.onreadystatechange = function() {

        		if (req.readyState != 4) {
        			return;
                }
                if(req.responseText) {
                	alert(req.responseText);
                	return;
                }

                if(req.responseJS.added == -1) {
                	$("#showwait").html(translate.please_login);
                	setTimeout("sswgt_CartManager.hidewait()", 2000);
                	return;
                }
                if(req.responseJS.added == 0) {
                	$("#showwait").html(translate.not_instock);
                	setTimeout("sswgt_CartManager.hidewait()", 2000);
                	return;
                }

                if (req.responseJS.added == 1) {
                	sswgt_CartManager.hidewait();
	                if (req.responseJS.shopping_cart_items) {
	                	document.getElementById('shpcrtgc').innerHTML = req.responseJS.shopping_cart_items + ' ' +cart_content_not_empty;
	                }
	                if (req.responseJS.shopping_cart_value_shown) {
	                	document.getElementById('shpcrtca').innerHTML = req.responseJS.shopping_cart_value_shown;
	                  	//document.getElementById('checkoutLink').style.display = '';
	                }
	                if (req.responseJS.shopping_cart_volume) {
	                	document.getElementById('shpcrtva').innerHTML = req.responseJS.shopping_cart_volume;
	                }
	                if (req.responseJS.shopping_cart_weight) {
	                	document.getElementById('shpcrtwa').innerHTML = req.responseJS.shopping_cart_weight;
	                }
	                //var imageID =  $('.prdImage'+paramObj['productID']) ? '#prdImage'+paramObj['productID']:'#img-current_picture';
	                $("span.priceSpan").css('color','black');
	                req.responseJS.priceID = (req.responseJS.priceID == 1) ? 1:req.responseJS.priceID;
	                $("span.totalPrice"+req.responseJS.priceID).css('color','red');

	                var img = $('.prdImage'+paramObj['productID']);
	                if (img) {
                    	var hide = false;
	                	//alert(img.css('width'));
	                	if (img.css('display') == 'none') {
	                		img.css('display', 'inline');
	                		hide = true;
	                	}
	                	var image = $('.prdImage'+paramObj['productID']).offset();

	                	if (hide) {
	                		img.css('display', 'none');
						}
						if (image) {

							var cart  = $('.cpt_shopping_cart_info').offset();
							var bodyWidth  = ($('body').width()-90)/2;

                            if (document.getElementById('prdImageTemp'+paramObj['productID'])) {
								$('.prdImageTemp'+paramObj['productID']).before('<img src="' + $('.prdImage'+paramObj['productID']).attr('src') + '" id="temp" style="width:'+img.css('width')+';z-index:2000;position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
                            } else {
								$('.prdImage'+paramObj['productID']).before('<img src="' + $('.prdImage'+paramObj['productID']).attr('src') + '" width="'+img.css('width')+'" height="'+img.css('height')+'" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
							}
							params = {
								top : (cart.top-160) + 'px',
								//left : (cart.left/2) + 'px',
								left : bodyWidth + 'px',
								opacity : 0.0,
								width : 80,
								heigth : 150
							};

							$('#temp').animate(params, 'slow', false, function () {//alert($('#temp'));
								$('#temp').remove();
							});
						}
						//var row = $(this).parents("td").parents("tr");
   						$("td", row).css("background-color", "#ffffff");
   						myflag = false;
					}
	           	}
    		};

    		try {
        		req.open('POST', set_query('&ukey=cart&caller=1&initscript=ajaxservice'), true);
        		req.send(paramObj);
    		}
    		catch ( e ){catchResult(e);}
    		finally { ;}
			return false;
		}

		element.onmouseover = function() {
				var row = $(this).parents("td").parents("tr");
                $("td", row).css("background-color", "#D1F4DE");
		}

		element.onmouseout = function() {
				if (!myflag) {
				var row = $(this).parents("td").parents("tr");
                $("td", row).css("background-color", "#ffffff");
                }
		}
		/**/
	},
	'a.deleteItem'	: function(e) {
		element.onmouseover = function() {
				var row = $(this).parents("td").parents("tr");
                $("td", row).css("background-color", "#D1F4DE");
				//alert();
		}
		element.onmouseout = function() {
				var row = $(this).parents("td").parents("tr");
                $("td", row).css("background-color", "#ffffff");
				//alert();
		}
	},
	'.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;
		}
	},
	'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)
					+ '%)';
	}
});
