/**
 *
 */
var bCurrent = null;

function showBMmenu(menu, sender){
	sender.blur();
	if(bCurrent==menu){
		$('bmenu_'+bCurrent).style.display = "none";
		$('lnk_'+bCurrent).className = $('lnk_'+bCurrent).className.split('-')[0];
		bCurrent = null;
		return false;
	} else if(bCurrent) {
		$('bmenu_'+bCurrent).style.display = "none";
		$('lnk_'+bCurrent).className = $('lnk_'+bCurrent).className.split('-')[0];
	}
	$('bmenu_'+menu).style.display = "block";
	sender.className = sender.className + '-active';
	bCurrent = menu;
	return false;
};

/**
 *
 */
function hideDiv(divId){
	$(divId).style.display = "none";
};

/**
 *
 */
function activeForm(form){
	elm = form.getElementsByTagName('INPUT');
	for(i=0; i<elm.length; i++){
		if( (elm[i].type == "text") || (elm[i].type == "password") ) {
			elm[i].onfocus=function(){ this.style.borderColor = '#AD6E4D'; };
			elm[i].onblur=function(){ this.style.borderColor = '#1F0C06'; };
		}
	};
	elm = form.getElementsByTagName('SELECT');
	for(i=0; i<elm.length; i++){
		elm[i].onfocus=function(){ this.style.borderColor = '#AD6E4D'; };
		elm[i].onblur=function(){ this.style.borderColor = '#1F0C06'; };
	};
	elm = form.getElementsByTagName('TEXTAREA');
	for(i=0; i<elm.length; i++){
		elm[i].onfocus=function(){ this.style.borderColor = '#AD6E4D'; };
		elm[i].onblur=function(){ this.style.borderColor = '#1F0C06'; };
	};
};

/**
 *
 */
function selectCountry(sender, codeiso) {
	$('codeiso').value = codeiso;
	$('countryFrm').action = sender.href;
	$('countryFrm').submit();
	return false;
};

/**
 *
 */
function findPosX(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 findPosY(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;
};

/**
 *
 */
function Gallery(imgCount) {
	this.imgCount = imgCount;
	this.obj = 'product_image_';
	this.im = 1;
};
//
Gallery.prototype.switchImage = function (direction) {
	$(this.obj+this.im).style.display = 'none';
	if(direction == 'n') {
		if(this.im < this.imgCount) {
			this.im++;
		}	else {
			this.im = 1;
		}
	} else {
		if(this.im > 1) {
			this.im--;
		}	else {
			this.im = this.imgCount;
		}
	}
	$(this.obj+this.im).style.display = 'block';
	return true;
};

/**
 *
 */
var zoomCollection = function(){

	this.current = null,

	this.open = function(divId,parId){
		this.close();
		this.current = divId;
		prv = $(divId);
		x = findPosX($(parId));
		y = findPosY($(parId));
		prv.style.left = 10 + x+'px';
		prv.style.top = 8 + y+'px';
		prv.style.display = "block";
	},

	this.close = function(divId,parId){
		this.current ? $(this.current).style.display = "none" : "";
		this.current = null;
	}

};

//
var zoomAssortiment = function(){};
zoomAssortiment.prototype.findPos = function() {
	
	var curleft = curtop = 0;
	if (this.obj.offsetParent) {
		do {
			curleft += this.obj.offsetLeft;
			curtop += this.obj.offsetTop;
			if(this.offset && (this.obj.offsetParent.id == this.offset)) {
				return [curleft,curtop];
			}
		} while (this.obj = this.obj.offsetParent);
	}
	return [curleft,curtop];
};
//
zoomAssortiment.prototype.mouseX = function(evt) {
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else return null;
}
//
zoomAssortiment.prototype.mouseY = function(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else return null;
};
//
zoomAssortiment.prototype.getZoom = function(key,obj,offset) {
	this.obj = obj;
	this.offset = offset;
	objPos = this.findPos();
	/*alert($('zoom_container_'+key));*/
	$('ajaxProductZoom').innerHTML = $('zoom_container_'+key).innerHTML;
	$('product_zoom_'+key).style.display = 'block';
	$('product_zoom_'+key).style.left = objPos[0]-50+'px';
	$('product_zoom_'+key).style.top = objPos[1]-150+'px';
	new Effect.Appear('product_zoom_'+key, {duration:0.9, from:5.0, to:20.0});
};

/**
 *
 */
function setBasket() {
	inp = document.getElementsByTagName('input');
	param = new Array();
	param['max'] = new Array();
	param['current'] = new Array();
	cnt=0;
	for(i=0; i<inp.length;i++) {
		current = inp[i].id;
		if(current && current.substring(0,15) == 'productQuantity' && inp[i].value) {
			param['current'][inp[i].name] = inp[i].value;
			param['max'][inp[i].name] = $('pvzAvailability_' + cnt).value;
			inp[i].value = 0;
			cnt++;
		}
	}
	return param;
};

/**
 *
 */
/*function changeProductQuantity(id,flag) {

	myInput = $(id);
	myVal = myInput.value;
	if(!hasForbiddenChar(myVal)) {
		if(flag < 0 && myInput.value == 0) {
			return null;
		}
		myInput.value = (myInput.value*1) + flag;
	}
	return null;
};*/

/**
 *
 */
var reAmount = /^\d+$/;
function setSelection(frm, sender, id){
	if( sender.value.match(reAmount) && (sender.value>0) ) {
		frm.elements[id].checked = true;
	} else {
		frm.elements[id].checked = false;
	}
};

/**
 *
 */
function validateFrm(frm,msg){
	for(i=0; i<frm.elements.length; ++i){
		if( frm.elements[i].type=='checkbox' && frm.elements[i+1].type=='text' ){
			if( frm.elements[i].checked && frm.elements[i+1].value.match(reAmount) && (frm.elements[i+1].value>0) ){
				return true;
			}
		}
	}
	alert(msg);
	return false;
};

/**
 *
 */
function showObjects(){
	objects = getObjectsToHide();
	objects_length = objects.length;
	for (i = 0; i != objects_length; i++) {
		objects[i].style.visibility = "visible";
	}
	if (typeof(hideOverlayObjects) != 'undefined') {
		for (i = 0; i < hideOverlayObjects.length; i++) {
			document.getElementById(hideOverlayObjects[i]).style.visibility = "visible";
		}
	}
};

/**
 *
 */
function hideObjects(){
	objects = getObjectsToHide();
	objects_length = objects.length;
	for (i = 0; i != objects_length; i++) {
		try{
			if(!inArray(skipOverlayObjects, objects[i].id)){
				objects[i].style.visibility = "hidden";
			}
		} catch(e) {
			objects[i].style.visibility = "hidden";
		}
	}
	if (typeof(hideOverlayObjects) != 'undefined') {
		for (i = 0; i < hideOverlayObjects.length; i++) {
			if(inArray(hideOverlayObjects, document.getElementById(hideOverlayObjects[i]).id)){
				document.getElementById(hideOverlayObjects[i]).style.visibility = "hidden";
			}
		}
	}
};

/**
 *
 */
function getObjectsToHide(){
	allobjects = [];
	tohide = ["OBJECT","EMBED","SELECT"];
	for(i=0; i<tohide.length; ++i){
		objects = document.getElementsByTagName(tohide[i]);
		for (n = 0; n < objects.length; n++) {
			allobjects.push(objects[n]);
		}
	}
	return allobjects;
};

/**
 * Additional methods for Element (used for the overlay)
 */
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth;
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src;
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href;
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

/**
 * tab interface function
 *
 * param:
 * object
 * 	{
 * 	tab: clicked div /often set to "this"/
 * 	tab_id: unique id, set to content div
 * 	parent: id of parent div, where to search children - if not set, gets tab.parentNode
 *
 * e.g.
 * <div id="par">
 * 	<div onclick="activate_tab({ tab: this, parent: 'par', id: 'tab1' })">tab_link</div>
 * 	<div id="tab1">tab_content</div>
 * </div>
 */
function activate_tab(args) {
	args = args || {};
	var tab				= args.tab;
	var tab_id		= args.id;
	var children 	= ($(args.parent) || args.tab.parentNode).getElementsByTagName('div');
	for(var i=0; i < children.length; i++) {
		var item = children[i];
		// deactivate all divs
		item.className = item.className.split('-')[0];
		// activate content
		if(item.getAttribute('id') == tab_id) {
			item.className += '-active';
		}
		// if it's a tab, make 1st child image inactive
		if(item.className.indexOf('tab') != -1) {
			var image = item.getElementsByTagName('img')[0];
			if(image) {
				image.src = image.src.replace(new RegExp('\-active'), '');
			}
		}
	}
	// activate clicked tab
	tab.className += '-active';
	// activate 1st image child of clicked tab
	var image = tab.getElementsByTagName('img')[0];
	if(image) {
		var cache = new Image();
		cache.src = image.src.replace(new RegExp('^(.*)\.(.{3})$'), '$1-active\.$2');
		image.src = cache.src;
	}
};

/**
 *
 */
function showOverlay(){
	// hide selects & textareas
	hideObjects();
	// the overlay
	arrayPageSize = document.getPageSize();
	Element.setHeight('messageOverlay', arrayPageSize[1]);
	// the message container
	arrayPageScroll = document.getPageScroll();
	messageTop = arrayPageScroll[1] + (arrayPageSize[3] / 4);
	Element.setTop('messageContainer', messageTop);
	new Effect.Appear('messageOverlay', { duration: 0.2, from: 0.0, to: 0.35 });
	if($('messageContainer').innerHTML)
		new Effect.Appear('messageContainer', { duration: 0.2, from: 0.0, to: 1.0 });
	return false;
};

/**
 *
 */
function hideOverlay(){
	mContainer = $('messageContainer');
	mContainer.style.display = 'none';
	mContainer.innerHTML = '';
	new Effect.Fade('messageOverlay', { duration: 0.2});
	showObjects();
};

/**
 *
 */
function showCBox(){
	arrayPageSize = document.getPageSize();
	Element.setHeight('messageOverlay', arrayPageSize[1]);
	arrayPageScroll = document.getPageScroll();
	messageTop = arrayPageScroll[1] + (arrayPageSize[3] / 4);
	Element.setTop('messageContainer', messageTop);
	new Effect.Appear('messageOverlay', { duration: 0.2, from: 0.0, to: 0.35 });
	new Effect.Appear('messageContainer', { duration: 0.2, from: 0.0, to: 1.0 });
	return false;
};

/**
 *
 */
function hideCBox(){
	mContainer = $('messageContainer');
	mContainer.style.display = 'none';
	mContainer.innerHTML = '';
	new Effect.Fade('messageOverlay', { duration: 0.2});
	showObjects();
	return false;
};

/**
 *
 */
function cAlert(msg){
	$('messageContainer').innerHTML = ''+
	'<table class="calert" cellpadding="0" cellspacing="0">'+
		'<tr><td class="close"><a href="javascript://" onclick="return hideCBox();"></a></td></tr>'+
		'<tr><td class="data">'+msg+'</td></tr>'+
		'<tr><td class="actions"><a href="javascript://" onclick="return hideCBox();"></a></td></tr>'+
	'</table>';
	showCBox();
	return false;
};

/**
 *
 */
function cConfirm(msg,action){
	$('messageContainer').innerHTML = ''+
	'<table class="cconfirm" cellpadding="0" cellspacing="0">'+
		'<tr><td class="close"><a href="javascript://" onclick="return hideCBox();"></a></td></tr>'+
		'<tr><td class="data">'+msg+'</td></tr>'+
		'<tr><td class="actions"><div>'+
			'<a class="ok" href="javascript://" onclick="'+action+';return hideCBox();"></a>'+
			'<a class="cancel" href="javascript://" onclick="return hideCBox();"></a>'+
		'</div></td></tr>'+
	'</table>';
	showCBox();
	return false;
};

function test(){
 alert('There in common.js');	
}


