function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight= document.documentElement.clientHeight;
		} else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth=0;
	if (typeof(window.innerWidth)=='number') {
		windowWidth=window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth= document.documentElement.clientWidth;
		} else {
			if (document.body&&document.body.clientWidth) {
				windowWidth=document.body.clientWidth;	
			}
		}
	}
	return windowWidth;
}

// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function layout_correct() {
	Position.prepare();
	var ww = getWindowWidth()-20;
	var wh = getWindowHeight();
	//var left = Math.round(Math.max(0,(ww-1073))/2);
	var left = 0;
	$('main').style.left = left+'px';
	var i =new Image();
	i.src='index.php?service=setleft&left='+left;
	var fh = $('footer').getDimensions().height+2;
	var cbottom = Position.cumulativeOffset($('content_bottom'))[1] + $('content_bottom').getDimensions().height;
	//alert('content bottom:'+cbottom+'px, wh:'+wh+'px');
	var lbottom = Position.cumulativeOffset($('left_box2'))[1] + $('left_box2').getDimensions().height;
	var rbottom = Position.cumulativeOffset($('right_box3'))[1] + $('right_box3').getDimensions().height + 20;
	var fpos = wh-fh;
	if (fpos<cbottom) {fpos = cbottom;}
	if (fpos<lbottom) {fpos = lbottom;}
	if (fpos<rbottom) {fpos = rbottom;}
	if (fpos<400) {fpos = 400;}
	$('main').style.height = fpos+'px';
	$('left').style.height = fpos+'px';
	$('footer').style.top = fpos+'px';
	$('footer').style.width = ww+'px';
	$('footer').style.display = 'block';
}

function product_cart(op,url,product_id) {
	var pcs = $('product_pcs_'+product_id).value;
	window.location.href = url+'&cart_op='+op+'&cart_pcs='+pcs+'&cart_product='+product_id;
}

function mf_select() {
	var sel = $('manufacturer_select');
	var mf = sel.options[sel.selectedIndex].value;
	window.location.href = mf;
}

function popup(name, width, height, url) {
	win = window.open (url,
		name,"menubar=0,resizable=0,location=0,status=0,scrollbars=1,width="+width+",height="+height);
	var ww = getWindowWidth();
	var wh = getWindowHeight();
	win.moveTo((ww-width)/2,(wh-height)/2);
	win.focus();
}

function jump(url) {
	window.location.href = url;
}

function verify_email(url,msg) {
	var email = ''+$('cart_email').value;
	if ((email.length>3)&&(email.indexOf('@')!= -1)) {
		jump(url+'&email='+escape(email));
	} else {
		alert(msg);
	}
}

function search_keypress(evt,type,actual,url) {
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt) {
		var charCode = (evt.charCode) ? evt.charCode :
			((evt.keyCode) ? evt.keyCode :
			((evt.which) ? evt.which : 0));
		if (charCode == 13) {
			search_click(type,actual,url);
		}
	}
}

function service_day(url) {
	url = url + '?date='+escape($('service_date').value);
	window.location.href = url;
}

function search_click(type,actual,url) {
	var dosearch = true;
	if (type=='felni') {
		if ($(type+'_param_0')) {
			if ($(type+'_param_0').value=='-') {
				dosearch = false;
				alert ('Kérjük válasszon márkát a kereséshez!');
			}
		} else {
			dosearch = false;
			alert ('Kérjük válasszon márkát a kereséshez!');
		}
	}

	if (dosearch) {
		url = url + '?search_type='+type;
		if ($(type+'_size_w')) {
			url = url + '&size_w='+escape($(type+'_size_w').value);
		} else {
			url = url + '&size_w=-';
		}
		if ($(type+'_size_h')) {
			url = url + '&size_h='+escape($(type+'_size_h').value);
		} else {
			url = url + '&size_h=-';
		}
		url = url + '&size_d='+escape($(type+'_size_d').value);
		if (actual=='autogumi') {
			url = url + '&param_0='+escape($(type+'_param_0').value);
			url = url + '&param_1='+escape(window.mcat_value);
			chks = document.getElementsByClassName("chk_"+window.mcat_value);
			var param_1='';
			for(i = 0; i < chks.length; i++) {
				var chk = chks[i];
				if (chk.checked) {
					if (param_1 != '') {
						param_1 = param_1 + ',';
					}
					param_1 = param_1 + chk.value;
				}
			}
			url = url + '&param_2='+escape(param_1);
		} else {
			if ($(type+'_param_0')) {
				url = url + '&param_0='+escape($(type+'_param_0').value);
			} else {
				url = url + '&param_0=-';
			}
			if ($(type+'_param_1')) {
				url = url + '&param_1='+escape($(type+'_param_1').value);
			} else {
				url = url + '&param_1=-';
			}
			url = url + '&param_2=-';
		}
		window.location.href = url;
	}
}

function m_cat(mcat) {
	allNodes = document.getElementsByClassName("use_cat");
	for(i = 0; i < allNodes.length; i++) {
		allNodes[i].hide();
	}
	$('use_cats_'+mcat).show();
	window.mcat_value = mcat;
}

function smode_change() {
	var smode = getCheckedValue(document.forms['checkout'].elements['checkout[smode]']);
	//var smode_obj = $('checkout_smode');
	//var smode = smode_obj.options[smode_obj.selectedIndex].value;
	if (saddress[smode]) {
		$('checkout_group_saddress').show();
		$('address_copy').show();
	} else {
		$('checkout_group_saddress').hide();
		$('address_copy').hide();
	}
	//if ($('checkout_sbill').checked) {
		$('checkout_group_baddress').show();
	//} else {
		//$('checkout_group_baddress').hide();
	//}
	footer_correct();
}

function sbill_change() {
	smode_change()
}

function baddress_copy() {
	$('checkout_bzip').value = $('checkout_szip').value;
	$('checkout_bcity').value = $('checkout_scity').value;
	$('checkout_bstreet').value = $('checkout_sstreet').value;
}


function menu_open(id,max) {
	for (i=0;i<max;i++) {
		if ((i==id)) {
			if (!$("submenu_"+i).visible()) {
				$("submenu_"+i).style.display='block';
				Effect.BlindDown("submenu_"+i,{duration:0.33});
			}
		} else {
			if ($("submenu_"+i).visible()) {
				//Effect.SwitchOff("submenu_"+i,{duration:0.1});
				$("submenu_"+i).hide();
			}
		}
	}
	layout_correct();
}

function init_correction() {
	layout_correct();
	window.onresize = layout_correct;
	$('content').onresize = layout_correct;
}


